btree_container.h 29 KB

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