forked from intel/compile-time-init-build
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConstexprMultiMap.hpp
240 lines (220 loc) · 6.47 KB
/
ConstexprMultiMap.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
233
234
235
236
237
238
239
240
#pragma once
#include <container/ConstexprMap.hpp>
#include <container/ConstexprSet.hpp>
#include <cstddef>
/**
* A fully constexpr multi-map implementation.
*
* A multi-map contains one or more values per key.
*
* ConstexprMultiMap 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.
*
* ConstexprMultiMap owns the storage for the keys and values it stores.
* Carefully consider whether the KeyType and ValueType are object values or
* pointers.
*
* ConstexprMultiMap has very high storage requirements. Storage scales
* quadratically, or O(n^2).
*
* Consider an appropriate Capacity for the ConstexprMultiMap. It will be
* able to contain up to "Capacity" number of keys, and each key will be
* able to contain up to "Capacity" number of values.
*
* @tparam KeyType
* @tparam ValueType
* @tparam KeyCapacity
*/
template <typename KeyType, typename ValueType, std::size_t KeyCapacity,
std::size_t ValueCapacity = KeyCapacity>
class ConstexprMultiMap {
private:
using StorageType =
ConstexprMap<KeyType, ConstexprSet<ValueType, ValueCapacity>,
KeyCapacity>;
StorageType storage{};
public:
/**
* <b>Runtime complexity:</b> O(1)
*
* @return
* Current number of keys stored in this multi-map.
*/
[[nodiscard]] constexpr auto getSize() const -> std::size_t {
return storage.getSize();
}
/**
* <b>Runtime complexity:</b> O(1)
*/
[[nodiscard]] constexpr auto begin() -> typename StorageType::Entry * {
return storage.begin();
}
/**
* <b>Runtime complexity:</b> O(1)
*/
[[nodiscard]] constexpr auto end() -> typename StorageType::Entry * {
return storage.end();
}
/**
* <b>Runtime complexity:</b> O(1)
*/
[[nodiscard]] constexpr auto cbegin() const ->
typename StorageType::Entry const * {
return storage.cbegin();
}
/**
* <b>Runtime complexity:</b> O(1)
*/
[[nodiscard]] constexpr auto cend() const ->
typename StorageType::Entry const * {
return storage.cend();
}
/**
* Put an entry into the multi-map. If the key 'k' already exists in the
* map, the existing entry is updated with an additional value 'v'.
*
* Behavior is undefined if the map would exceed its Capacity for keys,
* or its Capacity for values for key 'k'.
*
* <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 {
if (storage.contains(k)) {
storage.get(k).add(v);
} else {
ConstexprSet<ValueType, ValueCapacity> set;
set.add(v);
storage.put(k, set);
}
}
/**
* Put an entry into the multi-map. If the key 'k' already exists in the
* map, then do nothing.
*
* Behavior is undefined if the map would exceed its Capacity for keys.
*
* <b>Runtime complexity:</b> O(n)
*
* @param k
* New key 'k'.
*/
constexpr auto put(KeyType k) -> void {
if (!storage.contains(k)) {
ConstexprSet<ValueType, ValueCapacity> set;
storage.put(k, set);
}
}
/**
* Remove all values from the corresponding targetKey from the multi-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 {
storage.remove(targetKey);
}
/**
* Remove corresponding targetKey and targetValue from the multi-map.
* If targetKey or targetValue are not found, do nothing.
*
* <b>Runtime complexity:</b> O(n)
*
* @param targetKey
* The key to search for and remove.
*
* @param targetValue
* The value to search for and remove.
*/
constexpr auto remove(KeyType targetKey, ValueType targetValue) -> void {
if (storage.contains(targetKey)) {
storage.get(targetKey).remove(targetValue);
if (storage.get(targetKey).isEmpty()) {
storage.remove(targetKey);
}
}
}
/**
* Get a mutable reference to the ConstexprSet for the given targetKey.
*
* <b>Runtime complexity:</b> O(n)
*
* @param targetKey
* Key to search for.
*
* @return
* Reference to the ConstexprSet if the targetKey is found. Undefined
* behavior otherwise.
*/
[[nodiscard]] constexpr auto get(KeyType k)
-> ConstexprSet<ValueType, ValueCapacity> & {
return storage.get(k);
}
/**
* Get a const reference to the ConstexprSet for the given targetKey.
*
* <b>Runtime complexity:</b> O(n)
*
* @param targetKey
* Key to search for.
*
* @return
* Reference to the ConstexprSet if the targetKey is found. Undefined
* behavior otherwise.
*/
[[nodiscard]] constexpr auto get(KeyType k) const
-> ConstexprSet<ValueType, ValueCapacity> const & {
return storage.get(k);
}
/**
* <b>Runtime complexity:</b> O(1)
*
* @return
* True if the multi-map is empty.
*/
[[nodiscard]] constexpr auto isEmpty() const -> bool {
return storage.isEmpty();
}
/**
* Check if the multi-map contains targetKey.
*
* <b>Runtime complexity:</b> O(n)
*
* @param targetKey
* The key to search for.
*
* @return
* True if targetKey is found in the multi-map.
*/
[[nodiscard]] constexpr auto contains(KeyType targetKey) const -> bool {
return storage.contains(targetKey);
}
/**
* Check if the multi-map contains key 'k', and value 'v' in key 'k'.
*
* <b>Runtime complexity:</b> O(n)
*
* @param k
* The key to search for.
*
* @param v
* The value to search for.
*
* @return
* True if key 'k' and value 'v' is found in the multi-map.
*/
[[nodiscard]] constexpr auto contains(KeyType k, ValueType v) const
-> bool {
return contains(k) and get(k).contains(v);
}
};