src/cx/map.h

Sat, 26 Nov 2022 16:58:41 +0100

author
Mike Becker <universe@uap-core.de>
date
Sat, 26 Nov 2022 16:58:41 +0100
changeset 630
ac5e7f789048
parent 564
5d8ad7a0ff71
child 658
56c62780582e
permissions
-rw-r--r--

separate iterators and mutating iterators

Trade tons of code duplication for const-correctness.

universe@549 1 /*
universe@549 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
universe@549 3 *
universe@549 4 * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved.
universe@549 5 *
universe@549 6 * Redistribution and use in source and binary forms, with or without
universe@549 7 * modification, are permitted provided that the following conditions are met:
universe@549 8 *
universe@549 9 * 1. Redistributions of source code must retain the above copyright
universe@549 10 * notice, this list of conditions and the following disclaimer.
universe@549 11 *
universe@549 12 * 2. Redistributions in binary form must reproduce the above copyright
universe@549 13 * notice, this list of conditions and the following disclaimer in the
universe@549 14 * documentation and/or other materials provided with the distribution.
universe@549 15 *
universe@549 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
universe@549 17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
universe@549 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
universe@549 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
universe@549 20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
universe@549 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
universe@549 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
universe@549 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
universe@549 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
universe@549 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
universe@549 26 * POSSIBILITY OF SUCH DAMAGE.
universe@549 27 */
universe@549 28 /**
universe@549 29 * \file map.h
universe@549 30 * \brief Interface for map implementations.
universe@549 31 * \author Mike Becker
universe@549 32 * \author Olaf Wintermann
universe@549 33 * \version 3.0
universe@549 34 * \copyright 2-Clause BSD License
universe@549 35 */
universe@549 36
universe@549 37 #ifndef UCX_MAP_H
universe@549 38 #define UCX_MAP_H
universe@549 39
universe@549 40 #include "common.h"
universe@549 41 #include "allocator.h"
universe@549 42 #include "iterator.h"
universe@563 43 #include "hash_key.h"
universe@549 44
universe@549 45 #ifdef __cplusplus
universe@549 46 extern "C" {
universe@549 47 #endif
universe@549 48
universe@549 49 /** Type for the UCX map. */
universe@549 50 typedef struct cx_map_s CxMap;
universe@549 51
universe@549 52 /** Type for a map entry. */
universe@549 53 typedef struct cx_map_entry_s CxMapEntry;
universe@549 54
universe@549 55 /** Type for map class definitions. */
universe@549 56 typedef struct cx_map_class_s cx_map_class;
universe@549 57
universe@549 58 /** Structure for the UCX map. */
universe@549 59 struct cx_map_s {
universe@549 60 /** The map class definition. */
universe@549 61 cx_map_class *cl;
universe@549 62 /** An allocator that is used for the map elements. */
universe@549 63 CxAllocator *allocator;
universe@549 64 /** The number of elements currently stored. */
universe@549 65 size_t size;
universe@549 66 // TODO: elemsize + a flag if values shall be copied to the map
universe@549 67 };
universe@549 68
universe@549 69 /**
universe@549 70 * The class definition for arbitrary maps.
universe@549 71 */
universe@549 72 struct cx_map_class_s {
universe@549 73 /**
universe@549 74 * Deallocates the entire memory.
universe@549 75 */
universe@549 76 __attribute__((__nonnull__))
universe@549 77 void (*destructor)(struct cx_map_s *map);
universe@549 78
universe@549 79 /**
universe@549 80 * Removes all elements.
universe@549 81 */
universe@549 82 __attribute__((__nonnull__))
universe@549 83 void (*clear)(struct cx_map_s *map);
universe@549 84
universe@549 85 /**
universe@549 86 * Add or overwrite an element.
universe@549 87 */
universe@549 88 __attribute__((__nonnull__))
universe@549 89 int (*put)(
universe@549 90 CxMap *map,
universe@563 91 CxHashKey key,
universe@550 92 void *value
universe@549 93 );
universe@549 94
universe@549 95 /**
universe@549 96 * Returns an element.
universe@549 97 */
universe@549 98 __attribute__((__nonnull__, __warn_unused_result__))
universe@549 99 void *(*get)(
universe@549 100 CxMap const *map,
universe@563 101 CxHashKey key
universe@549 102 );
universe@549 103
universe@549 104 /**
universe@549 105 * Removes an element.
universe@549 106 */
universe@549 107 __attribute__((__nonnull__, __warn_unused_result__))
universe@549 108 void *(*remove)(
universe@550 109 CxMap *map,
universe@563 110 CxHashKey key
universe@549 111 );
universe@549 112
universe@549 113 /**
universe@549 114 * Iterator over the key/value pairs.
universe@549 115 */
universe@549 116 __attribute__((__nonnull__, __warn_unused_result__))
universe@630 117 CxIterator (*iterator)(CxMap const *map);
universe@549 118
universe@549 119 /**
universe@549 120 * Iterator over the keys.
universe@549 121 */
universe@549 122 __attribute__((__nonnull__, __warn_unused_result__))
universe@630 123 CxIterator (*iterator_keys)(CxMap const *map);
universe@549 124
universe@549 125 /**
universe@549 126 * Iterator over the values.
universe@549 127 */
universe@549 128 __attribute__((__nonnull__, __warn_unused_result__))
universe@630 129 CxIterator (*iterator_values)(CxMap const *map);
universe@630 130
universe@630 131 /**
universe@630 132 * Mutating iterator over the key/value pairs.
universe@630 133 */
universe@630 134 __attribute__((__nonnull__, __warn_unused_result__))
universe@630 135 CxMutIterator (*mut_iterator)(CxMap *map);
universe@630 136
universe@630 137 /**
universe@630 138 * Mutating iterator over the keys.
universe@630 139 */
universe@630 140 __attribute__((__nonnull__, __warn_unused_result__))
universe@630 141 CxMutIterator (*mut_iterator_keys)(CxMap *map);
universe@630 142
universe@630 143 /**
universe@630 144 * Mutating iterator over the values.
universe@630 145 */
universe@630 146 __attribute__((__nonnull__, __warn_unused_result__))
universe@630 147 CxMutIterator (*mut_iterator_values)(CxMap *map);
universe@549 148 };
universe@549 149
universe@549 150 /**
universe@549 151 * A map entry.
universe@549 152 */
universe@549 153 struct cx_map_entry_s {
universe@549 154 /**
universe@551 155 * A pointer to the key.
universe@549 156 */
universe@563 157 CxHashKey const *key;
universe@549 158 /**
universe@551 159 * A pointer to the value.
universe@549 160 */
universe@551 161 void *value;
universe@549 162 };
universe@549 163
universe@549 164
universe@549 165 /**
universe@549 166 * Deallocates the memory of the specified map.
universe@549 167 *
universe@549 168 * @param map the map to be destroyed
universe@549 169 */
universe@549 170 __attribute__((__nonnull__))
universe@549 171 static inline void cxMapDestroy(CxMap *map) {
universe@549 172 // TODO: likely to add auto-free feature for contents in the future
universe@549 173 map->cl->destructor(map);
universe@549 174 }
universe@549 175
universe@549 176
universe@549 177 /**
universe@549 178 * Clears a map by removing all elements.
universe@549 179 *
universe@549 180 * @param map the map to be cleared
universe@549 181 */
universe@549 182 __attribute__((__nonnull__))
universe@549 183 static inline void cxMapClear(CxMap *map) {
universe@549 184 map->cl->clear(map);
universe@549 185 }
universe@549 186
universe@549 187 /**
universe@549 188 * Puts a key/value-pair into the map.
universe@549 189 *
universe@549 190 * @param map the map
universe@549 191 * @param key the key
universe@549 192 * @param value the value
universe@549 193 * @return 0 on success, non-zero value on failure
universe@549 194 */
universe@549 195 __attribute__((__nonnull__))
universe@549 196 static inline int cxMapPut(
universe@549 197 CxMap *map,
universe@563 198 CxHashKey key,
universe@550 199 void *value
universe@549 200 ) {
universe@549 201 return map->cl->put(map, key, value);
universe@549 202 }
universe@549 203
universe@549 204 /**
universe@549 205 * Retrieves a value by using a key.
universe@549 206 *
universe@549 207 * @param map the map
universe@549 208 * @param key the key
universe@549 209 * @return the value
universe@549 210 */
universe@549 211 __attribute__((__nonnull__, __warn_unused_result__))
universe@553 212 static inline void *cxMapGet(
universe@549 213 CxMap const *map,
universe@563 214 CxHashKey key
universe@549 215 ) {
universe@549 216 return map->cl->get(map, key);
universe@549 217 }
universe@549 218
universe@549 219 /**
universe@549 220 * Removes a key/value-pair from the map by using the key.
universe@549 221 *
universe@549 222 * @param map the map
universe@549 223 * @param key the key
universe@549 224 * @return the removed value
universe@549 225 */
universe@549 226 __attribute__((__nonnull__, __warn_unused_result__))
universe@553 227 static inline void *cxMapRemove(
universe@549 228 CxMap *map,
universe@563 229 CxHashKey key
universe@549 230 ) {
universe@549 231 return map->cl->remove(map, key);
universe@549 232 }
universe@549 233
universe@549 234 // TODO: set-like map operations (union, intersect, difference)
universe@549 235
universe@549 236 /**
universe@549 237 * Creates a value iterator for a map.
universe@549 238 *
universe@549 239 * \note An iterator iterates over all elements successively. Therefore the order
universe@549 240 * highly depends on the map implementation and may change arbitrarily when the contents change.
universe@549 241 *
universe@549 242 * @param map the map to create the iterator for
universe@549 243 * @return an iterator for the currently stored values
universe@549 244 */
universe@549 245 __attribute__((__nonnull__, __warn_unused_result__))
universe@551 246 static inline CxIterator cxMapIteratorValues(CxMap *map) {
universe@549 247 return map->cl->iterator_values(map);
universe@549 248 }
universe@549 249
universe@549 250 /**
universe@549 251 * Creates a key iterator for a map.
universe@549 252 *
universe@564 253 * The elements of the iterator are keys of type CxHashKey.
universe@555 254 *
universe@549 255 * \note An iterator iterates over all elements successively. Therefore the order
universe@549 256 * highly depends on the map implementation and may change arbitrarily when the contents change.
universe@549 257 *
universe@549 258 * @param map the map to create the iterator for
universe@549 259 * @return an iterator for the currently stored keys
universe@549 260 */
universe@549 261 __attribute__((__nonnull__, __warn_unused_result__))
universe@551 262 static inline CxIterator cxMapIteratorKeys(CxMap *map) {
universe@549 263 return map->cl->iterator_keys(map);
universe@549 264 }
universe@549 265
universe@549 266 /**
universe@549 267 * Creates an iterator for a map.
universe@549 268 *
universe@555 269 * The elements of the iterator are key/value pairs of type CxMapEntry.
universe@549 270 *
universe@549 271 * \note An iterator iterates over all elements successively. Therefore the order
universe@549 272 * highly depends on the map implementation and may change arbitrarily when the contents change.
universe@549 273 *
universe@549 274 * @param map the map to create the iterator for
universe@555 275 * @return an iterator for the currently stored entries
universe@549 276 * @see cxMapIteratorKeys()
universe@549 277 * @see cxMapIteratorValues()
universe@549 278 */
universe@549 279 __attribute__((__nonnull__, __warn_unused_result__))
universe@551 280 static inline CxIterator cxMapIterator(CxMap *map) {
universe@549 281 return map->cl->iterator(map);
universe@549 282 }
universe@549 283
universe@630 284
universe@630 285 /**
universe@630 286 * Creates a mutating iterator over the values of a map.
universe@630 287 *
universe@630 288 * \note An iterator iterates over all elements successively. Therefore the order
universe@630 289 * highly depends on the map implementation and may change arbitrarily when the contents change.
universe@630 290 *
universe@630 291 * @param map the map to create the iterator for
universe@630 292 * @return an iterator for the currently stored values
universe@630 293 */
universe@630 294 __attribute__((__nonnull__, __warn_unused_result__))
universe@630 295 static inline CxMutIterator cxMapMutIteratorValues(CxMap *map) {
universe@630 296 return map->cl->mut_iterator_values(map);
universe@630 297 }
universe@630 298
universe@630 299 /**
universe@630 300 * Creates a mutating iterator over the keys of a map.
universe@630 301 *
universe@630 302 * The elements of the iterator are keys of type CxHashKey.
universe@630 303 *
universe@630 304 * \note An iterator iterates over all elements successively. Therefore the order
universe@630 305 * highly depends on the map implementation and may change arbitrarily when the contents change.
universe@630 306 *
universe@630 307 * @param map the map to create the iterator for
universe@630 308 * @return an iterator for the currently stored keys
universe@630 309 */
universe@630 310 __attribute__((__nonnull__, __warn_unused_result__))
universe@630 311 static inline CxMutIterator cxMapMutIteratorKeys(CxMap *map) {
universe@630 312 return map->cl->mut_iterator_keys(map);
universe@630 313 }
universe@630 314
universe@630 315 /**
universe@630 316 * Creates a mutating iterator for a map.
universe@630 317 *
universe@630 318 * The elements of the iterator are key/value pairs of type CxMapEntry.
universe@630 319 *
universe@630 320 * \note An iterator iterates over all elements successively. Therefore the order
universe@630 321 * highly depends on the map implementation and may change arbitrarily when the contents change.
universe@630 322 *
universe@630 323 * @param map the map to create the iterator for
universe@630 324 * @return an iterator for the currently stored entries
universe@630 325 * @see cxMapMutIteratorKeys()
universe@630 326 * @see cxMapMutIteratorValues()
universe@630 327 */
universe@630 328 __attribute__((__nonnull__, __warn_unused_result__))
universe@630 329 static inline CxMutIterator cxMapMutIterator(CxMap *map) {
universe@630 330 return map->cl->mut_iterator(map);
universe@630 331 }
universe@630 332
universe@549 333 #ifdef __cplusplus
universe@549 334 }
universe@549 335 #endif
universe@549 336
universe@549 337 #endif // UCX_MAP_H

mercurial