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