| 1 | /* |
| 2 | * BSD-3-Clause |
| 3 | * |
| 4 | * Copyright 2021 Ozan Tezcan |
| 5 | * All rights reserved. |
| 6 | * |
| 7 | * Redistribution and use in source and binary forms, with or without |
| 8 | * modification, are permitted provided that the following conditions are met: |
| 9 | * |
| 10 | * 1. Redistributions of source code must retain the above copyright notice, |
| 11 | * this list of conditions and the following disclaimer. |
| 12 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 13 | * this list of conditions and the following disclaimer in the documentation |
| 14 | * and/or other materials provided with the distribution. |
| 15 | * 3. Neither the name of the copyright holder nor the names of its contributors |
| 16 | * may be used to endorse or promote products derived from this software |
| 17 | * without specific prior written permission. |
| 18 | * |
| 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 23 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, |
| 24 | * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT |
| 25 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 29 | * POSSIBILITY OF SUCH DAMAGE. |
| 30 | */ |
| 31 | |
| 32 | #ifndef SC_MAP_H |
| 33 | #define SC_MAP_H |
| 34 | |
| 35 | #include <memory.h> |
| 36 | #include <stdbool.h> |
| 37 | #include <stddef.h> |
| 38 | #include <stdint.h> |
| 39 | #include <stdlib.h> |
| 40 | |
| 41 | #ifdef __cplusplus |
| 42 | extern "C" { |
| 43 | #endif |
| 44 | |
| 45 | #define SC_MAP_VERSION "2.0.0" |
| 46 | |
| 47 | #ifdef SC_HAVE_CONFIG_H |
| 48 | #include "config.h" |
| 49 | #else |
| 50 | #define sc_map_calloc calloc |
| 51 | #define sc_map_free free |
| 52 | #endif |
| 53 | |
| 54 | #define sc_map_dec_strkey(name, K, V) \ |
| 55 | struct sc_map_item_##name { \ |
| 56 | K key; \ |
| 57 | V value; \ |
| 58 | uint32_t hash; \ |
| 59 | }; \ |
| 60 | \ |
| 61 | sc_map_of(name, K, V) |
| 62 | |
| 63 | #define sc_map_dec_scalar(name, K, V) \ |
| 64 | struct sc_map_item_##name { \ |
| 65 | K key; \ |
| 66 | V value; \ |
| 67 | }; \ |
| 68 | \ |
| 69 | sc_map_of(name, K, V) |
| 70 | |
| 71 | #define sc_map_of(name, K, V) \ |
| 72 | struct sc_map_##name { \ |
| 73 | struct sc_map_item_##name *mem; \ |
| 74 | uint32_t cap; \ |
| 75 | uint32_t size; \ |
| 76 | uint32_t load_fac; \ |
| 77 | uint32_t remap; \ |
| 78 | bool used; \ |
| 79 | bool oom; \ |
| 80 | bool found; \ |
| 81 | }; \ |
| 82 | \ |
| 83 | /** \ |
| 84 | * Create map \ |
| 85 | * \ |
| 86 | * @param map map \ |
| 87 | * @param cap initial capacity, zero is accepted \ |
| 88 | * @param load_factor must be >25 and <95. Pass 0 for default value. \ |
| 89 | * @return 'true' on success, \ |
| 90 | * 'false' on out of memory or if 'load_factor' value is \ |
| 91 | * invalid. \ |
| 92 | */ \ |
| 93 | bool sc_map_init_##name(struct sc_map_##name *map, uint32_t cap, \ |
| 94 | uint32_t load_factor); \ |
| 95 | \ |
| 96 | /** \ |
| 97 | * Destroy map. \ |
| 98 | * \ |
| 99 | * @param map map \ |
| 100 | */ \ |
| 101 | void sc_map_term_##name(struct sc_map_##name *map); \ |
| 102 | \ |
| 103 | /** \ |
| 104 | * Get map element count \ |
| 105 | * \ |
| 106 | * @param map map \ |
| 107 | * @return element count \ |
| 108 | */ \ |
| 109 | uint32_t sc_map_size_##name(struct sc_map_##name *map); \ |
| 110 | \ |
| 111 | /** \ |
| 112 | * Clear map \ |
| 113 | * \ |
| 114 | * @param map map \ |
| 115 | */ \ |
| 116 | void sc_map_clear_##name(struct sc_map_##name *map); \ |
| 117 | \ |
| 118 | /** \ |
| 119 | * Put element to the map \ |
| 120 | * \ |
| 121 | * struct sc_map_str map; \ |
| 122 | * sc_map_put_str(&map, "key", "value"); \ |
| 123 | * \ |
| 124 | * @param map map \ |
| 125 | * @param K key \ |
| 126 | * @param V value \ |
| 127 | * @return previous value if exists \ |
| 128 | * call sc_map_found() to see if the returned value is valid. \ |
| 129 | */ \ |
| 130 | V sc_map_put_##name(struct sc_map_##name *map, K key, V val); \ |
| 131 | \ |
| 132 | /** \ |
| 133 | * Get element \ |
| 134 | * \ |
| 135 | * @param map map \ |
| 136 | * @param K key \ |
| 137 | * @return current value if exists. \ |
| 138 | * call sc_map_found() to see if the returned value is valid. \ |
| 139 | */ \ |
| 140 | /** NOLINTNEXTLINE */ \ |
| 141 | V sc_map_get_##name(struct sc_map_##name *map, K key); \ |
| 142 | \ |
| 143 | /** \ |
| 144 | * Delete element \ |
| 145 | * \ |
| 146 | * @param map map \ |
| 147 | * @param K key \ |
| 148 | * @return current value if exists. \ |
| 149 | * call sc_map_found() to see if the returned value is valid. \ |
| 150 | */ \ |
| 151 | /** NOLINTNEXTLINE */ \ |
| 152 | V sc_map_del_##name(struct sc_map_##name *map, K key); |
| 153 | |
| 154 | /** |
| 155 | * @param map map |
| 156 | * @return - if put operation overrides a value, returns true |
| 157 | * - if get operation finds the key, returns true |
| 158 | * - if del operation deletes a key, returns true |
| 159 | */ |
| 160 | #define sc_map_found(map) ((map)->found) |
| 161 | |
| 162 | /** |
| 163 | * @param map map |
| 164 | * @return true if put operation failed with out of memory |
| 165 | */ |
| 166 | #define sc_map_oom(map) ((map)->oom) |
| 167 | |
| 168 | // clang-format off |
| 169 | |
| 170 | /** |
| 171 | * Foreach loop |
| 172 | * |
| 173 | * char *key, *value; |
| 174 | * struct sc_map_str map; |
| 175 | * |
| 176 | * sc_map_foreach(&map, key, value) { |
| 177 | * printf("key = %s, value = %s \n"); |
| 178 | * } |
| 179 | */ |
| 180 | #define sc_map_foreach(map, K, V) \ |
| 181 | for (int64_t _i = -1, _b = 0; !_b && _i < (map)->cap; _i++) \ |
| 182 | for ((V) = (map)->mem[_i].value, (K) = (map)->mem[_i].key, _b = 1; \ |
| 183 | _b && ((_i == -1 && (map)->used) || (K) != 0) ? 1 : (_b = 0); \ |
| 184 | _b = 0) |
| 185 | |
| 186 | /** |
| 187 | * Foreach loop for keys |
| 188 | * |
| 189 | * char *key; |
| 190 | * struct sc_map_str map; |
| 191 | * |
| 192 | * sc_map_foreach_key(&map, key) { |
| 193 | * printf("key = %s \n"); |
| 194 | * } |
| 195 | */ |
| 196 | #define sc_map_foreach_key(map, K) \ |
| 197 | for (int64_t _i = -1, _b = 0; !_b && _i < (map)->cap; _i++) \ |
| 198 | for ((K) = (map)->mem[_i].key, _b = 1; \ |
| 199 | _b && ((_i == -1 && (map)->used) || (K) != 0) ? 1 : (_b = 0); \ |
| 200 | _b = 0) |
| 201 | |
| 202 | /** |
| 203 | * Foreach loop for values |
| 204 | * |
| 205 | * char *value; |
| 206 | * struct sc_map_str map; |
| 207 | * |
| 208 | * sc_map_foreach_value(&map, value) { |
| 209 | * printf("value = %s \n"); |
| 210 | * } |
| 211 | */ |
| 212 | #define sc_map_foreach_value(map, V) \ |
| 213 | for (int64_t _i = -1, _b = 0; !_b && _i < (map)->cap; _i++) \ |
| 214 | for ((V) = (map)->mem[_i].value, _b = 1; \ |
| 215 | _b && ((_i == -1 && (map)->used) || (map)->mem[_i].key != 0) ? 1 : (_b = 0); \ |
| 216 | _b = 0) |
| 217 | |
| 218 | // integer keys: name key type value type |
| 219 | sc_map_dec_scalar(int, int, int) |
| 220 | sc_map_dec_scalar(intv,int, void*) |
| 221 | sc_map_dec_scalar(ints,int, const char*) |
| 222 | sc_map_dec_scalar(ll, long long, long long) |
| 223 | sc_map_dec_scalar(llv, long long, void *) |
| 224 | sc_map_dec_scalar(lls, long long, const char *) |
| 225 | sc_map_dec_scalar(32, uint32_t, uint32_t) |
| 226 | sc_map_dec_scalar(64, uint64_t, uint64_t) |
| 227 | sc_map_dec_scalar(64v, uint64_t, void *) |
| 228 | sc_map_dec_scalar(64s, uint64_t, const char *) |
| 229 | |
| 230 | // string keys: name key type value type |
| 231 | sc_map_dec_strkey(str, const char *, const char *) |
| 232 | sc_map_dec_strkey(sv, const char *, void*) |
| 233 | sc_map_dec_strkey(s64, const char *, uint64_t) |
| 234 | sc_map_dec_strkey(sll, const char *, long long) |
| 235 | |
| 236 | // clang-format on |
| 237 | |
| 238 | #ifdef __cplusplus |
| 239 | } |
| 240 | #endif |
| 241 | |
| 242 | #endif |
| 243 | |