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 is provided a
370 // compatible 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 is provided a
382 // compatible heterogeneous comparator.
383 using Base::count;
384
385 // btree_map::equal_range()
386 //
387 // Returns a closed range [first, last], defined by a `std::pair` of two
388 // iterators, containing all elements with the passed key in the
389 // `btree_map`.
390 using Base::equal_range;
391
392 // btree_map::find()
393 //
394 // template <typename K> iterator find(const K& key):
395 // template <typename K> const_iterator find(const K& key) const:
396 //
397 // Finds an element with the passed `key` within the `btree_map`.
398 //
399 // Supports heterogeneous lookup, provided that the map is provided a
400 // compatible heterogeneous comparator.
401 using Base::find;
402
403 // btree_map::operator[]()
404 //
405 // Returns a reference to the value mapped to the passed key within the
406 // `btree_map`, performing an `insert()` if the key does not already
407 // exist.
408 //
409 // If an insertion occurs, any references, pointers, or iterators are
410 // invalidated. Otherwise iterators are not affected and references are not
411 // invalidated. Overloads are listed below.
412 //
413 // T& operator[](key_type&& key):
414 // T& operator[](const key_type& key):
415 //
416 // Inserts a value_type object constructed in-place if the element with the
417 // given key does not exist.
418 using Base::operator[];
419
420 // btree_map::get_allocator()
421 //
422 // Returns the allocator function associated with this `btree_map`.
423 using Base::get_allocator;
424
425 // btree_map::key_comp();
426 //
427 // Returns the key comparator associated with this `btree_map`.
428 using Base::key_comp;
429
430 // btree_map::value_comp();
431 //
432 // Returns the value comparator associated with this `btree_map`.
433 using Base::value_comp;
434 };
435
436 // absl::swap(absl::btree_map<>, absl::btree_map<>)
437 //
438 // Swaps the contents of two `absl::btree_map` containers.
439 template <typename K, typename V, typename C, typename A>
swap(btree_map<K,V,C,A> & x,btree_map<K,V,C,A> & y)440 void swap(btree_map<K, V, C, A> &x, btree_map<K, V, C, A> &y) {
441 return x.swap(y);
442 }
443
444 // absl::erase_if(absl::btree_map<>, Pred)
445 //
446 // Erases all elements that satisfy the predicate pred from the container.
447 template <typename K, typename V, typename C, typename A, typename Pred>
erase_if(btree_map<K,V,C,A> & map,Pred pred)448 void erase_if(btree_map<K, V, C, A> &map, Pred pred) {
449 for (auto it = map.begin(); it != map.end();) {
450 if (pred(*it)) {
451 it = map.erase(it);
452 } else {
453 ++it;
454 }
455 }
456 }
457
458 // absl::btree_multimap
459 //
460 // An `absl::btree_multimap<K, V>` is an ordered associative container of
461 // keys and associated values designed to be a more efficient replacement for
462 // `std::multimap` (in most cases). Unlike `absl::btree_map`, a B-tree multimap
463 // allows multiple elements with equivalent keys.
464 //
465 // Keys are sorted using an (optional) comparison function, which defaults to
466 // `std::less<K>`.
467 //
468 // An `absl::btree_multimap<K, V>` uses a default allocator of
469 // `std::allocator<std::pair<const K, V>>` to allocate (and deallocate)
470 // nodes, and construct and destruct values within those nodes. You may
471 // instead specify a custom allocator `A` (which in turn requires specifying a
472 // custom comparator `C`) as in `absl::btree_multimap<K, V, C, A>`.
473 //
474 template <typename Key, typename Value, typename Compare = std::less<Key>,
475 typename Alloc = std::allocator<std::pair<const Key, Value>>>
476 class btree_multimap
477 : public container_internal::btree_multimap_container<
478 container_internal::btree<container_internal::map_params<
479 Key, Value, Compare, Alloc, /*TargetNodeSize=*/256,
480 /*Multi=*/true>>> {
481 using Base = typename btree_multimap::btree_multimap_container;
482
483 public:
484 // Constructors and Assignment Operators
485 //
486 // A `btree_multimap` supports the same overload set as `std::multimap`
487 // for construction and assignment:
488 //
489 // * Default constructor
490 //
491 // absl::btree_multimap<int, std::string> map1;
492 //
493 // * Initializer List constructor
494 //
495 // absl::btree_multimap<int, std::string> map2 =
496 // {{1, "huey"}, {2, "dewey"}, {3, "louie"},};
497 //
498 // * Copy constructor
499 //
500 // absl::btree_multimap<int, std::string> map3(map2);
501 //
502 // * Copy assignment operator
503 //
504 // absl::btree_multimap<int, std::string> map4;
505 // map4 = map3;
506 //
507 // * Move constructor
508 //
509 // // Move is guaranteed efficient
510 // absl::btree_multimap<int, std::string> map5(std::move(map4));
511 //
512 // * Move assignment operator
513 //
514 // // May be efficient if allocators are compatible
515 // absl::btree_multimap<int, std::string> map6;
516 // map6 = std::move(map5);
517 //
518 // * Range constructor
519 //
520 // std::vector<std::pair<int, std::string>> v = {{1, "a"}, {2, "b"}};
521 // absl::btree_multimap<int, std::string> map7(v.begin(), v.end());
btree_multimap()522 btree_multimap() {}
523 using Base::Base;
524
525 // btree_multimap::begin()
526 //
527 // Returns an iterator to the beginning of the `btree_multimap`.
528 using Base::begin;
529
530 // btree_multimap::cbegin()
531 //
532 // Returns a const iterator to the beginning of the `btree_multimap`.
533 using Base::cbegin;
534
535 // btree_multimap::end()
536 //
537 // Returns an iterator to the end of the `btree_multimap`.
538 using Base::end;
539
540 // btree_multimap::cend()
541 //
542 // Returns a const iterator to the end of the `btree_multimap`.
543 using Base::cend;
544
545 // btree_multimap::empty()
546 //
547 // Returns whether or not the `btree_multimap` is empty.
548 using Base::empty;
549
550 // btree_multimap::max_size()
551 //
552 // Returns the largest theoretical possible number of elements within a
553 // `btree_multimap` under current memory constraints. This value can be
554 // thought of as the largest value of `std::distance(begin(), end())` for a
555 // `btree_multimap<Key, T>`.
556 using Base::max_size;
557
558 // btree_multimap::size()
559 //
560 // Returns the number of elements currently within the `btree_multimap`.
561 using Base::size;
562
563 // btree_multimap::clear()
564 //
565 // Removes all elements from the `btree_multimap`. Invalidates any references,
566 // pointers, or iterators referring to contained elements.
567 using Base::clear;
568
569 // btree_multimap::erase()
570 //
571 // Erases elements within the `btree_multimap`. If an erase occurs, any
572 // references, pointers, or iterators are invalidated.
573 // Overloads are listed below.
574 //
575 // iterator erase(iterator position):
576 // iterator erase(const_iterator position):
577 //
578 // Erases the element at `position` of the `btree_multimap`, returning
579 // the iterator pointing to the element after the one that was erased
580 // (or end() if none exists).
581 //
582 // iterator erase(const_iterator first, const_iterator last):
583 //
584 // Erases the elements in the open interval [`first`, `last`), returning
585 // the iterator pointing to the element after the interval that was erased
586 // (or end() if none exists).
587 //
588 // template <typename K> size_type erase(const K& key):
589 //
590 // Erases the elements matching the key, if any exist, returning the
591 // number of elements erased.
592 using Base::erase;
593
594 // btree_multimap::insert()
595 //
596 // Inserts an element of the specified value into the `btree_multimap`,
597 // returning an iterator pointing to the newly inserted element.
598 // Any references, pointers, or iterators are invalidated. Overloads are
599 // listed below.
600 //
601 // iterator insert(const value_type& value):
602 //
603 // Inserts a value into the `btree_multimap`, returning an iterator to the
604 // inserted element.
605 //
606 // iterator insert(value_type&& value):
607 //
608 // Inserts a moveable value into the `btree_multimap`, returning an iterator
609 // to the inserted element.
610 //
611 // iterator insert(const_iterator hint, const value_type& value):
612 // iterator insert(const_iterator hint, value_type&& value):
613 //
614 // Inserts a value, using the position of `hint` as a non-binding suggestion
615 // for where to begin the insertion search. Returns an iterator to the
616 // inserted element.
617 //
618 // void insert(InputIterator first, InputIterator last):
619 //
620 // Inserts a range of values [`first`, `last`).
621 //
622 // void insert(std::initializer_list<init_type> ilist):
623 //
624 // Inserts the elements within the initializer list `ilist`.
625 using Base::insert;
626
627 // btree_multimap::emplace()
628 //
629 // Inserts an element of the specified value by constructing it in-place
630 // within the `btree_multimap`. Any references, pointers, or iterators are
631 // invalidated.
632 using Base::emplace;
633
634 // btree_multimap::emplace_hint()
635 //
636 // Inserts an element of the specified value by constructing it in-place
637 // within the `btree_multimap`, using the position of `hint` as a non-binding
638 // suggestion for where to begin the insertion search.
639 //
640 // Any references, pointers, or iterators are invalidated.
641 using Base::emplace_hint;
642
643 // btree_multimap::extract()
644 //
645 // Extracts the indicated element, erasing it in the process, and returns it
646 // as a C++17-compatible node handle. Overloads are listed below.
647 //
648 // node_type extract(const_iterator position):
649 //
650 // Extracts the element at the indicated position and returns a node handle
651 // owning that extracted data.
652 //
653 // template <typename K> node_type extract(const K& k):
654 //
655 // Extracts the element with the key matching the passed key value and
656 // returns a node handle owning that extracted data. If the `btree_multimap`
657 // does not contain an element with a matching key, this function returns an
658 // empty node handle.
659 //
660 // NOTE: when compiled in an earlier version of C++ than C++17,
661 // `node_type::key()` returns a const reference to the key instead of a
662 // mutable reference. We cannot safely return a mutable reference without
663 // std::launder (which is not available before C++17).
664 //
665 // NOTE: In this context, `node_type` refers to the C++17 concept of a
666 // move-only type that owns and provides access to the elements in associative
667 // containers (https://en.cppreference.com/w/cpp/container/node_handle).
668 // It does NOT refer to the data layout of the underlying btree.
669 using Base::extract;
670
671 // btree_multimap::merge()
672 //
673 // Extracts elements from a given `source` btree_multimap into this
674 // `btree_multimap`. If the destination `btree_multimap` already contains an
675 // element with an equivalent key, that element is not extracted.
676 using Base::merge;
677
678 // btree_multimap::swap(btree_multimap& other)
679 //
680 // Exchanges the contents of this `btree_multimap` with those of the `other`
681 // btree_multimap, avoiding invocation of any move, copy, or swap operations
682 // on individual elements.
683 //
684 // All iterators and references on the `btree_multimap` remain valid,
685 // excepting for the past-the-end iterator, which is invalidated.
686 using Base::swap;
687
688 // btree_multimap::contains()
689 //
690 // template <typename K> bool contains(const K& key) const:
691 //
692 // Determines whether an element comparing equal to the given `key` exists
693 // within the `btree_multimap`, returning `true` if so or `false` otherwise.
694 //
695 // Supports heterogeneous lookup, provided that the map is provided a
696 // compatible heterogeneous comparator.
697 using Base::contains;
698
699 // btree_multimap::count()
700 //
701 // template <typename K> size_type count(const K& key) const:
702 //
703 // Returns the number of elements comparing equal to the given `key` within
704 // the `btree_multimap`.
705 //
706 // Supports heterogeneous lookup, provided that the map is provided a
707 // compatible heterogeneous comparator.
708 using Base::count;
709
710 // btree_multimap::equal_range()
711 //
712 // Returns a closed range [first, last], defined by a `std::pair` of two
713 // iterators, containing all elements with the passed key in the
714 // `btree_multimap`.
715 using Base::equal_range;
716
717 // btree_multimap::find()
718 //
719 // template <typename K> iterator find(const K& key):
720 // template <typename K> const_iterator find(const K& key) const:
721 //
722 // Finds an element with the passed `key` within the `btree_multimap`.
723 //
724 // Supports heterogeneous lookup, provided that the map is provided a
725 // compatible heterogeneous comparator.
726 using Base::find;
727
728 // btree_multimap::get_allocator()
729 //
730 // Returns the allocator function associated with this `btree_multimap`.
731 using Base::get_allocator;
732
733 // btree_multimap::key_comp();
734 //
735 // Returns the key comparator associated with this `btree_multimap`.
736 using Base::key_comp;
737
738 // btree_multimap::value_comp();
739 //
740 // Returns the value comparator associated with this `btree_multimap`.
741 using Base::value_comp;
742 };
743
744 // absl::swap(absl::btree_multimap<>, absl::btree_multimap<>)
745 //
746 // Swaps the contents of two `absl::btree_multimap` containers.
747 template <typename K, typename V, typename C, typename A>
swap(btree_multimap<K,V,C,A> & x,btree_multimap<K,V,C,A> & y)748 void swap(btree_multimap<K, V, C, A> &x, btree_multimap<K, V, C, A> &y) {
749 return x.swap(y);
750 }
751
752 // absl::erase_if(absl::btree_multimap<>, Pred)
753 //
754 // Erases all elements that satisfy the predicate pred from the container.
755 template <typename K, typename V, typename C, typename A, typename Pred>
erase_if(btree_multimap<K,V,C,A> & map,Pred pred)756 void erase_if(btree_multimap<K, V, C, A> &map, Pred pred) {
757 for (auto it = map.begin(); it != map.end();) {
758 if (pred(*it)) {
759 it = map.erase(it);
760 } else {
761 ++it;
762 }
763 }
764 }
765
766 ABSL_NAMESPACE_END
767 } // namespace absl
768
769 #endif // ABSL_CONTAINER_BTREE_MAP_H_
770