2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4 * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
30 * \brief Interface for map implementations.
32 * \author Olaf Wintermann
34 * \copyright 2-Clause BSD License
41 #include "allocator.h"
49 #ifndef CX_STORE_POINTERS
51 * Special constant used for creating collections that are storing pointers.
53 #define CX_STORE_POINTERS 0
56 /** Type for the UCX map. */
57 typedef struct cx_map_s CxMap;
59 /** Type for a map entry. */
60 typedef struct cx_map_entry_s CxMapEntry;
62 /** Type for map class definitions. */
63 typedef struct cx_map_class_s cx_map_class;
65 /** Structure for the UCX map. */
67 /** The map class definition. */
69 /** An allocator that is used for the map elements. */
70 CxAllocator *allocator;
71 /** The number of elements currently stored. */
74 * The size of an element.
78 * True, if this map shall store pointers instead
79 * of copies of objects.
85 * The class definition for arbitrary maps.
87 struct cx_map_class_s {
89 * Deallocates the entire memory.
91 __attribute__((__nonnull__))
92 void (*destructor)(struct cx_map_s *map);
95 * Removes all elements.
97 __attribute__((__nonnull__))
98 void (*clear)(struct cx_map_s *map);
101 * Add or overwrite an element.
103 __attribute__((__nonnull__))
111 * Returns an element.
113 __attribute__((__nonnull__, __warn_unused_result__))
120 * Removes an element.
122 __attribute__((__nonnull__))
129 * Iterator over the key/value pairs.
131 __attribute__((__nonnull__, __warn_unused_result__))
132 CxIterator (*iterator)(CxMap const *map);
135 * Iterator over the keys.
137 __attribute__((__nonnull__, __warn_unused_result__))
138 CxIterator (*iterator_keys)(CxMap const *map);
141 * Iterator over the values.
143 __attribute__((__nonnull__, __warn_unused_result__))
144 CxIterator (*iterator_values)(CxMap const *map);
147 * Mutating iterator over the key/value pairs.
149 __attribute__((__nonnull__, __warn_unused_result__))
150 CxMutIterator (*mut_iterator)(CxMap *map);
153 * Mutating iterator over the keys.
155 __attribute__((__nonnull__, __warn_unused_result__))
156 CxMutIterator (*mut_iterator_keys)(CxMap *map);
159 * Mutating iterator over the values.
161 __attribute__((__nonnull__, __warn_unused_result__))
162 CxMutIterator (*mut_iterator_values)(CxMap *map);
168 struct cx_map_entry_s {
170 * A pointer to the key.
172 CxHashKey const *key;
174 * A pointer to the value.
180 * Advises the map to store copies of the objects (default mode of operation).
182 * Retrieving objects from this map will yield pointers to the copies stored
186 * @see cxMapStorePointers()
188 __attribute__((__nonnull__))
189 static inline void cxMapStoreObjects(CxMap *map) {
190 map->store_pointers = false;
194 * Advises the map to only store pointers to the objects.
196 * Retrieving objects from this list will yield the original pointers stored.
198 * @note This function forcibly sets the element size to the size of a pointer.
199 * Invoking this function on a non-empty map that already stores copies of
200 * objects is undefined.
203 * @see cxMapStoreObjects()
205 __attribute__((__nonnull__))
206 static inline void cxMapStorePointers(CxMap *map) {
207 map->store_pointers = true;
208 map->itemsize = sizeof(void *);
213 * Deallocates the memory of the specified map.
215 * @param map the map to be destroyed
217 __attribute__((__nonnull__))
218 static inline void cxMapDestroy(CxMap *map) {
219 // TODO: likely to add auto-free feature for contents in the future
220 map->cl->destructor(map);
225 * Clears a map by removing all elements.
227 * @param map the map to be cleared
229 __attribute__((__nonnull__))
230 static inline void cxMapClear(CxMap *map) {
235 * Puts a key/value-pair into the map.
239 * @param value the value
240 * @return 0 on success, non-zero value on failure
242 __attribute__((__nonnull__))
243 static inline int cxMapPut(
248 return map->cl->put(map, key, value);
252 * Retrieves a value by using a key.
258 __attribute__((__nonnull__, __warn_unused_result__))
259 static inline void *cxMapGet(
263 return map->cl->get(map, key);
267 * Removes a key/value-pair from the map by using the key.
269 * If this map is storing pointers, you should make sure that the map
270 * is not the last location where this pointer is stored.
271 * Otherwise, use cxMapRemoveAndGet() to retrieve the pointer while
272 * removing it from the map.
276 * @see cxMapRemoveAndGet()
278 __attribute__((__nonnull__))
279 static inline void cxMapRemove(
283 (void) map->cl->remove(map, key);
287 * Removes a key/value-pair from the map by using the key.
289 * This function should only be used when the map is storing pointers,
290 * in order to retrieve the pointer you are about to remove.
291 * In any other case, cxMapRemove() is sufficient.
295 * @return the stored pointer or \c NULL if either the key is not present
296 * in the map or the map is not storing pointers
297 * @see cxMapStorePointers()
299 __attribute__((__nonnull__, __warn_unused_result__))
300 static inline void *cxMapRemoveAndGet(
304 return map->cl->remove(map, key);
307 // TODO: set-like map operations (union, intersect, difference)
310 * Creates a value iterator for a map.
312 * \note An iterator iterates over all elements successively. Therefore the order
313 * highly depends on the map implementation and may change arbitrarily when the contents change.
315 * @param map the map to create the iterator for
316 * @return an iterator for the currently stored values
318 __attribute__((__nonnull__, __warn_unused_result__))
319 static inline CxIterator cxMapIteratorValues(CxMap *map) {
320 return map->cl->iterator_values(map);
324 * Creates a key iterator for a map.
326 * The elements of the iterator are keys of type CxHashKey.
328 * \note An iterator iterates over all elements successively. Therefore the order
329 * highly depends on the map implementation and may change arbitrarily when the contents change.
331 * @param map the map to create the iterator for
332 * @return an iterator for the currently stored keys
334 __attribute__((__nonnull__, __warn_unused_result__))
335 static inline CxIterator cxMapIteratorKeys(CxMap *map) {
336 return map->cl->iterator_keys(map);
340 * Creates an iterator for a map.
342 * The elements of the iterator are key/value pairs of type CxMapEntry.
344 * \note An iterator iterates over all elements successively. Therefore the order
345 * highly depends on the map implementation and may change arbitrarily when the contents change.
347 * @param map the map to create the iterator for
348 * @return an iterator for the currently stored entries
349 * @see cxMapIteratorKeys()
350 * @see cxMapIteratorValues()
352 __attribute__((__nonnull__, __warn_unused_result__))
353 static inline CxIterator cxMapIterator(CxMap *map) {
354 return map->cl->iterator(map);
359 * Creates a mutating iterator over the values of a map.
361 * \note An iterator iterates over all elements successively. Therefore the order
362 * highly depends on the map implementation and may change arbitrarily when the contents change.
364 * @param map the map to create the iterator for
365 * @return an iterator for the currently stored values
367 __attribute__((__nonnull__, __warn_unused_result__))
368 static inline CxMutIterator cxMapMutIteratorValues(CxMap *map) {
369 return map->cl->mut_iterator_values(map);
373 * Creates a mutating iterator over the keys of a map.
375 * The elements of the iterator are keys of type CxHashKey.
377 * \note An iterator iterates over all elements successively. Therefore the order
378 * highly depends on the map implementation and may change arbitrarily when the contents change.
380 * @param map the map to create the iterator for
381 * @return an iterator for the currently stored keys
383 __attribute__((__nonnull__, __warn_unused_result__))
384 static inline CxMutIterator cxMapMutIteratorKeys(CxMap *map) {
385 return map->cl->mut_iterator_keys(map);
389 * Creates a mutating iterator for a map.
391 * The elements of the iterator are key/value pairs of type CxMapEntry.
393 * \note An iterator iterates over all elements successively. Therefore the order
394 * highly depends on the map implementation and may change arbitrarily when the contents change.
396 * @param map the map to create the iterator for
397 * @return an iterator for the currently stored entries
398 * @see cxMapMutIteratorKeys()
399 * @see cxMapMutIteratorValues()
401 __attribute__((__nonnull__, __warn_unused_result__))
402 static inline CxMutIterator cxMapMutIterator(CxMap *map) {
403 return map->cl->mut_iterator(map);