4#ifndef TETL_FLAT_SET_FLAT_SET_HPP
5#define TETL_FLAT_SET_FLAT_SET_HPP
7#include <etl/_config/all.hpp>
9#include <etl/_algorithm/equal.hpp>
10#include <etl/_algorithm/equal_range.hpp>
11#include <etl/_algorithm/lexicographical_compare.hpp>
12#include <etl/_algorithm/lower_bound.hpp>
13#include <etl/_algorithm/remove.hpp>
14#include <etl/_algorithm/remove_if.hpp>
15#include <etl/_algorithm/upper_bound.hpp>
16#include <etl/_concepts/emulation.hpp>
17#include <etl/_cstddef/size_t.hpp>
18#include <etl/_flat_set/sorted_unique.hpp>
19#include <etl/_functional/is_transparent.hpp>
20#include <etl/_functional/less.hpp>
21#include <etl/_functional/reference_wrapper.hpp>
22#include <etl/_iterator/begin.hpp>
23#include <etl/_iterator/data.hpp>
24#include <etl/_iterator/distance.hpp>
25#include <etl/_iterator/end.hpp>
26#include <etl/_iterator/rbegin.hpp>
27#include <etl/_iterator/rend.hpp>
28#include <etl/_iterator/reverse_iterator.hpp>
29#include <etl/_iterator/size.hpp>
30#include <etl/_type_traits/is_nothrow_swappable.hpp>
31#include <etl/_utility/forward.hpp>
32#include <etl/_utility/move.hpp>
33#include <etl/_utility/pair.hpp>
58template <
typename Key,
typename Container,
typename Compare =
etl::
less<Key>>
61 using key_compare = Compare;
62 using value_type = Key;
63 using value_compare = Compare;
64 using reference = value_type&;
65 using const_reference = value_type
const&;
66 using size_type =
typename Container::size_type;
67 using difference_type =
typename Container::difference_type;
68 using iterator =
typename Container::iterator;
69 using const_iterator =
typename Container::const_iterator;
70 using reverse_iterator =
etl::reverse_iterator<iterator>;
71 using const_reverse_iterator =
etl::reverse_iterator<const_iterator>;
72 using container_type = Container;
85 explicit constexpr flat_set(container_type
const& container)
86 requires(detail::RandomAccessRange<container_type>)
92 : _container{
etl::move(cont)}
97 explicit constexpr flat_set(Compare
const& comp)
103 template <
typename InputIt>
104 constexpr flat_set(InputIt first, InputIt last, Compare
const& comp = Compare())
111 template <
typename InputIt>
113 : _container(first, last)
120 return _container.begin();
124 return _container.begin();
128 return _container.begin();
133 return _container.end();
137 return _container.end();
141 return _container.end();
146 return _container.rbegin();
150 return _container.rbegin();
154 return _container.crbegin();
159 return _container.rend();
161 [[
nodiscard]]
constexpr auto rend()
const noexcept -> const_reverse_iterator
163 return _container.rend();
167 return _container.crend();
173 return _container.empty();
179 return _container.size();
185 return _container.max_size();
189 template <
typename... Args>
192 auto key = Key{
etl::forward<Args>(args)...};
193 iterator it = lower_bound(key);
195 if (it == end()
or _compare(key, *it)) {
196 it = _container.emplace(it,
etl::move(key));
197 return etl::make_pair(it,
true);
200 return etl::make_pair(it,
false);
203 template <
typename... Args>
204 constexpr auto emplace_hint(const_iterator , Args&&... args) -> iterator
206 return emplace(
etl::forward<Args>(args)...).first;
216 return emplace(
etl::move(x));
219 constexpr auto insert(const_iterator position, value_type
const& x) -> iterator
221 return emplace_hint(position, x);
224 constexpr auto insert(const_iterator position, value_type&& x) -> iterator
226 return emplace_hint(position,
etl::move(x));
229 template <
typename InputIt>
230 constexpr auto insert(InputIt first, InputIt last) ->
void
232 while (first != last) {
238 template <
typename InputIt>
243 auto&& container =
etl::move(_container);
248 constexpr auto replace(container_type&& container) ->
void
250 _container =
etl::move(container);
253 constexpr auto erase(iterator position) -> iterator
255 return _container.erase(position);
258 constexpr auto erase(const_iterator position) -> iterator
260 return _container.erase(position);
263 constexpr auto erase(key_type
const& key) -> size_type
265 auto const it =
etl::remove(begin(), end(), key);
266 auto const r =
static_cast<size_type>(
etl::distance(it, end()));
271 constexpr auto erase(const_iterator first, const_iterator last) -> iterator
273 return _container.erase(first, last);
281 swap(_compare, other._compare);
282 swap(_container, other._container);
285 constexpr auto clear()
noexcept ->
void
304 auto const it = lower_bound(key);
305 if (it == end()
or _compare(key, *it)) {
311 [[
nodiscard]]
constexpr auto find(key_type
const& key)
const -> const_iterator
313 auto const it = lower_bound(key);
314 if (it == end()
or _compare(key, *it)) {
320 template <
typename K>
321 requires etl::detail::is_transparent_v<Compare>
322 [[nodiscard]]
constexpr auto find(K
const& key) -> iterator
324 auto const it = lower_bound(key);
325 if (it == end()
or _compare(key, *it)) {
331 template <
typename K>
332 requires etl::detail::is_transparent_v<Compare>
333 [[nodiscard]]
constexpr auto find(K
const& key)
const -> const_iterator
335 auto const it = lower_bound(key);
336 if (it == end()
or _compare(key, *it)) {
344 return find(key) == end() ? 0 : 1;
347 template <
typename K>
348 requires etl::detail::is_transparent_v<Compare>
349 [[nodiscard]]
constexpr auto count(K
const& key)
const -> size_type
351 return find(key) == end() ? 0 : 1;
356 return count(key) == 1;
359 template <
typename K>
360 requires etl::detail::is_transparent_v<Compare>
361 [[nodiscard]]
constexpr auto contains(K
const& key)
const ->
bool
363 return count(key) == 1;
368 return etl::lower_bound(begin(), end(), key,
etl::ref(_compare));
373 return etl::lower_bound(begin(), end(), key,
etl::ref(_compare));
376 template <
typename K>
377 requires etl::detail::is_transparent_v<Compare>
378 [[nodiscard]]
constexpr auto lower_bound(K
const& key) -> iterator
380 return etl::lower_bound(begin(), end(), key,
etl::ref(_compare));
383 template <
typename K>
384 requires etl::detail::is_transparent_v<Compare>
385 [[nodiscard]]
constexpr auto lower_bound(K
const& key)
const -> const_iterator
387 return etl::lower_bound(begin(), end(), key,
etl::ref(_compare));
392 return etl::upper_bound(begin(), end(), key,
etl::ref(_compare));
397 return etl::upper_bound(begin(), end(), key,
etl::ref(_compare));
400 template <
typename K>
401 requires etl::detail::is_transparent_v<Compare>
402 [[nodiscard]]
constexpr auto upper_bound(K
const& key) -> iterator
404 return etl::upper_bound(begin(), end(), key,
etl::ref(_compare));
407 template <
typename K>
408 requires etl::detail::is_transparent_v<Compare>
409 [[nodiscard]]
constexpr auto upper_bound(K
const& key)
const -> const_iterator
411 return etl::upper_bound(begin(), end(), key,
etl::ref(_compare));
416 return etl::equal_range(begin(), end(), key,
etl::ref(_compare));
421 return etl::equal_range(begin(), end(), key,
etl::ref(_compare));
424 template <
typename K>
425 requires etl::detail::is_transparent_v<Compare>
428 return etl::equal_range(begin(), end(), key,
etl::ref(_compare));
431 template <
typename K>
432 requires etl::detail::is_transparent_v<Compare>
433 [[nodiscard]]
constexpr auto equal_range(K
const& key)
const ->
etl::
pair<const_iterator, const_iterator>
435 return etl::equal_range(begin(), end(), key,
etl::ref(_compare));
440 return etl::equal(lhs.begin(), lhs.end(), rhs.begin(), rhs.end());
445 return etl::lexicographical_compare(lhs.begin(), lhs.end(), rhs.begin(), rhs.end());
473template <
typename Key,
typename Container,
typename Compare,
typename Pred>
477 auto const it =
etl::remove_if(c.begin(), c.end(), pred);
478 auto const r =
etl::distance(it, c.end());
479 c.erase(it, c.end());
480 return static_cast<
typename etl::
flat_set<Key, Container, Compare>::size_type>(r);
Definition adjacent_find.hpp:9
constexpr auto erase_if(etl::flat_set< Key, Container, Compare > &c, Pred pred) -> typename etl::flat_set< Key, Container, Compare >::size_type
Definition flat_set.hpp:474
A flat_set is a container adaptor that provides an associative container interface that supports uniq...
Definition flat_set.hpp:59
constexpr auto erase(key_type const &key) -> size_type
Definition flat_set.hpp:263
constexpr auto lower_bound(K const &key) const -> const_iterator
Definition flat_set.hpp:385
constexpr auto upper_bound(K const &key) const -> const_iterator
Definition flat_set.hpp:409
constexpr auto erase(const_iterator first, const_iterator last) -> iterator
Definition flat_set.hpp:271
friend constexpr auto operator==(flat_set const &lhs, flat_set const &rhs) -> bool
Definition flat_set.hpp:438
constexpr auto erase(iterator position) -> iterator
Definition flat_set.hpp:253
constexpr auto insert(value_type &&x) -> etl::pair< iterator, bool >
Definition flat_set.hpp:214
constexpr auto find(key_type const &key) const -> const_iterator
Definition flat_set.hpp:311
constexpr auto crbegin() const noexcept -> const_reverse_iterator
Definition flat_set.hpp:152
constexpr auto insert(InputIt first, InputIt last) -> void
Definition flat_set.hpp:230
constexpr auto upper_bound(K const &key) -> iterator
Definition flat_set.hpp:402
constexpr auto upper_bound(key_type const &key) -> iterator
Definition flat_set.hpp:390
constexpr auto contains(key_type const &key) const -> bool
Definition flat_set.hpp:354
constexpr auto rbegin() const noexcept -> const_reverse_iterator
Definition flat_set.hpp:148
constexpr auto contains(K const &key) const -> bool
Definition flat_set.hpp:361
friend constexpr auto operator<=(flat_set const &x, flat_set const &y) -> bool
Definition flat_set.hpp:453
constexpr auto lower_bound(key_type const &key) -> iterator
Definition flat_set.hpp:366
friend constexpr auto operator>(flat_set const &x, flat_set const &y) -> bool
Definition flat_set.hpp:448
constexpr auto equal_range(key_type const &key) -> etl::pair< iterator, iterator >
Definition flat_set.hpp:414
constexpr auto insert(value_type const &x) -> etl::pair< iterator, bool >
Definition flat_set.hpp:209
constexpr auto lower_bound(key_type const &key) const -> const_iterator
Definition flat_set.hpp:371
constexpr auto rend() noexcept -> reverse_iterator
Definition flat_set.hpp:157
constexpr auto begin() noexcept -> iterator
Definition flat_set.hpp:118
constexpr flat_set(etl::sorted_unique_t, container_type cont)
Definition flat_set.hpp:91
constexpr auto end() noexcept -> iterator
Definition flat_set.hpp:131
constexpr auto begin() const noexcept -> const_iterator
Definition flat_set.hpp:122
constexpr auto replace(container_type &&container) -> void
Definition flat_set.hpp:248
constexpr auto cbegin() const noexcept -> const_iterator
Definition flat_set.hpp:126
constexpr auto count(K const &key) const -> size_type
Definition flat_set.hpp:349
constexpr auto find(key_type const &key) -> iterator
Definition flat_set.hpp:302
friend constexpr auto operator<(flat_set const &lhs, flat_set const &rhs) -> bool
Definition flat_set.hpp:443
constexpr flat_set(InputIt first, InputIt last, Compare const &comp=Compare())
Definition flat_set.hpp:104
constexpr auto empty() const noexcept -> bool
Returns true if the underlying container is empty.
Definition flat_set.hpp:171
constexpr auto clear() noexcept -> void
Definition flat_set.hpp:285
constexpr auto lower_bound(K const &key) -> iterator
Definition flat_set.hpp:378
constexpr auto value_comp() const -> value_compare
Definition flat_set.hpp:296
constexpr flat_set(etl::sorted_unique_t, InputIt first, InputIt last, Compare const &comp=Compare())
Definition flat_set.hpp:112
constexpr auto swap(flat_set &other) noexcept(etl::is_nothrow_swappable_v< Container > &&etl::is_nothrow_swappable_v< Compare >) -> void
Definition flat_set.hpp:277
constexpr auto equal_range(key_type const &key) const -> etl::pair< const_iterator, const_iterator >
Definition flat_set.hpp:419
constexpr flat_set(Compare const &comp)
Definition flat_set.hpp:97
constexpr auto max_size() const noexcept -> size_type
Returns the max_size of the underlying container.
Definition flat_set.hpp:183
constexpr auto key_comp() const -> key_compare
Definition flat_set.hpp:291
friend constexpr auto swap(flat_set &x, flat_set &y) noexcept(noexcept(x.swap(y))) -> void
Definition flat_set.hpp:463
constexpr auto find(K const &key) const -> const_iterator
Definition flat_set.hpp:333
constexpr auto end() const noexcept -> const_iterator
Definition flat_set.hpp:135
constexpr auto emplace(Args &&... args) -> etl::pair< iterator, bool >
Definition flat_set.hpp:190
constexpr auto count(key_type const &key) const -> size_type
Definition flat_set.hpp:342
constexpr auto crend() const noexcept -> const_reverse_iterator
Definition flat_set.hpp:165
constexpr auto equal_range(K const &key) -> etl::pair< iterator, iterator >
Definition flat_set.hpp:426
constexpr auto insert(const_iterator position, value_type const &x) -> iterator
Definition flat_set.hpp:219
constexpr auto extract() &&-> container_type
Definition flat_set.hpp:241
constexpr auto upper_bound(key_type const &key) const -> const_iterator
Definition flat_set.hpp:395
constexpr auto rend() const noexcept -> const_reverse_iterator
Definition flat_set.hpp:161
constexpr auto equal_range(K const &key) const -> etl::pair< const_iterator, const_iterator >
Definition flat_set.hpp:433
friend constexpr auto operator>=(flat_set const &x, flat_set const &y) -> bool
Definition flat_set.hpp:458
constexpr auto erase(const_iterator position) -> iterator
Definition flat_set.hpp:258
constexpr auto size() const noexcept -> size_type
Returns the size of the underlying container.
Definition flat_set.hpp:177
constexpr auto cend() const noexcept -> const_iterator
Definition flat_set.hpp:139
constexpr auto find(K const &key) -> iterator
Definition flat_set.hpp:322
constexpr auto insert(const_iterator position, value_type &&x) -> iterator
Definition flat_set.hpp:224
constexpr auto emplace_hint(const_iterator, Args &&... args) -> iterator
Definition flat_set.hpp:204
constexpr auto rbegin() noexcept -> reverse_iterator
Definition flat_set.hpp:144
constexpr flat_set(container_type const &container)
Initializes c with etl::move(cont), value-initializes compare, sorts the range [begin(),...
Definition flat_set.hpp:85
constexpr auto insert(etl::sorted_unique_t, InputIt first, InputIt last) -> void
constexpr flat_set()
Definition flat_set.hpp:74
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
Definition sorted_unique.hpp:9