xref: /aosp_15_r20/external/abseil-cpp/absl/container/btree_set.h (revision 9356374a3709195abf420251b3e825997ff56c0f)
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_set.h
17*9356374aSAndroid Build Coastguard Worker // -----------------------------------------------------------------------------
18*9356374aSAndroid Build Coastguard Worker //
19*9356374aSAndroid Build Coastguard Worker // This header file defines B-tree sets: sorted associative containers of
20*9356374aSAndroid Build Coastguard Worker // values.
21*9356374aSAndroid Build Coastguard Worker //
22*9356374aSAndroid Build Coastguard Worker //     * `absl::btree_set<>`
23*9356374aSAndroid Build Coastguard Worker //     * `absl::btree_multiset<>`
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::set` and `std::multiset`) 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::set` and `std::multiset`, which are commonly implemented using
31*9356374aSAndroid Build Coastguard Worker // red-black tree nodes, B-tree sets 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 sets perform better than their `std::set` 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::set` and `std::multiset` 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.
48*9356374aSAndroid Build Coastguard Worker //
49*9356374aSAndroid Build Coastguard Worker // Another API difference is that btree iterators can be subtracted, and this
50*9356374aSAndroid Build Coastguard Worker // is faster than using std::distance.
51*9356374aSAndroid Build Coastguard Worker //
52*9356374aSAndroid Build Coastguard Worker // B-tree sets are not exception-safe.
53*9356374aSAndroid Build Coastguard Worker 
54*9356374aSAndroid Build Coastguard Worker #ifndef ABSL_CONTAINER_BTREE_SET_H_
55*9356374aSAndroid Build Coastguard Worker #define ABSL_CONTAINER_BTREE_SET_H_
56*9356374aSAndroid Build Coastguard Worker 
57*9356374aSAndroid Build Coastguard Worker #include "absl/base/attributes.h"
58*9356374aSAndroid Build Coastguard Worker #include "absl/container/internal/btree.h"  // IWYU pragma: export
59*9356374aSAndroid Build Coastguard Worker #include "absl/container/internal/btree_container.h"  // IWYU pragma: export
60*9356374aSAndroid Build Coastguard Worker 
61*9356374aSAndroid Build Coastguard Worker namespace absl {
62*9356374aSAndroid Build Coastguard Worker ABSL_NAMESPACE_BEGIN
63*9356374aSAndroid Build Coastguard Worker 
64*9356374aSAndroid Build Coastguard Worker namespace container_internal {
65*9356374aSAndroid Build Coastguard Worker 
66*9356374aSAndroid Build Coastguard Worker template <typename Key>
67*9356374aSAndroid Build Coastguard Worker struct set_slot_policy;
68*9356374aSAndroid Build Coastguard Worker 
69*9356374aSAndroid Build Coastguard Worker template <typename Key, typename Compare, typename Alloc, int TargetNodeSize,
70*9356374aSAndroid Build Coastguard Worker           bool IsMulti>
71*9356374aSAndroid Build Coastguard Worker struct set_params;
72*9356374aSAndroid Build Coastguard Worker 
73*9356374aSAndroid Build Coastguard Worker }  // namespace container_internal
74*9356374aSAndroid Build Coastguard Worker 
75*9356374aSAndroid Build Coastguard Worker // absl::btree_set<>
76*9356374aSAndroid Build Coastguard Worker //
77*9356374aSAndroid Build Coastguard Worker // An `absl::btree_set<K>` is an ordered associative container of unique key
78*9356374aSAndroid Build Coastguard Worker // values designed to be a more efficient replacement for `std::set` (in most
79*9356374aSAndroid Build Coastguard Worker // cases).
80*9356374aSAndroid Build Coastguard Worker //
81*9356374aSAndroid Build Coastguard Worker // Keys are sorted using an (optional) comparison function, which defaults to
82*9356374aSAndroid Build Coastguard Worker // `std::less<K>`.
83*9356374aSAndroid Build Coastguard Worker //
84*9356374aSAndroid Build Coastguard Worker // An `absl::btree_set<K>` uses a default allocator of `std::allocator<K>` to
85*9356374aSAndroid Build Coastguard Worker // allocate (and deallocate) nodes, and construct and destruct values within
86*9356374aSAndroid Build Coastguard Worker // those nodes. You may instead specify a custom allocator `A` (which in turn
87*9356374aSAndroid Build Coastguard Worker // requires specifying a custom comparator `C`) as in
88*9356374aSAndroid Build Coastguard Worker // `absl::btree_set<K, C, A>`.
89*9356374aSAndroid Build Coastguard Worker //
90*9356374aSAndroid Build Coastguard Worker template <typename Key, typename Compare = std::less<Key>,
91*9356374aSAndroid Build Coastguard Worker           typename Alloc = std::allocator<Key>>
92*9356374aSAndroid Build Coastguard Worker class ABSL_INTERNAL_ATTRIBUTE_OWNER btree_set
93*9356374aSAndroid Build Coastguard Worker     : public container_internal::btree_set_container<
94*9356374aSAndroid Build Coastguard Worker           container_internal::btree<container_internal::set_params<
95*9356374aSAndroid Build Coastguard Worker               Key, Compare, Alloc, /*TargetNodeSize=*/256,
96*9356374aSAndroid Build Coastguard Worker               /*IsMulti=*/false>>> {
97*9356374aSAndroid Build Coastguard Worker   using Base = typename btree_set::btree_set_container;
98*9356374aSAndroid Build Coastguard Worker 
99*9356374aSAndroid Build Coastguard Worker  public:
100*9356374aSAndroid Build Coastguard Worker   // Constructors and Assignment Operators
101*9356374aSAndroid Build Coastguard Worker   //
102*9356374aSAndroid Build Coastguard Worker   // A `btree_set` supports the same overload set as `std::set`
103*9356374aSAndroid Build Coastguard Worker   // for construction and assignment:
104*9356374aSAndroid Build Coastguard Worker   //
105*9356374aSAndroid Build Coastguard Worker   // * Default constructor
106*9356374aSAndroid Build Coastguard Worker   //
107*9356374aSAndroid Build Coastguard Worker   //   absl::btree_set<std::string> set1;
108*9356374aSAndroid Build Coastguard Worker   //
109*9356374aSAndroid Build Coastguard Worker   // * Initializer List constructor
110*9356374aSAndroid Build Coastguard Worker   //
111*9356374aSAndroid Build Coastguard Worker   //   absl::btree_set<std::string> set2 =
112*9356374aSAndroid Build Coastguard Worker   //       {{"huey"}, {"dewey"}, {"louie"},};
113*9356374aSAndroid Build Coastguard Worker   //
114*9356374aSAndroid Build Coastguard Worker   // * Copy constructor
115*9356374aSAndroid Build Coastguard Worker   //
116*9356374aSAndroid Build Coastguard Worker   //   absl::btree_set<std::string> set3(set2);
117*9356374aSAndroid Build Coastguard Worker   //
118*9356374aSAndroid Build Coastguard Worker   // * Copy assignment operator
119*9356374aSAndroid Build Coastguard Worker   //
120*9356374aSAndroid Build Coastguard Worker   //  absl::btree_set<std::string> set4;
121*9356374aSAndroid Build Coastguard Worker   //  set4 = set3;
122*9356374aSAndroid Build Coastguard Worker   //
123*9356374aSAndroid Build Coastguard Worker   // * Move constructor
124*9356374aSAndroid Build Coastguard Worker   //
125*9356374aSAndroid Build Coastguard Worker   //   // Move is guaranteed efficient
126*9356374aSAndroid Build Coastguard Worker   //   absl::btree_set<std::string> set5(std::move(set4));
127*9356374aSAndroid Build Coastguard Worker   //
128*9356374aSAndroid Build Coastguard Worker   // * Move assignment operator
129*9356374aSAndroid Build Coastguard Worker   //
130*9356374aSAndroid Build Coastguard Worker   //   // May be efficient if allocators are compatible
131*9356374aSAndroid Build Coastguard Worker   //   absl::btree_set<std::string> set6;
132*9356374aSAndroid Build Coastguard Worker   //   set6 = std::move(set5);
133*9356374aSAndroid Build Coastguard Worker   //
134*9356374aSAndroid Build Coastguard Worker   // * Range constructor
135*9356374aSAndroid Build Coastguard Worker   //
136*9356374aSAndroid Build Coastguard Worker   //   std::vector<std::string> v = {"a", "b"};
137*9356374aSAndroid Build Coastguard Worker   //   absl::btree_set<std::string> set7(v.begin(), v.end());
btree_set()138*9356374aSAndroid Build Coastguard Worker   btree_set() {}
139*9356374aSAndroid Build Coastguard Worker   using Base::Base;
140*9356374aSAndroid Build Coastguard Worker 
141*9356374aSAndroid Build Coastguard Worker   // btree_set::begin()
142*9356374aSAndroid Build Coastguard Worker   //
143*9356374aSAndroid Build Coastguard Worker   // Returns an iterator to the beginning of the `btree_set`.
144*9356374aSAndroid Build Coastguard Worker   using Base::begin;
145*9356374aSAndroid Build Coastguard Worker 
146*9356374aSAndroid Build Coastguard Worker   // btree_set::cbegin()
147*9356374aSAndroid Build Coastguard Worker   //
148*9356374aSAndroid Build Coastguard Worker   // Returns a const iterator to the beginning of the `btree_set`.
149*9356374aSAndroid Build Coastguard Worker   using Base::cbegin;
150*9356374aSAndroid Build Coastguard Worker 
151*9356374aSAndroid Build Coastguard Worker   // btree_set::end()
152*9356374aSAndroid Build Coastguard Worker   //
153*9356374aSAndroid Build Coastguard Worker   // Returns an iterator to the end of the `btree_set`.
154*9356374aSAndroid Build Coastguard Worker   using Base::end;
155*9356374aSAndroid Build Coastguard Worker 
156*9356374aSAndroid Build Coastguard Worker   // btree_set::cend()
157*9356374aSAndroid Build Coastguard Worker   //
158*9356374aSAndroid Build Coastguard Worker   // Returns a const iterator to the end of the `btree_set`.
159*9356374aSAndroid Build Coastguard Worker   using Base::cend;
160*9356374aSAndroid Build Coastguard Worker 
161*9356374aSAndroid Build Coastguard Worker   // btree_set::empty()
162*9356374aSAndroid Build Coastguard Worker   //
163*9356374aSAndroid Build Coastguard Worker   // Returns whether or not the `btree_set` is empty.
164*9356374aSAndroid Build Coastguard Worker   using Base::empty;
165*9356374aSAndroid Build Coastguard Worker 
166*9356374aSAndroid Build Coastguard Worker   // btree_set::max_size()
167*9356374aSAndroid Build Coastguard Worker   //
168*9356374aSAndroid Build Coastguard Worker   // Returns the largest theoretical possible number of elements within a
169*9356374aSAndroid Build Coastguard Worker   // `btree_set` under current memory constraints. This value can be thought
170*9356374aSAndroid Build Coastguard Worker   // of as the largest value of `std::distance(begin(), end())` for a
171*9356374aSAndroid Build Coastguard Worker   // `btree_set<Key>`.
172*9356374aSAndroid Build Coastguard Worker   using Base::max_size;
173*9356374aSAndroid Build Coastguard Worker 
174*9356374aSAndroid Build Coastguard Worker   // btree_set::size()
175*9356374aSAndroid Build Coastguard Worker   //
176*9356374aSAndroid Build Coastguard Worker   // Returns the number of elements currently within the `btree_set`.
177*9356374aSAndroid Build Coastguard Worker   using Base::size;
178*9356374aSAndroid Build Coastguard Worker 
179*9356374aSAndroid Build Coastguard Worker   // btree_set::clear()
180*9356374aSAndroid Build Coastguard Worker   //
181*9356374aSAndroid Build Coastguard Worker   // Removes all elements from the `btree_set`. Invalidates any references,
182*9356374aSAndroid Build Coastguard Worker   // pointers, or iterators referring to contained elements.
183*9356374aSAndroid Build Coastguard Worker   using Base::clear;
184*9356374aSAndroid Build Coastguard Worker 
185*9356374aSAndroid Build Coastguard Worker   // btree_set::erase()
186*9356374aSAndroid Build Coastguard Worker   //
187*9356374aSAndroid Build Coastguard Worker   // Erases elements within the `btree_set`. 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_set`, 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_set::insert()
209*9356374aSAndroid Build Coastguard Worker   //
210*9356374aSAndroid Build Coastguard Worker   // Inserts an element of the specified value into the `btree_set`,
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_set`. 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_set`. 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_set::emplace()
247*9356374aSAndroid Build Coastguard Worker   //
248*9356374aSAndroid Build Coastguard Worker   // Inserts an element of the specified value by constructing it in-place
249*9356374aSAndroid Build Coastguard Worker   // within the `btree_set`, provided that no element with the given key
250*9356374aSAndroid Build Coastguard Worker   // already exists.
251*9356374aSAndroid Build Coastguard Worker   //
252*9356374aSAndroid Build Coastguard Worker   // The element may be constructed even if there already is an element with the
253*9356374aSAndroid Build Coastguard Worker   // key in the container, in which case the newly constructed element will be
254*9356374aSAndroid Build Coastguard Worker   // destroyed immediately.
255*9356374aSAndroid Build Coastguard Worker   //
256*9356374aSAndroid Build Coastguard Worker   // If an insertion occurs, any references, pointers, or iterators are
257*9356374aSAndroid Build Coastguard Worker   // invalidated.
258*9356374aSAndroid Build Coastguard Worker   using Base::emplace;
259*9356374aSAndroid Build Coastguard Worker 
260*9356374aSAndroid Build Coastguard Worker   // btree_set::emplace_hint()
261*9356374aSAndroid Build Coastguard Worker   //
262*9356374aSAndroid Build Coastguard Worker   // Inserts an element of the specified value by constructing it in-place
263*9356374aSAndroid Build Coastguard Worker   // within the `btree_set`, using the position of `hint` as a non-binding
264*9356374aSAndroid Build Coastguard Worker   // suggestion for where to begin the insertion search, and only inserts
265*9356374aSAndroid Build Coastguard Worker   // provided that no element with the given key already exists.
266*9356374aSAndroid Build Coastguard Worker   //
267*9356374aSAndroid Build Coastguard Worker   // The element may be constructed even if there already is an element with the
268*9356374aSAndroid Build Coastguard Worker   // key in the container, in which case the newly constructed element will be
269*9356374aSAndroid Build Coastguard Worker   // destroyed immediately.
270*9356374aSAndroid Build Coastguard Worker   //
271*9356374aSAndroid Build Coastguard Worker   // If an insertion occurs, any references, pointers, or iterators are
272*9356374aSAndroid Build Coastguard Worker   // invalidated.
273*9356374aSAndroid Build Coastguard Worker   using Base::emplace_hint;
274*9356374aSAndroid Build Coastguard Worker 
275*9356374aSAndroid Build Coastguard Worker   // btree_set::extract()
276*9356374aSAndroid Build Coastguard Worker   //
277*9356374aSAndroid Build Coastguard Worker   // Extracts the indicated element, erasing it in the process, and returns it
278*9356374aSAndroid Build Coastguard Worker   // as a C++17-compatible node handle. Any references, pointers, or iterators
279*9356374aSAndroid Build Coastguard Worker   // are invalidated. Overloads are listed below.
280*9356374aSAndroid Build Coastguard Worker   //
281*9356374aSAndroid Build Coastguard Worker   // node_type extract(const_iterator position):
282*9356374aSAndroid Build Coastguard Worker   //
283*9356374aSAndroid Build Coastguard Worker   //   Extracts the element at the indicated position and returns a node handle
284*9356374aSAndroid Build Coastguard Worker   //   owning that extracted data.
285*9356374aSAndroid Build Coastguard Worker   //
286*9356374aSAndroid Build Coastguard Worker   // template <typename K> node_type extract(const K& k):
287*9356374aSAndroid Build Coastguard Worker   //
288*9356374aSAndroid Build Coastguard Worker   //   Extracts the element with the key matching the passed key value and
289*9356374aSAndroid Build Coastguard Worker   //   returns a node handle owning that extracted data. If the `btree_set`
290*9356374aSAndroid Build Coastguard Worker   //   does not contain an element with a matching key, this function returns an
291*9356374aSAndroid Build Coastguard Worker   //   empty node handle.
292*9356374aSAndroid Build Coastguard Worker   //
293*9356374aSAndroid Build Coastguard Worker   // NOTE: In this context, `node_type` refers to the C++17 concept of a
294*9356374aSAndroid Build Coastguard Worker   // move-only type that owns and provides access to the elements in associative
295*9356374aSAndroid Build Coastguard Worker   // containers (https://en.cppreference.com/w/cpp/container/node_handle).
296*9356374aSAndroid Build Coastguard Worker   // It does NOT refer to the data layout of the underlying btree.
297*9356374aSAndroid Build Coastguard Worker   using Base::extract;
298*9356374aSAndroid Build Coastguard Worker 
299*9356374aSAndroid Build Coastguard Worker   // btree_set::extract_and_get_next()
300*9356374aSAndroid Build Coastguard Worker   //
301*9356374aSAndroid Build Coastguard Worker   // Extracts the indicated element, erasing it in the process, and returns it
302*9356374aSAndroid Build Coastguard Worker   // as a C++17-compatible node handle along with an iterator to the next
303*9356374aSAndroid Build Coastguard Worker   // element.
304*9356374aSAndroid Build Coastguard Worker   //
305*9356374aSAndroid Build Coastguard Worker   // extract_and_get_next_return_type extract_and_get_next(
306*9356374aSAndroid Build Coastguard Worker   //     const_iterator position):
307*9356374aSAndroid Build Coastguard Worker   //
308*9356374aSAndroid Build Coastguard Worker   //   Extracts the element at the indicated position, returns a struct
309*9356374aSAndroid Build Coastguard Worker   //   containing a member named `node`: a node handle owning that extracted
310*9356374aSAndroid Build Coastguard Worker   //   data and a member named `next`: an iterator pointing to the next element
311*9356374aSAndroid Build Coastguard Worker   //   in the btree.
312*9356374aSAndroid Build Coastguard Worker   using Base::extract_and_get_next;
313*9356374aSAndroid Build Coastguard Worker 
314*9356374aSAndroid Build Coastguard Worker   // btree_set::merge()
315*9356374aSAndroid Build Coastguard Worker   //
316*9356374aSAndroid Build Coastguard Worker   // Extracts elements from a given `source` btree_set into this
317*9356374aSAndroid Build Coastguard Worker   // `btree_set`. If the destination `btree_set` already contains an
318*9356374aSAndroid Build Coastguard Worker   // element with an equivalent key, that element is not extracted.
319*9356374aSAndroid Build Coastguard Worker   using Base::merge;
320*9356374aSAndroid Build Coastguard Worker 
321*9356374aSAndroid Build Coastguard Worker   // btree_set::swap(btree_set& other)
322*9356374aSAndroid Build Coastguard Worker   //
323*9356374aSAndroid Build Coastguard Worker   // Exchanges the contents of this `btree_set` with those of the `other`
324*9356374aSAndroid Build Coastguard Worker   // btree_set, avoiding invocation of any move, copy, or swap operations on
325*9356374aSAndroid Build Coastguard Worker   // individual elements.
326*9356374aSAndroid Build Coastguard Worker   //
327*9356374aSAndroid Build Coastguard Worker   // All iterators and references on the `btree_set` remain valid, excepting
328*9356374aSAndroid Build Coastguard Worker   // for the past-the-end iterator, which is invalidated.
329*9356374aSAndroid Build Coastguard Worker   using Base::swap;
330*9356374aSAndroid Build Coastguard Worker 
331*9356374aSAndroid Build Coastguard Worker   // btree_set::contains()
332*9356374aSAndroid Build Coastguard Worker   //
333*9356374aSAndroid Build Coastguard Worker   // template <typename K> bool contains(const K& key) const:
334*9356374aSAndroid Build Coastguard Worker   //
335*9356374aSAndroid Build Coastguard Worker   // Determines whether an element comparing equal to the given `key` exists
336*9356374aSAndroid Build Coastguard Worker   // within the `btree_set`, returning `true` if so or `false` otherwise.
337*9356374aSAndroid Build Coastguard Worker   //
338*9356374aSAndroid Build Coastguard Worker   // Supports heterogeneous lookup, provided that the set has a compatible
339*9356374aSAndroid Build Coastguard Worker   // heterogeneous comparator.
340*9356374aSAndroid Build Coastguard Worker   using Base::contains;
341*9356374aSAndroid Build Coastguard Worker 
342*9356374aSAndroid Build Coastguard Worker   // btree_set::count()
343*9356374aSAndroid Build Coastguard Worker   //
344*9356374aSAndroid Build Coastguard Worker   // template <typename K> size_type count(const K& key) const:
345*9356374aSAndroid Build Coastguard Worker   //
346*9356374aSAndroid Build Coastguard Worker   // Returns the number of elements comparing equal to the given `key` within
347*9356374aSAndroid Build Coastguard Worker   // the `btree_set`. Note that this function will return either `1` or `0`
348*9356374aSAndroid Build Coastguard Worker   // since duplicate elements are not allowed within a `btree_set`.
349*9356374aSAndroid Build Coastguard Worker   //
350*9356374aSAndroid Build Coastguard Worker   // Supports heterogeneous lookup, provided that the set has a compatible
351*9356374aSAndroid Build Coastguard Worker   // heterogeneous comparator.
352*9356374aSAndroid Build Coastguard Worker   using Base::count;
353*9356374aSAndroid Build Coastguard Worker 
354*9356374aSAndroid Build Coastguard Worker   // btree_set::equal_range()
355*9356374aSAndroid Build Coastguard Worker   //
356*9356374aSAndroid Build Coastguard Worker   // Returns a closed range [first, last], defined by a `std::pair` of two
357*9356374aSAndroid Build Coastguard Worker   // iterators, containing all elements with the passed key in the
358*9356374aSAndroid Build Coastguard Worker   // `btree_set`.
359*9356374aSAndroid Build Coastguard Worker   using Base::equal_range;
360*9356374aSAndroid Build Coastguard Worker 
361*9356374aSAndroid Build Coastguard Worker   // btree_set::find()
362*9356374aSAndroid Build Coastguard Worker   //
363*9356374aSAndroid Build Coastguard Worker   // template <typename K> iterator find(const K& key):
364*9356374aSAndroid Build Coastguard Worker   // template <typename K> const_iterator find(const K& key) const:
365*9356374aSAndroid Build Coastguard Worker   //
366*9356374aSAndroid Build Coastguard Worker   // Finds an element with the passed `key` within the `btree_set`.
367*9356374aSAndroid Build Coastguard Worker   //
368*9356374aSAndroid Build Coastguard Worker   // Supports heterogeneous lookup, provided that the set has a compatible
369*9356374aSAndroid Build Coastguard Worker   // heterogeneous comparator.
370*9356374aSAndroid Build Coastguard Worker   using Base::find;
371*9356374aSAndroid Build Coastguard Worker 
372*9356374aSAndroid Build Coastguard Worker   // btree_set::lower_bound()
373*9356374aSAndroid Build Coastguard Worker   //
374*9356374aSAndroid Build Coastguard Worker   // template <typename K> iterator lower_bound(const K& key):
375*9356374aSAndroid Build Coastguard Worker   // template <typename K> const_iterator lower_bound(const K& key) const:
376*9356374aSAndroid Build Coastguard Worker   //
377*9356374aSAndroid Build Coastguard Worker   // Finds the first element that is not less than `key` within the `btree_set`.
378*9356374aSAndroid Build Coastguard Worker   //
379*9356374aSAndroid Build Coastguard Worker   // Supports heterogeneous lookup, provided that the set has a compatible
380*9356374aSAndroid Build Coastguard Worker   // heterogeneous comparator.
381*9356374aSAndroid Build Coastguard Worker   using Base::lower_bound;
382*9356374aSAndroid Build Coastguard Worker 
383*9356374aSAndroid Build Coastguard Worker   // btree_set::upper_bound()
384*9356374aSAndroid Build Coastguard Worker   //
385*9356374aSAndroid Build Coastguard Worker   // template <typename K> iterator upper_bound(const K& key):
386*9356374aSAndroid Build Coastguard Worker   // template <typename K> const_iterator upper_bound(const K& key) const:
387*9356374aSAndroid Build Coastguard Worker   //
388*9356374aSAndroid Build Coastguard Worker   // Finds the first element that is greater than `key` within the `btree_set`.
389*9356374aSAndroid Build Coastguard Worker   //
390*9356374aSAndroid Build Coastguard Worker   // Supports heterogeneous lookup, provided that the set has a compatible
391*9356374aSAndroid Build Coastguard Worker   // heterogeneous comparator.
392*9356374aSAndroid Build Coastguard Worker   using Base::upper_bound;
393*9356374aSAndroid Build Coastguard Worker 
394*9356374aSAndroid Build Coastguard Worker   // btree_set::get_allocator()
395*9356374aSAndroid Build Coastguard Worker   //
396*9356374aSAndroid Build Coastguard Worker   // Returns the allocator function associated with this `btree_set`.
397*9356374aSAndroid Build Coastguard Worker   using Base::get_allocator;
398*9356374aSAndroid Build Coastguard Worker 
399*9356374aSAndroid Build Coastguard Worker   // btree_set::key_comp();
400*9356374aSAndroid Build Coastguard Worker   //
401*9356374aSAndroid Build Coastguard Worker   // Returns the key comparator associated with this `btree_set`.
402*9356374aSAndroid Build Coastguard Worker   using Base::key_comp;
403*9356374aSAndroid Build Coastguard Worker 
404*9356374aSAndroid Build Coastguard Worker   // btree_set::value_comp();
405*9356374aSAndroid Build Coastguard Worker   //
406*9356374aSAndroid Build Coastguard Worker   // Returns the value comparator associated with this `btree_set`. The keys to
407*9356374aSAndroid Build Coastguard Worker   // sort the elements are the values themselves, therefore `value_comp` and its
408*9356374aSAndroid Build Coastguard Worker   // sibling member function `key_comp` are equivalent.
409*9356374aSAndroid Build Coastguard Worker   using Base::value_comp;
410*9356374aSAndroid Build Coastguard Worker };
411*9356374aSAndroid Build Coastguard Worker 
412*9356374aSAndroid Build Coastguard Worker // absl::swap(absl::btree_set<>, absl::btree_set<>)
413*9356374aSAndroid Build Coastguard Worker //
414*9356374aSAndroid Build Coastguard Worker // Swaps the contents of two `absl::btree_set` containers.
415*9356374aSAndroid Build Coastguard Worker template <typename K, typename C, typename A>
swap(btree_set<K,C,A> & x,btree_set<K,C,A> & y)416*9356374aSAndroid Build Coastguard Worker void swap(btree_set<K, C, A> &x, btree_set<K, C, A> &y) {
417*9356374aSAndroid Build Coastguard Worker   return x.swap(y);
418*9356374aSAndroid Build Coastguard Worker }
419*9356374aSAndroid Build Coastguard Worker 
420*9356374aSAndroid Build Coastguard Worker // absl::erase_if(absl::btree_set<>, Pred)
421*9356374aSAndroid Build Coastguard Worker //
422*9356374aSAndroid Build Coastguard Worker // Erases all elements that satisfy the predicate pred from the container.
423*9356374aSAndroid Build Coastguard Worker // Returns the number of erased elements.
424*9356374aSAndroid Build Coastguard Worker template <typename K, typename C, typename A, typename Pred>
erase_if(btree_set<K,C,A> & set,Pred pred)425*9356374aSAndroid Build Coastguard Worker typename btree_set<K, C, A>::size_type erase_if(btree_set<K, C, A> &set,
426*9356374aSAndroid Build Coastguard Worker                                                 Pred pred) {
427*9356374aSAndroid Build Coastguard Worker   return container_internal::btree_access::erase_if(set, std::move(pred));
428*9356374aSAndroid Build Coastguard Worker }
429*9356374aSAndroid Build Coastguard Worker 
430*9356374aSAndroid Build Coastguard Worker // absl::btree_multiset<>
431*9356374aSAndroid Build Coastguard Worker //
432*9356374aSAndroid Build Coastguard Worker // An `absl::btree_multiset<K>` is an ordered associative container of
433*9356374aSAndroid Build Coastguard Worker // keys and associated values designed to be a more efficient replacement
434*9356374aSAndroid Build Coastguard Worker // for `std::multiset` (in most cases). Unlike `absl::btree_set`, a B-tree
435*9356374aSAndroid Build Coastguard Worker // multiset allows equivalent elements.
436*9356374aSAndroid Build Coastguard Worker //
437*9356374aSAndroid Build Coastguard Worker // Keys are sorted using an (optional) comparison function, which defaults to
438*9356374aSAndroid Build Coastguard Worker // `std::less<K>`.
439*9356374aSAndroid Build Coastguard Worker //
440*9356374aSAndroid Build Coastguard Worker // An `absl::btree_multiset<K>` uses a default allocator of `std::allocator<K>`
441*9356374aSAndroid Build Coastguard Worker // to allocate (and deallocate) nodes, and construct and destruct values within
442*9356374aSAndroid Build Coastguard Worker // those nodes. You may instead specify a custom allocator `A` (which in turn
443*9356374aSAndroid Build Coastguard Worker // requires specifying a custom comparator `C`) as in
444*9356374aSAndroid Build Coastguard Worker // `absl::btree_multiset<K, C, A>`.
445*9356374aSAndroid Build Coastguard Worker //
446*9356374aSAndroid Build Coastguard Worker template <typename Key, typename Compare = std::less<Key>,
447*9356374aSAndroid Build Coastguard Worker           typename Alloc = std::allocator<Key>>
448*9356374aSAndroid Build Coastguard Worker class ABSL_INTERNAL_ATTRIBUTE_OWNER btree_multiset
449*9356374aSAndroid Build Coastguard Worker     : public container_internal::btree_multiset_container<
450*9356374aSAndroid Build Coastguard Worker           container_internal::btree<container_internal::set_params<
451*9356374aSAndroid Build Coastguard Worker               Key, Compare, Alloc, /*TargetNodeSize=*/256,
452*9356374aSAndroid Build Coastguard Worker               /*IsMulti=*/true>>> {
453*9356374aSAndroid Build Coastguard Worker   using Base = typename btree_multiset::btree_multiset_container;
454*9356374aSAndroid Build Coastguard Worker 
455*9356374aSAndroid Build Coastguard Worker  public:
456*9356374aSAndroid Build Coastguard Worker   // Constructors and Assignment Operators
457*9356374aSAndroid Build Coastguard Worker   //
458*9356374aSAndroid Build Coastguard Worker   // A `btree_multiset` supports the same overload set as `std::set`
459*9356374aSAndroid Build Coastguard Worker   // for construction and assignment:
460*9356374aSAndroid Build Coastguard Worker   //
461*9356374aSAndroid Build Coastguard Worker   // * Default constructor
462*9356374aSAndroid Build Coastguard Worker   //
463*9356374aSAndroid Build Coastguard Worker   //   absl::btree_multiset<std::string> set1;
464*9356374aSAndroid Build Coastguard Worker   //
465*9356374aSAndroid Build Coastguard Worker   // * Initializer List constructor
466*9356374aSAndroid Build Coastguard Worker   //
467*9356374aSAndroid Build Coastguard Worker   //   absl::btree_multiset<std::string> set2 =
468*9356374aSAndroid Build Coastguard Worker   //       {{"huey"}, {"dewey"}, {"louie"},};
469*9356374aSAndroid Build Coastguard Worker   //
470*9356374aSAndroid Build Coastguard Worker   // * Copy constructor
471*9356374aSAndroid Build Coastguard Worker   //
472*9356374aSAndroid Build Coastguard Worker   //   absl::btree_multiset<std::string> set3(set2);
473*9356374aSAndroid Build Coastguard Worker   //
474*9356374aSAndroid Build Coastguard Worker   // * Copy assignment operator
475*9356374aSAndroid Build Coastguard Worker   //
476*9356374aSAndroid Build Coastguard Worker   //  absl::btree_multiset<std::string> set4;
477*9356374aSAndroid Build Coastguard Worker   //  set4 = set3;
478*9356374aSAndroid Build Coastguard Worker   //
479*9356374aSAndroid Build Coastguard Worker   // * Move constructor
480*9356374aSAndroid Build Coastguard Worker   //
481*9356374aSAndroid Build Coastguard Worker   //   // Move is guaranteed efficient
482*9356374aSAndroid Build Coastguard Worker   //   absl::btree_multiset<std::string> set5(std::move(set4));
483*9356374aSAndroid Build Coastguard Worker   //
484*9356374aSAndroid Build Coastguard Worker   // * Move assignment operator
485*9356374aSAndroid Build Coastguard Worker   //
486*9356374aSAndroid Build Coastguard Worker   //   // May be efficient if allocators are compatible
487*9356374aSAndroid Build Coastguard Worker   //   absl::btree_multiset<std::string> set6;
488*9356374aSAndroid Build Coastguard Worker   //   set6 = std::move(set5);
489*9356374aSAndroid Build Coastguard Worker   //
490*9356374aSAndroid Build Coastguard Worker   // * Range constructor
491*9356374aSAndroid Build Coastguard Worker   //
492*9356374aSAndroid Build Coastguard Worker   //   std::vector<std::string> v = {"a", "b"};
493*9356374aSAndroid Build Coastguard Worker   //   absl::btree_multiset<std::string> set7(v.begin(), v.end());
btree_multiset()494*9356374aSAndroid Build Coastguard Worker   btree_multiset() {}
495*9356374aSAndroid Build Coastguard Worker   using Base::Base;
496*9356374aSAndroid Build Coastguard Worker 
497*9356374aSAndroid Build Coastguard Worker   // btree_multiset::begin()
498*9356374aSAndroid Build Coastguard Worker   //
499*9356374aSAndroid Build Coastguard Worker   // Returns an iterator to the beginning of the `btree_multiset`.
500*9356374aSAndroid Build Coastguard Worker   using Base::begin;
501*9356374aSAndroid Build Coastguard Worker 
502*9356374aSAndroid Build Coastguard Worker   // btree_multiset::cbegin()
503*9356374aSAndroid Build Coastguard Worker   //
504*9356374aSAndroid Build Coastguard Worker   // Returns a const iterator to the beginning of the `btree_multiset`.
505*9356374aSAndroid Build Coastguard Worker   using Base::cbegin;
506*9356374aSAndroid Build Coastguard Worker 
507*9356374aSAndroid Build Coastguard Worker   // btree_multiset::end()
508*9356374aSAndroid Build Coastguard Worker   //
509*9356374aSAndroid Build Coastguard Worker   // Returns an iterator to the end of the `btree_multiset`.
510*9356374aSAndroid Build Coastguard Worker   using Base::end;
511*9356374aSAndroid Build Coastguard Worker 
512*9356374aSAndroid Build Coastguard Worker   // btree_multiset::cend()
513*9356374aSAndroid Build Coastguard Worker   //
514*9356374aSAndroid Build Coastguard Worker   // Returns a const iterator to the end of the `btree_multiset`.
515*9356374aSAndroid Build Coastguard Worker   using Base::cend;
516*9356374aSAndroid Build Coastguard Worker 
517*9356374aSAndroid Build Coastguard Worker   // btree_multiset::empty()
518*9356374aSAndroid Build Coastguard Worker   //
519*9356374aSAndroid Build Coastguard Worker   // Returns whether or not the `btree_multiset` is empty.
520*9356374aSAndroid Build Coastguard Worker   using Base::empty;
521*9356374aSAndroid Build Coastguard Worker 
522*9356374aSAndroid Build Coastguard Worker   // btree_multiset::max_size()
523*9356374aSAndroid Build Coastguard Worker   //
524*9356374aSAndroid Build Coastguard Worker   // Returns the largest theoretical possible number of elements within a
525*9356374aSAndroid Build Coastguard Worker   // `btree_multiset` under current memory constraints. This value can be
526*9356374aSAndroid Build Coastguard Worker   // thought of as the largest value of `std::distance(begin(), end())` for a
527*9356374aSAndroid Build Coastguard Worker   // `btree_multiset<Key>`.
528*9356374aSAndroid Build Coastguard Worker   using Base::max_size;
529*9356374aSAndroid Build Coastguard Worker 
530*9356374aSAndroid Build Coastguard Worker   // btree_multiset::size()
531*9356374aSAndroid Build Coastguard Worker   //
532*9356374aSAndroid Build Coastguard Worker   // Returns the number of elements currently within the `btree_multiset`.
533*9356374aSAndroid Build Coastguard Worker   using Base::size;
534*9356374aSAndroid Build Coastguard Worker 
535*9356374aSAndroid Build Coastguard Worker   // btree_multiset::clear()
536*9356374aSAndroid Build Coastguard Worker   //
537*9356374aSAndroid Build Coastguard Worker   // Removes all elements from the `btree_multiset`. Invalidates any references,
538*9356374aSAndroid Build Coastguard Worker   // pointers, or iterators referring to contained elements.
539*9356374aSAndroid Build Coastguard Worker   using Base::clear;
540*9356374aSAndroid Build Coastguard Worker 
541*9356374aSAndroid Build Coastguard Worker   // btree_multiset::erase()
542*9356374aSAndroid Build Coastguard Worker   //
543*9356374aSAndroid Build Coastguard Worker   // Erases elements within the `btree_multiset`. Overloads are listed below.
544*9356374aSAndroid Build Coastguard Worker   //
545*9356374aSAndroid Build Coastguard Worker   // iterator erase(iterator position):
546*9356374aSAndroid Build Coastguard Worker   // iterator erase(const_iterator position):
547*9356374aSAndroid Build Coastguard Worker   //
548*9356374aSAndroid Build Coastguard Worker   //   Erases the element at `position` of the `btree_multiset`, returning
549*9356374aSAndroid Build Coastguard Worker   //   the iterator pointing to the element after the one that was erased
550*9356374aSAndroid Build Coastguard Worker   //   (or end() if none exists).
551*9356374aSAndroid Build Coastguard Worker   //
552*9356374aSAndroid Build Coastguard Worker   // iterator erase(const_iterator first, const_iterator last):
553*9356374aSAndroid Build Coastguard Worker   //
554*9356374aSAndroid Build Coastguard Worker   //   Erases the elements in the open interval [`first`, `last`), returning
555*9356374aSAndroid Build Coastguard Worker   //   the iterator pointing to the element after the interval that was erased
556*9356374aSAndroid Build Coastguard Worker   //   (or end() if none exists).
557*9356374aSAndroid Build Coastguard Worker   //
558*9356374aSAndroid Build Coastguard Worker   // template <typename K> size_type erase(const K& key):
559*9356374aSAndroid Build Coastguard Worker   //
560*9356374aSAndroid Build Coastguard Worker   //   Erases the elements matching the key, if any exist, returning the
561*9356374aSAndroid Build Coastguard Worker   //   number of elements erased.
562*9356374aSAndroid Build Coastguard Worker   using Base::erase;
563*9356374aSAndroid Build Coastguard Worker 
564*9356374aSAndroid Build Coastguard Worker   // btree_multiset::insert()
565*9356374aSAndroid Build Coastguard Worker   //
566*9356374aSAndroid Build Coastguard Worker   // Inserts an element of the specified value into the `btree_multiset`,
567*9356374aSAndroid Build Coastguard Worker   // returning an iterator pointing to the newly inserted element.
568*9356374aSAndroid Build Coastguard Worker   // Any references, pointers, or iterators are invalidated.  Overloads are
569*9356374aSAndroid Build Coastguard Worker   // listed below.
570*9356374aSAndroid Build Coastguard Worker   //
571*9356374aSAndroid Build Coastguard Worker   // iterator insert(const value_type& value):
572*9356374aSAndroid Build Coastguard Worker   //
573*9356374aSAndroid Build Coastguard Worker   //   Inserts a value into the `btree_multiset`, returning an iterator to the
574*9356374aSAndroid Build Coastguard Worker   //   inserted element.
575*9356374aSAndroid Build Coastguard Worker   //
576*9356374aSAndroid Build Coastguard Worker   // iterator insert(value_type&& value):
577*9356374aSAndroid Build Coastguard Worker   //
578*9356374aSAndroid Build Coastguard Worker   //   Inserts a moveable value into the `btree_multiset`, returning an iterator
579*9356374aSAndroid Build Coastguard Worker   //   to the inserted element.
580*9356374aSAndroid Build Coastguard Worker   //
581*9356374aSAndroid Build Coastguard Worker   // iterator insert(const_iterator hint, const value_type& value):
582*9356374aSAndroid Build Coastguard Worker   // iterator insert(const_iterator hint, value_type&& value):
583*9356374aSAndroid Build Coastguard Worker   //
584*9356374aSAndroid Build Coastguard Worker   //   Inserts a value, using the position of `hint` as a non-binding suggestion
585*9356374aSAndroid Build Coastguard Worker   //   for where to begin the insertion search. Returns an iterator to the
586*9356374aSAndroid Build Coastguard Worker   //   inserted element.
587*9356374aSAndroid Build Coastguard Worker   //
588*9356374aSAndroid Build Coastguard Worker   // void insert(InputIterator first, InputIterator last):
589*9356374aSAndroid Build Coastguard Worker   //
590*9356374aSAndroid Build Coastguard Worker   //   Inserts a range of values [`first`, `last`).
591*9356374aSAndroid Build Coastguard Worker   //
592*9356374aSAndroid Build Coastguard Worker   // void insert(std::initializer_list<init_type> ilist):
593*9356374aSAndroid Build Coastguard Worker   //
594*9356374aSAndroid Build Coastguard Worker   //   Inserts the elements within the initializer list `ilist`.
595*9356374aSAndroid Build Coastguard Worker   using Base::insert;
596*9356374aSAndroid Build Coastguard Worker 
597*9356374aSAndroid Build Coastguard Worker   // btree_multiset::emplace()
598*9356374aSAndroid Build Coastguard Worker   //
599*9356374aSAndroid Build Coastguard Worker   // Inserts an element of the specified value by constructing it in-place
600*9356374aSAndroid Build Coastguard Worker   // within the `btree_multiset`. Any references, pointers, or iterators are
601*9356374aSAndroid Build Coastguard Worker   // invalidated.
602*9356374aSAndroid Build Coastguard Worker   using Base::emplace;
603*9356374aSAndroid Build Coastguard Worker 
604*9356374aSAndroid Build Coastguard Worker   // btree_multiset::emplace_hint()
605*9356374aSAndroid Build Coastguard Worker   //
606*9356374aSAndroid Build Coastguard Worker   // Inserts an element of the specified value by constructing it in-place
607*9356374aSAndroid Build Coastguard Worker   // within the `btree_multiset`, using the position of `hint` as a non-binding
608*9356374aSAndroid Build Coastguard Worker   // suggestion for where to begin the insertion search.
609*9356374aSAndroid Build Coastguard Worker   //
610*9356374aSAndroid Build Coastguard Worker   // Any references, pointers, or iterators are invalidated.
611*9356374aSAndroid Build Coastguard Worker   using Base::emplace_hint;
612*9356374aSAndroid Build Coastguard Worker 
613*9356374aSAndroid Build Coastguard Worker   // btree_multiset::extract()
614*9356374aSAndroid Build Coastguard Worker   //
615*9356374aSAndroid Build Coastguard Worker   // Extracts the indicated element, erasing it in the process, and returns it
616*9356374aSAndroid Build Coastguard Worker   // as a C++17-compatible node handle. Overloads are listed below.
617*9356374aSAndroid Build Coastguard Worker   //
618*9356374aSAndroid Build Coastguard Worker   // node_type extract(const_iterator position):
619*9356374aSAndroid Build Coastguard Worker   //
620*9356374aSAndroid Build Coastguard Worker   //   Extracts the element at the indicated position and returns a node handle
621*9356374aSAndroid Build Coastguard Worker   //   owning that extracted data.
622*9356374aSAndroid Build Coastguard Worker   //
623*9356374aSAndroid Build Coastguard Worker   // template <typename K> node_type extract(const K& k):
624*9356374aSAndroid Build Coastguard Worker   //
625*9356374aSAndroid Build Coastguard Worker   //   Extracts the element with the key matching the passed key value and
626*9356374aSAndroid Build Coastguard Worker   //   returns a node handle owning that extracted data. If the `btree_multiset`
627*9356374aSAndroid Build Coastguard Worker   //   does not contain an element with a matching key, this function returns an
628*9356374aSAndroid Build Coastguard Worker   //   empty node handle.
629*9356374aSAndroid Build Coastguard Worker   //
630*9356374aSAndroid Build Coastguard Worker   // NOTE: In this context, `node_type` refers to the C++17 concept of a
631*9356374aSAndroid Build Coastguard Worker   // move-only type that owns and provides access to the elements in associative
632*9356374aSAndroid Build Coastguard Worker   // containers (https://en.cppreference.com/w/cpp/container/node_handle).
633*9356374aSAndroid Build Coastguard Worker   // It does NOT refer to the data layout of the underlying btree.
634*9356374aSAndroid Build Coastguard Worker   using Base::extract;
635*9356374aSAndroid Build Coastguard Worker 
636*9356374aSAndroid Build Coastguard Worker   // btree_multiset::extract_and_get_next()
637*9356374aSAndroid Build Coastguard Worker   //
638*9356374aSAndroid Build Coastguard Worker   // Extracts the indicated element, erasing it in the process, and returns it
639*9356374aSAndroid Build Coastguard Worker   // as a C++17-compatible node handle along with an iterator to the next
640*9356374aSAndroid Build Coastguard Worker   // element.
641*9356374aSAndroid Build Coastguard Worker   //
642*9356374aSAndroid Build Coastguard Worker   // extract_and_get_next_return_type extract_and_get_next(
643*9356374aSAndroid Build Coastguard Worker   //     const_iterator position):
644*9356374aSAndroid Build Coastguard Worker   //
645*9356374aSAndroid Build Coastguard Worker   //   Extracts the element at the indicated position, returns a struct
646*9356374aSAndroid Build Coastguard Worker   //   containing a member named `node`: a node handle owning that extracted
647*9356374aSAndroid Build Coastguard Worker   //   data and a member named `next`: an iterator pointing to the next element
648*9356374aSAndroid Build Coastguard Worker   //   in the btree.
649*9356374aSAndroid Build Coastguard Worker   using Base::extract_and_get_next;
650*9356374aSAndroid Build Coastguard Worker 
651*9356374aSAndroid Build Coastguard Worker   // btree_multiset::merge()
652*9356374aSAndroid Build Coastguard Worker   //
653*9356374aSAndroid Build Coastguard Worker   // Extracts all elements from a given `source` btree_multiset into this
654*9356374aSAndroid Build Coastguard Worker   // `btree_multiset`.
655*9356374aSAndroid Build Coastguard Worker   using Base::merge;
656*9356374aSAndroid Build Coastguard Worker 
657*9356374aSAndroid Build Coastguard Worker   // btree_multiset::swap(btree_multiset& other)
658*9356374aSAndroid Build Coastguard Worker   //
659*9356374aSAndroid Build Coastguard Worker   // Exchanges the contents of this `btree_multiset` with those of the `other`
660*9356374aSAndroid Build Coastguard Worker   // btree_multiset, avoiding invocation of any move, copy, or swap operations
661*9356374aSAndroid Build Coastguard Worker   // on individual elements.
662*9356374aSAndroid Build Coastguard Worker   //
663*9356374aSAndroid Build Coastguard Worker   // All iterators and references on the `btree_multiset` remain valid,
664*9356374aSAndroid Build Coastguard Worker   // excepting for the past-the-end iterator, which is invalidated.
665*9356374aSAndroid Build Coastguard Worker   using Base::swap;
666*9356374aSAndroid Build Coastguard Worker 
667*9356374aSAndroid Build Coastguard Worker   // btree_multiset::contains()
668*9356374aSAndroid Build Coastguard Worker   //
669*9356374aSAndroid Build Coastguard Worker   // template <typename K> bool contains(const K& key) const:
670*9356374aSAndroid Build Coastguard Worker   //
671*9356374aSAndroid Build Coastguard Worker   // Determines whether an element comparing equal to the given `key` exists
672*9356374aSAndroid Build Coastguard Worker   // within the `btree_multiset`, returning `true` if so or `false` otherwise.
673*9356374aSAndroid Build Coastguard Worker   //
674*9356374aSAndroid Build Coastguard Worker   // Supports heterogeneous lookup, provided that the set has a compatible
675*9356374aSAndroid Build Coastguard Worker   // heterogeneous comparator.
676*9356374aSAndroid Build Coastguard Worker   using Base::contains;
677*9356374aSAndroid Build Coastguard Worker 
678*9356374aSAndroid Build Coastguard Worker   // btree_multiset::count()
679*9356374aSAndroid Build Coastguard Worker   //
680*9356374aSAndroid Build Coastguard Worker   // template <typename K> size_type count(const K& key) const:
681*9356374aSAndroid Build Coastguard Worker   //
682*9356374aSAndroid Build Coastguard Worker   // Returns the number of elements comparing equal to the given `key` within
683*9356374aSAndroid Build Coastguard Worker   // the `btree_multiset`.
684*9356374aSAndroid Build Coastguard Worker   //
685*9356374aSAndroid Build Coastguard Worker   // Supports heterogeneous lookup, provided that the set has a compatible
686*9356374aSAndroid Build Coastguard Worker   // heterogeneous comparator.
687*9356374aSAndroid Build Coastguard Worker   using Base::count;
688*9356374aSAndroid Build Coastguard Worker 
689*9356374aSAndroid Build Coastguard Worker   // btree_multiset::equal_range()
690*9356374aSAndroid Build Coastguard Worker   //
691*9356374aSAndroid Build Coastguard Worker   // Returns a closed range [first, last], defined by a `std::pair` of two
692*9356374aSAndroid Build Coastguard Worker   // iterators, containing all elements with the passed key in the
693*9356374aSAndroid Build Coastguard Worker   // `btree_multiset`.
694*9356374aSAndroid Build Coastguard Worker   using Base::equal_range;
695*9356374aSAndroid Build Coastguard Worker 
696*9356374aSAndroid Build Coastguard Worker   // btree_multiset::find()
697*9356374aSAndroid Build Coastguard Worker   //
698*9356374aSAndroid Build Coastguard Worker   // template <typename K> iterator find(const K& key):
699*9356374aSAndroid Build Coastguard Worker   // template <typename K> const_iterator find(const K& key) const:
700*9356374aSAndroid Build Coastguard Worker   //
701*9356374aSAndroid Build Coastguard Worker   // Finds an element with the passed `key` within the `btree_multiset`.
702*9356374aSAndroid Build Coastguard Worker   //
703*9356374aSAndroid Build Coastguard Worker   // Supports heterogeneous lookup, provided that the set has a compatible
704*9356374aSAndroid Build Coastguard Worker   // heterogeneous comparator.
705*9356374aSAndroid Build Coastguard Worker   using Base::find;
706*9356374aSAndroid Build Coastguard Worker 
707*9356374aSAndroid Build Coastguard Worker   // btree_multiset::lower_bound()
708*9356374aSAndroid Build Coastguard Worker   //
709*9356374aSAndroid Build Coastguard Worker   // template <typename K> iterator lower_bound(const K& key):
710*9356374aSAndroid Build Coastguard Worker   // template <typename K> const_iterator lower_bound(const K& key) const:
711*9356374aSAndroid Build Coastguard Worker   //
712*9356374aSAndroid Build Coastguard Worker   // Finds the first element that is not less than `key` within the
713*9356374aSAndroid Build Coastguard Worker   // `btree_multiset`.
714*9356374aSAndroid Build Coastguard Worker   //
715*9356374aSAndroid Build Coastguard Worker   // Supports heterogeneous lookup, provided that the set has a compatible
716*9356374aSAndroid Build Coastguard Worker   // heterogeneous comparator.
717*9356374aSAndroid Build Coastguard Worker   using Base::lower_bound;
718*9356374aSAndroid Build Coastguard Worker 
719*9356374aSAndroid Build Coastguard Worker   // btree_multiset::upper_bound()
720*9356374aSAndroid Build Coastguard Worker   //
721*9356374aSAndroid Build Coastguard Worker   // template <typename K> iterator upper_bound(const K& key):
722*9356374aSAndroid Build Coastguard Worker   // template <typename K> const_iterator upper_bound(const K& key) const:
723*9356374aSAndroid Build Coastguard Worker   //
724*9356374aSAndroid Build Coastguard Worker   // Finds the first element that is greater than `key` within the
725*9356374aSAndroid Build Coastguard Worker   // `btree_multiset`.
726*9356374aSAndroid Build Coastguard Worker   //
727*9356374aSAndroid Build Coastguard Worker   // Supports heterogeneous lookup, provided that the set has a compatible
728*9356374aSAndroid Build Coastguard Worker   // heterogeneous comparator.
729*9356374aSAndroid Build Coastguard Worker   using Base::upper_bound;
730*9356374aSAndroid Build Coastguard Worker 
731*9356374aSAndroid Build Coastguard Worker   // btree_multiset::get_allocator()
732*9356374aSAndroid Build Coastguard Worker   //
733*9356374aSAndroid Build Coastguard Worker   // Returns the allocator function associated with this `btree_multiset`.
734*9356374aSAndroid Build Coastguard Worker   using Base::get_allocator;
735*9356374aSAndroid Build Coastguard Worker 
736*9356374aSAndroid Build Coastguard Worker   // btree_multiset::key_comp();
737*9356374aSAndroid Build Coastguard Worker   //
738*9356374aSAndroid Build Coastguard Worker   // Returns the key comparator associated with this `btree_multiset`.
739*9356374aSAndroid Build Coastguard Worker   using Base::key_comp;
740*9356374aSAndroid Build Coastguard Worker 
741*9356374aSAndroid Build Coastguard Worker   // btree_multiset::value_comp();
742*9356374aSAndroid Build Coastguard Worker   //
743*9356374aSAndroid Build Coastguard Worker   // Returns the value comparator associated with this `btree_multiset`. The
744*9356374aSAndroid Build Coastguard Worker   // keys to sort the elements are the values themselves, therefore `value_comp`
745*9356374aSAndroid Build Coastguard Worker   // and its sibling member function `key_comp` are equivalent.
746*9356374aSAndroid Build Coastguard Worker   using Base::value_comp;
747*9356374aSAndroid Build Coastguard Worker };
748*9356374aSAndroid Build Coastguard Worker 
749*9356374aSAndroid Build Coastguard Worker // absl::swap(absl::btree_multiset<>, absl::btree_multiset<>)
750*9356374aSAndroid Build Coastguard Worker //
751*9356374aSAndroid Build Coastguard Worker // Swaps the contents of two `absl::btree_multiset` containers.
752*9356374aSAndroid Build Coastguard Worker template <typename K, typename C, typename A>
swap(btree_multiset<K,C,A> & x,btree_multiset<K,C,A> & y)753*9356374aSAndroid Build Coastguard Worker void swap(btree_multiset<K, C, A> &x, btree_multiset<K, C, A> &y) {
754*9356374aSAndroid Build Coastguard Worker   return x.swap(y);
755*9356374aSAndroid Build Coastguard Worker }
756*9356374aSAndroid Build Coastguard Worker 
757*9356374aSAndroid Build Coastguard Worker // absl::erase_if(absl::btree_multiset<>, Pred)
758*9356374aSAndroid Build Coastguard Worker //
759*9356374aSAndroid Build Coastguard Worker // Erases all elements that satisfy the predicate pred from the container.
760*9356374aSAndroid Build Coastguard Worker // Returns the number of erased elements.
761*9356374aSAndroid Build Coastguard Worker template <typename K, typename C, typename A, typename Pred>
erase_if(btree_multiset<K,C,A> & set,Pred pred)762*9356374aSAndroid Build Coastguard Worker typename btree_multiset<K, C, A>::size_type erase_if(
763*9356374aSAndroid Build Coastguard Worker    btree_multiset<K, C, A> & set, Pred pred) {
764*9356374aSAndroid Build Coastguard Worker   return container_internal::btree_access::erase_if(set, std::move(pred));
765*9356374aSAndroid Build Coastguard Worker }
766*9356374aSAndroid Build Coastguard Worker 
767*9356374aSAndroid Build Coastguard Worker namespace container_internal {
768*9356374aSAndroid Build Coastguard Worker 
769*9356374aSAndroid Build Coastguard Worker // This type implements the necessary functions from the
770*9356374aSAndroid Build Coastguard Worker // absl::container_internal::slot_type interface for btree_(multi)set.
771*9356374aSAndroid Build Coastguard Worker template <typename Key>
772*9356374aSAndroid Build Coastguard Worker struct set_slot_policy {
773*9356374aSAndroid Build Coastguard Worker   using slot_type = Key;
774*9356374aSAndroid Build Coastguard Worker   using value_type = Key;
775*9356374aSAndroid Build Coastguard Worker   using mutable_value_type = Key;
776*9356374aSAndroid Build Coastguard Worker 
elementset_slot_policy777*9356374aSAndroid Build Coastguard Worker   static value_type &element(slot_type *slot) { return *slot; }
elementset_slot_policy778*9356374aSAndroid Build Coastguard Worker   static const value_type &element(const slot_type *slot) { return *slot; }
779*9356374aSAndroid Build Coastguard Worker 
780*9356374aSAndroid Build Coastguard Worker   template <typename Alloc, class... Args>
constructset_slot_policy781*9356374aSAndroid Build Coastguard Worker   static void construct(Alloc *alloc, slot_type *slot, Args &&...args) {
782*9356374aSAndroid Build Coastguard Worker     absl::allocator_traits<Alloc>::construct(*alloc, slot,
783*9356374aSAndroid Build Coastguard Worker                                              std::forward<Args>(args)...);
784*9356374aSAndroid Build Coastguard Worker   }
785*9356374aSAndroid Build Coastguard Worker 
786*9356374aSAndroid Build Coastguard Worker   template <typename Alloc>
constructset_slot_policy787*9356374aSAndroid Build Coastguard Worker   static void construct(Alloc *alloc, slot_type *slot, slot_type *other) {
788*9356374aSAndroid Build Coastguard Worker     absl::allocator_traits<Alloc>::construct(*alloc, slot, std::move(*other));
789*9356374aSAndroid Build Coastguard Worker   }
790*9356374aSAndroid Build Coastguard Worker 
791*9356374aSAndroid Build Coastguard Worker   template <typename Alloc>
constructset_slot_policy792*9356374aSAndroid Build Coastguard Worker   static void construct(Alloc *alloc, slot_type *slot, const slot_type *other) {
793*9356374aSAndroid Build Coastguard Worker     absl::allocator_traits<Alloc>::construct(*alloc, slot, *other);
794*9356374aSAndroid Build Coastguard Worker   }
795*9356374aSAndroid Build Coastguard Worker 
796*9356374aSAndroid Build Coastguard Worker   template <typename Alloc>
destroyset_slot_policy797*9356374aSAndroid Build Coastguard Worker   static void destroy(Alloc *alloc, slot_type *slot) {
798*9356374aSAndroid Build Coastguard Worker     absl::allocator_traits<Alloc>::destroy(*alloc, slot);
799*9356374aSAndroid Build Coastguard Worker   }
800*9356374aSAndroid Build Coastguard Worker };
801*9356374aSAndroid Build Coastguard Worker 
802*9356374aSAndroid Build Coastguard Worker // A parameters structure for holding the type parameters for a btree_set.
803*9356374aSAndroid Build Coastguard Worker // Compare and Alloc should be nothrow copy-constructible.
804*9356374aSAndroid Build Coastguard Worker template <typename Key, typename Compare, typename Alloc, int TargetNodeSize,
805*9356374aSAndroid Build Coastguard Worker           bool IsMulti>
806*9356374aSAndroid Build Coastguard Worker struct set_params : common_params<Key, Compare, Alloc, TargetNodeSize, IsMulti,
807*9356374aSAndroid Build Coastguard Worker                                   /*IsMap=*/false, set_slot_policy<Key>> {
808*9356374aSAndroid Build Coastguard Worker   using value_type = Key;
809*9356374aSAndroid Build Coastguard Worker   using slot_type = typename set_params::common_params::slot_type;
810*9356374aSAndroid Build Coastguard Worker 
811*9356374aSAndroid Build Coastguard Worker   template <typename V>
keyset_params812*9356374aSAndroid Build Coastguard Worker   static const V &key(const V &value) {
813*9356374aSAndroid Build Coastguard Worker     return value;
814*9356374aSAndroid Build Coastguard Worker   }
keyset_params815*9356374aSAndroid Build Coastguard Worker   static const Key &key(const slot_type *slot) { return *slot; }
keyset_params816*9356374aSAndroid Build Coastguard Worker   static const Key &key(slot_type *slot) { return *slot; }
817*9356374aSAndroid Build Coastguard Worker };
818*9356374aSAndroid Build Coastguard Worker 
819*9356374aSAndroid Build Coastguard Worker }  // namespace container_internal
820*9356374aSAndroid Build Coastguard Worker 
821*9356374aSAndroid Build Coastguard Worker ABSL_NAMESPACE_END
822*9356374aSAndroid Build Coastguard Worker }  // namespace absl
823*9356374aSAndroid Build Coastguard Worker 
824*9356374aSAndroid Build Coastguard Worker #endif  // ABSL_CONTAINER_BTREE_SET_H_
825