xref: /aosp_15_r20/external/abseil-cpp/absl/container/internal/btree_container.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 #ifndef ABSL_CONTAINER_INTERNAL_BTREE_CONTAINER_H_
16*9356374aSAndroid Build Coastguard Worker #define ABSL_CONTAINER_INTERNAL_BTREE_CONTAINER_H_
17*9356374aSAndroid Build Coastguard Worker 
18*9356374aSAndroid Build Coastguard Worker #include <algorithm>
19*9356374aSAndroid Build Coastguard Worker #include <initializer_list>
20*9356374aSAndroid Build Coastguard Worker #include <iterator>
21*9356374aSAndroid Build Coastguard Worker #include <utility>
22*9356374aSAndroid Build Coastguard Worker 
23*9356374aSAndroid Build Coastguard Worker #include "absl/base/attributes.h"
24*9356374aSAndroid Build Coastguard Worker #include "absl/base/internal/throw_delegate.h"
25*9356374aSAndroid Build Coastguard Worker #include "absl/container/internal/btree.h"  // IWYU pragma: export
26*9356374aSAndroid Build Coastguard Worker #include "absl/container/internal/common.h"
27*9356374aSAndroid Build Coastguard Worker #include "absl/memory/memory.h"
28*9356374aSAndroid Build Coastguard Worker #include "absl/meta/type_traits.h"
29*9356374aSAndroid Build Coastguard Worker 
30*9356374aSAndroid Build Coastguard Worker namespace absl {
31*9356374aSAndroid Build Coastguard Worker ABSL_NAMESPACE_BEGIN
32*9356374aSAndroid Build Coastguard Worker namespace container_internal {
33*9356374aSAndroid Build Coastguard Worker 
34*9356374aSAndroid Build Coastguard Worker // A common base class for btree_set, btree_map, btree_multiset, and
35*9356374aSAndroid Build Coastguard Worker // btree_multimap.
36*9356374aSAndroid Build Coastguard Worker template <typename Tree>
37*9356374aSAndroid Build Coastguard Worker class btree_container {
38*9356374aSAndroid Build Coastguard Worker   using params_type = typename Tree::params_type;
39*9356374aSAndroid Build Coastguard Worker 
40*9356374aSAndroid Build Coastguard Worker  protected:
41*9356374aSAndroid Build Coastguard Worker   // Alias used for heterogeneous lookup functions.
42*9356374aSAndroid Build Coastguard Worker   // `key_arg<K>` evaluates to `K` when the functors are transparent and to
43*9356374aSAndroid Build Coastguard Worker   // `key_type` otherwise. It permits template argument deduction on `K` for the
44*9356374aSAndroid Build Coastguard Worker   // transparent case.
45*9356374aSAndroid Build Coastguard Worker   template <class K>
46*9356374aSAndroid Build Coastguard Worker   using key_arg =
47*9356374aSAndroid Build Coastguard Worker       typename KeyArg<params_type::kIsKeyCompareTransparent>::template type<
48*9356374aSAndroid Build Coastguard Worker           K, typename Tree::key_type>;
49*9356374aSAndroid Build Coastguard Worker 
50*9356374aSAndroid Build Coastguard Worker  public:
51*9356374aSAndroid Build Coastguard Worker   using key_type = typename Tree::key_type;
52*9356374aSAndroid Build Coastguard Worker   using value_type = typename Tree::value_type;
53*9356374aSAndroid Build Coastguard Worker   using size_type = typename Tree::size_type;
54*9356374aSAndroid Build Coastguard Worker   using difference_type = typename Tree::difference_type;
55*9356374aSAndroid Build Coastguard Worker   using key_compare = typename Tree::original_key_compare;
56*9356374aSAndroid Build Coastguard Worker   using value_compare = typename Tree::value_compare;
57*9356374aSAndroid Build Coastguard Worker   using allocator_type = typename Tree::allocator_type;
58*9356374aSAndroid Build Coastguard Worker   using reference = typename Tree::reference;
59*9356374aSAndroid Build Coastguard Worker   using const_reference = typename Tree::const_reference;
60*9356374aSAndroid Build Coastguard Worker   using pointer = typename Tree::pointer;
61*9356374aSAndroid Build Coastguard Worker   using const_pointer = typename Tree::const_pointer;
62*9356374aSAndroid Build Coastguard Worker   using iterator = typename Tree::iterator;
63*9356374aSAndroid Build Coastguard Worker   using const_iterator = typename Tree::const_iterator;
64*9356374aSAndroid Build Coastguard Worker   using reverse_iterator = typename Tree::reverse_iterator;
65*9356374aSAndroid Build Coastguard Worker   using const_reverse_iterator = typename Tree::const_reverse_iterator;
66*9356374aSAndroid Build Coastguard Worker   using node_type = typename Tree::node_handle_type;
67*9356374aSAndroid Build Coastguard Worker 
68*9356374aSAndroid Build Coastguard Worker   struct extract_and_get_next_return_type {
69*9356374aSAndroid Build Coastguard Worker     node_type node;
70*9356374aSAndroid Build Coastguard Worker     iterator next;
71*9356374aSAndroid Build Coastguard Worker   };
72*9356374aSAndroid Build Coastguard Worker 
73*9356374aSAndroid Build Coastguard Worker   // Constructors/assignments.
btree_container()74*9356374aSAndroid Build Coastguard Worker   btree_container() : tree_(key_compare(), allocator_type()) {}
75*9356374aSAndroid Build Coastguard Worker   explicit btree_container(const key_compare &comp,
76*9356374aSAndroid Build Coastguard Worker                            const allocator_type &alloc = allocator_type())
tree_(comp,alloc)77*9356374aSAndroid Build Coastguard Worker       : tree_(comp, alloc) {}
btree_container(const allocator_type & alloc)78*9356374aSAndroid Build Coastguard Worker   explicit btree_container(const allocator_type &alloc)
79*9356374aSAndroid Build Coastguard Worker       : tree_(key_compare(), alloc) {}
80*9356374aSAndroid Build Coastguard Worker 
btree_container(const btree_container & other)81*9356374aSAndroid Build Coastguard Worker   btree_container(const btree_container &other)
82*9356374aSAndroid Build Coastguard Worker       : btree_container(other, absl::allocator_traits<allocator_type>::
83*9356374aSAndroid Build Coastguard Worker                                    select_on_container_copy_construction(
84*9356374aSAndroid Build Coastguard Worker                                        other.get_allocator())) {}
btree_container(const btree_container & other,const allocator_type & alloc)85*9356374aSAndroid Build Coastguard Worker   btree_container(const btree_container &other, const allocator_type &alloc)
86*9356374aSAndroid Build Coastguard Worker       : tree_(other.tree_, alloc) {}
87*9356374aSAndroid Build Coastguard Worker 
88*9356374aSAndroid Build Coastguard Worker   btree_container(btree_container &&other) noexcept(
89*9356374aSAndroid Build Coastguard Worker       std::is_nothrow_move_constructible<Tree>::value) = default;
btree_container(btree_container && other,const allocator_type & alloc)90*9356374aSAndroid Build Coastguard Worker   btree_container(btree_container &&other, const allocator_type &alloc)
91*9356374aSAndroid Build Coastguard Worker       : tree_(std::move(other.tree_), alloc) {}
92*9356374aSAndroid Build Coastguard Worker 
93*9356374aSAndroid Build Coastguard Worker   btree_container &operator=(const btree_container &other) = default;
94*9356374aSAndroid Build Coastguard Worker   btree_container &operator=(btree_container &&other) noexcept(
95*9356374aSAndroid Build Coastguard Worker       std::is_nothrow_move_assignable<Tree>::value) = default;
96*9356374aSAndroid Build Coastguard Worker 
97*9356374aSAndroid Build Coastguard Worker   // Iterator routines.
begin()98*9356374aSAndroid Build Coastguard Worker   iterator begin() ABSL_ATTRIBUTE_LIFETIME_BOUND { return tree_.begin(); }
begin()99*9356374aSAndroid Build Coastguard Worker   const_iterator begin() const ABSL_ATTRIBUTE_LIFETIME_BOUND {
100*9356374aSAndroid Build Coastguard Worker     return tree_.begin();
101*9356374aSAndroid Build Coastguard Worker   }
cbegin()102*9356374aSAndroid Build Coastguard Worker   const_iterator cbegin() const ABSL_ATTRIBUTE_LIFETIME_BOUND {
103*9356374aSAndroid Build Coastguard Worker     return tree_.begin();
104*9356374aSAndroid Build Coastguard Worker   }
end()105*9356374aSAndroid Build Coastguard Worker   iterator end() ABSL_ATTRIBUTE_LIFETIME_BOUND { return tree_.end(); }
end()106*9356374aSAndroid Build Coastguard Worker   const_iterator end() const ABSL_ATTRIBUTE_LIFETIME_BOUND {
107*9356374aSAndroid Build Coastguard Worker     return tree_.end();
108*9356374aSAndroid Build Coastguard Worker   }
cend()109*9356374aSAndroid Build Coastguard Worker   const_iterator cend() const ABSL_ATTRIBUTE_LIFETIME_BOUND {
110*9356374aSAndroid Build Coastguard Worker     return tree_.end();
111*9356374aSAndroid Build Coastguard Worker   }
rbegin()112*9356374aSAndroid Build Coastguard Worker   reverse_iterator rbegin() ABSL_ATTRIBUTE_LIFETIME_BOUND {
113*9356374aSAndroid Build Coastguard Worker     return tree_.rbegin();
114*9356374aSAndroid Build Coastguard Worker   }
rbegin()115*9356374aSAndroid Build Coastguard Worker   const_reverse_iterator rbegin() const ABSL_ATTRIBUTE_LIFETIME_BOUND {
116*9356374aSAndroid Build Coastguard Worker     return tree_.rbegin();
117*9356374aSAndroid Build Coastguard Worker   }
crbegin()118*9356374aSAndroid Build Coastguard Worker   const_reverse_iterator crbegin() const ABSL_ATTRIBUTE_LIFETIME_BOUND {
119*9356374aSAndroid Build Coastguard Worker     return tree_.rbegin();
120*9356374aSAndroid Build Coastguard Worker   }
rend()121*9356374aSAndroid Build Coastguard Worker   reverse_iterator rend() ABSL_ATTRIBUTE_LIFETIME_BOUND { return tree_.rend(); }
rend()122*9356374aSAndroid Build Coastguard Worker   const_reverse_iterator rend() const ABSL_ATTRIBUTE_LIFETIME_BOUND {
123*9356374aSAndroid Build Coastguard Worker     return tree_.rend();
124*9356374aSAndroid Build Coastguard Worker   }
crend()125*9356374aSAndroid Build Coastguard Worker   const_reverse_iterator crend() const ABSL_ATTRIBUTE_LIFETIME_BOUND {
126*9356374aSAndroid Build Coastguard Worker     return tree_.rend();
127*9356374aSAndroid Build Coastguard Worker   }
128*9356374aSAndroid Build Coastguard Worker 
129*9356374aSAndroid Build Coastguard Worker   // Lookup routines.
130*9356374aSAndroid Build Coastguard Worker   template <typename K = key_type>
count(const key_arg<K> & key)131*9356374aSAndroid Build Coastguard Worker   size_type count(const key_arg<K> &key) const {
132*9356374aSAndroid Build Coastguard Worker     auto equal_range = this->equal_range(key);
133*9356374aSAndroid Build Coastguard Worker     return equal_range.second - equal_range.first;
134*9356374aSAndroid Build Coastguard Worker   }
135*9356374aSAndroid Build Coastguard Worker   template <typename K = key_type>
find(const key_arg<K> & key)136*9356374aSAndroid Build Coastguard Worker   iterator find(const key_arg<K> &key) ABSL_ATTRIBUTE_LIFETIME_BOUND {
137*9356374aSAndroid Build Coastguard Worker     return tree_.find(key);
138*9356374aSAndroid Build Coastguard Worker   }
139*9356374aSAndroid Build Coastguard Worker   template <typename K = key_type>
find(const key_arg<K> & key)140*9356374aSAndroid Build Coastguard Worker   const_iterator find(const key_arg<K> &key) const
141*9356374aSAndroid Build Coastguard Worker       ABSL_ATTRIBUTE_LIFETIME_BOUND {
142*9356374aSAndroid Build Coastguard Worker     return tree_.find(key);
143*9356374aSAndroid Build Coastguard Worker   }
144*9356374aSAndroid Build Coastguard Worker   template <typename K = key_type>
contains(const key_arg<K> & key)145*9356374aSAndroid Build Coastguard Worker   bool contains(const key_arg<K> &key) const {
146*9356374aSAndroid Build Coastguard Worker     return find(key) != end();
147*9356374aSAndroid Build Coastguard Worker   }
148*9356374aSAndroid Build Coastguard Worker   template <typename K = key_type>
lower_bound(const key_arg<K> & key)149*9356374aSAndroid Build Coastguard Worker   iterator lower_bound(const key_arg<K> &key) ABSL_ATTRIBUTE_LIFETIME_BOUND {
150*9356374aSAndroid Build Coastguard Worker     return tree_.lower_bound(key);
151*9356374aSAndroid Build Coastguard Worker   }
152*9356374aSAndroid Build Coastguard Worker   template <typename K = key_type>
lower_bound(const key_arg<K> & key)153*9356374aSAndroid Build Coastguard Worker   const_iterator lower_bound(const key_arg<K> &key) const
154*9356374aSAndroid Build Coastguard Worker       ABSL_ATTRIBUTE_LIFETIME_BOUND {
155*9356374aSAndroid Build Coastguard Worker     return tree_.lower_bound(key);
156*9356374aSAndroid Build Coastguard Worker   }
157*9356374aSAndroid Build Coastguard Worker   template <typename K = key_type>
upper_bound(const key_arg<K> & key)158*9356374aSAndroid Build Coastguard Worker   iterator upper_bound(const key_arg<K> &key) ABSL_ATTRIBUTE_LIFETIME_BOUND {
159*9356374aSAndroid Build Coastguard Worker     return tree_.upper_bound(key);
160*9356374aSAndroid Build Coastguard Worker   }
161*9356374aSAndroid Build Coastguard Worker   template <typename K = key_type>
upper_bound(const key_arg<K> & key)162*9356374aSAndroid Build Coastguard Worker   const_iterator upper_bound(const key_arg<K> &key) const
163*9356374aSAndroid Build Coastguard Worker       ABSL_ATTRIBUTE_LIFETIME_BOUND {
164*9356374aSAndroid Build Coastguard Worker     return tree_.upper_bound(key);
165*9356374aSAndroid Build Coastguard Worker   }
166*9356374aSAndroid Build Coastguard Worker   template <typename K = key_type>
equal_range(const key_arg<K> & key)167*9356374aSAndroid Build Coastguard Worker   std::pair<iterator, iterator> equal_range(const key_arg<K> &key)
168*9356374aSAndroid Build Coastguard Worker       ABSL_ATTRIBUTE_LIFETIME_BOUND {
169*9356374aSAndroid Build Coastguard Worker     return tree_.equal_range(key);
170*9356374aSAndroid Build Coastguard Worker   }
171*9356374aSAndroid Build Coastguard Worker   template <typename K = key_type>
equal_range(const key_arg<K> & key)172*9356374aSAndroid Build Coastguard Worker   std::pair<const_iterator, const_iterator> equal_range(
173*9356374aSAndroid Build Coastguard Worker       const key_arg<K> &key) const ABSL_ATTRIBUTE_LIFETIME_BOUND {
174*9356374aSAndroid Build Coastguard Worker     return tree_.equal_range(key);
175*9356374aSAndroid Build Coastguard Worker   }
176*9356374aSAndroid Build Coastguard Worker 
177*9356374aSAndroid Build Coastguard Worker   // Deletion routines. Note that there is also a deletion routine that is
178*9356374aSAndroid Build Coastguard Worker   // specific to btree_set_container/btree_multiset_container.
179*9356374aSAndroid Build Coastguard Worker 
180*9356374aSAndroid Build Coastguard Worker   // Erase the specified iterator from the btree. The iterator must be valid
181*9356374aSAndroid Build Coastguard Worker   // (i.e. not equal to end()).  Return an iterator pointing to the node after
182*9356374aSAndroid Build Coastguard Worker   // the one that was erased (or end() if none exists).
erase(const_iterator iter)183*9356374aSAndroid Build Coastguard Worker   iterator erase(const_iterator iter) ABSL_ATTRIBUTE_LIFETIME_BOUND {
184*9356374aSAndroid Build Coastguard Worker     return tree_.erase(iterator(iter));
185*9356374aSAndroid Build Coastguard Worker   }
erase(iterator iter)186*9356374aSAndroid Build Coastguard Worker   iterator erase(iterator iter) ABSL_ATTRIBUTE_LIFETIME_BOUND {
187*9356374aSAndroid Build Coastguard Worker     return tree_.erase(iter);
188*9356374aSAndroid Build Coastguard Worker   }
erase(const_iterator first,const_iterator last)189*9356374aSAndroid Build Coastguard Worker   iterator erase(const_iterator first,
190*9356374aSAndroid Build Coastguard Worker                  const_iterator last) ABSL_ATTRIBUTE_LIFETIME_BOUND {
191*9356374aSAndroid Build Coastguard Worker     return tree_.erase_range(iterator(first), iterator(last)).second;
192*9356374aSAndroid Build Coastguard Worker   }
193*9356374aSAndroid Build Coastguard Worker   template <typename K = key_type>
erase(const key_arg<K> & key)194*9356374aSAndroid Build Coastguard Worker   size_type erase(const key_arg<K> &key) {
195*9356374aSAndroid Build Coastguard Worker     auto equal_range = this->equal_range(key);
196*9356374aSAndroid Build Coastguard Worker     return tree_.erase_range(equal_range.first, equal_range.second).first;
197*9356374aSAndroid Build Coastguard Worker   }
198*9356374aSAndroid Build Coastguard Worker 
199*9356374aSAndroid Build Coastguard Worker   // Extract routines.
extract_and_get_next(const_iterator position)200*9356374aSAndroid Build Coastguard Worker   extract_and_get_next_return_type extract_and_get_next(const_iterator position)
201*9356374aSAndroid Build Coastguard Worker       ABSL_ATTRIBUTE_LIFETIME_BOUND {
202*9356374aSAndroid Build Coastguard Worker     // Use Construct instead of Transfer because the rebalancing code will
203*9356374aSAndroid Build Coastguard Worker     // destroy the slot later.
204*9356374aSAndroid Build Coastguard Worker     // Note: we rely on erase() taking place after Construct().
205*9356374aSAndroid Build Coastguard Worker     return {CommonAccess::Construct<node_type>(get_allocator(),
206*9356374aSAndroid Build Coastguard Worker                                                iterator(position).slot()),
207*9356374aSAndroid Build Coastguard Worker             erase(position)};
208*9356374aSAndroid Build Coastguard Worker   }
extract(iterator position)209*9356374aSAndroid Build Coastguard Worker   node_type extract(iterator position) {
210*9356374aSAndroid Build Coastguard Worker     // Use Construct instead of Transfer because the rebalancing code will
211*9356374aSAndroid Build Coastguard Worker     // destroy the slot later.
212*9356374aSAndroid Build Coastguard Worker     auto node =
213*9356374aSAndroid Build Coastguard Worker         CommonAccess::Construct<node_type>(get_allocator(), position.slot());
214*9356374aSAndroid Build Coastguard Worker     erase(position);
215*9356374aSAndroid Build Coastguard Worker     return node;
216*9356374aSAndroid Build Coastguard Worker   }
extract(const_iterator position)217*9356374aSAndroid Build Coastguard Worker   node_type extract(const_iterator position) {
218*9356374aSAndroid Build Coastguard Worker     return extract(iterator(position));
219*9356374aSAndroid Build Coastguard Worker   }
220*9356374aSAndroid Build Coastguard Worker 
221*9356374aSAndroid Build Coastguard Worker   // Utility routines.
clear()222*9356374aSAndroid Build Coastguard Worker   ABSL_ATTRIBUTE_REINITIALIZES void clear() { tree_.clear(); }
swap(btree_container & other)223*9356374aSAndroid Build Coastguard Worker   void swap(btree_container &other) { tree_.swap(other.tree_); }
verify()224*9356374aSAndroid Build Coastguard Worker   void verify() const { tree_.verify(); }
225*9356374aSAndroid Build Coastguard Worker 
226*9356374aSAndroid Build Coastguard Worker   // Size routines.
size()227*9356374aSAndroid Build Coastguard Worker   size_type size() const { return tree_.size(); }
max_size()228*9356374aSAndroid Build Coastguard Worker   size_type max_size() const { return tree_.max_size(); }
empty()229*9356374aSAndroid Build Coastguard Worker   bool empty() const { return tree_.empty(); }
230*9356374aSAndroid Build Coastguard Worker 
231*9356374aSAndroid Build Coastguard Worker   friend bool operator==(const btree_container &x, const btree_container &y) {
232*9356374aSAndroid Build Coastguard Worker     if (x.size() != y.size()) return false;
233*9356374aSAndroid Build Coastguard Worker     return std::equal(x.begin(), x.end(), y.begin());
234*9356374aSAndroid Build Coastguard Worker   }
235*9356374aSAndroid Build Coastguard Worker 
236*9356374aSAndroid Build Coastguard Worker   friend bool operator!=(const btree_container &x, const btree_container &y) {
237*9356374aSAndroid Build Coastguard Worker     return !(x == y);
238*9356374aSAndroid Build Coastguard Worker   }
239*9356374aSAndroid Build Coastguard Worker 
240*9356374aSAndroid Build Coastguard Worker   friend bool operator<(const btree_container &x, const btree_container &y) {
241*9356374aSAndroid Build Coastguard Worker     return std::lexicographical_compare(x.begin(), x.end(), y.begin(), y.end());
242*9356374aSAndroid Build Coastguard Worker   }
243*9356374aSAndroid Build Coastguard Worker 
244*9356374aSAndroid Build Coastguard Worker   friend bool operator>(const btree_container &x, const btree_container &y) {
245*9356374aSAndroid Build Coastguard Worker     return y < x;
246*9356374aSAndroid Build Coastguard Worker   }
247*9356374aSAndroid Build Coastguard Worker 
248*9356374aSAndroid Build Coastguard Worker   friend bool operator<=(const btree_container &x, const btree_container &y) {
249*9356374aSAndroid Build Coastguard Worker     return !(y < x);
250*9356374aSAndroid Build Coastguard Worker   }
251*9356374aSAndroid Build Coastguard Worker 
252*9356374aSAndroid Build Coastguard Worker   friend bool operator>=(const btree_container &x, const btree_container &y) {
253*9356374aSAndroid Build Coastguard Worker     return !(x < y);
254*9356374aSAndroid Build Coastguard Worker   }
255*9356374aSAndroid Build Coastguard Worker 
256*9356374aSAndroid Build Coastguard Worker   // The allocator used by the btree.
get_allocator()257*9356374aSAndroid Build Coastguard Worker   allocator_type get_allocator() const { return tree_.get_allocator(); }
258*9356374aSAndroid Build Coastguard Worker 
259*9356374aSAndroid Build Coastguard Worker   // The key comparator used by the btree.
key_comp()260*9356374aSAndroid Build Coastguard Worker   key_compare key_comp() const { return key_compare(tree_.key_comp()); }
value_comp()261*9356374aSAndroid Build Coastguard Worker   value_compare value_comp() const { return tree_.value_comp(); }
262*9356374aSAndroid Build Coastguard Worker 
263*9356374aSAndroid Build Coastguard Worker   // Support absl::Hash.
264*9356374aSAndroid Build Coastguard Worker   template <typename State>
AbslHashValue(State h,const btree_container & b)265*9356374aSAndroid Build Coastguard Worker   friend State AbslHashValue(State h, const btree_container &b) {
266*9356374aSAndroid Build Coastguard Worker     for (const auto &v : b) {
267*9356374aSAndroid Build Coastguard Worker       h = State::combine(std::move(h), v);
268*9356374aSAndroid Build Coastguard Worker     }
269*9356374aSAndroid Build Coastguard Worker     return State::combine(std::move(h), b.size());
270*9356374aSAndroid Build Coastguard Worker   }
271*9356374aSAndroid Build Coastguard Worker 
272*9356374aSAndroid Build Coastguard Worker  protected:
273*9356374aSAndroid Build Coastguard Worker   friend struct btree_access;
274*9356374aSAndroid Build Coastguard Worker   Tree tree_;
275*9356374aSAndroid Build Coastguard Worker };
276*9356374aSAndroid Build Coastguard Worker 
277*9356374aSAndroid Build Coastguard Worker // A common base class for btree_set and btree_map.
278*9356374aSAndroid Build Coastguard Worker template <typename Tree>
279*9356374aSAndroid Build Coastguard Worker class btree_set_container : public btree_container<Tree> {
280*9356374aSAndroid Build Coastguard Worker   using super_type = btree_container<Tree>;
281*9356374aSAndroid Build Coastguard Worker   using params_type = typename Tree::params_type;
282*9356374aSAndroid Build Coastguard Worker   using init_type = typename params_type::init_type;
283*9356374aSAndroid Build Coastguard Worker   using is_key_compare_to = typename params_type::is_key_compare_to;
284*9356374aSAndroid Build Coastguard Worker   friend class BtreeNodePeer;
285*9356374aSAndroid Build Coastguard Worker 
286*9356374aSAndroid Build Coastguard Worker  protected:
287*9356374aSAndroid Build Coastguard Worker   template <class K>
288*9356374aSAndroid Build Coastguard Worker   using key_arg = typename super_type::template key_arg<K>;
289*9356374aSAndroid Build Coastguard Worker 
290*9356374aSAndroid Build Coastguard Worker  public:
291*9356374aSAndroid Build Coastguard Worker   using key_type = typename Tree::key_type;
292*9356374aSAndroid Build Coastguard Worker   using value_type = typename Tree::value_type;
293*9356374aSAndroid Build Coastguard Worker   using size_type = typename Tree::size_type;
294*9356374aSAndroid Build Coastguard Worker   using key_compare = typename Tree::original_key_compare;
295*9356374aSAndroid Build Coastguard Worker   using allocator_type = typename Tree::allocator_type;
296*9356374aSAndroid Build Coastguard Worker   using iterator = typename Tree::iterator;
297*9356374aSAndroid Build Coastguard Worker   using const_iterator = typename Tree::const_iterator;
298*9356374aSAndroid Build Coastguard Worker   using node_type = typename super_type::node_type;
299*9356374aSAndroid Build Coastguard Worker   using insert_return_type = InsertReturnType<iterator, node_type>;
300*9356374aSAndroid Build Coastguard Worker 
301*9356374aSAndroid Build Coastguard Worker   // Inherit constructors.
302*9356374aSAndroid Build Coastguard Worker   using super_type::super_type;
btree_set_container()303*9356374aSAndroid Build Coastguard Worker   btree_set_container() {}
304*9356374aSAndroid Build Coastguard Worker 
305*9356374aSAndroid Build Coastguard Worker   // Range constructors.
306*9356374aSAndroid Build Coastguard Worker   template <class InputIterator>
307*9356374aSAndroid Build Coastguard Worker   btree_set_container(InputIterator b, InputIterator e,
308*9356374aSAndroid Build Coastguard Worker                       const key_compare &comp = key_compare(),
309*9356374aSAndroid Build Coastguard Worker                       const allocator_type &alloc = allocator_type())
super_type(comp,alloc)310*9356374aSAndroid Build Coastguard Worker       : super_type(comp, alloc) {
311*9356374aSAndroid Build Coastguard Worker     insert(b, e);
312*9356374aSAndroid Build Coastguard Worker   }
313*9356374aSAndroid Build Coastguard Worker   template <class InputIterator>
btree_set_container(InputIterator b,InputIterator e,const allocator_type & alloc)314*9356374aSAndroid Build Coastguard Worker   btree_set_container(InputIterator b, InputIterator e,
315*9356374aSAndroid Build Coastguard Worker                       const allocator_type &alloc)
316*9356374aSAndroid Build Coastguard Worker       : btree_set_container(b, e, key_compare(), alloc) {}
317*9356374aSAndroid Build Coastguard Worker 
318*9356374aSAndroid Build Coastguard Worker   // Initializer list constructors.
319*9356374aSAndroid Build Coastguard Worker   btree_set_container(std::initializer_list<init_type> init,
320*9356374aSAndroid Build Coastguard Worker                       const key_compare &comp = key_compare(),
321*9356374aSAndroid Build Coastguard Worker                       const allocator_type &alloc = allocator_type())
322*9356374aSAndroid Build Coastguard Worker       : btree_set_container(init.begin(), init.end(), comp, alloc) {}
btree_set_container(std::initializer_list<init_type> init,const allocator_type & alloc)323*9356374aSAndroid Build Coastguard Worker   btree_set_container(std::initializer_list<init_type> init,
324*9356374aSAndroid Build Coastguard Worker                       const allocator_type &alloc)
325*9356374aSAndroid Build Coastguard Worker       : btree_set_container(init.begin(), init.end(), alloc) {}
326*9356374aSAndroid Build Coastguard Worker 
327*9356374aSAndroid Build Coastguard Worker   // Insertion routines.
insert(const value_type & v)328*9356374aSAndroid Build Coastguard Worker   std::pair<iterator, bool> insert(const value_type &v)
329*9356374aSAndroid Build Coastguard Worker       ABSL_ATTRIBUTE_LIFETIME_BOUND {
330*9356374aSAndroid Build Coastguard Worker     return this->tree_.insert_unique(params_type::key(v), v);
331*9356374aSAndroid Build Coastguard Worker   }
insert(value_type && v)332*9356374aSAndroid Build Coastguard Worker   std::pair<iterator, bool> insert(value_type &&v)
333*9356374aSAndroid Build Coastguard Worker       ABSL_ATTRIBUTE_LIFETIME_BOUND {
334*9356374aSAndroid Build Coastguard Worker     return this->tree_.insert_unique(params_type::key(v), std::move(v));
335*9356374aSAndroid Build Coastguard Worker   }
336*9356374aSAndroid Build Coastguard Worker   template <typename... Args>
emplace(Args &&...args)337*9356374aSAndroid Build Coastguard Worker   std::pair<iterator, bool> emplace(Args &&...args)
338*9356374aSAndroid Build Coastguard Worker       ABSL_ATTRIBUTE_LIFETIME_BOUND {
339*9356374aSAndroid Build Coastguard Worker     // Use a node handle to manage a temp slot.
340*9356374aSAndroid Build Coastguard Worker     auto node = CommonAccess::Construct<node_type>(this->get_allocator(),
341*9356374aSAndroid Build Coastguard Worker                                                    std::forward<Args>(args)...);
342*9356374aSAndroid Build Coastguard Worker     auto *slot = CommonAccess::GetSlot(node);
343*9356374aSAndroid Build Coastguard Worker     return this->tree_.insert_unique(params_type::key(slot), slot);
344*9356374aSAndroid Build Coastguard Worker   }
insert(const_iterator hint,const value_type & v)345*9356374aSAndroid Build Coastguard Worker   iterator insert(const_iterator hint,
346*9356374aSAndroid Build Coastguard Worker                   const value_type &v) ABSL_ATTRIBUTE_LIFETIME_BOUND {
347*9356374aSAndroid Build Coastguard Worker     return this->tree_
348*9356374aSAndroid Build Coastguard Worker         .insert_hint_unique(iterator(hint), params_type::key(v), v)
349*9356374aSAndroid Build Coastguard Worker         .first;
350*9356374aSAndroid Build Coastguard Worker   }
insert(const_iterator hint,value_type && v)351*9356374aSAndroid Build Coastguard Worker   iterator insert(const_iterator hint,
352*9356374aSAndroid Build Coastguard Worker                   value_type &&v) ABSL_ATTRIBUTE_LIFETIME_BOUND {
353*9356374aSAndroid Build Coastguard Worker     return this->tree_
354*9356374aSAndroid Build Coastguard Worker         .insert_hint_unique(iterator(hint), params_type::key(v), std::move(v))
355*9356374aSAndroid Build Coastguard Worker         .first;
356*9356374aSAndroid Build Coastguard Worker   }
357*9356374aSAndroid Build Coastguard Worker   template <typename... Args>
emplace_hint(const_iterator hint,Args &&...args)358*9356374aSAndroid Build Coastguard Worker   iterator emplace_hint(const_iterator hint,
359*9356374aSAndroid Build Coastguard Worker                         Args &&...args) ABSL_ATTRIBUTE_LIFETIME_BOUND {
360*9356374aSAndroid Build Coastguard Worker     // Use a node handle to manage a temp slot.
361*9356374aSAndroid Build Coastguard Worker     auto node = CommonAccess::Construct<node_type>(this->get_allocator(),
362*9356374aSAndroid Build Coastguard Worker                                                    std::forward<Args>(args)...);
363*9356374aSAndroid Build Coastguard Worker     auto *slot = CommonAccess::GetSlot(node);
364*9356374aSAndroid Build Coastguard Worker     return this->tree_
365*9356374aSAndroid Build Coastguard Worker         .insert_hint_unique(iterator(hint), params_type::key(slot), slot)
366*9356374aSAndroid Build Coastguard Worker         .first;
367*9356374aSAndroid Build Coastguard Worker   }
368*9356374aSAndroid Build Coastguard Worker   template <typename InputIterator>
insert(InputIterator b,InputIterator e)369*9356374aSAndroid Build Coastguard Worker   void insert(InputIterator b, InputIterator e) {
370*9356374aSAndroid Build Coastguard Worker     this->tree_.insert_iterator_unique(b, e, 0);
371*9356374aSAndroid Build Coastguard Worker   }
insert(std::initializer_list<init_type> init)372*9356374aSAndroid Build Coastguard Worker   void insert(std::initializer_list<init_type> init) {
373*9356374aSAndroid Build Coastguard Worker     this->tree_.insert_iterator_unique(init.begin(), init.end(), 0);
374*9356374aSAndroid Build Coastguard Worker   }
insert(node_type && node)375*9356374aSAndroid Build Coastguard Worker   insert_return_type insert(node_type &&node) ABSL_ATTRIBUTE_LIFETIME_BOUND {
376*9356374aSAndroid Build Coastguard Worker     if (!node) return {this->end(), false, node_type()};
377*9356374aSAndroid Build Coastguard Worker     std::pair<iterator, bool> res =
378*9356374aSAndroid Build Coastguard Worker         this->tree_.insert_unique(params_type::key(CommonAccess::GetSlot(node)),
379*9356374aSAndroid Build Coastguard Worker                                   CommonAccess::GetSlot(node));
380*9356374aSAndroid Build Coastguard Worker     if (res.second) {
381*9356374aSAndroid Build Coastguard Worker       CommonAccess::Destroy(&node);
382*9356374aSAndroid Build Coastguard Worker       return {res.first, true, node_type()};
383*9356374aSAndroid Build Coastguard Worker     } else {
384*9356374aSAndroid Build Coastguard Worker       return {res.first, false, std::move(node)};
385*9356374aSAndroid Build Coastguard Worker     }
386*9356374aSAndroid Build Coastguard Worker   }
insert(const_iterator hint,node_type && node)387*9356374aSAndroid Build Coastguard Worker   iterator insert(const_iterator hint,
388*9356374aSAndroid Build Coastguard Worker                   node_type &&node) ABSL_ATTRIBUTE_LIFETIME_BOUND {
389*9356374aSAndroid Build Coastguard Worker     if (!node) return this->end();
390*9356374aSAndroid Build Coastguard Worker     std::pair<iterator, bool> res = this->tree_.insert_hint_unique(
391*9356374aSAndroid Build Coastguard Worker         iterator(hint), params_type::key(CommonAccess::GetSlot(node)),
392*9356374aSAndroid Build Coastguard Worker         CommonAccess::GetSlot(node));
393*9356374aSAndroid Build Coastguard Worker     if (res.second) CommonAccess::Destroy(&node);
394*9356374aSAndroid Build Coastguard Worker     return res.first;
395*9356374aSAndroid Build Coastguard Worker   }
396*9356374aSAndroid Build Coastguard Worker 
397*9356374aSAndroid Build Coastguard Worker   // Node extraction routines.
398*9356374aSAndroid Build Coastguard Worker   template <typename K = key_type>
extract(const key_arg<K> & key)399*9356374aSAndroid Build Coastguard Worker   node_type extract(const key_arg<K> &key) {
400*9356374aSAndroid Build Coastguard Worker     const std::pair<iterator, bool> lower_and_equal =
401*9356374aSAndroid Build Coastguard Worker         this->tree_.lower_bound_equal(key);
402*9356374aSAndroid Build Coastguard Worker     return lower_and_equal.second ? extract(lower_and_equal.first)
403*9356374aSAndroid Build Coastguard Worker                                   : node_type();
404*9356374aSAndroid Build Coastguard Worker   }
405*9356374aSAndroid Build Coastguard Worker   using super_type::extract;
406*9356374aSAndroid Build Coastguard Worker 
407*9356374aSAndroid Build Coastguard Worker   // Merge routines.
408*9356374aSAndroid Build Coastguard Worker   // Moves elements from `src` into `this`. If the element already exists in
409*9356374aSAndroid Build Coastguard Worker   // `this`, it is left unmodified in `src`.
410*9356374aSAndroid Build Coastguard Worker   template <
411*9356374aSAndroid Build Coastguard Worker       typename T,
412*9356374aSAndroid Build Coastguard Worker       typename absl::enable_if_t<
413*9356374aSAndroid Build Coastguard Worker           absl::conjunction<
414*9356374aSAndroid Build Coastguard Worker               std::is_same<value_type, typename T::value_type>,
415*9356374aSAndroid Build Coastguard Worker               std::is_same<allocator_type, typename T::allocator_type>,
416*9356374aSAndroid Build Coastguard Worker               std::is_same<typename params_type::is_map_container,
417*9356374aSAndroid Build Coastguard Worker                            typename T::params_type::is_map_container>>::value,
418*9356374aSAndroid Build Coastguard Worker           int> = 0>
merge(btree_container<T> & src)419*9356374aSAndroid Build Coastguard Worker   void merge(btree_container<T> &src) {  // NOLINT
420*9356374aSAndroid Build Coastguard Worker     for (auto src_it = src.begin(); src_it != src.end();) {
421*9356374aSAndroid Build Coastguard Worker       if (insert(std::move(params_type::element(src_it.slot()))).second) {
422*9356374aSAndroid Build Coastguard Worker         src_it = src.erase(src_it);
423*9356374aSAndroid Build Coastguard Worker       } else {
424*9356374aSAndroid Build Coastguard Worker         ++src_it;
425*9356374aSAndroid Build Coastguard Worker       }
426*9356374aSAndroid Build Coastguard Worker     }
427*9356374aSAndroid Build Coastguard Worker   }
428*9356374aSAndroid Build Coastguard Worker 
429*9356374aSAndroid Build Coastguard Worker   template <
430*9356374aSAndroid Build Coastguard Worker       typename T,
431*9356374aSAndroid Build Coastguard Worker       typename absl::enable_if_t<
432*9356374aSAndroid Build Coastguard Worker           absl::conjunction<
433*9356374aSAndroid Build Coastguard Worker               std::is_same<value_type, typename T::value_type>,
434*9356374aSAndroid Build Coastguard Worker               std::is_same<allocator_type, typename T::allocator_type>,
435*9356374aSAndroid Build Coastguard Worker               std::is_same<typename params_type::is_map_container,
436*9356374aSAndroid Build Coastguard Worker                            typename T::params_type::is_map_container>>::value,
437*9356374aSAndroid Build Coastguard Worker           int> = 0>
merge(btree_container<T> && src)438*9356374aSAndroid Build Coastguard Worker   void merge(btree_container<T> &&src) {
439*9356374aSAndroid Build Coastguard Worker     merge(src);
440*9356374aSAndroid Build Coastguard Worker   }
441*9356374aSAndroid Build Coastguard Worker };
442*9356374aSAndroid Build Coastguard Worker 
443*9356374aSAndroid Build Coastguard Worker // Base class for btree_map.
444*9356374aSAndroid Build Coastguard Worker template <typename Tree>
445*9356374aSAndroid Build Coastguard Worker class btree_map_container : public btree_set_container<Tree> {
446*9356374aSAndroid Build Coastguard Worker   using super_type = btree_set_container<Tree>;
447*9356374aSAndroid Build Coastguard Worker   using params_type = typename Tree::params_type;
448*9356374aSAndroid Build Coastguard Worker   friend class BtreeNodePeer;
449*9356374aSAndroid Build Coastguard Worker 
450*9356374aSAndroid Build Coastguard Worker  private:
451*9356374aSAndroid Build Coastguard Worker   template <class K>
452*9356374aSAndroid Build Coastguard Worker   using key_arg = typename super_type::template key_arg<K>;
453*9356374aSAndroid Build Coastguard Worker 
454*9356374aSAndroid Build Coastguard Worker  public:
455*9356374aSAndroid Build Coastguard Worker   using key_type = typename Tree::key_type;
456*9356374aSAndroid Build Coastguard Worker   using mapped_type = typename params_type::mapped_type;
457*9356374aSAndroid Build Coastguard Worker   using value_type = typename Tree::value_type;
458*9356374aSAndroid Build Coastguard Worker   using key_compare = typename Tree::original_key_compare;
459*9356374aSAndroid Build Coastguard Worker   using allocator_type = typename Tree::allocator_type;
460*9356374aSAndroid Build Coastguard Worker   using iterator = typename Tree::iterator;
461*9356374aSAndroid Build Coastguard Worker   using const_iterator = typename Tree::const_iterator;
462*9356374aSAndroid Build Coastguard Worker 
463*9356374aSAndroid Build Coastguard Worker   // Inherit constructors.
464*9356374aSAndroid Build Coastguard Worker   using super_type::super_type;
btree_map_container()465*9356374aSAndroid Build Coastguard Worker   btree_map_container() {}
466*9356374aSAndroid Build Coastguard Worker 
467*9356374aSAndroid Build Coastguard Worker   // Insertion routines.
468*9356374aSAndroid Build Coastguard Worker   // Note: the nullptr template arguments and extra `const M&` overloads allow
469*9356374aSAndroid Build Coastguard Worker   // for supporting bitfield arguments.
470*9356374aSAndroid Build Coastguard Worker   template <typename K = key_type, class M>
insert_or_assign(const key_arg<K> & k,const M & obj)471*9356374aSAndroid Build Coastguard Worker   std::pair<iterator, bool> insert_or_assign(const key_arg<K> &k, const M &obj)
472*9356374aSAndroid Build Coastguard Worker       ABSL_ATTRIBUTE_LIFETIME_BOUND {
473*9356374aSAndroid Build Coastguard Worker     return insert_or_assign_impl(k, obj);
474*9356374aSAndroid Build Coastguard Worker   }
475*9356374aSAndroid Build Coastguard Worker   template <typename K = key_type, class M, K * = nullptr>
insert_or_assign(key_arg<K> && k,const M & obj)476*9356374aSAndroid Build Coastguard Worker   std::pair<iterator, bool> insert_or_assign(key_arg<K> &&k, const M &obj)
477*9356374aSAndroid Build Coastguard Worker       ABSL_ATTRIBUTE_LIFETIME_BOUND {
478*9356374aSAndroid Build Coastguard Worker     return insert_or_assign_impl(std::forward<K>(k), obj);
479*9356374aSAndroid Build Coastguard Worker   }
480*9356374aSAndroid Build Coastguard Worker   template <typename K = key_type, class M, M * = nullptr>
insert_or_assign(const key_arg<K> & k,M && obj)481*9356374aSAndroid Build Coastguard Worker   std::pair<iterator, bool> insert_or_assign(const key_arg<K> &k, M &&obj)
482*9356374aSAndroid Build Coastguard Worker       ABSL_ATTRIBUTE_LIFETIME_BOUND {
483*9356374aSAndroid Build Coastguard Worker     return insert_or_assign_impl(k, std::forward<M>(obj));
484*9356374aSAndroid Build Coastguard Worker   }
485*9356374aSAndroid Build Coastguard Worker   template <typename K = key_type, class M, K * = nullptr, M * = nullptr>
insert_or_assign(key_arg<K> && k,M && obj)486*9356374aSAndroid Build Coastguard Worker   std::pair<iterator, bool> insert_or_assign(key_arg<K> &&k, M &&obj)
487*9356374aSAndroid Build Coastguard Worker       ABSL_ATTRIBUTE_LIFETIME_BOUND {
488*9356374aSAndroid Build Coastguard Worker     return insert_or_assign_impl(std::forward<K>(k), std::forward<M>(obj));
489*9356374aSAndroid Build Coastguard Worker   }
490*9356374aSAndroid Build Coastguard Worker   template <typename K = key_type, class M>
insert_or_assign(const_iterator hint,const key_arg<K> & k,const M & obj)491*9356374aSAndroid Build Coastguard Worker   iterator insert_or_assign(const_iterator hint, const key_arg<K> &k,
492*9356374aSAndroid Build Coastguard Worker                             const M &obj) ABSL_ATTRIBUTE_LIFETIME_BOUND {
493*9356374aSAndroid Build Coastguard Worker     return insert_or_assign_hint_impl(hint, k, obj);
494*9356374aSAndroid Build Coastguard Worker   }
495*9356374aSAndroid Build Coastguard Worker   template <typename K = key_type, class M, K * = nullptr>
insert_or_assign(const_iterator hint,key_arg<K> && k,const M & obj)496*9356374aSAndroid Build Coastguard Worker   iterator insert_or_assign(const_iterator hint, key_arg<K> &&k,
497*9356374aSAndroid Build Coastguard Worker                             const M &obj) ABSL_ATTRIBUTE_LIFETIME_BOUND {
498*9356374aSAndroid Build Coastguard Worker     return insert_or_assign_hint_impl(hint, std::forward<K>(k), obj);
499*9356374aSAndroid Build Coastguard Worker   }
500*9356374aSAndroid Build Coastguard Worker   template <typename K = key_type, class M, M * = nullptr>
insert_or_assign(const_iterator hint,const key_arg<K> & k,M && obj)501*9356374aSAndroid Build Coastguard Worker   iterator insert_or_assign(const_iterator hint, const key_arg<K> &k,
502*9356374aSAndroid Build Coastguard Worker                             M &&obj) ABSL_ATTRIBUTE_LIFETIME_BOUND {
503*9356374aSAndroid Build Coastguard Worker     return insert_or_assign_hint_impl(hint, k, std::forward<M>(obj));
504*9356374aSAndroid Build Coastguard Worker   }
505*9356374aSAndroid Build Coastguard Worker   template <typename K = key_type, class M, K * = nullptr, M * = nullptr>
insert_or_assign(const_iterator hint,key_arg<K> && k,M && obj)506*9356374aSAndroid Build Coastguard Worker   iterator insert_or_assign(const_iterator hint, key_arg<K> &&k,
507*9356374aSAndroid Build Coastguard Worker                             M &&obj) ABSL_ATTRIBUTE_LIFETIME_BOUND {
508*9356374aSAndroid Build Coastguard Worker     return insert_or_assign_hint_impl(hint, std::forward<K>(k),
509*9356374aSAndroid Build Coastguard Worker                                       std::forward<M>(obj));
510*9356374aSAndroid Build Coastguard Worker   }
511*9356374aSAndroid Build Coastguard Worker 
512*9356374aSAndroid Build Coastguard Worker   template <typename K = key_type, typename... Args,
513*9356374aSAndroid Build Coastguard Worker             typename absl::enable_if_t<
514*9356374aSAndroid Build Coastguard Worker                 !std::is_convertible<K, const_iterator>::value, int> = 0>
try_emplace(const key_arg<K> & k,Args &&...args)515*9356374aSAndroid Build Coastguard Worker   std::pair<iterator, bool> try_emplace(const key_arg<K> &k, Args &&...args)
516*9356374aSAndroid Build Coastguard Worker       ABSL_ATTRIBUTE_LIFETIME_BOUND {
517*9356374aSAndroid Build Coastguard Worker     return try_emplace_impl(k, std::forward<Args>(args)...);
518*9356374aSAndroid Build Coastguard Worker   }
519*9356374aSAndroid Build Coastguard Worker   template <typename K = key_type, typename... Args,
520*9356374aSAndroid Build Coastguard Worker             typename absl::enable_if_t<
521*9356374aSAndroid Build Coastguard Worker                 !std::is_convertible<K, const_iterator>::value, int> = 0>
try_emplace(key_arg<K> && k,Args &&...args)522*9356374aSAndroid Build Coastguard Worker   std::pair<iterator, bool> try_emplace(key_arg<K> &&k, Args &&...args)
523*9356374aSAndroid Build Coastguard Worker       ABSL_ATTRIBUTE_LIFETIME_BOUND {
524*9356374aSAndroid Build Coastguard Worker     return try_emplace_impl(std::forward<K>(k), std::forward<Args>(args)...);
525*9356374aSAndroid Build Coastguard Worker   }
526*9356374aSAndroid Build Coastguard Worker   template <typename K = key_type, typename... Args>
try_emplace(const_iterator hint,const key_arg<K> & k,Args &&...args)527*9356374aSAndroid Build Coastguard Worker   iterator try_emplace(const_iterator hint, const key_arg<K> &k,
528*9356374aSAndroid Build Coastguard Worker                        Args &&...args) ABSL_ATTRIBUTE_LIFETIME_BOUND {
529*9356374aSAndroid Build Coastguard Worker     return try_emplace_hint_impl(hint, k, std::forward<Args>(args)...);
530*9356374aSAndroid Build Coastguard Worker   }
531*9356374aSAndroid Build Coastguard Worker   template <typename K = key_type, typename... Args>
try_emplace(const_iterator hint,key_arg<K> && k,Args &&...args)532*9356374aSAndroid Build Coastguard Worker   iterator try_emplace(const_iterator hint, key_arg<K> &&k,
533*9356374aSAndroid Build Coastguard Worker                        Args &&...args) ABSL_ATTRIBUTE_LIFETIME_BOUND {
534*9356374aSAndroid Build Coastguard Worker     return try_emplace_hint_impl(hint, std::forward<K>(k),
535*9356374aSAndroid Build Coastguard Worker                                  std::forward<Args>(args)...);
536*9356374aSAndroid Build Coastguard Worker   }
537*9356374aSAndroid Build Coastguard Worker 
538*9356374aSAndroid Build Coastguard Worker   template <typename K = key_type>
539*9356374aSAndroid Build Coastguard Worker   mapped_type &operator[](const key_arg<K> &k) ABSL_ATTRIBUTE_LIFETIME_BOUND {
540*9356374aSAndroid Build Coastguard Worker     return try_emplace(k).first->second;
541*9356374aSAndroid Build Coastguard Worker   }
542*9356374aSAndroid Build Coastguard Worker   template <typename K = key_type>
543*9356374aSAndroid Build Coastguard Worker   mapped_type &operator[](key_arg<K> &&k) ABSL_ATTRIBUTE_LIFETIME_BOUND {
544*9356374aSAndroid Build Coastguard Worker     return try_emplace(std::forward<K>(k)).first->second;
545*9356374aSAndroid Build Coastguard Worker   }
546*9356374aSAndroid Build Coastguard Worker 
547*9356374aSAndroid Build Coastguard Worker   template <typename K = key_type>
at(const key_arg<K> & key)548*9356374aSAndroid Build Coastguard Worker   mapped_type &at(const key_arg<K> &key) ABSL_ATTRIBUTE_LIFETIME_BOUND {
549*9356374aSAndroid Build Coastguard Worker     auto it = this->find(key);
550*9356374aSAndroid Build Coastguard Worker     if (it == this->end())
551*9356374aSAndroid Build Coastguard Worker       base_internal::ThrowStdOutOfRange("absl::btree_map::at");
552*9356374aSAndroid Build Coastguard Worker     return it->second;
553*9356374aSAndroid Build Coastguard Worker   }
554*9356374aSAndroid Build Coastguard Worker   template <typename K = key_type>
at(const key_arg<K> & key)555*9356374aSAndroid Build Coastguard Worker   const mapped_type &at(const key_arg<K> &key) const
556*9356374aSAndroid Build Coastguard Worker       ABSL_ATTRIBUTE_LIFETIME_BOUND {
557*9356374aSAndroid Build Coastguard Worker     auto it = this->find(key);
558*9356374aSAndroid Build Coastguard Worker     if (it == this->end())
559*9356374aSAndroid Build Coastguard Worker       base_internal::ThrowStdOutOfRange("absl::btree_map::at");
560*9356374aSAndroid Build Coastguard Worker     return it->second;
561*9356374aSAndroid Build Coastguard Worker   }
562*9356374aSAndroid Build Coastguard Worker 
563*9356374aSAndroid Build Coastguard Worker  private:
564*9356374aSAndroid Build Coastguard Worker   // Note: when we call `std::forward<M>(obj)` twice, it's safe because
565*9356374aSAndroid Build Coastguard Worker   // insert_unique/insert_hint_unique are guaranteed to not consume `obj` when
566*9356374aSAndroid Build Coastguard Worker   // `ret.second` is false.
567*9356374aSAndroid Build Coastguard Worker   template <class K, class M>
insert_or_assign_impl(K && k,M && obj)568*9356374aSAndroid Build Coastguard Worker   std::pair<iterator, bool> insert_or_assign_impl(K &&k, M &&obj) {
569*9356374aSAndroid Build Coastguard Worker     const std::pair<iterator, bool> ret =
570*9356374aSAndroid Build Coastguard Worker         this->tree_.insert_unique(k, std::forward<K>(k), std::forward<M>(obj));
571*9356374aSAndroid Build Coastguard Worker     if (!ret.second) ret.first->second = std::forward<M>(obj);
572*9356374aSAndroid Build Coastguard Worker     return ret;
573*9356374aSAndroid Build Coastguard Worker   }
574*9356374aSAndroid Build Coastguard Worker   template <class K, class M>
insert_or_assign_hint_impl(const_iterator hint,K && k,M && obj)575*9356374aSAndroid Build Coastguard Worker   iterator insert_or_assign_hint_impl(const_iterator hint, K &&k, M &&obj) {
576*9356374aSAndroid Build Coastguard Worker     const std::pair<iterator, bool> ret = this->tree_.insert_hint_unique(
577*9356374aSAndroid Build Coastguard Worker         iterator(hint), k, std::forward<K>(k), std::forward<M>(obj));
578*9356374aSAndroid Build Coastguard Worker     if (!ret.second) ret.first->second = std::forward<M>(obj);
579*9356374aSAndroid Build Coastguard Worker     return ret.first;
580*9356374aSAndroid Build Coastguard Worker   }
581*9356374aSAndroid Build Coastguard Worker 
582*9356374aSAndroid Build Coastguard Worker   template <class K, class... Args>
try_emplace_impl(K && k,Args &&...args)583*9356374aSAndroid Build Coastguard Worker   std::pair<iterator, bool> try_emplace_impl(K &&k, Args &&... args) {
584*9356374aSAndroid Build Coastguard Worker     return this->tree_.insert_unique(
585*9356374aSAndroid Build Coastguard Worker         k, std::piecewise_construct, std::forward_as_tuple(std::forward<K>(k)),
586*9356374aSAndroid Build Coastguard Worker         std::forward_as_tuple(std::forward<Args>(args)...));
587*9356374aSAndroid Build Coastguard Worker   }
588*9356374aSAndroid Build Coastguard Worker   template <class K, class... Args>
try_emplace_hint_impl(const_iterator hint,K && k,Args &&...args)589*9356374aSAndroid Build Coastguard Worker   iterator try_emplace_hint_impl(const_iterator hint, K &&k, Args &&... args) {
590*9356374aSAndroid Build Coastguard Worker     return this->tree_
591*9356374aSAndroid Build Coastguard Worker         .insert_hint_unique(iterator(hint), k, std::piecewise_construct,
592*9356374aSAndroid Build Coastguard Worker                             std::forward_as_tuple(std::forward<K>(k)),
593*9356374aSAndroid Build Coastguard Worker                             std::forward_as_tuple(std::forward<Args>(args)...))
594*9356374aSAndroid Build Coastguard Worker         .first;
595*9356374aSAndroid Build Coastguard Worker   }
596*9356374aSAndroid Build Coastguard Worker };
597*9356374aSAndroid Build Coastguard Worker 
598*9356374aSAndroid Build Coastguard Worker // A common base class for btree_multiset and btree_multimap.
599*9356374aSAndroid Build Coastguard Worker template <typename Tree>
600*9356374aSAndroid Build Coastguard Worker class btree_multiset_container : public btree_container<Tree> {
601*9356374aSAndroid Build Coastguard Worker   using super_type = btree_container<Tree>;
602*9356374aSAndroid Build Coastguard Worker   using params_type = typename Tree::params_type;
603*9356374aSAndroid Build Coastguard Worker   using init_type = typename params_type::init_type;
604*9356374aSAndroid Build Coastguard Worker   using is_key_compare_to = typename params_type::is_key_compare_to;
605*9356374aSAndroid Build Coastguard Worker   friend class BtreeNodePeer;
606*9356374aSAndroid Build Coastguard Worker 
607*9356374aSAndroid Build Coastguard Worker   template <class K>
608*9356374aSAndroid Build Coastguard Worker   using key_arg = typename super_type::template key_arg<K>;
609*9356374aSAndroid Build Coastguard Worker 
610*9356374aSAndroid Build Coastguard Worker  public:
611*9356374aSAndroid Build Coastguard Worker   using key_type = typename Tree::key_type;
612*9356374aSAndroid Build Coastguard Worker   using value_type = typename Tree::value_type;
613*9356374aSAndroid Build Coastguard Worker   using size_type = typename Tree::size_type;
614*9356374aSAndroid Build Coastguard Worker   using key_compare = typename Tree::original_key_compare;
615*9356374aSAndroid Build Coastguard Worker   using allocator_type = typename Tree::allocator_type;
616*9356374aSAndroid Build Coastguard Worker   using iterator = typename Tree::iterator;
617*9356374aSAndroid Build Coastguard Worker   using const_iterator = typename Tree::const_iterator;
618*9356374aSAndroid Build Coastguard Worker   using node_type = typename super_type::node_type;
619*9356374aSAndroid Build Coastguard Worker 
620*9356374aSAndroid Build Coastguard Worker   // Inherit constructors.
621*9356374aSAndroid Build Coastguard Worker   using super_type::super_type;
btree_multiset_container()622*9356374aSAndroid Build Coastguard Worker   btree_multiset_container() {}
623*9356374aSAndroid Build Coastguard Worker 
624*9356374aSAndroid Build Coastguard Worker   // Range constructors.
625*9356374aSAndroid Build Coastguard Worker   template <class InputIterator>
626*9356374aSAndroid Build Coastguard Worker   btree_multiset_container(InputIterator b, InputIterator e,
627*9356374aSAndroid Build Coastguard Worker                            const key_compare &comp = key_compare(),
628*9356374aSAndroid Build Coastguard Worker                            const allocator_type &alloc = allocator_type())
super_type(comp,alloc)629*9356374aSAndroid Build Coastguard Worker       : super_type(comp, alloc) {
630*9356374aSAndroid Build Coastguard Worker     insert(b, e);
631*9356374aSAndroid Build Coastguard Worker   }
632*9356374aSAndroid Build Coastguard Worker   template <class InputIterator>
btree_multiset_container(InputIterator b,InputIterator e,const allocator_type & alloc)633*9356374aSAndroid Build Coastguard Worker   btree_multiset_container(InputIterator b, InputIterator e,
634*9356374aSAndroid Build Coastguard Worker                            const allocator_type &alloc)
635*9356374aSAndroid Build Coastguard Worker       : btree_multiset_container(b, e, key_compare(), alloc) {}
636*9356374aSAndroid Build Coastguard Worker 
637*9356374aSAndroid Build Coastguard Worker   // Initializer list constructors.
638*9356374aSAndroid Build Coastguard Worker   btree_multiset_container(std::initializer_list<init_type> init,
639*9356374aSAndroid Build Coastguard Worker                            const key_compare &comp = key_compare(),
640*9356374aSAndroid Build Coastguard Worker                            const allocator_type &alloc = allocator_type())
641*9356374aSAndroid Build Coastguard Worker       : btree_multiset_container(init.begin(), init.end(), comp, alloc) {}
btree_multiset_container(std::initializer_list<init_type> init,const allocator_type & alloc)642*9356374aSAndroid Build Coastguard Worker   btree_multiset_container(std::initializer_list<init_type> init,
643*9356374aSAndroid Build Coastguard Worker                            const allocator_type &alloc)
644*9356374aSAndroid Build Coastguard Worker       : btree_multiset_container(init.begin(), init.end(), alloc) {}
645*9356374aSAndroid Build Coastguard Worker 
646*9356374aSAndroid Build Coastguard Worker   // Insertion routines.
insert(const value_type & v)647*9356374aSAndroid Build Coastguard Worker   iterator insert(const value_type &v) ABSL_ATTRIBUTE_LIFETIME_BOUND {
648*9356374aSAndroid Build Coastguard Worker     return this->tree_.insert_multi(v);
649*9356374aSAndroid Build Coastguard Worker   }
insert(value_type && v)650*9356374aSAndroid Build Coastguard Worker   iterator insert(value_type &&v) ABSL_ATTRIBUTE_LIFETIME_BOUND {
651*9356374aSAndroid Build Coastguard Worker     return this->tree_.insert_multi(std::move(v));
652*9356374aSAndroid Build Coastguard Worker   }
insert(const_iterator hint,const value_type & v)653*9356374aSAndroid Build Coastguard Worker   iterator insert(const_iterator hint,
654*9356374aSAndroid Build Coastguard Worker                   const value_type &v) ABSL_ATTRIBUTE_LIFETIME_BOUND {
655*9356374aSAndroid Build Coastguard Worker     return this->tree_.insert_hint_multi(iterator(hint), v);
656*9356374aSAndroid Build Coastguard Worker   }
insert(const_iterator hint,value_type && v)657*9356374aSAndroid Build Coastguard Worker   iterator insert(const_iterator hint,
658*9356374aSAndroid Build Coastguard Worker                   value_type &&v) ABSL_ATTRIBUTE_LIFETIME_BOUND {
659*9356374aSAndroid Build Coastguard Worker     return this->tree_.insert_hint_multi(iterator(hint), std::move(v));
660*9356374aSAndroid Build Coastguard Worker   }
661*9356374aSAndroid Build Coastguard Worker   template <typename InputIterator>
insert(InputIterator b,InputIterator e)662*9356374aSAndroid Build Coastguard Worker   void insert(InputIterator b, InputIterator e) {
663*9356374aSAndroid Build Coastguard Worker     this->tree_.insert_iterator_multi(b, e);
664*9356374aSAndroid Build Coastguard Worker   }
insert(std::initializer_list<init_type> init)665*9356374aSAndroid Build Coastguard Worker   void insert(std::initializer_list<init_type> init) {
666*9356374aSAndroid Build Coastguard Worker     this->tree_.insert_iterator_multi(init.begin(), init.end());
667*9356374aSAndroid Build Coastguard Worker   }
668*9356374aSAndroid Build Coastguard Worker   template <typename... Args>
emplace(Args &&...args)669*9356374aSAndroid Build Coastguard Worker   iterator emplace(Args &&...args) ABSL_ATTRIBUTE_LIFETIME_BOUND {
670*9356374aSAndroid Build Coastguard Worker     // Use a node handle to manage a temp slot.
671*9356374aSAndroid Build Coastguard Worker     auto node = CommonAccess::Construct<node_type>(this->get_allocator(),
672*9356374aSAndroid Build Coastguard Worker                                                    std::forward<Args>(args)...);
673*9356374aSAndroid Build Coastguard Worker     return this->tree_.insert_multi(CommonAccess::GetSlot(node));
674*9356374aSAndroid Build Coastguard Worker   }
675*9356374aSAndroid Build Coastguard Worker   template <typename... Args>
emplace_hint(const_iterator hint,Args &&...args)676*9356374aSAndroid Build Coastguard Worker   iterator emplace_hint(const_iterator hint,
677*9356374aSAndroid Build Coastguard Worker                         Args &&...args) ABSL_ATTRIBUTE_LIFETIME_BOUND {
678*9356374aSAndroid Build Coastguard Worker     // Use a node handle to manage a temp slot.
679*9356374aSAndroid Build Coastguard Worker     auto node = CommonAccess::Construct<node_type>(this->get_allocator(),
680*9356374aSAndroid Build Coastguard Worker                                                    std::forward<Args>(args)...);
681*9356374aSAndroid Build Coastguard Worker     return this->tree_.insert_hint_multi(iterator(hint),
682*9356374aSAndroid Build Coastguard Worker                                          CommonAccess::GetSlot(node));
683*9356374aSAndroid Build Coastguard Worker   }
insert(node_type && node)684*9356374aSAndroid Build Coastguard Worker   iterator insert(node_type &&node) ABSL_ATTRIBUTE_LIFETIME_BOUND {
685*9356374aSAndroid Build Coastguard Worker     if (!node) return this->end();
686*9356374aSAndroid Build Coastguard Worker     iterator res =
687*9356374aSAndroid Build Coastguard Worker         this->tree_.insert_multi(params_type::key(CommonAccess::GetSlot(node)),
688*9356374aSAndroid Build Coastguard Worker                                  CommonAccess::GetSlot(node));
689*9356374aSAndroid Build Coastguard Worker     CommonAccess::Destroy(&node);
690*9356374aSAndroid Build Coastguard Worker     return res;
691*9356374aSAndroid Build Coastguard Worker   }
insert(const_iterator hint,node_type && node)692*9356374aSAndroid Build Coastguard Worker   iterator insert(const_iterator hint,
693*9356374aSAndroid Build Coastguard Worker                   node_type &&node) ABSL_ATTRIBUTE_LIFETIME_BOUND {
694*9356374aSAndroid Build Coastguard Worker     if (!node) return this->end();
695*9356374aSAndroid Build Coastguard Worker     iterator res = this->tree_.insert_hint_multi(
696*9356374aSAndroid Build Coastguard Worker         iterator(hint),
697*9356374aSAndroid Build Coastguard Worker         std::move(params_type::element(CommonAccess::GetSlot(node))));
698*9356374aSAndroid Build Coastguard Worker     CommonAccess::Destroy(&node);
699*9356374aSAndroid Build Coastguard Worker     return res;
700*9356374aSAndroid Build Coastguard Worker   }
701*9356374aSAndroid Build Coastguard Worker 
702*9356374aSAndroid Build Coastguard Worker   // Node extraction routines.
703*9356374aSAndroid Build Coastguard Worker   template <typename K = key_type>
extract(const key_arg<K> & key)704*9356374aSAndroid Build Coastguard Worker   node_type extract(const key_arg<K> &key) {
705*9356374aSAndroid Build Coastguard Worker     const std::pair<iterator, bool> lower_and_equal =
706*9356374aSAndroid Build Coastguard Worker         this->tree_.lower_bound_equal(key);
707*9356374aSAndroid Build Coastguard Worker     return lower_and_equal.second ? extract(lower_and_equal.first)
708*9356374aSAndroid Build Coastguard Worker                                   : node_type();
709*9356374aSAndroid Build Coastguard Worker   }
710*9356374aSAndroid Build Coastguard Worker   using super_type::extract;
711*9356374aSAndroid Build Coastguard Worker 
712*9356374aSAndroid Build Coastguard Worker   // Merge routines.
713*9356374aSAndroid Build Coastguard Worker   // Moves all elements from `src` into `this`.
714*9356374aSAndroid Build Coastguard Worker   template <
715*9356374aSAndroid Build Coastguard Worker       typename T,
716*9356374aSAndroid Build Coastguard Worker       typename absl::enable_if_t<
717*9356374aSAndroid Build Coastguard Worker           absl::conjunction<
718*9356374aSAndroid Build Coastguard Worker               std::is_same<value_type, typename T::value_type>,
719*9356374aSAndroid Build Coastguard Worker               std::is_same<allocator_type, typename T::allocator_type>,
720*9356374aSAndroid Build Coastguard Worker               std::is_same<typename params_type::is_map_container,
721*9356374aSAndroid Build Coastguard Worker                            typename T::params_type::is_map_container>>::value,
722*9356374aSAndroid Build Coastguard Worker           int> = 0>
merge(btree_container<T> & src)723*9356374aSAndroid Build Coastguard Worker   void merge(btree_container<T> &src) {  // NOLINT
724*9356374aSAndroid Build Coastguard Worker     for (auto src_it = src.begin(), end = src.end(); src_it != end; ++src_it) {
725*9356374aSAndroid Build Coastguard Worker       insert(std::move(params_type::element(src_it.slot())));
726*9356374aSAndroid Build Coastguard Worker     }
727*9356374aSAndroid Build Coastguard Worker     src.clear();
728*9356374aSAndroid Build Coastguard Worker   }
729*9356374aSAndroid Build Coastguard Worker 
730*9356374aSAndroid Build Coastguard Worker   template <
731*9356374aSAndroid Build Coastguard Worker       typename T,
732*9356374aSAndroid Build Coastguard Worker       typename absl::enable_if_t<
733*9356374aSAndroid Build Coastguard Worker           absl::conjunction<
734*9356374aSAndroid Build Coastguard Worker               std::is_same<value_type, typename T::value_type>,
735*9356374aSAndroid Build Coastguard Worker               std::is_same<allocator_type, typename T::allocator_type>,
736*9356374aSAndroid Build Coastguard Worker               std::is_same<typename params_type::is_map_container,
737*9356374aSAndroid Build Coastguard Worker                            typename T::params_type::is_map_container>>::value,
738*9356374aSAndroid Build Coastguard Worker           int> = 0>
merge(btree_container<T> && src)739*9356374aSAndroid Build Coastguard Worker   void merge(btree_container<T> &&src) {
740*9356374aSAndroid Build Coastguard Worker     merge(src);
741*9356374aSAndroid Build Coastguard Worker   }
742*9356374aSAndroid Build Coastguard Worker };
743*9356374aSAndroid Build Coastguard Worker 
744*9356374aSAndroid Build Coastguard Worker // A base class for btree_multimap.
745*9356374aSAndroid Build Coastguard Worker template <typename Tree>
746*9356374aSAndroid Build Coastguard Worker class btree_multimap_container : public btree_multiset_container<Tree> {
747*9356374aSAndroid Build Coastguard Worker   using super_type = btree_multiset_container<Tree>;
748*9356374aSAndroid Build Coastguard Worker   using params_type = typename Tree::params_type;
749*9356374aSAndroid Build Coastguard Worker   friend class BtreeNodePeer;
750*9356374aSAndroid Build Coastguard Worker 
751*9356374aSAndroid Build Coastguard Worker  public:
752*9356374aSAndroid Build Coastguard Worker   using mapped_type = typename params_type::mapped_type;
753*9356374aSAndroid Build Coastguard Worker 
754*9356374aSAndroid Build Coastguard Worker   // Inherit constructors.
755*9356374aSAndroid Build Coastguard Worker   using super_type::super_type;
btree_multimap_container()756*9356374aSAndroid Build Coastguard Worker   btree_multimap_container() {}
757*9356374aSAndroid Build Coastguard Worker };
758*9356374aSAndroid Build Coastguard Worker 
759*9356374aSAndroid Build Coastguard Worker }  // namespace container_internal
760*9356374aSAndroid Build Coastguard Worker ABSL_NAMESPACE_END
761*9356374aSAndroid Build Coastguard Worker }  // namespace absl
762*9356374aSAndroid Build Coastguard Worker 
763*9356374aSAndroid Build Coastguard Worker #endif  // ABSL_CONTAINER_INTERNAL_BTREE_CONTAINER_H_
764