src/cx/map.h

Thu, 19 May 2022 14:30:20 +0200

author
Mike Becker <universe@uap-core.de>
date
Thu, 19 May 2022 14:30:20 +0200
changeset 550
89b2a83728b1
parent 549
d7f0b5a9a985
child 551
2946e13c89a4
permissions
-rw-r--r--

#189 basic map implementation

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@549 43
universe@549 44 #ifdef __cplusplus
universe@549 45 extern "C" {
universe@549 46 #endif
universe@549 47
universe@549 48 /** Type for the UCX map. */
universe@549 49 typedef struct cx_map_s CxMap;
universe@549 50
universe@549 51 /** Type for a map entry. */
universe@549 52 typedef struct cx_map_entry_s CxMapEntry;
universe@549 53
universe@549 54 /** Type for map class definitions. */
universe@549 55 typedef struct cx_map_class_s cx_map_class;
universe@549 56
universe@549 57 /** Structure for the UCX map. */
universe@549 58 struct cx_map_s {
universe@549 59 /** The map class definition. */
universe@549 60 cx_map_class *cl;
universe@549 61 /** An allocator that is used for the map elements. */
universe@549 62 CxAllocator *allocator;
universe@549 63 /** The number of elements currently stored. */
universe@549 64 size_t size;
universe@549 65 // TODO: elemsize + a flag if values shall be copied to the map
universe@549 66 };
universe@549 67
universe@549 68 /**
universe@549 69 * The class definition for arbitrary maps.
universe@549 70 */
universe@549 71 struct cx_map_class_s {
universe@549 72 /**
universe@549 73 * Deallocates the entire memory.
universe@549 74 */
universe@549 75 __attribute__((__nonnull__))
universe@549 76 void (*destructor)(struct cx_map_s *map);
universe@549 77
universe@549 78 /**
universe@549 79 * Removes all elements.
universe@549 80 */
universe@549 81 __attribute__((__nonnull__))
universe@549 82 void (*clear)(struct cx_map_s *map);
universe@549 83
universe@549 84 /**
universe@549 85 * Add or overwrite an element.
universe@549 86 */
universe@549 87 __attribute__((__nonnull__))
universe@549 88 int (*put)(
universe@549 89 CxMap *map,
universe@549 90 CxDataPtr key,
universe@550 91 void *value
universe@549 92 );
universe@549 93
universe@549 94 /**
universe@549 95 * Returns an element.
universe@549 96 */
universe@549 97 __attribute__((__nonnull__, __warn_unused_result__))
universe@549 98 void *(*get)(
universe@549 99 CxMap const *map,
universe@549 100 CxDataPtr key
universe@549 101 );
universe@549 102
universe@549 103 /**
universe@549 104 * Removes an element.
universe@549 105 */
universe@549 106 __attribute__((__nonnull__, __warn_unused_result__))
universe@549 107 void *(*remove)(
universe@550 108 CxMap *map,
universe@549 109 CxDataPtr key
universe@549 110 );
universe@549 111
universe@549 112 /**
universe@549 113 * Iterator over the key/value pairs.
universe@549 114 */
universe@549 115 __attribute__((__nonnull__, __warn_unused_result__))
universe@549 116 CxIterator (*iterator)(CxMap const *map);
universe@549 117
universe@549 118 /**
universe@549 119 * Iterator over the keys.
universe@549 120 */
universe@549 121 __attribute__((__nonnull__, __warn_unused_result__))
universe@549 122 CxIterator (*iterator_keys)(CxMap const *map);
universe@549 123
universe@549 124 /**
universe@549 125 * Iterator over the values.
universe@549 126 */
universe@549 127 __attribute__((__nonnull__, __warn_unused_result__))
universe@549 128 CxIterator (*iterator_values)(CxMap const *map);
universe@549 129 };
universe@549 130
universe@549 131 /**
universe@549 132 * A map entry.
universe@549 133 */
universe@549 134 struct cx_map_entry_s {
universe@549 135 /**
universe@549 136 * The key.
universe@549 137 */
universe@549 138 CxDataPtr key;
universe@549 139 /**
universe@549 140 * The value.
universe@549 141 */
universe@549 142 void const *value;
universe@549 143 };
universe@549 144
universe@549 145
universe@549 146 /**
universe@549 147 * Deallocates the memory of the specified map.
universe@549 148 *
universe@549 149 * @param map the map to be destroyed
universe@549 150 */
universe@549 151 __attribute__((__nonnull__))
universe@549 152 static inline void cxMapDestroy(CxMap *map) {
universe@549 153 // TODO: likely to add auto-free feature for contents in the future
universe@549 154 map->cl->destructor(map);
universe@549 155 }
universe@549 156
universe@549 157
universe@549 158 /**
universe@549 159 * Clears a map by removing all elements.
universe@549 160 *
universe@549 161 * @param map the map to be cleared
universe@549 162 */
universe@549 163 __attribute__((__nonnull__))
universe@549 164 static inline void cxMapClear(CxMap *map) {
universe@549 165 map->cl->clear(map);
universe@549 166 }
universe@549 167
universe@549 168 /**
universe@549 169 * Puts a key/value-pair into the map.
universe@549 170 *
universe@549 171 * @param map the map
universe@549 172 * @param key the key
universe@549 173 * @param value the value
universe@549 174 * @return 0 on success, non-zero value on failure
universe@549 175 */
universe@549 176 __attribute__((__nonnull__))
universe@549 177 static inline int cxMapPut(
universe@549 178 CxMap *map,
universe@549 179 CxDataPtr key,
universe@550 180 void *value
universe@549 181 ) {
universe@549 182 return map->cl->put(map, key, value);
universe@549 183 }
universe@549 184
universe@549 185 /**
universe@549 186 * Retrieves a value by using a key.
universe@549 187 *
universe@549 188 * @param map the map
universe@549 189 * @param key the key
universe@549 190 * @return the value
universe@549 191 */
universe@549 192 __attribute__((__nonnull__, __warn_unused_result__))
universe@549 193 void *cxMapGet(
universe@549 194 CxMap const *map,
universe@549 195 CxDataPtr key
universe@549 196 ) {
universe@549 197 return map->cl->get(map, key);
universe@549 198 }
universe@549 199
universe@549 200 /**
universe@549 201 * Removes a key/value-pair from the map by using the key.
universe@549 202 *
universe@549 203 * @param map the map
universe@549 204 * @param key the key
universe@549 205 * @return the removed value
universe@549 206 */
universe@549 207 __attribute__((__nonnull__, __warn_unused_result__))
universe@549 208 void *cxMapRemove(
universe@549 209 CxMap *map,
universe@549 210 CxDataPtr key
universe@549 211 ) {
universe@549 212 return map->cl->remove(map, key);
universe@549 213 }
universe@549 214
universe@549 215 // TODO: set-like map operations (union, intersect, difference)
universe@549 216
universe@549 217 /**
universe@549 218 * Creates a value iterator for a map.
universe@549 219 *
universe@549 220 * \note An iterator iterates over all elements successively. Therefore the order
universe@549 221 * highly depends on the map implementation and may change arbitrarily when the contents change.
universe@549 222 *
universe@549 223 * @param map the map to create the iterator for
universe@549 224 * @return an iterator for the currently stored values
universe@549 225 */
universe@549 226 __attribute__((__nonnull__, __warn_unused_result__))
universe@549 227 static inline CxIterator cxMapIteratorValues(CxMap const *map) {
universe@549 228 return map->cl->iterator_values(map);
universe@549 229 }
universe@549 230
universe@549 231 /**
universe@549 232 * Creates a key iterator for a map.
universe@549 233 *
universe@549 234 * \note An iterator iterates over all elements successively. Therefore the order
universe@549 235 * highly depends on the map implementation and may change arbitrarily when the contents change.
universe@549 236 *
universe@549 237 * @param map the map to create the iterator for
universe@549 238 * @return an iterator for the currently stored keys
universe@549 239 */
universe@549 240 __attribute__((__nonnull__, __warn_unused_result__))
universe@549 241 static inline CxIterator cxMapIteratorKeys(CxMap const *map) {
universe@549 242 return map->cl->iterator_keys(map);
universe@549 243 }
universe@549 244
universe@549 245 /**
universe@549 246 * Creates an iterator for a map.
universe@549 247 *
universe@549 248 * The values of the iterator are dynamically created key/value pairs of type CxMapEntry.
universe@549 249 *
universe@549 250 * \note An iterator iterates over all elements successively. Therefore the order
universe@549 251 * highly depends on the map implementation and may change arbitrarily when the contents change.
universe@549 252 *
universe@549 253 * @param map the map to create the iterator for
universe@549 254 * @return an iterator for the currently stored keys
universe@549 255 * @see cxMapIteratorKeys()
universe@549 256 * @see cxMapIteratorValues()
universe@549 257 */
universe@549 258 __attribute__((__nonnull__, __warn_unused_result__))
universe@549 259 static inline CxIterator cxMapIterator(CxMap const *map) {
universe@549 260 return map->cl->iterator(map);
universe@549 261 }
universe@549 262
universe@549 263
universe@549 264 #ifdef __cplusplus
universe@549 265 }
universe@549 266 #endif
universe@549 267
universe@549 268 #endif // UCX_MAP_H

mercurial