Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-30 09:31:41

0001 // Copyright 2018 The Abseil Authors.
0002 //
0003 // Licensed under the Apache License, Version 2.0 (the "License");
0004 // you may not use this file except in compliance with the License.
0005 // You may obtain a copy of the License at
0006 //
0007 //      https://www.apache.org/licenses/LICENSE-2.0
0008 //
0009 // Unless required by applicable law or agreed to in writing, software
0010 // distributed under the License is distributed on an "AS IS" BASIS,
0011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0012 // See the License for the specific language governing permissions and
0013 // limitations under the License.
0014 //
0015 // -----------------------------------------------------------------------------
0016 // File: btree_map.h
0017 // -----------------------------------------------------------------------------
0018 //
0019 // This header file defines B-tree maps: sorted associative containers mapping
0020 // keys to values.
0021 //
0022 //     * `absl::btree_map<>`
0023 //     * `absl::btree_multimap<>`
0024 //
0025 // These B-tree types are similar to the corresponding types in the STL
0026 // (`std::map` and `std::multimap`) and generally conform to the STL interfaces
0027 // of those types. However, because they are implemented using B-trees, they
0028 // are more efficient in most situations.
0029 //
0030 // Unlike `std::map` and `std::multimap`, which are commonly implemented using
0031 // red-black tree nodes, B-tree maps use more generic B-tree nodes able to hold
0032 // multiple values per node. Holding multiple values per node often makes
0033 // B-tree maps perform better than their `std::map` counterparts, because
0034 // multiple entries can be checked within the same cache hit.
0035 //
0036 // However, these types should not be considered drop-in replacements for
0037 // `std::map` and `std::multimap` as there are some API differences, which are
0038 // noted in this header file. The most consequential differences with respect to
0039 // migrating to b-tree from the STL types are listed in the next paragraph.
0040 // Other API differences are minor.
0041 //
0042 // Importantly, insertions and deletions may invalidate outstanding iterators,
0043 // pointers, and references to elements. Such invalidations are typically only
0044 // an issue if insertion and deletion operations are interleaved with the use of
0045 // more than one iterator, pointer, or reference simultaneously.  For this
0046 // reason, `insert()`, `erase()`, and `extract_and_get_next()` return a valid
0047 // iterator at the current position. Another important difference is that
0048 // key-types must be copy-constructible.
0049 //
0050 // Another API difference is that btree iterators can be subtracted, and this
0051 // is faster than using std::distance.
0052 //
0053 // B-tree maps are not exception-safe.
0054 
0055 #ifndef ABSL_CONTAINER_BTREE_MAP_H_
0056 #define ABSL_CONTAINER_BTREE_MAP_H_
0057 
0058 #include "absl/base/attributes.h"
0059 #include "absl/container/internal/btree.h"  // IWYU pragma: export
0060 #include "absl/container/internal/btree_container.h"  // IWYU pragma: export
0061 
0062 namespace absl {
0063 ABSL_NAMESPACE_BEGIN
0064 
0065 namespace container_internal {
0066 
0067 template <typename Key, typename Data, typename Compare, typename Alloc,
0068           int TargetNodeSize, bool IsMulti>
0069 struct map_params;
0070 
0071 }  // namespace container_internal
0072 
0073 // absl::btree_map<>
0074 //
0075 // An `absl::btree_map<K, V>` is an ordered associative container of
0076 // unique keys and associated values designed to be a more efficient replacement
0077 // for `std::map` (in most cases).
0078 //
0079 // Keys are sorted using an (optional) comparison function, which defaults to
0080 // `std::less<K>`.
0081 //
0082 // An `absl::btree_map<K, V>` uses a default allocator of
0083 // `std::allocator<std::pair<const K, V>>` to allocate (and deallocate)
0084 // nodes, and construct and destruct values within those nodes. You may
0085 // instead specify a custom allocator `A` (which in turn requires specifying a
0086 // custom comparator `C`) as in `absl::btree_map<K, V, C, A>`.
0087 //
0088 template <typename Key, typename Value, typename Compare = std::less<Key>,
0089           typename Alloc = std::allocator<std::pair<const Key, Value>>>
0090 class ABSL_INTERNAL_ATTRIBUTE_OWNER btree_map
0091     : public container_internal::btree_map_container<
0092           container_internal::btree<container_internal::map_params<
0093               Key, Value, Compare, Alloc, /*TargetNodeSize=*/256,
0094               /*IsMulti=*/false>>> {
0095   using Base = typename btree_map::btree_map_container;
0096 
0097  public:
0098   // Constructors and Assignment Operators
0099   //
0100   // A `btree_map` supports the same overload set as `std::map`
0101   // for construction and assignment:
0102   //
0103   // * Default constructor
0104   //
0105   //   absl::btree_map<int, std::string> map1;
0106   //
0107   // * Initializer List constructor
0108   //
0109   //   absl::btree_map<int, std::string> map2 =
0110   //       {{1, "huey"}, {2, "dewey"}, {3, "louie"},};
0111   //
0112   // * Copy constructor
0113   //
0114   //   absl::btree_map<int, std::string> map3(map2);
0115   //
0116   // * Copy assignment operator
0117   //
0118   //  absl::btree_map<int, std::string> map4;
0119   //  map4 = map3;
0120   //
0121   // * Move constructor
0122   //
0123   //   // Move is guaranteed efficient
0124   //   absl::btree_map<int, std::string> map5(std::move(map4));
0125   //
0126   // * Move assignment operator
0127   //
0128   //   // May be efficient if allocators are compatible
0129   //   absl::btree_map<int, std::string> map6;
0130   //   map6 = std::move(map5);
0131   //
0132   // * Range constructor
0133   //
0134   //   std::vector<std::pair<int, std::string>> v = {{1, "a"}, {2, "b"}};
0135   //   absl::btree_map<int, std::string> map7(v.begin(), v.end());
0136   btree_map() {}
0137   using Base::Base;
0138 
0139   // btree_map::begin()
0140   //
0141   // Returns an iterator to the beginning of the `btree_map`.
0142   using Base::begin;
0143 
0144   // btree_map::cbegin()
0145   //
0146   // Returns a const iterator to the beginning of the `btree_map`.
0147   using Base::cbegin;
0148 
0149   // btree_map::end()
0150   //
0151   // Returns an iterator to the end of the `btree_map`.
0152   using Base::end;
0153 
0154   // btree_map::cend()
0155   //
0156   // Returns a const iterator to the end of the `btree_map`.
0157   using Base::cend;
0158 
0159   // btree_map::empty()
0160   //
0161   // Returns whether or not the `btree_map` is empty.
0162   using Base::empty;
0163 
0164   // btree_map::max_size()
0165   //
0166   // Returns the largest theoretical possible number of elements within a
0167   // `btree_map` under current memory constraints. This value can be thought
0168   // of as the largest value of `std::distance(begin(), end())` for a
0169   // `btree_map<Key, T>`.
0170   using Base::max_size;
0171 
0172   // btree_map::size()
0173   //
0174   // Returns the number of elements currently within the `btree_map`.
0175   using Base::size;
0176 
0177   // btree_map::clear()
0178   //
0179   // Removes all elements from the `btree_map`. Invalidates any references,
0180   // pointers, or iterators referring to contained elements.
0181   using Base::clear;
0182 
0183   // btree_map::erase()
0184   //
0185   // Erases elements within the `btree_map`. If an erase occurs, any references,
0186   // pointers, or iterators are invalidated.
0187   // Overloads are listed below.
0188   //
0189   // iterator erase(iterator position):
0190   // iterator erase(const_iterator position):
0191   //
0192   //   Erases the element at `position` of the `btree_map`, returning
0193   //   the iterator pointing to the element after the one that was erased
0194   //   (or end() if none exists).
0195   //
0196   // iterator erase(const_iterator first, const_iterator last):
0197   //
0198   //   Erases the elements in the open interval [`first`, `last`), returning
0199   //   the iterator pointing to the element after the interval that was erased
0200   //   (or end() if none exists).
0201   //
0202   // template <typename K> size_type erase(const K& key):
0203   //
0204   //   Erases the element with the matching key, if it exists, returning the
0205   //   number of elements erased (0 or 1).
0206   using Base::erase;
0207 
0208   // btree_map::insert()
0209   //
0210   // Inserts an element of the specified value into the `btree_map`,
0211   // returning an iterator pointing to the newly inserted element, provided that
0212   // an element with the given key does not already exist. If an insertion
0213   // occurs, any references, pointers, or iterators are invalidated.
0214   // Overloads are listed below.
0215   //
0216   // std::pair<iterator,bool> insert(const value_type& value):
0217   //
0218   //   Inserts a value into the `btree_map`. Returns a pair consisting of an
0219   //   iterator to the inserted element (or to the element that prevented the
0220   //   insertion) and a bool denoting whether the insertion took place.
0221   //
0222   // std::pair<iterator,bool> insert(value_type&& value):
0223   //
0224   //   Inserts a moveable value into the `btree_map`. Returns a pair
0225   //   consisting of an iterator to the inserted element (or to the element that
0226   //   prevented the insertion) and a bool denoting whether the insertion took
0227   //   place.
0228   //
0229   // iterator insert(const_iterator hint, const value_type& value):
0230   // iterator insert(const_iterator hint, value_type&& value):
0231   //
0232   //   Inserts a value, using the position of `hint` as a non-binding suggestion
0233   //   for where to begin the insertion search. Returns an iterator to the
0234   //   inserted element, or to the existing element that prevented the
0235   //   insertion.
0236   //
0237   // void insert(InputIterator first, InputIterator last):
0238   //
0239   //   Inserts a range of values [`first`, `last`).
0240   //
0241   // void insert(std::initializer_list<init_type> ilist):
0242   //
0243   //   Inserts the elements within the initializer list `ilist`.
0244   using Base::insert;
0245 
0246   // btree_map::insert_or_assign()
0247   //
0248   // Inserts an element of the specified value into the `btree_map` provided
0249   // that a value with the given key does not already exist, or replaces the
0250   // corresponding mapped type with the forwarded `obj` argument if a key for
0251   // that value already exists, returning an iterator pointing to the newly
0252   // inserted element. Overloads are listed below.
0253   //
0254   // pair<iterator, bool> insert_or_assign(const key_type& k, M&& obj):
0255   // pair<iterator, bool> insert_or_assign(key_type&& k, M&& obj):
0256   //
0257   //   Inserts/Assigns (or moves) the element of the specified key into the
0258   //   `btree_map`. If the returned bool is true, insertion took place, and if
0259   //   it's false, assignment took place.
0260   //
0261   // iterator insert_or_assign(const_iterator hint,
0262   //                           const key_type& k, M&& obj):
0263   // iterator insert_or_assign(const_iterator hint, key_type&& k, M&& obj):
0264   //
0265   //   Inserts/Assigns (or moves) the element of the specified key into the
0266   //   `btree_map` using the position of `hint` as a non-binding suggestion
0267   //   for where to begin the insertion search.
0268   using Base::insert_or_assign;
0269 
0270   // btree_map::emplace()
0271   //
0272   // Inserts an element of the specified value by constructing it in-place
0273   // within the `btree_map`, provided that no element with the given key
0274   // already exists.
0275   //
0276   // The element may be constructed even if there already is an element with the
0277   // key in the container, in which case the newly constructed element will be
0278   // destroyed immediately. Prefer `try_emplace()` unless your key is not
0279   // copyable or moveable.
0280   //
0281   // If an insertion occurs, any references, pointers, or iterators are
0282   // invalidated.
0283   using Base::emplace;
0284 
0285   // btree_map::emplace_hint()
0286   //
0287   // Inserts an element of the specified value by constructing it in-place
0288   // within the `btree_map`, using the position of `hint` as a non-binding
0289   // suggestion for where to begin the insertion search, and only inserts
0290   // provided that no element with the given key already exists.
0291   //
0292   // The element may be constructed even if there already is an element with the
0293   // key in the container, in which case the newly constructed element will be
0294   // destroyed immediately. Prefer `try_emplace()` unless your key is not
0295   // copyable or moveable.
0296   //
0297   // If an insertion occurs, any references, pointers, or iterators are
0298   // invalidated.
0299   using Base::emplace_hint;
0300 
0301   // btree_map::try_emplace()
0302   //
0303   // Inserts an element of the specified value by constructing it in-place
0304   // within the `btree_map`, provided that no element with the given key
0305   // already exists. Unlike `emplace()`, if an element with the given key
0306   // already exists, we guarantee that no element is constructed.
0307   //
0308   // If an insertion occurs, any references, pointers, or iterators are
0309   // invalidated.
0310   //
0311   // Overloads are listed below.
0312   //
0313   //   std::pair<iterator, bool> try_emplace(const key_type& k, Args&&... args):
0314   //   std::pair<iterator, bool> try_emplace(key_type&& k, Args&&... args):
0315   //
0316   // Inserts (via copy or move) the element of the specified key into the
0317   // `btree_map`.
0318   //
0319   //   iterator try_emplace(const_iterator hint,
0320   //                        const key_type& k, Args&&... args):
0321   //   iterator try_emplace(const_iterator hint, key_type&& k, Args&&... args):
0322   //
0323   // Inserts (via copy or move) the element of the specified key into the
0324   // `btree_map` using the position of `hint` as a non-binding suggestion
0325   // for where to begin the insertion search.
0326   using Base::try_emplace;
0327 
0328   // btree_map::extract()
0329   //
0330   // Extracts the indicated element, erasing it in the process, and returns it
0331   // as a C++17-compatible node handle. Any references, pointers, or iterators
0332   // are invalidated. Overloads are listed below.
0333   //
0334   // node_type extract(const_iterator position):
0335   //
0336   //   Extracts the element at the indicated position and returns a node handle
0337   //   owning that extracted data.
0338   //
0339   // template <typename K> node_type extract(const K& k):
0340   //
0341   //   Extracts the element with the key matching the passed key value and
0342   //   returns a node handle owning that extracted data. If the `btree_map`
0343   //   does not contain an element with a matching key, this function returns an
0344   //   empty node handle.
0345   //
0346   // NOTE: when compiled in an earlier version of C++ than C++17,
0347   // `node_type::key()` returns a const reference to the key instead of a
0348   // mutable reference. We cannot safely return a mutable reference without
0349   // std::launder (which is not available before C++17).
0350   //
0351   // NOTE: In this context, `node_type` refers to the C++17 concept of a
0352   // move-only type that owns and provides access to the elements in associative
0353   // containers (https://en.cppreference.com/w/cpp/container/node_handle).
0354   // It does NOT refer to the data layout of the underlying btree.
0355   using Base::extract;
0356 
0357   // btree_map::extract_and_get_next()
0358   //
0359   // Extracts the indicated element, erasing it in the process, and returns it
0360   // as a C++17-compatible node handle along with an iterator to the next
0361   // element.
0362   //
0363   // extract_and_get_next_return_type extract_and_get_next(
0364   //     const_iterator position):
0365   //
0366   //   Extracts the element at the indicated position, returns a struct
0367   //   containing a member named `node`: a node handle owning that extracted
0368   //   data and a member named `next`: an iterator pointing to the next element
0369   //   in the btree.
0370   using Base::extract_and_get_next;
0371 
0372   // btree_map::merge()
0373   //
0374   // Extracts elements from a given `source` btree_map into this
0375   // `btree_map`. If the destination `btree_map` already contains an
0376   // element with an equivalent key, that element is not extracted.
0377   using Base::merge;
0378 
0379   // btree_map::swap(btree_map& other)
0380   //
0381   // Exchanges the contents of this `btree_map` with those of the `other`
0382   // btree_map, avoiding invocation of any move, copy, or swap operations on
0383   // individual elements.
0384   //
0385   // All iterators and references on the `btree_map` remain valid, excepting
0386   // for the past-the-end iterator, which is invalidated.
0387   using Base::swap;
0388 
0389   // btree_map::at()
0390   //
0391   // Returns a reference to the mapped value of the element with key equivalent
0392   // to the passed key.
0393   using Base::at;
0394 
0395   // btree_map::contains()
0396   //
0397   // template <typename K> bool contains(const K& key) const:
0398   //
0399   // Determines whether an element comparing equal to the given `key` exists
0400   // within the `btree_map`, returning `true` if so or `false` otherwise.
0401   //
0402   // Supports heterogeneous lookup, provided that the map has a compatible
0403   // heterogeneous comparator.
0404   using Base::contains;
0405 
0406   // btree_map::count()
0407   //
0408   // template <typename K> size_type count(const K& key) const:
0409   //
0410   // Returns the number of elements comparing equal to the given `key` within
0411   // the `btree_map`. Note that this function will return either `1` or `0`
0412   // since duplicate elements are not allowed within a `btree_map`.
0413   //
0414   // Supports heterogeneous lookup, provided that the map has a compatible
0415   // heterogeneous comparator.
0416   using Base::count;
0417 
0418   // btree_map::equal_range()
0419   //
0420   // Returns a half-open range [first, last), defined by a `std::pair` of two
0421   // iterators, containing all elements with the passed key in the `btree_map`.
0422   using Base::equal_range;
0423 
0424   // btree_map::find()
0425   //
0426   // template <typename K> iterator find(const K& key):
0427   // template <typename K> const_iterator find(const K& key) const:
0428   //
0429   // Finds an element with the passed `key` within the `btree_map`.
0430   //
0431   // Supports heterogeneous lookup, provided that the map has a compatible
0432   // heterogeneous comparator.
0433   using Base::find;
0434 
0435   // btree_map::lower_bound()
0436   //
0437   // template <typename K> iterator lower_bound(const K& key):
0438   // template <typename K> const_iterator lower_bound(const K& key) const:
0439   //
0440   // Finds the first element with a key that is not less than `key` within the
0441   // `btree_map`.
0442   //
0443   // Supports heterogeneous lookup, provided that the map has a compatible
0444   // heterogeneous comparator.
0445   using Base::lower_bound;
0446 
0447   // btree_map::upper_bound()
0448   //
0449   // template <typename K> iterator upper_bound(const K& key):
0450   // template <typename K> const_iterator upper_bound(const K& key) const:
0451   //
0452   // Finds the first element with a key that is greater than `key` within the
0453   // `btree_map`.
0454   //
0455   // Supports heterogeneous lookup, provided that the map has a compatible
0456   // heterogeneous comparator.
0457   using Base::upper_bound;
0458 
0459   // btree_map::operator[]()
0460   //
0461   // Returns a reference to the value mapped to the passed key within the
0462   // `btree_map`, performing an `insert()` if the key does not already
0463   // exist.
0464   //
0465   // If an insertion occurs, any references, pointers, or iterators are
0466   // invalidated. Otherwise iterators are not affected and references are not
0467   // invalidated. Overloads are listed below.
0468   //
0469   // T& operator[](key_type&& key):
0470   // T& operator[](const key_type& key):
0471   //
0472   //   Inserts a value_type object constructed in-place if the element with the
0473   //   given key does not exist.
0474   using Base::operator[];
0475 
0476   // btree_map::get_allocator()
0477   //
0478   // Returns the allocator function associated with this `btree_map`.
0479   using Base::get_allocator;
0480 
0481   // btree_map::key_comp();
0482   //
0483   // Returns the key comparator associated with this `btree_map`.
0484   using Base::key_comp;
0485 
0486   // btree_map::value_comp();
0487   //
0488   // Returns the value comparator associated with this `btree_map`.
0489   using Base::value_comp;
0490 };
0491 
0492 // absl::swap(absl::btree_map<>, absl::btree_map<>)
0493 //
0494 // Swaps the contents of two `absl::btree_map` containers.
0495 template <typename K, typename V, typename C, typename A>
0496 void swap(btree_map<K, V, C, A> &x, btree_map<K, V, C, A> &y) {
0497   return x.swap(y);
0498 }
0499 
0500 // absl::erase_if(absl::btree_map<>, Pred)
0501 //
0502 // Erases all elements that satisfy the predicate pred from the container.
0503 // Returns the number of erased elements.
0504 template <typename K, typename V, typename C, typename A, typename Pred>
0505 typename btree_map<K, V, C, A>::size_type erase_if(
0506     btree_map<K, V, C, A> &map, Pred pred) {
0507   return container_internal::btree_access::erase_if(map, std::move(pred));
0508 }
0509 
0510 // absl::btree_multimap
0511 //
0512 // An `absl::btree_multimap<K, V>` is an ordered associative container of
0513 // keys and associated values designed to be a more efficient replacement for
0514 // `std::multimap` (in most cases). Unlike `absl::btree_map`, a B-tree multimap
0515 // allows multiple elements with equivalent keys.
0516 //
0517 // Keys are sorted using an (optional) comparison function, which defaults to
0518 // `std::less<K>`.
0519 //
0520 // An `absl::btree_multimap<K, V>` uses a default allocator of
0521 // `std::allocator<std::pair<const K, V>>` to allocate (and deallocate)
0522 // nodes, and construct and destruct values within those nodes. You may
0523 // instead specify a custom allocator `A` (which in turn requires specifying a
0524 // custom comparator `C`) as in `absl::btree_multimap<K, V, C, A>`.
0525 //
0526 template <typename Key, typename Value, typename Compare = std::less<Key>,
0527           typename Alloc = std::allocator<std::pair<const Key, Value>>>
0528 class ABSL_INTERNAL_ATTRIBUTE_OWNER btree_multimap
0529     : public container_internal::btree_multimap_container<
0530           container_internal::btree<container_internal::map_params<
0531               Key, Value, Compare, Alloc, /*TargetNodeSize=*/256,
0532               /*IsMulti=*/true>>> {
0533   using Base = typename btree_multimap::btree_multimap_container;
0534 
0535  public:
0536   // Constructors and Assignment Operators
0537   //
0538   // A `btree_multimap` supports the same overload set as `std::multimap`
0539   // for construction and assignment:
0540   //
0541   // * Default constructor
0542   //
0543   //   absl::btree_multimap<int, std::string> map1;
0544   //
0545   // * Initializer List constructor
0546   //
0547   //   absl::btree_multimap<int, std::string> map2 =
0548   //       {{1, "huey"}, {2, "dewey"}, {3, "louie"},};
0549   //
0550   // * Copy constructor
0551   //
0552   //   absl::btree_multimap<int, std::string> map3(map2);
0553   //
0554   // * Copy assignment operator
0555   //
0556   //  absl::btree_multimap<int, std::string> map4;
0557   //  map4 = map3;
0558   //
0559   // * Move constructor
0560   //
0561   //   // Move is guaranteed efficient
0562   //   absl::btree_multimap<int, std::string> map5(std::move(map4));
0563   //
0564   // * Move assignment operator
0565   //
0566   //   // May be efficient if allocators are compatible
0567   //   absl::btree_multimap<int, std::string> map6;
0568   //   map6 = std::move(map5);
0569   //
0570   // * Range constructor
0571   //
0572   //   std::vector<std::pair<int, std::string>> v = {{1, "a"}, {2, "b"}};
0573   //   absl::btree_multimap<int, std::string> map7(v.begin(), v.end());
0574   btree_multimap() {}
0575   using Base::Base;
0576 
0577   // btree_multimap::begin()
0578   //
0579   // Returns an iterator to the beginning of the `btree_multimap`.
0580   using Base::begin;
0581 
0582   // btree_multimap::cbegin()
0583   //
0584   // Returns a const iterator to the beginning of the `btree_multimap`.
0585   using Base::cbegin;
0586 
0587   // btree_multimap::end()
0588   //
0589   // Returns an iterator to the end of the `btree_multimap`.
0590   using Base::end;
0591 
0592   // btree_multimap::cend()
0593   //
0594   // Returns a const iterator to the end of the `btree_multimap`.
0595   using Base::cend;
0596 
0597   // btree_multimap::empty()
0598   //
0599   // Returns whether or not the `btree_multimap` is empty.
0600   using Base::empty;
0601 
0602   // btree_multimap::max_size()
0603   //
0604   // Returns the largest theoretical possible number of elements within a
0605   // `btree_multimap` under current memory constraints. This value can be
0606   // thought of as the largest value of `std::distance(begin(), end())` for a
0607   // `btree_multimap<Key, T>`.
0608   using Base::max_size;
0609 
0610   // btree_multimap::size()
0611   //
0612   // Returns the number of elements currently within the `btree_multimap`.
0613   using Base::size;
0614 
0615   // btree_multimap::clear()
0616   //
0617   // Removes all elements from the `btree_multimap`. Invalidates any references,
0618   // pointers, or iterators referring to contained elements.
0619   using Base::clear;
0620 
0621   // btree_multimap::erase()
0622   //
0623   // Erases elements within the `btree_multimap`. If an erase occurs, any
0624   // references, pointers, or iterators are invalidated.
0625   // Overloads are listed below.
0626   //
0627   // iterator erase(iterator position):
0628   // iterator erase(const_iterator position):
0629   //
0630   //   Erases the element at `position` of the `btree_multimap`, returning
0631   //   the iterator pointing to the element after the one that was erased
0632   //   (or end() if none exists).
0633   //
0634   // iterator erase(const_iterator first, const_iterator last):
0635   //
0636   //   Erases the elements in the open interval [`first`, `last`), returning
0637   //   the iterator pointing to the element after the interval that was erased
0638   //   (or end() if none exists).
0639   //
0640   // template <typename K> size_type erase(const K& key):
0641   //
0642   //   Erases the elements matching the key, if any exist, returning the
0643   //   number of elements erased.
0644   using Base::erase;
0645 
0646   // btree_multimap::insert()
0647   //
0648   // Inserts an element of the specified value into the `btree_multimap`,
0649   // returning an iterator pointing to the newly inserted element.
0650   // Any references, pointers, or iterators are invalidated.  Overloads are
0651   // listed below.
0652   //
0653   // iterator insert(const value_type& value):
0654   //
0655   //   Inserts a value into the `btree_multimap`, returning an iterator to the
0656   //   inserted element.
0657   //
0658   // iterator insert(value_type&& value):
0659   //
0660   //   Inserts a moveable value into the `btree_multimap`, returning an iterator
0661   //   to the inserted element.
0662   //
0663   // iterator insert(const_iterator hint, const value_type& value):
0664   // iterator insert(const_iterator hint, value_type&& value):
0665   //
0666   //   Inserts a value, using the position of `hint` as a non-binding suggestion
0667   //   for where to begin the insertion search. Returns an iterator to the
0668   //   inserted element.
0669   //
0670   // void insert(InputIterator first, InputIterator last):
0671   //
0672   //   Inserts a range of values [`first`, `last`).
0673   //
0674   // void insert(std::initializer_list<init_type> ilist):
0675   //
0676   //   Inserts the elements within the initializer list `ilist`.
0677   using Base::insert;
0678 
0679   // btree_multimap::emplace()
0680   //
0681   // Inserts an element of the specified value by constructing it in-place
0682   // within the `btree_multimap`. Any references, pointers, or iterators are
0683   // invalidated.
0684   using Base::emplace;
0685 
0686   // btree_multimap::emplace_hint()
0687   //
0688   // Inserts an element of the specified value by constructing it in-place
0689   // within the `btree_multimap`, using the position of `hint` as a non-binding
0690   // suggestion for where to begin the insertion search.
0691   //
0692   // Any references, pointers, or iterators are invalidated.
0693   using Base::emplace_hint;
0694 
0695   // btree_multimap::extract()
0696   //
0697   // Extracts the indicated element, erasing it in the process, and returns it
0698   // as a C++17-compatible node handle. Overloads are listed below.
0699   //
0700   // node_type extract(const_iterator position):
0701   //
0702   //   Extracts the element at the indicated position and returns a node handle
0703   //   owning that extracted data.
0704   //
0705   // template <typename K> node_type extract(const K& k):
0706   //
0707   //   Extracts the element with the key matching the passed key value and
0708   //   returns a node handle owning that extracted data. If the `btree_multimap`
0709   //   does not contain an element with a matching key, this function returns an
0710   //   empty node handle.
0711   //
0712   // NOTE: when compiled in an earlier version of C++ than C++17,
0713   // `node_type::key()` returns a const reference to the key instead of a
0714   // mutable reference. We cannot safely return a mutable reference without
0715   // std::launder (which is not available before C++17).
0716   //
0717   // NOTE: In this context, `node_type` refers to the C++17 concept of a
0718   // move-only type that owns and provides access to the elements in associative
0719   // containers (https://en.cppreference.com/w/cpp/container/node_handle).
0720   // It does NOT refer to the data layout of the underlying btree.
0721   using Base::extract;
0722 
0723   // btree_multimap::extract_and_get_next()
0724   //
0725   // Extracts the indicated element, erasing it in the process, and returns it
0726   // as a C++17-compatible node handle along with an iterator to the next
0727   // element.
0728   //
0729   // extract_and_get_next_return_type extract_and_get_next(
0730   //     const_iterator position):
0731   //
0732   //   Extracts the element at the indicated position, returns a struct
0733   //   containing a member named `node`: a node handle owning that extracted
0734   //   data and a member named `next`: an iterator pointing to the next element
0735   //   in the btree.
0736   using Base::extract_and_get_next;
0737 
0738   // btree_multimap::merge()
0739   //
0740   // Extracts all elements from a given `source` btree_multimap into this
0741   // `btree_multimap`.
0742   using Base::merge;
0743 
0744   // btree_multimap::swap(btree_multimap& other)
0745   //
0746   // Exchanges the contents of this `btree_multimap` with those of the `other`
0747   // btree_multimap, avoiding invocation of any move, copy, or swap operations
0748   // on individual elements.
0749   //
0750   // All iterators and references on the `btree_multimap` remain valid,
0751   // excepting for the past-the-end iterator, which is invalidated.
0752   using Base::swap;
0753 
0754   // btree_multimap::contains()
0755   //
0756   // template <typename K> bool contains(const K& key) const:
0757   //
0758   // Determines whether an element comparing equal to the given `key` exists
0759   // within the `btree_multimap`, returning `true` if so or `false` otherwise.
0760   //
0761   // Supports heterogeneous lookup, provided that the map has a compatible
0762   // heterogeneous comparator.
0763   using Base::contains;
0764 
0765   // btree_multimap::count()
0766   //
0767   // template <typename K> size_type count(const K& key) const:
0768   //
0769   // Returns the number of elements comparing equal to the given `key` within
0770   // the `btree_multimap`.
0771   //
0772   // Supports heterogeneous lookup, provided that the map has a compatible
0773   // heterogeneous comparator.
0774   using Base::count;
0775 
0776   // btree_multimap::equal_range()
0777   //
0778   // Returns a half-open range [first, last), defined by a `std::pair` of two
0779   // iterators, containing all elements with the passed key in the
0780   // `btree_multimap`.
0781   using Base::equal_range;
0782 
0783   // btree_multimap::find()
0784   //
0785   // template <typename K> iterator find(const K& key):
0786   // template <typename K> const_iterator find(const K& key) const:
0787   //
0788   // Finds an element with the passed `key` within the `btree_multimap`.
0789   //
0790   // Supports heterogeneous lookup, provided that the map has a compatible
0791   // heterogeneous comparator.
0792   using Base::find;
0793 
0794   // btree_multimap::lower_bound()
0795   //
0796   // template <typename K> iterator lower_bound(const K& key):
0797   // template <typename K> const_iterator lower_bound(const K& key) const:
0798   //
0799   // Finds the first element with a key that is not less than `key` within the
0800   // `btree_multimap`.
0801   //
0802   // Supports heterogeneous lookup, provided that the map has a compatible
0803   // heterogeneous comparator.
0804   using Base::lower_bound;
0805 
0806   // btree_multimap::upper_bound()
0807   //
0808   // template <typename K> iterator upper_bound(const K& key):
0809   // template <typename K> const_iterator upper_bound(const K& key) const:
0810   //
0811   // Finds the first element with a key that is greater than `key` within the
0812   // `btree_multimap`.
0813   //
0814   // Supports heterogeneous lookup, provided that the map has a compatible
0815   // heterogeneous comparator.
0816   using Base::upper_bound;
0817 
0818   // btree_multimap::get_allocator()
0819   //
0820   // Returns the allocator function associated with this `btree_multimap`.
0821   using Base::get_allocator;
0822 
0823   // btree_multimap::key_comp();
0824   //
0825   // Returns the key comparator associated with this `btree_multimap`.
0826   using Base::key_comp;
0827 
0828   // btree_multimap::value_comp();
0829   //
0830   // Returns the value comparator associated with this `btree_multimap`.
0831   using Base::value_comp;
0832 };
0833 
0834 // absl::swap(absl::btree_multimap<>, absl::btree_multimap<>)
0835 //
0836 // Swaps the contents of two `absl::btree_multimap` containers.
0837 template <typename K, typename V, typename C, typename A>
0838 void swap(btree_multimap<K, V, C, A> &x, btree_multimap<K, V, C, A> &y) {
0839   return x.swap(y);
0840 }
0841 
0842 // absl::erase_if(absl::btree_multimap<>, Pred)
0843 //
0844 // Erases all elements that satisfy the predicate pred from the container.
0845 // Returns the number of erased elements.
0846 template <typename K, typename V, typename C, typename A, typename Pred>
0847 typename btree_multimap<K, V, C, A>::size_type erase_if(
0848     btree_multimap<K, V, C, A> &map, Pred pred) {
0849   return container_internal::btree_access::erase_if(map, std::move(pred));
0850 }
0851 
0852 namespace container_internal {
0853 
0854 // A parameters structure for holding the type parameters for a btree_map.
0855 // Compare and Alloc should be nothrow copy-constructible.
0856 template <typename Key, typename Data, typename Compare, typename Alloc,
0857           int TargetNodeSize, bool IsMulti>
0858 struct map_params : common_params<Key, Compare, Alloc, TargetNodeSize, IsMulti,
0859                                   /*IsMap=*/true, map_slot_policy<Key, Data>> {
0860   using super_type = typename map_params::common_params;
0861   using mapped_type = Data;
0862   // This type allows us to move keys when it is safe to do so. It is safe
0863   // for maps in which value_type and mutable_value_type are layout compatible.
0864   using slot_policy = typename super_type::slot_policy;
0865   using slot_type = typename super_type::slot_type;
0866   using value_type = typename super_type::value_type;
0867   using init_type = typename super_type::init_type;
0868 
0869   template <typename V>
0870   static auto key(const V &value ABSL_ATTRIBUTE_LIFETIME_BOUND)
0871       -> decltype((value.first)) {
0872     return value.first;
0873   }
0874   static const Key &key(const slot_type *s) { return slot_policy::key(s); }
0875   static const Key &key(slot_type *s) { return slot_policy::key(s); }
0876   // For use in node handle.
0877   static auto mutable_key(slot_type *s)
0878       -> decltype(slot_policy::mutable_key(s)) {
0879     return slot_policy::mutable_key(s);
0880   }
0881   static mapped_type &value(value_type *value) { return value->second; }
0882 };
0883 
0884 }  // namespace container_internal
0885 
0886 ABSL_NAMESPACE_END
0887 }  // namespace absl
0888 
0889 #endif  // ABSL_CONTAINER_BTREE_MAP_H_