Wed, 08 Jun 2022 21:38:45 +0200
fix obsolete documentation
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@551 | 117 | CxIterator (*iterator)(CxMap *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@551 | 123 | CxIterator (*iterator_keys)(CxMap *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@551 | 129 | CxIterator (*iterator_values)(CxMap *map); |
universe@549 | 130 | }; |
universe@549 | 131 | |
universe@549 | 132 | /** |
universe@549 | 133 | * A map entry. |
universe@549 | 134 | */ |
universe@549 | 135 | struct cx_map_entry_s { |
universe@549 | 136 | /** |
universe@551 | 137 | * A pointer to the key. |
universe@549 | 138 | */ |
universe@563 | 139 | CxHashKey const *key; |
universe@549 | 140 | /** |
universe@551 | 141 | * A pointer to the value. |
universe@549 | 142 | */ |
universe@551 | 143 | void *value; |
universe@549 | 144 | }; |
universe@549 | 145 | |
universe@549 | 146 | |
universe@549 | 147 | /** |
universe@549 | 148 | * Deallocates the memory of the specified map. |
universe@549 | 149 | * |
universe@549 | 150 | * @param map the map to be destroyed |
universe@549 | 151 | */ |
universe@549 | 152 | __attribute__((__nonnull__)) |
universe@549 | 153 | static inline void cxMapDestroy(CxMap *map) { |
universe@549 | 154 | // TODO: likely to add auto-free feature for contents in the future |
universe@549 | 155 | map->cl->destructor(map); |
universe@549 | 156 | } |
universe@549 | 157 | |
universe@549 | 158 | |
universe@549 | 159 | /** |
universe@549 | 160 | * Clears a map by removing all elements. |
universe@549 | 161 | * |
universe@549 | 162 | * @param map the map to be cleared |
universe@549 | 163 | */ |
universe@549 | 164 | __attribute__((__nonnull__)) |
universe@549 | 165 | static inline void cxMapClear(CxMap *map) { |
universe@549 | 166 | map->cl->clear(map); |
universe@549 | 167 | } |
universe@549 | 168 | |
universe@549 | 169 | /** |
universe@549 | 170 | * Puts a key/value-pair into the map. |
universe@549 | 171 | * |
universe@549 | 172 | * @param map the map |
universe@549 | 173 | * @param key the key |
universe@549 | 174 | * @param value the value |
universe@549 | 175 | * @return 0 on success, non-zero value on failure |
universe@549 | 176 | */ |
universe@549 | 177 | __attribute__((__nonnull__)) |
universe@549 | 178 | static inline int cxMapPut( |
universe@549 | 179 | CxMap *map, |
universe@563 | 180 | CxHashKey key, |
universe@550 | 181 | void *value |
universe@549 | 182 | ) { |
universe@549 | 183 | return map->cl->put(map, key, value); |
universe@549 | 184 | } |
universe@549 | 185 | |
universe@549 | 186 | /** |
universe@549 | 187 | * Retrieves a value by using a key. |
universe@549 | 188 | * |
universe@549 | 189 | * @param map the map |
universe@549 | 190 | * @param key the key |
universe@549 | 191 | * @return the value |
universe@549 | 192 | */ |
universe@549 | 193 | __attribute__((__nonnull__, __warn_unused_result__)) |
universe@553 | 194 | static inline void *cxMapGet( |
universe@549 | 195 | CxMap const *map, |
universe@563 | 196 | CxHashKey key |
universe@549 | 197 | ) { |
universe@549 | 198 | return map->cl->get(map, key); |
universe@549 | 199 | } |
universe@549 | 200 | |
universe@549 | 201 | /** |
universe@549 | 202 | * Removes a key/value-pair from the map by using the key. |
universe@549 | 203 | * |
universe@549 | 204 | * @param map the map |
universe@549 | 205 | * @param key the key |
universe@549 | 206 | * @return the removed value |
universe@549 | 207 | */ |
universe@549 | 208 | __attribute__((__nonnull__, __warn_unused_result__)) |
universe@553 | 209 | static inline void *cxMapRemove( |
universe@549 | 210 | CxMap *map, |
universe@563 | 211 | CxHashKey key |
universe@549 | 212 | ) { |
universe@549 | 213 | return map->cl->remove(map, key); |
universe@549 | 214 | } |
universe@549 | 215 | |
universe@549 | 216 | // TODO: set-like map operations (union, intersect, difference) |
universe@549 | 217 | |
universe@549 | 218 | /** |
universe@549 | 219 | * Creates a value iterator for a map. |
universe@549 | 220 | * |
universe@549 | 221 | * \note An iterator iterates over all elements successively. Therefore the order |
universe@549 | 222 | * highly depends on the map implementation and may change arbitrarily when the contents change. |
universe@549 | 223 | * |
universe@549 | 224 | * @param map the map to create the iterator for |
universe@549 | 225 | * @return an iterator for the currently stored values |
universe@549 | 226 | */ |
universe@549 | 227 | __attribute__((__nonnull__, __warn_unused_result__)) |
universe@551 | 228 | static inline CxIterator cxMapIteratorValues(CxMap *map) { |
universe@549 | 229 | return map->cl->iterator_values(map); |
universe@549 | 230 | } |
universe@549 | 231 | |
universe@549 | 232 | /** |
universe@549 | 233 | * Creates a key iterator for a map. |
universe@549 | 234 | * |
universe@564 | 235 | * The elements of the iterator are keys of type CxHashKey. |
universe@555 | 236 | * |
universe@549 | 237 | * \note An iterator iterates over all elements successively. Therefore the order |
universe@549 | 238 | * highly depends on the map implementation and may change arbitrarily when the contents change. |
universe@549 | 239 | * |
universe@549 | 240 | * @param map the map to create the iterator for |
universe@549 | 241 | * @return an iterator for the currently stored keys |
universe@549 | 242 | */ |
universe@549 | 243 | __attribute__((__nonnull__, __warn_unused_result__)) |
universe@551 | 244 | static inline CxIterator cxMapIteratorKeys(CxMap *map) { |
universe@549 | 245 | return map->cl->iterator_keys(map); |
universe@549 | 246 | } |
universe@549 | 247 | |
universe@549 | 248 | /** |
universe@549 | 249 | * Creates an iterator for a map. |
universe@549 | 250 | * |
universe@555 | 251 | * The elements of the iterator are key/value pairs of type CxMapEntry. |
universe@549 | 252 | * |
universe@549 | 253 | * \note An iterator iterates over all elements successively. Therefore the order |
universe@549 | 254 | * highly depends on the map implementation and may change arbitrarily when the contents change. |
universe@549 | 255 | * |
universe@549 | 256 | * @param map the map to create the iterator for |
universe@555 | 257 | * @return an iterator for the currently stored entries |
universe@549 | 258 | * @see cxMapIteratorKeys() |
universe@549 | 259 | * @see cxMapIteratorValues() |
universe@549 | 260 | */ |
universe@549 | 261 | __attribute__((__nonnull__, __warn_unused_result__)) |
universe@551 | 262 | static inline CxIterator cxMapIterator(CxMap *map) { |
universe@549 | 263 | return map->cl->iterator(map); |
universe@549 | 264 | } |
universe@549 | 265 | |
universe@549 | 266 | #ifdef __cplusplus |
universe@549 | 267 | } |
universe@549 | 268 | #endif |
universe@549 | 269 | |
universe@549 | 270 | #endif // UCX_MAP_H |