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