Stxxl  1.3.2
iterator_map.h
1 /***************************************************************************
2  * include/stxxl/bits/containers/btree/iterator_map.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_MAP_H
14 #define STXXL_CONTAINERS_BTREE__ITERATOR_MAP_H
15 
16 #include <map>
17 
18 #include <stxxl/bits/noncopyable.h>
19 #include <stxxl/bits/containers/btree/iterator.h>
20 #include <stxxl/bits/common/error_handling.h>
21 
22 
23 __STXXL_BEGIN_NAMESPACE
24 
25 namespace btree
26 {
27  template <class BTreeType>
28  class iterator_map : private noncopyable
29  {
30  public:
31  typedef BTreeType btree_type;
32  typedef typename btree_type::leaf_bid_type bid_type;
33  typedef btree_iterator_base<btree_type> iterator_base;
34 
35  private:
36  struct Key
37  {
38  bid_type bid;
39  unsigned pos;
40  Key() { }
41  Key(const bid_type & b, unsigned p) : bid(b), pos(p) { }
42  };
43 
44  struct bid_comp
45  {
46  bool operator () (const bid_type & a, const bid_type & b) const
47  {
48  return (a.storage < b.storage) || (a.storage == b.storage && a.offset < b.offset);
49  }
50  };
51  struct KeyCmp
52  {
53  bid_comp BIDComp;
54  bool operator () (const Key & a, const Key & b) const
55  {
56  return BIDComp(a.bid, b.bid) || (a.bid == b.bid && a.pos < b.pos);
57  }
58  };
59 
60  typedef std::multimap<Key, iterator_base *, KeyCmp> multimap_type;
61 
62  multimap_type It2Addr_;
63  btree_type * btree_;
64 
65  typedef typename multimap_type::value_type pair_type;
66  typedef typename multimap_type::iterator mmiterator_type;
67  typedef typename multimap_type::const_iterator mmconst_iterator_type;
68 
69 
70  // changes btree pointer in all contained iterators
71  void change_btree_pointers(btree_type * b)
72  {
73  mmconst_iterator_type it = It2Addr_.begin();
74  for ( ; it != It2Addr_.end(); ++it)
75  {
76  (it->second)->btree_ = b;
77  }
78  }
79 
80  public:
81  iterator_map(btree_type * b) : btree_(b)
82  { }
83 
84  void register_iterator(iterator_base & it)
85  {
86  STXXL_VERBOSE2("btree::iterator_map register_iterator addr=" << &it <<
87  " BID: " << it.bid << " POS: " << it.pos);
88  It2Addr_.insert(pair_type(Key(it.bid, it.pos), &it));
89  }
90  void unregister_iterator(iterator_base & it)
91  {
92  STXXL_VERBOSE2("btree::iterator_map unregister_iterator addr=" << &it <<
93  " BID: " << it.bid << " POS: " << it.pos);
94  assert(!It2Addr_.empty());
95  Key key(it.bid, it.pos);
96  std::pair<mmiterator_type, mmiterator_type> range =
97  It2Addr_.equal_range(key);
98 
99  assert(range.first != range.second);
100 
101  mmiterator_type i = range.first;
102  for ( ; i != range.second; ++i)
103  {
104  assert(it.bid == (*i).first.bid);
105  assert(it.pos == (*i).first.pos);
106 
107  if ((*i).second == &it)
108  {
109  // found it
110  It2Addr_.erase(i);
111  return;
112  }
113  }
114 
115  STXXL_THROW(std::runtime_error, "unregister_iterator", "Panic in btree::iterator_map, can not find and unregister iterator");
116  }
117  template <class OutputContainer>
118  void find(const bid_type & bid,
119  unsigned first_pos,
120  unsigned last_pos,
121  OutputContainer & out
122  )
123  {
124  Key firstkey(bid, first_pos);
125  Key lastkey(bid, last_pos);
126  mmconst_iterator_type begin = It2Addr_.lower_bound(firstkey);
127  mmconst_iterator_type end = It2Addr_.upper_bound(lastkey);
128 
129  mmconst_iterator_type i = begin;
130  for ( ; i != end; ++i)
131  {
132  assert(bid == (*i).first.bid);
133  out.push_back((*i).second);
134  }
135  }
136 
137  virtual ~iterator_map()
138  {
139  mmconst_iterator_type it = It2Addr_.begin();
140  for ( ; it != It2Addr_.end(); ++it)
141  (it->second)->make_invalid();
142  }
143 
144  void swap(iterator_map & obj)
145  {
146  std::swap(It2Addr_, obj.It2Addr_);
147  change_btree_pointers(btree_);
148  obj.change_btree_pointers(obj.btree_);
149  }
150  };
151 }
152 
153 __STXXL_END_NAMESPACE
154 
155 
156 namespace std
157 {
158  template <class BTreeType>
159  void swap(stxxl::btree::iterator_map<BTreeType> & a,
160  stxxl::btree::iterator_map<BTreeType> & b)
161  {
162  a.swap(b);
163  }
164 }
165 
166 #endif /* STXXL_CONTAINERS_BTREE__ITERATOR_MAP_H */
_ExtIterator find(_ExtIterator _begin, _ExtIterator _end, const _EqualityComparable &_value, int_type nbuffers)
External equivalent of std::find.
Definition: scan.h:215