31#ifndef ETL_CONST_MAP_INCLUDED
32#define ETL_CONST_MAP_INCLUDED
51 template <
typename TKey,
typename TMapped,
typename TKeyCompare>
56 using key_type = TKey;
57 using value_type = ETL_OR_STD::pair<const TKey, TMapped>;
58 using mapped_type = TMapped;
59 using key_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;
73 ETL_CONSTEXPR14
bool operator()(
const value_type& element1,
const value_type& element2)
const ETL_NOEXCEPT
75 return kcompare(element1.first, element2.first);
79 ETL_CONSTEXPR14
bool operator()(
const value_type& element,
const key_type& key)
const ETL_NOEXCEPT
81 return kcompare(element.first, key);
86 template <typename K, typename KC = TKeyCompare, etl::enable_if_t<comparator_is_transparent<KC>::value,
int> = 0>
87 ETL_CONSTEXPR14
bool operator()(
const value_type& element,
const K& key)
const ETL_NOEXCEPT
89 return kcompare(element.first, key);
93 ETL_CONSTEXPR14
bool operator()(
const key_type& key,
const value_type& element)
const ETL_NOEXCEPT
95 return kcompare(key, element.first);
100 template <typename K, typename KC = TKeyCompare, etl::enable_if_t<comparator_is_transparent<KC>::value,
int> = 0>
101 ETL_CONSTEXPR14
bool operator()(
const K& key,
const value_type& element)
const ETL_NOEXCEPT
103 return kcompare(key, element.first);
106 key_compare kcompare;
114 ETL_CONSTEXPR14
bool is_valid() const ETL_NOEXCEPT
122 ETL_CONSTEXPR14 const_iterator
begin() const ETL_NOEXCEPT
130 ETL_CONSTEXPR14 const_iterator
cbegin() const ETL_NOEXCEPT
138 ETL_CONSTEXPR14 const_iterator
end() const ETL_NOEXCEPT
140 return element_list_end;
146 ETL_CONSTEXPR14 const_iterator
cend() const ETL_NOEXCEPT
148 return element_list_end;
154 ETL_CONSTEXPR14 const_pointer
data() const ETL_NOEXCEPT
166 ETL_CONSTEXPR14
const mapped_type& operator[](
const key_type& key)
const ETL_NOEXCEPT
168 const_iterator itr = find(key);
181 template <typename K, typename KC = TKeyCompare, etl::enable_if_t<comparator_is_transparent<KC>::value,
int> = 0>
182 ETL_CONSTEXPR14
const mapped_type& operator[](
const K& key)
const ETL_NOEXCEPT
184 const_iterator itr = find(key);
196 ETL_CONSTEXPR14
const mapped_type& at(
const key_type& key)
const ETL_NOEXCEPT
198 const_iterator itr = find(key);
211 template <typename K, typename KC = TKeyCompare, etl::enable_if_t<comparator_is_transparent<KC>::value,
int> = 0>
212 ETL_CONSTEXPR14
const mapped_type& at(
const K& key)
const ETL_NOEXCEPT
214 const_iterator itr = find(key);
225 ETL_CONSTEXPR14 const_iterator find(
const key_type& key)
const ETL_NOEXCEPT
227 const_iterator itr = lower_bound(key);
229 if ((itr !=
end()) && (keys_are_equal(itr->first, key)))
244 template <typename K, typename KC = TKeyCompare, etl::enable_if_t<comparator_is_transparent<KC>::value,
int> = 0>
245 ETL_CONSTEXPR14 const_iterator find(
const K& key)
const ETL_NOEXCEPT
247 const_iterator itr = lower_bound(key);
249 if ((itr !=
end()) && (keys_are_equal(itr->first, key)))
262 ETL_CONSTEXPR14
bool contains(
const key_type& key)
const ETL_NOEXCEPT
264 return find(key) !=
end();
273 template <typename K, typename KC = TKeyCompare, etl::enable_if_t<comparator_is_transparent<KC>::value,
int> = 0>
274 ETL_CONSTEXPR14
bool contains(
const K& key)
const ETL_NOEXCEPT
276 return find(key) !=
end();
284 ETL_CONSTEXPR14 size_type count(
const key_type& key)
const ETL_NOEXCEPT
286 return contains(key) ? 1 : 0;
295 template <typename K, typename KC = TKeyCompare, etl::enable_if_t<comparator_is_transparent<KC>::value,
int> = 0>
296 ETL_CONSTEXPR14 size_type count(
const K& key)
const ETL_NOEXCEPT
298 return contains(key) ? 1 : 0;
309 ETL_CONSTEXPR14 ETL_OR_STD::pair<const_iterator, const_iterator> equal_range(
const key_type& key)
const ETL_NOEXCEPT
311 return etl::equal_range(
begin(),
end(), key, vcompare);
323 template <typename K, typename KC = TKeyCompare, etl::enable_if_t<comparator_is_transparent<KC>::value,
int> = 0>
324 ETL_CONSTEXPR14 ETL_OR_STD::pair<const_iterator, const_iterator> equal_range(
const K& key)
const ETL_NOEXCEPT
326 return etl::equal_range(
begin(),
end(), key, vcompare);
337 ETL_CONSTEXPR14 const_iterator lower_bound(
const key_type& key)
const ETL_NOEXCEPT
339 return etl::lower_bound(
begin(),
end(), key, vcompare);
350 template <typename K, typename KC = TKeyCompare, etl::enable_if_t<comparator_is_transparent<KC>::value,
int> = 0>
351 ETL_CONSTEXPR14 const_iterator lower_bound(
const K& key)
const ETL_NOEXCEPT
353 return etl::lower_bound(
begin(),
end(), key, vcompare);
364 ETL_CONSTEXPR14 const_iterator upper_bound(
const key_type& key)
const ETL_NOEXCEPT
366 return etl::upper_bound(
begin(),
end(), key, vcompare);
377 template <typename K, typename KC = TKeyCompare, etl::enable_if_t<comparator_is_transparent<KC>::value,
int> = 0>
378 ETL_CONSTEXPR14 const_iterator upper_bound(
const K& key)
const ETL_NOEXCEPT
380 return etl::upper_bound(
begin(),
end(), key, vcompare);
387 ETL_CONSTEXPR14 size_type empty() const ETL_NOEXCEPT
396 ETL_CONSTEXPR14 size_type full() const ETL_NOEXCEPT
398 return (max_elements != 0) && (
size() == max_elements);
405 ETL_CONSTEXPR14 size_type
size() const ETL_NOEXCEPT
407 return size_type(element_list_end - element_list);
414 ETL_CONSTEXPR14 size_type max_size() const ETL_NOEXCEPT
424 ETL_CONSTEXPR14 size_type capacity() const ETL_NOEXCEPT
433 ETL_CONSTEXPR14 key_compare key_comp() const ETL_NOEXCEPT
435 return vcompare.kcompare;
442 ETL_CONSTEXPR14 value_compare value_comp() const ETL_NOEXCEPT
452 template <
typename... TElements>
453 ETL_CONSTEXPR14
explicit iconst_map(
const value_type* element_list_, size_type size_, size_type max_elements_) ETL_NOEXCEPT
454 : element_list(element_list_)
455 , element_list_end{element_list_ + size_}
456 , max_elements(max_elements_)
465 ETL_CONSTEXPR14
bool keys_are_equal(
const key_type& key1,
const key_type& key2)
const ETL_NOEXCEPT
467 return !key_compare()(key1, key2) && !key_compare()(key2, key1);
474 template <typename K1, typename K2, typename KC = TKeyCompare, etl::enable_if_t<comparator_is_transparent<KC>::value,
int> = 0>
475 ETL_CONSTEXPR14
bool keys_are_equal(
const K1& key1,
const K2& key2)
const ETL_NOEXCEPT
477 return !key_compare()(key1, key2) && !key_compare()(key2, key1);
480 value_compare vcompare;
482 const value_type* element_list;
483 const value_type* element_list_end;
484 size_type max_elements;
490 template <
typename TKey,
typename TMapped,
size_t Size,
typename TKeyCompare = etl::less<TKey>>
491 class const_map :
public iconst_map<TKey, TMapped, TKeyCompare>
495 using base_t = iconst_map<TKey, TMapped, TKeyCompare>;
497 using key_type =
typename base_t::key_type;
498 using value_type =
typename base_t::value_type;
499 using mapped_type =
typename base_t::mapped_type;
500 using key_compare =
typename base_t::key_compare;
501 using const_reference =
typename base_t::const_reference;
502 using const_pointer =
typename base_t::const_pointer;
503 using const_iterator =
typename base_t::const_iterator;
504 using size_type =
typename base_t::size_type;
506 static_assert((etl::is_default_constructible<key_type>::value),
"key_type must be default constructible");
507 static_assert((etl::is_default_constructible<mapped_type>::value),
"mapped_type must be default constructible");
515 template <
typename... TElements>
516 ETL_CONSTEXPR14
explicit const_map(TElements&&... elements) ETL_NOEXCEPT
517 : iconst_map<TKey, TMapped, TKeyCompare>(element_list,
sizeof...(elements), Size)
518 , element_list{etl::forward<TElements>(elements)...}
520 static_assert((etl::are_all_same<value_type, etl::decay_t<TElements>...>::value),
"All elements must be value_type");
521 static_assert(
sizeof...(elements) <= Size,
"Number of elements exceeds capacity");
526 value_type element_list[Size];
533 template <
typename... TElements>
534 const_map(TElements...)
535 -> const_map<
typename etl::nth_type_t<0, TElements...>::first_type,
typename etl::nth_type_t<0, TElements...>::second_type,
sizeof...(TElements)>;
541 template <
typename TKey,
typename TMapped,
typename TKeyCompare = etl::less<TKey>>
542 class const_map_ext :
public iconst_map<TKey, TMapped, TKeyCompare>
546 using base_t = iconst_map<TKey, TMapped, TKeyCompare>;
548 using key_type =
typename base_t::key_type;
549 using value_type =
typename base_t::value_type;
550 using mapped_type =
typename base_t::mapped_type;
551 using key_compare =
typename base_t::key_compare;
552 using const_reference =
typename base_t::const_reference;
553 using const_pointer =
typename base_t::const_pointer;
554 using const_iterator =
typename base_t::const_iterator;
555 using size_type =
typename base_t::size_type;
557 static_assert((etl::is_default_constructible<key_type>::value),
"key_type must be default constructible");
558 static_assert((etl::is_default_constructible<mapped_type>::value),
"mapped_type must be default constructible");
563 ETL_CONSTEXPR14 const_map_ext() ETL_NOEXCEPT
564 : iconst_map<TKey, TMapped, TKeyCompare>(
nullptr, 0, 0)
571 template <
size_type Size>
572 ETL_CONSTEXPR14
explicit const_map_ext(
const etl::span<const value_type, Size>& sp) ETL_NOEXCEPT
573 : iconst_map<TKey, TMapped, TKeyCompare>(sp.data(), Size, Size)
580 template <
size_type Size>
581 ETL_CONSTEXPR14
explicit const_map_ext(
const value_type (&begin_)[Size]) ETL_NOEXCEPT
582 : iconst_map<TKey, TMapped, TKeyCompare>(begin_, Size, Size)
591 template <
typename TElements,
size_t Size>
592 const_map_ext(
const etl::span<TElements, Size>&) -> const_map_ext<typename TElements::first_type, typename TElements::second_type>;
594 template <
typename TElements,
size_t Size>
595 const_map_ext(
const TElements (&)[Size]) -> const_map_ext<typename TElements::first_type, typename TElements::second_type>;
601 template <
typename TKey,
typename TMapped,
typename TKeyCompare>
602 ETL_CONSTEXPR14
bool operator==(
const etl::iconst_map<TKey, TMapped, TKeyCompare>& lhs,
const etl::iconst_map<TKey, TMapped, TKeyCompare>& rhs)
605 return (lhs.size() == rhs.size()) && etl::equal(lhs.begin(), lhs.end(), rhs.begin());
611 template <
typename TKey,
typename TMapped,
typename TKeyCompare>
612 ETL_CONSTEXPR14
bool operator!=(
const etl::iconst_map<TKey, TMapped, TKeyCompare>& lhs,
const etl::iconst_map<TKey, TMapped, TKeyCompare>& rhs)
615 return !(lhs == rhs);
621 template <
typename TKey,
typename TMapped,
typename TKeyCompare>
622 ETL_CONSTEXPR14
bool operator<(
const etl::iconst_map<TKey, TMapped, TKeyCompare>& lhs,
const etl::iconst_map<TKey, TMapped, TKeyCompare>& rhs)
625 return etl::lexicographical_compare(lhs.begin(), lhs.end(), rhs.begin(), rhs.end(), lhs.value_comp());
631 template <
typename TKey,
typename TMapped,
typename TKeyCompare>
632 ETL_CONSTEXPR14
bool operator>(
const etl::iconst_map<TKey, TMapped, TKeyCompare>& lhs,
const etl::iconst_map<TKey, TMapped, TKeyCompare>& rhs)
641 template <
typename TKey,
typename TMapped,
typename TKeyCompare>
642 ETL_CONSTEXPR14
bool operator<=(
const etl::iconst_map<TKey, TMapped, TKeyCompare>& lhs,
const etl::iconst_map<TKey, TMapped, TKeyCompare>& rhs)
651 template <
typename TKey,
typename TMapped,
typename TKeyCompare>
652 ETL_CONSTEXPR14
bool operator>=(
const etl::iconst_map<TKey, TMapped, TKeyCompare>& lhs,
const etl::iconst_map<TKey, TMapped, TKeyCompare>& rhs)
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