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. The most consequential differences with respect to
39 // migrating to b-tree from the STL types are listed in the next paragraph.
40 // Other API differences are minor.
41 //
42 // Importantly, insertions and deletions may invalidate outstanding iterators,
43 // pointers, and references to elements. Such invalidations are typically only
44 // an issue if insertion and deletion operations are interleaved with the use of
45 // more than one iterator, pointer, or reference simultaneously. For this
46 // reason, `insert()` and `erase()` return a valid iterator at the current
47 // position (and `extract()` cannot be used in this way).
48 //
49 // Another API difference is that btree iterators can be subtracted, and this
50 // is faster than using std::distance.
51
52 #ifndef ABSL_CONTAINER_BTREE_SET_H_
53 #define ABSL_CONTAINER_BTREE_SET_H_
54
55 #include "absl/container/internal/btree.h" // IWYU pragma: export
56 #include "absl/container/internal/btree_container.h" // IWYU pragma: export
57
58 namespace absl {
59 ABSL_NAMESPACE_BEGIN
60
61 namespace container_internal {
62
63 template <typename Key>
64 struct set_slot_policy;
65
66 template <typename Key, typename Compare, typename Alloc, int TargetNodeSize,
67 bool IsMulti>
68 struct set_params;
69
70 } // namespace container_internal
71
72 // absl::btree_set<>
73 //
74 // An `absl::btree_set<K>` is an ordered associative container of unique key
75 // values designed to be a more efficient replacement for `std::set` (in most
76 // cases).
77 //
78 // Keys are sorted using an (optional) comparison function, which defaults to
79 // `std::less<K>`.
80 //
81 // An `absl::btree_set<K>` uses a default allocator of `std::allocator<K>` to
82 // allocate (and deallocate) nodes, and construct and destruct values within
83 // those nodes. You may instead specify a custom allocator `A` (which in turn
84 // requires specifying a custom comparator `C`) as in
85 // `absl::btree_set<K, C, A>`.
86 //
87 template <typename Key, typename Compare = std::less<Key>,
88 typename Alloc = std::allocator<Key>>
89 class btree_set
90 : public container_internal::btree_set_container<
91 container_internal::btree<container_internal::set_params<
92 Key, Compare, Alloc, /*TargetNodeSize=*/256,
93 /*IsMulti=*/false>>> {
94 using Base = typename btree_set::btree_set_container;
95
96 public:
97 // Constructors and Assignment Operators
98 //
99 // A `btree_set` supports the same overload set as `std::set`
100 // for construction and assignment:
101 //
102 // * Default constructor
103 //
104 // absl::btree_set<std::string> set1;
105 //
106 // * Initializer List constructor
107 //
108 // absl::btree_set<std::string> set2 =
109 // {{"huey"}, {"dewey"}, {"louie"},};
110 //
111 // * Copy constructor
112 //
113 // absl::btree_set<std::string> set3(set2);
114 //
115 // * Copy assignment operator
116 //
117 // absl::btree_set<std::string> set4;
118 // set4 = set3;
119 //
120 // * Move constructor
121 //
122 // // Move is guaranteed efficient
123 // absl::btree_set<std::string> set5(std::move(set4));
124 //
125 // * Move assignment operator
126 //
127 // // May be efficient if allocators are compatible
128 // absl::btree_set<std::string> set6;
129 // set6 = std::move(set5);
130 //
131 // * Range constructor
132 //
133 // std::vector<std::string> v = {"a", "b"};
134 // absl::btree_set<std::string> set7(v.begin(), v.end());
btree_set()135 btree_set() {}
136 using Base::Base;
137
138 // btree_set::begin()
139 //
140 // Returns an iterator to the beginning of the `btree_set`.
141 using Base::begin;
142
143 // btree_set::cbegin()
144 //
145 // Returns a const iterator to the beginning of the `btree_set`.
146 using Base::cbegin;
147
148 // btree_set::end()
149 //
150 // Returns an iterator to the end of the `btree_set`.
151 using Base::end;
152
153 // btree_set::cend()
154 //
155 // Returns a const iterator to the end of the `btree_set`.
156 using Base::cend;
157
158 // btree_set::empty()
159 //
160 // Returns whether or not the `btree_set` is empty.
161 using Base::empty;
162
163 // btree_set::max_size()
164 //
165 // Returns the largest theoretical possible number of elements within a
166 // `btree_set` under current memory constraints. This value can be thought
167 // of as the largest value of `std::distance(begin(), end())` for a
168 // `btree_set<Key>`.
169 using Base::max_size;
170
171 // btree_set::size()
172 //
173 // Returns the number of elements currently within the `btree_set`.
174 using Base::size;
175
176 // btree_set::clear()
177 //
178 // Removes all elements from the `btree_set`. Invalidates any references,
179 // pointers, or iterators referring to contained elements.
180 using Base::clear;
181
182 // btree_set::erase()
183 //
184 // Erases elements within the `btree_set`. Overloads are listed below.
185 //
186 // iterator erase(iterator position):
187 // iterator erase(const_iterator position):
188 //
189 // Erases the element at `position` of the `btree_set`, returning
190 // the iterator pointing to the element after the one that was erased
191 // (or end() if none exists).
192 //
193 // iterator erase(const_iterator first, const_iterator last):
194 //
195 // Erases the elements in the open interval [`first`, `last`), returning
196 // the iterator pointing to the element after the interval that was erased
197 // (or end() if none exists).
198 //
199 // template <typename K> size_type erase(const K& key):
200 //
201 // Erases the element with the matching key, if it exists, returning the
202 // number of elements erased (0 or 1).
203 using Base::erase;
204
205 // btree_set::insert()
206 //
207 // Inserts an element of the specified value into the `btree_set`,
208 // returning an iterator pointing to the newly inserted element, provided that
209 // an element with the given key does not already exist. If an insertion
210 // occurs, any references, pointers, or iterators are invalidated.
211 // Overloads are listed below.
212 //
213 // std::pair<iterator,bool> insert(const value_type& value):
214 //
215 // Inserts a value into the `btree_set`. Returns a pair consisting of an
216 // iterator to the inserted element (or to the element that prevented the
217 // insertion) and a bool denoting whether the insertion took place.
218 //
219 // std::pair<iterator,bool> insert(value_type&& value):
220 //
221 // Inserts a moveable value into the `btree_set`. Returns a pair
222 // consisting of an iterator to the inserted element (or to the element that
223 // prevented the insertion) and a bool denoting whether the insertion took
224 // place.
225 //
226 // iterator insert(const_iterator hint, const value_type& value):
227 // iterator insert(const_iterator hint, value_type&& value):
228 //
229 // Inserts a value, using the position of `hint` as a non-binding suggestion
230 // for where to begin the insertion search. Returns an iterator to the
231 // inserted element, or to the existing element that prevented the
232 // insertion.
233 //
234 // void insert(InputIterator first, InputIterator last):
235 //
236 // Inserts a range of values [`first`, `last`).
237 //
238 // void insert(std::initializer_list<init_type> ilist):
239 //
240 // Inserts the elements within the initializer list `ilist`.
241 using Base::insert;
242
243 // btree_set::emplace()
244 //
245 // Inserts an element of the specified value by constructing it in-place
246 // within the `btree_set`, provided that no element with the given key
247 // already exists.
248 //
249 // The element may be constructed even if there already is an element with the
250 // key in the container, in which case the newly constructed element will be
251 // destroyed immediately.
252 //
253 // If an insertion occurs, any references, pointers, or iterators are
254 // invalidated.
255 using Base::emplace;
256
257 // btree_set::emplace_hint()
258 //
259 // Inserts an element of the specified value by constructing it in-place
260 // within the `btree_set`, using the position of `hint` as a non-binding
261 // suggestion for where to begin the insertion search, and only inserts
262 // provided that no element with the given key already exists.
263 //
264 // The element may be constructed even if there already is an element with the
265 // key in the container, in which case the newly constructed element will be
266 // destroyed immediately.
267 //
268 // If an insertion occurs, any references, pointers, or iterators are
269 // invalidated.
270 using Base::emplace_hint;
271
272 // btree_set::extract()
273 //
274 // Extracts the indicated element, erasing it in the process, and returns it
275 // as a C++17-compatible node handle. Any references, pointers, or iterators
276 // are invalidated. Overloads are listed below.
277 //
278 // node_type extract(const_iterator position):
279 //
280 // Extracts the element at the indicated position and returns a node handle
281 // owning that extracted data.
282 //
283 // template <typename K> node_type extract(const K& k):
284 //
285 // Extracts the element with the key matching the passed key value and
286 // returns a node handle owning that extracted data. If the `btree_set`
287 // does not contain an element with a matching key, this function returns an
288 // empty node handle.
289 //
290 // NOTE: In this context, `node_type` refers to the C++17 concept of a
291 // move-only type that owns and provides access to the elements in associative
292 // containers (https://en.cppreference.com/w/cpp/container/node_handle).
293 // It does NOT refer to the data layout of the underlying btree.
294 using Base::extract;
295
296 // btree_set::merge()
297 //
298 // Extracts elements from a given `source` btree_set into this
299 // `btree_set`. If the destination `btree_set` already contains an
300 // element with an equivalent key, that element is not extracted.
301 using Base::merge;
302
303 // btree_set::swap(btree_set& other)
304 //
305 // Exchanges the contents of this `btree_set` with those of the `other`
306 // btree_set, avoiding invocation of any move, copy, or swap operations on
307 // individual elements.
308 //
309 // All iterators and references on the `btree_set` remain valid, excepting
310 // for the past-the-end iterator, which is invalidated.
311 using Base::swap;
312
313 // btree_set::contains()
314 //
315 // template <typename K> bool contains(const K& key) const:
316 //
317 // Determines whether an element comparing equal to the given `key` exists
318 // within the `btree_set`, returning `true` if so or `false` otherwise.
319 //
320 // Supports heterogeneous lookup, provided that the set has a compatible
321 // heterogeneous comparator.
322 using Base::contains;
323
324 // btree_set::count()
325 //
326 // template <typename K> size_type count(const K& key) const:
327 //
328 // Returns the number of elements comparing equal to the given `key` within
329 // the `btree_set`. Note that this function will return either `1` or `0`
330 // since duplicate elements are not allowed within a `btree_set`.
331 //
332 // Supports heterogeneous lookup, provided that the set has a compatible
333 // heterogeneous comparator.
334 using Base::count;
335
336 // btree_set::equal_range()
337 //
338 // Returns a closed range [first, last], defined by a `std::pair` of two
339 // iterators, containing all elements with the passed key in the
340 // `btree_set`.
341 using Base::equal_range;
342
343 // btree_set::find()
344 //
345 // template <typename K> iterator find(const K& key):
346 // template <typename K> const_iterator find(const K& key) const:
347 //
348 // Finds an element with the passed `key` within the `btree_set`.
349 //
350 // Supports heterogeneous lookup, provided that the set has a compatible
351 // heterogeneous comparator.
352 using Base::find;
353
354 // btree_set::lower_bound()
355 //
356 // template <typename K> iterator lower_bound(const K& key):
357 // template <typename K> const_iterator lower_bound(const K& key) const:
358 //
359 // Finds the first element that is not less than `key` within the `btree_set`.
360 //
361 // Supports heterogeneous lookup, provided that the set has a compatible
362 // heterogeneous comparator.
363 using Base::lower_bound;
364
365 // btree_set::upper_bound()
366 //
367 // template <typename K> iterator upper_bound(const K& key):
368 // template <typename K> const_iterator upper_bound(const K& key) const:
369 //
370 // Finds the first element that is greater than `key` within the `btree_set`.
371 //
372 // Supports heterogeneous lookup, provided that the set has a compatible
373 // heterogeneous comparator.
374 using Base::upper_bound;
375
376 // btree_set::get_allocator()
377 //
378 // Returns the allocator function associated with this `btree_set`.
379 using Base::get_allocator;
380
381 // btree_set::key_comp();
382 //
383 // Returns the key comparator associated with this `btree_set`.
384 using Base::key_comp;
385
386 // btree_set::value_comp();
387 //
388 // Returns the value comparator associated with this `btree_set`. The keys to
389 // sort the elements are the values themselves, therefore `value_comp` and its
390 // sibling member function `key_comp` are equivalent.
391 using Base::value_comp;
392 };
393
394 // absl::swap(absl::btree_set<>, absl::btree_set<>)
395 //
396 // Swaps the contents of two `absl::btree_set` containers.
397 template <typename K, typename C, typename A>
swap(btree_set<K,C,A> & x,btree_set<K,C,A> & y)398 void swap(btree_set<K, C, A> &x, btree_set<K, C, A> &y) {
399 return x.swap(y);
400 }
401
402 // absl::erase_if(absl::btree_set<>, Pred)
403 //
404 // Erases all elements that satisfy the predicate pred from the container.
405 // Returns the number of erased elements.
406 template <typename K, typename C, typename A, typename Pred>
erase_if(btree_set<K,C,A> & set,Pred pred)407 typename btree_set<K, C, A>::size_type erase_if(btree_set<K, C, A> &set,
408 Pred pred) {
409 return container_internal::btree_access::erase_if(set, std::move(pred));
410 }
411
412 // absl::btree_multiset<>
413 //
414 // An `absl::btree_multiset<K>` is an ordered associative container of
415 // keys and associated values designed to be a more efficient replacement
416 // for `std::multiset` (in most cases). Unlike `absl::btree_set`, a B-tree
417 // multiset allows equivalent elements.
418 //
419 // Keys are sorted using an (optional) comparison function, which defaults to
420 // `std::less<K>`.
421 //
422 // An `absl::btree_multiset<K>` uses a default allocator of `std::allocator<K>`
423 // to allocate (and deallocate) nodes, and construct and destruct values within
424 // those nodes. You may instead specify a custom allocator `A` (which in turn
425 // requires specifying a custom comparator `C`) as in
426 // `absl::btree_multiset<K, C, A>`.
427 //
428 template <typename Key, typename Compare = std::less<Key>,
429 typename Alloc = std::allocator<Key>>
430 class btree_multiset
431 : public container_internal::btree_multiset_container<
432 container_internal::btree<container_internal::set_params<
433 Key, Compare, Alloc, /*TargetNodeSize=*/256,
434 /*IsMulti=*/true>>> {
435 using Base = typename btree_multiset::btree_multiset_container;
436
437 public:
438 // Constructors and Assignment Operators
439 //
440 // A `btree_multiset` supports the same overload set as `std::set`
441 // for construction and assignment:
442 //
443 // * Default constructor
444 //
445 // absl::btree_multiset<std::string> set1;
446 //
447 // * Initializer List constructor
448 //
449 // absl::btree_multiset<std::string> set2 =
450 // {{"huey"}, {"dewey"}, {"louie"},};
451 //
452 // * Copy constructor
453 //
454 // absl::btree_multiset<std::string> set3(set2);
455 //
456 // * Copy assignment operator
457 //
458 // absl::btree_multiset<std::string> set4;
459 // set4 = set3;
460 //
461 // * Move constructor
462 //
463 // // Move is guaranteed efficient
464 // absl::btree_multiset<std::string> set5(std::move(set4));
465 //
466 // * Move assignment operator
467 //
468 // // May be efficient if allocators are compatible
469 // absl::btree_multiset<std::string> set6;
470 // set6 = std::move(set5);
471 //
472 // * Range constructor
473 //
474 // std::vector<std::string> v = {"a", "b"};
475 // absl::btree_multiset<std::string> set7(v.begin(), v.end());
btree_multiset()476 btree_multiset() {}
477 using Base::Base;
478
479 // btree_multiset::begin()
480 //
481 // Returns an iterator to the beginning of the `btree_multiset`.
482 using Base::begin;
483
484 // btree_multiset::cbegin()
485 //
486 // Returns a const iterator to the beginning of the `btree_multiset`.
487 using Base::cbegin;
488
489 // btree_multiset::end()
490 //
491 // Returns an iterator to the end of the `btree_multiset`.
492 using Base::end;
493
494 // btree_multiset::cend()
495 //
496 // Returns a const iterator to the end of the `btree_multiset`.
497 using Base::cend;
498
499 // btree_multiset::empty()
500 //
501 // Returns whether or not the `btree_multiset` is empty.
502 using Base::empty;
503
504 // btree_multiset::max_size()
505 //
506 // Returns the largest theoretical possible number of elements within a
507 // `btree_multiset` under current memory constraints. This value can be
508 // thought of as the largest value of `std::distance(begin(), end())` for a
509 // `btree_multiset<Key>`.
510 using Base::max_size;
511
512 // btree_multiset::size()
513 //
514 // Returns the number of elements currently within the `btree_multiset`.
515 using Base::size;
516
517 // btree_multiset::clear()
518 //
519 // Removes all elements from the `btree_multiset`. Invalidates any references,
520 // pointers, or iterators referring to contained elements.
521 using Base::clear;
522
523 // btree_multiset::erase()
524 //
525 // Erases elements within the `btree_multiset`. Overloads are listed below.
526 //
527 // iterator erase(iterator position):
528 // iterator erase(const_iterator position):
529 //
530 // Erases the element at `position` of the `btree_multiset`, returning
531 // the iterator pointing to the element after the one that was erased
532 // (or end() if none exists).
533 //
534 // iterator erase(const_iterator first, const_iterator last):
535 //
536 // Erases the elements in the open interval [`first`, `last`), returning
537 // the iterator pointing to the element after the interval that was erased
538 // (or end() if none exists).
539 //
540 // template <typename K> size_type erase(const K& key):
541 //
542 // Erases the elements matching the key, if any exist, returning the
543 // number of elements erased.
544 using Base::erase;
545
546 // btree_multiset::insert()
547 //
548 // Inserts an element of the specified value into the `btree_multiset`,
549 // returning an iterator pointing to the newly inserted element.
550 // Any references, pointers, or iterators are invalidated. Overloads are
551 // listed below.
552 //
553 // iterator insert(const value_type& value):
554 //
555 // Inserts a value into the `btree_multiset`, returning an iterator to the
556 // inserted element.
557 //
558 // iterator insert(value_type&& value):
559 //
560 // Inserts a moveable value into the `btree_multiset`, returning an iterator
561 // to the inserted element.
562 //
563 // iterator insert(const_iterator hint, const value_type& value):
564 // iterator insert(const_iterator hint, value_type&& value):
565 //
566 // Inserts a value, using the position of `hint` as a non-binding suggestion
567 // for where to begin the insertion search. Returns an iterator to the
568 // inserted element.
569 //
570 // void insert(InputIterator first, InputIterator last):
571 //
572 // Inserts a range of values [`first`, `last`).
573 //
574 // void insert(std::initializer_list<init_type> ilist):
575 //
576 // Inserts the elements within the initializer list `ilist`.
577 using Base::insert;
578
579 // btree_multiset::emplace()
580 //
581 // Inserts an element of the specified value by constructing it in-place
582 // within the `btree_multiset`. Any references, pointers, or iterators are
583 // invalidated.
584 using Base::emplace;
585
586 // btree_multiset::emplace_hint()
587 //
588 // Inserts an element of the specified value by constructing it in-place
589 // within the `btree_multiset`, using the position of `hint` as a non-binding
590 // suggestion for where to begin the insertion search.
591 //
592 // Any references, pointers, or iterators are invalidated.
593 using Base::emplace_hint;
594
595 // btree_multiset::extract()
596 //
597 // Extracts the indicated element, erasing it in the process, and returns it
598 // as a C++17-compatible node handle. Overloads are listed below.
599 //
600 // node_type extract(const_iterator position):
601 //
602 // Extracts the element at the indicated position and returns a node handle
603 // owning that extracted data.
604 //
605 // template <typename K> node_type extract(const K& k):
606 //
607 // Extracts the element with the key matching the passed key value and
608 // returns a node handle owning that extracted data. If the `btree_multiset`
609 // does not contain an element with a matching key, this function returns an
610 // empty node handle.
611 //
612 // NOTE: In this context, `node_type` refers to the C++17 concept of a
613 // move-only type that owns and provides access to the elements in associative
614 // containers (https://en.cppreference.com/w/cpp/container/node_handle).
615 // It does NOT refer to the data layout of the underlying btree.
616 using Base::extract;
617
618 // btree_multiset::merge()
619 //
620 // Extracts all elements from a given `source` btree_multiset into this
621 // `btree_multiset`.
622 using Base::merge;
623
624 // btree_multiset::swap(btree_multiset& other)
625 //
626 // Exchanges the contents of this `btree_multiset` with those of the `other`
627 // btree_multiset, avoiding invocation of any move, copy, or swap operations
628 // on individual elements.
629 //
630 // All iterators and references on the `btree_multiset` remain valid,
631 // excepting for the past-the-end iterator, which is invalidated.
632 using Base::swap;
633
634 // btree_multiset::contains()
635 //
636 // template <typename K> bool contains(const K& key) const:
637 //
638 // Determines whether an element comparing equal to the given `key` exists
639 // within the `btree_multiset`, returning `true` if so or `false` otherwise.
640 //
641 // Supports heterogeneous lookup, provided that the set has a compatible
642 // heterogeneous comparator.
643 using Base::contains;
644
645 // btree_multiset::count()
646 //
647 // template <typename K> size_type count(const K& key) const:
648 //
649 // Returns the number of elements comparing equal to the given `key` within
650 // the `btree_multiset`.
651 //
652 // Supports heterogeneous lookup, provided that the set has a compatible
653 // heterogeneous comparator.
654 using Base::count;
655
656 // btree_multiset::equal_range()
657 //
658 // Returns a closed range [first, last], defined by a `std::pair` of two
659 // iterators, containing all elements with the passed key in the
660 // `btree_multiset`.
661 using Base::equal_range;
662
663 // btree_multiset::find()
664 //
665 // template <typename K> iterator find(const K& key):
666 // template <typename K> const_iterator find(const K& key) const:
667 //
668 // Finds an element with the passed `key` within the `btree_multiset`.
669 //
670 // Supports heterogeneous lookup, provided that the set has a compatible
671 // heterogeneous comparator.
672 using Base::find;
673
674 // btree_multiset::lower_bound()
675 //
676 // template <typename K> iterator lower_bound(const K& key):
677 // template <typename K> const_iterator lower_bound(const K& key) const:
678 //
679 // Finds the first element that is not less than `key` within the
680 // `btree_multiset`.
681 //
682 // Supports heterogeneous lookup, provided that the set has a compatible
683 // heterogeneous comparator.
684 using Base::lower_bound;
685
686 // btree_multiset::upper_bound()
687 //
688 // template <typename K> iterator upper_bound(const K& key):
689 // template <typename K> const_iterator upper_bound(const K& key) const:
690 //
691 // Finds the first element that is greater than `key` within the
692 // `btree_multiset`.
693 //
694 // Supports heterogeneous lookup, provided that the set has a compatible
695 // heterogeneous comparator.
696 using Base::upper_bound;
697
698 // btree_multiset::get_allocator()
699 //
700 // Returns the allocator function associated with this `btree_multiset`.
701 using Base::get_allocator;
702
703 // btree_multiset::key_comp();
704 //
705 // Returns the key comparator associated with this `btree_multiset`.
706 using Base::key_comp;
707
708 // btree_multiset::value_comp();
709 //
710 // Returns the value comparator associated with this `btree_multiset`. The
711 // keys to sort the elements are the values themselves, therefore `value_comp`
712 // and its sibling member function `key_comp` are equivalent.
713 using Base::value_comp;
714 };
715
716 // absl::swap(absl::btree_multiset<>, absl::btree_multiset<>)
717 //
718 // Swaps the contents of two `absl::btree_multiset` containers.
719 template <typename K, typename C, typename A>
swap(btree_multiset<K,C,A> & x,btree_multiset<K,C,A> & y)720 void swap(btree_multiset<K, C, A> &x, btree_multiset<K, C, A> &y) {
721 return x.swap(y);
722 }
723
724 // absl::erase_if(absl::btree_multiset<>, Pred)
725 //
726 // Erases all elements that satisfy the predicate pred from the container.
727 // Returns the number of erased elements.
728 template <typename K, typename C, typename A, typename Pred>
erase_if(btree_multiset<K,C,A> & set,Pred pred)729 typename btree_multiset<K, C, A>::size_type erase_if(
730 btree_multiset<K, C, A> & set, Pred pred) {
731 return container_internal::btree_access::erase_if(set, std::move(pred));
732 }
733
734 namespace container_internal {
735
736 // This type implements the necessary functions from the
737 // absl::container_internal::slot_type interface for btree_(multi)set.
738 template <typename Key>
739 struct set_slot_policy {
740 using slot_type = Key;
741 using value_type = Key;
742 using mutable_value_type = Key;
743
elementset_slot_policy744 static value_type &element(slot_type *slot) { return *slot; }
elementset_slot_policy745 static const value_type &element(const slot_type *slot) { return *slot; }
746
747 template <typename Alloc, class... Args>
constructset_slot_policy748 static void construct(Alloc *alloc, slot_type *slot, Args &&...args) {
749 absl::allocator_traits<Alloc>::construct(*alloc, slot,
750 std::forward<Args>(args)...);
751 }
752
753 template <typename Alloc>
constructset_slot_policy754 static void construct(Alloc *alloc, slot_type *slot, slot_type *other) {
755 absl::allocator_traits<Alloc>::construct(*alloc, slot, std::move(*other));
756 }
757
758 template <typename Alloc>
constructset_slot_policy759 static void construct(Alloc *alloc, slot_type *slot, const slot_type *other) {
760 absl::allocator_traits<Alloc>::construct(*alloc, slot, *other);
761 }
762
763 template <typename Alloc>
destroyset_slot_policy764 static void destroy(Alloc *alloc, slot_type *slot) {
765 absl::allocator_traits<Alloc>::destroy(*alloc, slot);
766 }
767 };
768
769 // A parameters structure for holding the type parameters for a btree_set.
770 // Compare and Alloc should be nothrow copy-constructible.
771 template <typename Key, typename Compare, typename Alloc, int TargetNodeSize,
772 bool IsMulti>
773 struct set_params : common_params<Key, Compare, Alloc, TargetNodeSize, IsMulti,
774 /*IsMap=*/false, set_slot_policy<Key>> {
775 using value_type = Key;
776 using slot_type = typename set_params::common_params::slot_type;
777
778 template <typename V>
keyset_params779 static const V &key(const V &value) {
780 return value;
781 }
keyset_params782 static const Key &key(const slot_type *slot) { return *slot; }
keyset_params783 static const Key &key(slot_type *slot) { return *slot; }
784 };
785
786 } // namespace container_internal
787
788 ABSL_NAMESPACE_END
789 } // namespace absl
790
791 #endif // ABSL_CONTAINER_BTREE_SET_H_
792