• 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/memory/memory.h"
27 #include "absl/meta/type_traits.h"
28 
29 namespace absl {
30 ABSL_NAMESPACE_BEGIN
31 namespace container_internal {
32 
33 // A common base class for btree_set, btree_map, btree_multiset, and
34 // btree_multimap.
35 template <typename Tree>
36 class btree_container {
37   using params_type = typename Tree::params_type;
38 
39  protected:
40   // Alias used for heterogeneous lookup functions.
41   // `key_arg<K>` evaluates to `K` when the functors are transparent and to
42   // `key_type` otherwise. It permits template argument deduction on `K` for the
43   // transparent case.
44   template <class K>
45   using key_arg =
46       typename KeyArg<IsTransparent<typename Tree::key_compare>::value>::
47           template type<K, typename Tree::key_type>;
48 
49  public:
50   using key_type = typename Tree::key_type;
51   using value_type = typename Tree::value_type;
52   using size_type = typename Tree::size_type;
53   using difference_type = typename Tree::difference_type;
54   using key_compare = typename Tree::key_compare;
55   using value_compare = typename Tree::value_compare;
56   using allocator_type = typename Tree::allocator_type;
57   using reference = typename Tree::reference;
58   using const_reference = typename Tree::const_reference;
59   using pointer = typename Tree::pointer;
60   using const_pointer = typename Tree::const_pointer;
61   using iterator = typename Tree::iterator;
62   using const_iterator = typename Tree::const_iterator;
63   using reverse_iterator = typename Tree::reverse_iterator;
64   using const_reverse_iterator = typename Tree::const_reverse_iterator;
65   using node_type = typename Tree::node_handle_type;
66 
67   // Constructors/assignments.
btree_container()68   btree_container() : tree_(key_compare(), allocator_type()) {}
69   explicit btree_container(const key_compare &comp,
70                            const allocator_type &alloc = allocator_type())
tree_(comp,alloc)71       : tree_(comp, alloc) {}
btree_container(const allocator_type & alloc)72   explicit btree_container(const allocator_type &alloc)
73       : tree_(key_compare(), alloc) {}
74 
btree_container(const btree_container & other)75   btree_container(const btree_container &other)
76       : btree_container(other, absl::allocator_traits<allocator_type>::
77                                    select_on_container_copy_construction(
78                                        other.get_allocator())) {}
btree_container(const btree_container & other,const allocator_type & alloc)79   btree_container(const btree_container &other, const allocator_type &alloc)
80       : tree_(other.tree_, alloc) {}
81 
82   btree_container(btree_container &&other) noexcept(
83       std::is_nothrow_move_constructible<Tree>::value) = default;
btree_container(btree_container && other,const allocator_type & alloc)84   btree_container(btree_container &&other, const allocator_type &alloc)
85       : tree_(std::move(other.tree_), alloc) {}
86 
87   btree_container &operator=(const btree_container &other) = default;
88   btree_container &operator=(btree_container &&other) noexcept(
89       std::is_nothrow_move_assignable<Tree>::value) = default;
90 
91   // Iterator routines.
begin()92   iterator begin() { return tree_.begin(); }
begin()93   const_iterator begin() const { return tree_.begin(); }
cbegin()94   const_iterator cbegin() const { return tree_.begin(); }
end()95   iterator end() { return tree_.end(); }
end()96   const_iterator end() const { return tree_.end(); }
cend()97   const_iterator cend() const { return tree_.end(); }
rbegin()98   reverse_iterator rbegin() { return tree_.rbegin(); }
rbegin()99   const_reverse_iterator rbegin() const { return tree_.rbegin(); }
crbegin()100   const_reverse_iterator crbegin() const { return tree_.rbegin(); }
rend()101   reverse_iterator rend() { return tree_.rend(); }
rend()102   const_reverse_iterator rend() const { return tree_.rend(); }
crend()103   const_reverse_iterator crend() const { return tree_.rend(); }
104 
105   // Lookup routines.
106   template <typename K = key_type>
count(const key_arg<K> & key)107   size_type count(const key_arg<K> &key) const {
108     auto equal_range = this->equal_range(key);
109     return std::distance(equal_range.first, equal_range.second);
110   }
111   template <typename K = key_type>
find(const key_arg<K> & key)112   iterator find(const key_arg<K> &key) {
113     return tree_.find(key);
114   }
115   template <typename K = key_type>
find(const key_arg<K> & key)116   const_iterator find(const key_arg<K> &key) const {
117     return tree_.find(key);
118   }
119   template <typename K = key_type>
contains(const key_arg<K> & key)120   bool contains(const key_arg<K> &key) const {
121     return find(key) != end();
122   }
123   template <typename K = key_type>
lower_bound(const key_arg<K> & key)124   iterator lower_bound(const key_arg<K> &key) {
125     return tree_.lower_bound(key);
126   }
127   template <typename K = key_type>
lower_bound(const key_arg<K> & key)128   const_iterator lower_bound(const key_arg<K> &key) const {
129     return tree_.lower_bound(key);
130   }
131   template <typename K = key_type>
upper_bound(const key_arg<K> & key)132   iterator upper_bound(const key_arg<K> &key) {
133     return tree_.upper_bound(key);
134   }
135   template <typename K = key_type>
upper_bound(const key_arg<K> & key)136   const_iterator upper_bound(const key_arg<K> &key) const {
137     return tree_.upper_bound(key);
138   }
139   template <typename K = key_type>
equal_range(const key_arg<K> & key)140   std::pair<iterator, iterator> equal_range(const key_arg<K> &key) {
141     return tree_.equal_range(key);
142   }
143   template <typename K = key_type>
equal_range(const key_arg<K> & key)144   std::pair<const_iterator, const_iterator> equal_range(
145       const key_arg<K> &key) const {
146     return tree_.equal_range(key);
147   }
148 
149   // Deletion routines. Note that there is also a deletion routine that is
150   // specific to btree_set_container/btree_multiset_container.
151 
152   // Erase the specified iterator from the btree. The iterator must be valid
153   // (i.e. not equal to end()).  Return an iterator pointing to the node after
154   // the one that was erased (or end() if none exists).
erase(const_iterator iter)155   iterator erase(const_iterator iter) { return tree_.erase(iterator(iter)); }
erase(iterator iter)156   iterator erase(iterator iter) { return tree_.erase(iter); }
erase(const_iterator first,const_iterator last)157   iterator erase(const_iterator first, const_iterator last) {
158     return tree_.erase_range(iterator(first), iterator(last)).second;
159   }
160   template <typename K = key_type>
erase(const key_arg<K> & key)161   size_type erase(const key_arg<K> &key) {
162     auto equal_range = this->equal_range(key);
163     return tree_.erase_range(equal_range.first, equal_range.second).first;
164   }
165 
166   // Extract routines.
extract(iterator position)167   node_type extract(iterator position) {
168     // Use Move instead of Transfer, because the rebalancing code expects to
169     // have a valid object to scribble metadata bits on top of.
170     auto node = CommonAccess::Move<node_type>(get_allocator(), position.slot());
171     erase(position);
172     return node;
173   }
extract(const_iterator position)174   node_type extract(const_iterator position) {
175     return extract(iterator(position));
176   }
177 
178   // Utility routines.
clear()179   void clear() { tree_.clear(); }
swap(btree_container & other)180   void swap(btree_container &other) { tree_.swap(other.tree_); }
verify()181   void verify() const { tree_.verify(); }
182 
183   // Size routines.
size()184   size_type size() const { return tree_.size(); }
max_size()185   size_type max_size() const { return tree_.max_size(); }
empty()186   bool empty() const { return tree_.empty(); }
187 
188   friend bool operator==(const btree_container &x, const btree_container &y) {
189     if (x.size() != y.size()) return false;
190     return std::equal(x.begin(), x.end(), y.begin());
191   }
192 
193   friend bool operator!=(const btree_container &x, const btree_container &y) {
194     return !(x == y);
195   }
196 
197   friend bool operator<(const btree_container &x, const btree_container &y) {
198     return std::lexicographical_compare(x.begin(), x.end(), y.begin(), y.end());
199   }
200 
201   friend bool operator>(const btree_container &x, const btree_container &y) {
202     return y < x;
203   }
204 
205   friend bool operator<=(const btree_container &x, const btree_container &y) {
206     return !(y < x);
207   }
208 
209   friend bool operator>=(const btree_container &x, const btree_container &y) {
210     return !(x < y);
211   }
212 
213   // The allocator used by the btree.
get_allocator()214   allocator_type get_allocator() const { return tree_.get_allocator(); }
215 
216   // The key comparator used by the btree.
key_comp()217   key_compare key_comp() const { return tree_.key_comp(); }
value_comp()218   value_compare value_comp() const { return tree_.value_comp(); }
219 
220   // Support absl::Hash.
221   template <typename State>
AbslHashValue(State h,const btree_container & b)222   friend State AbslHashValue(State h, const btree_container &b) {
223     for (const auto &v : b) {
224       h = State::combine(std::move(h), v);
225     }
226     return State::combine(std::move(h), b.size());
227   }
228 
229  protected:
230   Tree tree_;
231 };
232 
233 // A common base class for btree_set and btree_map.
234 template <typename Tree>
235 class btree_set_container : public btree_container<Tree> {
236   using super_type = btree_container<Tree>;
237   using params_type = typename Tree::params_type;
238   using init_type = typename params_type::init_type;
239   using is_key_compare_to = typename params_type::is_key_compare_to;
240   friend class BtreeNodePeer;
241 
242  protected:
243   template <class K>
244   using key_arg = typename super_type::template key_arg<K>;
245 
246  public:
247   using key_type = typename Tree::key_type;
248   using value_type = typename Tree::value_type;
249   using size_type = typename Tree::size_type;
250   using key_compare = typename Tree::key_compare;
251   using allocator_type = typename Tree::allocator_type;
252   using iterator = typename Tree::iterator;
253   using const_iterator = typename Tree::const_iterator;
254   using node_type = typename super_type::node_type;
255   using insert_return_type = InsertReturnType<iterator, node_type>;
256 
257   // Inherit constructors.
258   using super_type::super_type;
btree_set_container()259   btree_set_container() {}
260 
261   // Range constructors.
262   template <class InputIterator>
263   btree_set_container(InputIterator b, InputIterator e,
264                       const key_compare &comp = key_compare(),
265                       const allocator_type &alloc = allocator_type())
super_type(comp,alloc)266       : super_type(comp, alloc) {
267     insert(b, e);
268   }
269   template <class InputIterator>
btree_set_container(InputIterator b,InputIterator e,const allocator_type & alloc)270   btree_set_container(InputIterator b, InputIterator e,
271                       const allocator_type &alloc)
272       : btree_set_container(b, e, key_compare(), alloc) {}
273 
274   // Initializer list constructors.
275   btree_set_container(std::initializer_list<init_type> init,
276                       const key_compare &comp = key_compare(),
277                       const allocator_type &alloc = allocator_type())
278       : btree_set_container(init.begin(), init.end(), comp, alloc) {}
btree_set_container(std::initializer_list<init_type> init,const allocator_type & alloc)279   btree_set_container(std::initializer_list<init_type> init,
280                       const allocator_type &alloc)
281       : btree_set_container(init.begin(), init.end(), alloc) {}
282 
283   // Insertion routines.
insert(const value_type & v)284   std::pair<iterator, bool> insert(const value_type &v) {
285     return this->tree_.insert_unique(params_type::key(v), v);
286   }
insert(value_type && v)287   std::pair<iterator, bool> insert(value_type &&v) {
288     return this->tree_.insert_unique(params_type::key(v), std::move(v));
289   }
290   template <typename... Args>
emplace(Args &&...args)291   std::pair<iterator, bool> emplace(Args &&... args) {
292     init_type v(std::forward<Args>(args)...);
293     return this->tree_.insert_unique(params_type::key(v), std::move(v));
294   }
insert(const_iterator hint,const value_type & v)295   iterator insert(const_iterator hint, const value_type &v) {
296     return this->tree_
297         .insert_hint_unique(iterator(hint), params_type::key(v), v)
298         .first;
299   }
insert(const_iterator hint,value_type && v)300   iterator insert(const_iterator hint, value_type &&v) {
301     return this->tree_
302         .insert_hint_unique(iterator(hint), params_type::key(v), std::move(v))
303         .first;
304   }
305   template <typename... Args>
emplace_hint(const_iterator hint,Args &&...args)306   iterator emplace_hint(const_iterator hint, Args &&... args) {
307     init_type v(std::forward<Args>(args)...);
308     return this->tree_
309         .insert_hint_unique(iterator(hint), params_type::key(v), std::move(v))
310         .first;
311   }
312   template <typename InputIterator>
insert(InputIterator b,InputIterator e)313   void insert(InputIterator b, InputIterator e) {
314     this->tree_.insert_iterator_unique(b, e, 0);
315   }
insert(std::initializer_list<init_type> init)316   void insert(std::initializer_list<init_type> init) {
317     this->tree_.insert_iterator_unique(init.begin(), init.end(), 0);
318   }
insert(node_type && node)319   insert_return_type insert(node_type &&node) {
320     if (!node) return {this->end(), false, node_type()};
321     std::pair<iterator, bool> res =
322         this->tree_.insert_unique(params_type::key(CommonAccess::GetSlot(node)),
323                                   CommonAccess::GetSlot(node));
324     if (res.second) {
325       CommonAccess::Destroy(&node);
326       return {res.first, true, node_type()};
327     } else {
328       return {res.first, false, std::move(node)};
329     }
330   }
insert(const_iterator hint,node_type && node)331   iterator insert(const_iterator hint, node_type &&node) {
332     if (!node) return this->end();
333     std::pair<iterator, bool> res = this->tree_.insert_hint_unique(
334         iterator(hint), params_type::key(CommonAccess::GetSlot(node)),
335         CommonAccess::GetSlot(node));
336     if (res.second) CommonAccess::Destroy(&node);
337     return res.first;
338   }
339 
340   // Node extraction routines.
341   // TODO(ezb): when the comparator is heterogeneous and has different
342   // equivalence classes for different lookup types, we should extract the first
343   // equivalent value if there are multiple.
344   template <typename K = key_type>
extract(const key_arg<K> & key)345   node_type extract(const key_arg<K> &key) {
346     auto it = this->find(key);
347     return it == this->end() ? node_type() : extract(it);
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::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::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   // TODO(ezb): we are supposed to extract the first equivalent key if there are
625   // multiple, but this isn't guaranteed to extract the first one.
626   template <typename K = key_type>
extract(const key_arg<K> & key)627   node_type extract(const key_arg<K> &key) {
628     auto it = this->find(key);
629     return it == this->end() ? node_type() : extract(it);
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