31#ifndef ETL_CONST_SET_INCLUDED
32#define ETL_CONST_SET_INCLUDED
51 template <
typename TKey,
typename TKeyCompare>
56 using key_type = TKey;
57 using value_type = TKey;
58 using key_compare = TKeyCompare;
59 using value_compare = TKeyCompare;
60 using const_reference =
const value_type&;
61 using const_pointer =
const value_type*;
62 using const_iterator =
const value_type*;
63 using size_type = size_t;
70 ETL_CONSTEXPR14
bool is_valid() const ETL_NOEXCEPT
78 ETL_CONSTEXPR14 const_iterator
begin() const ETL_NOEXCEPT
86 ETL_CONSTEXPR14 const_iterator
cbegin() const ETL_NOEXCEPT
94 ETL_CONSTEXPR14 const_iterator
end() const ETL_NOEXCEPT
96 return element_list_end;
102 ETL_CONSTEXPR14 const_iterator
cend() const ETL_NOEXCEPT
104 return element_list_end;
110 ETL_CONSTEXPR14 const_pointer
data() const ETL_NOEXCEPT
121 ETL_CONSTEXPR14 const_iterator find(
const key_type& key)
const ETL_NOEXCEPT
123 const_iterator itr = lower_bound(key);
125 if ((itr !=
end()) && (keys_are_equal(*itr, key)))
140 template <typename K, typename KC = TKeyCompare, etl::enable_if_t<comparator_is_transparent<KC>::value,
int> = 0>
141 ETL_CONSTEXPR14 const_iterator find(
const K& key)
const ETL_NOEXCEPT
143 const_iterator itr = lower_bound(key);
145 if ((itr !=
end()) && (keys_are_equal(*itr, key)))
158 ETL_CONSTEXPR14
bool contains(
const key_type& key)
const ETL_NOEXCEPT
160 return find(key) !=
end();
169 template <typename K, typename KC = TKeyCompare, etl::enable_if_t<comparator_is_transparent<KC>::value,
int> = 0>
170 ETL_CONSTEXPR14
bool contains(
const K& key)
const ETL_NOEXCEPT
172 return find(key) !=
end();
180 ETL_CONSTEXPR14 size_type count(
const key_type& key)
const ETL_NOEXCEPT
182 return contains(key) ? 1 : 0;
191 template <typename K, typename KC = TKeyCompare, etl::enable_if_t<comparator_is_transparent<KC>::value,
int> = 0>
192 ETL_CONSTEXPR14 size_type count(
const K& key)
const ETL_NOEXCEPT
194 return contains(key) ? 1 : 0;
205 ETL_CONSTEXPR14 ETL_OR_STD::pair<const_iterator, const_iterator> equal_range(
const key_type& key)
const ETL_NOEXCEPT
207 return etl::equal_range(
begin(),
end(), key, vcompare);
219 template <typename K, typename KC = TKeyCompare, etl::enable_if_t<comparator_is_transparent<KC>::value,
int> = 0>
220 ETL_CONSTEXPR14 ETL_OR_STD::pair<const_iterator, const_iterator> equal_range(
const K& key)
const ETL_NOEXCEPT
222 return etl::equal_range(
begin(),
end(), key, vcompare);
233 ETL_CONSTEXPR14 const_iterator lower_bound(
const key_type& key)
const ETL_NOEXCEPT
235 return etl::lower_bound(
begin(),
end(), key, vcompare);
246 template <typename K, typename KC = TKeyCompare, etl::enable_if_t<comparator_is_transparent<KC>::value,
int> = 0>
247 ETL_CONSTEXPR14 const_iterator lower_bound(
const K& key)
const ETL_NOEXCEPT
249 return etl::lower_bound(
begin(),
end(), key, vcompare);
260 ETL_CONSTEXPR14 const_iterator upper_bound(
const key_type& key)
const ETL_NOEXCEPT
262 return etl::upper_bound(
begin(),
end(), key, vcompare);
273 template <typename K, typename KC = TKeyCompare, etl::enable_if_t<comparator_is_transparent<KC>::value,
int> = 0>
274 ETL_CONSTEXPR14 const_iterator upper_bound(
const K& key)
const ETL_NOEXCEPT
276 return etl::upper_bound(
begin(),
end(), key, vcompare);
283 ETL_CONSTEXPR14 size_type empty() const ETL_NOEXCEPT
292 ETL_CONSTEXPR14 size_type full() const ETL_NOEXCEPT
294 return (max_elements != 0) && (
size() == max_elements);
301 ETL_CONSTEXPR14 size_type
size() const ETL_NOEXCEPT
303 return size_type(element_list_end - element_list);
310 ETL_CONSTEXPR14 size_type max_size() const ETL_NOEXCEPT
320 ETL_CONSTEXPR14 size_type capacity() const ETL_NOEXCEPT
329 ETL_CONSTEXPR14 key_compare key_comp() const ETL_NOEXCEPT
331 return key_compare();
338 ETL_CONSTEXPR14 value_compare value_comp() const ETL_NOEXCEPT
340 return value_compare();
348 template <
typename... TElements>
349 ETL_CONSTEXPR14
explicit iconst_set(
const value_type* element_list_, size_type size_, size_type max_elements_) ETL_NOEXCEPT
350 : element_list(element_list_)
351 , element_list_end{element_list_ + size_}
352 , max_elements(max_elements_)
361 ETL_CONSTEXPR14
bool keys_are_equal(const_reference key1, const_reference key2)
const ETL_NOEXCEPT
363 return !key_compare()(key1, key2) && !key_compare()(key2, key1);
370 template <typename K1, typename K2, typename KC = TKeyCompare, etl::enable_if_t<comparator_is_transparent<KC>::value,
int> = 0>
371 ETL_CONSTEXPR14
bool keys_are_equal(
const K1& key1,
const K2& key2)
const ETL_NOEXCEPT
373 return !key_compare()(key1, key2) && !key_compare()(key2, key1);
376 key_compare kcompare;
377 value_compare vcompare;
379 const value_type* element_list;
380 const value_type* element_list_end;
381 size_type max_elements;
387 template <
typename TKey,
size_t Size,
typename TKeyCompare = etl::less<TKey>>
388 class const_set :
public iconst_set<TKey, TKeyCompare>
392 using base_t = iconst_set<TKey, TKeyCompare>;
394 using key_type =
typename base_t::key_type;
395 using value_type =
typename base_t::value_type;
396 using key_compare =
typename base_t::key_compare;
397 using const_reference =
typename base_t::const_reference;
398 using const_pointer =
typename base_t::const_pointer;
399 using const_iterator =
typename base_t::const_iterator;
400 using size_type =
typename base_t::size_type;
403 using const_key_reference =
const key_type&;
405 static_assert((etl::is_default_constructible<key_type>::value),
"key_type must be default constructible");
413 template <
typename... TElements>
414 ETL_CONSTEXPR14
explicit const_set(TElements&&... elements) ETL_NOEXCEPT
415 : iconst_set<TKey, TKeyCompare>(element_list,
sizeof...(elements), Size)
416 , element_list{etl::forward<TElements>(elements)...}
418 static_assert((etl::are_all_same<value_type, etl::decay_t<TElements>...>::value),
"All elements must be key_type");
419 static_assert(
sizeof...(elements) <= Size,
"Number of elements exceeds capacity");
424 value_type element_list[Size];
431 template <
typename... TElements>
432 const_set(TElements...) -> const_set<etl::nth_type_t<0, TElements...>,
sizeof...(TElements)>;
438 template <
typename TKey,
typename TKeyCompare = etl::less<TKey>>
439 class const_set_ext :
public iconst_set<TKey, TKeyCompare>
443 using base_t = iconst_set<TKey, TKeyCompare>;
445 using key_type =
typename base_t::key_type;
446 using value_type =
typename base_t::value_type;
447 using key_compare =
typename base_t::key_compare;
448 using const_reference =
typename base_t::const_reference;
449 using const_pointer =
typename base_t::const_pointer;
450 using const_iterator =
typename base_t::const_iterator;
451 using size_type =
typename base_t::size_type;
453 static_assert((etl::is_default_constructible<key_type>::value),
"key_type must be default constructible");
458 ETL_CONSTEXPR14 const_set_ext() ETL_NOEXCEPT
459 : iconst_set<TKey, TKeyCompare>(
nullptr, 0, 0)
466 template <
size_type Size>
467 ETL_CONSTEXPR14
explicit const_set_ext(
const etl::span<const value_type, Size>& sp) ETL_NOEXCEPT
468 : iconst_set<TKey, TKeyCompare>(sp.data(), Size, Size)
475 template <
size_type Size>
476 ETL_CONSTEXPR14
explicit const_set_ext(
const value_type (&begin_)[Size]) ETL_NOEXCEPT
477 : iconst_set<TKey, TKeyCompare>(begin_, Size, Size)
486 template <
typename TElements,
size_t Size>
487 const_set_ext(
const etl::span<TElements, Size>&) -> const_set_ext<TElements>;
489 template <
typename TElements,
size_t Size>
490 const_set_ext(
const TElements (&)[Size]) -> const_set_ext<TElements>;
496 template <
typename TKey,
typename TKeyCompare>
497 ETL_CONSTEXPR14
bool operator==(
const etl::iconst_set<TKey, TKeyCompare>& lhs,
const etl::iconst_set<TKey, TKeyCompare>& rhs) ETL_NOEXCEPT
499 return (lhs.size() == rhs.size()) && etl::equal(lhs.begin(), lhs.end(), rhs.begin());
505 template <
typename TKey,
typename TKeyCompare>
506 ETL_CONSTEXPR14
bool operator!=(
const etl::iconst_set<TKey, TKeyCompare>& lhs,
const etl::iconst_set<TKey, TKeyCompare>& rhs) ETL_NOEXCEPT
508 return !(lhs == rhs);
514 template <
typename TKey,
typename TKeyCompare>
515 ETL_CONSTEXPR14
bool operator<(
const etl::iconst_set<TKey, TKeyCompare>& lhs,
const etl::iconst_set<TKey, TKeyCompare>& rhs) ETL_NOEXCEPT
517 return etl::lexicographical_compare(lhs.begin(), lhs.end(), rhs.begin(), rhs.end(), lhs.value_comp());
523 template <
typename TKey,
typename TKeyCompare>
524 ETL_CONSTEXPR14
bool operator>(
const etl::iconst_set<TKey, TKeyCompare>& lhs,
const etl::iconst_set<TKey, TKeyCompare>& rhs) ETL_NOEXCEPT
532 template <
typename TKey,
typename TKeyCompare>
533 ETL_CONSTEXPR14
bool operator<=(
const etl::iconst_set<TKey, TKeyCompare>& lhs,
const etl::iconst_set<TKey, TKeyCompare>& rhs) ETL_NOEXCEPT
541 template <
typename TKey,
typename TKeyCompare>
542 ETL_CONSTEXPR14
bool operator>=(
const etl::iconst_set<TKey, TKeyCompare>& lhs,
const etl::iconst_set<TKey, TKeyCompare>& rhs) ETL_NOEXCEPT
ETL_NODISCARD ETL_CONSTEXPR14 bool is_unique_sorted(TIterator begin, TIterator end)
Definition algorithm.h:1727
ETL_CONSTEXPR14 bool operator==(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition array.h:1078
ETL_CONSTEXPR TContainer::pointer data(TContainer &container)
Definition iterator.h:1470
bool operator>(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition array.h:1130
TContainer::const_iterator cbegin(const TContainer &container)
Definition iterator.h:1156
bool operator>=(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition array.h:1144
ETL_CONSTEXPR14 bool operator!=(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition array.h:1090
ETL_CONSTEXPR TContainer::size_type size(const TContainer &container)
Definition iterator.h:1434
TContainer::iterator end(TContainer &container)
Definition iterator.h:1166
TContainer::const_iterator cend(const TContainer &container)
Definition iterator.h:1186
TContainer::iterator begin(TContainer &container)
Definition iterator.h:1136
bool operator<(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition array.h:1103
bool operator<=(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition array.h:1117