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_set.h 32 kB

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