Fri, 19 Dec 2025 17:53:18 +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 | ||
| 889 | 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}; |
| 889 | 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 | 0, |
|
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 | NULL, |
|
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, |
| 891 | 78 | NULL, |
| 79 | NULL, | |
| 80 | NULL, | |
|
854
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
81 | false, |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
82 | true |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
83 | }, |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
84 | &cx_empty_map_class |
| 750 | 85 | }; |
| 86 | ||
| 87 | CxMap *const cxEmptyMap = &cx_empty_map; | |
| 88 | ||
| 89 | // </editor-fold> | |
| 90 | ||
| 889 | 91 | void cxMapClear(CxMap *map) { |
| 92 | map->cl->clear(map); | |
| 93 | } | |
| 94 | ||
| 95 | size_t cxMapSize(const CxMap *map) { | |
| 96 | return map->collection.size; | |
| 97 | } | |
| 98 | ||
| 99 | CxMapIterator cxMapIteratorValues(const CxMap *map) { | |
| 886 | 100 | if (map == NULL) map = cxEmptyMap; |
| 889 | 101 | return map->cl->iterator(map, CX_MAP_ITERATOR_VALUES); |
| 102 | } | |
| 103 | ||
| 104 | CxMapIterator cxMapIteratorKeys(const CxMap *map) { | |
| 105 | if (map == NULL) map = cxEmptyMap; | |
| 106 | return map->cl->iterator(map, CX_MAP_ITERATOR_KEYS); | |
| 750 | 107 | } |
| 108 | ||
| 889 | 109 | CxMapIterator cxMapIterator(const CxMap *map) { |
| 886 | 110 | if (map == NULL) map = cxEmptyMap; |
| 889 | 111 | return map->cl->iterator(map, CX_MAP_ITERATOR_PAIRS); |
| 112 | } | |
| 113 | ||
| 114 | int cx_map_put(CxMap *map, CxHashKey key, void *value) { | |
| 891 | 115 | return map->cl->put(map, key, value).key == NULL; |
| 750 | 116 | } |
| 117 | ||
| 889 | 118 | void *cx_map_emplace(CxMap *map, CxHashKey key) { |
| 891 | 119 | const CxMapEntry entry = map->cl->put(map, key, NULL); |
| 120 | if (entry.key == NULL) return NULL; | |
| 121 | return entry.value; | |
| 889 | 122 | } |
| 123 | ||
| 124 | void *cx_map_get(const CxMap *map, CxHashKey key) { | |
| 125 | return map->cl->get(map, key); | |
| 126 | } | |
| 127 | ||
| 128 | int cx_map_remove(CxMap *map, CxHashKey key, void *targetbuf) { | |
| 129 | return map->cl->remove(map, key, targetbuf); | |
| 750 | 130 | } |
| 852 | 131 | |
| 132 | void cxMapFree(CxMap *map) { | |
| 133 | if (map == NULL) return; | |
| 134 | map->cl->deallocate(map); | |
| 135 | } | |
| 889 | 136 | |
| 137 | static void cx_map_remove_uninitialized_entry(CxMap *map, CxHashKey key) { | |
| 138 | cx_destructor_func destr_bak = map->collection.simple_destructor; | |
| 139 | cx_destructor_func2 destr2_bak = map->collection.advanced_destructor; | |
| 140 | map->collection.simple_destructor = NULL; | |
| 141 | map->collection.advanced_destructor = NULL; | |
| 142 | cxMapRemove(map, key); | |
| 143 | map->collection.simple_destructor = destr_bak; | |
| 144 | map->collection.advanced_destructor = destr2_bak; | |
| 145 | } | |
| 146 | ||
| 891 | 147 | static void* cx_map_shallow_clone_func(void *dst, const void *src, const CxAllocator *al, void *data) { |
| 148 | size_t elem_size = *(size_t*)data; | |
| 149 | if (dst == NULL) dst = cxMalloc(al, elem_size); | |
| 150 | if (dst != NULL) memcpy(dst, src, elem_size); | |
| 151 | return dst; | |
| 152 | } | |
| 153 | ||
| 154 | #define use_shallow_clone_func(map) cx_map_shallow_clone_func, NULL, (void*)&((map)->collection.elem_size) | |
| 155 | ||
| 889 | 156 | int cxMapClone(CxMap *dst, const CxMap *src, cx_clone_func clone_func, |
| 157 | const CxAllocator *clone_allocator, void *data) { | |
| 158 | if (clone_allocator == NULL) clone_allocator = cxDefaultAllocator; | |
| 159 | CxMapIterator src_iter = cxMapIterator(src); | |
| 160 | for (size_t i = 0; i < cxMapSize(src); i++) { | |
| 161 | const CxMapEntry *entry = cxIteratorCurrent(src_iter); | |
| 162 | void **dst_mem = cxMapEmplace(dst, *(entry->key)); | |
| 163 | if (dst_mem == NULL) { | |
| 164 | return 1; // LCOV_EXCL_LINE | |
| 165 | } | |
| 166 | void *target = cxCollectionStoresPointers(dst) ? NULL : dst_mem; | |
| 167 | void *dst_ptr = clone_func(target, entry->value, clone_allocator, data); | |
| 168 | if (dst_ptr == NULL) { | |
| 169 | cx_map_remove_uninitialized_entry(dst, *(entry->key)); | |
| 170 | return 1; | |
| 171 | } | |
| 172 | if (cxCollectionStoresPointers(dst)) { | |
| 173 | *dst_mem = dst_ptr; | |
| 174 | } | |
| 175 | cxIteratorNext(src_iter); | |
| 176 | } | |
| 177 | return 0; | |
| 178 | } | |
| 179 | ||
| 180 | int cxMapDifference(CxMap *dst, const CxMap *minuend, const CxMap *subtrahend, | |
| 181 | cx_clone_func clone_func, const CxAllocator *clone_allocator, void *data) { | |
| 182 | if (clone_allocator == NULL) clone_allocator = cxDefaultAllocator; | |
| 183 | ||
| 184 | CxMapIterator src_iter = cxMapIterator(minuend); | |
| 185 | cx_foreach(const CxMapEntry *, entry, src_iter) { | |
| 186 | if (cxMapContains(subtrahend, *entry->key)) { | |
| 187 | continue; | |
| 188 | } | |
| 189 | void** dst_mem = cxMapEmplace(dst, *entry->key); | |
| 190 | if (dst_mem == NULL) { | |
| 191 | return 1; // LCOV_EXCL_LINE | |
| 192 | } | |
| 193 | void *target = cxCollectionStoresPointers(dst) ? NULL : dst_mem; | |
| 194 | void* dst_ptr = clone_func(target, entry->value, clone_allocator, data); | |
| 195 | if (dst_ptr == NULL) { | |
| 196 | cx_map_remove_uninitialized_entry(dst, *(entry->key)); | |
| 197 | return 1; | |
| 198 | } | |
| 199 | if (cxCollectionStoresPointers(dst)) { | |
| 200 | *dst_mem = dst_ptr; | |
| 201 | } | |
| 202 | } | |
| 203 | return 0; | |
| 204 | } | |
| 205 | ||
| 206 | int cxMapListDifference(CxMap *dst, const CxMap *src, const CxList *keys, | |
| 207 | cx_clone_func clone_func, const CxAllocator *clone_allocator, void *data) { | |
| 208 | if (clone_allocator == NULL) clone_allocator = cxDefaultAllocator; | |
| 209 | ||
| 210 | CxMapIterator src_iter = cxMapIterator(src); | |
| 211 | cx_foreach(const CxMapEntry *, entry, src_iter) { | |
| 212 | if (cxListContains(keys, entry->key)) { | |
| 213 | continue; | |
| 214 | } | |
| 215 | void** dst_mem = cxMapEmplace(dst, *entry->key); | |
| 216 | if (dst_mem == NULL) { | |
| 217 | return 1; // LCOV_EXCL_LINE | |
| 218 | } | |
| 219 | void *target = cxCollectionStoresPointers(dst) ? NULL : dst_mem; | |
| 220 | void* dst_ptr = clone_func(target, entry->value, clone_allocator, data); | |
| 221 | if (dst_ptr == NULL) { | |
| 222 | cx_map_remove_uninitialized_entry(dst, *(entry->key)); | |
| 223 | return 1; | |
| 224 | } | |
| 225 | if (cxCollectionStoresPointers(dst)) { | |
| 226 | *dst_mem = dst_ptr; | |
| 227 | } | |
| 228 | } | |
| 229 | return 0; | |
| 230 | } | |
| 231 | ||
| 232 | int cxMapIntersection(CxMap *dst, const CxMap *src, const CxMap *other, | |
| 233 | cx_clone_func clone_func, const CxAllocator *clone_allocator, void *data) { | |
| 234 | if (clone_allocator == NULL) clone_allocator = cxDefaultAllocator; | |
| 235 | ||
| 236 | CxMapIterator src_iter = cxMapIterator(src); | |
| 237 | cx_foreach(const CxMapEntry *, entry, src_iter) { | |
| 238 | if (!cxMapContains(other, *entry->key)) { | |
| 239 | continue; | |
| 240 | } | |
| 241 | void** dst_mem = cxMapEmplace(dst, *entry->key); | |
| 242 | if (dst_mem == NULL) { | |
| 243 | return 1; // LCOV_EXCL_LINE | |
| 244 | } | |
| 245 | void *target = cxCollectionStoresPointers(dst) ? NULL : dst_mem; | |
| 246 | void* dst_ptr = clone_func(target, entry->value, clone_allocator, data); | |
| 247 | if (dst_ptr == NULL) { | |
| 248 | cx_map_remove_uninitialized_entry(dst, *(entry->key)); | |
| 249 | return 1; | |
| 250 | } | |
| 251 | if (cxCollectionStoresPointers(dst)) { | |
| 252 | *dst_mem = dst_ptr; | |
| 253 | } | |
| 254 | } | |
| 255 | return 0; | |
| 256 | } | |
| 257 | ||
| 258 | int cxMapListIntersection(CxMap *dst, const CxMap *src, const CxList *keys, | |
| 259 | cx_clone_func clone_func, const CxAllocator *clone_allocator, void *data) { | |
| 260 | if (clone_allocator == NULL) clone_allocator = cxDefaultAllocator; | |
| 261 | ||
| 262 | CxMapIterator src_iter = cxMapIterator(src); | |
| 263 | cx_foreach(const CxMapEntry *, entry, src_iter) { | |
| 264 | if (!cxListContains(keys, entry->key)) { | |
| 265 | continue; | |
| 266 | } | |
| 267 | void** dst_mem = cxMapEmplace(dst, *entry->key); | |
| 268 | if (dst_mem == NULL) { | |
| 269 | return 1; // LCOV_EXCL_LINE | |
| 270 | } | |
| 271 | void *target = cxCollectionStoresPointers(dst) ? NULL : dst_mem; | |
| 272 | void* dst_ptr = clone_func(target, entry->value, clone_allocator, data); | |
| 273 | if (dst_ptr == NULL) { | |
| 274 | cx_map_remove_uninitialized_entry(dst, *(entry->key)); | |
| 275 | return 1; | |
| 276 | } | |
| 277 | if (cxCollectionStoresPointers(dst)) { | |
| 278 | *dst_mem = dst_ptr; | |
| 279 | } | |
| 280 | } | |
| 281 | return 0; | |
| 282 | } | |
| 283 | ||
| 284 | int cxMapUnion(CxMap *dst, const CxMap *src, | |
| 285 | cx_clone_func clone_func, const CxAllocator *clone_allocator, void *data) { | |
| 286 | if (clone_allocator == NULL) clone_allocator = cxDefaultAllocator; | |
| 287 | ||
| 288 | CxMapIterator src_iter = cxMapIterator(src); | |
| 289 | cx_foreach(const CxMapEntry *, entry, src_iter) { | |
| 290 | if (cxMapContains(dst, *entry->key)) { | |
| 291 | continue; | |
| 292 | } | |
| 293 | void** dst_mem = cxMapEmplace(dst, *entry->key); | |
| 294 | if (dst_mem == NULL) { | |
| 295 | return 1; // LCOV_EXCL_LINE | |
| 296 | } | |
| 297 | void *target = cxCollectionStoresPointers(dst) ? NULL : dst_mem; | |
| 298 | void* dst_ptr = clone_func(target, entry->value, clone_allocator, data); | |
| 299 | if (dst_ptr == NULL) { | |
| 300 | cx_map_remove_uninitialized_entry(dst, *(entry->key)); | |
| 301 | return 1; | |
| 302 | } | |
| 303 | if (cxCollectionStoresPointers(dst)) { | |
| 304 | *dst_mem = dst_ptr; | |
| 305 | } | |
| 306 | } | |
| 307 | return 0; | |
| 308 | } | |
| 891 | 309 | |
| 310 | int cxMapCloneShallow(CxMap *dst, const CxMap *src) { | |
| 311 | return cxMapClone(dst, src, use_shallow_clone_func(src)); | |
| 312 | } | |
| 313 | ||
| 314 | int cxMapDifferenceShallow(CxMap *dst, const CxMap *minuend, const CxMap *subtrahend) { | |
| 315 | return cxMapDifference(dst, minuend, subtrahend, use_shallow_clone_func(minuend)); | |
| 316 | } | |
| 317 | ||
| 318 | int cxMapListDifferenceShallow(CxMap *dst, const CxMap *src, const CxList *keys) { | |
| 319 | return cxMapListDifference(dst, src, keys, use_shallow_clone_func(src)); | |
| 320 | } | |
| 321 | ||
| 322 | int cxMapIntersectionShallow(CxMap *dst, const CxMap *src, const CxMap *other) { | |
| 323 | return cxMapIntersection(dst, src, other, use_shallow_clone_func(src)); | |
| 324 | } | |
| 325 | ||
| 326 | int cxMapListIntersectionShallow(CxMap *dst, const CxMap *src, const CxList *keys) { | |
| 327 | return cxMapListIntersection(dst, src, keys, use_shallow_clone_func(src)); | |
| 328 | } | |
| 329 | ||
| 330 | int cxMapUnionShallow(CxMap *dst, const CxMap *src) { | |
| 331 | return cxMapUnion(dst, src, use_shallow_clone_func(src)); | |
| 332 | } | |
| 333 | ||
| 334 | int cxMapCompare(const CxMap *map, const CxMap *other) { | |
| 335 | // compare map sizes | |
| 336 | const size_t size_left = cxMapSize(map); | |
| 337 | const size_t size_right = cxMapSize(other); | |
| 338 | if (size_left < size_right) { | |
| 339 | return -1; | |
| 340 | } else if (size_left > size_right) { | |
| 341 | return 1; | |
| 342 | } | |
| 343 | ||
| 344 | // iterate through the first map | |
| 345 | CxMapIterator iter = cxMapIterator(map); | |
| 346 | cx_foreach(const CxMapEntry *, entry, iter) { | |
| 347 | const void *value_left = entry->value; | |
| 348 | const void *value_right = cxMapGet(other, *entry->key); | |
| 349 | // if the other map does not have the key, we are done | |
| 350 | if (value_right == NULL) { | |
| 351 | return -1; | |
| 352 | } | |
| 353 | // compare the values | |
| 354 | const int d = cx_invoke_compare_func(map, value_left, value_right); | |
| 355 | if (d != 0) { | |
| 356 | return d; | |
| 357 | } | |
| 358 | } | |
| 359 | ||
| 360 | return 0; | |
| 361 | } |