123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824 |
- // Copyright 2018 The Abseil Authors.
- //
- // Licensed under the Apache License, Version 2.0 (the "License");
- // you may not use this file except in compliance with the License.
- // You may obtain a copy of the License at
- //
- // https://www.apache.org/licenses/LICENSE-2.0
- //
- // Unless required by applicable law or agreed to in writing, software
- // distributed under the License is distributed on an "AS IS" BASIS,
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- // See the License for the specific language governing permissions and
- // limitations under the License.
- //
- // -----------------------------------------------------------------------------
- // File: btree_set.h
- // -----------------------------------------------------------------------------
- //
- // This header file defines B-tree sets: sorted associative containers of
- // values.
- //
- // * `y_absl::btree_set<>`
- // * `y_absl::btree_multiset<>`
- //
- // These B-tree types are similar to the corresponding types in the STL
- // (`std::set` and `std::multiset`) and generally conform to the STL interfaces
- // of those types. However, because they are implemented using B-trees, they
- // are more efficient in most situations.
- //
- // Unlike `std::set` and `std::multiset`, which are commonly implemented using
- // red-black tree nodes, B-tree sets use more generic B-tree nodes able to hold
- // multiple values per node. Holding multiple values per node often makes
- // B-tree sets perform better than their `std::set` counterparts, because
- // multiple entries can be checked within the same cache hit.
- //
- // However, these types should not be considered drop-in replacements for
- // `std::set` and `std::multiset` as there are some API differences, which are
- // noted in this header file. The most consequential differences with respect to
- // migrating to b-tree from the STL types are listed in the next paragraph.
- // Other API differences are minor.
- //
- // Importantly, insertions and deletions may invalidate outstanding iterators,
- // pointers, and references to elements. Such invalidations are typically only
- // an issue if insertion and deletion operations are interleaved with the use of
- // more than one iterator, pointer, or reference simultaneously. For this
- // reason, `insert()`, `erase()`, and `extract_and_get_next()` return a valid
- // iterator at the current position.
- //
- // Another API difference is that btree iterators can be subtracted, and this
- // is faster than using std::distance.
- //
- // B-tree sets are not exception-safe.
- #ifndef Y_ABSL_CONTAINER_BTREE_SET_H_
- #define Y_ABSL_CONTAINER_BTREE_SET_H_
- #include "y_absl/base/attributes.h"
- #include "y_absl/container/internal/btree.h" // IWYU pragma: export
- #include "y_absl/container/internal/btree_container.h" // IWYU pragma: export
- namespace y_absl {
- Y_ABSL_NAMESPACE_BEGIN
- namespace container_internal {
- template <typename Key>
- struct set_slot_policy;
- template <typename Key, typename Compare, typename Alloc, int TargetNodeSize,
- bool IsMulti>
- struct set_params;
- } // namespace container_internal
- // y_absl::btree_set<>
- //
- // An `y_absl::btree_set<K>` is an ordered associative container of unique key
- // values designed to be a more efficient replacement for `std::set` (in most
- // cases).
- //
- // Keys are sorted using an (optional) comparison function, which defaults to
- // `std::less<K>`.
- //
- // An `y_absl::btree_set<K>` uses a default allocator of `std::allocator<K>` to
- // allocate (and deallocate) nodes, and construct and destruct values within
- // those nodes. You may instead specify a custom allocator `A` (which in turn
- // requires specifying a custom comparator `C`) as in
- // `y_absl::btree_set<K, C, A>`.
- //
- template <typename Key, typename Compare = std::less<Key>,
- typename Alloc = std::allocator<Key>>
- class Y_ABSL_INTERNAL_ATTRIBUTE_OWNER btree_set
- : public container_internal::btree_set_container<
- container_internal::btree<container_internal::set_params<
- Key, Compare, Alloc, /*TargetNodeSize=*/256,
- /*IsMulti=*/false>>> {
- using Base = typename btree_set::btree_set_container;
- public:
- // Constructors and Assignment Operators
- //
- // A `btree_set` supports the same overload set as `std::set`
- // for construction and assignment:
- //
- // * Default constructor
- //
- // y_absl::btree_set<TString> set1;
- //
- // * Initializer List constructor
- //
- // y_absl::btree_set<TString> set2 =
- // {{"huey"}, {"dewey"}, {"louie"},};
- //
- // * Copy constructor
- //
- // y_absl::btree_set<TString> set3(set2);
- //
- // * Copy assignment operator
- //
- // y_absl::btree_set<TString> set4;
- // set4 = set3;
- //
- // * Move constructor
- //
- // // Move is guaranteed efficient
- // y_absl::btree_set<TString> set5(std::move(set4));
- //
- // * Move assignment operator
- //
- // // May be efficient if allocators are compatible
- // y_absl::btree_set<TString> set6;
- // set6 = std::move(set5);
- //
- // * Range constructor
- //
- // std::vector<TString> v = {"a", "b"};
- // y_absl::btree_set<TString> set7(v.begin(), v.end());
- btree_set() {}
- using Base::Base;
- // btree_set::begin()
- //
- // Returns an iterator to the beginning of the `btree_set`.
- using Base::begin;
- // btree_set::cbegin()
- //
- // Returns a const iterator to the beginning of the `btree_set`.
- using Base::cbegin;
- // btree_set::end()
- //
- // Returns an iterator to the end of the `btree_set`.
- using Base::end;
- // btree_set::cend()
- //
- // Returns a const iterator to the end of the `btree_set`.
- using Base::cend;
- // btree_set::empty()
- //
- // Returns whether or not the `btree_set` is empty.
- using Base::empty;
- // btree_set::max_size()
- //
- // Returns the largest theoretical possible number of elements within a
- // `btree_set` under current memory constraints. This value can be thought
- // of as the largest value of `std::distance(begin(), end())` for a
- // `btree_set<Key>`.
- using Base::max_size;
- // btree_set::size()
- //
- // Returns the number of elements currently within the `btree_set`.
- using Base::size;
- // btree_set::clear()
- //
- // Removes all elements from the `btree_set`. Invalidates any references,
- // pointers, or iterators referring to contained elements.
- using Base::clear;
- // btree_set::erase()
- //
- // Erases elements within the `btree_set`. Overloads are listed below.
- //
- // iterator erase(iterator position):
- // iterator erase(const_iterator position):
- //
- // Erases the element at `position` of the `btree_set`, returning
- // the iterator pointing to the element after the one that was erased
- // (or end() if none exists).
- //
- // iterator erase(const_iterator first, const_iterator last):
- //
- // Erases the elements in the open interval [`first`, `last`), returning
- // the iterator pointing to the element after the interval that was erased
- // (or end() if none exists).
- //
- // template <typename K> size_type erase(const K& key):
- //
- // Erases the element with the matching key, if it exists, returning the
- // number of elements erased (0 or 1).
- using Base::erase;
- // btree_set::insert()
- //
- // Inserts an element of the specified value into the `btree_set`,
- // returning an iterator pointing to the newly inserted element, provided that
- // an element with the given key does not already exist. If an insertion
- // occurs, any references, pointers, or iterators are invalidated.
- // Overloads are listed below.
- //
- // std::pair<iterator,bool> insert(const value_type& value):
- //
- // Inserts a value into the `btree_set`. Returns a pair consisting of an
- // iterator to the inserted element (or to the element that prevented the
- // insertion) and a bool denoting whether the insertion took place.
- //
- // std::pair<iterator,bool> insert(value_type&& value):
- //
- // Inserts a moveable value into the `btree_set`. Returns a pair
- // consisting of an iterator to the inserted element (or to the element that
- // prevented the insertion) and a bool denoting whether the insertion took
- // place.
- //
- // iterator insert(const_iterator hint, const value_type& value):
- // iterator insert(const_iterator hint, value_type&& value):
- //
- // Inserts a value, using the position of `hint` as a non-binding suggestion
- // for where to begin the insertion search. Returns an iterator to the
- // inserted element, or to the existing element that prevented the
- // insertion.
- //
- // void insert(InputIterator first, InputIterator last):
- //
- // Inserts a range of values [`first`, `last`).
- //
- // void insert(std::initializer_list<init_type> ilist):
- //
- // Inserts the elements within the initializer list `ilist`.
- using Base::insert;
- // btree_set::emplace()
- //
- // Inserts an element of the specified value by constructing it in-place
- // within the `btree_set`, provided that no element with the given key
- // already exists.
- //
- // The element may be constructed even if there already is an element with the
- // key in the container, in which case the newly constructed element will be
- // destroyed immediately.
- //
- // If an insertion occurs, any references, pointers, or iterators are
- // invalidated.
- using Base::emplace;
- // btree_set::emplace_hint()
- //
- // Inserts an element of the specified value by constructing it in-place
- // within the `btree_set`, using the position of `hint` as a non-binding
- // suggestion for where to begin the insertion search, and only inserts
- // provided that no element with the given key already exists.
- //
- // The element may be constructed even if there already is an element with the
- // key in the container, in which case the newly constructed element will be
- // destroyed immediately.
- //
- // If an insertion occurs, any references, pointers, or iterators are
- // invalidated.
- using Base::emplace_hint;
- // btree_set::extract()
- //
- // Extracts the indicated element, erasing it in the process, and returns it
- // as a C++17-compatible node handle. Any references, pointers, or iterators
- // are invalidated. Overloads are listed below.
- //
- // node_type extract(const_iterator position):
- //
- // Extracts the element at the indicated position and returns a node handle
- // owning that extracted data.
- //
- // template <typename K> node_type extract(const K& k):
- //
- // Extracts the element with the key matching the passed key value and
- // returns a node handle owning that extracted data. If the `btree_set`
- // does not contain an element with a matching key, this function returns an
- // empty node handle.
- //
- // NOTE: In this context, `node_type` refers to the C++17 concept of a
- // move-only type that owns and provides access to the elements in associative
- // containers (https://en.cppreference.com/w/cpp/container/node_handle).
- // It does NOT refer to the data layout of the underlying btree.
- using Base::extract;
- // btree_set::extract_and_get_next()
- //
- // Extracts the indicated element, erasing it in the process, and returns it
- // as a C++17-compatible node handle along with an iterator to the next
- // element.
- //
- // extract_and_get_next_return_type extract_and_get_next(
- // const_iterator position):
- //
- // Extracts the element at the indicated position, returns a struct
- // containing a member named `node`: a node handle owning that extracted
- // data and a member named `next`: an iterator pointing to the next element
- // in the btree.
- using Base::extract_and_get_next;
- // btree_set::merge()
- //
- // Extracts elements from a given `source` btree_set into this
- // `btree_set`. If the destination `btree_set` already contains an
- // element with an equivalent key, that element is not extracted.
- using Base::merge;
- // btree_set::swap(btree_set& other)
- //
- // Exchanges the contents of this `btree_set` with those of the `other`
- // btree_set, avoiding invocation of any move, copy, or swap operations on
- // individual elements.
- //
- // All iterators and references on the `btree_set` remain valid, excepting
- // for the past-the-end iterator, which is invalidated.
- using Base::swap;
- // btree_set::contains()
- //
- // template <typename K> bool contains(const K& key) const:
- //
- // Determines whether an element comparing equal to the given `key` exists
- // within the `btree_set`, returning `true` if so or `false` otherwise.
- //
- // Supports heterogeneous lookup, provided that the set has a compatible
- // heterogeneous comparator.
- using Base::contains;
- // btree_set::count()
- //
- // template <typename K> size_type count(const K& key) const:
- //
- // Returns the number of elements comparing equal to the given `key` within
- // the `btree_set`. Note that this function will return either `1` or `0`
- // since duplicate elements are not allowed within a `btree_set`.
- //
- // Supports heterogeneous lookup, provided that the set has a compatible
- // heterogeneous comparator.
- using Base::count;
- // btree_set::equal_range()
- //
- // Returns a closed range [first, last], defined by a `std::pair` of two
- // iterators, containing all elements with the passed key in the
- // `btree_set`.
- using Base::equal_range;
- // btree_set::find()
- //
- // template <typename K> iterator find(const K& key):
- // template <typename K> const_iterator find(const K& key) const:
- //
- // Finds an element with the passed `key` within the `btree_set`.
- //
- // Supports heterogeneous lookup, provided that the set has a compatible
- // heterogeneous comparator.
- using Base::find;
- // btree_set::lower_bound()
- //
- // template <typename K> iterator lower_bound(const K& key):
- // template <typename K> const_iterator lower_bound(const K& key) const:
- //
- // Finds the first element that is not less than `key` within the `btree_set`.
- //
- // Supports heterogeneous lookup, provided that the set has a compatible
- // heterogeneous comparator.
- using Base::lower_bound;
- // btree_set::upper_bound()
- //
- // template <typename K> iterator upper_bound(const K& key):
- // template <typename K> const_iterator upper_bound(const K& key) const:
- //
- // Finds the first element that is greater than `key` within the `btree_set`.
- //
- // Supports heterogeneous lookup, provided that the set has a compatible
- // heterogeneous comparator.
- using Base::upper_bound;
- // btree_set::get_allocator()
- //
- // Returns the allocator function associated with this `btree_set`.
- using Base::get_allocator;
- // btree_set::key_comp();
- //
- // Returns the key comparator associated with this `btree_set`.
- using Base::key_comp;
- // btree_set::value_comp();
- //
- // Returns the value comparator associated with this `btree_set`. The keys to
- // sort the elements are the values themselves, therefore `value_comp` and its
- // sibling member function `key_comp` are equivalent.
- using Base::value_comp;
- };
- // y_absl::swap(y_absl::btree_set<>, y_absl::btree_set<>)
- //
- // Swaps the contents of two `y_absl::btree_set` containers.
- template <typename K, typename C, typename A>
- void swap(btree_set<K, C, A> &x, btree_set<K, C, A> &y) {
- return x.swap(y);
- }
- // y_absl::erase_if(y_absl::btree_set<>, Pred)
- //
- // Erases all elements that satisfy the predicate pred from the container.
- // Returns the number of erased elements.
- template <typename K, typename C, typename A, typename Pred>
- typename btree_set<K, C, A>::size_type erase_if(btree_set<K, C, A> &set,
- Pred pred) {
- return container_internal::btree_access::erase_if(set, std::move(pred));
- }
- // y_absl::btree_multiset<>
- //
- // An `y_absl::btree_multiset<K>` is an ordered associative container of
- // keys and associated values designed to be a more efficient replacement
- // for `std::multiset` (in most cases). Unlike `y_absl::btree_set`, a B-tree
- // multiset allows equivalent elements.
- //
- // Keys are sorted using an (optional) comparison function, which defaults to
- // `std::less<K>`.
- //
- // An `y_absl::btree_multiset<K>` uses a default allocator of `std::allocator<K>`
- // to allocate (and deallocate) nodes, and construct and destruct values within
- // those nodes. You may instead specify a custom allocator `A` (which in turn
- // requires specifying a custom comparator `C`) as in
- // `y_absl::btree_multiset<K, C, A>`.
- //
- template <typename Key, typename Compare = std::less<Key>,
- typename Alloc = std::allocator<Key>>
- class Y_ABSL_INTERNAL_ATTRIBUTE_OWNER btree_multiset
- : public container_internal::btree_multiset_container<
- container_internal::btree<container_internal::set_params<
- Key, Compare, Alloc, /*TargetNodeSize=*/256,
- /*IsMulti=*/true>>> {
- using Base = typename btree_multiset::btree_multiset_container;
- public:
- // Constructors and Assignment Operators
- //
- // A `btree_multiset` supports the same overload set as `std::set`
- // for construction and assignment:
- //
- // * Default constructor
- //
- // y_absl::btree_multiset<TString> set1;
- //
- // * Initializer List constructor
- //
- // y_absl::btree_multiset<TString> set2 =
- // {{"huey"}, {"dewey"}, {"louie"},};
- //
- // * Copy constructor
- //
- // y_absl::btree_multiset<TString> set3(set2);
- //
- // * Copy assignment operator
- //
- // y_absl::btree_multiset<TString> set4;
- // set4 = set3;
- //
- // * Move constructor
- //
- // // Move is guaranteed efficient
- // y_absl::btree_multiset<TString> set5(std::move(set4));
- //
- // * Move assignment operator
- //
- // // May be efficient if allocators are compatible
- // y_absl::btree_multiset<TString> set6;
- // set6 = std::move(set5);
- //
- // * Range constructor
- //
- // std::vector<TString> v = {"a", "b"};
- // y_absl::btree_multiset<TString> set7(v.begin(), v.end());
- btree_multiset() {}
- using Base::Base;
- // btree_multiset::begin()
- //
- // Returns an iterator to the beginning of the `btree_multiset`.
- using Base::begin;
- // btree_multiset::cbegin()
- //
- // Returns a const iterator to the beginning of the `btree_multiset`.
- using Base::cbegin;
- // btree_multiset::end()
- //
- // Returns an iterator to the end of the `btree_multiset`.
- using Base::end;
- // btree_multiset::cend()
- //
- // Returns a const iterator to the end of the `btree_multiset`.
- using Base::cend;
- // btree_multiset::empty()
- //
- // Returns whether or not the `btree_multiset` is empty.
- using Base::empty;
- // btree_multiset::max_size()
- //
- // Returns the largest theoretical possible number of elements within a
- // `btree_multiset` under current memory constraints. This value can be
- // thought of as the largest value of `std::distance(begin(), end())` for a
- // `btree_multiset<Key>`.
- using Base::max_size;
- // btree_multiset::size()
- //
- // Returns the number of elements currently within the `btree_multiset`.
- using Base::size;
- // btree_multiset::clear()
- //
- // Removes all elements from the `btree_multiset`. Invalidates any references,
- // pointers, or iterators referring to contained elements.
- using Base::clear;
- // btree_multiset::erase()
- //
- // Erases elements within the `btree_multiset`. Overloads are listed below.
- //
- // iterator erase(iterator position):
- // iterator erase(const_iterator position):
- //
- // Erases the element at `position` of the `btree_multiset`, returning
- // the iterator pointing to the element after the one that was erased
- // (or end() if none exists).
- //
- // iterator erase(const_iterator first, const_iterator last):
- //
- // Erases the elements in the open interval [`first`, `last`), returning
- // the iterator pointing to the element after the interval that was erased
- // (or end() if none exists).
- //
- // template <typename K> size_type erase(const K& key):
- //
- // Erases the elements matching the key, if any exist, returning the
- // number of elements erased.
- using Base::erase;
- // btree_multiset::insert()
- //
- // Inserts an element of the specified value into the `btree_multiset`,
- // returning an iterator pointing to the newly inserted element.
- // Any references, pointers, or iterators are invalidated. Overloads are
- // listed below.
- //
- // iterator insert(const value_type& value):
- //
- // Inserts a value into the `btree_multiset`, returning an iterator to the
- // inserted element.
- //
- // iterator insert(value_type&& value):
- //
- // Inserts a moveable value into the `btree_multiset`, returning an iterator
- // to the inserted element.
- //
- // iterator insert(const_iterator hint, const value_type& value):
- // iterator insert(const_iterator hint, value_type&& value):
- //
- // Inserts a value, using the position of `hint` as a non-binding suggestion
- // for where to begin the insertion search. Returns an iterator to the
- // inserted element.
- //
- // void insert(InputIterator first, InputIterator last):
- //
- // Inserts a range of values [`first`, `last`).
- //
- // void insert(std::initializer_list<init_type> ilist):
- //
- // Inserts the elements within the initializer list `ilist`.
- using Base::insert;
- // btree_multiset::emplace()
- //
- // Inserts an element of the specified value by constructing it in-place
- // within the `btree_multiset`. Any references, pointers, or iterators are
- // invalidated.
- using Base::emplace;
- // btree_multiset::emplace_hint()
- //
- // Inserts an element of the specified value by constructing it in-place
- // within the `btree_multiset`, using the position of `hint` as a non-binding
- // suggestion for where to begin the insertion search.
- //
- // Any references, pointers, or iterators are invalidated.
- using Base::emplace_hint;
- // btree_multiset::extract()
- //
- // Extracts the indicated element, erasing it in the process, and returns it
- // as a C++17-compatible node handle. Overloads are listed below.
- //
- // node_type extract(const_iterator position):
- //
- // Extracts the element at the indicated position and returns a node handle
- // owning that extracted data.
- //
- // template <typename K> node_type extract(const K& k):
- //
- // Extracts the element with the key matching the passed key value and
- // returns a node handle owning that extracted data. If the `btree_multiset`
- // does not contain an element with a matching key, this function returns an
- // empty node handle.
- //
- // NOTE: In this context, `node_type` refers to the C++17 concept of a
- // move-only type that owns and provides access to the elements in associative
- // containers (https://en.cppreference.com/w/cpp/container/node_handle).
- // It does NOT refer to the data layout of the underlying btree.
- using Base::extract;
- // btree_multiset::extract_and_get_next()
- //
- // Extracts the indicated element, erasing it in the process, and returns it
- // as a C++17-compatible node handle along with an iterator to the next
- // element.
- //
- // extract_and_get_next_return_type extract_and_get_next(
- // const_iterator position):
- //
- // Extracts the element at the indicated position, returns a struct
- // containing a member named `node`: a node handle owning that extracted
- // data and a member named `next`: an iterator pointing to the next element
- // in the btree.
- using Base::extract_and_get_next;
- // btree_multiset::merge()
- //
- // Extracts all elements from a given `source` btree_multiset into this
- // `btree_multiset`.
- using Base::merge;
- // btree_multiset::swap(btree_multiset& other)
- //
- // Exchanges the contents of this `btree_multiset` with those of the `other`
- // btree_multiset, avoiding invocation of any move, copy, or swap operations
- // on individual elements.
- //
- // All iterators and references on the `btree_multiset` remain valid,
- // excepting for the past-the-end iterator, which is invalidated.
- using Base::swap;
- // btree_multiset::contains()
- //
- // template <typename K> bool contains(const K& key) const:
- //
- // Determines whether an element comparing equal to the given `key` exists
- // within the `btree_multiset`, returning `true` if so or `false` otherwise.
- //
- // Supports heterogeneous lookup, provided that the set has a compatible
- // heterogeneous comparator.
- using Base::contains;
- // btree_multiset::count()
- //
- // template <typename K> size_type count(const K& key) const:
- //
- // Returns the number of elements comparing equal to the given `key` within
- // the `btree_multiset`.
- //
- // Supports heterogeneous lookup, provided that the set has a compatible
- // heterogeneous comparator.
- using Base::count;
- // btree_multiset::equal_range()
- //
- // Returns a closed range [first, last], defined by a `std::pair` of two
- // iterators, containing all elements with the passed key in the
- // `btree_multiset`.
- using Base::equal_range;
- // btree_multiset::find()
- //
- // template <typename K> iterator find(const K& key):
- // template <typename K> const_iterator find(const K& key) const:
- //
- // Finds an element with the passed `key` within the `btree_multiset`.
- //
- // Supports heterogeneous lookup, provided that the set has a compatible
- // heterogeneous comparator.
- using Base::find;
- // btree_multiset::lower_bound()
- //
- // template <typename K> iterator lower_bound(const K& key):
- // template <typename K> const_iterator lower_bound(const K& key) const:
- //
- // Finds the first element that is not less than `key` within the
- // `btree_multiset`.
- //
- // Supports heterogeneous lookup, provided that the set has a compatible
- // heterogeneous comparator.
- using Base::lower_bound;
- // btree_multiset::upper_bound()
- //
- // template <typename K> iterator upper_bound(const K& key):
- // template <typename K> const_iterator upper_bound(const K& key) const:
- //
- // Finds the first element that is greater than `key` within the
- // `btree_multiset`.
- //
- // Supports heterogeneous lookup, provided that the set has a compatible
- // heterogeneous comparator.
- using Base::upper_bound;
- // btree_multiset::get_allocator()
- //
- // Returns the allocator function associated with this `btree_multiset`.
- using Base::get_allocator;
- // btree_multiset::key_comp();
- //
- // Returns the key comparator associated with this `btree_multiset`.
- using Base::key_comp;
- // btree_multiset::value_comp();
- //
- // Returns the value comparator associated with this `btree_multiset`. The
- // keys to sort the elements are the values themselves, therefore `value_comp`
- // and its sibling member function `key_comp` are equivalent.
- using Base::value_comp;
- };
- // y_absl::swap(y_absl::btree_multiset<>, y_absl::btree_multiset<>)
- //
- // Swaps the contents of two `y_absl::btree_multiset` containers.
- template <typename K, typename C, typename A>
- void swap(btree_multiset<K, C, A> &x, btree_multiset<K, C, A> &y) {
- return x.swap(y);
- }
- // y_absl::erase_if(y_absl::btree_multiset<>, Pred)
- //
- // Erases all elements that satisfy the predicate pred from the container.
- // Returns the number of erased elements.
- template <typename K, typename C, typename A, typename Pred>
- typename btree_multiset<K, C, A>::size_type erase_if(
- btree_multiset<K, C, A> & set, Pred pred) {
- return container_internal::btree_access::erase_if(set, std::move(pred));
- }
- namespace container_internal {
- // This type implements the necessary functions from the
- // y_absl::container_internal::slot_type interface for btree_(multi)set.
- template <typename Key>
- struct set_slot_policy {
- using slot_type = Key;
- using value_type = Key;
- using mutable_value_type = Key;
- static value_type &element(slot_type *slot) { return *slot; }
- static const value_type &element(const slot_type *slot) { return *slot; }
- template <typename Alloc, class... Args>
- static void construct(Alloc *alloc, slot_type *slot, Args &&...args) {
- y_absl::allocator_traits<Alloc>::construct(*alloc, slot,
- std::forward<Args>(args)...);
- }
- template <typename Alloc>
- static void construct(Alloc *alloc, slot_type *slot, slot_type *other) {
- y_absl::allocator_traits<Alloc>::construct(*alloc, slot, std::move(*other));
- }
- template <typename Alloc>
- static void construct(Alloc *alloc, slot_type *slot, const slot_type *other) {
- y_absl::allocator_traits<Alloc>::construct(*alloc, slot, *other);
- }
- template <typename Alloc>
- static void destroy(Alloc *alloc, slot_type *slot) {
- y_absl::allocator_traits<Alloc>::destroy(*alloc, slot);
- }
- };
- // A parameters structure for holding the type parameters for a btree_set.
- // Compare and Alloc should be nothrow copy-constructible.
- template <typename Key, typename Compare, typename Alloc, int TargetNodeSize,
- bool IsMulti>
- struct set_params : common_params<Key, Compare, Alloc, TargetNodeSize, IsMulti,
- /*IsMap=*/false, set_slot_policy<Key>> {
- using value_type = Key;
- using slot_type = typename set_params::common_params::slot_type;
- template <typename V>
- static const V &key(const V &value) {
- return value;
- }
- static const Key &key(const slot_type *slot) { return *slot; }
- static const Key &key(slot_type *slot) { return *slot; }
- };
- } // namespace container_internal
- Y_ABSL_NAMESPACE_END
- } // namespace y_absl
- #endif // Y_ABSL_CONTAINER_BTREE_SET_H_
|