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_map.h 36 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869
  1. // Copyright 2018 The Abseil Authors.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // https://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. //
  15. // -----------------------------------------------------------------------------
  16. // File: btree_map.h
  17. // -----------------------------------------------------------------------------
  18. //
  19. // This header file defines B-tree maps: sorted associative containers mapping
  20. // keys to values.
  21. //
  22. // * `absl::btree_map<>`
  23. // * `absl::btree_multimap<>`
  24. //
  25. // These B-tree types are similar to the corresponding types in the STL
  26. // (`std::map` and `std::multimap`) and generally conform to the STL interfaces
  27. // of those types. However, because they are implemented using B-trees, they
  28. // are more efficient in most situations.
  29. //
  30. // Unlike `std::map` and `std::multimap`, which are commonly implemented using
  31. // red-black tree nodes, B-tree maps use more generic B-tree nodes able to hold
  32. // multiple values per node. Holding multiple values per node often makes
  33. // B-tree maps perform better than their `std::map` counterparts, because
  34. // multiple entries can be checked within the same cache hit.
  35. //
  36. // However, these types should not be considered drop-in replacements for
  37. // `std::map` and `std::multimap` as there are some API differences, which are
  38. // noted in this header file. The most consequential differences with respect to
  39. // migrating to b-tree from the STL types are listed in the next paragraph.
  40. // Other API differences are minor.
  41. //
  42. // Importantly, insertions and deletions may invalidate outstanding iterators,
  43. // pointers, and references to elements. Such invalidations are typically only
  44. // an issue if insertion and deletion operations are interleaved with the use of
  45. // more than one iterator, pointer, or reference simultaneously. For this
  46. // reason, `insert()` and `erase()` return a valid iterator at the current
  47. // position. Another important difference is that key-types must be
  48. // copy-constructible.
  49. #ifndef ABSL_CONTAINER_BTREE_MAP_H_
  50. #define ABSL_CONTAINER_BTREE_MAP_H_
  51. #include "absl/container/internal/btree.h" // IWYU pragma: export
  52. #include "absl/container/internal/btree_container.h" // IWYU pragma: export
  53. namespace absl
  54. {
  55. ABSL_NAMESPACE_BEGIN
  56. namespace container_internal
  57. {
  58. template<typename Key, typename Data, typename Compare, typename Alloc, int TargetNodeSize, bool IsMulti>
  59. struct map_params;
  60. } // namespace container_internal
  61. // absl::btree_map<>
  62. //
  63. // An `absl::btree_map<K, V>` is an ordered associative container of
  64. // unique keys and associated values designed to be a more efficient replacement
  65. // for `std::map` (in most cases).
  66. //
  67. // Keys are sorted using an (optional) comparison function, which defaults to
  68. // `std::less<K>`.
  69. //
  70. // An `absl::btree_map<K, V>` uses a default allocator of
  71. // `std::allocator<std::pair<const K, V>>` to allocate (and deallocate)
  72. // nodes, and construct and destruct values within those nodes. You may
  73. // instead specify a custom allocator `A` (which in turn requires specifying a
  74. // custom comparator `C`) as in `absl::btree_map<K, V, C, A>`.
  75. //
  76. template<typename Key, typename Value, typename Compare = std::less<Key>, typename Alloc = std::allocator<std::pair<const Key, Value>>>
  77. class btree_map : public container_internal::btree_map_container<container_internal::btree<container_internal::map_params<Key, Value, Compare, Alloc, /*TargetNodeSize=*/256,
  78. /*IsMulti=*/false>>>
  79. {
  80. using Base = typename btree_map::btree_map_container;
  81. public:
  82. // Constructors and Assignment Operators
  83. //
  84. // A `btree_map` supports the same overload set as `std::map`
  85. // for construction and assignment:
  86. //
  87. // * Default constructor
  88. //
  89. // absl::btree_map<int, std::string> map1;
  90. //
  91. // * Initializer List constructor
  92. //
  93. // absl::btree_map<int, std::string> map2 =
  94. // {{1, "huey"}, {2, "dewey"}, {3, "louie"},};
  95. //
  96. // * Copy constructor
  97. //
  98. // absl::btree_map<int, std::string> map3(map2);
  99. //
  100. // * Copy assignment operator
  101. //
  102. // absl::btree_map<int, std::string> map4;
  103. // map4 = map3;
  104. //
  105. // * Move constructor
  106. //
  107. // // Move is guaranteed efficient
  108. // absl::btree_map<int, std::string> map5(std::move(map4));
  109. //
  110. // * Move assignment operator
  111. //
  112. // // May be efficient if allocators are compatible
  113. // absl::btree_map<int, std::string> map6;
  114. // map6 = std::move(map5);
  115. //
  116. // * Range constructor
  117. //
  118. // std::vector<std::pair<int, std::string>> v = {{1, "a"}, {2, "b"}};
  119. // absl::btree_map<int, std::string> map7(v.begin(), v.end());
  120. btree_map()
  121. {
  122. }
  123. using Base::Base;
  124. // btree_map::begin()
  125. //
  126. // Returns an iterator to the beginning of the `btree_map`.
  127. using Base::begin;
  128. // btree_map::cbegin()
  129. //
  130. // Returns a const iterator to the beginning of the `btree_map`.
  131. using Base::cbegin;
  132. // btree_map::end()
  133. //
  134. // Returns an iterator to the end of the `btree_map`.
  135. using Base::end;
  136. // btree_map::cend()
  137. //
  138. // Returns a const iterator to the end of the `btree_map`.
  139. using Base::cend;
  140. // btree_map::empty()
  141. //
  142. // Returns whether or not the `btree_map` is empty.
  143. using Base::empty;
  144. // btree_map::max_size()
  145. //
  146. // Returns the largest theoretical possible number of elements within a
  147. // `btree_map` under current memory constraints. This value can be thought
  148. // of as the largest value of `std::distance(begin(), end())` for a
  149. // `btree_map<Key, T>`.
  150. using Base::max_size;
  151. // btree_map::size()
  152. //
  153. // Returns the number of elements currently within the `btree_map`.
  154. using Base::size;
  155. // btree_map::clear()
  156. //
  157. // Removes all elements from the `btree_map`. Invalidates any references,
  158. // pointers, or iterators referring to contained elements.
  159. using Base::clear;
  160. // btree_map::erase()
  161. //
  162. // Erases elements within the `btree_map`. If an erase occurs, any references,
  163. // pointers, or iterators are invalidated.
  164. // Overloads are listed below.
  165. //
  166. // iterator erase(iterator position):
  167. // iterator erase(const_iterator position):
  168. //
  169. // Erases the element at `position` of the `btree_map`, returning
  170. // the iterator pointing to the element after the one that was erased
  171. // (or end() if none exists).
  172. //
  173. // iterator erase(const_iterator first, const_iterator last):
  174. //
  175. // Erases the elements in the open interval [`first`, `last`), returning
  176. // the iterator pointing to the element after the interval that was erased
  177. // (or end() if none exists).
  178. //
  179. // template <typename K> size_type erase(const K& key):
  180. //
  181. // Erases the element with the matching key, if it exists, returning the
  182. // number of elements erased (0 or 1).
  183. using Base::erase;
  184. // btree_map::insert()
  185. //
  186. // Inserts an element of the specified value into the `btree_map`,
  187. // returning an iterator pointing to the newly inserted element, provided that
  188. // an element with the given key does not already exist. If an insertion
  189. // occurs, any references, pointers, or iterators are invalidated.
  190. // Overloads are listed below.
  191. //
  192. // std::pair<iterator,bool> insert(const value_type& value):
  193. //
  194. // Inserts a value into the `btree_map`. Returns a pair consisting of an
  195. // iterator to the inserted element (or to the element that prevented the
  196. // insertion) and a bool denoting whether the insertion took place.
  197. //
  198. // std::pair<iterator,bool> insert(value_type&& value):
  199. //
  200. // Inserts a moveable value into the `btree_map`. Returns a pair
  201. // consisting of an iterator to the inserted element (or to the element that
  202. // prevented the insertion) and a bool denoting whether the insertion took
  203. // place.
  204. //
  205. // iterator insert(const_iterator hint, const value_type& value):
  206. // iterator insert(const_iterator hint, value_type&& value):
  207. //
  208. // Inserts a value, using the position of `hint` as a non-binding suggestion
  209. // for where to begin the insertion search. Returns an iterator to the
  210. // inserted element, or to the existing element that prevented the
  211. // insertion.
  212. //
  213. // void insert(InputIterator first, InputIterator last):
  214. //
  215. // Inserts a range of values [`first`, `last`).
  216. //
  217. // void insert(std::initializer_list<init_type> ilist):
  218. //
  219. // Inserts the elements within the initializer list `ilist`.
  220. using Base::insert;
  221. // btree_map::insert_or_assign()
  222. //
  223. // Inserts an element of the specified value into the `btree_map` provided
  224. // that a value with the given key does not already exist, or replaces the
  225. // corresponding mapped type with the forwarded `obj` argument if a key for
  226. // that value already exists, returning an iterator pointing to the newly
  227. // inserted element. Overloads are listed below.
  228. //
  229. // pair<iterator, bool> insert_or_assign(const key_type& k, M&& obj):
  230. // pair<iterator, bool> insert_or_assign(key_type&& k, M&& obj):
  231. //
  232. // Inserts/Assigns (or moves) the element of the specified key into the
  233. // `btree_map`. If the returned bool is true, insertion took place, and if
  234. // it's false, assignment took place.
  235. //
  236. // iterator insert_or_assign(const_iterator hint,
  237. // const key_type& k, M&& obj):
  238. // iterator insert_or_assign(const_iterator hint, key_type&& k, M&& obj):
  239. //
  240. // Inserts/Assigns (or moves) the element of the specified key into the
  241. // `btree_map` using the position of `hint` as a non-binding suggestion
  242. // for where to begin the insertion search.
  243. using Base::insert_or_assign;
  244. // btree_map::emplace()
  245. //
  246. // Inserts an element of the specified value by constructing it in-place
  247. // within the `btree_map`, provided that no element with the given key
  248. // already exists.
  249. //
  250. // The element may be constructed even if there already is an element with the
  251. // key in the container, in which case the newly constructed element will be
  252. // destroyed immediately. Prefer `try_emplace()` unless your key is not
  253. // copyable or moveable.
  254. //
  255. // If an insertion occurs, any references, pointers, or iterators are
  256. // invalidated.
  257. using Base::emplace;
  258. // btree_map::emplace_hint()
  259. //
  260. // Inserts an element of the specified value by constructing it in-place
  261. // within the `btree_map`, using the position of `hint` as a non-binding
  262. // suggestion for where to begin the insertion search, and only inserts
  263. // provided that no element with the given key already exists.
  264. //
  265. // The element may be constructed even if there already is an element with the
  266. // key in the container, in which case the newly constructed element will be
  267. // destroyed immediately. Prefer `try_emplace()` unless your key is not
  268. // copyable or moveable.
  269. //
  270. // If an insertion occurs, any references, pointers, or iterators are
  271. // invalidated.
  272. using Base::emplace_hint;
  273. // btree_map::try_emplace()
  274. //
  275. // Inserts an element of the specified value by constructing it in-place
  276. // within the `btree_map`, provided that no element with the given key
  277. // already exists. Unlike `emplace()`, if an element with the given key
  278. // already exists, we guarantee that no element is constructed.
  279. //
  280. // If an insertion occurs, any references, pointers, or iterators are
  281. // invalidated.
  282. //
  283. // Overloads are listed below.
  284. //
  285. // std::pair<iterator, bool> try_emplace(const key_type& k, Args&&... args):
  286. // std::pair<iterator, bool> try_emplace(key_type&& k, Args&&... args):
  287. //
  288. // Inserts (via copy or move) the element of the specified key into the
  289. // `btree_map`.
  290. //
  291. // iterator try_emplace(const_iterator hint,
  292. // const key_type& k, Args&&... args):
  293. // iterator try_emplace(const_iterator hint, key_type&& k, Args&&... args):
  294. //
  295. // Inserts (via copy or move) the element of the specified key into the
  296. // `btree_map` using the position of `hint` as a non-binding suggestion
  297. // for where to begin the insertion search.
  298. using Base::try_emplace;
  299. // btree_map::extract()
  300. //
  301. // Extracts the indicated element, erasing it in the process, and returns it
  302. // as a C++17-compatible node handle. Overloads are listed below.
  303. //
  304. // node_type extract(const_iterator position):
  305. //
  306. // Extracts the element at the indicated position and returns a node handle
  307. // owning that extracted data.
  308. //
  309. // template <typename K> node_type extract(const K& k):
  310. //
  311. // Extracts the element with the key matching the passed key value and
  312. // returns a node handle owning that extracted data. If the `btree_map`
  313. // does not contain an element with a matching key, this function returns an
  314. // empty node handle.
  315. //
  316. // NOTE: when compiled in an earlier version of C++ than C++17,
  317. // `node_type::key()` returns a const reference to the key instead of a
  318. // mutable reference. We cannot safely return a mutable reference without
  319. // std::launder (which is not available before C++17).
  320. //
  321. // NOTE: In this context, `node_type` refers to the C++17 concept of a
  322. // move-only type that owns and provides access to the elements in associative
  323. // containers (https://en.cppreference.com/w/cpp/container/node_handle).
  324. // It does NOT refer to the data layout of the underlying btree.
  325. using Base::extract;
  326. // btree_map::merge()
  327. //
  328. // Extracts elements from a given `source` btree_map into this
  329. // `btree_map`. If the destination `btree_map` already contains an
  330. // element with an equivalent key, that element is not extracted.
  331. using Base::merge;
  332. // btree_map::swap(btree_map& other)
  333. //
  334. // Exchanges the contents of this `btree_map` with those of the `other`
  335. // btree_map, avoiding invocation of any move, copy, or swap operations on
  336. // individual elements.
  337. //
  338. // All iterators and references on the `btree_map` remain valid, excepting
  339. // for the past-the-end iterator, which is invalidated.
  340. using Base::swap;
  341. // btree_map::at()
  342. //
  343. // Returns a reference to the mapped value of the element with key equivalent
  344. // to the passed key.
  345. using Base::at;
  346. // btree_map::contains()
  347. //
  348. // template <typename K> bool contains(const K& key) const:
  349. //
  350. // Determines whether an element comparing equal to the given `key` exists
  351. // within the `btree_map`, returning `true` if so or `false` otherwise.
  352. //
  353. // Supports heterogeneous lookup, provided that the map has a compatible
  354. // heterogeneous comparator.
  355. using Base::contains;
  356. // btree_map::count()
  357. //
  358. // template <typename K> size_type count(const K& key) const:
  359. //
  360. // Returns the number of elements comparing equal to the given `key` within
  361. // the `btree_map`. Note that this function will return either `1` or `0`
  362. // since duplicate elements are not allowed within a `btree_map`.
  363. //
  364. // Supports heterogeneous lookup, provided that the map has a compatible
  365. // heterogeneous comparator.
  366. using Base::count;
  367. // btree_map::equal_range()
  368. //
  369. // Returns a half-open range [first, last), defined by a `std::pair` of two
  370. // iterators, containing all elements with the passed key in the `btree_map`.
  371. using Base::equal_range;
  372. // btree_map::find()
  373. //
  374. // template <typename K> iterator find(const K& key):
  375. // template <typename K> const_iterator find(const K& key) const:
  376. //
  377. // Finds an element with the passed `key` within the `btree_map`.
  378. //
  379. // Supports heterogeneous lookup, provided that the map has a compatible
  380. // heterogeneous comparator.
  381. using Base::find;
  382. // btree_map::lower_bound()
  383. //
  384. // template <typename K> iterator lower_bound(const K& key):
  385. // template <typename K> const_iterator lower_bound(const K& key) const:
  386. //
  387. // Finds the first element with a key that is not less than `key` within the
  388. // `btree_map`.
  389. //
  390. // Supports heterogeneous lookup, provided that the map has a compatible
  391. // heterogeneous comparator.
  392. using Base::lower_bound;
  393. // btree_map::upper_bound()
  394. //
  395. // template <typename K> iterator upper_bound(const K& key):
  396. // template <typename K> const_iterator upper_bound(const K& key) const:
  397. //
  398. // Finds the first element with a key that is greater than `key` within the
  399. // `btree_map`.
  400. //
  401. // Supports heterogeneous lookup, provided that the map has a compatible
  402. // heterogeneous comparator.
  403. using Base::upper_bound;
  404. // btree_map::operator[]()
  405. //
  406. // Returns a reference to the value mapped to the passed key within the
  407. // `btree_map`, performing an `insert()` if the key does not already
  408. // exist.
  409. //
  410. // If an insertion occurs, any references, pointers, or iterators are
  411. // invalidated. Otherwise iterators are not affected and references are not
  412. // invalidated. Overloads are listed below.
  413. //
  414. // T& operator[](key_type&& key):
  415. // T& operator[](const key_type& key):
  416. //
  417. // Inserts a value_type object constructed in-place if the element with the
  418. // given key does not exist.
  419. using Base::operator[];
  420. // btree_map::get_allocator()
  421. //
  422. // Returns the allocator function associated with this `btree_map`.
  423. using Base::get_allocator;
  424. // btree_map::key_comp();
  425. //
  426. // Returns the key comparator associated with this `btree_map`.
  427. using Base::key_comp;
  428. // btree_map::value_comp();
  429. //
  430. // Returns the value comparator associated with this `btree_map`.
  431. using Base::value_comp;
  432. };
  433. // absl::swap(absl::btree_map<>, absl::btree_map<>)
  434. //
  435. // Swaps the contents of two `absl::btree_map` containers.
  436. template<typename K, typename V, typename C, typename A>
  437. void swap(btree_map<K, V, C, A>& x, btree_map<K, V, C, A>& y)
  438. {
  439. return x.swap(y);
  440. }
  441. // absl::erase_if(absl::btree_map<>, Pred)
  442. //
  443. // Erases all elements that satisfy the predicate pred from the container.
  444. // Returns the number of erased elements.
  445. template<typename K, typename V, typename C, typename A, typename Pred>
  446. typename btree_map<K, V, C, A>::size_type erase_if(
  447. btree_map<K, V, C, A>& map, Pred pred
  448. )
  449. {
  450. return container_internal::btree_access::erase_if(map, std::move(pred));
  451. }
  452. // absl::btree_multimap
  453. //
  454. // An `absl::btree_multimap<K, V>` is an ordered associative container of
  455. // keys and associated values designed to be a more efficient replacement for
  456. // `std::multimap` (in most cases). Unlike `absl::btree_map`, a B-tree multimap
  457. // allows multiple elements with equivalent keys.
  458. //
  459. // Keys are sorted using an (optional) comparison function, which defaults to
  460. // `std::less<K>`.
  461. //
  462. // An `absl::btree_multimap<K, V>` uses a default allocator of
  463. // `std::allocator<std::pair<const K, V>>` to allocate (and deallocate)
  464. // nodes, and construct and destruct values within those nodes. You may
  465. // instead specify a custom allocator `A` (which in turn requires specifying a
  466. // custom comparator `C`) as in `absl::btree_multimap<K, V, C, A>`.
  467. //
  468. template<typename Key, typename Value, typename Compare = std::less<Key>, typename Alloc = std::allocator<std::pair<const Key, Value>>>
  469. class btree_multimap : public container_internal::btree_multimap_container<container_internal::btree<container_internal::map_params<Key, Value, Compare, Alloc, /*TargetNodeSize=*/256,
  470. /*IsMulti=*/true>>>
  471. {
  472. using Base = typename btree_multimap::btree_multimap_container;
  473. public:
  474. // Constructors and Assignment Operators
  475. //
  476. // A `btree_multimap` supports the same overload set as `std::multimap`
  477. // for construction and assignment:
  478. //
  479. // * Default constructor
  480. //
  481. // absl::btree_multimap<int, std::string> map1;
  482. //
  483. // * Initializer List constructor
  484. //
  485. // absl::btree_multimap<int, std::string> map2 =
  486. // {{1, "huey"}, {2, "dewey"}, {3, "louie"},};
  487. //
  488. // * Copy constructor
  489. //
  490. // absl::btree_multimap<int, std::string> map3(map2);
  491. //
  492. // * Copy assignment operator
  493. //
  494. // absl::btree_multimap<int, std::string> map4;
  495. // map4 = map3;
  496. //
  497. // * Move constructor
  498. //
  499. // // Move is guaranteed efficient
  500. // absl::btree_multimap<int, std::string> map5(std::move(map4));
  501. //
  502. // * Move assignment operator
  503. //
  504. // // May be efficient if allocators are compatible
  505. // absl::btree_multimap<int, std::string> map6;
  506. // map6 = std::move(map5);
  507. //
  508. // * Range constructor
  509. //
  510. // std::vector<std::pair<int, std::string>> v = {{1, "a"}, {2, "b"}};
  511. // absl::btree_multimap<int, std::string> map7(v.begin(), v.end());
  512. btree_multimap()
  513. {
  514. }
  515. using Base::Base;
  516. // btree_multimap::begin()
  517. //
  518. // Returns an iterator to the beginning of the `btree_multimap`.
  519. using Base::begin;
  520. // btree_multimap::cbegin()
  521. //
  522. // Returns a const iterator to the beginning of the `btree_multimap`.
  523. using Base::cbegin;
  524. // btree_multimap::end()
  525. //
  526. // Returns an iterator to the end of the `btree_multimap`.
  527. using Base::end;
  528. // btree_multimap::cend()
  529. //
  530. // Returns a const iterator to the end of the `btree_multimap`.
  531. using Base::cend;
  532. // btree_multimap::empty()
  533. //
  534. // Returns whether or not the `btree_multimap` is empty.
  535. using Base::empty;
  536. // btree_multimap::max_size()
  537. //
  538. // Returns the largest theoretical possible number of elements within a
  539. // `btree_multimap` under current memory constraints. This value can be
  540. // thought of as the largest value of `std::distance(begin(), end())` for a
  541. // `btree_multimap<Key, T>`.
  542. using Base::max_size;
  543. // btree_multimap::size()
  544. //
  545. // Returns the number of elements currently within the `btree_multimap`.
  546. using Base::size;
  547. // btree_multimap::clear()
  548. //
  549. // Removes all elements from the `btree_multimap`. Invalidates any references,
  550. // pointers, or iterators referring to contained elements.
  551. using Base::clear;
  552. // btree_multimap::erase()
  553. //
  554. // Erases elements within the `btree_multimap`. If an erase occurs, any
  555. // references, pointers, or iterators are invalidated.
  556. // Overloads are listed below.
  557. //
  558. // iterator erase(iterator position):
  559. // iterator erase(const_iterator position):
  560. //
  561. // Erases the element at `position` of the `btree_multimap`, returning
  562. // the iterator pointing to the element after the one that was erased
  563. // (or end() if none exists).
  564. //
  565. // iterator erase(const_iterator first, const_iterator last):
  566. //
  567. // Erases the elements in the open interval [`first`, `last`), returning
  568. // the iterator pointing to the element after the interval that was erased
  569. // (or end() if none exists).
  570. //
  571. // template <typename K> size_type erase(const K& key):
  572. //
  573. // Erases the elements matching the key, if any exist, returning the
  574. // number of elements erased.
  575. using Base::erase;
  576. // btree_multimap::insert()
  577. //
  578. // Inserts an element of the specified value into the `btree_multimap`,
  579. // returning an iterator pointing to the newly inserted element.
  580. // Any references, pointers, or iterators are invalidated. Overloads are
  581. // listed below.
  582. //
  583. // iterator insert(const value_type& value):
  584. //
  585. // Inserts a value into the `btree_multimap`, returning an iterator to the
  586. // inserted element.
  587. //
  588. // iterator insert(value_type&& value):
  589. //
  590. // Inserts a moveable value into the `btree_multimap`, returning an iterator
  591. // to the inserted element.
  592. //
  593. // iterator insert(const_iterator hint, const value_type& value):
  594. // iterator insert(const_iterator hint, value_type&& value):
  595. //
  596. // Inserts a value, using the position of `hint` as a non-binding suggestion
  597. // for where to begin the insertion search. Returns an iterator to the
  598. // inserted element.
  599. //
  600. // void insert(InputIterator first, InputIterator last):
  601. //
  602. // Inserts a range of values [`first`, `last`).
  603. //
  604. // void insert(std::initializer_list<init_type> ilist):
  605. //
  606. // Inserts the elements within the initializer list `ilist`.
  607. using Base::insert;
  608. // btree_multimap::emplace()
  609. //
  610. // Inserts an element of the specified value by constructing it in-place
  611. // within the `btree_multimap`. Any references, pointers, or iterators are
  612. // invalidated.
  613. using Base::emplace;
  614. // btree_multimap::emplace_hint()
  615. //
  616. // Inserts an element of the specified value by constructing it in-place
  617. // within the `btree_multimap`, using the position of `hint` as a non-binding
  618. // suggestion for where to begin the insertion search.
  619. //
  620. // Any references, pointers, or iterators are invalidated.
  621. using Base::emplace_hint;
  622. // btree_multimap::extract()
  623. //
  624. // Extracts the indicated element, erasing it in the process, and returns it
  625. // as a C++17-compatible node handle. Overloads are listed below.
  626. //
  627. // node_type extract(const_iterator position):
  628. //
  629. // Extracts the element at the indicated position and returns a node handle
  630. // owning that extracted data.
  631. //
  632. // template <typename K> node_type extract(const K& k):
  633. //
  634. // Extracts the element with the key matching the passed key value and
  635. // returns a node handle owning that extracted data. If the `btree_multimap`
  636. // does not contain an element with a matching key, this function returns an
  637. // empty node handle.
  638. //
  639. // NOTE: when compiled in an earlier version of C++ than C++17,
  640. // `node_type::key()` returns a const reference to the key instead of a
  641. // mutable reference. We cannot safely return a mutable reference without
  642. // std::launder (which is not available before C++17).
  643. //
  644. // NOTE: In this context, `node_type` refers to the C++17 concept of a
  645. // move-only type that owns and provides access to the elements in associative
  646. // containers (https://en.cppreference.com/w/cpp/container/node_handle).
  647. // It does NOT refer to the data layout of the underlying btree.
  648. using Base::extract;
  649. // btree_multimap::merge()
  650. //
  651. // Extracts all elements from a given `source` btree_multimap into this
  652. // `btree_multimap`.
  653. using Base::merge;
  654. // btree_multimap::swap(btree_multimap& other)
  655. //
  656. // Exchanges the contents of this `btree_multimap` with those of the `other`
  657. // btree_multimap, avoiding invocation of any move, copy, or swap operations
  658. // on individual elements.
  659. //
  660. // All iterators and references on the `btree_multimap` remain valid,
  661. // excepting for the past-the-end iterator, which is invalidated.
  662. using Base::swap;
  663. // btree_multimap::contains()
  664. //
  665. // template <typename K> bool contains(const K& key) const:
  666. //
  667. // Determines whether an element comparing equal to the given `key` exists
  668. // within the `btree_multimap`, returning `true` if so or `false` otherwise.
  669. //
  670. // Supports heterogeneous lookup, provided that the map has a compatible
  671. // heterogeneous comparator.
  672. using Base::contains;
  673. // btree_multimap::count()
  674. //
  675. // template <typename K> size_type count(const K& key) const:
  676. //
  677. // Returns the number of elements comparing equal to the given `key` within
  678. // the `btree_multimap`.
  679. //
  680. // Supports heterogeneous lookup, provided that the map has a compatible
  681. // heterogeneous comparator.
  682. using Base::count;
  683. // btree_multimap::equal_range()
  684. //
  685. // Returns a half-open range [first, last), defined by a `std::pair` of two
  686. // iterators, containing all elements with the passed key in the
  687. // `btree_multimap`.
  688. using Base::equal_range;
  689. // btree_multimap::find()
  690. //
  691. // template <typename K> iterator find(const K& key):
  692. // template <typename K> const_iterator find(const K& key) const:
  693. //
  694. // Finds an element with the passed `key` within the `btree_multimap`.
  695. //
  696. // Supports heterogeneous lookup, provided that the map has a compatible
  697. // heterogeneous comparator.
  698. using Base::find;
  699. // btree_multimap::lower_bound()
  700. //
  701. // template <typename K> iterator lower_bound(const K& key):
  702. // template <typename K> const_iterator lower_bound(const K& key) const:
  703. //
  704. // Finds the first element with a key that is not less than `key` within the
  705. // `btree_multimap`.
  706. //
  707. // Supports heterogeneous lookup, provided that the map has a compatible
  708. // heterogeneous comparator.
  709. using Base::lower_bound;
  710. // btree_multimap::upper_bound()
  711. //
  712. // template <typename K> iterator upper_bound(const K& key):
  713. // template <typename K> const_iterator upper_bound(const K& key) const:
  714. //
  715. // Finds the first element with a key that is greater than `key` within the
  716. // `btree_multimap`.
  717. //
  718. // Supports heterogeneous lookup, provided that the map has a compatible
  719. // heterogeneous comparator.
  720. using Base::upper_bound;
  721. // btree_multimap::get_allocator()
  722. //
  723. // Returns the allocator function associated with this `btree_multimap`.
  724. using Base::get_allocator;
  725. // btree_multimap::key_comp();
  726. //
  727. // Returns the key comparator associated with this `btree_multimap`.
  728. using Base::key_comp;
  729. // btree_multimap::value_comp();
  730. //
  731. // Returns the value comparator associated with this `btree_multimap`.
  732. using Base::value_comp;
  733. };
  734. // absl::swap(absl::btree_multimap<>, absl::btree_multimap<>)
  735. //
  736. // Swaps the contents of two `absl::btree_multimap` containers.
  737. template<typename K, typename V, typename C, typename A>
  738. void swap(btree_multimap<K, V, C, A>& x, btree_multimap<K, V, C, A>& y)
  739. {
  740. return x.swap(y);
  741. }
  742. // absl::erase_if(absl::btree_multimap<>, Pred)
  743. //
  744. // Erases all elements that satisfy the predicate pred from the container.
  745. // Returns the number of erased elements.
  746. template<typename K, typename V, typename C, typename A, typename Pred>
  747. typename btree_multimap<K, V, C, A>::size_type erase_if(
  748. btree_multimap<K, V, C, A>& map, Pred pred
  749. )
  750. {
  751. return container_internal::btree_access::erase_if(map, std::move(pred));
  752. }
  753. namespace container_internal
  754. {
  755. // A parameters structure for holding the type parameters for a btree_map.
  756. // Compare and Alloc should be nothrow copy-constructible.
  757. template<typename Key, typename Data, typename Compare, typename Alloc, int TargetNodeSize, bool IsMulti>
  758. struct map_params : common_params<Key, Compare, Alloc, TargetNodeSize, IsMulti,
  759. /*IsMap=*/true,
  760. map_slot_policy<Key, Data>>
  761. {
  762. using super_type = typename map_params::common_params;
  763. using mapped_type = Data;
  764. // This type allows us to move keys when it is safe to do so. It is safe
  765. // for maps in which value_type and mutable_value_type are layout compatible.
  766. using slot_policy = typename super_type::slot_policy;
  767. using slot_type = typename super_type::slot_type;
  768. using value_type = typename super_type::value_type;
  769. using init_type = typename super_type::init_type;
  770. template<typename V>
  771. static auto key(const V& value) -> decltype(value.first)
  772. {
  773. return value.first;
  774. }
  775. static const Key& key(const slot_type* s)
  776. {
  777. return slot_policy::key(s);
  778. }
  779. static const Key& key(slot_type* s)
  780. {
  781. return slot_policy::key(s);
  782. }
  783. // For use in node handle.
  784. static auto mutable_key(slot_type* s)
  785. -> decltype(slot_policy::mutable_key(s))
  786. {
  787. return slot_policy::mutable_key(s);
  788. }
  789. static mapped_type& value(value_type* value)
  790. {
  791. return value->second;
  792. }
  793. };
  794. } // namespace container_internal
  795. ABSL_NAMESPACE_END
  796. } // namespace absl
  797. #endif // ABSL_CONTAINER_BTREE_MAP_H_