Fri, 27 May 2022 11:39:50 +0200
fix missing static inline for map functions
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_HASH_MAP_H |
universe@549 | 38 | #define UCX_HASH_MAP_H |
universe@549 | 39 | |
universe@549 | 40 | #include "map.h" |
universe@549 | 41 | |
universe@549 | 42 | #ifdef __cplusplus |
universe@549 | 43 | extern "C" { |
universe@549 | 44 | #endif |
universe@549 | 45 | |
universe@549 | 46 | /** Internal structure for a key within a hash map. */ |
universe@549 | 47 | struct cx_hash_map_key_s { |
universe@549 | 48 | /** The key data. */ |
universe@550 | 49 | unsigned char *data; |
universe@550 | 50 | /** |
universe@550 | 51 | * The key data length. |
universe@550 | 52 | */ |
universe@550 | 53 | size_t len; |
universe@549 | 54 | /** The hash value of the key data. */ |
universe@549 | 55 | unsigned hash; |
universe@549 | 56 | }; |
universe@549 | 57 | |
universe@549 | 58 | /** Internal structure for an element of a hash map. */ |
universe@549 | 59 | struct cx_hash_map_element_s { |
universe@549 | 60 | /** The value data. */ |
universe@549 | 61 | void *data; |
universe@549 | 62 | |
universe@549 | 63 | /** A pointer to the next element in the current bucket. */ |
universe@549 | 64 | struct cx_hash_map_element_s *next; |
universe@549 | 65 | |
universe@549 | 66 | /** The corresponding key. */ |
universe@549 | 67 | struct cx_hash_map_key_s key; |
universe@549 | 68 | }; |
universe@549 | 69 | |
universe@550 | 70 | /** |
universe@550 | 71 | * Internal structure for a hash map. |
universe@550 | 72 | */ |
universe@550 | 73 | struct cx_hash_map_s { |
universe@550 | 74 | /** |
universe@550 | 75 | * Base structure for maps. |
universe@550 | 76 | */ |
universe@550 | 77 | struct cx_map_s base; |
universe@550 | 78 | /** |
universe@550 | 79 | * The buckets of this map, each containing a linked list of elements. |
universe@550 | 80 | */ |
universe@550 | 81 | struct cx_hash_map_element_s **buckets; |
universe@550 | 82 | /** |
universe@550 | 83 | * The number of buckets. |
universe@550 | 84 | */ |
universe@550 | 85 | size_t bucket_count; |
universe@550 | 86 | }; |
universe@550 | 87 | |
universe@549 | 88 | |
universe@549 | 89 | /** |
universe@550 | 90 | * Creates a new hash map with the specified number of buckets. |
universe@550 | 91 | * |
universe@550 | 92 | * If \p buckets is zero, an implementation defined default will be used. |
universe@550 | 93 | * |
universe@550 | 94 | * @note Iterators provided by this hash map implementation do provide the remove operation, because |
universe@550 | 95 | * a remove never causes an immediate rehashing. The iterators are also position-aware in the sense |
universe@550 | 96 | * that the index is initialized with zero and incremented when the iterator advances. |
universe@549 | 97 | * |
universe@549 | 98 | * @param allocator the allocator to use |
universe@549 | 99 | * @param buckets the initial number of buckets in this hash map |
universe@549 | 100 | * @return a pointer to the new hash map |
universe@549 | 101 | */ |
universe@550 | 102 | __attribute__((__nonnull__, __warn_unused_result__)) |
universe@549 | 103 | CxMap *cxHashMapCreate( |
universe@549 | 104 | CxAllocator *allocator, |
universe@549 | 105 | size_t buckets |
universe@549 | 106 | ); |
universe@549 | 107 | |
universe@549 | 108 | /** |
universe@549 | 109 | * Increases the number of buckets, if necessary. |
universe@549 | 110 | * |
universe@549 | 111 | * The load value is \c loadFactor*buckets. If the element count exceeds the load |
universe@549 | 112 | * value, the map needs to be rehashed. Otherwise, no action is performed and |
universe@549 | 113 | * this function simply returns 0. |
universe@549 | 114 | * |
universe@549 | 115 | * The rehashing process ensures, that the number of buckets is at least |
universe@549 | 116 | * \p bucketFactor times the number of stored items. So there is enough room for additional |
universe@549 | 117 | * elements without the need of another soon rehashing. |
universe@549 | 118 | * |
universe@549 | 119 | * You can use this function after filling a map to dramatically increase access performance. |
universe@549 | 120 | * |
universe@549 | 121 | * @note If the specified map is not a hash map, the behavior is undefined. |
universe@549 | 122 | * |
universe@549 | 123 | * @param map the map to rehash |
universe@549 | 124 | * @param loadFactor a percentage threshold for the load of the map |
universe@549 | 125 | * @param bucketFactor a factor for the number of buckets that shall be available after rehashing |
universe@549 | 126 | * @return zero on success, non-zero if a memory allocation error occurred |
universe@549 | 127 | */ |
universe@549 | 128 | __attribute__((__nonnull__)) |
universe@549 | 129 | int cxMapRehash2( |
universe@549 | 130 | CxMap *map, |
universe@549 | 131 | float loadFactor, |
universe@549 | 132 | float bucketFactor |
universe@549 | 133 | ); |
universe@549 | 134 | |
universe@549 | 135 | /** |
universe@549 | 136 | * Rehashes the map with load factor 0.75 and bucket factor 2.5. |
universe@549 | 137 | * |
universe@549 | 138 | * @param map the map to rehash |
universe@549 | 139 | * @return zero on success, non-zero if a memory allocation error occurred |
universe@549 | 140 | * @see cxMapRehash2() |
universe@549 | 141 | */ |
universe@549 | 142 | __attribute__((__nonnull__)) |
universe@549 | 143 | static inline int cxMapRehash(CxMap *map) { |
universe@549 | 144 | return cxMapRehash2(map, 0.75f, 2.5f); |
universe@549 | 145 | } |
universe@549 | 146 | |
universe@549 | 147 | |
universe@549 | 148 | #ifdef __cplusplus |
universe@549 | 149 | } // extern "C" |
universe@549 | 150 | #endif |
universe@549 | 151 | |
universe@549 | 152 | #endif // UCX_HASH_MAP_H |