• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2018 The Abseil Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15 // -----------------------------------------------------------------------------
16 // File: btree_set.h
17 // -----------------------------------------------------------------------------
18 //
19 // This header file defines B-tree sets: sorted associative containers of
20 // values.
21 //
22 //     * `absl::btree_set<>`
23 //     * `absl::btree_multiset<>`
24 //
25 // These B-tree types are similar to the corresponding types in the STL
26 // (`std::set` and `std::multiset`) and generally conform to the STL interfaces
27 // of those types. However, because they are implemented using B-trees, they
28 // are more efficient in most situations.
29 //
30 // Unlike `std::set` and `std::multiset`, which are commonly implemented using
31 // red-black tree nodes, B-tree sets use more generic B-tree nodes able to hold
32 // multiple values per node. Holding multiple values per node often makes
33 // B-tree sets perform better than their `std::set` counterparts, because
34 // multiple entries can be checked within the same cache hit.
35 //
36 // However, these types should not be considered drop-in replacements for
37 // `std::set` and `std::multiset` as there are some API differences, which are
38 // noted in this header file.
39 //
40 // Importantly, insertions and deletions may invalidate outstanding iterators,
41 // pointers, and references to elements. Such invalidations are typically only
42 // an issue if insertion and deletion operations are interleaved with the use of
43 // more than one iterator, pointer, or reference simultaneously. For this
44 // reason, `insert()` and `erase()` return a valid iterator at the current
45 // position.
46 
47 #ifndef ABSL_CONTAINER_BTREE_SET_H_
48 #define ABSL_CONTAINER_BTREE_SET_H_
49 
50 #include "absl/container/internal/btree.h"  // IWYU pragma: export
51 #include "absl/container/internal/btree_container.h"  // IWYU pragma: export
52 
53 namespace absl {
54 ABSL_NAMESPACE_BEGIN
55 
56 // absl::btree_set<>
57 //
58 // An `absl::btree_set<K>` is an ordered associative container of unique key
59 // values designed to be a more efficient replacement for `std::set` (in most
60 // cases).
61 //
62 // Keys are sorted using an (optional) comparison function, which defaults to
63 // `std::less<K>`.
64 //
65 // An `absl::btree_set<K>` uses a default allocator of `std::allocator<K>` to
66 // allocate (and deallocate) nodes, and construct and destruct values within
67 // those nodes. You may instead specify a custom allocator `A` (which in turn
68 // requires specifying a custom comparator `C`) as in
69 // `absl::btree_set<K, C, A>`.
70 //
71 template <typename Key, typename Compare = std::less<Key>,
72           typename Alloc = std::allocator<Key>>
73 class btree_set
74     : public container_internal::btree_set_container<
75           container_internal::btree<container_internal::set_params<
76               Key, Compare, Alloc, /*TargetNodeSize=*/256,
77               /*Multi=*/false>>> {
78   using Base = typename btree_set::btree_set_container;
79 
80  public:
81   // Constructors and Assignment Operators
82   //
83   // A `btree_set` supports the same overload set as `std::set`
84   // for construction and assignment:
85   //
86   // * Default constructor
87   //
88   //   absl::btree_set<std::string> set1;
89   //
90   // * Initializer List constructor
91   //
92   //   absl::btree_set<std::string> set2 =
93   //       {{"huey"}, {"dewey"}, {"louie"},};
94   //
95   // * Copy constructor
96   //
97   //   absl::btree_set<std::string> set3(set2);
98   //
99   // * Copy assignment operator
100   //
101   //  absl::btree_set<std::string> set4;
102   //  set4 = set3;
103   //
104   // * Move constructor
105   //
106   //   // Move is guaranteed efficient
107   //   absl::btree_set<std::string> set5(std::move(set4));
108   //
109   // * Move assignment operator
110   //
111   //   // May be efficient if allocators are compatible
112   //   absl::btree_set<std::string> set6;
113   //   set6 = std::move(set5);
114   //
115   // * Range constructor
116   //
117   //   std::vector<std::string> v = {"a", "b"};
118   //   absl::btree_set<std::string> set7(v.begin(), v.end());
btree_set()119   btree_set() {}
120   using Base::Base;
121 
122   // btree_set::begin()
123   //
124   // Returns an iterator to the beginning of the `btree_set`.
125   using Base::begin;
126 
127   // btree_set::cbegin()
128   //
129   // Returns a const iterator to the beginning of the `btree_set`.
130   using Base::cbegin;
131 
132   // btree_set::end()
133   //
134   // Returns an iterator to the end of the `btree_set`.
135   using Base::end;
136 
137   // btree_set::cend()
138   //
139   // Returns a const iterator to the end of the `btree_set`.
140   using Base::cend;
141 
142   // btree_set::empty()
143   //
144   // Returns whether or not the `btree_set` is empty.
145   using Base::empty;
146 
147   // btree_set::max_size()
148   //
149   // Returns the largest theoretical possible number of elements within a
150   // `btree_set` under current memory constraints. This value can be thought
151   // of as the largest value of `std::distance(begin(), end())` for a
152   // `btree_set<Key>`.
153   using Base::max_size;
154 
155   // btree_set::size()
156   //
157   // Returns the number of elements currently within the `btree_set`.
158   using Base::size;
159 
160   // btree_set::clear()
161   //
162   // Removes all elements from the `btree_set`. Invalidates any references,
163   // pointers, or iterators referring to contained elements.
164   using Base::clear;
165 
166   // btree_set::erase()
167   //
168   // Erases elements within the `btree_set`. Overloads are listed below.
169   //
170   // iterator erase(iterator position):
171   // iterator erase(const_iterator position):
172   //
173   //   Erases the element at `position` of the `btree_set`, returning
174   //   the iterator pointing to the element after the one that was erased
175   //   (or end() if none exists).
176   //
177   // iterator erase(const_iterator first, const_iterator last):
178   //
179   //   Erases the elements in the open interval [`first`, `last`), returning
180   //   the iterator pointing to the element after the interval that was erased
181   //   (or end() if none exists).
182   //
183   // template <typename K> size_type erase(const K& key):
184   //
185   //   Erases the element with the matching key, if it exists, returning the
186   //   number of elements erased (0 or 1).
187   using Base::erase;
188 
189   // btree_set::insert()
190   //
191   // Inserts an element of the specified value into the `btree_set`,
192   // returning an iterator pointing to the newly inserted element, provided that
193   // an element with the given key does not already exist. If an insertion
194   // occurs, any references, pointers, or iterators are invalidated.
195   // Overloads are listed below.
196   //
197   // std::pair<iterator,bool> insert(const value_type& value):
198   //
199   //   Inserts a value into the `btree_set`. Returns a pair consisting of an
200   //   iterator to the inserted element (or to the element that prevented the
201   //   insertion) and a bool denoting whether the insertion took place.
202   //
203   // std::pair<iterator,bool> insert(value_type&& value):
204   //
205   //   Inserts a moveable value into the `btree_set`. Returns a pair
206   //   consisting of an iterator to the inserted element (or to the element that
207   //   prevented the insertion) and a bool denoting whether the insertion took
208   //   place.
209   //
210   // iterator insert(const_iterator hint, const value_type& value):
211   // iterator insert(const_iterator hint, value_type&& value):
212   //
213   //   Inserts a value, using the position of `hint` as a non-binding suggestion
214   //   for where to begin the insertion search. Returns an iterator to the
215   //   inserted element, or to the existing element that prevented the
216   //   insertion.
217   //
218   // void insert(InputIterator first, InputIterator last):
219   //
220   //   Inserts a range of values [`first`, `last`).
221   //
222   // void insert(std::initializer_list<init_type> ilist):
223   //
224   //   Inserts the elements within the initializer list `ilist`.
225   using Base::insert;
226 
227   // btree_set::emplace()
228   //
229   // Inserts an element of the specified value by constructing it in-place
230   // within the `btree_set`, provided that no element with the given key
231   // already exists.
232   //
233   // The element may be constructed even if there already is an element with the
234   // key in the container, in which case the newly constructed element will be
235   // destroyed immediately.
236   //
237   // If an insertion occurs, any references, pointers, or iterators are
238   // invalidated.
239   using Base::emplace;
240 
241   // btree_set::emplace_hint()
242   //
243   // Inserts an element of the specified value by constructing it in-place
244   // within the `btree_set`, using the position of `hint` as a non-binding
245   // suggestion for where to begin the insertion search, and only inserts
246   // provided that no element with the given key already exists.
247   //
248   // The element may be constructed even if there already is an element with the
249   // key in the container, in which case the newly constructed element will be
250   // destroyed immediately.
251   //
252   // If an insertion occurs, any references, pointers, or iterators are
253   // invalidated.
254   using Base::emplace_hint;
255 
256   // btree_set::extract()
257   //
258   // Extracts the indicated element, erasing it in the process, and returns it
259   // as a C++17-compatible node handle. Overloads are listed below.
260   //
261   // node_type extract(const_iterator position):
262   //
263   //   Extracts the element at the indicated position and returns a node handle
264   //   owning that extracted data.
265   //
266   // template <typename K> node_type extract(const K& k):
267   //
268   //   Extracts the element with the key matching the passed key value and
269   //   returns a node handle owning that extracted data. If the `btree_set`
270   //   does not contain an element with a matching key, this function returns an
271   //   empty node handle.
272   //
273   // NOTE: In this context, `node_type` refers to the C++17 concept of a
274   // move-only type that owns and provides access to the elements in associative
275   // containers (https://en.cppreference.com/w/cpp/container/node_handle).
276   // It does NOT refer to the data layout of the underlying btree.
277   using Base::extract;
278 
279   // btree_set::merge()
280   //
281   // Extracts elements from a given `source` btree_set into this
282   // `btree_set`. If the destination `btree_set` already contains an
283   // element with an equivalent key, that element is not extracted.
284   using Base::merge;
285 
286   // btree_set::swap(btree_set& other)
287   //
288   // Exchanges the contents of this `btree_set` with those of the `other`
289   // btree_set, avoiding invocation of any move, copy, or swap operations on
290   // individual elements.
291   //
292   // All iterators and references on the `btree_set` remain valid, excepting
293   // for the past-the-end iterator, which is invalidated.
294   using Base::swap;
295 
296   // btree_set::contains()
297   //
298   // template <typename K> bool contains(const K& key) const:
299   //
300   // Determines whether an element comparing equal to the given `key` exists
301   // within the `btree_set`, returning `true` if so or `false` otherwise.
302   //
303   // Supports heterogeneous lookup, provided that the set is provided a
304   // compatible heterogeneous comparator.
305   using Base::contains;
306 
307   // btree_set::count()
308   //
309   // template <typename K> size_type count(const K& key) const:
310   //
311   // Returns the number of elements comparing equal to the given `key` within
312   // the `btree_set`. Note that this function will return either `1` or `0`
313   // since duplicate elements are not allowed within a `btree_set`.
314   //
315   // Supports heterogeneous lookup, provided that the set is provided a
316   // compatible heterogeneous comparator.
317   using Base::count;
318 
319   // btree_set::equal_range()
320   //
321   // Returns a closed range [first, last], defined by a `std::pair` of two
322   // iterators, containing all elements with the passed key in the
323   // `btree_set`.
324   using Base::equal_range;
325 
326   // btree_set::find()
327   //
328   // template <typename K> iterator find(const K& key):
329   // template <typename K> const_iterator find(const K& key) const:
330   //
331   // Finds an element with the passed `key` within the `btree_set`.
332   //
333   // Supports heterogeneous lookup, provided that the set is provided a
334   // compatible heterogeneous comparator.
335   using Base::find;
336 
337   // btree_set::get_allocator()
338   //
339   // Returns the allocator function associated with this `btree_set`.
340   using Base::get_allocator;
341 
342   // btree_set::key_comp();
343   //
344   // Returns the key comparator associated with this `btree_set`.
345   using Base::key_comp;
346 
347   // btree_set::value_comp();
348   //
349   // Returns the value comparator associated with this `btree_set`. The keys to
350   // sort the elements are the values themselves, therefore `value_comp` and its
351   // sibling member function `key_comp` are equivalent.
352   using Base::value_comp;
353 };
354 
355 // absl::swap(absl::btree_set<>, absl::btree_set<>)
356 //
357 // Swaps the contents of two `absl::btree_set` containers.
358 template <typename K, typename C, typename A>
swap(btree_set<K,C,A> & x,btree_set<K,C,A> & y)359 void swap(btree_set<K, C, A> &x, btree_set<K, C, A> &y) {
360   return x.swap(y);
361 }
362 
363 // absl::erase_if(absl::btree_set<>, Pred)
364 //
365 // Erases all elements that satisfy the predicate pred from the container.
366 template <typename K, typename C, typename A, typename Pred>
erase_if(btree_set<K,C,A> & set,Pred pred)367 void erase_if(btree_set<K, C, A> &set, Pred pred) {
368   for (auto it = set.begin(); it != set.end();) {
369     if (pred(*it)) {
370       it = set.erase(it);
371     } else {
372       ++it;
373     }
374   }
375 }
376 
377 // absl::btree_multiset<>
378 //
379 // An `absl::btree_multiset<K>` is an ordered associative container of
380 // keys and associated values designed to be a more efficient replacement
381 // for `std::multiset` (in most cases). Unlike `absl::btree_set`, a B-tree
382 // multiset allows equivalent elements.
383 //
384 // Keys are sorted using an (optional) comparison function, which defaults to
385 // `std::less<K>`.
386 //
387 // An `absl::btree_multiset<K>` uses a default allocator of `std::allocator<K>`
388 // to allocate (and deallocate) nodes, and construct and destruct values within
389 // those nodes. You may instead specify a custom allocator `A` (which in turn
390 // requires specifying a custom comparator `C`) as in
391 // `absl::btree_multiset<K, C, A>`.
392 //
393 template <typename Key, typename Compare = std::less<Key>,
394           typename Alloc = std::allocator<Key>>
395 class btree_multiset
396     : public container_internal::btree_multiset_container<
397           container_internal::btree<container_internal::set_params<
398               Key, Compare, Alloc, /*TargetNodeSize=*/256,
399               /*Multi=*/true>>> {
400   using Base = typename btree_multiset::btree_multiset_container;
401 
402  public:
403   // Constructors and Assignment Operators
404   //
405   // A `btree_multiset` supports the same overload set as `std::set`
406   // for construction and assignment:
407   //
408   // * Default constructor
409   //
410   //   absl::btree_multiset<std::string> set1;
411   //
412   // * Initializer List constructor
413   //
414   //   absl::btree_multiset<std::string> set2 =
415   //       {{"huey"}, {"dewey"}, {"louie"},};
416   //
417   // * Copy constructor
418   //
419   //   absl::btree_multiset<std::string> set3(set2);
420   //
421   // * Copy assignment operator
422   //
423   //  absl::btree_multiset<std::string> set4;
424   //  set4 = set3;
425   //
426   // * Move constructor
427   //
428   //   // Move is guaranteed efficient
429   //   absl::btree_multiset<std::string> set5(std::move(set4));
430   //
431   // * Move assignment operator
432   //
433   //   // May be efficient if allocators are compatible
434   //   absl::btree_multiset<std::string> set6;
435   //   set6 = std::move(set5);
436   //
437   // * Range constructor
438   //
439   //   std::vector<std::string> v = {"a", "b"};
440   //   absl::btree_multiset<std::string> set7(v.begin(), v.end());
btree_multiset()441   btree_multiset() {}
442   using Base::Base;
443 
444   // btree_multiset::begin()
445   //
446   // Returns an iterator to the beginning of the `btree_multiset`.
447   using Base::begin;
448 
449   // btree_multiset::cbegin()
450   //
451   // Returns a const iterator to the beginning of the `btree_multiset`.
452   using Base::cbegin;
453 
454   // btree_multiset::end()
455   //
456   // Returns an iterator to the end of the `btree_multiset`.
457   using Base::end;
458 
459   // btree_multiset::cend()
460   //
461   // Returns a const iterator to the end of the `btree_multiset`.
462   using Base::cend;
463 
464   // btree_multiset::empty()
465   //
466   // Returns whether or not the `btree_multiset` is empty.
467   using Base::empty;
468 
469   // btree_multiset::max_size()
470   //
471   // Returns the largest theoretical possible number of elements within a
472   // `btree_multiset` under current memory constraints. This value can be
473   // thought of as the largest value of `std::distance(begin(), end())` for a
474   // `btree_multiset<Key>`.
475   using Base::max_size;
476 
477   // btree_multiset::size()
478   //
479   // Returns the number of elements currently within the `btree_multiset`.
480   using Base::size;
481 
482   // btree_multiset::clear()
483   //
484   // Removes all elements from the `btree_multiset`. Invalidates any references,
485   // pointers, or iterators referring to contained elements.
486   using Base::clear;
487 
488   // btree_multiset::erase()
489   //
490   // Erases elements within the `btree_multiset`. Overloads are listed below.
491   //
492   // iterator erase(iterator position):
493   // iterator erase(const_iterator position):
494   //
495   //   Erases the element at `position` of the `btree_multiset`, returning
496   //   the iterator pointing to the element after the one that was erased
497   //   (or end() if none exists).
498   //
499   // iterator erase(const_iterator first, const_iterator last):
500   //
501   //   Erases the elements in the open interval [`first`, `last`), returning
502   //   the iterator pointing to the element after the interval that was erased
503   //   (or end() if none exists).
504   //
505   // template <typename K> size_type erase(const K& key):
506   //
507   //   Erases the elements matching the key, if any exist, returning the
508   //   number of elements erased.
509   using Base::erase;
510 
511   // btree_multiset::insert()
512   //
513   // Inserts an element of the specified value into the `btree_multiset`,
514   // returning an iterator pointing to the newly inserted element.
515   // Any references, pointers, or iterators are invalidated.  Overloads are
516   // listed below.
517   //
518   // iterator insert(const value_type& value):
519   //
520   //   Inserts a value into the `btree_multiset`, returning an iterator to the
521   //   inserted element.
522   //
523   // iterator insert(value_type&& value):
524   //
525   //   Inserts a moveable value into the `btree_multiset`, returning an iterator
526   //   to the inserted element.
527   //
528   // iterator insert(const_iterator hint, const value_type& value):
529   // iterator insert(const_iterator hint, value_type&& value):
530   //
531   //   Inserts a value, using the position of `hint` as a non-binding suggestion
532   //   for where to begin the insertion search. Returns an iterator to the
533   //   inserted element.
534   //
535   // void insert(InputIterator first, InputIterator last):
536   //
537   //   Inserts a range of values [`first`, `last`).
538   //
539   // void insert(std::initializer_list<init_type> ilist):
540   //
541   //   Inserts the elements within the initializer list `ilist`.
542   using Base::insert;
543 
544   // btree_multiset::emplace()
545   //
546   // Inserts an element of the specified value by constructing it in-place
547   // within the `btree_multiset`. Any references, pointers, or iterators are
548   // invalidated.
549   using Base::emplace;
550 
551   // btree_multiset::emplace_hint()
552   //
553   // Inserts an element of the specified value by constructing it in-place
554   // within the `btree_multiset`, using the position of `hint` as a non-binding
555   // suggestion for where to begin the insertion search.
556   //
557   // Any references, pointers, or iterators are invalidated.
558   using Base::emplace_hint;
559 
560   // btree_multiset::extract()
561   //
562   // Extracts the indicated element, erasing it in the process, and returns it
563   // as a C++17-compatible node handle. Overloads are listed below.
564   //
565   // node_type extract(const_iterator position):
566   //
567   //   Extracts the element at the indicated position and returns a node handle
568   //   owning that extracted data.
569   //
570   // template <typename K> node_type extract(const K& k):
571   //
572   //   Extracts the element with the key matching the passed key value and
573   //   returns a node handle owning that extracted data. If the `btree_multiset`
574   //   does not contain an element with a matching key, this function returns an
575   //   empty node handle.
576   //
577   // NOTE: In this context, `node_type` refers to the C++17 concept of a
578   // move-only type that owns and provides access to the elements in associative
579   // containers (https://en.cppreference.com/w/cpp/container/node_handle).
580   // It does NOT refer to the data layout of the underlying btree.
581   using Base::extract;
582 
583   // btree_multiset::merge()
584   //
585   // Extracts elements from a given `source` btree_multiset into this
586   // `btree_multiset`. If the destination `btree_multiset` already contains an
587   // element with an equivalent key, that element is not extracted.
588   using Base::merge;
589 
590   // btree_multiset::swap(btree_multiset& other)
591   //
592   // Exchanges the contents of this `btree_multiset` with those of the `other`
593   // btree_multiset, avoiding invocation of any move, copy, or swap operations
594   // on individual elements.
595   //
596   // All iterators and references on the `btree_multiset` remain valid,
597   // excepting for the past-the-end iterator, which is invalidated.
598   using Base::swap;
599 
600   // btree_multiset::contains()
601   //
602   // template <typename K> bool contains(const K& key) const:
603   //
604   // Determines whether an element comparing equal to the given `key` exists
605   // within the `btree_multiset`, returning `true` if so or `false` otherwise.
606   //
607   // Supports heterogeneous lookup, provided that the set is provided a
608   // compatible heterogeneous comparator.
609   using Base::contains;
610 
611   // btree_multiset::count()
612   //
613   // template <typename K> size_type count(const K& key) const:
614   //
615   // Returns the number of elements comparing equal to the given `key` within
616   // the `btree_multiset`.
617   //
618   // Supports heterogeneous lookup, provided that the set is provided a
619   // compatible heterogeneous comparator.
620   using Base::count;
621 
622   // btree_multiset::equal_range()
623   //
624   // Returns a closed range [first, last], defined by a `std::pair` of two
625   // iterators, containing all elements with the passed key in the
626   // `btree_multiset`.
627   using Base::equal_range;
628 
629   // btree_multiset::find()
630   //
631   // template <typename K> iterator find(const K& key):
632   // template <typename K> const_iterator find(const K& key) const:
633   //
634   // Finds an element with the passed `key` within the `btree_multiset`.
635   //
636   // Supports heterogeneous lookup, provided that the set is provided a
637   // compatible heterogeneous comparator.
638   using Base::find;
639 
640   // btree_multiset::get_allocator()
641   //
642   // Returns the allocator function associated with this `btree_multiset`.
643   using Base::get_allocator;
644 
645   // btree_multiset::key_comp();
646   //
647   // Returns the key comparator associated with this `btree_multiset`.
648   using Base::key_comp;
649 
650   // btree_multiset::value_comp();
651   //
652   // Returns the value comparator associated with this `btree_multiset`. The
653   // keys to sort the elements are the values themselves, therefore `value_comp`
654   // and its sibling member function `key_comp` are equivalent.
655   using Base::value_comp;
656 };
657 
658 // absl::swap(absl::btree_multiset<>, absl::btree_multiset<>)
659 //
660 // Swaps the contents of two `absl::btree_multiset` containers.
661 template <typename K, typename C, typename A>
swap(btree_multiset<K,C,A> & x,btree_multiset<K,C,A> & y)662 void swap(btree_multiset<K, C, A> &x, btree_multiset<K, C, A> &y) {
663   return x.swap(y);
664 }
665 
666 // absl::erase_if(absl::btree_multiset<>, Pred)
667 //
668 // Erases all elements that satisfy the predicate pred from the container.
669 template <typename K, typename C, typename A, typename Pred>
erase_if(btree_multiset<K,C,A> & set,Pred pred)670 void erase_if(btree_multiset<K, C, A> &set, Pred pred) {
671   for (auto it = set.begin(); it != set.end();) {
672     if (pred(*it)) {
673       it = set.erase(it);
674     } else {
675       ++it;
676     }
677   }
678 }
679 
680 ABSL_NAMESPACE_END
681 }  // namespace absl
682 
683 #endif  // ABSL_CONTAINER_BTREE_SET_H_
684