1#ifndef KOUTIL_CONTAINER_COMPTIME_MAP_H
2#define KOUTIL_CONTAINER_COMPTIME_MAP_H
20template <
typename Key,
typename Value, std::
size_t Size>
class ComptimeMap {
22 using pair_t = std::pair<Key, Value>;
24 static constexpr auto npos = std::numeric_limits<std::size_t>::max();
32 :
m_data(std::move(pairs)) {
37 std::ranges::sort(
m_data, {}, &pair_t::first);
47 for (std::size_t i = 0; i < Size; i++) {
48 for (std::size_t j = i + 1; j < Size; j++) {
64 [[nodiscard]]
constexpr std::size_t
find(
const Key& key)
const {
66 const auto iter = std::ranges::lower_bound(
m_data.begin(),
m_data.end(), key, {}, &pair_t::first);
67 if (iter ==
m_data.end() || iter->first != key) {
70 return std::distance(
m_data.begin(), iter);
80 [[nodiscard]]
constexpr const Value&
operator[](
const Key& key)
const {
return get(key); }
88 [[nodiscard]]
constexpr const Value&
at(
const Key& key)
const {
89 const auto index =
find(key);
100 [[nodiscard]]
constexpr const Value&
safe_at(
const Key& key,
const Value& default_value)
const {
101 const auto index =
find(key);
111 [[nodiscard]]
constexpr const Value&
unsafe_get(std::size_t index)
const {
return m_data[index].second; }
135 [[nodiscard]]
consteval bool test_value(
const Value& value)
const {
137 for (
auto&& [_, val] :
m_data) {
166template <
typename Key,
typename Value, std::
size_t Size>
179template <
typename Key,
typename Value, std::
size_t Size>
182 return { std::to_array(pairs) };
A compile-time map implementation.
Definition comptime_map.h:20
static constexpr auto npos
Definition comptime_map.h:24
constexpr const Value & at(const Key &key) const
Retrieves the value associated with a key.
Definition comptime_map.h:88
std::array< pair_t, Size > pairs_t
Definition comptime_map.h:23
constexpr std::size_t find(const Key &key) const
Finds the index of a key in the map.
Definition comptime_map.h:64
consteval bool test_value(const Value &value) const
Checks if a specific value exists in the map.
Definition comptime_map.h:135
pairs_t m_data
Definition comptime_map.h:155
constexpr const Value & operator[](const Key &key) const
Retrieves the value associated with a key.
Definition comptime_map.h:80
constexpr const pair_t & get_pair(const Key &key) const
Retrieves the key-value pair associated with a key.
Definition comptime_map.h:119
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:100
consteval ComptimeMap(pairs_t pairs)
Constructs a compile-time map from an array of pairs.
Definition comptime_map.h:31
consteval bool contains_duplicate_key() const
Checks if there are duplicate keys in the map.
Definition comptime_map.h:45
constexpr const Value & unsafe_get(std::size_t index) const
Retrieves the value at a specific index.
Definition comptime_map.h:111
constexpr bool contains(const Key &key) const
Checks if the map contains a key.
Definition comptime_map.h:127
consteval bool test_no_value(const Value &value) const
Checks if a specific value does not exist in the map.
Definition comptime_map.h:152
std::pair< Key, Value > pair_t
Definition comptime_map.h:22
Definition comptime_map.h:11
consteval ComptimeMap< 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:167