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