btree_map.h 32 KB

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