1#ifndef KOUTIL_CONTAINER_COMPTIME_MAP_H
2#define KOUTIL_CONTAINER_COMPTIME_MAP_H
21template <
typename Key,
typename Value, std::
size_t Size>
class comptime_map {
23 using pair_t = std::pair<Key, Value>;
25 static constexpr auto npos = std::numeric_limits<std::size_t>::max();
33 :
m_data(std::move(pairs)) {
36 assert(
false &&
"Duplicate keys are not allowed in a compile-time map.");
38 std::ranges::sort(
m_data, {}, &pair_t::first);
41 template <std::
size_t Count> [[nodiscard]]
consteval auto extend(
const std::array<pair_t, Count>& pairs)
const {
49 template <std::
size_t OtherSize>
60 for (std::size_t i = 0; i < Size; i++) {
61 for (std::size_t j = i + 1; j < Size; j++) {
77 [[nodiscard]]
constexpr std::size_t
find(
const Key& key)
const {
78 const auto iter = std::ranges::lower_bound(
m_data.begin(),
m_data.end(), key, {}, &pair_t::first);
79 if (iter ==
m_data.end() || iter->first != key) {
82 return std::distance(
m_data.begin(), iter);
92 [[nodiscard]]
constexpr const Value&
operator[](
const Key& key)
const {
return get(key); }
100 [[nodiscard]]
constexpr const Value&
at(
const Key& key)
const {
101 const auto index =
find(key);
112 [[nodiscard]]
constexpr const Value&
safe_at(
const Key& key,
const Value& default_value)
const {
113 const auto index =
find(key);
123 [[nodiscard]]
constexpr const Value&
unsafe_get(std::size_t index)
const {
return m_data[index].second; }
147 [[nodiscard]]
consteval bool test_value(
const Value& value)
const {
148 for (
auto&& [_, val] :
m_data) {
177template <
typename Key,
typename Value, std::
size_t Size>
190template <
typename Key,
typename Value, std::
size_t Size>
193 return { std::to_array(pairs) };
A compile-time map implementation.
Definition comptime_map.h:21
consteval comptime_map(pairs_t pairs)
Constructs a compile-time map from an array of pairs.
Definition comptime_map.h:32
constexpr std::size_t find(const Key &key) const
Finds the index of a key in the map.
Definition comptime_map.h:77
consteval auto extend(const pair_t &pair) const
Definition comptime_map.h:45
static constexpr auto npos
Definition comptime_map.h:25
std::pair< Key, Value > pair_t
Definition comptime_map.h:23
consteval auto extend(const std::array< pair_t, Count > &pairs) const
Definition comptime_map.h:41
std::array< pair_t, Size > pairs_t
Definition comptime_map.h:24
constexpr const Value & operator[](const Key &key) const
Retrieves the value associated with a key.
Definition comptime_map.h:92
constexpr const Value & safe_at(const Key &key, const Value &default_value) const
Retrieves the value associated with a key or a default value if not found.
Definition comptime_map.h:112
pairs_t m_data
Definition comptime_map.h:166
consteval auto extend(const comptime_map< Key, Value, OtherSize > &other) const
Definition comptime_map.h:50
constexpr const Value & at(const Key &key) const
Retrieves the value associated with a key.
Definition comptime_map.h:100
consteval bool contains_duplicate_key() const
Checks if there are duplicate keys in the map.
Definition comptime_map.h:59
constexpr bool contains(const Key &key) const
Checks if the map contains a key.
Definition comptime_map.h:139
consteval bool test_no_value(const Value &value) const
Checks if a specific value does not exist in the map.
Definition comptime_map.h:163
constexpr const Value & unsafe_get(std::size_t index) const
Retrieves the value at a specific index.
Definition comptime_map.h:123
consteval bool test_value(const Value &value) const
Checks if a specific value exists in the map.
Definition comptime_map.h:147
constexpr const pair_t & get_pair(const Key &key) const
Retrieves the key-value pair associated with a key.
Definition comptime_map.h:131
Definition comptime_map.h:12
consteval comptime_map< Key, Value, Size > to_map(std::array< std::pair< Key, Value >, Size > pairs)
Converts an array of pairs to a compile-time map.
Definition comptime_map.h:178
constexpr auto array_concat(const std::array< T, Sizes > &... arrays)
Concatenates multiple arrays into one array.
Definition array_concat.h:19