Stxxl  1.3.2
map.h
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_MAP_HEADER
15 #define STXXL_MAP_HEADER
16 
17 #include <stxxl/bits/noncopyable.h>
18 #include <stxxl/bits/containers/btree/btree.h>
19 
20 
21 __STXXL_BEGIN_NAMESPACE
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 
37 
39 
73 template <class KeyType,
74  class DataType,
75  class CompareType,
76  unsigned RawNodeSize = 16 * 1024, // 16 KBytes default
77  unsigned RawLeafSize = 128 * 1024, // 128 KBytes default
78  class PDAllocStrategy = stxxl::SR
79  >
80 class map : private noncopyable
81 {
82  typedef btree::btree<KeyType, DataType, CompareType, RawNodeSize, RawLeafSize, PDAllocStrategy> impl_type;
83 
84  impl_type Impl;
85 
86 public:
89 
90  typedef typename impl_type::key_type key_type;
91  typedef typename impl_type::data_type data_type;
92  typedef typename impl_type::data_type mapped_type;
93  typedef typename impl_type::value_type value_type;
94  typedef typename impl_type::key_compare key_compare;
95  typedef typename impl_type::value_compare value_compare;
96  typedef typename impl_type::pointer pointer;
97  typedef typename impl_type::const_pointer const_pointer;
98  typedef typename impl_type::reference reference;
99  typedef typename impl_type::const_reference const_reference;
100  typedef typename impl_type::size_type size_type;
101  typedef typename impl_type::difference_type difference_type;
102  typedef typename impl_type::iterator iterator;
103  typedef typename impl_type::const_iterator const_iterator;
104  typedef std::reverse_iterator<iterator> reverse_iterator;
105  typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
106 
107  iterator begin() { return Impl.begin(); }
108  iterator end() { return Impl.end(); }
109  const_iterator begin() const { return Impl.begin(); }
110  const_iterator end() const { return Impl.end(); }
111  const_iterator cbegin() const { return begin(); }
112  const_iterator cend() const { return end(); }
113 
114  reverse_iterator rbegin()
115  {
116  return reverse_iterator(end());
117  }
118  const_reverse_iterator rbegin() const
119  {
120  return const_reverse_iterator(end());
121  }
122  const_reverse_iterator crbegin() const
123  {
124  return const_reverse_iterator(end());
125  }
126  reverse_iterator rend()
127  {
128  return reverse_iterator(begin());
129  }
130  const_reverse_iterator rend() const
131  {
132  return const_reverse_iterator(begin());
133  }
134  const_reverse_iterator crend() const
135  {
136  return const_reverse_iterator(begin());
137  }
138 
139  size_type size() const { return Impl.size(); }
140  size_type max_size() const { return Impl.max_size(); }
141  bool empty() const { return Impl.empty(); }
142  key_compare key_comp() const { return Impl.key_comp(); }
143  value_compare value_comp() const { return Impl.value_comp(); }
144 
148  map(unsigned_type node_cache_size_in_bytes,
149  unsigned_type leaf_cache_size_in_bytes
150  ) : Impl(node_cache_size_in_bytes, leaf_cache_size_in_bytes)
151  { }
152 
157  map(const key_compare & c_,
158  unsigned_type node_cache_size_in_bytes,
159  unsigned_type leaf_cache_size_in_bytes
160  ) : Impl(c_, node_cache_size_in_bytes, leaf_cache_size_in_bytes)
161  { }
162 
172  template <class InputIterator>
173  map(InputIterator b,
174  InputIterator e,
175  unsigned_type node_cache_size_in_bytes,
176  unsigned_type leaf_cache_size_in_bytes,
177  bool range_sorted = false,
178  double node_fill_factor = 0.75,
179  double leaf_fill_factor = 0.6
180  ) : Impl(b, e, node_cache_size_in_bytes, leaf_cache_size_in_bytes,
181  range_sorted, node_fill_factor, leaf_fill_factor)
182  { }
183 
194  template <class InputIterator>
195  map(InputIterator b,
196  InputIterator e,
197  const key_compare & c_,
198  unsigned_type node_cache_size_in_bytes,
199  unsigned_type leaf_cache_size_in_bytes,
200  bool range_sorted = false,
201  double node_fill_factor = 0.75,
202  double leaf_fill_factor = 0.6
203  ) : Impl(b, e, c_, node_cache_size_in_bytes, leaf_cache_size_in_bytes,
204  range_sorted, node_fill_factor, leaf_fill_factor)
205  { }
206 
207  void swap(map & obj) { std::swap(Impl, obj.Impl); }
208  std::pair<iterator, bool> insert(const value_type & x)
209  {
210  return Impl.insert(x);
211  }
212  iterator insert(iterator pos, const value_type & x)
213  {
214  return Impl.insert(pos, x);
215  }
216  template <class InputIterator>
217  void insert(InputIterator b, InputIterator e)
218  {
219  Impl.insert(b, e);
220  }
221  void erase(iterator pos)
222  {
223  Impl.erase(pos);
224  }
225  size_type erase(const key_type & k)
226  {
227  return Impl.erase(k);
228  }
229  void erase(iterator first, iterator last)
230  {
231  Impl.erase(first, last);
232  }
233  void clear()
234  {
235  Impl.clear();
236  }
237  iterator find(const key_type & k)
238  {
239  return Impl.find(k);
240  }
241  const_iterator find(const key_type & k) const
242  {
243  return Impl.find(k);
244  }
245  size_type count(const key_type & k)
246  {
247  return Impl.count(k);
248  }
249  iterator lower_bound(const key_type & k)
250  {
251  return Impl.lower_bound(k);
252  }
253  const_iterator lower_bound(const key_type & k) const
254  {
255  return Impl.lower_bound(k);
256  }
257  iterator upper_bound(const key_type & k)
258  {
259  return Impl.upper_bound(k);
260  }
261  const_iterator upper_bound(const key_type & k) const
262  {
263  return Impl.upper_bound(k);
264  }
265  std::pair<iterator, iterator> equal_range(const key_type & k)
266  {
267  return Impl.equal_range(k);
268  }
269  std::pair<const_iterator, const_iterator> equal_range(const key_type & k) const
270  {
271  return Impl.equal_range(k);
272  }
273  data_type & operator [] (const key_type & k)
274  {
275  return Impl[k];
276  }
277 
280  {
281  Impl.enable_prefetching();
282  }
283 
286  {
287  Impl.disable_prefetching();
288  }
289 
292  {
293  return Impl.prefetching_enabled();
294  }
295 
297  void print_statistics(std::ostream & o) const
298  {
299  Impl.print_statistics(o);
300  }
301 
304  {
305  Impl.reset_statistics();
306  }
307 
309  template <class KeyType_,
310  class DataType_,
311  class CompareType_,
312  unsigned RawNodeSize_,
313  unsigned RawLeafSize_,
314  class PDAllocStrategy_>
318  template <class KeyType_,
319  class DataType_,
320  class CompareType_,
321  unsigned RawNodeSize_,
322  unsigned RawLeafSize_,
323  class PDAllocStrategy_>
324  friend bool operator < (const map<KeyType_, DataType_, CompareType_, RawNodeSize_, RawLeafSize_, PDAllocStrategy_> & a,
327  template <class KeyType_,
328  class DataType_,
329  class CompareType_,
330  unsigned RawNodeSize_,
331  unsigned RawLeafSize_,
332  class PDAllocStrategy_>
336  template <class KeyType_,
337  class DataType_,
338  class CompareType_,
339  unsigned RawNodeSize_,
340  unsigned RawLeafSize_,
341  class PDAllocStrategy_>
345  template <class KeyType_,
346  class DataType_,
347  class CompareType_,
348  unsigned RawNodeSize_,
349  unsigned RawLeafSize_,
350  class PDAllocStrategy_>
351  friend bool operator <= (const map<KeyType_, DataType_, CompareType_, RawNodeSize_, RawLeafSize_, PDAllocStrategy_> & a,
354  template <class KeyType_,
355  class DataType_,
356  class CompareType_,
357  unsigned RawNodeSize_,
358  unsigned RawLeafSize_,
359  class PDAllocStrategy_>
363 };
364 
365 template <class KeyType,
366  class DataType,
367  class CompareType,
368  unsigned RawNodeSize,
369  unsigned RawLeafSize,
370  class PDAllocStrategy
371  >
374 {
375  return a.Impl == b.Impl;
376 }
377 
378 template <class KeyType,
379  class DataType,
380  class CompareType,
381  unsigned RawNodeSize,
382  unsigned RawLeafSize,
383  class PDAllocStrategy
384  >
385 inline bool operator < (const map<KeyType, DataType, CompareType, RawNodeSize, RawLeafSize, PDAllocStrategy> & a,
387 {
388  return a.Impl < b.Impl;
389 }
390 
391 template <class KeyType,
392  class DataType,
393  class CompareType,
394  unsigned RawNodeSize,
395  unsigned RawLeafSize,
396  class PDAllocStrategy
397  >
400 {
401  return a.Impl > b.Impl;
402 }
403 
404 template <class KeyType,
405  class DataType,
406  class CompareType,
407  unsigned RawNodeSize,
408  unsigned RawLeafSize,
409  class PDAllocStrategy
410  >
413 {
414  return a.Impl != b.Impl;
415 }
416 
417 template <class KeyType,
418  class DataType,
419  class CompareType,
420  unsigned RawNodeSize,
421  unsigned RawLeafSize,
422  class PDAllocStrategy
423  >
424 inline bool operator <= (const map<KeyType, DataType, CompareType, RawNodeSize, RawLeafSize, PDAllocStrategy> & a,
426 {
427  return a.Impl <= b.Impl;
428 }
429 
430 template <class KeyType,
431  class DataType,
432  class CompareType,
433  unsigned RawNodeSize,
434  unsigned RawLeafSize,
435  class PDAllocStrategy
436  >
439 {
440  return a.Impl >= b.Impl;
441 }
442 
444 
445 __STXXL_END_NAMESPACE
446 
447 namespace std
448 {
449  template <class KeyType,
450  class DataType,
451  class CompareType,
452  unsigned RawNodeSize,
453  unsigned RawLeafSize,
454  class PDAllocStrategy
455  >
456  void swap(stxxl::map<KeyType, DataType, CompareType, RawNodeSize, RawLeafSize, PDAllocStrategy> & a,
457  stxxl::map<KeyType, DataType, CompareType, RawNodeSize, RawLeafSize, PDAllocStrategy> & b
458  )
459  {
460  a.swap(b);
461  }
462 }
463 
464 #endif // !STXXL_MAP_HEADER
void print_statistics(std::ostream &o) const
Prints cache statistics.
Definition: map.h:297
Block containing elements of fixed length.
Definition: typed_block.h:223
bool prefetching_enabled()
Returns the status of leaf prefetching during scanning.
Definition: map.h:291
void enable_prefetching()
Enables leaf prefetching during scanning.
Definition: map.h:279
External associative container.
Definition: map.h:80
map(unsigned_type node_cache_size_in_bytes, unsigned_type leaf_cache_size_in_bytes)
A constructor.
Definition: map.h:148
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:173
void reset_statistics()
Resets cache statistics.
Definition: map.h:303
void disable_prefetching()
Disables leaf prefetching during scanning.
Definition: map.h:285
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:195
map(const key_compare &c_, unsigned_type node_cache_size_in_bytes, unsigned_type leaf_cache_size_in_bytes)
A constructor.
Definition: map.h:157