This source file includes following definitions.
- gl_map_create_empty
- gl_map_nx_create_empty
- gl_map_size
- gl_map_search
- gl_map_nx_getput
- gl_map_getremove
- gl_map_free
- gl_map_iterator
- gl_map_iterator_next
- gl_map_iterator_free
- gl_map_get
- gl_map_nx_put
- gl_map_remove
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 #ifndef _GL_MAP_H
19 #define _GL_MAP_H
20
21 #include <stdbool.h>
22 #include <stddef.h>
23
24 #ifndef _GL_INLINE_HEADER_BEGIN
25 #error "Please include config.h first."
26 #endif
27 _GL_INLINE_HEADER_BEGIN
28 #ifndef GL_MAP_INLINE
29 # define GL_MAP_INLINE _GL_INLINE
30 #endif
31
32 #ifdef __cplusplus
33 extern "C" {
34 #endif
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 typedef bool (*gl_mapkey_equals_fn) (const void *key1, const void *key2);
81
82
83
84 typedef size_t (*gl_mapkey_hashcode_fn) (const void *key);
85
86 #ifndef _GL_MAP_DISPOSE_FNS_DEFINED
87
88
89
90 typedef void (*gl_mapkey_dispose_fn) (const void *key);
91
92
93
94 typedef void (*gl_mapvalue_dispose_fn) (const void *value);
95
96 # define _GL_MAP_DISPOSE_FNS_DEFINED 1
97 #endif
98
99 struct gl_map_impl;
100
101 typedef struct gl_map_impl * gl_map_t;
102
103 struct gl_map_implementation;
104
105 typedef const struct gl_map_implementation * gl_map_implementation_t;
106
107 #if 0
108
109
110
111
112
113
114
115
116 extern gl_map_t gl_map_create_empty (gl_map_implementation_t implementation,
117 gl_mapkey_equals_fn equals_fn,
118 gl_mapkey_hashcode_fn hashcode_fn,
119 gl_mapkey_dispose_fn kdispose_fn,
120 gl_mapvalue_dispose_fn vdispose_fn)
121
122 _GL_ATTRIBUTE_RETURNS_NONNULL;
123
124 extern gl_map_t gl_map_nx_create_empty (gl_map_implementation_t implementation,
125 gl_mapkey_equals_fn equals_fn,
126 gl_mapkey_hashcode_fn hashcode_fn,
127 gl_mapkey_dispose_fn kdispose_fn,
128 gl_mapvalue_dispose_fn vdispose_fn)
129 ;
130
131
132 extern size_t gl_map_size (gl_map_t map);
133
134
135
136 extern const void * gl_map_get (gl_map_t map, const void *key);
137
138
139
140
141 extern bool gl_map_search (gl_map_t map, const void *key, const void **valuep);
142
143
144
145
146
147
148
149 extern bool gl_map_put (gl_map_t map, const void *key, const void *value);
150
151 _GL_ATTRIBUTE_NODISCARD
152 extern int gl_map_nx_put (gl_map_t map, const void *key, const void *value);
153
154
155
156
157
158
159
160 extern bool gl_map_getput (gl_map_t map, const void *key, const void *value,
161 const void **oldvaluep);
162
163 _GL_ATTRIBUTE_NODISCARD
164 extern int gl_map_nx_getput (gl_map_t map, const void *key, const void *value,
165 const void **oldvaluep);
166
167
168
169
170 extern bool gl_map_remove (gl_map_t map, const void *key);
171
172
173
174
175
176 extern bool gl_map_getremove (gl_map_t map, const void *key,
177 const void **oldvaluep);
178
179
180
181
182
183 extern void gl_map_free (gl_map_t map);
184
185 #endif
186
187
188
189
190
191
192
193
194
195
196
197 typedef struct
198 {
199
200 const struct gl_map_implementation *vtable;
201
202 gl_map_t map;
203 size_t count;
204
205 void *p; void *q;
206 size_t i; size_t j;
207 } gl_map_iterator_t;
208
209 #if 0
210
211
212
213
214
215 extern gl_map_iterator_t gl_map_iterator (gl_map_t map);
216
217
218
219 extern bool gl_map_iterator_next (gl_map_iterator_t *iterator,
220 const void **keyp, const void **valuep);
221
222
223 extern void gl_map_iterator_free (gl_map_iterator_t *iterator);
224
225 #endif
226
227
228
229 struct gl_map_implementation
230 {
231
232 gl_map_t (*nx_create_empty) (gl_map_implementation_t implementation,
233 gl_mapkey_equals_fn equals_fn,
234 gl_mapkey_hashcode_fn hashcode_fn,
235 gl_mapkey_dispose_fn kdispose_fn,
236 gl_mapvalue_dispose_fn vdispose_fn);
237 size_t (*size) (gl_map_t map);
238 bool (*search) (gl_map_t map, const void *key, const void **valuep);
239 int (*nx_getput) (gl_map_t map, const void *key, const void *value,
240 const void **oldvaluep);
241 bool (*getremove) (gl_map_t map, const void *key, const void **oldvaluep);
242 void (*map_free) (gl_map_t map);
243
244 gl_map_iterator_t (*iterator) (gl_map_t map);
245 bool (*iterator_next) (gl_map_iterator_t *iterator,
246 const void **keyp, const void **valuep);
247 void (*iterator_free) (gl_map_iterator_t *iterator);
248 };
249
250 struct gl_map_impl_base
251 {
252 const struct gl_map_implementation *vtable;
253 gl_mapkey_equals_fn equals_fn;
254 gl_mapkey_dispose_fn kdispose_fn;
255 gl_mapvalue_dispose_fn vdispose_fn;
256 };
257
258
259
260
261 GL_MAP_INLINE
262
263 gl_map_t
264 gl_map_nx_create_empty (gl_map_implementation_t implementation,
265 gl_mapkey_equals_fn equals_fn,
266 gl_mapkey_hashcode_fn hashcode_fn,
267 gl_mapkey_dispose_fn kdispose_fn,
268 gl_mapvalue_dispose_fn vdispose_fn)
269 {
270 return implementation->nx_create_empty (implementation,
271 equals_fn, hashcode_fn,
272 kdispose_fn, vdispose_fn);
273 }
274
275 GL_MAP_INLINE size_t
276 gl_map_size (gl_map_t map)
277 {
278 return ((const struct gl_map_impl_base *) map)->vtable->size (map);
279 }
280
281 GL_MAP_INLINE bool
282 gl_map_search (gl_map_t map, const void *key, const void **valuep)
283 {
284 return ((const struct gl_map_impl_base *) map)->vtable
285 ->search (map, key, valuep);
286 }
287
288 _GL_ATTRIBUTE_NODISCARD GL_MAP_INLINE int
289 gl_map_nx_getput (gl_map_t map, const void *key, const void *value,
290 const void **oldvaluep)
291 {
292 return ((const struct gl_map_impl_base *) map)->vtable
293 ->nx_getput (map, key, value, oldvaluep);
294 }
295
296 GL_MAP_INLINE bool
297 gl_map_getremove (gl_map_t map, const void *key, const void **oldvaluep)
298 {
299 return ((const struct gl_map_impl_base *) map)->vtable
300 ->getremove (map, key, oldvaluep);
301 }
302
303 GL_MAP_INLINE void
304 gl_map_free (gl_map_t map)
305 {
306 ((const struct gl_map_impl_base *) map)->vtable->map_free (map);
307 }
308
309 GL_MAP_INLINE gl_map_iterator_t
310 gl_map_iterator (gl_map_t map)
311 {
312 return ((const struct gl_map_impl_base *) map)->vtable->iterator (map);
313 }
314
315 GL_MAP_INLINE bool
316 gl_map_iterator_next (gl_map_iterator_t *iterator,
317 const void **keyp, const void **valuep)
318 {
319 return iterator->vtable->iterator_next (iterator, keyp, valuep);
320 }
321
322 GL_MAP_INLINE void
323 gl_map_iterator_free (gl_map_iterator_t *iterator)
324 {
325 iterator->vtable->iterator_free (iterator);
326 }
327
328
329
330
331 GL_MAP_INLINE const void *
332 gl_map_get (gl_map_t map, const void *key)
333 {
334 const void *value = NULL;
335 gl_map_search (map, key, &value);
336 return value;
337 }
338
339 _GL_ATTRIBUTE_NODISCARD GL_MAP_INLINE int
340 gl_map_nx_put (gl_map_t map, const void *key, const void *value)
341 {
342 const void *oldvalue;
343 int result = gl_map_nx_getput (map, key, value, &oldvalue);
344 if (result == 0)
345 {
346 gl_mapvalue_dispose_fn vdispose_fn =
347 ((const struct gl_map_impl_base *) map)->vdispose_fn;
348 if (vdispose_fn != NULL)
349 vdispose_fn (oldvalue);
350 }
351 return result;
352 }
353
354 GL_MAP_INLINE bool
355 gl_map_remove (gl_map_t map, const void *key)
356 {
357 const void *oldvalue;
358 bool result = gl_map_getremove (map, key, &oldvalue);
359 if (result)
360 {
361 gl_mapvalue_dispose_fn vdispose_fn =
362 ((const struct gl_map_impl_base *) map)->vdispose_fn;
363 if (vdispose_fn != NULL)
364 vdispose_fn (oldvalue);
365 }
366 return result;
367 }
368
369 #ifdef __cplusplus
370 }
371 #endif
372
373 _GL_INLINE_HEADER_END
374
375 #endif