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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
|
/*************************************************************************/
/* ordered_hash_map.h */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* http://www.godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
#ifndef ORDERED_HASH_MAP_H
#define ORDERED_HASH_MAP_H
#include "hash_map.h"
#include "list.h"
#include "pair.h"
/**
* A hash map which allows to iterate elements in insertion order.
* Insertion, lookup, deletion have O(1) complexity.
* The API aims to be consistent with Map rather than HashMap, because the
* former is more frequently used and is more coherent with the rest of the
* codebase.
* Deletion during iteration is safe and will preserve the order.
*/
template <class K, class V, class Hasher = HashMapHasherDefault, class Comparator = HashMapComparatorDefault<K>, uint8_t MIN_HASH_TABLE_POWER = 3, uint8_t RELATIONSHIP = 8>
class OrderedHashMap {
typedef List<Pair<const K *, V> > InternalList;
typedef HashMap<K, typename InternalList::Element *, Hasher, Comparator, MIN_HASH_TABLE_POWER, RELATIONSHIP> InternalMap;
InternalList list;
InternalMap map;
public:
class Element {
friend class OrderedHashMap<K, V, Hasher, Comparator, MIN_HASH_TABLE_POWER, RELATIONSHIP>;
typename InternalList::Element *list_element;
typename InternalList::Element *prev_element;
typename InternalList::Element *next_element;
Element(typename InternalList::Element *p_element) {
list_element = p_element;
if (list_element) {
next_element = list_element->next();
prev_element = list_element->prev();
}
}
public:
_FORCE_INLINE_ Element() :
list_element(NULL),
prev_element(NULL),
next_element(NULL) {
}
Element next() const {
return Element(next_element);
}
Element prev() const {
return Element(prev_element);
}
Element(const Element &other) :
list_element(other.list_element),
prev_element(other.prev_element),
next_element(other.next_element) {
}
Element &operator=(const Element &other) {
list_element = other.list_element;
next_element = other.next_element;
prev_element = other.prev_element;
return *this;
}
_FORCE_INLINE_ bool operator==(const Element &p_other) const {
return this->list_element == p_other.list_element;
}
_FORCE_INLINE_ bool operator!=(const Element &p_other) const {
return this->list_element != p_other.list_element;
}
operator bool() const {
return (list_element != NULL);
}
const K &key() const {
CRASH_COND(!list_element);
return *(list_element->get().first);
};
V &value() {
CRASH_COND(!list_element);
return list_element->get().second;
};
const V &value() const {
CRASH_COND(!list_element);
return list_element->get().second;
};
V &get() {
CRASH_COND(!list_element);
return list_element->get().second;
};
const V &get() const {
CRASH_COND(!list_element);
return list_element->get().second;
};
};
class ConstElement {
friend class OrderedHashMap<K, V, Hasher, Comparator, MIN_HASH_TABLE_POWER, RELATIONSHIP>;
const typename InternalList::Element *list_element;
ConstElement(const typename InternalList::Element *p_element) :
list_element(p_element) {
}
public:
_FORCE_INLINE_ ConstElement() :
list_element(NULL) {
}
ConstElement(const ConstElement &other) :
list_element(other.list_element) {
}
ConstElement &operator=(const ConstElement &other) {
list_element = other.list_element;
return *this;
}
ConstElement next() const {
return ConstElement(list_element ? list_element->next() : NULL);
}
ConstElement prev() const {
return ConstElement(list_element ? list_element->prev() : NULL);
}
_FORCE_INLINE_ bool operator==(const ConstElement &p_other) const {
return this->list_element == p_other.list_element;
}
_FORCE_INLINE_ bool operator!=(const ConstElement &p_other) const {
return this->list_element != p_other.list_element;
}
operator bool() const {
return (list_element != NULL);
}
const K &key() const {
CRASH_COND(!list_element);
return *(list_element->get().first);
};
const V &value() const {
CRASH_COND(!list_element);
return list_element->get().second;
};
const V &get() const {
CRASH_COND(!list_element);
return list_element->get().second;
};
};
ConstElement find(const K &p_key) const {
typename InternalList::Element *const *list_element = map.getptr(p_key);
if (list_element) {
return ConstElement(*list_element);
}
return ConstElement(NULL);
}
Element find(const K &p_key) {
typename InternalList::Element **list_element = map.getptr(p_key);
if (list_element) {
return Element(*list_element);
}
return Element(NULL);
}
Element insert(const K &p_key, const V &p_value) {
typename InternalList::Element **list_element = map.getptr(p_key);
if (list_element) {
(*list_element)->get().second = p_value;
return Element(*list_element);
}
typename InternalList::Element *new_element = list.push_back(Pair<const K *, V>(NULL, p_value));
typename InternalMap::Element *e = map.set(p_key, new_element);
new_element->get().first = &e->key();
return Element(new_element);
}
void erase(Element &p_element) {
map.erase(p_element.key());
list.erase(p_element.list_element);
p_element.list_element = NULL;
}
bool erase(const K &p_key) {
typename InternalList::Element **list_element = map.getptr(p_key);
if (list_element) {
list.erase(*list_element);
map.erase(p_key);
return true;
}
return false;
}
inline bool has(const K &p_key) const {
return map.has(p_key);
}
const V &operator[](const K &p_key) const {
ConstElement e = find(p_key);
CRASH_COND(!e);
return e.value();
}
V &operator[](const K &p_key) {
Element e = find(p_key);
if (!e) {
// consistent with Map behaviour
e = insert(p_key, V());
}
return e.value();
}
inline Element front() {
return Element(list.front());
}
inline Element back() {
return Element(list.back());
}
inline ConstElement front() const {
return ConstElement(list.front());
}
inline ConstElement back() const {
return ConstElement(list.back());
}
inline bool empty() const { return list.empty(); }
inline int size() const { return list.size(); }
void clear() {
map.clear();
list.clear();
}
private:
void _copy_from(const OrderedHashMap &p_map) {
for (ConstElement E = p_map.front(); E; E = E.next()) {
insert(E.key(), E.value());
}
}
public:
void operator=(const OrderedHashMap &p_map) {
_copy_from(p_map);
}
OrderedHashMap(const OrderedHashMap &p_map) {
_copy_from(p_map);
}
_FORCE_INLINE_ OrderedHashMap() {
}
};
#endif // ORDERED_HASH_MAP_H
|