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