STXXL  1.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
map.h
Go to the documentation of this file.
1 /***************************************************************************
2  * include/stxxl/bits/containers/map.h
3  *
4  * Part of the STXXL. See http://stxxl.sourceforge.net
5  *
6  * Copyright (C) 2006 Roman Dementiev <[email protected]>
7  * Copyright (C) 2008, 2009 Andreas Beckmann <[email protected]>
8  *
9  * Distributed under the Boost Software License, Version 1.0.
10  * (See accompanying file LICENSE_1_0.txt or copy at
11  * http://www.boost.org/LICENSE_1_0.txt)
12  **************************************************************************/
13 
14 #ifndef STXXL_CONTAINERS_MAP_HEADER
15 #define STXXL_CONTAINERS_MAP_HEADER
16 
17 #include <stxxl/bits/noncopyable.h>
19 
20 
22 
23 namespace btree {
24 
25 template <class KeyType,
26  class DataType,
27  class CompareType,
28  unsigned LogNodeSize,
29  unsigned LogLeafSize,
30  class PDAllocStrategy
31  >
32 class btree;
33 
34 } // namespace btree
35 
36 //! \addtogroup stlcont
37 //! \{
38 
39 //! External associative container (map). \n
40 //! <b> Introduction </b> to map container: see \ref tutorial_map tutorial. \n
41 //! <b> Design and Internals </b> of map container: see \ref design_map
42 //!
43 //! \tparam KeyType key type (POD with no references to internal memory)
44 //! \tparam DataType data type (POD with no references to internal memory)
45 //! \tparam CompareType comparison type used to determine
46 //! whether a key is smaller than another one.
47 //! If CompareType()(x,y) is true, then x is smaller than y.
48 //! CompareType must also provide a static \c max_value method, that returns
49 //! a value of type KeyType that is
50 //! larger than any key stored in map : i.e. for all \b x in map holds
51 //! CompareType()(x,CompareType::max_value())
52 //!
53 //! <BR>
54 //! Example: :
55 //! \verbatim
56 //! struct CmpIntGreater
57 //! {
58 //! bool operator () (const int & a, const int & b) const { return a>b; }
59 //! static int max_value() { return std::numeric_limits<int>::min(); }
60 //! };
61 //! \endverbatim
62 //! Another example:
63 //! \verbatim
64 //! struct CmpIntLess
65 //! {
66 //! bool operator () (const int & a, const int & b) const { return a<b; }
67 //! static int max_value() const { return std::numeric_limits<int>::max(); }
68 //! };
69 //! \endverbatim
70 //! Note that CompareType must define a strict weak ordering.
71 //! (<A HREF="http://www.sgi.com/tech/stl/StrictWeakOrdering.html">see what it is</A>)
72 //! \tparam RawNodeSize size of internal nodes of map in bytes (btree implementation).
73 //! \tparam RawLeafSize size of leaves of map in bytes (btree implementation).
74 //! \tparam PDAllocStrategy parallel disk allocation strategy (\c stxxl::SR is recommended and default)
75 //!
76 template <class KeyType,
77  class DataType,
78  class CompareType,
79  unsigned RawNodeSize = 16* 1024, // 16 KBytes default
80  unsigned RawLeafSize = 128* 1024, // 128 KBytes default
81  class PDAllocStrategy = stxxl::SR
82  >
83 class map : private noncopyable
84 {
86 
88 
89 public:
92 
93  typedef typename impl_type::key_type key_type;
94  typedef typename impl_type::data_type data_type;
99  typedef typename impl_type::pointer pointer;
101  typedef typename impl_type::reference reference;
103  typedef typename impl_type::size_type size_type;
105  typedef typename impl_type::iterator iterator;
107  typedef std::reverse_iterator<iterator> reverse_iterator;
108  typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
109 
110  //! \name Iterators
111  //! \{
112 
113  iterator begin() { return Impl.begin(); }
114  iterator end() { return Impl.end(); }
115  const_iterator begin() const { return Impl.begin(); }
116  const_iterator end() const { return Impl.end(); }
117  const_iterator cbegin() const { return begin(); }
118  const_iterator cend() const { return end(); }
119 
121  {
122  return reverse_iterator(end());
123  }
125  {
126  return const_reverse_iterator(end());
127  }
129  {
130  return const_reverse_iterator(end());
131  }
133  {
134  return reverse_iterator(begin());
135  }
137  {
138  return const_reverse_iterator(begin());
139  }
141  {
142  return const_reverse_iterator(begin());
143  }
144 
145  //! \}
146 
147  //! \name Capacity
148  //! \{
149 
150  size_type size() const { return Impl.size(); }
151  size_type max_size() const { return Impl.max_size(); }
152  bool empty() const { return Impl.empty(); }
153 
154  //! \}
155 
156  //! \name Observers
157  //! \{
158 
159  key_compare key_comp() const { return Impl.key_comp(); }
160  value_compare value_comp() const { return Impl.value_comp(); }
161 
162  //! \}
163 
164  //! \name Constructors/Destructors
165  //! \{
166 
167  //! A constructor
168  //! \param node_cache_size_in_bytes size of node cache in bytes (btree implementation)
169  //! \param leaf_cache_size_in_bytes size of leaf cache in bytes (btree implementation)
170  map(unsigned_type node_cache_size_in_bytes,
171  unsigned_type leaf_cache_size_in_bytes
172  ) : Impl(node_cache_size_in_bytes, leaf_cache_size_in_bytes)
173  { }
174 
175  //! A constructor
176  //! \param c_ comparator object
177  //! \param node_cache_size_in_bytes size of node cache in bytes (btree implementation)
178  //! \param leaf_cache_size_in_bytes size of leaf cache in bytes (btree implementation)
179  map(const key_compare& c_,
180  unsigned_type node_cache_size_in_bytes,
181  unsigned_type leaf_cache_size_in_bytes
182  ) : Impl(c_, node_cache_size_in_bytes, leaf_cache_size_in_bytes)
183  { }
184 
185  //! Constructs a map from a given input range
186  //! \param b beginning of the range
187  //! \param e end of the range
188  //! \param node_cache_size_in_bytes size of node cache in bytes (btree implementation)
189  //! \param leaf_cache_size_in_bytes size of leaf cache in bytes (btree implementation)
190  //! \param range_sorted if \c true than the constructor assumes that the range is sorted
191  //! and performs a fast bottom-up bulk construction of the map (btree implementation)
192  //! \param node_fill_factor node fill factor in [0,1] for bulk construction
193  //! \param leaf_fill_factor leaf fill factor in [0,1] for bulk construction
194  template <class InputIterator>
195  map(InputIterator b,
196  InputIterator e,
197  unsigned_type node_cache_size_in_bytes,
198  unsigned_type leaf_cache_size_in_bytes,
199  bool range_sorted = false,
200  double node_fill_factor = 0.75,
201  double leaf_fill_factor = 0.6
202  ) : Impl(b, e, node_cache_size_in_bytes, leaf_cache_size_in_bytes,
203  range_sorted, node_fill_factor, leaf_fill_factor)
204  { }
205 
206  //! Constructs a map from a given input range
207  //! \param b beginning of the range
208  //! \param e end of the range
209  //! \param c_ comparator object
210  //! \param node_cache_size_in_bytes size of node cache in bytes (btree implementation)
211  //! \param leaf_cache_size_in_bytes size of leaf cache in bytes (btree implementation)
212  //! \param range_sorted if \c true than the constructor assumes that the range is sorted
213  //! and performs a fast bottom-up bulk construction of the map (btree implementation)
214  //! \param node_fill_factor node fill factor in [0,1] for bulk construction
215  //! \param leaf_fill_factor leaf fill factor in [0,1] for bulk construction
216  template <class InputIterator>
217  map(InputIterator b,
218  InputIterator e,
219  const key_compare& c_,
220  unsigned_type node_cache_size_in_bytes,
221  unsigned_type leaf_cache_size_in_bytes,
222  bool range_sorted = false,
223  double node_fill_factor = 0.75,
224  double leaf_fill_factor = 0.6
225  ) : Impl(b, e, c_, node_cache_size_in_bytes, leaf_cache_size_in_bytes,
226  range_sorted, node_fill_factor, leaf_fill_factor)
227  { }
228 
229  //! \}
230 
231  //! \name Modifiers
232  //! \{
233 
234  void swap(map& obj) { std::swap(Impl, obj.Impl); }
235  std::pair<iterator, bool> insert(const value_type& x)
236  {
237  return Impl.insert(x);
238  }
240  {
241  return Impl.insert(pos, x);
242  }
243  template <class InputIterator>
244  void insert(InputIterator b, InputIterator e)
245  {
246  Impl.insert(b, e);
247  }
248  void erase(iterator pos)
249  {
250  Impl.erase(pos);
251  }
253  {
254  return Impl.erase(k);
255  }
256  void erase(iterator first, iterator last)
257  {
258  Impl.erase(first, last);
259  }
260  void clear()
261  {
262  Impl.clear();
263  }
264 
265  //! \}
266 
267  //! \name Operations
268  //! \{
269 
271  {
272  return Impl.find(k);
273  }
274  const_iterator find(const key_type& k) const
275  {
276  return Impl.find(k);
277  }
279  {
280  return Impl.count(k);
281  }
283  {
284  return Impl.lower_bound(k);
285  }
287  {
288  return Impl.lower_bound(k);
289  }
291  {
292  return Impl.upper_bound(k);
293  }
295  {
296  return Impl.upper_bound(k);
297  }
298  std::pair<iterator, iterator> equal_range(const key_type& k)
299  {
300  return Impl.equal_range(k);
301  }
302  std::pair<const_iterator, const_iterator> equal_range(const key_type& k) const
303  {
304  return Impl.equal_range(k);
305  }
306 
307  //! \}
308 
309  //! \name Operators
310  //! \{
311 
312  data_type& operator [] (const key_type& k)
313  {
314  return Impl[k];
315  }
316 
317  //! \}
318 
319  //! \name Miscellaneous
320  //! \{
321 
322  //! Enables leaf prefetching during scanning
324  {
325  Impl.enable_prefetching();
326  }
327 
328  //! Disables leaf prefetching during scanning
330  {
331  Impl.disable_prefetching();
332  }
333 
334  //! Returns the status of leaf prefetching during scanning
336  {
337  return Impl.prefetching_enabled();
338  }
339 
340  //! Prints cache statistics
341  void print_statistics(std::ostream& o) const
342  {
343  Impl.print_statistics(o);
344  }
345 
346  //! Resets cache statistics
348  {
349  Impl.reset_statistics();
350  }
351 
352  //! \}
353 
354  //////////////////////////////////////////////////
355  template <class KeyType_,
356  class DataType_,
357  class CompareType_,
358  unsigned RawNodeSize_,
359  unsigned RawLeafSize_,
360  class PDAllocStrategy_>
363  //////////////////////////////////////////////////
364  template <class KeyType_,
365  class DataType_,
366  class CompareType_,
367  unsigned RawNodeSize_,
368  unsigned RawLeafSize_,
369  class PDAllocStrategy_>
370  friend bool operator < (const map<KeyType_, DataType_, CompareType_, RawNodeSize_, RawLeafSize_, PDAllocStrategy_>& a,
372  //////////////////////////////////////////////////
373  template <class KeyType_,
374  class DataType_,
375  class CompareType_,
376  unsigned RawNodeSize_,
377  unsigned RawLeafSize_,
378  class PDAllocStrategy_>
381  //////////////////////////////////////////////////
382  template <class KeyType_,
383  class DataType_,
384  class CompareType_,
385  unsigned RawNodeSize_,
386  unsigned RawLeafSize_,
387  class PDAllocStrategy_>
390  //////////////////////////////////////////////////
391  template <class KeyType_,
392  class DataType_,
393  class CompareType_,
394  unsigned RawNodeSize_,
395  unsigned RawLeafSize_,
396  class PDAllocStrategy_>
397  friend bool operator <= (const map<KeyType_, DataType_, CompareType_, RawNodeSize_, RawLeafSize_, PDAllocStrategy_>& a,
399  //////////////////////////////////////////////////
400  template <class KeyType_,
401  class DataType_,
402  class CompareType_,
403  unsigned RawNodeSize_,
404  unsigned RawLeafSize_,
405  class PDAllocStrategy_>
408  //////////////////////////////////////////////////
409 };
410 
411 template <class KeyType,
412  class DataType,
413  class CompareType,
414  unsigned RawNodeSize,
415  unsigned RawLeafSize,
416  class PDAllocStrategy
417  >
420 {
421  return a.Impl == b.Impl;
422 }
423 
424 template <class KeyType,
425  class DataType,
426  class CompareType,
427  unsigned RawNodeSize,
428  unsigned RawLeafSize,
429  class PDAllocStrategy
430  >
431 inline bool operator < (const map<KeyType, DataType, CompareType, RawNodeSize, RawLeafSize, PDAllocStrategy>& a,
433 {
434  return a.Impl < b.Impl;
435 }
436 
437 template <class KeyType,
438  class DataType,
439  class CompareType,
440  unsigned RawNodeSize,
441  unsigned RawLeafSize,
442  class PDAllocStrategy
443  >
446 {
447  return a.Impl > b.Impl;
448 }
449 
450 template <class KeyType,
451  class DataType,
452  class CompareType,
453  unsigned RawNodeSize,
454  unsigned RawLeafSize,
455  class PDAllocStrategy
456  >
459 {
460  return a.Impl != b.Impl;
461 }
462 
463 template <class KeyType,
464  class DataType,
465  class CompareType,
466  unsigned RawNodeSize,
467  unsigned RawLeafSize,
468  class PDAllocStrategy
469  >
470 inline bool operator <= (const map<KeyType, DataType, CompareType, RawNodeSize, RawLeafSize, PDAllocStrategy>& a,
472 {
473  return a.Impl <= b.Impl;
474 }
475 
476 template <class KeyType,
477  class DataType,
478  class CompareType,
479  unsigned RawNodeSize,
480  unsigned RawLeafSize,
481  class PDAllocStrategy
482  >
485 {
486  return a.Impl >= b.Impl;
487 }
488 
489 //! \}
490 
492 
493 namespace std {
494 
495 template <class KeyType,
496  class DataType,
497  class CompareType,
498  unsigned RawNodeSize,
499  unsigned RawLeafSize,
500  class PDAllocStrategy
501  >
504  )
505 {
506  a.swap(b);
507 }
508 
509 } // namespace std
510 
511 #endif // !STXXL_CONTAINERS_MAP_HEADER
impl_type::key_compare key_compare
Definition: map.h:97
const_reverse_iterator rend() const
Definition: map.h:136
impl_type::reference reference
Definition: map.h:101
const_iterator upper_bound(const key_type &k) const
Definition: map.h:294
stxxl::uint64 size_type
Definition: btree.h:49
const_reverse_iterator rbegin() const
Definition: map.h:124
size_type erase(const key_type &k)
Definition: map.h:252
const_iterator cbegin() const
Definition: map.h:117
const_iterator find(const key_type &k) const
Definition: map.h:274
map(unsigned_type node_cache_size_in_bytes, unsigned_type leaf_cache_size_in_bytes)
A constructor.
Definition: map.h:170
value_type & reference
Definition: btree.h:52
iterator find(const key_type &k)
Definition: map.h:270
impl_type::node_block_type node_block_type
Definition: map.h:90
size_type max_size() const
Definition: map.h:151
impl_type::key_type key_type
Definition: map.h:93
bool empty() const
Definition: map.h:152
impl_type::pointer pointer
Definition: map.h:99
std::reverse_iterator< const_iterator > const_reverse_iterator
Definition: map.h:108
const_iterator lower_bound(const key_type &k) const
Definition: map.h:286
iterator begin()
Definition: map.h:113
impl_type Impl
Definition: map.h:87
void print_statistics(std::ostream &o) const
Prints cache statistics.
Definition: map.h:341
iterator lower_bound(const key_type &k)
Definition: map.h:282
std::pair< const key_type, data_type > value_type
Definition: btree.h:51
iterator end()
Definition: map.h:114
std::pair< iterator, bool > insert(const value_type &x)
Definition: map.h:235
Block containing elements of fixed length.
Definition: typed_block.h:238
size_type count(const key_type &k)
Definition: map.h:278
stxxl::int64 difference_type
Definition: btree.h:50
bool operator!=(const uint_pair &b) const
inequality checking operator
Definition: uint_types.h:201
impl_type::leaf_block_type leaf_block_type
Definition: map.h:91
CompareType key_compare
Definition: btree.h:43
iterator upper_bound(const key_type &k)
Definition: map.h:290
void enable_prefetching()
Enables leaf prefetching during scanning.
Definition: map.h:323
const value_type & const_reference
Definition: btree.h:53
bool operator>(const uint_pair &b) const
greater comparison operator
Definition: uint_types.h:219
impl_type::difference_type difference_type
Definition: map.h:104
bool operator>=(const uint_pair &b) const
greater-or-equal comparison operator
Definition: uint_types.h:225
value_compare value_comp() const
Definition: map.h:160
void clear()
Definition: map.h:260
const_reverse_iterator crend() const
Definition: map.h:140
#define STXXL_BEGIN_NAMESPACE
Definition: namespace.h:16
iterator insert(iterator pos, const value_type &x)
Definition: map.h:239
map(InputIterator b, InputIterator e, const key_compare &c_, unsigned_type node_cache_size_in_bytes, unsigned_type leaf_cache_size_in_bytes, bool range_sorted=false, double node_fill_factor=0.75, double leaf_fill_factor=0.6)
Constructs a map from a given input range.
Definition: map.h:217
bool prefetching_enabled()
Returns the status of leaf prefetching during scanning.
Definition: map.h:335
Simple randomized disk allocation scheme functor.
Definition: block_alloc.h:94
void reset_statistics()
Resets cache statistics.
Definition: map.h:347
KeyType key_type
Definition: btree.h:41
const_reverse_iterator crbegin() const
Definition: map.h:128
value_type const * const_pointer
Definition: btree.h:55
void swap(map &obj)
Definition: map.h:234
impl_type::data_type data_type
Definition: map.h:94
reverse_iterator rbegin()
Definition: map.h:120
void erase(iterator first, iterator last)
Definition: map.h:256
size_type size() const
Definition: map.h:150
map(const key_compare &c_, unsigned_type node_cache_size_in_bytes, unsigned_type leaf_cache_size_in_bytes)
A constructor.
Definition: map.h:179
std::pair< const_iterator, const_iterator > equal_range(const key_type &k) const
Definition: map.h:302
choose_int_types< my_pointer_size >::unsigned_type unsigned_type
Definition: types.h:67
impl_type::const_reference const_reference
Definition: map.h:102
value_type * pointer
Definition: btree.h:54
std::pair< iterator, iterator > equal_range(const key_type &k)
Definition: map.h:298
impl_type::size_type size_type
Definition: map.h:103
std::reverse_iterator< iterator > reverse_iterator
Definition: map.h:107
map(InputIterator b, InputIterator e, unsigned_type node_cache_size_in_bytes, unsigned_type leaf_cache_size_in_bytes, bool range_sorted=false, double node_fill_factor=0.75, double leaf_fill_factor=0.6)
Constructs a map from a given input range.
Definition: map.h:195
External associative container (map). Introduction to map container: see STXXL Map (B+-tree) tutor...
Definition: map.h:83
btree::btree< KeyType, DataType, CompareType, RawNodeSize, RawLeafSize, PDAllocStrategy > impl_type
Definition: map.h:85
void disable_prefetching()
Disables leaf prefetching during scanning.
Definition: map.h:329
DataType data_type
Definition: btree.h:42
void erase(iterator pos)
Definition: map.h:248
key_compare key_comp() const
Definition: map.h:159
impl_type::const_pointer const_pointer
Definition: map.h:100
void insert(InputIterator b, InputIterator e)
Definition: map.h:244
impl_type::value_compare value_compare
Definition: map.h:98
reverse_iterator rend()
Definition: map.h:132
impl_type::const_iterator const_iterator
Definition: map.h:106
impl_type::data_type mapped_type
Definition: map.h:95
const_iterator cend() const
Definition: map.h:118
impl_type::value_type value_type
Definition: map.h:96
bool operator==(const uint_pair &b) const
equality checking operator
Definition: uint_types.h:195
const_iterator end() const
Definition: map.h:116
#define STXXL_END_NAMESPACE
Definition: namespace.h:17
impl_type::iterator iterator
Definition: map.h:105
const_iterator begin() const
Definition: map.h:115