row_iterator.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005 Tommi Maekitalo
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * As a special exception, you may use this file as part of a free
10  * software library without restriction. Specifically, if other files
11  * instantiate templates or use macros or inline functions from this
12  * file, or you compile this file and link it with other files to
13  * produce an executable, this file does not by itself cause the
14  * resulting executable to be covered by the GNU General Public
15  * License. This exception does not however invalidate any other
16  * reasons why the executable file might be covered by the GNU Library
17  * General Public License.
18  *
19  * This library is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22  * Lesser General Public License for more details.
23  *
24  * You should have received a copy of the GNU Lesser General Public
25  * License along with this library; if not, write to the Free Software
26  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27  */
28 
29 #ifndef TNTDB_BITS_ROW_ITERATOR_H
30 #define TNTDB_BITS_ROW_ITERATOR_H
31 
32 #include <tntdb/bits/row.h>
33 #include <tntdb/bits/value.h>
34 
35 namespace tntdb
36 {
38  : public std::iterator<std::random_access_iterator_tag, Value>
39  {
40  public:
41  typedef const value_type& const_reference;
42  typedef const value_type* const_pointer;
43 
44  private:
45  const IRow* row;
46  size_type offset;
47  Value current;
48 
49  void setOffset(size_type off)
50  {
51  if (off != offset)
52  {
53  offset = off;
54  if (offset < row->size())
55  current = Value(row->getValueByNumber(offset));
56  }
57  }
58 
59  public:
60  const_iterator(const Row& row_, size_type offset_)
61  : row(row_.getImpl()),
62  offset(offset_)
63  {
64  if (offset < row->size())
65  current = Value(row->getValueByNumber(offset));
66  }
67 
68  bool operator== (const const_iterator& it) const
69  { return offset == it.offset; }
70 
71  bool operator!= (const const_iterator& it) const
72  { return !operator== (it); }
73 
75  { setOffset(offset + 1); return *this; }
76 
78  {
79  const_iterator ret = *this;
80  setOffset(offset + 1);
81  return ret;
82  }
83 
85  { setOffset(offset - 1); return *this; }
86 
88  {
89  const_iterator ret = *this;
90  setOffset(offset - 1);
91  return ret;
92  }
93 
95  { return current; }
96 
98  { return &current; }
99 
100  const_iterator& operator+= (difference_type n)
101  {
102  setOffset(offset + n);
103  return *this;
104  }
105 
106  const_iterator operator+ (difference_type n) const
107  {
108  const_iterator it(*this);
109  it += n;
110  return it;
111  }
112 
113  const_iterator& operator-= (difference_type n)
114  {
115  setOffset(offset - n);
116  return *this;
117  }
118 
119  const_iterator operator- (difference_type n) const
120  {
121  const_iterator it(*this);
122  it -= n;
123  return it;
124  }
125 
126  difference_type operator- (const const_iterator& it) const
127  { return offset - it.offset; }
128  };
129 }
130 
131 #endif // TNTDB_BITS_ROW_ITERATOR_H
132