STXXL  1.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
iterator_map.h
Go to the documentation of this file.
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_HEADER
14 #define STXXL_CONTAINERS_BTREE_ITERATOR_MAP_HEADER
15 
16 #include <map>
17 
18 #include <stxxl/bits/noncopyable.h>
21 
22 
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;
34 
35 private:
36  struct Key
37  {
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  {
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 
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 
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  }
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_THROW2(std::runtime_error, "btree::iterator_map::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 } // namespace btree
153 
155 
156 
157 namespace std {
158 
159 template <class BTreeType>
162 {
163  a.swap(b);
164 }
165 
166 } // namespace std
167 
168 #endif // !STXXL_CONTAINERS_BTREE_ITERATOR_MAP_HEADER
multimap_type::value_type pair_type
Definition: iterator_map.h:65
#define STXXL_VERBOSE2(x)
Definition: verbose.h:107
multimap_type::const_iterator mmconst_iterator_type
Definition: iterator_map.h:67
void swap(iterator_map &obj)
Definition: iterator_map.h:144
Key(const bid_type &b, unsigned p)
Definition: iterator_map.h:41
multimap_type::iterator mmiterator_type
Definition: iterator_map.h:66
iterator_map(btree_type *b)
Definition: iterator_map.h:81
void unregister_iterator(iterator_base &it)
Definition: iterator_map.h:90
#define STXXL_BEGIN_NAMESPACE
Definition: namespace.h:16
std::multimap< Key, iterator_base *, KeyCmp > multimap_type
Definition: iterator_map.h:60
#define STXXL_THROW2(exception_type, location, error_message)
Throws exception_type with &quot;Error in [location] : [error_message]&quot;.
btree_iterator_base< btree_type > iterator_base
Definition: iterator_map.h:33
btree_type::leaf_bid_type bid_type
Definition: iterator_map.h:32
void change_btree_pointers(btree_type *b)
Definition: iterator_map.h:71
void register_iterator(iterator_base &it)
Definition: iterator_map.h:84
void find(const bid_type &bid, unsigned first_pos, unsigned last_pos, OutputContainer &out)
Definition: iterator_map.h:118
#define STXXL_END_NAMESPACE
Definition: namespace.h:17