btree_container.h 26 KB

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