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<params_type::kIsKeyCompareTransparent>::template type< 48 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 equal_range.second - equal_range.first; 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 Construct instead of Transfer because the rebalancing code will 170 // destroy the slot later. 171 auto node = 172 CommonAccess::Construct<node_type>(get_allocator(), position.slot()); 173 erase(position); 174 return node; 175 } extract(const_iterator position)176 node_type extract(const_iterator position) { 177 return extract(iterator(position)); 178 } 179 180 // Utility routines. clear()181 ABSL_ATTRIBUTE_REINITIALIZES void clear() { tree_.clear(); } swap(btree_container & other)182 void swap(btree_container &other) { tree_.swap(other.tree_); } verify()183 void verify() const { tree_.verify(); } 184 185 // Size routines. size()186 size_type size() const { return tree_.size(); } max_size()187 size_type max_size() const { return tree_.max_size(); } empty()188 bool empty() const { return tree_.empty(); } 189 190 friend bool operator==(const btree_container &x, const btree_container &y) { 191 if (x.size() != y.size()) return false; 192 return std::equal(x.begin(), x.end(), y.begin()); 193 } 194 195 friend bool operator!=(const btree_container &x, const btree_container &y) { 196 return !(x == y); 197 } 198 199 friend bool operator<(const btree_container &x, const btree_container &y) { 200 return std::lexicographical_compare(x.begin(), x.end(), y.begin(), y.end()); 201 } 202 203 friend bool operator>(const btree_container &x, const btree_container &y) { 204 return y < x; 205 } 206 207 friend bool operator<=(const btree_container &x, const btree_container &y) { 208 return !(y < x); 209 } 210 211 friend bool operator>=(const btree_container &x, const btree_container &y) { 212 return !(x < y); 213 } 214 215 // The allocator used by the btree. get_allocator()216 allocator_type get_allocator() const { return tree_.get_allocator(); } 217 218 // The key comparator used by the btree. key_comp()219 key_compare key_comp() const { return key_compare(tree_.key_comp()); } value_comp()220 value_compare value_comp() const { return tree_.value_comp(); } 221 222 // Support absl::Hash. 223 template <typename State> AbslHashValue(State h,const btree_container & b)224 friend State AbslHashValue(State h, const btree_container &b) { 225 for (const auto &v : b) { 226 h = State::combine(std::move(h), v); 227 } 228 return State::combine(std::move(h), b.size()); 229 } 230 231 protected: 232 friend struct btree_access; 233 Tree tree_; 234 }; 235 236 // A common base class for btree_set and btree_map. 237 template <typename Tree> 238 class btree_set_container : public btree_container<Tree> { 239 using super_type = btree_container<Tree>; 240 using params_type = typename Tree::params_type; 241 using init_type = typename params_type::init_type; 242 using is_key_compare_to = typename params_type::is_key_compare_to; 243 friend class BtreeNodePeer; 244 245 protected: 246 template <class K> 247 using key_arg = typename super_type::template key_arg<K>; 248 249 public: 250 using key_type = typename Tree::key_type; 251 using value_type = typename Tree::value_type; 252 using size_type = typename Tree::size_type; 253 using key_compare = typename Tree::original_key_compare; 254 using allocator_type = typename Tree::allocator_type; 255 using iterator = typename Tree::iterator; 256 using const_iterator = typename Tree::const_iterator; 257 using node_type = typename super_type::node_type; 258 using insert_return_type = InsertReturnType<iterator, node_type>; 259 260 // Inherit constructors. 261 using super_type::super_type; btree_set_container()262 btree_set_container() {} 263 264 // Range constructors. 265 template <class InputIterator> 266 btree_set_container(InputIterator b, InputIterator e, 267 const key_compare &comp = key_compare(), 268 const allocator_type &alloc = allocator_type()) super_type(comp,alloc)269 : super_type(comp, alloc) { 270 insert(b, e); 271 } 272 template <class InputIterator> btree_set_container(InputIterator b,InputIterator e,const allocator_type & alloc)273 btree_set_container(InputIterator b, InputIterator e, 274 const allocator_type &alloc) 275 : btree_set_container(b, e, key_compare(), alloc) {} 276 277 // Initializer list constructors. 278 btree_set_container(std::initializer_list<init_type> init, 279 const key_compare &comp = key_compare(), 280 const allocator_type &alloc = allocator_type()) 281 : btree_set_container(init.begin(), init.end(), comp, alloc) {} btree_set_container(std::initializer_list<init_type> init,const allocator_type & alloc)282 btree_set_container(std::initializer_list<init_type> init, 283 const allocator_type &alloc) 284 : btree_set_container(init.begin(), init.end(), alloc) {} 285 286 // Insertion routines. insert(const value_type & v)287 std::pair<iterator, bool> insert(const value_type &v) { 288 return this->tree_.insert_unique(params_type::key(v), v); 289 } insert(value_type && v)290 std::pair<iterator, bool> insert(value_type &&v) { 291 return this->tree_.insert_unique(params_type::key(v), std::move(v)); 292 } 293 template <typename... Args> emplace(Args &&...args)294 std::pair<iterator, bool> emplace(Args &&... args) { 295 // Use a node handle to manage a temp slot. 296 auto node = CommonAccess::Construct<node_type>(this->get_allocator(), 297 std::forward<Args>(args)...); 298 auto *slot = CommonAccess::GetSlot(node); 299 return this->tree_.insert_unique(params_type::key(slot), slot); 300 } insert(const_iterator hint,const value_type & v)301 iterator insert(const_iterator hint, const value_type &v) { 302 return this->tree_ 303 .insert_hint_unique(iterator(hint), params_type::key(v), v) 304 .first; 305 } insert(const_iterator hint,value_type && v)306 iterator insert(const_iterator hint, value_type &&v) { 307 return this->tree_ 308 .insert_hint_unique(iterator(hint), params_type::key(v), std::move(v)) 309 .first; 310 } 311 template <typename... Args> emplace_hint(const_iterator hint,Args &&...args)312 iterator emplace_hint(const_iterator hint, Args &&... args) { 313 // Use a node handle to manage a temp slot. 314 auto node = CommonAccess::Construct<node_type>(this->get_allocator(), 315 std::forward<Args>(args)...); 316 auto *slot = CommonAccess::GetSlot(node); 317 return this->tree_ 318 .insert_hint_unique(iterator(hint), params_type::key(slot), slot) 319 .first; 320 } 321 template <typename InputIterator> insert(InputIterator b,InputIterator e)322 void insert(InputIterator b, InputIterator e) { 323 this->tree_.insert_iterator_unique(b, e, 0); 324 } insert(std::initializer_list<init_type> init)325 void insert(std::initializer_list<init_type> init) { 326 this->tree_.insert_iterator_unique(init.begin(), init.end(), 0); 327 } insert(node_type && node)328 insert_return_type insert(node_type &&node) { 329 if (!node) return {this->end(), false, node_type()}; 330 std::pair<iterator, bool> res = 331 this->tree_.insert_unique(params_type::key(CommonAccess::GetSlot(node)), 332 CommonAccess::GetSlot(node)); 333 if (res.second) { 334 CommonAccess::Destroy(&node); 335 return {res.first, true, node_type()}; 336 } else { 337 return {res.first, false, std::move(node)}; 338 } 339 } insert(const_iterator hint,node_type && node)340 iterator insert(const_iterator hint, node_type &&node) { 341 if (!node) return this->end(); 342 std::pair<iterator, bool> res = this->tree_.insert_hint_unique( 343 iterator(hint), params_type::key(CommonAccess::GetSlot(node)), 344 CommonAccess::GetSlot(node)); 345 if (res.second) CommonAccess::Destroy(&node); 346 return res.first; 347 } 348 349 // Node extraction routines. 350 template <typename K = key_type> extract(const key_arg<K> & key)351 node_type extract(const key_arg<K> &key) { 352 const std::pair<iterator, bool> lower_and_equal = 353 this->tree_.lower_bound_equal(key); 354 return lower_and_equal.second ? extract(lower_and_equal.first) 355 : node_type(); 356 } 357 using super_type::extract; 358 359 // Merge routines. 360 // Moves elements from `src` into `this`. If the element already exists in 361 // `this`, it is left unmodified in `src`. 362 template < 363 typename T, 364 typename absl::enable_if_t< 365 absl::conjunction< 366 std::is_same<value_type, typename T::value_type>, 367 std::is_same<allocator_type, typename T::allocator_type>, 368 std::is_same<typename params_type::is_map_container, 369 typename T::params_type::is_map_container>>::value, 370 int> = 0> merge(btree_container<T> & src)371 void merge(btree_container<T> &src) { // NOLINT 372 for (auto src_it = src.begin(); src_it != src.end();) { 373 if (insert(std::move(params_type::element(src_it.slot()))).second) { 374 src_it = src.erase(src_it); 375 } else { 376 ++src_it; 377 } 378 } 379 } 380 381 template < 382 typename T, 383 typename absl::enable_if_t< 384 absl::conjunction< 385 std::is_same<value_type, typename T::value_type>, 386 std::is_same<allocator_type, typename T::allocator_type>, 387 std::is_same<typename params_type::is_map_container, 388 typename T::params_type::is_map_container>>::value, 389 int> = 0> merge(btree_container<T> && src)390 void merge(btree_container<T> &&src) { 391 merge(src); 392 } 393 }; 394 395 // Base class for btree_map. 396 template <typename Tree> 397 class btree_map_container : public btree_set_container<Tree> { 398 using super_type = btree_set_container<Tree>; 399 using params_type = typename Tree::params_type; 400 friend class BtreeNodePeer; 401 402 private: 403 template <class K> 404 using key_arg = typename super_type::template key_arg<K>; 405 406 public: 407 using key_type = typename Tree::key_type; 408 using mapped_type = typename params_type::mapped_type; 409 using value_type = typename Tree::value_type; 410 using key_compare = typename Tree::original_key_compare; 411 using allocator_type = typename Tree::allocator_type; 412 using iterator = typename Tree::iterator; 413 using const_iterator = typename Tree::const_iterator; 414 415 // Inherit constructors. 416 using super_type::super_type; btree_map_container()417 btree_map_container() {} 418 419 // Insertion routines. 420 // Note: the nullptr template arguments and extra `const M&` overloads allow 421 // for supporting bitfield arguments. 422 template <typename K = key_type, class M> insert_or_assign(const key_arg<K> & k,const M & obj)423 std::pair<iterator, bool> insert_or_assign(const key_arg<K> &k, 424 const M &obj) { 425 return insert_or_assign_impl(k, obj); 426 } 427 template <typename K = key_type, class M, K * = nullptr> insert_or_assign(key_arg<K> && k,const M & obj)428 std::pair<iterator, bool> insert_or_assign(key_arg<K> &&k, const M &obj) { 429 return insert_or_assign_impl(std::forward<K>(k), obj); 430 } 431 template <typename K = key_type, class M, M * = nullptr> insert_or_assign(const key_arg<K> & k,M && obj)432 std::pair<iterator, bool> insert_or_assign(const key_arg<K> &k, M &&obj) { 433 return insert_or_assign_impl(k, std::forward<M>(obj)); 434 } 435 template <typename K = key_type, class M, K * = nullptr, M * = nullptr> insert_or_assign(key_arg<K> && k,M && obj)436 std::pair<iterator, bool> insert_or_assign(key_arg<K> &&k, M &&obj) { 437 return insert_or_assign_impl(std::forward<K>(k), std::forward<M>(obj)); 438 } 439 template <typename K = key_type, class M> insert_or_assign(const_iterator hint,const key_arg<K> & k,const M & obj)440 iterator insert_or_assign(const_iterator hint, const key_arg<K> &k, 441 const M &obj) { 442 return insert_or_assign_hint_impl(hint, k, obj); 443 } 444 template <typename K = key_type, class M, K * = nullptr> insert_or_assign(const_iterator hint,key_arg<K> && k,const M & obj)445 iterator insert_or_assign(const_iterator hint, key_arg<K> &&k, const M &obj) { 446 return insert_or_assign_hint_impl(hint, std::forward<K>(k), obj); 447 } 448 template <typename K = key_type, class M, M * = nullptr> insert_or_assign(const_iterator hint,const key_arg<K> & k,M && obj)449 iterator insert_or_assign(const_iterator hint, const key_arg<K> &k, M &&obj) { 450 return insert_or_assign_hint_impl(hint, k, std::forward<M>(obj)); 451 } 452 template <typename K = key_type, class M, K * = nullptr, M * = nullptr> insert_or_assign(const_iterator hint,key_arg<K> && k,M && obj)453 iterator insert_or_assign(const_iterator hint, key_arg<K> &&k, M &&obj) { 454 return insert_or_assign_hint_impl(hint, std::forward<K>(k), 455 std::forward<M>(obj)); 456 } 457 458 template <typename K = key_type, typename... Args, 459 typename absl::enable_if_t< 460 !std::is_convertible<K, const_iterator>::value, int> = 0> try_emplace(const key_arg<K> & k,Args &&...args)461 std::pair<iterator, bool> try_emplace(const key_arg<K> &k, Args &&... args) { 462 return try_emplace_impl(k, std::forward<Args>(args)...); 463 } 464 template <typename K = key_type, typename... Args, 465 typename absl::enable_if_t< 466 !std::is_convertible<K, const_iterator>::value, int> = 0> try_emplace(key_arg<K> && k,Args &&...args)467 std::pair<iterator, bool> try_emplace(key_arg<K> &&k, Args &&... args) { 468 return try_emplace_impl(std::forward<K>(k), std::forward<Args>(args)...); 469 } 470 template <typename K = key_type, typename... Args> try_emplace(const_iterator hint,const key_arg<K> & k,Args &&...args)471 iterator try_emplace(const_iterator hint, const key_arg<K> &k, 472 Args &&... args) { 473 return try_emplace_hint_impl(hint, k, std::forward<Args>(args)...); 474 } 475 template <typename K = key_type, typename... Args> try_emplace(const_iterator hint,key_arg<K> && k,Args &&...args)476 iterator try_emplace(const_iterator hint, key_arg<K> &&k, Args &&... args) { 477 return try_emplace_hint_impl(hint, std::forward<K>(k), 478 std::forward<Args>(args)...); 479 } 480 481 template <typename K = key_type> 482 mapped_type &operator[](const key_arg<K> &k) { 483 return try_emplace(k).first->second; 484 } 485 template <typename K = key_type> 486 mapped_type &operator[](key_arg<K> &&k) { 487 return try_emplace(std::forward<K>(k)).first->second; 488 } 489 490 template <typename K = key_type> at(const key_arg<K> & key)491 mapped_type &at(const key_arg<K> &key) { 492 auto it = this->find(key); 493 if (it == this->end()) 494 base_internal::ThrowStdOutOfRange("absl::btree_map::at"); 495 return it->second; 496 } 497 template <typename K = key_type> at(const key_arg<K> & key)498 const mapped_type &at(const key_arg<K> &key) const { 499 auto it = this->find(key); 500 if (it == this->end()) 501 base_internal::ThrowStdOutOfRange("absl::btree_map::at"); 502 return it->second; 503 } 504 505 private: 506 // Note: when we call `std::forward<M>(obj)` twice, it's safe because 507 // insert_unique/insert_hint_unique are guaranteed to not consume `obj` when 508 // `ret.second` is false. 509 template <class K, class M> insert_or_assign_impl(K && k,M && obj)510 std::pair<iterator, bool> insert_or_assign_impl(K &&k, M &&obj) { 511 const std::pair<iterator, bool> ret = 512 this->tree_.insert_unique(k, std::forward<K>(k), std::forward<M>(obj)); 513 if (!ret.second) ret.first->second = std::forward<M>(obj); 514 return ret; 515 } 516 template <class K, class M> insert_or_assign_hint_impl(const_iterator hint,K && k,M && obj)517 iterator insert_or_assign_hint_impl(const_iterator hint, K &&k, M &&obj) { 518 const std::pair<iterator, bool> ret = this->tree_.insert_hint_unique( 519 iterator(hint), k, std::forward<K>(k), std::forward<M>(obj)); 520 if (!ret.second) ret.first->second = std::forward<M>(obj); 521 return ret.first; 522 } 523 524 template <class K, class... Args> try_emplace_impl(K && k,Args &&...args)525 std::pair<iterator, bool> try_emplace_impl(K &&k, Args &&... args) { 526 return this->tree_.insert_unique( 527 k, std::piecewise_construct, std::forward_as_tuple(std::forward<K>(k)), 528 std::forward_as_tuple(std::forward<Args>(args)...)); 529 } 530 template <class K, class... Args> try_emplace_hint_impl(const_iterator hint,K && k,Args &&...args)531 iterator try_emplace_hint_impl(const_iterator hint, K &&k, Args &&... args) { 532 return this->tree_ 533 .insert_hint_unique(iterator(hint), k, std::piecewise_construct, 534 std::forward_as_tuple(std::forward<K>(k)), 535 std::forward_as_tuple(std::forward<Args>(args)...)) 536 .first; 537 } 538 }; 539 540 // A common base class for btree_multiset and btree_multimap. 541 template <typename Tree> 542 class btree_multiset_container : public btree_container<Tree> { 543 using super_type = btree_container<Tree>; 544 using params_type = typename Tree::params_type; 545 using init_type = typename params_type::init_type; 546 using is_key_compare_to = typename params_type::is_key_compare_to; 547 friend class BtreeNodePeer; 548 549 template <class K> 550 using key_arg = typename super_type::template key_arg<K>; 551 552 public: 553 using key_type = typename Tree::key_type; 554 using value_type = typename Tree::value_type; 555 using size_type = typename Tree::size_type; 556 using key_compare = typename Tree::original_key_compare; 557 using allocator_type = typename Tree::allocator_type; 558 using iterator = typename Tree::iterator; 559 using const_iterator = typename Tree::const_iterator; 560 using node_type = typename super_type::node_type; 561 562 // Inherit constructors. 563 using super_type::super_type; btree_multiset_container()564 btree_multiset_container() {} 565 566 // Range constructors. 567 template <class InputIterator> 568 btree_multiset_container(InputIterator b, InputIterator e, 569 const key_compare &comp = key_compare(), 570 const allocator_type &alloc = allocator_type()) super_type(comp,alloc)571 : super_type(comp, alloc) { 572 insert(b, e); 573 } 574 template <class InputIterator> btree_multiset_container(InputIterator b,InputIterator e,const allocator_type & alloc)575 btree_multiset_container(InputIterator b, InputIterator e, 576 const allocator_type &alloc) 577 : btree_multiset_container(b, e, key_compare(), alloc) {} 578 579 // Initializer list constructors. 580 btree_multiset_container(std::initializer_list<init_type> init, 581 const key_compare &comp = key_compare(), 582 const allocator_type &alloc = allocator_type()) 583 : btree_multiset_container(init.begin(), init.end(), comp, alloc) {} btree_multiset_container(std::initializer_list<init_type> init,const allocator_type & alloc)584 btree_multiset_container(std::initializer_list<init_type> init, 585 const allocator_type &alloc) 586 : btree_multiset_container(init.begin(), init.end(), alloc) {} 587 588 // Insertion routines. insert(const value_type & v)589 iterator insert(const value_type &v) { return this->tree_.insert_multi(v); } insert(value_type && v)590 iterator insert(value_type &&v) { 591 return this->tree_.insert_multi(std::move(v)); 592 } insert(const_iterator hint,const value_type & v)593 iterator insert(const_iterator hint, const value_type &v) { 594 return this->tree_.insert_hint_multi(iterator(hint), v); 595 } insert(const_iterator hint,value_type && v)596 iterator insert(const_iterator hint, value_type &&v) { 597 return this->tree_.insert_hint_multi(iterator(hint), std::move(v)); 598 } 599 template <typename InputIterator> insert(InputIterator b,InputIterator e)600 void insert(InputIterator b, InputIterator e) { 601 this->tree_.insert_iterator_multi(b, e); 602 } insert(std::initializer_list<init_type> init)603 void insert(std::initializer_list<init_type> init) { 604 this->tree_.insert_iterator_multi(init.begin(), init.end()); 605 } 606 template <typename... Args> emplace(Args &&...args)607 iterator emplace(Args &&... args) { 608 // Use a node handle to manage a temp slot. 609 auto node = CommonAccess::Construct<node_type>(this->get_allocator(), 610 std::forward<Args>(args)...); 611 return this->tree_.insert_multi(CommonAccess::GetSlot(node)); 612 } 613 template <typename... Args> emplace_hint(const_iterator hint,Args &&...args)614 iterator emplace_hint(const_iterator hint, Args &&... args) { 615 // Use a node handle to manage a temp slot. 616 auto node = CommonAccess::Construct<node_type>(this->get_allocator(), 617 std::forward<Args>(args)...); 618 return this->tree_.insert_hint_multi(iterator(hint), 619 CommonAccess::GetSlot(node)); 620 } insert(node_type && node)621 iterator insert(node_type &&node) { 622 if (!node) return this->end(); 623 iterator res = 624 this->tree_.insert_multi(params_type::key(CommonAccess::GetSlot(node)), 625 CommonAccess::GetSlot(node)); 626 CommonAccess::Destroy(&node); 627 return res; 628 } insert(const_iterator hint,node_type && node)629 iterator insert(const_iterator hint, node_type &&node) { 630 if (!node) return this->end(); 631 iterator res = this->tree_.insert_hint_multi( 632 iterator(hint), 633 std::move(params_type::element(CommonAccess::GetSlot(node)))); 634 CommonAccess::Destroy(&node); 635 return res; 636 } 637 638 // Node extraction routines. 639 template <typename K = key_type> extract(const key_arg<K> & key)640 node_type extract(const key_arg<K> &key) { 641 const std::pair<iterator, bool> lower_and_equal = 642 this->tree_.lower_bound_equal(key); 643 return lower_and_equal.second ? extract(lower_and_equal.first) 644 : node_type(); 645 } 646 using super_type::extract; 647 648 // Merge routines. 649 // Moves all elements from `src` into `this`. 650 template < 651 typename T, 652 typename absl::enable_if_t< 653 absl::conjunction< 654 std::is_same<value_type, typename T::value_type>, 655 std::is_same<allocator_type, typename T::allocator_type>, 656 std::is_same<typename params_type::is_map_container, 657 typename T::params_type::is_map_container>>::value, 658 int> = 0> merge(btree_container<T> & src)659 void merge(btree_container<T> &src) { // NOLINT 660 for (auto src_it = src.begin(), end = src.end(); src_it != end; ++src_it) { 661 insert(std::move(params_type::element(src_it.slot()))); 662 } 663 src.clear(); 664 } 665 666 template < 667 typename T, 668 typename absl::enable_if_t< 669 absl::conjunction< 670 std::is_same<value_type, typename T::value_type>, 671 std::is_same<allocator_type, typename T::allocator_type>, 672 std::is_same<typename params_type::is_map_container, 673 typename T::params_type::is_map_container>>::value, 674 int> = 0> merge(btree_container<T> && src)675 void merge(btree_container<T> &&src) { 676 merge(src); 677 } 678 }; 679 680 // A base class for btree_multimap. 681 template <typename Tree> 682 class btree_multimap_container : public btree_multiset_container<Tree> { 683 using super_type = btree_multiset_container<Tree>; 684 using params_type = typename Tree::params_type; 685 friend class BtreeNodePeer; 686 687 public: 688 using mapped_type = typename params_type::mapped_type; 689 690 // Inherit constructors. 691 using super_type::super_type; btree_multimap_container()692 btree_multimap_container() {} 693 }; 694 695 } // namespace container_internal 696 ABSL_NAMESPACE_END 697 } // namespace absl 698 699 #endif // ABSL_CONTAINER_INTERNAL_BTREE_CONTAINER_H_ 700