4#ifndef TETL_SET_STATIC_SET_HPP
5#define TETL_SET_STATIC_SET_HPP
7#include <etl/_algorithm/lexicographical_compare.hpp>
8#include <etl/_algorithm/lower_bound.hpp>
9#include <etl/_algorithm/rotate.hpp>
10#include <etl/_contracts/check.hpp>
11#include <etl/_functional/less.hpp>
12#include <etl/_iterator/begin.hpp>
13#include <etl/_iterator/data.hpp>
14#include <etl/_iterator/end.hpp>
15#include <etl/_iterator/rbegin.hpp>
16#include <etl/_iterator/rend.hpp>
17#include <etl/_iterator/reverse_iterator.hpp>
18#include <etl/_iterator/size.hpp>
19#include <etl/_vector/static_vector.hpp>
29template <
typename Key, size_t Capacity,
typename Compare =
less<Key>>
38 static_assert(is_default_constructible_v<Compare>);
41 storage_type _storage{};
44 using key_type =
typename storage_type::value_type;
45 using value_type =
typename storage_type::value_type;
46 using size_type = size_t;
47 using difference_type = ptrdiff_t;
48 using key_compare = Compare;
49 using value_compare = Compare;
50 using reference = value_type&;
51 using const_reference = value_type
const&;
52 using pointer =
typename storage_type::pointer;
53 using const_pointer =
typename storage_type::const_pointer;
54 using iterator =
typename storage_type::pointer;
55 using const_iterator =
typename storage_type::const_pointer;
56 using reverse_iterator =
etl::reverse_iterator<iterator>;
57 using const_reverse_iterator =
etl::reverse_iterator<const_iterator>;
65 template <
typename InputIt>
66 requires(detail::InputIterator<InputIt>)
69 if constexpr (detail::RandomAccessIterator<InputIt>) {
70 TETL_PRECONDITION(last - first >= 0);
71 TETL_PRECONDITION(
static_cast<size_type>(last - first) <= max_size());
93 return _storage.begin();
99 return _storage.begin();
112 return _storage.end();
119 return _storage.end();
133 return reverse_iterator(end());
140 return const_reverse_iterator(end());
155 return reverse_iterator(begin());
161 [[
nodiscard]]
constexpr auto rend()
const noexcept -> const_reverse_iterator
163 return const_reverse_iterator(begin());
178 return _storage.empty();
184 return _storage.full();
191 return _storage.size();
198 return _storage.max_size();
203 constexpr auto clear()
noexcept ->
void
210 constexpr auto insert(value_type&& value) ->
pair<iterator,
bool>
211 requires(is_move_constructible_v<value_type>)
214 auto cmp = key_compare{};
215 auto* p =
etl::lower_bound(_storage.begin(), _storage.end(), value, cmp);
216 if (p == _storage.end() || *(p) != value) {
217 _storage.push_back(
etl::move(value));
218 auto* pos = rotate(p, _storage.end() - 1, _storage.end());
219 return make_pair(pos,
true);
223 return pair<iterator,
bool>(
nullptr,
false);
229 ->
pair<iterator,
bool>
230 requires(is_copy_constructible_v<value_type>)
232 value_type tmp = value;
233 return insert(
etl::move(tmp));
239 template <
typename InputIter>
240 requires(detail::InputIterator<InputIter>)
243 for (; first != last; ++first) {
251 template <
typename... Args>
252 requires(is_copy_constructible_v<key_type>)
255 return insert(value_type(
etl::forward<Args>(args)...));
263 constexpr auto erase(iterator pos)
noexcept -> iterator
265 return _storage.erase(pos);
274 constexpr auto erase(iterator first, iterator last) -> iterator
277 for (; first != last; ++first) {
289 constexpr auto erase(key_type
const& key)
noexcept -> size_type
291 if (
auto* pos =
etl::lower_bound(begin(), end(), key); pos != end()) {
299 constexpr auto swap(
static_set& other)
noexcept(is_nothrow_swappable_v<key_type>) ->
void
300 requires(is_assignable_v<key_type&, key_type &&>)
302 etl::swap(_storage, other._storage);
308 [[
nodiscard]]
constexpr auto count(key_type
const& key)
const noexcept -> size_type
310 return contains(key) ? 1 : 0;
315 template <
typename K>
316 requires(detail::is_transparent_v<key_compare>)
317 [[nodiscard]]
constexpr auto count(K
const& x)
const -> size_type
319 return contains(x) ? 1 : 0;
328 return etl::find(begin(), end(), key);
335 [[
nodiscard]]
constexpr auto find(key_type
const& key)
const noexcept -> const_iterator
337 return etl::find(begin(), end(), key);
342 template <
typename K>
343 requires(detail::is_transparent_v<key_compare>)
344 [[nodiscard]]
constexpr auto find(K
const& x) -> iterator
346 return find_if(begin(), end(), [&x](
auto const& val) {
347 auto comp = key_compare();
354 template <
typename K>
355 requires(detail::is_transparent_v<key_compare>)
356 [[nodiscard]]
constexpr auto find(K
const& x)
const -> const_iterator
359 auto comp = key_compare();
368 return find(key) != end();
373 template <
typename K>
374 requires(detail::is_transparent_v<key_compare>)
375 [[nodiscard]]
constexpr auto contains(K
const& x)
const ->
bool
377 return find(x) != end();
384 return etl::lower_bound(begin(), end(), key, key_compare{});
391 return etl::lower_bound(begin(), end(), key, key_compare{});
396 template <
typename K>
397 requires(detail::is_transparent_v<key_compare>)
398 [[nodiscard]]
constexpr auto lower_bound(K
const& key) -> iterator
400 return etl::lower_bound(begin(), end(), key, key_compare{});
405 template <
typename K>
406 requires(detail::is_transparent_v<key_compare>)
407 [[nodiscard]]
constexpr auto lower_bound(K
const& key)
const -> const_iterator
409 return etl::lower_bound(begin(), end(), key, key_compare{});
416 return etl::upper_bound(begin(), end(), key, key_compare{});
423 return etl::upper_bound(begin(), end(), key, key_compare{});
428 template <
typename K>
429 requires(detail::is_transparent_v<key_compare>)
430 [[nodiscard]]
constexpr auto upper_bound(K
const& key) -> iterator
432 return etl::upper_bound(begin(), end(), key, key_compare{});
437 template <
typename K>
438 requires(detail::is_transparent_v<key_compare>)
439 [[nodiscard]]
constexpr auto upper_bound(K
const& key)
const -> const_iterator
441 return etl::upper_bound(begin(), end(), key, key_compare{});
451 return etl::equal_range(begin(), end(), key, key_compare{});
461 return etl::equal_range(begin(), end(), key, key_compare{});
469 template <
typename K>
470 requires(detail::is_transparent_v<key_compare>)
471 [[nodiscard]]
constexpr auto equal_range(K
const& key) -> iterator
473 return etl::equal_range(begin(), end(), key, key_compare{});
481 template <
typename K>
482 requires(detail::is_transparent_v<key_compare>)
483 [[nodiscard]]
constexpr auto equal_range(K
const& key)
const -> const_iterator
485 return etl::equal_range(begin(), end(), key, key_compare{});
495 return key_compare();
504 return value_compare();
513template <
typename Key, size_t Capacity,
typename Comp>
514[[nodiscard]]
constexpr auto
517 return lhs.size() == rhs.size() && equal(begin(lhs), end(lhs), begin(rhs));
525template <
typename Key, size_t Capacity,
typename Comp>
526[[nodiscard]]
constexpr auto
529 return !(lhs == rhs);
538template <
typename Key, size_t Capacity,
typename Comp>
539[[nodiscard]]
constexpr auto
542 return lexicographical_compare(begin(lhs), end(lhs), begin(rhs), end(rhs));
551template <
typename Key, size_t Capacity,
typename Comp>
552[[nodiscard]]
constexpr auto
564template <
typename Key, size_t Capacity,
typename Comp>
565[[nodiscard]]
constexpr auto
577template <
typename Key, size_t Capacity,
typename Comp>
578[[nodiscard]]
constexpr auto
586template <
typename Key, size_t Capacity,
typename Compare>
Definition adjacent_find.hpp:9
constexpr auto swap(static_set< Key, Capacity, Compare > &lhs, static_set< Key, Capacity, Compare > &rhs) noexcept(noexcept(lhs.swap(rhs))) -> void
Specializes the swap algorithm for set. Swaps the contents of lhs and rhs. Calls lhs....
Definition static_set.hpp:588
constexpr auto operator>(static_set< Key, Capacity, Comp > const &lhs, static_set< Key, Capacity, Comp > const &rhs) -> bool
Compares the contents of two sets.
Definition static_set.hpp:566
constexpr auto operator!=(static_set< Key, Capacity, Comp > const &lhs, static_set< Key, Capacity, Comp > const &rhs) -> bool
Compares the contents of two sets.
Definition static_set.hpp:527
constexpr auto operator<=(static_set< Key, Capacity, Comp > const &lhs, static_set< Key, Capacity, Comp > const &rhs) -> bool
Compares the contents of two sets.
Definition static_set.hpp:553
constexpr auto operator<(static_set< Key, Capacity, Comp > const &lhs, static_set< Key, Capacity, Comp > const &rhs) -> bool
Compares the contents of two sets.
Definition static_set.hpp:540
constexpr auto operator>=(static_set< Key, Capacity, Comp > const &lhs, static_set< Key, Capacity, Comp > const &rhs) -> bool
Compares the contents of two sets.
Definition static_set.hpp:579
constexpr auto operator==(static_set< Key, Capacity, Comp > const &lhs, static_set< Key, Capacity, Comp > const &rhs) -> bool
Compares the contents of two sets.
Definition static_set.hpp:515
Function object for performing comparisons. Unless specialised, invokes operator< on type T....
Definition less.hpp:15
etl::pair is a class template that provides a way to store two heterogeneous objects as a single unit...
Definition pair.hpp:37
static_set is an associative container that contains a sorted set of unique objects of type Key....
Definition static_set.hpp:30
constexpr auto equal_range(key_type const &key) -> iterator
Returns a range containing all elements with the given key in the container. The range is defined by ...
Definition static_set.hpp:449
constexpr auto equal_range(K const &key) -> iterator
Returns a range containing all elements with the given key in the container. The range is defined by ...
Definition static_set.hpp:471
constexpr auto lower_bound(K const &key) const -> const_iterator
Returns an iterator pointing to the first element that is not less than (i.e. greater or equal to) ke...
Definition static_set.hpp:407
constexpr auto find(key_type const &key) const noexcept -> const_iterator
Finds an element with key equivalent to key.
Definition static_set.hpp:335
constexpr auto contains(key_type const &key) const noexcept -> bool
Checks if there is an element with key equivalent to key in the container.
Definition static_set.hpp:366
constexpr auto emplace(Args &&... args) noexcept(noexcept(insert(declval< key_type >()))) -> pair< iterator, bool >
Inserts a new element into the container constructed in-place with the given args if there is no elem...
Definition static_set.hpp:253
constexpr static_set(static_set &&other) noexcept(noexcept(etl::move(declval< storage_type >())))=default
constexpr auto erase(key_type const &key) noexcept -> size_type
Removes the element (if one exists) with the key equivalent to key.
Definition static_set.hpp:289
constexpr auto value_comp() const noexcept -> value_compare
Returns the function object that compares the values. It is the same as key_comp.
Definition static_set.hpp:502
constexpr auto find(key_type const &key) noexcept -> iterator
Finds an element with key equivalent to key.
Definition static_set.hpp:326
constexpr auto swap(static_set &other) noexcept(is_nothrow_swappable_v< key_type >) -> void requires(is_assignable_v< key_type &, key_type && >)
Exchanges the contents of the container with those of other.
Definition static_set.hpp:299
constexpr auto count(key_type const &key) const noexcept -> size_type
Returns the number of elements with key that compares equivalent to the specified argument,...
Definition static_set.hpp:308
constexpr auto crbegin() const noexcept -> const_reverse_iterator
Returns a reverse iterator to the first element of the reversed set. It corresponds to the last eleme...
Definition static_set.hpp:145
constexpr auto upper_bound(key_type const &key) -> iterator
Returns an iterator pointing to the first element that is greater than key.
Definition static_set.hpp:414
constexpr auto upper_bound(K const &key) -> iterator
Returns an iterator pointing to the first element that is greater than key.
Definition static_set.hpp:430
constexpr auto rbegin() const noexcept -> const_reverse_iterator
Returns a reverse iterator to the first element of the reversed set. It corresponds to the last eleme...
Definition static_set.hpp:138
constexpr auto lower_bound(key_type const &key) -> iterator
Returns an iterator pointing to the first element that is not less than (i.e. greater or equal to) ke...
Definition static_set.hpp:382
constexpr auto find(K const &x) -> iterator
Finds an element with key that compares equivalent to the value x.
Definition static_set.hpp:344
constexpr static_set()=default
Constructs empty container.
constexpr static_set(InputIt first, InputIt last)
Constructs with the contents of the range [first, last). If multiple elements in the range have keys ...
Definition static_set.hpp:67
constexpr auto lower_bound(key_type const &key) const -> const_iterator
Returns an iterator pointing to the first element that is not less than (i.e. greater or equal to) ke...
Definition static_set.hpp:389
constexpr auto rend() noexcept -> reverse_iterator
Returns a reverse iterator to the element following the last element of the reversed set....
Definition static_set.hpp:153
constexpr auto begin() noexcept -> iterator
Returns an iterator to the first element of the set.
Definition static_set.hpp:91
constexpr auto insert(InputIter first, InputIter last) noexcept(noexcept(insert(declval< key_type >()))) -> void
Inserts elements from range [first, last). If multiple elements in the range have keys that compare e...
Definition static_set.hpp:241
constexpr auto end() noexcept -> iterator
Returns an iterator to the element following the last element of the set.
Definition static_set.hpp:110
constexpr auto begin() const noexcept -> const_iterator
Returns an iterator to the first element of the set.
Definition static_set.hpp:97
constexpr auto cbegin() const noexcept -> const_iterator
Returns an iterator to the first element of the set.
Definition static_set.hpp:103
constexpr auto insert(value_type const &value) noexcept(noexcept(insert(etl::move(declval< key_type >())))) -> pair< iterator, bool > requires(is_copy_constructible_v< value_type >)
Inserts element into the container, if the container doesn't already contain an element with an equiv...
Definition static_set.hpp:228
constexpr auto empty() const noexcept -> bool
Checks if the container has no elements, i.e. whether begin() == end().
Definition static_set.hpp:176
constexpr auto clear() noexcept -> void
Erases all elements from the container. After this call, size() returns zero.
Definition static_set.hpp:203
constexpr auto find(K const &x) const -> const_iterator
Finds an element with key that compares equivalent to the value x.
Definition static_set.hpp:356
constexpr auto equal_range(K const &key) const -> const_iterator
Returns a range containing all elements with the given key in the container. The range is defined by ...
Definition static_set.hpp:483
constexpr auto count(K const &x) const -> size_type
Returns the number of elements with key that compares equivalent to the value x.
Definition static_set.hpp:317
constexpr auto full() const noexcept -> bool
Checks if the container full, i.e. whether size() == Capacity.
Definition static_set.hpp:182
constexpr auto contains(K const &x) const -> bool
Checks if there is an element with key that compares equivalent to the value x.
Definition static_set.hpp:375
constexpr auto max_size() const noexcept -> size_type
Returns the maximum number of elements the container is able to hold.
Definition static_set.hpp:196
constexpr auto end() const noexcept -> const_iterator
Returns an iterator to the element following the last element of the set.
Definition static_set.hpp:117
constexpr auto upper_bound(K const &key) const -> const_iterator
Returns an iterator pointing to the first element that is greater than key.
Definition static_set.hpp:439
constexpr auto equal_range(key_type const &key) const -> const_iterator
Returns a range containing all elements with the given key in the container. The range is defined by ...
Definition static_set.hpp:459
constexpr auto operator=(static_set &&other) noexcept(noexcept(etl::move(declval< storage_type >()))) -> static_set &=default
constexpr static_set(static_set const &other)=default
constexpr auto lower_bound(K const &key) -> iterator
Returns an iterator pointing to the first element that is not less than (i.e. greater or equal to) ke...
Definition static_set.hpp:398
constexpr auto crend() const noexcept -> const_reverse_iterator
Returns a reverse iterator to the element following the last element of the reversed set....
Definition static_set.hpp:169
constexpr auto insert(value_type &&value) -> pair< iterator, bool > requires(is_move_constructible_v< value_type >)
Inserts element into the container, if the container doesn't already contain an element with an equiv...
Definition static_set.hpp:210
constexpr auto erase(iterator pos) noexcept -> iterator
Removes the element at pos.
Definition static_set.hpp:263
constexpr auto erase(iterator first, iterator last) -> iterator
Removes the elements in the range [first; last), which must be a valid range in *this.
Definition static_set.hpp:274
constexpr auto upper_bound(key_type const &key) const -> const_iterator
Returns an iterator pointing to the first element that is greater than key.
Definition static_set.hpp:421
constexpr auto rend() const noexcept -> const_reverse_iterator
Returns a reverse iterator to the element following the last element of the reversed set....
Definition static_set.hpp:161
constexpr auto size() const noexcept -> size_type
Returns the number of elements in the container, i.e. distance(begin(), end()).
Definition static_set.hpp:189
constexpr auto cend() const noexcept -> const_iterator
Returns an iterator to the element following the last element of the set.
Definition static_set.hpp:124
constexpr auto operator=(static_set const &other) -> static_set &=default
constexpr auto key_comp() const noexcept -> key_compare
Returns the function object that compares the keys, which is a copy of this container's constructor a...
Definition static_set.hpp:493
constexpr auto rbegin() noexcept -> reverse_iterator
Returns a reverse iterator to the first element of the reversed set. It corresponds to the last eleme...
Definition static_set.hpp:131
Dynamically-resizable fixed-capacity vector.
Definition static_vector.hpp:401