STXXL  1.4.0
 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>
19 
20 
22 
23 
24 namespace btree {
25 
26 template <class BTreeType>
28 template <class BTreeType>
30 template <class BTreeType>
32 template <class KeyType_, class DataType_, class KeyCmp_, 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  friend class iterator_map<btree_type>;
48  template <class KeyType_, class DataType_,
49  class KeyCmp_, unsigned LogNElem_, class BTreeType__>
50  friend class normal_leaf;
51 
52  template <class BTreeType_>
53  friend bool operator == (const btree_iterator<BTreeType_>& a,
55  template <class BTreeType_>
56  friend bool operator != (const btree_iterator<BTreeType_>& a,
58 
59 protected:
60  btree_type* btree_;
62  unsigned pos;
63 
65  {
66  STXXL_VERBOSE3("btree_iterator_base def construct addr=" << this);
67  make_invalid();
68  }
69 
71  btree_type* btree__,
72  const bid_type& b,
73  unsigned p
74  ) : btree_(btree__), bid(b), pos(p)
75  {
76  STXXL_VERBOSE3("btree_iterator_base parameter construct addr=" << this);
77  btree_->iterator_map_.register_iterator(*this);
78  }
79 
80  void make_invalid()
81  {
82  btree_ = NULL;
83  pos = 0;
84  }
85 
87  {
88  STXXL_VERBOSE3("btree_iterator_base constr from" << (&obj) << " to " << this);
89  btree_ = obj.btree_;
90  bid = obj.bid;
91  pos = obj.pos;
92  if (btree_)
93  btree_->iterator_map_.register_iterator(*this);
94  }
95 
96  btree_iterator_base& operator = (const btree_iterator_base& obj)
97  {
98  STXXL_VERBOSE3("btree_iterator_base copy from" << (&obj) << " to " << this);
99  if (&obj != this)
100  {
101  if (btree_)
102  btree_->iterator_map_.unregister_iterator(*this);
103 
104  btree_ = obj.btree_;
105  bid = obj.bid;
106  pos = obj.pos;
107  if (btree_)
108  btree_->iterator_map_.register_iterator(*this);
109  }
110  return *this;
111  }
112 
114  {
115  assert(btree_);
116  typename btree_type::leaf_type * Leaf = btree_->leaf_cache_.get_node(bid);
117  assert(Leaf);
118  return (reference)((*Leaf)[pos]);
119  }
120 
122  {
123  assert(btree_);
124  typename btree_type::leaf_type const* Leaf = btree_->leaf_cache_.get_const_node(bid);
125  assert(Leaf);
126  return (reference)((*Leaf)[pos]);
127  }
128 
129  bool operator == (const btree_iterator_base& obj) const
130  {
131  return bid == obj.bid && pos == obj.pos && btree_ == obj.btree_;
132  }
133 
134  bool operator != (const btree_iterator_base& obj) const
135  {
136  return bid != obj.bid || pos != obj.pos || btree_ != obj.btree_;
137  }
138 
140  {
141  assert(btree_);
142  bid_type cur_bid = bid;
143  typename btree_type::leaf_type const* Leaf = btree_->leaf_cache_.get_const_node(bid, true);
144  assert(Leaf);
145  Leaf->increment_iterator(*this);
146  btree_->leaf_cache_.unfix_node(cur_bid);
147  return *this;
148  }
149 
151  {
152  assert(btree_);
153  bid_type cur_bid = bid;
154  typename btree_type::leaf_type const* Leaf = btree_->leaf_cache_.get_const_node(bid, true);
155  assert(Leaf);
156  Leaf->decrement_iterator(*this);
157  btree_->leaf_cache_.unfix_node(cur_bid);
158  return *this;
159  }
160 
161 public:
163  {
164  STXXL_VERBOSE3("btree_iterator_base deconst " << this);
165  if (btree_)
166  btree_->iterator_map_.unregister_iterator(*this);
167  }
168 };
169 
170 template <class BTreeType>
171 class btree_iterator : public btree_iterator_base<BTreeType>
172 {
173 public:
174  typedef BTreeType btree_type;
175  typedef typename btree_type::leaf_bid_type bid_type;
176  typedef typename btree_type::value_type value_type;
177  typedef typename btree_type::reference reference;
178  typedef typename btree_type::const_reference const_reference;
179  typedef typename btree_type::pointer pointer;
180 
181  template <class KeyType_, class DataType_,
182  class KeyCmp_, unsigned LogNElem_, class BTreeType__>
183  friend class normal_leaf;
184 
186 
188  { }
189 
192  { }
193 
194  btree_iterator& operator = (const btree_iterator& obj)
195  {
197  return *this;
198  }
199 
200  reference operator * ()
201  {
202  return non_const_access();
203  }
204 
205  pointer operator -> ()
206  {
207  return &(non_const_access());
208  }
209 
210  bool operator == (const btree_iterator& obj) const
211  {
213  }
214 
215  bool operator != (const btree_iterator& obj) const
216  {
218  }
219 
221  {
222  assert(*this != btree_iterator_base<btree_type>::btree_->end());
224  return *this;
225  }
226 
228  {
230  return *this;
231  }
232 
234  {
235  assert(*this != btree_iterator_base<btree_type>::btree_->end());
236  btree_iterator result(*this);
238  return result;
239  }
240 
242  {
243  btree_iterator result(*this);
245  return result;
246  }
247 
248 private:
250  btree_type* btree__,
251  const bid_type& b,
252  unsigned p
253  ) : btree_iterator_base<btree_type>(btree__, b, p)
254  { }
255 };
256 
257 template <class BTreeType>
258 class btree_const_iterator : public btree_iterator_base<BTreeType>
259 {
260 public:
262 
263  typedef BTreeType btree_type;
264  typedef typename btree_type::leaf_bid_type bid_type;
265  typedef typename btree_type::value_type value_type;
266  typedef typename btree_type::const_reference reference;
267  typedef typename btree_type::const_pointer pointer;
268 
269  template <class KeyType_, class DataType_,
270  class KeyCmp_, unsigned LogNElem_, class BTreeType__>
271  friend class normal_leaf;
272 
274 
276  { }
277 
280  { }
281 
284  { }
285 
287  {
289  return *this;
290  }
291 
292  reference operator * ()
293  {
294  return const_access();
295  }
296 
297  pointer operator -> ()
298  {
299  return &(const_access());
300  }
301 
302 
303  bool operator == (const iterator& obj) const
304  {
306  }
307 
308  bool operator != (const iterator& obj) const
309  {
311  }
312 
313  bool operator == (const btree_const_iterator& obj) const
314  {
316  }
317 
318  bool operator != (const btree_const_iterator& obj) const
319  {
321  }
322 
324  {
325  assert(*this != btree_iterator_base<btree_type>::btree_->end());
327  return *this;
328  }
329 
331  {
333  return *this;
334  }
335 
337  {
338  assert(*this != btree_iterator_base<btree_type>::btree_->end());
339  btree_const_iterator result(*this);
341  return result;
342  }
343 
345  {
346  btree_const_iterator result(*this);
348  return result;
349  }
350 
351 private:
353  btree_type* btree__,
354  const bid_type& b,
355  unsigned p
356  ) : btree_iterator_base<btree_type>(btree__, b, p)
357  { }
358 };
359 
360 template <class BTreeType>
363 {
364  return a.btree_iterator_base<BTreeType>::operator == (b);
365 }
366 
367 template <class BTreeType>
370 {
371  return a.btree_iterator_base<BTreeType>::operator != (b);
372 }
373 
374 } // namespace btree
375 
377 
378 #endif // !STXXL_CONTAINERS_BTREE_ITERATOR_HEADER
btree_type::const_pointer pointer
Definition: iterator.h:267
btree_type::pointer pointer
Definition: iterator.h:179
btree_iterator(btree_type *btree__, const bid_type &b, unsigned p)
Definition: iterator.h:249
btree_type::reference reference
Definition: iterator.h:177
btree_type::value_type value_type
Definition: iterator.h:265
std::bidirectional_iterator_tag iterator_category
Definition: iterator.h:44
btree_type::leaf_bid_type bid_type
Definition: iterator.h:40
btree_type::leaf_bid_type bid_type
Definition: iterator.h:175
btree_type::const_reference const_reference
Definition: iterator.h:178
btree_const_iterator(const btree_const_iterator &obj)
Definition: iterator.h:278
uint_pair & operator++()
prefix increment operator (directly manipulates the integer parts)
Definition: uint_types.h:166
btree_const_iterator(const iterator &obj)
Definition: iterator.h:282
bool operator!=(const uint_pair &b) const
inequality checking operator
Definition: uint_types.h:201
#define STXXL_VERBOSE3(x)
Definition: verbose.h:113
btree_type::difference_type difference_type
Definition: iterator.h:45
btree_type::value_type value_type
Definition: iterator.h:176
btree_iterator_base(btree_type *btree__, const bid_type &b, unsigned p)
Definition: iterator.h:70
#define STXXL_BEGIN_NAMESPACE
Definition: namespace.h:16
uint_pair & operator--()
prefix decrement operator (directly manipulates the integer parts)
Definition: uint_types.h:176
btree_type::value_type value_type
Definition: iterator.h:41
btree_iterator_base & decrement()
Definition: iterator.h:150
btree_iterator(const btree_iterator &obj)
Definition: iterator.h:190
btree_iterator< BTreeType > iterator
Definition: iterator.h:261
btree_type::const_reference reference
Definition: iterator.h:266
btree_type::const_reference const_reference
Definition: iterator.h:43
const_reference const_access() const
Definition: iterator.h:121
btree_const_iterator(btree_type *btree__, const bid_type &b, unsigned p)
Definition: iterator.h:352
btree_type::reference reference
Definition: iterator.h:42
btree_iterator_base & increment()
Definition: iterator.h:139
bool operator==(const uint_pair &b) const
equality checking operator
Definition: uint_types.h:195
#define STXXL_END_NAMESPACE
Definition: namespace.h:17
btree_type::leaf_bid_type bid_type
Definition: iterator.h:264
btree_iterator_base(const btree_iterator_base &obj)
Definition: iterator.h:86