btree_set.h 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728
  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_set.h
  17. // -----------------------------------------------------------------------------
  18. //
  19. // This header file defines B-tree sets: sorted associative containers of
  20. // values.
  21. //
  22. // * `absl::btree_set<>`
  23. // * `absl::btree_multiset<>`
  24. //
  25. // These B-tree types are similar to the corresponding types in the STL
  26. // (`std::set` and `std::multiset`) 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::set` and `std::multiset`, which are commonly implemented using
  31. // red-black tree nodes, B-tree sets use more generic B-tree nodes able to hold
  32. // multiple values per node. Holding multiple values per node often makes
  33. // B-tree sets perform better than their `std::set` 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::set` and `std::multiset` as there are some API differences, which are
  38. // noted in this header file.
  39. //
  40. // Importantly, insertions and deletions may invalidate outstanding iterators,
  41. // pointers, and references to elements. Such invalidations are typically only
  42. // an issue if insertion and deletion operations are interleaved with the use of
  43. // more than one iterator, pointer, or reference simultaneously. For this
  44. // reason, `insert()` and `erase()` return a valid iterator at the current
  45. // position.
  46. #ifndef ABSL_CONTAINER_BTREE_SET_H_
  47. #define ABSL_CONTAINER_BTREE_SET_H_
  48. #include "absl/container/internal/btree.h" // IWYU pragma: export
  49. #include "absl/container/internal/btree_container.h" // IWYU pragma: export
  50. namespace absl {
  51. ABSL_NAMESPACE_BEGIN
  52. // absl::btree_set<>
  53. //
  54. // An `absl::btree_set<K>` is an ordered associative container of unique key
  55. // values designed to be a more efficient replacement for `std::set` (in most
  56. // cases).
  57. //
  58. // Keys are sorted using an (optional) comparison function, which defaults to
  59. // `std::less<K>`.
  60. //
  61. // An `absl::btree_set<K>` uses a default allocator of `std::allocator<K>` to
  62. // allocate (and deallocate) nodes, and construct and destruct values within
  63. // those nodes. You may instead specify a custom allocator `A` (which in turn
  64. // requires specifying a custom comparator `C`) as in
  65. // `absl::btree_set<K, C, A>`.
  66. //
  67. template <typename Key, typename Compare = std::less<Key>,
  68. typename Alloc = std::allocator<Key>>
  69. class btree_set
  70. : public container_internal::btree_set_container<
  71. container_internal::btree<container_internal::set_params<
  72. Key, Compare, Alloc, /*TargetNodeSize=*/256,
  73. /*Multi=*/false>>> {
  74. using Base = typename btree_set::btree_set_container;
  75. public:
  76. // Constructors and Assignment Operators
  77. //
  78. // A `btree_set` supports the same overload set as `std::set`
  79. // for construction and assignment:
  80. //
  81. // * Default constructor
  82. //
  83. // absl::btree_set<std::string> set1;
  84. //
  85. // * Initializer List constructor
  86. //
  87. // absl::btree_set<std::string> set2 =
  88. // {{"huey"}, {"dewey"}, {"louie"},};
  89. //
  90. // * Copy constructor
  91. //
  92. // absl::btree_set<std::string> set3(set2);
  93. //
  94. // * Copy assignment operator
  95. //
  96. // absl::btree_set<std::string> set4;
  97. // set4 = set3;
  98. //
  99. // * Move constructor
  100. //
  101. // // Move is guaranteed efficient
  102. // absl::btree_set<std::string> set5(std::move(set4));
  103. //
  104. // * Move assignment operator
  105. //
  106. // // May be efficient if allocators are compatible
  107. // absl::btree_set<std::string> set6;
  108. // set6 = std::move(set5);
  109. //
  110. // * Range constructor
  111. //
  112. // std::vector<std::string> v = {"a", "b"};
  113. // absl::btree_set<std::string> set7(v.begin(), v.end());
  114. btree_set() {}
  115. using Base::Base;
  116. // btree_set::begin()
  117. //
  118. // Returns an iterator to the beginning of the `btree_set`.
  119. using Base::begin;
  120. // btree_set::cbegin()
  121. //
  122. // Returns a const iterator to the beginning of the `btree_set`.
  123. using Base::cbegin;
  124. // btree_set::end()
  125. //
  126. // Returns an iterator to the end of the `btree_set`.
  127. using Base::end;
  128. // btree_set::cend()
  129. //
  130. // Returns a const iterator to the end of the `btree_set`.
  131. using Base::cend;
  132. // btree_set::empty()
  133. //
  134. // Returns whether or not the `btree_set` is empty.
  135. using Base::empty;
  136. // btree_set::max_size()
  137. //
  138. // Returns the largest theoretical possible number of elements within a
  139. // `btree_set` under current memory constraints. This value can be thought
  140. // of as the largest value of `std::distance(begin(), end())` for a
  141. // `btree_set<Key>`.
  142. using Base::max_size;
  143. // btree_set::size()
  144. //
  145. // Returns the number of elements currently within the `btree_set`.
  146. using Base::size;
  147. // btree_set::clear()
  148. //
  149. // Removes all elements from the `btree_set`. Invalidates any references,
  150. // pointers, or iterators referring to contained elements.
  151. using Base::clear;
  152. // btree_set::erase()
  153. //
  154. // Erases elements within the `btree_set`. Overloads are listed below.
  155. //
  156. // iterator erase(iterator position):
  157. // iterator erase(const_iterator position):
  158. //
  159. // Erases the element at `position` of the `btree_set`, returning
  160. // the iterator pointing to the element after the one that was erased
  161. // (or end() if none exists).
  162. //
  163. // iterator erase(const_iterator first, const_iterator last):
  164. //
  165. // Erases the elements in the open interval [`first`, `last`), returning
  166. // the iterator pointing to the element after the interval that was erased
  167. // (or end() if none exists).
  168. //
  169. // template <typename K> size_type erase(const K& key):
  170. //
  171. // Erases the element with the matching key, if it exists, returning the
  172. // number of elements erased (0 or 1).
  173. using Base::erase;
  174. // btree_set::insert()
  175. //
  176. // Inserts an element of the specified value into the `btree_set`,
  177. // returning an iterator pointing to the newly inserted element, provided that
  178. // an element with the given key does not already exist. If an insertion
  179. // occurs, any references, pointers, or iterators are invalidated.
  180. // Overloads are listed below.
  181. //
  182. // std::pair<iterator,bool> insert(const value_type& value):
  183. //
  184. // Inserts a value into the `btree_set`. Returns a pair consisting of an
  185. // iterator to the inserted element (or to the element that prevented the
  186. // insertion) and a bool denoting whether the insertion took place.
  187. //
  188. // std::pair<iterator,bool> insert(value_type&& value):
  189. //
  190. // Inserts a moveable value into the `btree_set`. Returns a pair
  191. // consisting of an iterator to the inserted element (or to the element that
  192. // prevented the insertion) and a bool denoting whether the insertion took
  193. // place.
  194. //
  195. // iterator insert(const_iterator hint, const value_type& value):
  196. // iterator insert(const_iterator hint, value_type&& value):
  197. //
  198. // Inserts a value, using the position of `hint` as a non-binding suggestion
  199. // for where to begin the insertion search. Returns an iterator to the
  200. // inserted element, or to the existing element that prevented the
  201. // insertion.
  202. //
  203. // void insert(InputIterator first, InputIterator last):
  204. //
  205. // Inserts a range of values [`first`, `last`).
  206. //
  207. // void insert(std::initializer_list<init_type> ilist):
  208. //
  209. // Inserts the elements within the initializer list `ilist`.
  210. using Base::insert;
  211. // btree_set::emplace()
  212. //
  213. // Inserts an element of the specified value by constructing it in-place
  214. // within the `btree_set`, provided that no element with the given key
  215. // already exists.
  216. //
  217. // The element may be constructed even if there already is an element with the
  218. // key in the container, in which case the newly constructed element will be
  219. // destroyed immediately.
  220. //
  221. // If an insertion occurs, any references, pointers, or iterators are
  222. // invalidated.
  223. using Base::emplace;
  224. // btree_set::emplace_hint()
  225. //
  226. // Inserts an element of the specified value by constructing it in-place
  227. // within the `btree_set`, using the position of `hint` as a non-binding
  228. // suggestion for where to begin the insertion search, and only inserts
  229. // provided that no element with the given key already exists.
  230. //
  231. // The element may be constructed even if there already is an element with the
  232. // key in the container, in which case the newly constructed element will be
  233. // destroyed immediately.
  234. //
  235. // If an insertion occurs, any references, pointers, or iterators are
  236. // invalidated.
  237. using Base::emplace_hint;
  238. // btree_set::extract()
  239. //
  240. // Extracts the indicated element, erasing it in the process, and returns it
  241. // as a C++17-compatible node handle. Overloads are listed below.
  242. //
  243. // node_type extract(const_iterator position):
  244. //
  245. // Extracts the element at the indicated position and returns a node handle
  246. // owning that extracted data.
  247. //
  248. // template <typename K> node_type extract(const K& k):
  249. //
  250. // Extracts the element with the key matching the passed key value and
  251. // returns a node handle owning that extracted data. If the `btree_set`
  252. // does not contain an element with a matching key, this function returns an
  253. // empty node handle.
  254. //
  255. // NOTE: In this context, `node_type` refers to the C++17 concept of a
  256. // move-only type that owns and provides access to the elements in associative
  257. // containers (https://en.cppreference.com/w/cpp/container/node_handle).
  258. // It does NOT refer to the data layout of the underlying btree.
  259. using Base::extract;
  260. // btree_set::merge()
  261. //
  262. // Extracts elements from a given `source` btree_set into this
  263. // `btree_set`. If the destination `btree_set` already contains an
  264. // element with an equivalent key, that element is not extracted.
  265. using Base::merge;
  266. // btree_set::swap(btree_set& other)
  267. //
  268. // Exchanges the contents of this `btree_set` with those of the `other`
  269. // btree_set, avoiding invocation of any move, copy, or swap operations on
  270. // individual elements.
  271. //
  272. // All iterators and references on the `btree_set` remain valid, excepting
  273. // for the past-the-end iterator, which is invalidated.
  274. using Base::swap;
  275. // btree_set::contains()
  276. //
  277. // template <typename K> bool contains(const K& key) const:
  278. //
  279. // Determines whether an element comparing equal to the given `key` exists
  280. // within the `btree_set`, returning `true` if so or `false` otherwise.
  281. //
  282. // Supports heterogeneous lookup, provided that the set has a compatible
  283. // heterogeneous comparator.
  284. using Base::contains;
  285. // btree_set::count()
  286. //
  287. // template <typename K> size_type count(const K& key) const:
  288. //
  289. // Returns the number of elements comparing equal to the given `key` within
  290. // the `btree_set`. Note that this function will return either `1` or `0`
  291. // since duplicate elements are not allowed within a `btree_set`.
  292. //
  293. // Supports heterogeneous lookup, provided that the set has a compatible
  294. // heterogeneous comparator.
  295. using Base::count;
  296. // btree_set::equal_range()
  297. //
  298. // Returns a closed range [first, last], defined by a `std::pair` of two
  299. // iterators, containing all elements with the passed key in the
  300. // `btree_set`.
  301. using Base::equal_range;
  302. // btree_set::find()
  303. //
  304. // template <typename K> iterator find(const K& key):
  305. // template <typename K> const_iterator find(const K& key) const:
  306. //
  307. // Finds an element with the passed `key` within the `btree_set`.
  308. //
  309. // Supports heterogeneous lookup, provided that the set has a compatible
  310. // heterogeneous comparator.
  311. using Base::find;
  312. // btree_set::lower_bound()
  313. //
  314. // template <typename K> iterator lower_bound(const K& key):
  315. // template <typename K> const_iterator lower_bound(const K& key) const:
  316. //
  317. // Finds the first element that is not less than `key` within the `btree_set`.
  318. //
  319. // Supports heterogeneous lookup, provided that the set has a compatible
  320. // heterogeneous comparator.
  321. using Base::lower_bound;
  322. // btree_set::upper_bound()
  323. //
  324. // template <typename K> iterator upper_bound(const K& key):
  325. // template <typename K> const_iterator upper_bound(const K& key) const:
  326. //
  327. // Finds the first element that is greater than `key` within the `btree_set`.
  328. //
  329. // Supports heterogeneous lookup, provided that the set has a compatible
  330. // heterogeneous comparator.
  331. using Base::upper_bound;
  332. // btree_set::get_allocator()
  333. //
  334. // Returns the allocator function associated with this `btree_set`.
  335. using Base::get_allocator;
  336. // btree_set::key_comp();
  337. //
  338. // Returns the key comparator associated with this `btree_set`.
  339. using Base::key_comp;
  340. // btree_set::value_comp();
  341. //
  342. // Returns the value comparator associated with this `btree_set`. The keys to
  343. // sort the elements are the values themselves, therefore `value_comp` and its
  344. // sibling member function `key_comp` are equivalent.
  345. using Base::value_comp;
  346. };
  347. // absl::swap(absl::btree_set<>, absl::btree_set<>)
  348. //
  349. // Swaps the contents of two `absl::btree_set` containers.
  350. template <typename K, typename C, typename A>
  351. void swap(btree_set<K, C, A> &x, btree_set<K, C, A> &y) {
  352. return x.swap(y);
  353. }
  354. // absl::erase_if(absl::btree_set<>, Pred)
  355. //
  356. // Erases all elements that satisfy the predicate pred from the container.
  357. template <typename K, typename C, typename A, typename Pred>
  358. void erase_if(btree_set<K, C, A> &set, Pred pred) {
  359. for (auto it = set.begin(); it != set.end();) {
  360. if (pred(*it)) {
  361. it = set.erase(it);
  362. } else {
  363. ++it;
  364. }
  365. }
  366. }
  367. // absl::btree_multiset<>
  368. //
  369. // An `absl::btree_multiset<K>` is an ordered associative container of
  370. // keys and associated values designed to be a more efficient replacement
  371. // for `std::multiset` (in most cases). Unlike `absl::btree_set`, a B-tree
  372. // multiset allows equivalent elements.
  373. //
  374. // Keys are sorted using an (optional) comparison function, which defaults to
  375. // `std::less<K>`.
  376. //
  377. // An `absl::btree_multiset<K>` uses a default allocator of `std::allocator<K>`
  378. // to allocate (and deallocate) nodes, and construct and destruct values within
  379. // those nodes. You may instead specify a custom allocator `A` (which in turn
  380. // requires specifying a custom comparator `C`) as in
  381. // `absl::btree_multiset<K, C, A>`.
  382. //
  383. template <typename Key, typename Compare = std::less<Key>,
  384. typename Alloc = std::allocator<Key>>
  385. class btree_multiset
  386. : public container_internal::btree_multiset_container<
  387. container_internal::btree<container_internal::set_params<
  388. Key, Compare, Alloc, /*TargetNodeSize=*/256,
  389. /*Multi=*/true>>> {
  390. using Base = typename btree_multiset::btree_multiset_container;
  391. public:
  392. // Constructors and Assignment Operators
  393. //
  394. // A `btree_multiset` supports the same overload set as `std::set`
  395. // for construction and assignment:
  396. //
  397. // * Default constructor
  398. //
  399. // absl::btree_multiset<std::string> set1;
  400. //
  401. // * Initializer List constructor
  402. //
  403. // absl::btree_multiset<std::string> set2 =
  404. // {{"huey"}, {"dewey"}, {"louie"},};
  405. //
  406. // * Copy constructor
  407. //
  408. // absl::btree_multiset<std::string> set3(set2);
  409. //
  410. // * Copy assignment operator
  411. //
  412. // absl::btree_multiset<std::string> set4;
  413. // set4 = set3;
  414. //
  415. // * Move constructor
  416. //
  417. // // Move is guaranteed efficient
  418. // absl::btree_multiset<std::string> set5(std::move(set4));
  419. //
  420. // * Move assignment operator
  421. //
  422. // // May be efficient if allocators are compatible
  423. // absl::btree_multiset<std::string> set6;
  424. // set6 = std::move(set5);
  425. //
  426. // * Range constructor
  427. //
  428. // std::vector<std::string> v = {"a", "b"};
  429. // absl::btree_multiset<std::string> set7(v.begin(), v.end());
  430. btree_multiset() {}
  431. using Base::Base;
  432. // btree_multiset::begin()
  433. //
  434. // Returns an iterator to the beginning of the `btree_multiset`.
  435. using Base::begin;
  436. // btree_multiset::cbegin()
  437. //
  438. // Returns a const iterator to the beginning of the `btree_multiset`.
  439. using Base::cbegin;
  440. // btree_multiset::end()
  441. //
  442. // Returns an iterator to the end of the `btree_multiset`.
  443. using Base::end;
  444. // btree_multiset::cend()
  445. //
  446. // Returns a const iterator to the end of the `btree_multiset`.
  447. using Base::cend;
  448. // btree_multiset::empty()
  449. //
  450. // Returns whether or not the `btree_multiset` is empty.
  451. using Base::empty;
  452. // btree_multiset::max_size()
  453. //
  454. // Returns the largest theoretical possible number of elements within a
  455. // `btree_multiset` under current memory constraints. This value can be
  456. // thought of as the largest value of `std::distance(begin(), end())` for a
  457. // `btree_multiset<Key>`.
  458. using Base::max_size;
  459. // btree_multiset::size()
  460. //
  461. // Returns the number of elements currently within the `btree_multiset`.
  462. using Base::size;
  463. // btree_multiset::clear()
  464. //
  465. // Removes all elements from the `btree_multiset`. Invalidates any references,
  466. // pointers, or iterators referring to contained elements.
  467. using Base::clear;
  468. // btree_multiset::erase()
  469. //
  470. // Erases elements within the `btree_multiset`. Overloads are listed below.
  471. //
  472. // iterator erase(iterator position):
  473. // iterator erase(const_iterator position):
  474. //
  475. // Erases the element at `position` of the `btree_multiset`, returning
  476. // the iterator pointing to the element after the one that was erased
  477. // (or end() if none exists).
  478. //
  479. // iterator erase(const_iterator first, const_iterator last):
  480. //
  481. // Erases the elements in the open interval [`first`, `last`), returning
  482. // the iterator pointing to the element after the interval that was erased
  483. // (or end() if none exists).
  484. //
  485. // template <typename K> size_type erase(const K& key):
  486. //
  487. // Erases the elements matching the key, if any exist, returning the
  488. // number of elements erased.
  489. using Base::erase;
  490. // btree_multiset::insert()
  491. //
  492. // Inserts an element of the specified value into the `btree_multiset`,
  493. // returning an iterator pointing to the newly inserted element.
  494. // Any references, pointers, or iterators are invalidated. Overloads are
  495. // listed below.
  496. //
  497. // iterator insert(const value_type& value):
  498. //
  499. // Inserts a value into the `btree_multiset`, returning an iterator to the
  500. // inserted element.
  501. //
  502. // iterator insert(value_type&& value):
  503. //
  504. // Inserts a moveable value into the `btree_multiset`, returning an iterator
  505. // to the inserted element.
  506. //
  507. // iterator insert(const_iterator hint, const value_type& value):
  508. // iterator insert(const_iterator hint, value_type&& value):
  509. //
  510. // Inserts a value, using the position of `hint` as a non-binding suggestion
  511. // for where to begin the insertion search. Returns an iterator to the
  512. // inserted element.
  513. //
  514. // void insert(InputIterator first, InputIterator last):
  515. //
  516. // Inserts a range of values [`first`, `last`).
  517. //
  518. // void insert(std::initializer_list<init_type> ilist):
  519. //
  520. // Inserts the elements within the initializer list `ilist`.
  521. using Base::insert;
  522. // btree_multiset::emplace()
  523. //
  524. // Inserts an element of the specified value by constructing it in-place
  525. // within the `btree_multiset`. Any references, pointers, or iterators are
  526. // invalidated.
  527. using Base::emplace;
  528. // btree_multiset::emplace_hint()
  529. //
  530. // Inserts an element of the specified value by constructing it in-place
  531. // within the `btree_multiset`, using the position of `hint` as a non-binding
  532. // suggestion for where to begin the insertion search.
  533. //
  534. // Any references, pointers, or iterators are invalidated.
  535. using Base::emplace_hint;
  536. // btree_multiset::extract()
  537. //
  538. // Extracts the indicated element, erasing it in the process, and returns it
  539. // as a C++17-compatible node handle. Overloads are listed below.
  540. //
  541. // node_type extract(const_iterator position):
  542. //
  543. // Extracts the element at the indicated position and returns a node handle
  544. // owning that extracted data.
  545. //
  546. // template <typename K> node_type extract(const K& k):
  547. //
  548. // Extracts the element with the key matching the passed key value and
  549. // returns a node handle owning that extracted data. If the `btree_multiset`
  550. // does not contain an element with a matching key, this function returns an
  551. // empty node handle.
  552. //
  553. // NOTE: In this context, `node_type` refers to the C++17 concept of a
  554. // move-only type that owns and provides access to the elements in associative
  555. // containers (https://en.cppreference.com/w/cpp/container/node_handle).
  556. // It does NOT refer to the data layout of the underlying btree.
  557. using Base::extract;
  558. // btree_multiset::merge()
  559. //
  560. // Extracts all elements from a given `source` btree_multiset into this
  561. // `btree_multiset`.
  562. using Base::merge;
  563. // btree_multiset::swap(btree_multiset& other)
  564. //
  565. // Exchanges the contents of this `btree_multiset` with those of the `other`
  566. // btree_multiset, avoiding invocation of any move, copy, or swap operations
  567. // on individual elements.
  568. //
  569. // All iterators and references on the `btree_multiset` remain valid,
  570. // excepting for the past-the-end iterator, which is invalidated.
  571. using Base::swap;
  572. // btree_multiset::contains()
  573. //
  574. // template <typename K> bool contains(const K& key) const:
  575. //
  576. // Determines whether an element comparing equal to the given `key` exists
  577. // within the `btree_multiset`, returning `true` if so or `false` otherwise.
  578. //
  579. // Supports heterogeneous lookup, provided that the set has a compatible
  580. // heterogeneous comparator.
  581. using Base::contains;
  582. // btree_multiset::count()
  583. //
  584. // template <typename K> size_type count(const K& key) const:
  585. //
  586. // Returns the number of elements comparing equal to the given `key` within
  587. // the `btree_multiset`.
  588. //
  589. // Supports heterogeneous lookup, provided that the set has a compatible
  590. // heterogeneous comparator.
  591. using Base::count;
  592. // btree_multiset::equal_range()
  593. //
  594. // Returns a closed range [first, last], defined by a `std::pair` of two
  595. // iterators, containing all elements with the passed key in the
  596. // `btree_multiset`.
  597. using Base::equal_range;
  598. // btree_multiset::find()
  599. //
  600. // template <typename K> iterator find(const K& key):
  601. // template <typename K> const_iterator find(const K& key) const:
  602. //
  603. // Finds an element with the passed `key` within the `btree_multiset`.
  604. //
  605. // Supports heterogeneous lookup, provided that the set has a compatible
  606. // heterogeneous comparator.
  607. using Base::find;
  608. // btree_multiset::lower_bound()
  609. //
  610. // template <typename K> iterator lower_bound(const K& key):
  611. // template <typename K> const_iterator lower_bound(const K& key) const:
  612. //
  613. // Finds the first element that is not less than `key` within the
  614. // `btree_multiset`.
  615. //
  616. // Supports heterogeneous lookup, provided that the set has a compatible
  617. // heterogeneous comparator.
  618. using Base::lower_bound;
  619. // btree_multiset::upper_bound()
  620. //
  621. // template <typename K> iterator upper_bound(const K& key):
  622. // template <typename K> const_iterator upper_bound(const K& key) const:
  623. //
  624. // Finds the first element that is greater than `key` within the
  625. // `btree_multiset`.
  626. //
  627. // Supports heterogeneous lookup, provided that the set has a compatible
  628. // heterogeneous comparator.
  629. using Base::upper_bound;
  630. // btree_multiset::get_allocator()
  631. //
  632. // Returns the allocator function associated with this `btree_multiset`.
  633. using Base::get_allocator;
  634. // btree_multiset::key_comp();
  635. //
  636. // Returns the key comparator associated with this `btree_multiset`.
  637. using Base::key_comp;
  638. // btree_multiset::value_comp();
  639. //
  640. // Returns the value comparator associated with this `btree_multiset`. The
  641. // keys to sort the elements are the values themselves, therefore `value_comp`
  642. // and its sibling member function `key_comp` are equivalent.
  643. using Base::value_comp;
  644. };
  645. // absl::swap(absl::btree_multiset<>, absl::btree_multiset<>)
  646. //
  647. // Swaps the contents of two `absl::btree_multiset` containers.
  648. template <typename K, typename C, typename A>
  649. void swap(btree_multiset<K, C, A> &x, btree_multiset<K, C, A> &y) {
  650. return x.swap(y);
  651. }
  652. // absl::erase_if(absl::btree_multiset<>, Pred)
  653. //
  654. // Erases all elements that satisfy the predicate pred from the container.
  655. template <typename K, typename C, typename A, typename Pred>
  656. void erase_if(btree_multiset<K, C, A> &set, Pred pred) {
  657. for (auto it = set.begin(); it != set.end();) {
  658. if (pred(*it)) {
  659. it = set.erase(it);
  660. } else {
  661. ++it;
  662. }
  663. }
  664. }
  665. ABSL_NAMESPACE_END
  666. } // namespace absl
  667. #endif // ABSL_CONTAINER_BTREE_SET_H_