timespec_get: New module.
[gnulib.git] / lib / gl_omap.h
blobfe2e211304656d9f12b1b693f934065cfae2e1f4
1 /* Abstract ordered map data type.
2 Copyright (C) 2006-2007, 2009-2021 Free Software Foundation, Inc.
3 Written by Bruno Haible <bruno@clisp.org>, 2018.
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>. */
18 #ifndef _GL_OMAP_H
19 #define _GL_OMAP_H
21 #include <stdbool.h>
22 #include <stddef.h>
24 #ifndef _GL_INLINE_HEADER_BEGIN
25 #error "Please include config.h first."
26 #endif
27 _GL_INLINE_HEADER_BEGIN
28 #ifndef GL_OMAP_INLINE
29 # define GL_OMAP_INLINE _GL_INLINE
30 #endif
32 #ifdef __cplusplus
33 extern "C" {
34 #endif
37 /* gl_omap is an abstract ordered map data type. It can contain any number
38 of (key, value) pairs, where
39 - keys and values are objects ('void *' or 'const void *' pointers),
40 - The (key, value) pairs are ordered by the key, in the order of a given
41 comparator function.
42 - There are no (key, value1) and (key, value2) pairs with the same key
43 (in the sense of the comparator function).
45 There are several implementations of this ordered map datatype, optimized
46 for different operations or for memory. You can start using the simplest
47 ordered map implementation, GL_ARRAY_OMAP, and switch to a different
48 implementation later, when you realize which operations are performed
49 the most frequently. The API of the different implementations is exactly
50 the same; when switching to a different implementation, you only have to
51 change the gl_omap_create call.
53 The implementations are:
54 GL_ARRAY_OMAP a growable array
55 GL_AVLTREE_OMAP a binary tree (AVL tree)
56 GL_RBTREE_OMAP a binary tree (red-black tree)
58 The memory consumption is asymptotically the same: O(1) for every pair
59 in the map. When looking more closely at the average memory consumed
60 for an pair, GL_ARRAY_OMAP is the most compact representation, and
61 GL_AVLTREE_OMAP, GL_RBTREE_OMAP need more memory.
63 The guaranteed average performance of the operations is, for a map of
64 n pairs:
66 Operation ARRAY TREE
68 gl_omap_size O(1) O(1)
69 gl_omap_get O(log n) O(log n)
70 gl_omap_put O(n) O(log n)
71 gl_omap_remove O(n) O(log n)
72 gl_omap_search O(log n) O(log n)
73 gl_omap_search_atleast O(log n) O(log n)
74 gl_omap_iterator O(1) O(log n)
75 gl_omap_iterator_next O(1) O(log n)
78 /* -------------------------- gl_omap_t Data Type -------------------------- */
80 /* Type of function used to compare two keys. Same as for qsort().
81 NULL denotes pointer comparison. */
82 typedef int (*gl_mapkey_compar_fn) (const void *key1, const void *key2);
84 #ifndef _GL_MAP_DISPOSE_FNS_DEFINED
86 /* Type of function used to dispose a key once a (key, value) pair is removed
87 from a map. NULL denotes a no-op. */
88 typedef void (*gl_mapkey_dispose_fn) (const void *key);
90 /* Type of function used to dispose a value once a (key, value) pair is removed
91 from a map. NULL denotes a no-op. */
92 typedef void (*gl_mapvalue_dispose_fn) (const void *value);
94 # define _GL_MAP_DISPOSE_FNS_DEFINED 1
95 #endif
97 /* Type of function used to compare a key with a threshold.
98 Returns true if the key is greater or equal than the threshold. */
99 typedef bool (*gl_mapkey_threshold_fn) (const void *key, const void *threshold);
101 struct gl_omap_impl;
102 /* Type representing an entire ordered map. */
103 typedef struct gl_omap_impl * gl_omap_t;
105 struct gl_omap_implementation;
106 /* Type representing a ordered map datatype implementation. */
107 typedef const struct gl_omap_implementation * gl_omap_implementation_t;
109 #if 0 /* Unless otherwise specified, these are defined inline below. */
111 /* Creates an empty map.
112 IMPLEMENTATION is one of GL_ARRAY_OMAP, GL_AVLTREE_OMAP, GL_RBTREE_OMAP.
113 COMPAR_FN is a key comparison function or NULL.
114 KDISPOSE_FN is a key disposal function or NULL.
115 VDISPOSE_FN is a value disposal function or NULL. */
116 /* declared in gl_xomap.h */
117 extern gl_omap_t gl_omap_create_empty (gl_omap_implementation_t implementation,
118 gl_mapkey_compar_fn compar_fn,
119 gl_mapkey_dispose_fn kdispose_fn,
120 gl_mapvalue_dispose_fn vdispose_fn);
121 /* Likewise. Returns NULL upon out-of-memory. */
122 extern gl_omap_t gl_omap_nx_create_empty (gl_omap_implementation_t implementation,
123 gl_mapkey_compar_fn compar_fn,
124 gl_mapkey_dispose_fn kdispose_fn,
125 gl_mapvalue_dispose_fn vdispose_fn);
127 /* Returns the current number of pairs in an ordered map. */
128 extern size_t gl_omap_size (gl_omap_t map);
130 /* Searches whether a pair with the given key is already in the ordered map.
131 Returns the value if found, or NULL if not present in the map. */
132 extern const void * gl_omap_get (gl_omap_t map, const void *key);
134 /* Searches whether a pair with the given key is already in the ordered map.
135 Returns true and sets *VALUEP to the value if found.
136 Returns false if not present in the map. */
137 extern bool gl_omap_search (gl_omap_t map, const void *key,
138 const void **valuep);
140 /* Searches the pair with the least key in the ordered map that compares
141 greater or equal to the given THRESHOLD. The representation of the
142 THRESHOLD is defined by the THRESHOLD_FN.
143 Returns true and stores the found pair in *KEYP and *VALUEP if found.
144 Otherwise returns false. */
145 extern bool gl_omap_search_atleast (gl_omap_t map,
146 gl_mapkey_threshold_fn threshold_fn,
147 const void *threshold,
148 const void **keyp, const void **valuep);
150 /* Adds a pair to an ordered map.
151 Returns true if a pair with the given key was not already in the map and so
152 this pair was added.
153 Returns false if a pair with the given key was already in the map and only
154 its value was replaced. */
155 /* declared in gl_xomap.h */
156 extern bool gl_omap_put (gl_omap_t map, const void *key, const void *value);
157 /* Likewise. Returns -1 upon out-of-memory. */
158 extern int gl_omap_nx_put (gl_omap_t map, const void *key, const void *value)
159 _GL_ATTRIBUTE_NODISCARD;
161 /* Adds a pair to an ordered map and retrieves the previous value.
162 Returns true if a pair with the given key was not already in the map and so
163 this pair was added.
164 Returns false and sets *OLDVALUEP to the previous value, if a pair with the
165 given key was already in the map and only its value was replaced. */
166 /* declared in gl_xomap.h */
167 extern bool gl_omap_getput (gl_omap_t map, const void *key, const void *value,
168 const void **oldvaluep);
169 /* Likewise. Returns -1 upon out-of-memory. */
170 extern int gl_omap_nx_getput (gl_omap_t map, const void *key, const void *value,
171 const void **oldvaluep)
172 _GL_ATTRIBUTE_NODISCARD;
174 /* Removes a pair from an ordered map.
175 Returns true if the key was found and its pair removed.
176 Returns false otherwise. */
177 extern bool gl_omap_remove (gl_omap_t map, const void *key);
179 /* Removes a pair from an ordered map and retrieves the previous value.
180 Returns true and sets *OLDVALUEP to the previous value, if the key was found
181 and its pair removed.
182 Returns false otherwise. */
183 extern bool gl_omap_getremove (gl_omap_t map, const void *key,
184 const void **oldvaluep);
186 /* Frees an entire ordered map.
187 (But this call does not free the keys and values of the pairs in the map.
188 It only invokes the KDISPOSE_FN on each key and the VDISPOSE_FN on each value
189 of the pairs in the map.) */
190 extern void gl_omap_free (gl_omap_t map);
192 #endif /* End of inline and gl_xomap.h-defined functions. */
194 /* ---------------------- gl_omap_iterator_t Data Type ---------------------- */
196 /* Functions for iterating through an ordered map. */
198 /* Type of an iterator that traverses an ordered map.
199 This is a fixed-size struct, so that creation of an iterator doesn't need
200 memory allocation on the heap. */
201 typedef struct
203 /* For fast dispatch of gl_omap_iterator_next. */
204 const struct gl_omap_implementation *vtable;
205 /* For detecting whether the last returned pair was removed. */
206 gl_omap_t map;
207 size_t count;
208 /* Other, implementation-private fields. */
209 void *p; void *q;
210 size_t i; size_t j;
211 } gl_omap_iterator_t;
213 #if 0 /* These are defined inline below. */
215 /* Creates an iterator traversing an ordered map.
216 The map's contents must not be modified while the iterator is in use,
217 except for modifying the value of the last returned key or removing the
218 last returned pair. */
219 extern gl_omap_iterator_t gl_omap_iterator (gl_omap_t map);
221 /* If there is a next pair, stores the next pair in *KEYP and *VALUEP, advances
222 the iterator, and returns true. Otherwise, returns false. */
223 extern bool gl_omap_iterator_next (gl_omap_iterator_t *iterator,
224 const void **keyp, const void **valuep);
226 /* Frees an iterator. */
227 extern void gl_omap_iterator_free (gl_omap_iterator_t *iterator);
229 #endif /* End of inline functions. */
231 /* ------------------------- Implementation Details ------------------------- */
233 struct gl_omap_implementation
235 /* gl_omap_t functions. */
236 gl_omap_t (*nx_create_empty) (gl_omap_implementation_t implementation,
237 gl_mapkey_compar_fn compar_fn,
238 gl_mapkey_dispose_fn kdispose_fn,
239 gl_mapvalue_dispose_fn vdispose_fn);
240 size_t (*size) (gl_omap_t map);
241 bool (*search) (gl_omap_t map, const void *key, const void **valuep);
242 bool (*search_atleast) (gl_omap_t map,
243 gl_mapkey_threshold_fn threshold_fn,
244 const void *threshold,
245 const void **keyp, const void **valuep);
246 int (*nx_getput) (gl_omap_t map, const void *key, const void *value,
247 const void **oldvaluep);
248 bool (*getremove) (gl_omap_t map, const void *key, const void **oldvaluep);
249 void (*omap_free) (gl_omap_t map);
250 /* gl_omap_iterator_t functions. */
251 gl_omap_iterator_t (*iterator) (gl_omap_t map);
252 bool (*iterator_next) (gl_omap_iterator_t *iterator,
253 const void **keyp, const void **valuep);
254 void (*iterator_free) (gl_omap_iterator_t *iterator);
257 struct gl_omap_impl_base
259 const struct gl_omap_implementation *vtable;
260 gl_mapkey_compar_fn compar_fn;
261 gl_mapkey_dispose_fn kdispose_fn;
262 gl_mapvalue_dispose_fn vdispose_fn;
265 /* Define most functions of this file as accesses to the
266 struct gl_omap_implementation. */
268 GL_OMAP_INLINE gl_omap_t
269 gl_omap_nx_create_empty (gl_omap_implementation_t implementation,
270 gl_mapkey_compar_fn compar_fn,
271 gl_mapkey_dispose_fn kdispose_fn,
272 gl_mapvalue_dispose_fn vdispose_fn)
274 return implementation->nx_create_empty (implementation, compar_fn,
275 kdispose_fn, vdispose_fn);
278 GL_OMAP_INLINE size_t
279 gl_omap_size (gl_omap_t map)
281 return ((const struct gl_omap_impl_base *) map)->vtable->size (map);
284 GL_OMAP_INLINE bool
285 gl_omap_search (gl_omap_t map, const void *key, const void **valuep)
287 return ((const struct gl_omap_impl_base *) map)->vtable
288 ->search (map, key, valuep);
291 GL_OMAP_INLINE bool
292 gl_omap_search_atleast (gl_omap_t map,
293 gl_mapkey_threshold_fn threshold_fn,
294 const void *threshold,
295 const void **keyp, const void **valuep)
297 return ((const struct gl_omap_impl_base *) map)->vtable
298 ->search_atleast (map, threshold_fn, threshold, keyp, valuep);
301 GL_OMAP_INLINE _GL_ATTRIBUTE_NODISCARD int
302 gl_omap_nx_getput (gl_omap_t map, const void *key, const void *value,
303 const void **oldvaluep)
305 return ((const struct gl_omap_impl_base *) map)->vtable
306 ->nx_getput (map, key, value, oldvaluep);
309 GL_OMAP_INLINE bool
310 gl_omap_getremove (gl_omap_t map, const void *key, const void **oldvaluep)
312 return ((const struct gl_omap_impl_base *) map)->vtable
313 ->getremove (map, key, oldvaluep);
316 GL_OMAP_INLINE void
317 gl_omap_free (gl_omap_t map)
319 ((const struct gl_omap_impl_base *) map)->vtable->omap_free (map);
322 GL_OMAP_INLINE gl_omap_iterator_t
323 gl_omap_iterator (gl_omap_t map)
325 return ((const struct gl_omap_impl_base *) map)->vtable->iterator (map);
328 GL_OMAP_INLINE bool
329 gl_omap_iterator_next (gl_omap_iterator_t *iterator,
330 const void **keyp, const void **valuep)
332 return iterator->vtable->iterator_next (iterator, keyp, valuep);
335 GL_OMAP_INLINE void
336 gl_omap_iterator_free (gl_omap_iterator_t *iterator)
338 iterator->vtable->iterator_free (iterator);
341 /* Define the convenience functions, that is, the functions that are independent
342 of the implementation. */
344 GL_OMAP_INLINE const void *
345 gl_omap_get (gl_omap_t map, const void *key)
347 const void *value = NULL;
348 gl_omap_search (map, key, &value);
349 return value;
352 GL_OMAP_INLINE _GL_ATTRIBUTE_NODISCARD int
353 gl_omap_nx_put (gl_omap_t map, const void *key, const void *value)
355 const void *oldvalue;
356 int result = gl_omap_nx_getput (map, key, value, &oldvalue);
357 if (result == 0)
359 gl_mapvalue_dispose_fn vdispose_fn =
360 ((const struct gl_omap_impl_base *) map)->vdispose_fn;
361 if (vdispose_fn != NULL)
362 vdispose_fn (oldvalue);
364 return result;
367 GL_OMAP_INLINE bool
368 gl_omap_remove (gl_omap_t map, const void *key)
370 const void *oldvalue;
371 bool result = gl_omap_getremove (map, key, &oldvalue);
372 if (result)
374 gl_mapvalue_dispose_fn vdispose_fn =
375 ((const struct gl_omap_impl_base *) map)->vdispose_fn;
376 if (vdispose_fn != NULL)
377 vdispose_fn (oldvalue);
379 return result;
382 #ifdef __cplusplus
384 #endif
386 _GL_INLINE_HEADER_END
388 #endif /* _GL_OMAP_H */