Loading...
Searching...
No Matches
const_multimap.h
Go to the documentation of this file.
/******************************************************************************
The MIT License(MIT)
Embedded Template Library.
https://github.com/ETLCPP/etl
https://www.etlcpp.com
Copyright(c) 2025 John Wellbelove
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files(the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions :
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
******************************************************************************/
#ifndef ETL_CONST_MULTIMAP_INCLUDED
#define ETL_CONST_MULTIMAP_INCLUDED
#include "platform.h"
#include "algorithm.h"
#include "functional.h"
#include "nth_type.h"
#include "span.h"
#include "type_traits.h"
#include "private/comparator_is_transparent.h"
#if ETL_USING_CPP11
namespace etl
{
template <typename TKey, typename TMapped, typename TKeyCompare = etl::less<TKey>>
class iconst_multimap
{
public:
using key_type = TKey;
using value_type = ETL_OR_STD::pair<const TKey, TMapped>;
using mapped_type = TMapped;
using key_compare = TKeyCompare;
using const_reference = const value_type&;
using const_pointer = const value_type*;
using const_iterator = const value_type*;
using size_type = size_t;
//*********************************************************************
//*********************************************************************
class value_compare
{
public:
// Compare two value types.
ETL_CONSTEXPR14 bool operator()(const value_type& element1, const value_type& element2) const ETL_NOEXCEPT
{
return kcompare(element1.first, element2.first);
}
// Compare value type and key.
ETL_CONSTEXPR14 bool operator()(const value_type& element, const key_type& key) const ETL_NOEXCEPT
{
return kcompare(element.first, key);
}
// Compare value types and key.
// Enabled for transparent comparators.
template <typename K, typename KC = TKeyCompare, etl::enable_if_t<comparator_is_transparent<KC>::value, int> = 0>
ETL_CONSTEXPR14 bool operator()(const value_type& element, const K& key) const ETL_NOEXCEPT
{
return kcompare(element.first, key);
}
// Compare key and value type.
ETL_CONSTEXPR14 bool operator()(const key_type& key, const value_type& element) const ETL_NOEXCEPT
{
return kcompare(key, element.first);
}
// Compare key and value type.
// Enabled for transparent comparators.
template <typename K, typename KC = TKeyCompare, etl::enable_if_t<comparator_is_transparent<KC>::value, int> = 0>
ETL_CONSTEXPR14 bool operator()(const K& key, const value_type& element) const ETL_NOEXCEPT
{
return kcompare(key, element.first);
}
key_compare kcompare;
};
//*************************************************************************
//*************************************************************************
ETL_CONSTEXPR14 bool is_valid() const ETL_NOEXCEPT
{
return etl::is_sorted(begin(), end(), vcompare);
}
//*************************************************************************
//*************************************************************************
ETL_CONSTEXPR14 const_iterator begin() const ETL_NOEXCEPT
{
return element_list;
}
//*************************************************************************
//*************************************************************************
ETL_CONSTEXPR14 const_iterator cbegin() const ETL_NOEXCEPT
{
return element_list;
}
//*************************************************************************
//*************************************************************************
ETL_CONSTEXPR14 const_iterator end() const ETL_NOEXCEPT
{
return element_list_end;
}
//*************************************************************************
//*************************************************************************
ETL_CONSTEXPR14 const_iterator cend() const ETL_NOEXCEPT
{
return element_list_end;
}
//*************************************************************************
//*************************************************************************
ETL_CONSTEXPR14 const_pointer data() const ETL_NOEXCEPT
{
return element_list;
}
//*************************************************************************
//*************************************************************************
ETL_CONSTEXPR14 const_iterator find(const key_type& key) const ETL_NOEXCEPT
{
const_iterator itr = lower_bound(key);
if ((itr != end()) && (keys_are_equal(itr->first, key)))
{
return itr;
}
return end();
}
//*************************************************************************
//*************************************************************************
template <typename K, typename KC = TKeyCompare, etl::enable_if_t<comparator_is_transparent<KC>::value, int> = 0>
ETL_CONSTEXPR14 const_iterator find(const K& key) const ETL_NOEXCEPT
{
const_iterator itr = lower_bound(key);
if ((itr != end()) && (keys_are_equal(itr->first, key)))
{
return itr;
}
return end();
}
//*************************************************************************
//*************************************************************************
ETL_CONSTEXPR14 bool contains(const key_type& key) const ETL_NOEXCEPT
{
return find(key) != end();
}
//*************************************************************************
//*************************************************************************
template <typename K, typename KC = TKeyCompare, etl::enable_if_t<comparator_is_transparent<KC>::value, int> = 0>
ETL_CONSTEXPR14 bool contains(const K& key) const ETL_NOEXCEPT
{
return find(key) != end();
}
//*************************************************************************
//*************************************************************************
ETL_CONSTEXPR14 size_type count(const key_type& key) const ETL_NOEXCEPT
{
auto range = equal_range(key);
return size_type(etl::distance(range.first, range.second));
}
//*************************************************************************
//*************************************************************************
template <typename K, typename KC = TKeyCompare, etl::enable_if_t<comparator_is_transparent<KC>::value, int> = 0>
ETL_CONSTEXPR14 size_type count(const K& key) const ETL_NOEXCEPT
{
auto range = equal_range(key);
return size_type(etl::distance(range.first, range.second));
}
//*************************************************************************
//*************************************************************************
ETL_CONSTEXPR14 ETL_OR_STD::pair<const_iterator, const_iterator> equal_range(const key_type& key) const ETL_NOEXCEPT
{
return etl::equal_range(begin(), end(), key, vcompare);
}
//*************************************************************************
//*************************************************************************
template <typename K, typename KC = TKeyCompare, etl::enable_if_t<comparator_is_transparent<KC>::value, int> = 0>
ETL_CONSTEXPR14 ETL_OR_STD::pair<const_iterator, const_iterator> equal_range(const K& key) const ETL_NOEXCEPT
{
return etl::equal_range(begin(), end(), key, vcompare);
}
//*************************************************************************
//*************************************************************************
ETL_CONSTEXPR14 const_iterator lower_bound(const key_type& key) const ETL_NOEXCEPT
{
return etl::lower_bound(begin(), end(), key, vcompare);
}
//*************************************************************************
//*************************************************************************
template <typename K, typename KC = TKeyCompare, etl::enable_if_t<comparator_is_transparent<KC>::value, int> = 0>
ETL_CONSTEXPR14 const_iterator lower_bound(const K& key) const ETL_NOEXCEPT
{
return etl::lower_bound(begin(), end(), key, vcompare);
}
//*************************************************************************
//*************************************************************************
ETL_CONSTEXPR14 const_iterator upper_bound(const key_type& key) const ETL_NOEXCEPT
{
return etl::upper_bound(begin(), end(), key, vcompare);
}
//*************************************************************************
//*************************************************************************
template <typename K, typename KC = TKeyCompare, etl::enable_if_t<comparator_is_transparent<KC>::value, int> = 0>
ETL_CONSTEXPR14 const_iterator upper_bound(const K& key) const ETL_NOEXCEPT
{
return etl::upper_bound(begin(), end(), key, vcompare);
}
//*************************************************************************
//*************************************************************************
ETL_CONSTEXPR14 size_type empty() const ETL_NOEXCEPT
{
return size() == 0U;
}
//*************************************************************************
//*************************************************************************
ETL_CONSTEXPR14 size_type full() const ETL_NOEXCEPT
{
return (max_elements != 0) && (size() == max_elements);
}
//*************************************************************************
//*************************************************************************
ETL_CONSTEXPR14 size_type size() const ETL_NOEXCEPT
{
return size_type(element_list_end - element_list);
}
//*************************************************************************
//*************************************************************************
ETL_CONSTEXPR14 size_type max_size() const ETL_NOEXCEPT
{
return max_elements;
}
//*************************************************************************
//*************************************************************************
ETL_CONSTEXPR14 size_type capacity() const ETL_NOEXCEPT
{
return max_elements;
}
//*************************************************************************
//*************************************************************************
ETL_CONSTEXPR14 key_compare key_comp() const ETL_NOEXCEPT
{
return vcompare.kcompare;
}
//*************************************************************************
//*************************************************************************
ETL_CONSTEXPR14 value_compare value_comp() const ETL_NOEXCEPT
{
return vcompare;
}
protected:
//*************************************************************************
//*************************************************************************
template <typename... TElements>
ETL_CONSTEXPR14 explicit iconst_multimap(const value_type* element_list_, size_type size_, size_type max_elements_) ETL_NOEXCEPT
: element_list(element_list_)
, element_list_end{element_list_ + size_}
, max_elements(max_elements_)
{
}
private:
//*********************************************************************
//*********************************************************************
ETL_CONSTEXPR14 bool keys_are_equal(const key_type& key1, const key_type& key2) const ETL_NOEXCEPT
{
return !key_compare()(key1, key2) && !key_compare()(key2, key1);
}
//*********************************************************************
//*********************************************************************
template <typename K1, typename K2, typename KC = TKeyCompare, etl::enable_if_t<comparator_is_transparent<KC>::value, int> = 0>
ETL_CONSTEXPR14 bool keys_are_equal(const K1& key1, const K2& key2) const ETL_NOEXCEPT
{
return !key_compare()(key1, key2) && !key_compare()(key2, key1);
}
value_compare vcompare;
const value_type* element_list;
const value_type* element_list_end;
size_type max_elements;
};
//*************************************************************************
//*************************************************************************
template <typename TKey, typename TMapped, size_t Size, typename TKeyCompare = etl::less<TKey>>
class const_multimap : public iconst_multimap<TKey, TMapped, TKeyCompare>
{
public:
using base_t = iconst_multimap<TKey, TMapped, TKeyCompare>;
using key_type = typename base_t::key_type;
using value_type = typename base_t::value_type;
using mapped_type = typename base_t::mapped_type;
using key_compare = typename base_t::key_compare;
using const_reference = typename base_t::const_reference;
using const_pointer = typename base_t::const_pointer;
using const_iterator = typename base_t::const_iterator;
using size_type = typename base_t::size_type;
static_assert((etl::is_default_constructible<key_type>::value), "key_type must be default constructible");
static_assert((etl::is_default_constructible<mapped_type>::value), "mapped_type must be default constructible");
//*************************************************************************
//*************************************************************************
template <typename... TElements>
ETL_CONSTEXPR14 explicit const_multimap(TElements&&... elements) ETL_NOEXCEPT
: iconst_multimap<TKey, TMapped, TKeyCompare>(element_list, sizeof...(elements), Size)
, element_list{etl::forward<TElements>(elements)...}
{
static_assert((etl::are_all_same<value_type, etl::decay_t<TElements>...>::value), "All elements must be value_type");
static_assert(sizeof...(elements) <= Size, "Number of elements exceeds capacity");
}
private:
value_type element_list[Size];
};
//*************************************************************************
//*************************************************************************
#if ETL_USING_CPP17
template <typename... TPairs>
const_multimap(TPairs...)
-> const_multimap<typename etl::nth_type_t<0, TPairs...>::first_type, typename etl::nth_type_t<0, TPairs...>::second_type, sizeof...(TPairs)>;
#endif
//*********************************************************************
//*********************************************************************
template <typename TKey, typename TMapped, typename TKeyCompare = etl::less<TKey>>
class const_multimap_ext : public iconst_multimap<TKey, TMapped, TKeyCompare>
{
public:
using base_t = iconst_multimap<TKey, TMapped, TKeyCompare>;
using key_type = typename base_t::key_type;
using value_type = typename base_t::value_type;
using mapped_type = typename base_t::mapped_type;
using key_compare = typename base_t::key_compare;
using const_reference = typename base_t::const_reference;
using const_pointer = typename base_t::const_pointer;
using const_iterator = typename base_t::const_iterator;
using size_type = typename base_t::size_type;
static_assert((etl::is_default_constructible<key_type>::value), "key_type must be default constructible");
static_assert((etl::is_default_constructible<mapped_type>::value), "mapped_type must be default constructible");
//*************************************************************************
//*************************************************************************
ETL_CONSTEXPR14 const_multimap_ext() ETL_NOEXCEPT
: iconst_multimap<TKey, TMapped, TKeyCompare>(nullptr, 0, 0)
{
}
//*************************************************************************
//*************************************************************************
template <size_type Size>
ETL_CONSTEXPR14 explicit const_multimap_ext(const etl::span<const value_type, Size>& sp) ETL_NOEXCEPT
: iconst_multimap<TKey, TMapped, TKeyCompare>(sp.data(), Size, Size)
{
}
//*************************************************************************
//*************************************************************************
template <size_type Size>
ETL_CONSTEXPR14 explicit const_multimap_ext(const value_type (&begin_)[Size]) ETL_NOEXCEPT
: iconst_multimap<TKey, TMapped, TKeyCompare>(begin_, Size, Size)
{
}
};
//*************************************************************************
//*************************************************************************
#if ETL_USING_CPP17
template <typename TElements, size_t Size>
const_multimap_ext(const etl::span<TElements, Size>&) -> const_multimap_ext<typename TElements::first_type, typename TElements::second_type>;
template <typename TElements, size_t Size>
const_multimap_ext(const TElements (&)[Size]) -> const_multimap_ext<typename TElements::first_type, typename TElements::second_type>;
#endif
//*************************************************************************
//*************************************************************************
template <typename TKey, typename TMapped, typename TKeyCompare>
ETL_CONSTEXPR14 bool operator==(const etl::iconst_multimap<TKey, TMapped, TKeyCompare>& lhs,
const etl::iconst_multimap<TKey, TMapped, TKeyCompare>& rhs) ETL_NOEXCEPT
{
return etl::equal(lhs.begin(), lhs.end(), rhs.begin());
}
//*************************************************************************
//*************************************************************************
template <typename TKey, typename TMapped, typename TKeyCompare>
ETL_CONSTEXPR14 bool operator!=(const etl::iconst_multimap<TKey, TMapped, TKeyCompare>& lhs,
const etl::iconst_multimap<TKey, TMapped, TKeyCompare>& rhs) ETL_NOEXCEPT
{
return !(lhs == rhs);
}
//*************************************************************************
//*************************************************************************
template <typename TKey, typename TMapped, typename TKeyCompare>
ETL_CONSTEXPR14 bool operator<(const etl::iconst_multimap<TKey, TMapped, TKeyCompare>& lhs,
const etl::iconst_multimap<TKey, TMapped, TKeyCompare>& rhs) ETL_NOEXCEPT
{
return etl::lexicographical_compare(lhs.begin(), lhs.end(), rhs.begin(), rhs.end(), lhs.value_comp());
}
//*************************************************************************
//*************************************************************************
template <typename TKey, typename TMapped, typename TKeyCompare>
ETL_CONSTEXPR14 bool operator>(const etl::iconst_multimap<TKey, TMapped, TKeyCompare>& lhs,
const etl::iconst_multimap<TKey, TMapped, TKeyCompare>& rhs) ETL_NOEXCEPT
{
return (rhs < lhs);
}
//*************************************************************************
//*************************************************************************
template <typename TKey, typename TMapped, typename TKeyCompare>
ETL_CONSTEXPR14 bool operator<=(const etl::iconst_multimap<TKey, TMapped, TKeyCompare>& lhs,
const etl::iconst_multimap<TKey, TMapped, TKeyCompare>& rhs) ETL_NOEXCEPT
{
return !(rhs < lhs);
}
//*************************************************************************
//*************************************************************************
template <typename TKey, typename TMapped, typename TKeyCompare>
ETL_CONSTEXPR14 bool operator>=(const etl::iconst_multimap<TKey, TMapped, TKeyCompare>& lhs,
const etl::iconst_multimap<TKey, TMapped, TKeyCompare>& rhs) ETL_NOEXCEPT
{
return !(lhs < rhs);
}
} // namespace etl
#endif
#endif
algorithm.h
comparator_is_transparent.h
functional.h
etl::is_sorted
ETL_NODISCARD ETL_CONSTEXPR14 bool is_sorted(TIterator begin, TIterator end)
Definition algorithm.h:1669
etl
Definition absolute.h:40
etl::operator==
ETL_CONSTEXPR14 bool operator==(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition array.h:1078
etl::data
ETL_CONSTEXPR TContainer::pointer data(TContainer &container)
Definition iterator.h:1470
etl::operator>
bool operator>(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition array.h:1130
etl::cbegin
TContainer::const_iterator cbegin(const TContainer &container)
Definition iterator.h:1156
etl::operator>=
bool operator>=(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition array.h:1144
etl::operator!=
ETL_CONSTEXPR14 bool operator!=(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition array.h:1090
etl::size
ETL_CONSTEXPR TContainer::size_type size(const TContainer &container)
Definition iterator.h:1434
etl::end
TContainer::iterator end(TContainer &container)
Definition iterator.h:1166
etl::cend
TContainer::const_iterator cend(const TContainer &container)
Definition iterator.h:1186
etl::begin
TContainer::iterator begin(TContainer &container)
Definition iterator.h:1136
etl::operator<
bool operator<(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition array.h:1103
etl::operator<=
bool operator<=(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition array.h:1117
platform.h
span.h
type_traits.h