koutil
Loading...
Searching...
No Matches
comptime_map.h
Go to the documentation of this file.
1#ifndef KOUTIL_CONTAINER_COMPTIME_MAP_H
2#define KOUTIL_CONTAINER_COMPTIME_MAP_H
3
5#include <algorithm>
6#include <array>
7#include <cassert>
8#include <cstddef>
9#include <limits>
10#include <utility>
11
13
21template <typename Key, typename Value, std::size_t Size> class comptime_map {
22public:
23 using pair_t = std::pair<Key, Value>;
24 using pairs_t = std::array<pair_t, Size>;
25 static constexpr auto npos = std::numeric_limits<std::size_t>::max();
26
32 consteval comptime_map(pairs_t pairs)
33 : m_data(std::move(pairs)) {
34
36 assert(false && "Duplicate keys are not allowed in a compile-time map.");
37 }
38 std::ranges::sort(m_data, {}, &pair_t::first);
39 }
40
41 template <std::size_t Count> [[nodiscard]] consteval auto extend(const std::array<pair_t, Count>& pairs) const {
43 }
44
45 [[nodiscard]] consteval auto extend(const pair_t& pair) const {
46 return comptime_map<Key, Value, Size + 1>(type::array_concat(m_data, std::array<pair_t, 1>({ pair })));
47 }
48
49 template <std::size_t OtherSize>
50 [[nodiscard]] consteval auto extend(const comptime_map<Key, Value, OtherSize>& other) const {
51 return extend(other.m_data);
52 }
53
59 [[nodiscard]] consteval bool contains_duplicate_key() const {
60 for (std::size_t i = 0; i < Size; i++) {
61 for (std::size_t j = i + 1; j < Size; j++) {
62 if (m_data[i].first == m_data[j].first) {
63 return true;
64 }
65 }
66 }
67
68 return false;
69 }
70
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) {
80 return npos;
81 } else {
82 return std::distance(m_data.begin(), iter);
83 }
84 }
85
92 [[nodiscard]] constexpr const Value& operator[](const Key& key) const { return get(key); }
93
100 [[nodiscard]] constexpr const Value& at(const Key& key) const {
101 const auto index = find(key);
102 return unsafe_get(index);
103 }
104
112 [[nodiscard]] constexpr const Value& safe_at(const Key& key, const Value& default_value) const {
113 const auto index = find(key);
114 return (index == npos) ? default_value : unsafe_get(index);
115 }
116
123 [[nodiscard]] constexpr const Value& unsafe_get(std::size_t index) const { return m_data[index].second; }
124
131 [[nodiscard]] constexpr const pair_t& get_pair(const Key& key) const { return m_data[find(key)]; }
132
139 [[nodiscard]] constexpr bool contains(const Key& key) const { return find(key) != npos; }
140
147 [[nodiscard]] consteval bool test_value(const Value& value) const {
148 for (auto&& [_, val] : m_data) {
149 if (val == value) {
150 return true;
151 }
152 }
153
154 return false;
155 }
156
163 [[nodiscard]] consteval bool test_no_value(const Value& value) const { return !test_value(value); }
164
165private:
167};
168
177template <typename Key, typename Value, std::size_t Size>
178consteval comptime_map<Key, Value, Size> to_map(std::array<std::pair<Key, Value>, Size> pairs) {
179 return { pairs };
180}
181
190template <typename Key, typename Value, std::size_t Size>
191// NOLINTNEXTLINE(modernize-avoid-c-arrays)
192consteval comptime_map<Key, Value, Size> to_map(std::pair<Key, Value> (&&pairs)[Size]) {
193 return { std::to_array(pairs) };
194}
195
196}
197
198#endif
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