-
Notifications
You must be signed in to change notification settings - Fork 0
/
MultiMapTemplate.h
234 lines (234 loc) · 18.1 KB
/
MultiMapTemplate.h
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
#ifndef MapRESERVED__MULTIMAPTEMPLATE_H
#define MapRESERVED__MULTIMAPTEMPLATE_H
#include <stddef.h>
#include <stdint.h>
#define MAP_DECLARATION(Name, Qualifiers, Key, Value, Extra) \
typedef struct MapEntry_##Name MapEntry_##Name; \
typedef struct Map_##Name Map_##Name; \
typedef typeof(Key) MapKey_##Name; \
struct MapEntry_##Name { \
MapEntry_##Name *MapNext; \
size_t MapHash; \
MapKey_##Name MapKey; \
Value \
}; \
struct Map_##Name { \
Extra size_t MapEntryCount; \
size_t MapBucketsSize; \
MapEntry_##Name **MapBuckets; \
}; \
Qualifiers void MapRESERVED__MapRehash_##Name(Map_##Name *, size_t); \
Qualifiers MapEntry_##Name **MapFind_##Name(Map_##Name *, MapKey_##Name); \
Qualifiers MapEntry_##Name **MapFindNext_##Name(Map_##Name *, \
MapEntry_##Name *); \
Qualifiers MapEntry_##Name **MapAdd_##Name(Map_##Name *, MapKey_##Name); \
Qualifiers MapEntry_##Name **MapLocate_##Name(Map_##Name *, \
MapEntry_##Name *); \
Qualifiers void MapRemove_##Name(Map_##Name *, MapEntry_##Name **); \
Qualifiers void MapClear_##Name(Map_##Name *);
#define MAP_DEFINITION(Name, Qualifiers, Hash, IsEqual, Allocate, Free) \
Qualifiers void MapRESERVED__MapRehash_##Name( \
Map_##Name *MapRESERVED__Map, size_t MapRESERVED__NewBucketsSize) { \
MapEntry_##Name **MapRESERVED__NewBuckets = Allocate( \
MapRESERVED__NewBucketsSize * sizeof(*MapRESERVED__NewBuckets), \
MapRESERVED__Map); \
if (!MapRESERVED__NewBuckets) { \
return; \
} \
for (size_t MapRESERVED__Index = 0; \
MapRESERVED__Index != MapRESERVED__NewBucketsSize; \
++MapRESERVED__Index) { \
MapRESERVED__NewBuckets[MapRESERVED__Index] = 0; \
} /* insert all of the old entries into the new table (doesn't preserve \
any order) */ \
for (size_t MapRESERVED__Index = 0; \
MapRESERVED__Index != MapRESERVED__Map->MapBucketsSize; \
++MapRESERVED__Index) { \
MapEntry_##Name *MapRESERVED__Entry = \
MapRESERVED__Map->MapBuckets[MapRESERVED__Index]; \
while (MapRESERVED__Entry) { \
MapEntry_##Name *MapRESERVED__Next = MapRESERVED__Entry->MapNext; \
size_t MapRESERVED__NewIndex = \
MapRESERVED__Entry->MapHash & MapRESERVED__NewBucketsSize - 1; \
MapRESERVED__Entry->MapNext = \
MapRESERVED__NewBuckets[MapRESERVED__NewIndex]; \
MapRESERVED__NewBuckets[MapRESERVED__NewIndex] = MapRESERVED__Entry; \
MapRESERVED__Entry = MapRESERVED__Next; \
} \
} \
Free(MapRESERVED__Map->MapBuckets, MapRESERVED__Map); \
MapRESERVED__Map->MapBuckets = MapRESERVED__NewBuckets; \
MapRESERVED__Map->MapBucketsSize = MapRESERVED__NewBucketsSize; \
} \
Qualifiers MapEntry_##Name **MapFind_##Name( \
Map_##Name *MapRESERVED__Map, MapKey_##Name MapRESERVED__Key) { \
if (!MapRESERVED__Map->MapBucketsSize) { \
return 0; \
} \
size_t MapRESERVED__Hash = Hash(MapRESERVED__Key, MapRESERVED__Map); \
size_t MapRESERVED__Index = \
MapRESERVED__Hash & MapRESERVED__Map->MapBucketsSize - 1; \
MapEntry_##Name *MapRESERVED__Entry = \
MapRESERVED__Map->MapBuckets[MapRESERVED__Index]; \
if (!MapRESERVED__Entry) { \
return 0; \
} /* find the first entry in the bucket that is equal to the key, compare \
hashes first in case IsEqual is expensive */ \
if (MapRESERVED__Hash == MapRESERVED__Entry->MapHash && \
IsEqual(MapRESERVED__Key, MapRESERVED__Entry->MapKey, \
MapRESERVED__Map)) { \
return MapRESERVED__Map->MapBuckets + MapRESERVED__Index; \
} \
for (;;) { \
MapEntry_##Name *MapRESERVED__Next = MapRESERVED__Entry->MapNext; \
if (!MapRESERVED__Next) { \
return 0; \
} \
if (MapRESERVED__Hash == MapRESERVED__Entry->MapHash && \
IsEqual(MapRESERVED__Key, MapRESERVED__Next->MapKey, \
MapRESERVED__Map)) { \
return &MapRESERVED__Entry->MapNext; \
} \
MapRESERVED__Entry = MapRESERVED__Next; \
} \
} \
Qualifiers MapEntry_##Name **MapFindNext_##Name( \
Map_##Name *MapRESERVED__Map, MapEntry_##Name *MapRESERVED__Entry) { \
size_t MapRESERVED__Hash = MapRESERVED__Entry->MapHash; \
for (MapKey_##Name MapRESERVED__Key = MapRESERVED__Entry->MapKey;;) { \
MapEntry_##Name *MapRESERVED__Next = MapRESERVED__Entry->MapNext; \
if (!MapRESERVED__Next) { \
return 0; \
} \
if (MapRESERVED__Hash == MapRESERVED__Next->MapHash && \
IsEqual(MapRESERVED__Key, MapRESERVED__Next->MapKey, \
MapRESERVED__Map)) { \
return &MapRESERVED__Entry->MapNext; \
} \
MapRESERVED__Entry = MapRESERVED__Next; \
} \
} \
Qualifiers MapEntry_##Name **MapAdd_##Name(Map_##Name *MapRESERVED__Map, \
MapKey_##Name MapRESERVED__Key) { \
if (MapRESERVED__Map->MapEntryCount == SIZE_MAX) { \
return 0; \
} \
MapEntry_##Name *MapRESERVED__Entry = \
Allocate(sizeof(*MapRESERVED__Entry), MapRESERVED__Map); \
if (!MapRESERVED__Entry) { \
return 0; \
} \
MapRESERVED__Entry->MapKey = MapRESERVED__Key; \
if (!MapRESERVED__Map \
->MapBucketsSize) { /* empty map, allocate space for 8 buckets \
and insert the new element */ \
if (sizeof(MapEntry_##Name *) > SIZE_MAX >> 3 || \
!(MapRESERVED__Map->MapBuckets = \
Allocate(sizeof(MapEntry_##Name *) << 3, MapRESERVED__Map))) { \
Free(MapRESERVED__Entry, MapRESERVED__Map); \
return 0; \
} \
size_t MapRESERVED__Index = (MapRESERVED__Entry->MapHash = Hash( \
MapRESERVED__Key, MapRESERVED__Map)) & \
7; \
MapRESERVED__Map->MapBucketsSize = 8; \
MapRESERVED__Map->MapEntryCount = 1; \
MapRESERVED__Entry->MapNext = 0; \
MapRESERVED__Map->MapBuckets[MapRESERVED__Index] = MapRESERVED__Entry; \
MapRESERVED__Map->MapBuckets[MapRESERVED__Index + 1 & 7] = \
MapRESERVED__Map->MapBuckets[MapRESERVED__Index + 2 & 7] = \
MapRESERVED__Map->MapBuckets[MapRESERVED__Index + 3 & 7] = \
MapRESERVED__Map->MapBuckets[MapRESERVED__Index + 4 & 7] = \
MapRESERVED__Map \
->MapBuckets[MapRESERVED__Index + 5 & 7] = \
MapRESERVED__Map \
->MapBuckets[MapRESERVED__Index + 6 & 7] = \
MapRESERVED__Map \
->MapBuckets[MapRESERVED__Index + 7 & 7] = \
0; \
return MapRESERVED__Map->MapBuckets + MapRESERVED__Index; \
} /* when the ratio of the number of entries to buckets is more than 3:4, \
try to reallocate to add more buckets */ \
if (++MapRESERVED__Map->MapEntryCount > \
(MapRESERVED__Map->MapBucketsSize >> 1) + \
(MapRESERVED__Map->MapBucketsSize >> 2) && \
MapRESERVED__Map->MapBucketsSize <= \
SIZE_MAX / sizeof(MapEntry_##Name *) >> 1) { \
MapRESERVED__MapRehash_##Name(MapRESERVED__Map, \
MapRESERVED__Map->MapBucketsSize << 1); \
} /* add the new token into the bucket */ \
size_t MapRESERVED__Index = (MapRESERVED__Entry->MapHash = Hash( \
MapRESERVED__Key, MapRESERVED__Map)) & \
MapRESERVED__Map->MapBucketsSize - 1; \
MapRESERVED__Entry->MapNext = \
MapRESERVED__Map->MapBuckets[MapRESERVED__Index]; \
MapRESERVED__Map->MapBuckets[MapRESERVED__Index] = MapRESERVED__Entry; \
return MapRESERVED__Map->MapBuckets + MapRESERVED__Index; \
} \
Qualifiers MapEntry_##Name **MapLocate_##Name( \
Map_##Name *MapRESERVED__Map, MapEntry_##Name *MapRESERVED__Entry) { \
size_t MapRESERVED__Index = \
MapRESERVED__Entry->MapHash & MapRESERVED__Map->MapBucketsSize - 1; \
MapEntry_##Name *MapRESERVED__Previous = \
MapRESERVED__Map->MapBuckets[MapRESERVED__Index]; \
/* search into the bucket to find what points at the entry */ \
if (MapRESERVED__Previous == MapRESERVED__Entry) { \
return MapRESERVED__Map->MapBuckets + MapRESERVED__Index; \
} \
for (;;) { \
MapEntry_##Name *MapRESERVED__Next = MapRESERVED__Previous->MapNext; \
if (MapRESERVED__Next == MapRESERVED__Entry) { \
return &MapRESERVED__Previous->MapNext; \
} \
MapRESERVED__Previous = MapRESERVED__Next; \
} \
} \
Qualifiers void MapRemove_##Name(Map_##Name *MapRESERVED__Map, \
MapEntry_##Name **MapRESERVED__Entry) { \
MapEntry_##Name *MapRESERVED__Next = (*MapRESERVED__Entry)->MapNext; \
Free(*MapRESERVED__Entry, MapRESERVED__Map); \
*MapRESERVED__Entry = MapRESERVED__Next; \
if (!--MapRESERVED__Map->MapEntryCount) { /* an empty map must have no \
memory left to free */ \
Free(MapRESERVED__Map->MapBuckets, MapRESERVED__Map); \
MapRESERVED__Map->MapBuckets = 0; \
MapRESERVED__Map->MapBucketsSize = 0; \
} else if (MapRESERVED__Map->MapEntryCount < \
MapRESERVED__Map->MapBucketsSize >> \
3) { /* when the ratio of the number of entries to buckets is \
less than 1:8, try to shrink to save space */ \
MapRESERVED__MapRehash_##Name(MapRESERVED__Map, \
MapRESERVED__Map->MapBucketsSize >> 1); \
} \
} \
Qualifiers void MapClear_##Name(Map_##Name *MapRESERVED__Map) { \
if (!MapRESERVED__Map->MapBucketsSize) { \
return; \
} \
for (size_t MapRESERVED__Index = 0;;) { \
MapEntry_##Name *MapRESERVED__Entry = \
MapRESERVED__Map->MapBuckets[MapRESERVED__Index]; \
while (MapRESERVED__Entry) { \
MapEntry_##Name *MapRESERVED__Next = MapRESERVED__Entry->MapNext; \
Free(MapRESERVED__Entry, MapRESERVED__Map); \
MapRESERVED__Entry = MapRESERVED__Next; \
} \
if (++MapRESERVED__Index == MapRESERVED__Map->MapBucketsSize) { \
break; \
} \
} \
Free(MapRESERVED__Map->MapBuckets, MapRESERVED__Map); \
MapRESERVED__Map->MapBuckets = 0; \
MapRESERVED__Map->MapBucketsSize = MapRESERVED__Map->MapEntryCount = 0; \
}
#define MAP_FUNCTION_DECLARATIONS(Name, Qualifiers) \
Qualifiers void MapRESERVED__MapRehash_##Name(Map_##Name *, size_t); \
Qualifiers MapEntry_##Name **MapFind_##Name(Map_##Name *, MapKey_##Name); \
Qualifiers MapEntry_##Name **MapFindNext_##Name(Map_##Name *, \
MapEntry_##Name *); \
Qualifiers MapEntry_##Name **MapAdd_##Name(Map_##Name *, MapKey_##Name); \
Qualifiers MapEntry_##Name **MapLocate_##Name(Map_##Name *, \
MapEntry_##Name *); \
Qualifiers void MapRemove_##Name(Map_##Name *, MapEntry_##Name **); \
Qualifiers void MapClear_##Name(Map_##Name *);
#endif