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