STXXL  1.4-dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
iterator.h
Go to the documentation of this file.
1 /***************************************************************************
2  * include/stxxl/bits/containers/btree/iterator.h
3  *
4  * Part of the STXXL. See http://stxxl.sourceforge.net
5  *
6  * Copyright (C) 2006 Roman Dementiev <[email protected]>
7  *
8  * Distributed under the Boost Software License, Version 1.0.
9  * (See accompanying file LICENSE_1_0.txt or copy at
10  * http://www.boost.org/LICENSE_1_0.txt)
11  **************************************************************************/
12 
13 #ifndef STXXL_CONTAINERS_BTREE_ITERATOR_HEADER
14 #define STXXL_CONTAINERS_BTREE_ITERATOR_HEADER
15 
16 #include <iterator>
17 #include <cassert>
18 #include <stxxl/bits/verbose.h>
20 
22 
23 namespace btree {
24 
25 template <class BTreeType>
27 template <class BTreeType>
29 template <class BTreeType>
31 template <class KeyType, class DataType, class KeyCmp,
32  unsigned LogNElem, class BTreeType>
34 
35 template <class BTreeType>
37 {
38 public:
39  typedef BTreeType btree_type;
40  typedef typename btree_type::leaf_bid_type bid_type;
41  typedef typename btree_type::value_type value_type;
42  typedef typename btree_type::reference reference;
43  typedef typename btree_type::const_reference const_reference;
44  typedef std::bidirectional_iterator_tag iterator_category;
45  typedef typename btree_type::difference_type difference_type;
46 
47  typedef typename btree_type::leaf_type leaf_type;
48 
49  friend class iterator_map<btree_type>;
50  template <class KeyType, class DataType,
51  class KeyCmp, unsigned LogNElem, class AnyBTreeType>
52  friend class normal_leaf;
53 
54  template <class AnyBTreeType>
55  friend bool operator == (const btree_iterator<AnyBTreeType>& a,
57  template <class AnyBTreeType>
58  friend bool operator != (const btree_iterator<AnyBTreeType>& a,
60 
61 protected:
62  btree_type* btree;
65 
67  {
68  STXXL_VERBOSE3("btree_iterator_base def construct addr=" << this);
69  make_invalid();
70  }
71 
72  btree_iterator_base(btree_type* _btree,
73  const bid_type& _bid,
74  unsigned_type _pos)
75  : btree(_btree), bid(_bid), pos(_pos)
76  {
77  STXXL_VERBOSE3("btree_iterator_base parameter construct addr=" << this);
79  }
80 
81  void make_invalid()
82  {
83  btree = NULL;
84  pos = 0;
85  }
86 
88  {
89  STXXL_VERBOSE3("btree_iterator_base constr from" << (&obj) << " to " << this);
90  btree = obj.btree;
91  bid = obj.bid;
92  pos = obj.pos;
93  if (btree)
95  }
96 
97  btree_iterator_base& operator = (const btree_iterator_base& obj)
98  {
99  STXXL_VERBOSE3("btree_iterator_base copy from" << (&obj) << " to " << this);
100  if (&obj != this)
101  {
102  if (btree)
104 
105  btree = obj.btree;
106  bid = obj.bid;
107  pos = obj.pos;
108  if (btree)
110  }
111  return *this;
112  }
113 
115  {
116  assert(btree);
117  leaf_type* leaf = btree->m_leaf_cache.get_node(bid);
118  assert(leaf);
119  return (reference)((*leaf)[pos]);
120  }
121 
123  {
124  assert(btree);
125  leaf_type const* leaf = btree->m_leaf_cache.get_const_node(bid);
126  assert(leaf);
127  return (reference)((*leaf)[pos]);
128  }
129 
130  bool operator == (const btree_iterator_base& obj) const
131  {
132  return bid == obj.bid && pos == obj.pos && btree == obj.btree;
133  }
134 
135  bool operator != (const btree_iterator_base& obj) const
136  {
137  return bid != obj.bid || pos != obj.pos || btree != obj.btree;
138  }
139 
141  {
142  assert(btree);
143  bid_type cur_bid = bid;
144  const leaf_type* leaf = btree->m_leaf_cache.get_const_node(bid, true);
145  assert(leaf);
146  leaf->increment_iterator(*this);
147  btree->m_leaf_cache.unfix_node(cur_bid);
148  return *this;
149  }
150 
152  {
153  assert(btree);
154  bid_type cur_bid = bid;
155  const leaf_type* leaf = btree->m_leaf_cache.get_const_node(bid, true);
156  assert(leaf);
157  leaf->decrement_iterator(*this);
158  btree->m_leaf_cache.unfix_node(cur_bid);
159  return *this;
160  }
161 
162 public:
164  {
165  STXXL_VERBOSE3("btree_iterator_base deconst " << this);
166  if (btree)
168  }
169 };
170 
171 template <class BTreeType>
172 class btree_iterator : public btree_iterator_base<BTreeType>
173 {
174 public:
175  typedef BTreeType btree_type;
176  typedef typename btree_type::leaf_bid_type bid_type;
177  typedef typename btree_type::value_type value_type;
178  typedef typename btree_type::reference reference;
179  typedef typename btree_type::const_reference const_reference;
180  typedef typename btree_type::pointer pointer;
181 
183 
184  template <class KeyType, class DataType,
185  class KeyCmp, unsigned LogNElem, class AnyBTreeType>
186  friend class normal_leaf;
187 
188  using base_type::non_const_access;
189 
191  : base_type()
192  { }
193 
195  : base_type(obj)
196  { }
197 
198  btree_iterator& operator = (const btree_iterator& obj)
199  {
200  base_type::operator = (obj);
201  return *this;
202  }
203 
204  reference operator * ()
205  {
206  return non_const_access();
207  }
208 
209  pointer operator -> ()
210  {
211  return &(non_const_access());
212  }
213 
214  bool operator == (const btree_iterator& obj) const
215  {
216  return base_type::operator == (obj);
217  }
218 
219  bool operator != (const btree_iterator& obj) const
220  {
221  return base_type::operator != (obj);
222  }
223 
225  {
226  assert(*this != base_type::btree->end());
227  base_type::increment();
228  return *this;
229  }
230 
232  {
233  base_type::decrement();
234  return *this;
235  }
236 
238  {
239  assert(*this != base_type::btree->end());
240  btree_iterator result(*this);
241  base_type::increment();
242  return result;
243  }
244 
246  {
247  btree_iterator result(*this);
248  base_type::decrement();
249  return result;
250  }
251 
252 private:
254  const bid_type& _bid,
255  unsigned_type _pos)
256  : base_type(_btree, _bid, _pos)
257  { }
258 };
259 
260 template <class BTreeType>
261 class btree_const_iterator : public btree_iterator_base<BTreeType>
262 {
263 public:
265 
266  typedef BTreeType btree_type;
267  typedef typename btree_type::leaf_bid_type bid_type;
268  typedef typename btree_type::value_type value_type;
269  typedef typename btree_type::const_reference reference;
270  typedef typename btree_type::const_pointer pointer;
271 
273 
274  template <class KeyType, class DataType,
275  class KeyCmp, unsigned LogNElem, class AnyBTreeType>
276  friend class normal_leaf;
277 
278  using base_type::const_access;
279 
281  : base_type()
282  { }
283 
285  : base_type(obj)
286  { }
287 
289  : base_type(obj)
290  { }
291 
293  {
294  base_type::operator = (obj);
295  return *this;
296  }
297 
298  reference operator * ()
299  {
300  return const_access();
301  }
302 
303  pointer operator -> ()
304  {
305  return &(const_access());
306  }
307 
308  bool operator == (const iterator& obj) const
309  {
310  return base_type::operator == (obj);
311  }
312 
313  bool operator != (const iterator& obj) const
314  {
315  return base_type::operator != (obj);
316  }
317 
318  bool operator == (const btree_const_iterator& obj) const
319  {
320  return base_type::operator == (obj);
321  }
322 
323  bool operator != (const btree_const_iterator& obj) const
324  {
325  return base_type::operator != (obj);
326  }
327 
329  {
330  assert(*this != base_type::btree->end());
331  base_type::increment();
332  return *this;
333  }
334 
336  {
337  base_type::decrement();
338  return *this;
339  }
340 
342  {
343  assert(*this != base_type::btree->end());
344  btree_const_iterator result(*this);
345  base_type::increment();
346  return result;
347  }
348 
350  {
351  btree_const_iterator result(*this);
352  base_type::decrement();
353  return result;
354  }
355 
356 private:
358  const bid_type& _bid,
359  unsigned_type _pos)
360  : base_type(_btree, _bid, _pos)
361  { }
362 };
363 
364 template <class BTreeType>
367 {
368  return a.btree_iterator_base<BTreeType>::operator == (b);
369 }
370 
371 template <class BTreeType>
374 {
375  return a.btree_iterator_base<BTreeType>::operator != (b);
376 }
377 
378 } // namespace btree
379 
381 
382 #endif // !STXXL_CONTAINERS_BTREE_ITERATOR_HEADER
btree_type::const_pointer pointer
Definition: iterator.h:270
btree_type::pointer pointer
Definition: iterator.h:180
iterator_map_type m_iterator_map
Definition: btree.h:91
btree_type::reference reference
Definition: iterator.h:178
btree_type::value_type value_type
Definition: iterator.h:268
leaf_cache_type m_leaf_cache
Definition: btree.h:90
node_type const * get_const_node(const bid_type &bid, bool fix=false)
Definition: node_cache.h:358
std::bidirectional_iterator_tag iterator_category
Definition: iterator.h:44
btree_iterator_base< btree_type > base_type
Definition: iterator.h:272
btree_type::leaf_bid_type bid_type
Definition: iterator.h:40
btree_type::leaf_bid_type bid_type
Definition: iterator.h:176
btree_type::const_reference const_reference
Definition: iterator.h:179
btree_const_iterator(const btree_const_iterator &obj)
Definition: iterator.h:284
uint_pair & operator++()
prefix increment operator (directly manipulates the integer parts)
Definition: uint_types.h:163
void unfix_node(const bid_type &bid)
Definition: node_cache.h:553
btree_const_iterator(const iterator &obj)
Definition: iterator.h:288
bool operator!=(const uint_pair &b) const
inequality checking operator
Definition: uint_types.h:198
btree_const_iterator(btree_type *_btree, const bid_type &_bid, unsigned_type _pos)
Definition: iterator.h:357
#define STXXL_VERBOSE3(x)
Definition: verbose.h:127
btree_type::difference_type difference_type
Definition: iterator.h:45
btree_type::value_type value_type
Definition: iterator.h:177
void unregister_iterator(iterator_base &it)
Definition: iterator_map.h:90
#define STXXL_BEGIN_NAMESPACE
Definition: namespace.h:16
uint_pair & operator--()
prefix decrement operator (directly manipulates the integer parts)
Definition: uint_types.h:173
btree_type::value_type value_type
Definition: iterator.h:41
btree_iterator_base & decrement()
Definition: iterator.h:151
btree_iterator(const btree_iterator &obj)
Definition: iterator.h:194
btree_iterator< BTreeType > iterator
Definition: iterator.h:264
btree_iterator_base(btree_type *_btree, const bid_type &_bid, unsigned_type _pos)
Definition: iterator.h:72
btree_type::const_reference reference
Definition: iterator.h:269
choose_int_types< my_pointer_size >::unsigned_type unsigned_type
Definition: types.h:64
btree_type::const_reference const_reference
Definition: iterator.h:43
const_reference const_access() const
Definition: iterator.h:122
btree_iterator(btree_type *_btree, const bid_type &_bid, unsigned_type _pos)
Definition: iterator.h:253
void register_iterator(iterator_base &it)
Definition: iterator_map.h:84
btree_iterator_base< btree_type > base_type
Definition: iterator.h:182
btree_type::reference reference
Definition: iterator.h:42
btree_iterator_base & increment()
Definition: iterator.h:140
node_type * get_node(const bid_type &bid, bool fix=false)
Definition: node_cache.h:264
btree_type::leaf_type leaf_type
Definition: iterator.h:47
bool operator==(const uint_pair &b) const
equality checking operator
Definition: uint_types.h:192
#define STXXL_END_NAMESPACE
Definition: namespace.h:17
btree_type::leaf_bid_type bid_type
Definition: iterator.h:267
btree_iterator_base(const btree_iterator_base &obj)
Definition: iterator.h:87