Sat, 08 Nov 2025 23:06:11 +0100
update ucx
| 750 | 1 | /* |
| 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. | |
| 3 | * | |
| 4 | * Copyright 2023 Mike Becker, Olaf Wintermann All rights reserved. | |
| 5 | * | |
| 6 | * Redistribution and use in source and binary forms, with or without | |
| 7 | * modification, are permitted provided that the following conditions are met: | |
| 8 | * | |
| 9 | * 1. Redistributions of source code must retain the above copyright | |
| 10 | * notice, this list of conditions and the following disclaimer. | |
| 11 | * | |
| 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. | |
| 15 | * | |
| 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. | |
| 27 | */ | |
| 28 | ||
| 29 | #include "cx/map.h" | |
| 30 | #include <string.h> | |
| 31 | ||
| 888 | 32 | #include "cx/list.h" |
| 33 | ||
| 750 | 34 | // <editor-fold desc="empty map implementation"> |
| 35 | ||
| 852 | 36 | static void cx_empty_map_noop(cx_attr_unused CxMap *map) { |
| 750 | 37 | // this is a noop, but MUST be implemented |
| 38 | } | |
| 39 | ||
| 40 | static void *cx_empty_map_get( | |
| 852 | 41 | cx_attr_unused const CxMap *map, |
| 42 | cx_attr_unused CxHashKey key | |
| 750 | 43 | ) { |
| 44 | return NULL; | |
| 45 | } | |
| 46 | ||
| 852 | 47 | static bool cx_empty_map_iter_valid(cx_attr_unused const void *iter) { |
| 750 | 48 | return false; |
| 49 | } | |
| 50 | ||
|
854
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
51 | static CxMapIterator cx_empty_map_iterator( |
| 852 | 52 | const struct cx_map_s *map, |
| 53 | cx_attr_unused enum cx_map_iterator_type type | |
| 750 | 54 | ) { |
|
854
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
55 | CxMapIterator iter = {0}; |
| 888 | 56 | iter.map = (CxMap*) map; |
| 750 | 57 | iter.base.valid = cx_empty_map_iter_valid; |
| 58 | return iter; | |
| 59 | } | |
| 60 | ||
| 61 | static struct cx_map_class_s cx_empty_map_class = { | |
| 62 | cx_empty_map_noop, | |
| 63 | cx_empty_map_noop, | |
| 64 | NULL, | |
| 65 | cx_empty_map_get, | |
| 66 | NULL, | |
| 67 | cx_empty_map_iterator | |
| 68 | }; | |
| 69 | ||
| 70 | CxMap cx_empty_map = { | |
|
854
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
71 | { |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
72 | NULL, |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
73 | NULL, |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
74 | 0, |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
75 | 0, |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
76 | NULL, |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
77 | NULL, |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
78 | NULL, |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
79 | false, |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
80 | true |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
81 | }, |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
82 | &cx_empty_map_class |
| 750 | 83 | }; |
| 84 | ||
| 85 | CxMap *const cxEmptyMap = &cx_empty_map; | |
| 86 | ||
| 87 | // </editor-fold> | |
| 88 | ||
| 888 | 89 | void cxMapClear(CxMap *map) { |
| 90 | map->cl->clear(map); | |
| 91 | } | |
| 92 | ||
| 93 | size_t cxMapSize(const CxMap *map) { | |
| 94 | return map->collection.size; | |
| 95 | } | |
| 96 | ||
| 97 | CxMapIterator cxMapIteratorValues(const CxMap *map) { | |
| 98 | if (map == NULL) map = cxEmptyMap; | |
| 99 | return map->cl->iterator(map, CX_MAP_ITERATOR_VALUES); | |
| 100 | } | |
| 101 | ||
| 102 | CxMapIterator cxMapIteratorKeys(const CxMap *map) { | |
| 103 | if (map == NULL) map = cxEmptyMap; | |
| 104 | return map->cl->iterator(map, CX_MAP_ITERATOR_KEYS); | |
| 750 | 105 | } |
| 106 | ||
| 888 | 107 | CxMapIterator cxMapIterator(const CxMap *map) { |
| 108 | if (map == NULL) map = cxEmptyMap; | |
| 109 | return map->cl->iterator(map, CX_MAP_ITERATOR_PAIRS); | |
| 110 | } | |
| 111 | ||
| 112 | int cx_map_put(CxMap *map, CxHashKey key, void *value) { | |
| 113 | return map->cl->put(map, key, value) == NULL; | |
| 750 | 114 | } |
| 115 | ||
| 888 | 116 | void *cx_map_emplace(CxMap *map, CxHashKey key) { |
| 117 | return map->cl->put(map, key, NULL); | |
| 118 | } | |
| 119 | ||
| 120 | void *cx_map_get(const CxMap *map, CxHashKey key) { | |
| 121 | return map->cl->get(map, key); | |
| 122 | } | |
| 123 | ||
| 124 | int cx_map_remove(CxMap *map, CxHashKey key, void *targetbuf) { | |
| 125 | return map->cl->remove(map, key, targetbuf); | |
| 750 | 126 | } |
| 852 | 127 | |
| 128 | void cxMapFree(CxMap *map) { | |
| 129 | if (map == NULL) return; | |
| 130 | map->cl->deallocate(map); | |
| 131 | } | |
| 888 | 132 | |
| 133 | static void cx_map_remove_uninitialized_entry(CxMap *map, CxHashKey key) { | |
| 134 | cx_destructor_func destr_bak = map->collection.simple_destructor; | |
| 135 | cx_destructor_func2 destr2_bak = map->collection.advanced_destructor; | |
| 136 | map->collection.simple_destructor = NULL; | |
| 137 | map->collection.advanced_destructor = NULL; | |
| 138 | cxMapRemove(map, key); | |
| 139 | map->collection.simple_destructor = destr_bak; | |
| 140 | map->collection.advanced_destructor = destr2_bak; | |
| 141 | } | |
| 142 | ||
| 143 | int cxMapClone(CxMap *dst, const CxMap *src, cx_clone_func clone_func, | |
| 144 | const CxAllocator *clone_allocator, void *data) { | |
| 145 | if (clone_allocator == NULL) clone_allocator = cxDefaultAllocator; | |
| 146 | CxMapIterator src_iter = cxMapIterator(src); | |
| 147 | for (size_t i = 0; i < cxMapSize(src); i++) { | |
| 148 | const CxMapEntry *entry = cxIteratorCurrent(src_iter); | |
| 149 | void **dst_mem = cxMapEmplace(dst, *(entry->key)); | |
| 150 | if (dst_mem == NULL) { | |
| 151 | return 1; // LCOV_EXCL_LINE | |
| 152 | } | |
| 153 | void *target = cxCollectionStoresPointers(dst) ? NULL : dst_mem; | |
| 154 | void *dst_ptr = clone_func(target, entry->value, clone_allocator, data); | |
| 155 | if (dst_ptr == NULL) { | |
| 156 | cx_map_remove_uninitialized_entry(dst, *(entry->key)); | |
| 157 | return 1; | |
| 158 | } | |
| 159 | if (cxCollectionStoresPointers(dst)) { | |
| 160 | *dst_mem = dst_ptr; | |
| 161 | } | |
| 162 | cxIteratorNext(src_iter); | |
| 163 | } | |
| 164 | return 0; | |
| 165 | } | |
| 166 | ||
| 167 | int cxMapDifference(CxMap *dst, const CxMap *minuend, const CxMap *subtrahend, | |
| 168 | cx_clone_func clone_func, const CxAllocator *clone_allocator, void *data) { | |
| 169 | if (clone_allocator == NULL) clone_allocator = cxDefaultAllocator; | |
| 170 | ||
| 171 | CxMapIterator src_iter = cxMapIterator(minuend); | |
| 172 | cx_foreach(const CxMapEntry *, entry, src_iter) { | |
| 173 | if (cxMapContains(subtrahend, *entry->key)) { | |
| 174 | continue; | |
| 175 | } | |
| 176 | void** dst_mem = cxMapEmplace(dst, *entry->key); | |
| 177 | if (dst_mem == NULL) { | |
| 178 | return 1; // LCOV_EXCL_LINE | |
| 179 | } | |
| 180 | void *target = cxCollectionStoresPointers(dst) ? NULL : dst_mem; | |
| 181 | void* dst_ptr = clone_func(target, entry->value, clone_allocator, data); | |
| 182 | if (dst_ptr == NULL) { | |
| 183 | cx_map_remove_uninitialized_entry(dst, *(entry->key)); | |
| 184 | return 1; | |
| 185 | } | |
| 186 | if (cxCollectionStoresPointers(dst)) { | |
| 187 | *dst_mem = dst_ptr; | |
| 188 | } | |
| 189 | } | |
| 190 | return 0; | |
| 191 | } | |
| 192 | ||
| 193 | int cxMapListDifference(CxMap *dst, const CxMap *src, const CxList *keys, | |
| 194 | cx_clone_func clone_func, const CxAllocator *clone_allocator, void *data) { | |
| 195 | if (clone_allocator == NULL) clone_allocator = cxDefaultAllocator; | |
| 196 | ||
| 197 | CxMapIterator src_iter = cxMapIterator(src); | |
| 198 | cx_foreach(const CxMapEntry *, entry, src_iter) { | |
| 199 | if (cxListContains(keys, entry->key)) { | |
| 200 | continue; | |
| 201 | } | |
| 202 | void** dst_mem = cxMapEmplace(dst, *entry->key); | |
| 203 | if (dst_mem == NULL) { | |
| 204 | return 1; // LCOV_EXCL_LINE | |
| 205 | } | |
| 206 | void *target = cxCollectionStoresPointers(dst) ? NULL : dst_mem; | |
| 207 | void* dst_ptr = clone_func(target, entry->value, clone_allocator, data); | |
| 208 | if (dst_ptr == NULL) { | |
| 209 | cx_map_remove_uninitialized_entry(dst, *(entry->key)); | |
| 210 | return 1; | |
| 211 | } | |
| 212 | if (cxCollectionStoresPointers(dst)) { | |
| 213 | *dst_mem = dst_ptr; | |
| 214 | } | |
| 215 | } | |
| 216 | return 0; | |
| 217 | } | |
| 218 | ||
| 219 | int cxMapIntersection(CxMap *dst, const CxMap *src, const CxMap *other, | |
| 220 | cx_clone_func clone_func, const CxAllocator *clone_allocator, void *data) { | |
| 221 | if (clone_allocator == NULL) clone_allocator = cxDefaultAllocator; | |
| 222 | ||
| 223 | CxMapIterator src_iter = cxMapIterator(src); | |
| 224 | cx_foreach(const CxMapEntry *, entry, src_iter) { | |
| 225 | if (!cxMapContains(other, *entry->key)) { | |
| 226 | continue; | |
| 227 | } | |
| 228 | void** dst_mem = cxMapEmplace(dst, *entry->key); | |
| 229 | if (dst_mem == NULL) { | |
| 230 | return 1; // LCOV_EXCL_LINE | |
| 231 | } | |
| 232 | void *target = cxCollectionStoresPointers(dst) ? NULL : dst_mem; | |
| 233 | void* dst_ptr = clone_func(target, entry->value, clone_allocator, data); | |
| 234 | if (dst_ptr == NULL) { | |
| 235 | cx_map_remove_uninitialized_entry(dst, *(entry->key)); | |
| 236 | return 1; | |
| 237 | } | |
| 238 | if (cxCollectionStoresPointers(dst)) { | |
| 239 | *dst_mem = dst_ptr; | |
| 240 | } | |
| 241 | } | |
| 242 | return 0; | |
| 243 | } | |
| 244 | ||
| 245 | int cxMapListIntersection(CxMap *dst, const CxMap *src, const CxList *keys, | |
| 246 | cx_clone_func clone_func, const CxAllocator *clone_allocator, void *data) { | |
| 247 | if (clone_allocator == NULL) clone_allocator = cxDefaultAllocator; | |
| 248 | ||
| 249 | CxMapIterator src_iter = cxMapIterator(src); | |
| 250 | cx_foreach(const CxMapEntry *, entry, src_iter) { | |
| 251 | if (!cxListContains(keys, entry->key)) { | |
| 252 | continue; | |
| 253 | } | |
| 254 | void** dst_mem = cxMapEmplace(dst, *entry->key); | |
| 255 | if (dst_mem == NULL) { | |
| 256 | return 1; // LCOV_EXCL_LINE | |
| 257 | } | |
| 258 | void *target = cxCollectionStoresPointers(dst) ? NULL : dst_mem; | |
| 259 | void* dst_ptr = clone_func(target, entry->value, clone_allocator, data); | |
| 260 | if (dst_ptr == NULL) { | |
| 261 | cx_map_remove_uninitialized_entry(dst, *(entry->key)); | |
| 262 | return 1; | |
| 263 | } | |
| 264 | if (cxCollectionStoresPointers(dst)) { | |
| 265 | *dst_mem = dst_ptr; | |
| 266 | } | |
| 267 | } | |
| 268 | return 0; | |
| 269 | } | |
| 270 | ||
| 271 | int cxMapUnion(CxMap *dst, const CxMap *src, | |
| 272 | cx_clone_func clone_func, const CxAllocator *clone_allocator, void *data) { | |
| 273 | if (clone_allocator == NULL) clone_allocator = cxDefaultAllocator; | |
| 274 | ||
| 275 | CxMapIterator src_iter = cxMapIterator(src); | |
| 276 | cx_foreach(const CxMapEntry *, entry, src_iter) { | |
| 277 | if (cxMapContains(dst, *entry->key)) { | |
| 278 | continue; | |
| 279 | } | |
| 280 | void** dst_mem = cxMapEmplace(dst, *entry->key); | |
| 281 | if (dst_mem == NULL) { | |
| 282 | return 1; // LCOV_EXCL_LINE | |
| 283 | } | |
| 284 | void *target = cxCollectionStoresPointers(dst) ? NULL : dst_mem; | |
| 285 | void* dst_ptr = clone_func(target, entry->value, clone_allocator, data); | |
| 286 | if (dst_ptr == NULL) { | |
| 287 | cx_map_remove_uninitialized_entry(dst, *(entry->key)); | |
| 288 | return 1; | |
| 289 | } | |
| 290 | if (cxCollectionStoresPointers(dst)) { | |
| 291 | *dst_mem = dst_ptr; | |
| 292 | } | |
| 293 | } | |
| 294 | return 0; | |
| 295 | } |