You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

btree_container.h 27 kB

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