forked from intel/compile-time-init-build
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConstexprMap.hpp
232 lines (207 loc) · 5.81 KB
/
ConstexprMap.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#pragma once
#include <log/log.hpp>
#include <array>
#include <cstddef>
#include <iterator>
#include <optional>
#include <utility>
/**
* A single (key, value) entry in the map.
*
* @tparam KeyType
* @tparam ValueType
*/
template <typename KeyType, typename ValueType> struct MapEntry {
KeyType key{};
ValueType value{};
constexpr MapEntry(KeyType k, ValueType v) : key(k), value(v) {}
constexpr MapEntry() = default;
};
/**
* A fully constexpr map implementation.
*
* ConstexprMap is perfect for compile-time initialization and configuration,
* but its performance may not be suitable for run-time operations. in_t
* particular, the current implementation has linear run-time O(n) for any key
* lookup operation.
*
* ConstexprMap owns the storage for the keys and values it stores. Carefully
* consider whether the KeyType and ValueType are object values or pointers.
*
* Consider an appropriate Capacity for the ConstexprMap. It will be able to
* contain up to "Capacity" number of entries.
*
* @tparam KeyType
* @tparam ValueType
* @tparam Capacity
*/
template <typename KeyType, typename ValueType, std::size_t Capacity>
class ConstexprMap {
public:
using Entry = MapEntry<KeyType, ValueType>;
private:
std::array<Entry, Capacity> storage{};
std::size_t size{};
/**
* Find the storage index of the given key.
*
* <b>Runtime complexity:</b> O(n)
*
* @param targetKey
* The key to look for.
*
* @return
* Index into storage where the corresponding entry is stored. If
* the targetKey cannot be found, then -1 is returned.
*/
[[nodiscard]] constexpr auto find(KeyType targetKey) const
-> std::optional<std::size_t> {
for (std::size_t i = 0; i < size; i++) {
if (storage[i].key == targetKey) {
return i;
}
}
return {};
}
public:
constexpr ConstexprMap() = default;
template <typename T> constexpr explicit ConstexprMap(T const &rhs) {
for (auto const &entry : std::as_const(rhs)) {
put(entry.key, entry.value);
}
}
/**
* <b>Runtime complexity:</b> O(1)
*
* @return
* Current number of entries stored in this map.
*/
[[nodiscard]] constexpr auto getSize() const -> std::size_t { return size; }
/**
* Remove an unspecified entry from the map.
*
* <b>Runtime complexity:</b> O(1)
*
* @return
* An Entry from the map.
*/
[[nodiscard]] constexpr auto pop() -> Entry {
CIB_ASSERT(size > 0);
size--;
return storage[size];
}
/**
* <b>Runtime complexity:</b> O(1)
*
* @return
* True if the map is empty.
*/
[[nodiscard]] constexpr auto isEmpty() const -> bool { return size == 0; }
/**
* <b>Runtime complexity:</b> O(1)
*/
[[nodiscard]] constexpr auto begin() -> Entry * {
return std::begin(storage);
}
/**
* <b>Runtime complexity:</b> O(1)
*/
[[nodiscard]] constexpr auto end() -> Entry * { return &(storage[size]); }
/**
* <b>Runtime complexity:</b> O(1)
*/
[[nodiscard]] constexpr auto begin() const -> Entry const * {
return std::cbegin(storage);
}
/**
* <b>Runtime complexity:</b> O(1)
*/
[[nodiscard]] constexpr auto end() const -> Entry const * {
return &(storage[size]);
}
/**
* Get a mutable reference to the value for the given targetKey.
*
* <b>Runtime complexity:</b> O(n)
*
* @param targetKey
* Key to search for.
*
* @return
* Reference to the value if the targetKey is found.
*/
[[nodiscard]] constexpr auto get(KeyType targetKey) -> ValueType & {
auto entryIndex = find(targetKey);
CIB_ASSERT(entryIndex);
return storage[*entryIndex].value;
}
/**
* Get a const reference to the value for the given targetKey.
*
* <b>Runtime complexity:</b> O(n)
*
* @param targetKey
* Key to search for.
*
* @return
* Reference to the value if the targetKey is found.
*/
[[nodiscard]] constexpr auto get(KeyType targetKey) const
-> ValueType const & {
auto entryIndex = find(targetKey);
CIB_ASSERT(entryIndex);
return storage[*entryIndex].value;
}
/**
* Put an entry into the map. If the key 'k' already exists in the map, the
* existing entry is updated with the new value 'v'.
*
* <b>Runtime complexity:</b> O(n)
*
* @param k
* New key 'k' to associate with value 'v'.
*
* @param v
* New value 'v' for key 'k'.
*/
constexpr auto put(KeyType k, ValueType v) -> void {
auto entryIndex = find(k);
if (entryIndex) {
storage[*entryIndex].value = v;
} else {
CIB_ASSERT(size < Capacity);
storage[size] = Entry(k, v);
size++;
}
}
/**
* Check if the map contains targetKey.
*
* <b>Runtime complexity:</b> O(n)
*
* @param targetKey
* The key to search for.
*
* @return
* True if targetKey is found in the map.
*/
[[nodiscard]] constexpr auto contains(KeyType targetKey) const -> bool {
return static_cast<bool>(find(targetKey));
}
/**
* Remove the entry corresponding to targetKey from the map. If targetKey
* is not found, do nothing.
*
* <b>Runtime complexity:</b> O(n)
*
* @param targetKey
* The key to search for and remove.
*/
constexpr auto remove(KeyType targetKey) -> void {
auto entryIndex = find(targetKey);
if (entryIndex) {
storage[*entryIndex] = storage[size - 1];
size--;
}
}
};