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
4#include <algorithm>
5#include <array>
6#include <cassert>
7#include <cstddef>
8#include <limits>
9#include <utility>
10
12
20template <typename Key, typename Value, std::size_t Size> class ComptimeMap {
21public:
22 using pair_t = std::pair<Key, Value>;
23 using pairs_t = std::array<pair_t, Size>;
24 static constexpr auto npos = std::numeric_limits<std::size_t>::max();
25
31 consteval ComptimeMap(pairs_t pairs)
32 : m_data(std::move(pairs)) {
33
35 assert(false);
36 }
37 std::ranges::sort(m_data, {}, &pair_t::first);
38 }
39
45 [[nodiscard]] consteval bool contains_duplicate_key() const {
46
47 for (std::size_t i = 0; i < Size; i++) {
48 for (std::size_t j = i + 1; j < Size; j++) {
49 if (m_data[i].first == m_data[j].first) {
50 return true;
51 }
52 }
53 }
54
55 return false;
56 }
57
64 [[nodiscard]] constexpr std::size_t find(const Key& key) const {
65
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) {
68 return npos;
69 } else {
70 return std::distance(m_data.begin(), iter);
71 }
72 }
73
80 [[nodiscard]] constexpr const Value& operator[](const Key& key) const { return get(key); }
81
88 [[nodiscard]] constexpr const Value& at(const Key& key) const {
89 const auto index = find(key);
90 return unsafe_get(index);
91 }
92
100 [[nodiscard]] constexpr const Value& safe_at(const Key& key, const Value& default_value) const {
101 const auto index = find(key);
102 return (index == npos) ? default_value : unsafe_get(index);
103 }
104
111 [[nodiscard]] constexpr const Value& unsafe_get(std::size_t index) const { return m_data[index].second; }
112
119 [[nodiscard]] constexpr const pair_t& get_pair(const Key& key) const { return m_data[find(key)]; }
120
127 [[nodiscard]] constexpr bool contains(const Key& key) const { return find(key) != npos; }
128
135 [[nodiscard]] consteval bool test_value(const Value& value) const {
136
137 for (auto&& [_, val] : m_data) {
138 if (val == value) {
139 return true;
140 }
141 }
142
143 return false;
144 }
145
152 [[nodiscard]] consteval bool test_no_value(const Value& value) const { return !test_value(value); }
153
154private:
156};
157
166template <typename Key, typename Value, std::size_t Size>
167consteval ComptimeMap<Key, Value, Size> to_map(std::array<std::pair<Key, Value>, Size> pairs) {
168 return { pairs };
169}
170
179template <typename Key, typename Value, std::size_t Size>
180// NOLINTNEXTLINE(modernize-avoid-c-arrays)
181consteval ComptimeMap<Key, Value, Size> to_map(std::pair<Key, Value> (&&pairs)[Size]) {
182 return { std::to_array(pairs) };
183}
184
185}
186
187#endif
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