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