Wed, 31 Dec 2025 16:40:12 +0100
update ucx to version 4.0
| 174 | 1 | /* |
| 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. | |
| 3 | * | |
| 4 | * Copyright 2021 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 | /** | |
| 440 | 29 | * @file hash_map.h |
| 30 | * @brief Hash map implementation. | |
| 31 | * @author Mike Becker | |
| 32 | * @author Olaf Wintermann | |
| 33 | * @copyright 2-Clause BSD License | |
| 174 | 34 | */ |
| 35 | ||
| 36 | #ifndef UCX_HASH_MAP_H | |
| 37 | #define UCX_HASH_MAP_H | |
| 38 | ||
| 39 | #include "map.h" | |
| 40 | ||
| 41 | /** Internal structure for an element of a hash map. */ | |
| 42 | struct cx_hash_map_element_s; | |
| 43 | ||
| 44 | /** | |
| 45 | * Internal structure for a hash map. | |
| 46 | */ | |
| 47 | struct cx_hash_map_s { | |
| 48 | /** | |
| 49 | * Base structure for maps. | |
| 50 | */ | |
| 51 | struct cx_map_s base; | |
| 52 | /** | |
| 53 | * The buckets of this map, each containing a linked list of elements. | |
| 54 | */ | |
| 55 | struct cx_hash_map_element_s **buckets; | |
| 56 | /** | |
| 57 | * The number of buckets. | |
| 58 | */ | |
| 59 | size_t bucket_count; | |
| 60 | }; | |
| 61 | ||
| 62 | ||
| 63 | /** | |
| 64 | * Creates a new hash map with the specified number of buckets. | |
| 65 | * | |
| 440 | 66 | * If @p buckets is zero, an implementation defined default will be used. |
| 174 | 67 | * |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
68 | * If @p elem_size is #CX_STORE_POINTERS, the created map stores pointers instead of |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
69 | * copies of the added elements. |
| 174 | 70 | * |
| 71 | * @note Iterators provided by this hash map implementation provide the remove operation. | |
| 845 | 72 | * The index value of an iterator is incremented when the iterator advanced without |
| 73 | * removing an entry. | |
| 440 | 74 | * In other words, when the iterator is finished, @c index==size . |
| 174 | 75 | * |
| 76 | * @param allocator the allocator to use | |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
77 | * (if @c NULL, the cxDefaultAllocator will be used) |
| 174 | 78 | * @param itemsize the size of one element |
| 79 | * @param buckets the initial number of buckets in this hash map | |
| 80 | * @return a pointer to the new hash map | |
| 81 | */ | |
|
1040
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1016
diff
changeset
|
82 | CX_EXTERN CX_NODISCARD CX_MALLOC CX_DEALLOC(cxMapFree, 1) |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1016
diff
changeset
|
83 | CxMap *cxHashMapCreate(const CxAllocator *allocator, |
| 870 | 84 | size_t itemsize, size_t buckets); |
| 174 | 85 | |
| 86 | /** | |
| 87 | * Increases the number of buckets, if necessary. | |
| 88 | * | |
| 440 | 89 | * The load threshold is @c 0.75*buckets. If the element count exceeds the load |
| 845 | 90 | * threshold, the map will be rehashed. Otherwise, no action is performed, and |
| 174 | 91 | * this function simply returns 0. |
| 92 | * | |
| 845 | 93 | * The rehashing process ensures that the number of buckets is at least |
| 174 | 94 | * 2.5 times the element count. So there is enough room for additional |
| 95 | * elements without the need of another soon rehashing. | |
| 96 | * | |
| 97 | * You can use this function after filling a map to increase access performance. | |
| 98 | * | |
| 99 | * @note If the specified map is not a hash map, the behavior is undefined. | |
| 100 | * | |
| 101 | * @param map the map to rehash | |
| 440 | 102 | * @retval zero success |
| 103 | * @retval non-zero if a memory allocation error occurred | |
| 174 | 104 | */ |
|
1040
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1016
diff
changeset
|
105 | CX_EXTERN CX_NONNULL |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1016
diff
changeset
|
106 | int cxMapRehash(CxMap *map); |
| 174 | 107 | |
| 108 | #endif // UCX_HASH_MAP_H |