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