Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @file hash_table.c |
| 3 | * @author Radek Krejci <rkrejci@cesnet.cz> |
Michal Vasko | ae130f5 | 2023-04-20 14:25:16 +0200 | [diff] [blame] | 4 | * @author Michal Vasko <mvasko@cesnet.cz> |
| 5 | * @brief libyang generic hash table implementation |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 6 | * |
Michal Vasko | ae130f5 | 2023-04-20 14:25:16 +0200 | [diff] [blame] | 7 | * Copyright (c) 2015 - 2023 CESNET, z.s.p.o. |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 8 | * |
| 9 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 10 | * You may not use this file except in compliance with the License. |
| 11 | * You may obtain a copy of the License at |
| 12 | * |
| 13 | * https://opensource.org/licenses/BSD-3-Clause |
| 14 | */ |
| 15 | |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 16 | #include "hash_table.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 17 | |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 18 | #include <assert.h> |
| 19 | #include <pthread.h> |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 20 | #include <stdint.h> |
| 21 | #include <stdlib.h> |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 22 | #include <string.h> |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 23 | |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 24 | #include "common.h" |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 25 | #include "compat.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 26 | #include "dict.h" |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 27 | #include "log.h" |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 28 | |
Michal Vasko | ae130f5 | 2023-04-20 14:25:16 +0200 | [diff] [blame] | 29 | LIBYANG_API_DEF uint32_t |
| 30 | lyht_hash_multi(uint32_t hash, const char *key_part, size_t len) |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 31 | { |
| 32 | uint32_t i; |
| 33 | |
aPiecek | 4f07c3e | 2021-06-11 10:53:07 +0200 | [diff] [blame] | 34 | if (key_part && len) { |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 35 | for (i = 0; i < len; ++i) { |
| 36 | hash += key_part[i]; |
| 37 | hash += (hash << 10); |
| 38 | hash ^= (hash >> 6); |
| 39 | } |
| 40 | } else { |
| 41 | hash += (hash << 3); |
| 42 | hash ^= (hash >> 11); |
| 43 | hash += (hash << 15); |
| 44 | } |
| 45 | |
| 46 | return hash; |
| 47 | } |
| 48 | |
Michal Vasko | ae130f5 | 2023-04-20 14:25:16 +0200 | [diff] [blame] | 49 | LIBYANG_API_DEF uint32_t |
| 50 | lyht_hash(const char *key, size_t len) |
Radek Krejci | f2dc4c5 | 2018-11-08 09:04:13 +0100 | [diff] [blame] | 51 | { |
| 52 | uint32_t hash; |
| 53 | |
Michal Vasko | ae130f5 | 2023-04-20 14:25:16 +0200 | [diff] [blame] | 54 | hash = lyht_hash_multi(0, key, len); |
| 55 | return lyht_hash_multi(hash, NULL, len); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 56 | } |
| 57 | |
Michal Vasko | 8efac24 | 2023-03-30 08:24:56 +0200 | [diff] [blame] | 58 | struct ly_ht_rec * |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 59 | lyht_get_rec(unsigned char *recs, uint16_t rec_size, uint32_t idx) |
| 60 | { |
Michal Vasko | 8efac24 | 2023-03-30 08:24:56 +0200 | [diff] [blame] | 61 | return (struct ly_ht_rec *)&recs[idx * rec_size]; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 62 | } |
| 63 | |
Michal Vasko | ae130f5 | 2023-04-20 14:25:16 +0200 | [diff] [blame] | 64 | LIBYANG_API_DEF struct ly_ht * |
Michal Vasko | 62524a9 | 2021-02-26 10:08:50 +0100 | [diff] [blame] | 65 | lyht_new(uint32_t size, uint16_t val_size, lyht_value_equal_cb val_equal, void *cb_data, uint16_t resize) |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 66 | { |
Michal Vasko | 8efac24 | 2023-03-30 08:24:56 +0200 | [diff] [blame] | 67 | struct ly_ht *ht; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 68 | |
| 69 | /* check that 2^x == size (power of 2) */ |
| 70 | assert(size && !(size & (size - 1))); |
| 71 | assert(val_equal && val_size); |
| 72 | assert(resize == 0 || resize == 1); |
| 73 | |
| 74 | if (size < LYHT_MIN_SIZE) { |
| 75 | size = LYHT_MIN_SIZE; |
| 76 | } |
| 77 | |
| 78 | ht = malloc(sizeof *ht); |
Michal Vasko | b3d0d6b | 2018-09-07 10:17:33 +0200 | [diff] [blame] | 79 | LY_CHECK_ERR_RET(!ht, LOGMEM(NULL), NULL); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 80 | |
| 81 | ht->used = 0; |
| 82 | ht->size = size; |
Michal Vasko | dc95d9c | 2021-04-12 15:11:48 +0200 | [diff] [blame] | 83 | ht->invalid = 0; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 84 | ht->val_equal = val_equal; |
| 85 | ht->cb_data = cb_data; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 86 | ht->resize = resize; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 87 | |
Michal Vasko | 8efac24 | 2023-03-30 08:24:56 +0200 | [diff] [blame] | 88 | ht->rec_size = (sizeof(struct ly_ht_rec) - 1) + val_size; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 89 | /* allocate the records correctly */ |
| 90 | ht->recs = calloc(size, ht->rec_size); |
Michal Vasko | b3d0d6b | 2018-09-07 10:17:33 +0200 | [diff] [blame] | 91 | LY_CHECK_ERR_RET(!ht->recs, free(ht); LOGMEM(NULL), NULL); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 92 | |
| 93 | return ht; |
| 94 | } |
| 95 | |
Michal Vasko | ae130f5 | 2023-04-20 14:25:16 +0200 | [diff] [blame] | 96 | LIBYANG_API_DEF lyht_value_equal_cb |
Michal Vasko | 8efac24 | 2023-03-30 08:24:56 +0200 | [diff] [blame] | 97 | lyht_set_cb(struct ly_ht *ht, lyht_value_equal_cb new_val_equal) |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 98 | { |
Michal Vasko | 62524a9 | 2021-02-26 10:08:50 +0100 | [diff] [blame] | 99 | lyht_value_equal_cb prev; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 100 | |
| 101 | prev = ht->val_equal; |
| 102 | ht->val_equal = new_val_equal; |
| 103 | return prev; |
| 104 | } |
| 105 | |
Michal Vasko | ae130f5 | 2023-04-20 14:25:16 +0200 | [diff] [blame] | 106 | LIBYANG_API_DEF void * |
Michal Vasko | 8efac24 | 2023-03-30 08:24:56 +0200 | [diff] [blame] | 107 | lyht_set_cb_data(struct ly_ht *ht, void *new_cb_data) |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 108 | { |
| 109 | void *prev; |
| 110 | |
| 111 | prev = ht->cb_data; |
| 112 | ht->cb_data = new_cb_data; |
| 113 | return prev; |
| 114 | } |
| 115 | |
Michal Vasko | ae130f5 | 2023-04-20 14:25:16 +0200 | [diff] [blame] | 116 | LIBYANG_API_DEF struct ly_ht * |
Michal Vasko | 8efac24 | 2023-03-30 08:24:56 +0200 | [diff] [blame] | 117 | lyht_dup(const struct ly_ht *orig) |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 118 | { |
Michal Vasko | 8efac24 | 2023-03-30 08:24:56 +0200 | [diff] [blame] | 119 | struct ly_ht *ht; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 120 | |
Michal Vasko | b3d0d6b | 2018-09-07 10:17:33 +0200 | [diff] [blame] | 121 | LY_CHECK_ARG_RET(NULL, orig, NULL); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 122 | |
Michal Vasko | 8efac24 | 2023-03-30 08:24:56 +0200 | [diff] [blame] | 123 | ht = lyht_new(orig->size, orig->rec_size - (sizeof(struct ly_ht_rec) - 1), orig->val_equal, orig->cb_data, orig->resize ? 1 : 0); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 124 | if (!ht) { |
| 125 | return NULL; |
| 126 | } |
| 127 | |
Radek Krejci | d78fd46 | 2021-06-02 21:29:20 +0200 | [diff] [blame] | 128 | memcpy(ht->recs, orig->recs, (size_t)orig->used * (size_t)orig->rec_size); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 129 | ht->used = orig->used; |
Michal Vasko | dc95d9c | 2021-04-12 15:11:48 +0200 | [diff] [blame] | 130 | ht->invalid = orig->invalid; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 131 | return ht; |
| 132 | } |
| 133 | |
Michal Vasko | ae130f5 | 2023-04-20 14:25:16 +0200 | [diff] [blame] | 134 | LIBYANG_API_DEF void |
Michal Vasko | 8efac24 | 2023-03-30 08:24:56 +0200 | [diff] [blame] | 135 | lyht_free(struct ly_ht *ht, void (*val_free)(void *val_p)) |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 136 | { |
Michal Vasko | 8efac24 | 2023-03-30 08:24:56 +0200 | [diff] [blame] | 137 | struct ly_ht_rec *rec; |
Michal Vasko | 77b7f90a | 2023-01-31 15:42:41 +0100 | [diff] [blame] | 138 | uint32_t i; |
| 139 | |
| 140 | if (!ht) { |
| 141 | return; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 142 | } |
Michal Vasko | 77b7f90a | 2023-01-31 15:42:41 +0100 | [diff] [blame] | 143 | |
| 144 | if (val_free) { |
| 145 | for (i = 0; i < ht->size; ++i) { |
| 146 | rec = lyht_get_rec(ht->recs, ht->rec_size, i); |
| 147 | if (rec->hits > 0) { |
| 148 | val_free(&rec->val); |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | free(ht->recs); |
| 153 | free(ht); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 154 | } |
| 155 | |
Michal Vasko | dc95d9c | 2021-04-12 15:11:48 +0200 | [diff] [blame] | 156 | /** |
| 157 | * @brief Resize a hash table. |
| 158 | * |
| 159 | * @param[in] ht Hash table to resize. |
| 160 | * @param[in] operation Operation to perform. 1 to enlarge, -1 to shrink, 0 to only rehash all records. |
| 161 | * @return LY_ERR value. |
| 162 | */ |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 163 | static LY_ERR |
Michal Vasko | 8efac24 | 2023-03-30 08:24:56 +0200 | [diff] [blame] | 164 | lyht_resize(struct ly_ht *ht, int operation) |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 165 | { |
Michal Vasko | 8efac24 | 2023-03-30 08:24:56 +0200 | [diff] [blame] | 166 | struct ly_ht_rec *rec; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 167 | unsigned char *old_recs; |
| 168 | uint32_t i, old_size; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 169 | |
| 170 | old_recs = ht->recs; |
| 171 | old_size = ht->size; |
| 172 | |
Michal Vasko | dc95d9c | 2021-04-12 15:11:48 +0200 | [diff] [blame] | 173 | if (operation > 0) { |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 174 | /* double the size */ |
| 175 | ht->size <<= 1; |
Michal Vasko | dc95d9c | 2021-04-12 15:11:48 +0200 | [diff] [blame] | 176 | } else if (operation < 0) { |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 177 | /* half the size */ |
| 178 | ht->size >>= 1; |
| 179 | } |
| 180 | |
| 181 | ht->recs = calloc(ht->size, ht->rec_size); |
Michal Vasko | b3d0d6b | 2018-09-07 10:17:33 +0200 | [diff] [blame] | 182 | LY_CHECK_ERR_RET(!ht->recs, LOGMEM(NULL); ht->recs = old_recs; ht->size = old_size, LY_EMEM); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 183 | |
Michal Vasko | dc95d9c | 2021-04-12 15:11:48 +0200 | [diff] [blame] | 184 | /* reset used and invalid, it will increase again */ |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 185 | ht->used = 0; |
Michal Vasko | dc95d9c | 2021-04-12 15:11:48 +0200 | [diff] [blame] | 186 | ht->invalid = 0; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 187 | |
| 188 | /* add all the old records into the new records array */ |
| 189 | for (i = 0; i < old_size; ++i) { |
| 190 | rec = lyht_get_rec(old_recs, ht->rec_size, i); |
| 191 | if (rec->hits > 0) { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 192 | LY_ERR ret = lyht_insert(ht, rec->val, rec->hash, NULL); |
Michal Vasko | 26bbb27 | 2022-08-02 14:54:33 +0200 | [diff] [blame] | 193 | |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 194 | assert(!ret); |
| 195 | (void)ret; |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | /* final touches */ |
| 200 | free(old_recs); |
| 201 | return LY_SUCCESS; |
| 202 | } |
| 203 | |
Michal Vasko | da85903 | 2020-07-14 12:20:14 +0200 | [diff] [blame] | 204 | /** |
| 205 | * @brief Search for the first match. |
| 206 | * |
| 207 | * @param[in] ht Hash table to search in. |
| 208 | * @param[in] hash Hash to find. |
| 209 | * @param[out] rec_p Optional found record. |
| 210 | * @return LY_SUCCESS hash found, returned its record, |
| 211 | * @return LY_ENOTFOUND hash not found, returned the record where it would be inserted. |
| 212 | */ |
| 213 | static LY_ERR |
Michal Vasko | 8efac24 | 2023-03-30 08:24:56 +0200 | [diff] [blame] | 214 | lyht_find_first(struct ly_ht *ht, uint32_t hash, struct ly_ht_rec **rec_p) |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 215 | { |
Michal Vasko | 8efac24 | 2023-03-30 08:24:56 +0200 | [diff] [blame] | 216 | struct ly_ht_rec *rec; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 217 | uint32_t i, idx; |
| 218 | |
| 219 | if (rec_p) { |
| 220 | *rec_p = NULL; |
| 221 | } |
| 222 | |
| 223 | idx = i = hash & (ht->size - 1); |
| 224 | rec = lyht_get_rec(ht->recs, ht->rec_size, idx); |
| 225 | |
| 226 | /* skip through overflow and deleted records */ |
| 227 | while ((rec->hits != 0) && ((rec->hits == -1) || ((rec->hash & (ht->size - 1)) != idx))) { |
| 228 | if ((rec->hits == -1) && rec_p && !(*rec_p)) { |
| 229 | /* remember this record for return */ |
| 230 | *rec_p = rec; |
| 231 | } |
| 232 | i = (i + 1) % ht->size; |
| 233 | if (i == idx) { |
| 234 | /* we went through all the records (very unlikely, but possible when many records are invalid), |
| 235 | * just return not found */ |
| 236 | assert(!rec_p || *rec_p); |
Michal Vasko | da85903 | 2020-07-14 12:20:14 +0200 | [diff] [blame] | 237 | return LY_ENOTFOUND; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 238 | } |
| 239 | rec = lyht_get_rec(ht->recs, ht->rec_size, i); |
| 240 | } |
| 241 | if (rec->hits == 0) { |
| 242 | /* we could not find the value */ |
| 243 | if (rec_p && !*rec_p) { |
| 244 | *rec_p = rec; |
| 245 | } |
Michal Vasko | da85903 | 2020-07-14 12:20:14 +0200 | [diff] [blame] | 246 | return LY_ENOTFOUND; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | /* we have found a record with equal (shortened) hash */ |
| 250 | if (rec_p) { |
| 251 | *rec_p = rec; |
| 252 | } |
Michal Vasko | da85903 | 2020-07-14 12:20:14 +0200 | [diff] [blame] | 253 | return LY_SUCCESS; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | /** |
| 257 | * @brief Search for the next collision. |
| 258 | * |
| 259 | * @param[in] ht Hash table to search in. |
| 260 | * @param[in,out] last Last returned collision record. |
| 261 | * @param[in] first First collision record (hits > 1). |
Michal Vasko | da85903 | 2020-07-14 12:20:14 +0200 | [diff] [blame] | 262 | * @return LY_SUCCESS when hash collision found, \p last points to this next collision, |
| 263 | * @return LY_ENOTFOUND when hash collision not found, \p last points to the record where it would be inserted. |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 264 | */ |
Michal Vasko | da85903 | 2020-07-14 12:20:14 +0200 | [diff] [blame] | 265 | static LY_ERR |
Michal Vasko | 8efac24 | 2023-03-30 08:24:56 +0200 | [diff] [blame] | 266 | lyht_find_collision(struct ly_ht *ht, struct ly_ht_rec **last, struct ly_ht_rec *first) |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 267 | { |
Michal Vasko | 8efac24 | 2023-03-30 08:24:56 +0200 | [diff] [blame] | 268 | struct ly_ht_rec *empty = NULL; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 269 | uint32_t i, idx; |
| 270 | |
| 271 | assert(last && *last); |
| 272 | |
| 273 | idx = (*last)->hash & (ht->size - 1); |
| 274 | i = (((unsigned char *)*last) - ht->recs) / ht->rec_size; |
| 275 | |
| 276 | do { |
| 277 | i = (i + 1) % ht->size; |
| 278 | *last = lyht_get_rec(ht->recs, ht->rec_size, i); |
| 279 | if (*last == first) { |
| 280 | /* we went through all the records (very unlikely, but possible when many records are invalid), |
| 281 | * just return an invalid record */ |
| 282 | assert(empty); |
| 283 | *last = empty; |
Michal Vasko | da85903 | 2020-07-14 12:20:14 +0200 | [diff] [blame] | 284 | return LY_ENOTFOUND; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | if (((*last)->hits == -1) && !empty) { |
| 288 | empty = *last; |
| 289 | } |
| 290 | } while (((*last)->hits != 0) && (((*last)->hits == -1) || (((*last)->hash & (ht->size - 1)) != idx))); |
| 291 | |
| 292 | if ((*last)->hits > 0) { |
| 293 | /* we found a collision */ |
| 294 | assert((*last)->hits == 1); |
Michal Vasko | da85903 | 2020-07-14 12:20:14 +0200 | [diff] [blame] | 295 | return LY_SUCCESS; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | /* no next collision found, return the record where it would be inserted */ |
| 299 | if (empty) { |
| 300 | *last = empty; |
| 301 | } /* else (*last)->hits == 0, it is already correct */ |
Michal Vasko | da85903 | 2020-07-14 12:20:14 +0200 | [diff] [blame] | 302 | return LY_ENOTFOUND; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 303 | } |
| 304 | |
Michal Vasko | da85903 | 2020-07-14 12:20:14 +0200 | [diff] [blame] | 305 | /** |
| 306 | * @brief Search for a record with specific value and hash. |
| 307 | * |
| 308 | * @param[in] ht Hash table to search in. |
| 309 | * @param[in] val_p Pointer to the value to find. |
| 310 | * @param[in] hash Hash to find. |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 311 | * @param[in] mod Whether the operation modifies the hash table (insert or remove) or not (find). |
Michal Vasko | da85903 | 2020-07-14 12:20:14 +0200 | [diff] [blame] | 312 | * @param[out] crec_p Optional found first record. |
| 313 | * @param[out] col Optional collision number of @p rec_p, 0 for no collision. |
| 314 | * @param[out] rec_p Found exact matching record, may be a collision of @p crec_p. |
| 315 | * @return LY_ENOTFOUND if no record found, |
| 316 | * @return LY_SUCCESS if record was found. |
| 317 | */ |
| 318 | static LY_ERR |
Michal Vasko | 8efac24 | 2023-03-30 08:24:56 +0200 | [diff] [blame] | 319 | lyht_find_rec(struct ly_ht *ht, void *val_p, uint32_t hash, ly_bool mod, struct ly_ht_rec **crec_p, uint32_t *col, |
| 320 | struct ly_ht_rec **rec_p) |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 321 | { |
Michal Vasko | 8efac24 | 2023-03-30 08:24:56 +0200 | [diff] [blame] | 322 | struct ly_ht_rec *rec, *crec; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 323 | uint32_t i, c; |
Michal Vasko | da85903 | 2020-07-14 12:20:14 +0200 | [diff] [blame] | 324 | LY_ERR r; |
| 325 | |
| 326 | if (crec_p) { |
| 327 | *crec_p = NULL; |
| 328 | } |
| 329 | if (col) { |
| 330 | *col = 0; |
| 331 | } |
| 332 | *rec_p = NULL; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 333 | |
| 334 | if (lyht_find_first(ht, hash, &rec)) { |
| 335 | /* not found */ |
Michal Vasko | da85903 | 2020-07-14 12:20:14 +0200 | [diff] [blame] | 336 | return LY_ENOTFOUND; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 337 | } |
Michal Vasko | da85903 | 2020-07-14 12:20:14 +0200 | [diff] [blame] | 338 | if ((rec->hash == hash) && ht->val_equal(val_p, &rec->val, mod, ht->cb_data)) { |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 339 | /* even the value matches */ |
Michal Vasko | da85903 | 2020-07-14 12:20:14 +0200 | [diff] [blame] | 340 | if (crec_p) { |
| 341 | *crec_p = rec; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 342 | } |
Michal Vasko | da85903 | 2020-07-14 12:20:14 +0200 | [diff] [blame] | 343 | if (col) { |
| 344 | *col = 0; |
| 345 | } |
| 346 | *rec_p = rec; |
| 347 | return LY_SUCCESS; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | /* some collisions, we need to go through them, too */ |
| 351 | crec = rec; |
Michal Vasko | da85903 | 2020-07-14 12:20:14 +0200 | [diff] [blame] | 352 | c = crec->hits; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 353 | for (i = 1; i < c; ++i) { |
| 354 | r = lyht_find_collision(ht, &rec, crec); |
| 355 | assert(!r); |
| 356 | (void)r; |
| 357 | |
| 358 | /* compare values */ |
Michal Vasko | da85903 | 2020-07-14 12:20:14 +0200 | [diff] [blame] | 359 | if ((rec->hash == hash) && ht->val_equal(val_p, &rec->val, mod, ht->cb_data)) { |
| 360 | if (crec_p) { |
| 361 | *crec_p = crec; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 362 | } |
Michal Vasko | da85903 | 2020-07-14 12:20:14 +0200 | [diff] [blame] | 363 | if (col) { |
| 364 | *col = i; |
| 365 | } |
| 366 | *rec_p = rec; |
| 367 | return LY_SUCCESS; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 368 | } |
| 369 | } |
| 370 | |
| 371 | /* not found even in collisions */ |
Michal Vasko | da85903 | 2020-07-14 12:20:14 +0200 | [diff] [blame] | 372 | return LY_ENOTFOUND; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 373 | } |
| 374 | |
Michal Vasko | ae130f5 | 2023-04-20 14:25:16 +0200 | [diff] [blame] | 375 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | 8efac24 | 2023-03-30 08:24:56 +0200 | [diff] [blame] | 376 | lyht_find(struct ly_ht *ht, void *val_p, uint32_t hash, void **match_p) |
Michal Vasko | da85903 | 2020-07-14 12:20:14 +0200 | [diff] [blame] | 377 | { |
Michal Vasko | 8efac24 | 2023-03-30 08:24:56 +0200 | [diff] [blame] | 378 | struct ly_ht_rec *rec; |
Michal Vasko | da85903 | 2020-07-14 12:20:14 +0200 | [diff] [blame] | 379 | |
| 380 | lyht_find_rec(ht, val_p, hash, 0, NULL, NULL, &rec); |
| 381 | |
| 382 | if (rec && match_p) { |
| 383 | *match_p = rec->val; |
| 384 | } |
| 385 | return rec ? LY_SUCCESS : LY_ENOTFOUND; |
| 386 | } |
| 387 | |
Michal Vasko | ae130f5 | 2023-04-20 14:25:16 +0200 | [diff] [blame] | 388 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | 8efac24 | 2023-03-30 08:24:56 +0200 | [diff] [blame] | 389 | lyht_find_next_with_collision_cb(struct ly_ht *ht, void *val_p, uint32_t hash, |
Michal Vasko | 6374de2 | 2022-09-05 15:48:48 +0200 | [diff] [blame] | 390 | lyht_value_equal_cb collision_val_equal, void **match_p) |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 391 | { |
Michal Vasko | 8efac24 | 2023-03-30 08:24:56 +0200 | [diff] [blame] | 392 | struct ly_ht_rec *rec, *crec; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 393 | uint32_t i, c; |
Michal Vasko | da85903 | 2020-07-14 12:20:14 +0200 | [diff] [blame] | 394 | LY_ERR r; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 395 | |
Michal Vasko | 6374de2 | 2022-09-05 15:48:48 +0200 | [diff] [blame] | 396 | /* find the record of the previously found value */ |
Michal Vasko | da85903 | 2020-07-14 12:20:14 +0200 | [diff] [blame] | 397 | if (lyht_find_rec(ht, val_p, hash, 1, &crec, &i, &rec)) { |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 398 | /* not found, cannot happen */ |
Michal Vasko | da85903 | 2020-07-14 12:20:14 +0200 | [diff] [blame] | 399 | LOGINT_RET(NULL); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 400 | } |
| 401 | |
Michal Vasko | 6374de2 | 2022-09-05 15:48:48 +0200 | [diff] [blame] | 402 | /* go through collisions and find the next one after the previous one */ |
Michal Vasko | da85903 | 2020-07-14 12:20:14 +0200 | [diff] [blame] | 403 | c = crec->hits; |
| 404 | for (++i; i < c; ++i) { |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 405 | r = lyht_find_collision(ht, &rec, crec); |
| 406 | assert(!r); |
| 407 | (void)r; |
| 408 | |
Michal Vasko | 6374de2 | 2022-09-05 15:48:48 +0200 | [diff] [blame] | 409 | if (rec->hash != hash) { |
| 410 | continue; |
| 411 | } |
| 412 | |
| 413 | if (collision_val_equal) { |
| 414 | if (collision_val_equal(val_p, &rec->val, 0, ht->cb_data)) { |
| 415 | /* even the value matches */ |
| 416 | if (match_p) { |
| 417 | *match_p = rec->val; |
| 418 | } |
| 419 | return LY_SUCCESS; |
| 420 | } |
| 421 | } else if (ht->val_equal(val_p, &rec->val, 0, ht->cb_data)) { |
Michal Vasko | da85903 | 2020-07-14 12:20:14 +0200 | [diff] [blame] | 422 | /* even the value matches */ |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 423 | if (match_p) { |
| 424 | *match_p = rec->val; |
| 425 | } |
Michal Vasko | da85903 | 2020-07-14 12:20:14 +0200 | [diff] [blame] | 426 | return LY_SUCCESS; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 427 | } |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | /* the last equal value was already returned */ |
Michal Vasko | da85903 | 2020-07-14 12:20:14 +0200 | [diff] [blame] | 431 | return LY_ENOTFOUND; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 432 | } |
| 433 | |
Michal Vasko | ae130f5 | 2023-04-20 14:25:16 +0200 | [diff] [blame] | 434 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | 8efac24 | 2023-03-30 08:24:56 +0200 | [diff] [blame] | 435 | lyht_find_next(struct ly_ht *ht, void *val_p, uint32_t hash, void **match_p) |
Michal Vasko | 6374de2 | 2022-09-05 15:48:48 +0200 | [diff] [blame] | 436 | { |
| 437 | return lyht_find_next_with_collision_cb(ht, val_p, hash, NULL, match_p); |
| 438 | } |
| 439 | |
Michal Vasko | ae130f5 | 2023-04-20 14:25:16 +0200 | [diff] [blame] | 440 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | 8efac24 | 2023-03-30 08:24:56 +0200 | [diff] [blame] | 441 | lyht_insert_with_resize_cb(struct ly_ht *ht, void *val_p, uint32_t hash, lyht_value_equal_cb resize_val_equal, |
Michal Vasko | 62524a9 | 2021-02-26 10:08:50 +0100 | [diff] [blame] | 442 | void **match_p) |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 443 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 444 | LY_ERR r, ret = LY_SUCCESS; |
Michal Vasko | 8efac24 | 2023-03-30 08:24:56 +0200 | [diff] [blame] | 445 | struct ly_ht_rec *rec, *crec = NULL; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 446 | int32_t i; |
Michal Vasko | 62524a9 | 2021-02-26 10:08:50 +0100 | [diff] [blame] | 447 | lyht_value_equal_cb old_val_equal = NULL; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 448 | |
| 449 | if (!lyht_find_first(ht, hash, &rec)) { |
| 450 | /* we found matching shortened hash */ |
| 451 | if ((rec->hash == hash) && ht->val_equal(val_p, &rec->val, 1, ht->cb_data)) { |
| 452 | /* even the value matches */ |
| 453 | if (match_p) { |
| 454 | *match_p = (void *)&rec->val; |
| 455 | } |
| 456 | return LY_EEXIST; |
| 457 | } |
| 458 | |
| 459 | /* some collisions, we need to go through them, too */ |
| 460 | crec = rec; |
| 461 | for (i = 1; i < crec->hits; ++i) { |
| 462 | r = lyht_find_collision(ht, &rec, crec); |
| 463 | assert(!r); |
| 464 | |
| 465 | /* compare values */ |
| 466 | if ((rec->hash == hash) && ht->val_equal(val_p, &rec->val, 1, ht->cb_data)) { |
| 467 | if (match_p) { |
| 468 | *match_p = (void *)&rec->val; |
| 469 | } |
| 470 | return LY_EEXIST; |
| 471 | } |
| 472 | } |
| 473 | |
| 474 | /* value not found, get the record where it will be inserted */ |
| 475 | r = lyht_find_collision(ht, &rec, crec); |
| 476 | assert(r); |
| 477 | } |
| 478 | |
| 479 | /* insert it into the returned record */ |
| 480 | assert(rec->hits < 1); |
Michal Vasko | dc95d9c | 2021-04-12 15:11:48 +0200 | [diff] [blame] | 481 | if (rec->hits < 0) { |
| 482 | --ht->invalid; |
| 483 | } |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 484 | rec->hash = hash; |
| 485 | rec->hits = 1; |
Michal Vasko | 8efac24 | 2023-03-30 08:24:56 +0200 | [diff] [blame] | 486 | memcpy(&rec->val, val_p, ht->rec_size - (sizeof(struct ly_ht_rec) - 1)); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 487 | if (match_p) { |
| 488 | *match_p = (void *)&rec->val; |
| 489 | } |
| 490 | |
| 491 | if (crec) { |
| 492 | /* there was a collision, increase hits */ |
| 493 | if (crec->hits == INT32_MAX) { |
| 494 | LOGINT(NULL); |
| 495 | } |
| 496 | ++crec->hits; |
| 497 | } |
| 498 | |
| 499 | /* check size & enlarge if needed */ |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 500 | ++ht->used; |
| 501 | if (ht->resize) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 502 | r = (ht->used * LYHT_HUNDRED_PERCENTAGE) / ht->size; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 503 | if ((ht->resize == 1) && (r >= LYHT_FIRST_SHRINK_PERCENTAGE)) { |
| 504 | /* enable shrinking */ |
| 505 | ht->resize = 2; |
| 506 | } |
| 507 | if ((ht->resize == 2) && (r >= LYHT_ENLARGE_PERCENTAGE)) { |
| 508 | if (resize_val_equal) { |
| 509 | old_val_equal = lyht_set_cb(ht, resize_val_equal); |
| 510 | } |
| 511 | |
| 512 | /* enlarge */ |
| 513 | ret = lyht_resize(ht, 1); |
| 514 | /* if hash_table was resized, we need to find new matching value */ |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 515 | if ((ret == LY_SUCCESS) && match_p) { |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 516 | lyht_find(ht, val_p, hash, match_p); |
| 517 | } |
| 518 | |
| 519 | if (resize_val_equal) { |
| 520 | lyht_set_cb(ht, old_val_equal); |
| 521 | } |
| 522 | } |
| 523 | } |
| 524 | return ret; |
| 525 | } |
| 526 | |
Michal Vasko | ae130f5 | 2023-04-20 14:25:16 +0200 | [diff] [blame] | 527 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | 8efac24 | 2023-03-30 08:24:56 +0200 | [diff] [blame] | 528 | lyht_insert(struct ly_ht *ht, void *val_p, uint32_t hash, void **match_p) |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 529 | { |
| 530 | return lyht_insert_with_resize_cb(ht, val_p, hash, NULL, match_p); |
| 531 | } |
| 532 | |
Michal Vasko | ae130f5 | 2023-04-20 14:25:16 +0200 | [diff] [blame] | 533 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | 8efac24 | 2023-03-30 08:24:56 +0200 | [diff] [blame] | 534 | lyht_remove_with_resize_cb(struct ly_ht *ht, void *val_p, uint32_t hash, lyht_value_equal_cb resize_val_equal) |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 535 | { |
Michal Vasko | 8efac24 | 2023-03-30 08:24:56 +0200 | [diff] [blame] | 536 | struct ly_ht_rec *rec, *crec; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 537 | int32_t i; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 538 | ly_bool first_matched = 0; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 539 | LY_ERR r, ret = LY_SUCCESS; |
steweg | d8e2fc9 | 2023-05-31 09:52:56 +0200 | [diff] [blame] | 540 | lyht_value_equal_cb old_val_equal = NULL; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 541 | |
Michal Vasko | 4a4c7ed | 2020-07-17 09:30:12 +0200 | [diff] [blame] | 542 | LY_CHECK_ERR_RET(lyht_find_first(ht, hash, &rec), LOGARG(NULL, hash), LY_ENOTFOUND); /* hash not found */ |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 543 | |
| 544 | if ((rec->hash == hash) && ht->val_equal(val_p, &rec->val, 1, ht->cb_data)) { |
| 545 | /* even the value matches */ |
| 546 | first_matched = 1; |
| 547 | } |
| 548 | |
| 549 | /* we always need to go through collisions */ |
| 550 | crec = rec; |
| 551 | for (i = 1; i < crec->hits; ++i) { |
| 552 | r = lyht_find_collision(ht, &rec, crec); |
| 553 | assert(!r); |
| 554 | |
| 555 | /* compare values */ |
| 556 | if (!first_matched && (rec->hash == hash) && ht->val_equal(val_p, &rec->val, 1, ht->cb_data)) { |
| 557 | break; |
| 558 | } |
| 559 | } |
| 560 | |
| 561 | if (i < crec->hits) { |
| 562 | /* one of collisions matched, reduce collision count, remove the record */ |
| 563 | assert(!first_matched); |
| 564 | --crec->hits; |
| 565 | rec->hits = -1; |
| 566 | } else if (first_matched) { |
| 567 | /* the first record matches */ |
| 568 | if (crec != rec) { |
| 569 | /* ... so put the last collision in its place */ |
| 570 | rec->hits = crec->hits - 1; |
| 571 | memcpy(crec, rec, ht->rec_size); |
| 572 | } |
| 573 | rec->hits = -1; |
| 574 | } else { |
| 575 | /* value not found even in collisions */ |
Michal Vasko | 4a4c7ed | 2020-07-17 09:30:12 +0200 | [diff] [blame] | 576 | return LY_ENOTFOUND; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 577 | } |
| 578 | |
| 579 | /* check size & shrink if needed */ |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 580 | --ht->used; |
Michal Vasko | dc95d9c | 2021-04-12 15:11:48 +0200 | [diff] [blame] | 581 | ++ht->invalid; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 582 | if (ht->resize == 2) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 583 | r = (ht->used * LYHT_HUNDRED_PERCENTAGE) / ht->size; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 584 | if ((r < LYHT_SHRINK_PERCENTAGE) && (ht->size > LYHT_MIN_SIZE)) { |
Michal Vasko | 5bcc33b | 2020-10-06 15:33:44 +0200 | [diff] [blame] | 585 | if (resize_val_equal) { |
| 586 | old_val_equal = lyht_set_cb(ht, resize_val_equal); |
| 587 | } |
| 588 | |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 589 | /* shrink */ |
Michal Vasko | dc95d9c | 2021-04-12 15:11:48 +0200 | [diff] [blame] | 590 | ret = lyht_resize(ht, -1); |
Michal Vasko | 5bcc33b | 2020-10-06 15:33:44 +0200 | [diff] [blame] | 591 | |
| 592 | if (resize_val_equal) { |
| 593 | lyht_set_cb(ht, old_val_equal); |
| 594 | } |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 595 | } |
| 596 | } |
| 597 | |
Michal Vasko | dc95d9c | 2021-04-12 15:11:48 +0200 | [diff] [blame] | 598 | /* rehash all records if needed */ |
| 599 | r = ((ht->size - ht->used - ht->invalid) * 100) / ht->size; |
| 600 | if (r < LYHT_REHASH_PERCENTAGE) { |
| 601 | if (resize_val_equal) { |
| 602 | old_val_equal = lyht_set_cb(ht, resize_val_equal); |
| 603 | } |
| 604 | |
| 605 | /* rehash */ |
| 606 | ret = lyht_resize(ht, 0); |
| 607 | |
| 608 | if (resize_val_equal) { |
| 609 | lyht_set_cb(ht, old_val_equal); |
| 610 | } |
| 611 | } |
| 612 | |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 613 | return ret; |
| 614 | } |
Michal Vasko | 5bcc33b | 2020-10-06 15:33:44 +0200 | [diff] [blame] | 615 | |
Michal Vasko | ae130f5 | 2023-04-20 14:25:16 +0200 | [diff] [blame] | 616 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | 8efac24 | 2023-03-30 08:24:56 +0200 | [diff] [blame] | 617 | lyht_remove(struct ly_ht *ht, void *val_p, uint32_t hash) |
Michal Vasko | 5bcc33b | 2020-10-06 15:33:44 +0200 | [diff] [blame] | 618 | { |
| 619 | return lyht_remove_with_resize_cb(ht, val_p, hash, NULL); |
| 620 | } |
Michal Vasko | 626196f | 2022-08-05 12:49:52 +0200 | [diff] [blame] | 621 | |
Michal Vasko | ae130f5 | 2023-04-20 14:25:16 +0200 | [diff] [blame] | 622 | LIBYANG_API_DEF uint32_t |
Michal Vasko | 626196f | 2022-08-05 12:49:52 +0200 | [diff] [blame] | 623 | lyht_get_fixed_size(uint32_t item_count) |
| 624 | { |
| 625 | uint32_t i, size = 0; |
| 626 | |
| 627 | /* detect number of upper zero bits in the items' counter value ... */ |
| 628 | for (i = (sizeof item_count * CHAR_BIT) - 1; i > 0; i--) { |
| 629 | size = item_count << i; |
| 630 | size = size >> i; |
| 631 | if (size == item_count) { |
| 632 | break; |
| 633 | } |
| 634 | } |
| 635 | assert(i); |
| 636 | |
| 637 | /* ... and then we convert it to the position of the highest non-zero bit ... */ |
| 638 | i = (sizeof item_count * CHAR_BIT) - i; |
| 639 | |
| 640 | /* ... and by using it to shift 1 to the left we get the closest sufficient hash table size */ |
| 641 | size = 1 << i; |
| 642 | |
| 643 | return size; |
| 644 | } |