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 | |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 24 | #include "compat.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 25 | #include "dict.h" |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 26 | #include "log.h" |
Michal Vasko | 8f702ee | 2024-02-20 15:44:24 +0100 | [diff] [blame] | 27 | #include "ly_common.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 | |
Olivier Matz | 75c0019 | 2023-09-21 14:35:12 +0200 | [diff] [blame] | 58 | static LY_ERR |
| 59 | lyht_init_hlists_and_records(struct ly_ht *ht) |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 60 | { |
Olivier Matz | 75c0019 | 2023-09-21 14:35:12 +0200 | [diff] [blame] | 61 | struct ly_ht_rec *rec; |
| 62 | uint32_t i; |
| 63 | |
| 64 | ht->recs = calloc(ht->size, ht->rec_size); |
| 65 | LY_CHECK_ERR_RET(!ht->recs, LOGMEM(NULL), LY_EMEM); |
| 66 | for (i = 0; i < ht->size; i++) { |
| 67 | rec = lyht_get_rec(ht->recs, ht->rec_size, i); |
Olivier Matz | c0feb68 | 2023-09-21 08:53:59 +0200 | [diff] [blame] | 68 | if (i != ht->size) { |
Olivier Matz | 75c0019 | 2023-09-21 14:35:12 +0200 | [diff] [blame] | 69 | rec->next = i + 1; |
Olivier Matz | c0feb68 | 2023-09-21 08:53:59 +0200 | [diff] [blame] | 70 | } else { |
Olivier Matz | 75c0019 | 2023-09-21 14:35:12 +0200 | [diff] [blame] | 71 | rec->next = LYHT_NO_RECORD; |
Olivier Matz | c0feb68 | 2023-09-21 08:53:59 +0200 | [diff] [blame] | 72 | } |
Olivier Matz | 75c0019 | 2023-09-21 14:35:12 +0200 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | ht->hlists = malloc(sizeof(ht->hlists[0]) * ht->size); |
| 76 | LY_CHECK_ERR_RET(!ht->hlists, free(ht->recs); LOGMEM(NULL), LY_EMEM); |
Olivier Matz | 6a669c1 | 2023-09-28 12:07:12 +0200 | [diff] [blame] | 77 | for (i = 0; i < ht->size; i++) { |
| 78 | ht->hlists[i].first = LYHT_NO_RECORD; |
| 79 | ht->hlists[i].last = LYHT_NO_RECORD; |
| 80 | } |
Olivier Matz | 75c0019 | 2023-09-21 14:35:12 +0200 | [diff] [blame] | 81 | ht->first_free_rec = 0; |
| 82 | |
| 83 | return LY_SUCCESS; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 84 | } |
| 85 | |
Michal Vasko | ae130f5 | 2023-04-20 14:25:16 +0200 | [diff] [blame] | 86 | LIBYANG_API_DEF struct ly_ht * |
Michal Vasko | 62524a9 | 2021-02-26 10:08:50 +0100 | [diff] [blame] | 87 | 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] | 88 | { |
Michal Vasko | 8efac24 | 2023-03-30 08:24:56 +0200 | [diff] [blame] | 89 | struct ly_ht *ht; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 90 | |
| 91 | /* check that 2^x == size (power of 2) */ |
| 92 | assert(size && !(size & (size - 1))); |
| 93 | assert(val_equal && val_size); |
| 94 | assert(resize == 0 || resize == 1); |
| 95 | |
| 96 | if (size < LYHT_MIN_SIZE) { |
| 97 | size = LYHT_MIN_SIZE; |
| 98 | } |
| 99 | |
| 100 | ht = malloc(sizeof *ht); |
Michal Vasko | b3d0d6b | 2018-09-07 10:17:33 +0200 | [diff] [blame] | 101 | LY_CHECK_ERR_RET(!ht, LOGMEM(NULL), NULL); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 102 | |
| 103 | ht->used = 0; |
| 104 | ht->size = size; |
| 105 | ht->val_equal = val_equal; |
| 106 | ht->cb_data = cb_data; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 107 | ht->resize = resize; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 108 | |
Olivier Matz | 75c0019 | 2023-09-21 14:35:12 +0200 | [diff] [blame] | 109 | ht->rec_size = SIZEOF_LY_HT_REC + val_size; |
| 110 | if (lyht_init_hlists_and_records(ht) != LY_SUCCESS) { |
| 111 | free(ht); |
| 112 | return NULL; |
| 113 | } |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 114 | |
| 115 | return ht; |
| 116 | } |
| 117 | |
Michal Vasko | ae130f5 | 2023-04-20 14:25:16 +0200 | [diff] [blame] | 118 | LIBYANG_API_DEF lyht_value_equal_cb |
Michal Vasko | 8efac24 | 2023-03-30 08:24:56 +0200 | [diff] [blame] | 119 | 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] | 120 | { |
Michal Vasko | 62524a9 | 2021-02-26 10:08:50 +0100 | [diff] [blame] | 121 | lyht_value_equal_cb prev; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 122 | |
| 123 | prev = ht->val_equal; |
| 124 | ht->val_equal = new_val_equal; |
| 125 | return prev; |
| 126 | } |
| 127 | |
Michal Vasko | ae130f5 | 2023-04-20 14:25:16 +0200 | [diff] [blame] | 128 | LIBYANG_API_DEF void * |
Michal Vasko | 8efac24 | 2023-03-30 08:24:56 +0200 | [diff] [blame] | 129 | lyht_set_cb_data(struct ly_ht *ht, void *new_cb_data) |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 130 | { |
| 131 | void *prev; |
| 132 | |
| 133 | prev = ht->cb_data; |
| 134 | ht->cb_data = new_cb_data; |
| 135 | return prev; |
| 136 | } |
| 137 | |
Michal Vasko | ae130f5 | 2023-04-20 14:25:16 +0200 | [diff] [blame] | 138 | LIBYANG_API_DEF struct ly_ht * |
Michal Vasko | 8efac24 | 2023-03-30 08:24:56 +0200 | [diff] [blame] | 139 | lyht_dup(const struct ly_ht *orig) |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 140 | { |
Michal Vasko | 8efac24 | 2023-03-30 08:24:56 +0200 | [diff] [blame] | 141 | struct ly_ht *ht; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 142 | |
Michal Vasko | b3d0d6b | 2018-09-07 10:17:33 +0200 | [diff] [blame] | 143 | LY_CHECK_ARG_RET(NULL, orig, NULL); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 144 | |
Olivier Matz | 75c0019 | 2023-09-21 14:35:12 +0200 | [diff] [blame] | 145 | ht = lyht_new(orig->size, orig->rec_size - SIZEOF_LY_HT_REC, orig->val_equal, orig->cb_data, orig->resize ? 1 : 0); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 146 | if (!ht) { |
| 147 | return NULL; |
| 148 | } |
| 149 | |
Olivier Matz | 75c0019 | 2023-09-21 14:35:12 +0200 | [diff] [blame] | 150 | memcpy(ht->hlists, orig->hlists, sizeof(ht->hlists[0]) * orig->size); |
| 151 | memcpy(ht->recs, orig->recs, (size_t)orig->size * orig->rec_size); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 152 | ht->used = orig->used; |
| 153 | return ht; |
| 154 | } |
| 155 | |
Michal Vasko | ae130f5 | 2023-04-20 14:25:16 +0200 | [diff] [blame] | 156 | LIBYANG_API_DEF void |
Michal Vasko | 8efac24 | 2023-03-30 08:24:56 +0200 | [diff] [blame] | 157 | lyht_free(struct ly_ht *ht, void (*val_free)(void *val_p)) |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 158 | { |
Michal Vasko | 8efac24 | 2023-03-30 08:24:56 +0200 | [diff] [blame] | 159 | struct ly_ht_rec *rec; |
Olivier Matz | 75c0019 | 2023-09-21 14:35:12 +0200 | [diff] [blame] | 160 | uint32_t hlist_idx; |
| 161 | uint32_t rec_idx; |
Michal Vasko | 77b7f90a | 2023-01-31 15:42:41 +0100 | [diff] [blame] | 162 | |
| 163 | if (!ht) { |
| 164 | return; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 165 | } |
Michal Vasko | 77b7f90a | 2023-01-31 15:42:41 +0100 | [diff] [blame] | 166 | |
| 167 | if (val_free) { |
Olivier Matz | c0feb68 | 2023-09-21 08:53:59 +0200 | [diff] [blame] | 168 | LYHT_ITER_ALL_RECS(ht, hlist_idx, rec_idx, rec) { |
Olivier Matz | 75c0019 | 2023-09-21 14:35:12 +0200 | [diff] [blame] | 169 | val_free(&rec->val); |
Olivier Matz | c0feb68 | 2023-09-21 08:53:59 +0200 | [diff] [blame] | 170 | } |
Michal Vasko | 77b7f90a | 2023-01-31 15:42:41 +0100 | [diff] [blame] | 171 | } |
Olivier Matz | 75c0019 | 2023-09-21 14:35:12 +0200 | [diff] [blame] | 172 | free(ht->hlists); |
Michal Vasko | 77b7f90a | 2023-01-31 15:42:41 +0100 | [diff] [blame] | 173 | free(ht->recs); |
| 174 | free(ht); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 175 | } |
| 176 | |
Michal Vasko | dc95d9c | 2021-04-12 15:11:48 +0200 | [diff] [blame] | 177 | /** |
| 178 | * @brief Resize a hash table. |
| 179 | * |
| 180 | * @param[in] ht Hash table to resize. |
| 181 | * @param[in] operation Operation to perform. 1 to enlarge, -1 to shrink, 0 to only rehash all records. |
| 182 | * @return LY_ERR value. |
| 183 | */ |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 184 | static LY_ERR |
Olivier Matz | 6ad93d9 | 2023-09-28 12:13:36 +0200 | [diff] [blame] | 185 | lyht_resize(struct ly_ht *ht, int operation, int check) |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 186 | { |
Michal Vasko | 8efac24 | 2023-03-30 08:24:56 +0200 | [diff] [blame] | 187 | struct ly_ht_rec *rec; |
Olivier Matz | 6a669c1 | 2023-09-28 12:07:12 +0200 | [diff] [blame] | 188 | struct ly_ht_hlist *old_hlists; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 189 | unsigned char *old_recs; |
Olivier Matz | 75c0019 | 2023-09-21 14:35:12 +0200 | [diff] [blame] | 190 | uint32_t old_first_free_rec; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 191 | uint32_t i, old_size; |
Olivier Matz | 75c0019 | 2023-09-21 14:35:12 +0200 | [diff] [blame] | 192 | uint32_t rec_idx; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 193 | |
Olivier Matz | 75c0019 | 2023-09-21 14:35:12 +0200 | [diff] [blame] | 194 | old_hlists = ht->hlists; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 195 | old_recs = ht->recs; |
| 196 | old_size = ht->size; |
Olivier Matz | 75c0019 | 2023-09-21 14:35:12 +0200 | [diff] [blame] | 197 | old_first_free_rec = ht->first_free_rec; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 198 | |
Michal Vasko | dc95d9c | 2021-04-12 15:11:48 +0200 | [diff] [blame] | 199 | if (operation > 0) { |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 200 | /* double the size */ |
| 201 | ht->size <<= 1; |
Michal Vasko | dc95d9c | 2021-04-12 15:11:48 +0200 | [diff] [blame] | 202 | } else if (operation < 0) { |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 203 | /* half the size */ |
| 204 | ht->size >>= 1; |
| 205 | } |
| 206 | |
Olivier Matz | 75c0019 | 2023-09-21 14:35:12 +0200 | [diff] [blame] | 207 | if (lyht_init_hlists_and_records(ht) != LY_SUCCESS) { |
| 208 | ht->hlists = old_hlists; |
| 209 | ht->recs = old_recs; |
| 210 | ht->size = old_size; |
| 211 | ht->first_free_rec = old_first_free_rec; |
| 212 | return LY_EMEM; |
| 213 | } |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 214 | |
Olivier Matz | 75c0019 | 2023-09-21 14:35:12 +0200 | [diff] [blame] | 215 | /* reset used, it will increase again */ |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 216 | ht->used = 0; |
| 217 | |
| 218 | /* add all the old records into the new records array */ |
Olivier Matz | 75c0019 | 2023-09-21 14:35:12 +0200 | [diff] [blame] | 219 | for (i = 0; i < old_size; i++) { |
Olivier Matz | 6a669c1 | 2023-09-28 12:07:12 +0200 | [diff] [blame] | 220 | for (rec_idx = old_hlists[i].first, rec = lyht_get_rec(old_recs, ht->rec_size, rec_idx); |
Olivier Matz | c0feb68 | 2023-09-21 08:53:59 +0200 | [diff] [blame] | 221 | rec_idx != LYHT_NO_RECORD; |
| 222 | rec_idx = rec->next, rec = lyht_get_rec(old_recs, ht->rec_size, rec_idx)) { |
Olivier Matz | 75c0019 | 2023-09-21 14:35:12 +0200 | [diff] [blame] | 223 | LY_ERR ret; |
| 224 | |
Olivier Matz | c0feb68 | 2023-09-21 08:53:59 +0200 | [diff] [blame] | 225 | if (check) { |
Olivier Matz | 6ad93d9 | 2023-09-28 12:13:36 +0200 | [diff] [blame] | 226 | ret = lyht_insert(ht, rec->val, rec->hash, NULL); |
Olivier Matz | c0feb68 | 2023-09-21 08:53:59 +0200 | [diff] [blame] | 227 | } else { |
Olivier Matz | 6ad93d9 | 2023-09-28 12:13:36 +0200 | [diff] [blame] | 228 | ret = lyht_insert_no_check(ht, rec->val, rec->hash, NULL); |
Olivier Matz | c0feb68 | 2023-09-21 08:53:59 +0200 | [diff] [blame] | 229 | } |
Michal Vasko | 26bbb27 | 2022-08-02 14:54:33 +0200 | [diff] [blame] | 230 | |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 231 | assert(!ret); |
| 232 | (void)ret; |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | /* final touches */ |
| 237 | free(old_recs); |
Olivier Matz | 75c0019 | 2023-09-21 14:35:12 +0200 | [diff] [blame] | 238 | free(old_hlists); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 239 | return LY_SUCCESS; |
| 240 | } |
| 241 | |
Michal Vasko | da85903 | 2020-07-14 12:20:14 +0200 | [diff] [blame] | 242 | /** |
Michal Vasko | da85903 | 2020-07-14 12:20:14 +0200 | [diff] [blame] | 243 | * @brief Search for a record with specific value and hash. |
| 244 | * |
| 245 | * @param[in] ht Hash table to search in. |
| 246 | * @param[in] val_p Pointer to the value to find. |
| 247 | * @param[in] hash Hash to find. |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 248 | * @param[in] mod Whether the operation modifies the hash table (insert or remove) or not (find). |
Michal Vasko | f1e0941 | 2023-10-23 10:03:21 +0200 | [diff] [blame] | 249 | * @param[in] val_equal Callback for checking value equivalence. |
Michal Vasko | da85903 | 2020-07-14 12:20:14 +0200 | [diff] [blame] | 250 | * @param[out] col Optional collision number of @p rec_p, 0 for no collision. |
| 251 | * @param[out] rec_p Found exact matching record, may be a collision of @p crec_p. |
| 252 | * @return LY_ENOTFOUND if no record found, |
| 253 | * @return LY_SUCCESS if record was found. |
| 254 | */ |
| 255 | static LY_ERR |
Michal Vasko | f1e0941 | 2023-10-23 10:03:21 +0200 | [diff] [blame] | 256 | lyht_find_rec(const struct ly_ht *ht, void *val_p, uint32_t hash, ly_bool mod, lyht_value_equal_cb val_equal, |
Ondrej Kusnirik | c2f76fe | 2024-08-15 09:09:13 +0200 | [diff] [blame] | 257 | uint32_t *col, struct ly_ht_rec **rec_p) |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 258 | { |
Olivier Matz | 75c0019 | 2023-09-21 14:35:12 +0200 | [diff] [blame] | 259 | uint32_t hlist_idx = hash & (ht->size - 1); |
| 260 | struct ly_ht_rec *rec; |
| 261 | uint32_t rec_idx; |
Michal Vasko | da85903 | 2020-07-14 12:20:14 +0200 | [diff] [blame] | 262 | |
Michal Vasko | da85903 | 2020-07-14 12:20:14 +0200 | [diff] [blame] | 263 | if (col) { |
| 264 | *col = 0; |
| 265 | } |
| 266 | *rec_p = NULL; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 267 | |
Olivier Matz | 75c0019 | 2023-09-21 14:35:12 +0200 | [diff] [blame] | 268 | LYHT_ITER_HLIST_RECS(ht, hlist_idx, rec_idx, rec) { |
Michal Vasko | f1e0941 | 2023-10-23 10:03:21 +0200 | [diff] [blame] | 269 | if ((rec->hash == hash) && val_equal(val_p, &rec->val, mod, ht->cb_data)) { |
Michal Vasko | da85903 | 2020-07-14 12:20:14 +0200 | [diff] [blame] | 270 | *rec_p = rec; |
| 271 | return LY_SUCCESS; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 272 | } |
Olivier Matz | 75c0019 | 2023-09-21 14:35:12 +0200 | [diff] [blame] | 273 | |
| 274 | if (col) { |
| 275 | *col = *col + 1; |
| 276 | } |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | /* not found even in collisions */ |
Michal Vasko | da85903 | 2020-07-14 12:20:14 +0200 | [diff] [blame] | 280 | return LY_ENOTFOUND; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 281 | } |
| 282 | |
Michal Vasko | ae130f5 | 2023-04-20 14:25:16 +0200 | [diff] [blame] | 283 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | f1e0941 | 2023-10-23 10:03:21 +0200 | [diff] [blame] | 284 | lyht_find(const 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] | 285 | { |
Michal Vasko | 8efac24 | 2023-03-30 08:24:56 +0200 | [diff] [blame] | 286 | struct ly_ht_rec *rec; |
Michal Vasko | da85903 | 2020-07-14 12:20:14 +0200 | [diff] [blame] | 287 | |
Michal Vasko | 3a2d461 | 2024-08-16 15:55:15 +0200 | [diff] [blame^] | 288 | if (match_p) { |
| 289 | *match_p = NULL; |
| 290 | } |
| 291 | |
Ondrej Kusnirik | c2f76fe | 2024-08-15 09:09:13 +0200 | [diff] [blame] | 292 | lyht_find_rec(ht, val_p, hash, 0, ht->val_equal, NULL, &rec); |
Michal Vasko | da85903 | 2020-07-14 12:20:14 +0200 | [diff] [blame] | 293 | |
| 294 | if (rec && match_p) { |
| 295 | *match_p = rec->val; |
| 296 | } |
| 297 | return rec ? LY_SUCCESS : LY_ENOTFOUND; |
| 298 | } |
| 299 | |
Michal Vasko | ae130f5 | 2023-04-20 14:25:16 +0200 | [diff] [blame] | 300 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | f1e0941 | 2023-10-23 10:03:21 +0200 | [diff] [blame] | 301 | lyht_find_with_val_cb(const struct ly_ht *ht, void *val_p, uint32_t hash, lyht_value_equal_cb val_equal, void **match_p) |
| 302 | { |
| 303 | struct ly_ht_rec *rec; |
| 304 | |
Ondrej Kusnirik | c2f76fe | 2024-08-15 09:09:13 +0200 | [diff] [blame] | 305 | lyht_find_rec(ht, val_p, hash, 0, val_equal ? val_equal : ht->val_equal, NULL, &rec); |
Michal Vasko | f1e0941 | 2023-10-23 10:03:21 +0200 | [diff] [blame] | 306 | |
| 307 | if (rec && match_p) { |
| 308 | *match_p = rec->val; |
| 309 | } |
| 310 | return rec ? LY_SUCCESS : LY_ENOTFOUND; |
| 311 | } |
| 312 | |
| 313 | LIBYANG_API_DEF LY_ERR |
| 314 | lyht_find_next_with_collision_cb(const struct ly_ht *ht, void *val_p, uint32_t hash, |
Michal Vasko | 6374de2 | 2022-09-05 15:48:48 +0200 | [diff] [blame] | 315 | lyht_value_equal_cb collision_val_equal, void **match_p) |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 316 | { |
Ondrej Kusnirik | c2f76fe | 2024-08-15 09:09:13 +0200 | [diff] [blame] | 317 | struct ly_ht_rec *rec; |
Olivier Matz | 75c0019 | 2023-09-21 14:35:12 +0200 | [diff] [blame] | 318 | uint32_t rec_idx; |
| 319 | uint32_t i; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 320 | |
Michal Vasko | 6374de2 | 2022-09-05 15:48:48 +0200 | [diff] [blame] | 321 | /* find the record of the previously found value */ |
Ondrej Kusnirik | c2f76fe | 2024-08-15 09:09:13 +0200 | [diff] [blame] | 322 | if (lyht_find_rec(ht, val_p, hash, 1, ht->val_equal, &i, &rec)) { |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 323 | /* not found, cannot happen */ |
Michal Vasko | da85903 | 2020-07-14 12:20:14 +0200 | [diff] [blame] | 324 | LOGINT_RET(NULL); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 325 | } |
| 326 | |
Olivier Matz | 75c0019 | 2023-09-21 14:35:12 +0200 | [diff] [blame] | 327 | for (rec_idx = rec->next, rec = lyht_get_rec(ht->recs, ht->rec_size, rec_idx); |
Olivier Matz | c0feb68 | 2023-09-21 08:53:59 +0200 | [diff] [blame] | 328 | rec_idx != LYHT_NO_RECORD; |
| 329 | rec_idx = rec->next, rec = lyht_get_rec(ht->recs, ht->rec_size, rec_idx)) { |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 330 | |
Michal Vasko | 6374de2 | 2022-09-05 15:48:48 +0200 | [diff] [blame] | 331 | if (rec->hash != hash) { |
| 332 | continue; |
| 333 | } |
| 334 | |
| 335 | if (collision_val_equal) { |
| 336 | if (collision_val_equal(val_p, &rec->val, 0, ht->cb_data)) { |
| 337 | /* even the value matches */ |
| 338 | if (match_p) { |
| 339 | *match_p = rec->val; |
| 340 | } |
| 341 | return LY_SUCCESS; |
| 342 | } |
| 343 | } 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] | 344 | /* even the value matches */ |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 345 | if (match_p) { |
| 346 | *match_p = rec->val; |
| 347 | } |
Michal Vasko | da85903 | 2020-07-14 12:20:14 +0200 | [diff] [blame] | 348 | return LY_SUCCESS; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 349 | } |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | /* the last equal value was already returned */ |
Michal Vasko | da85903 | 2020-07-14 12:20:14 +0200 | [diff] [blame] | 353 | return LY_ENOTFOUND; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 354 | } |
| 355 | |
Michal Vasko | ae130f5 | 2023-04-20 14:25:16 +0200 | [diff] [blame] | 356 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | f1e0941 | 2023-10-23 10:03:21 +0200 | [diff] [blame] | 357 | lyht_find_next(const 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] | 358 | { |
| 359 | return lyht_find_next_with_collision_cb(ht, val_p, hash, NULL, match_p); |
| 360 | } |
| 361 | |
Olivier Matz | 75c0019 | 2023-09-21 14:35:12 +0200 | [diff] [blame] | 362 | static LY_ERR |
Michal Vasko | f1e0941 | 2023-10-23 10:03:21 +0200 | [diff] [blame] | 363 | _lyht_insert_with_resize_cb(struct ly_ht *ht, void *val_p, uint32_t hash, lyht_value_equal_cb resize_val_equal, |
Olivier Matz | 6ad93d9 | 2023-09-28 12:13:36 +0200 | [diff] [blame] | 364 | void **match_p, int check) |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 365 | { |
Olivier Matz | 75c0019 | 2023-09-21 14:35:12 +0200 | [diff] [blame] | 366 | uint32_t hlist_idx = hash & (ht->size - 1); |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 367 | LY_ERR r, ret = LY_SUCCESS; |
Olivier Matz | 6a669c1 | 2023-09-28 12:07:12 +0200 | [diff] [blame] | 368 | struct ly_ht_rec *rec, *prev_rec; |
Michal Vasko | 62524a9 | 2021-02-26 10:08:50 +0100 | [diff] [blame] | 369 | lyht_value_equal_cb old_val_equal = NULL; |
Olivier Matz | 75c0019 | 2023-09-21 14:35:12 +0200 | [diff] [blame] | 370 | uint32_t rec_idx; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 371 | |
Olivier Matz | 6ad93d9 | 2023-09-28 12:13:36 +0200 | [diff] [blame] | 372 | if (check) { |
Ondrej Kusnirik | c2f76fe | 2024-08-15 09:09:13 +0200 | [diff] [blame] | 373 | if (lyht_find_rec(ht, val_p, hash, 1, ht->val_equal, NULL, &rec) == LY_SUCCESS) { |
Olivier Matz | 6ad93d9 | 2023-09-28 12:13:36 +0200 | [diff] [blame] | 374 | if (rec && match_p) { |
| 375 | *match_p = rec->val; |
| 376 | } |
| 377 | return LY_EEXIST; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 378 | } |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 379 | } |
| 380 | |
Olivier Matz | 75c0019 | 2023-09-21 14:35:12 +0200 | [diff] [blame] | 381 | rec_idx = ht->first_free_rec; |
| 382 | assert(rec_idx < ht->size); |
| 383 | rec = lyht_get_rec(ht->recs, ht->rec_size, rec_idx); |
| 384 | ht->first_free_rec = rec->next; |
Olivier Matz | 6a669c1 | 2023-09-28 12:07:12 +0200 | [diff] [blame] | 385 | |
| 386 | if (ht->hlists[hlist_idx].first == LYHT_NO_RECORD) { |
| 387 | ht->hlists[hlist_idx].first = rec_idx; |
| 388 | } else { |
| 389 | prev_rec = lyht_get_rec(ht->recs, ht->rec_size, ht->hlists[hlist_idx].last); |
| 390 | prev_rec->next = rec_idx; |
| 391 | } |
| 392 | rec->next = LYHT_NO_RECORD; |
| 393 | ht->hlists[hlist_idx].last = rec_idx; |
Olivier Matz | 75c0019 | 2023-09-21 14:35:12 +0200 | [diff] [blame] | 394 | |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 395 | rec->hash = hash; |
Olivier Matz | 75c0019 | 2023-09-21 14:35:12 +0200 | [diff] [blame] | 396 | memcpy(&rec->val, val_p, ht->rec_size - SIZEOF_LY_HT_REC); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 397 | if (match_p) { |
| 398 | *match_p = (void *)&rec->val; |
| 399 | } |
| 400 | |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 401 | /* check size & enlarge if needed */ |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 402 | ++ht->used; |
| 403 | if (ht->resize) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 404 | r = (ht->used * LYHT_HUNDRED_PERCENTAGE) / ht->size; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 405 | if ((ht->resize == 1) && (r >= LYHT_FIRST_SHRINK_PERCENTAGE)) { |
| 406 | /* enable shrinking */ |
| 407 | ht->resize = 2; |
| 408 | } |
| 409 | if ((ht->resize == 2) && (r >= LYHT_ENLARGE_PERCENTAGE)) { |
| 410 | if (resize_val_equal) { |
| 411 | old_val_equal = lyht_set_cb(ht, resize_val_equal); |
| 412 | } |
| 413 | |
| 414 | /* enlarge */ |
Olivier Matz | 6ad93d9 | 2023-09-28 12:13:36 +0200 | [diff] [blame] | 415 | ret = lyht_resize(ht, 1, check); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 416 | /* if hash_table was resized, we need to find new matching value */ |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 417 | if ((ret == LY_SUCCESS) && match_p) { |
Michal Vasko | 5911880 | 2023-10-04 16:00:26 +0200 | [diff] [blame] | 418 | ret = lyht_find(ht, val_p, hash, match_p); |
| 419 | assert(!ret); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | if (resize_val_equal) { |
| 423 | lyht_set_cb(ht, old_val_equal); |
| 424 | } |
| 425 | } |
| 426 | } |
| 427 | return ret; |
| 428 | } |
| 429 | |
Michal Vasko | ae130f5 | 2023-04-20 14:25:16 +0200 | [diff] [blame] | 430 | LIBYANG_API_DEF LY_ERR |
Olivier Matz | 75c0019 | 2023-09-21 14:35:12 +0200 | [diff] [blame] | 431 | lyht_insert_with_resize_cb(struct ly_ht *ht, void *val_p, uint32_t hash, lyht_value_equal_cb resize_val_equal, |
| 432 | void **match_p) |
| 433 | { |
Michal Vasko | f1e0941 | 2023-10-23 10:03:21 +0200 | [diff] [blame] | 434 | return _lyht_insert_with_resize_cb(ht, val_p, hash, resize_val_equal, match_p, 1); |
Olivier Matz | 75c0019 | 2023-09-21 14:35:12 +0200 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | 8efac24 | 2023-03-30 08:24:56 +0200 | [diff] [blame] | 438 | 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] | 439 | { |
Michal Vasko | f1e0941 | 2023-10-23 10:03:21 +0200 | [diff] [blame] | 440 | return _lyht_insert_with_resize_cb(ht, val_p, hash, NULL, match_p, 1); |
Olivier Matz | 6ad93d9 | 2023-09-28 12:13:36 +0200 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | LIBYANG_API_DEF LY_ERR |
| 444 | lyht_insert_no_check(struct ly_ht *ht, void *val_p, uint32_t hash, void **match_p) |
| 445 | { |
Michal Vasko | f1e0941 | 2023-10-23 10:03:21 +0200 | [diff] [blame] | 446 | return _lyht_insert_with_resize_cb(ht, val_p, hash, NULL, match_p, 0); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 447 | } |
| 448 | |
Michal Vasko | ae130f5 | 2023-04-20 14:25:16 +0200 | [diff] [blame] | 449 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | 8efac24 | 2023-03-30 08:24:56 +0200 | [diff] [blame] | 450 | 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] | 451 | { |
Olivier Matz | 75c0019 | 2023-09-21 14:35:12 +0200 | [diff] [blame] | 452 | struct ly_ht_rec *found_rec, *prev_rec, *rec; |
| 453 | uint32_t hlist_idx = hash & (ht->size - 1); |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 454 | LY_ERR r, ret = LY_SUCCESS; |
steweg | d8e2fc9 | 2023-05-31 09:52:56 +0200 | [diff] [blame] | 455 | lyht_value_equal_cb old_val_equal = NULL; |
Olivier Matz | 75c0019 | 2023-09-21 14:35:12 +0200 | [diff] [blame] | 456 | uint32_t prev_rec_idx; |
| 457 | uint32_t rec_idx; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 458 | |
Ondrej Kusnirik | c2f76fe | 2024-08-15 09:09:13 +0200 | [diff] [blame] | 459 | if (lyht_find_rec(ht, val_p, hash, 1, ht->val_equal, NULL, &found_rec)) { |
Michal Vasko | c70cf03 | 2023-10-23 10:04:29 +0200 | [diff] [blame] | 460 | LOGARG(NULL, hash); |
| 461 | return LY_ENOTFOUND; |
| 462 | } |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 463 | |
Olivier Matz | 75c0019 | 2023-09-21 14:35:12 +0200 | [diff] [blame] | 464 | prev_rec_idx = LYHT_NO_RECORD; |
| 465 | LYHT_ITER_HLIST_RECS(ht, hlist_idx, rec_idx, rec) { |
Olivier Matz | c0feb68 | 2023-09-21 08:53:59 +0200 | [diff] [blame] | 466 | if (rec == found_rec) { |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 467 | break; |
Olivier Matz | c0feb68 | 2023-09-21 08:53:59 +0200 | [diff] [blame] | 468 | } |
Olivier Matz | 75c0019 | 2023-09-21 14:35:12 +0200 | [diff] [blame] | 469 | prev_rec_idx = rec_idx; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 470 | } |
| 471 | |
Olivier Matz | 75c0019 | 2023-09-21 14:35:12 +0200 | [diff] [blame] | 472 | if (prev_rec_idx == LYHT_NO_RECORD) { |
Olivier Matz | 6a669c1 | 2023-09-28 12:07:12 +0200 | [diff] [blame] | 473 | ht->hlists[hlist_idx].first = rec->next; |
Olivier Matz | c0feb68 | 2023-09-21 08:53:59 +0200 | [diff] [blame] | 474 | if (rec->next == LYHT_NO_RECORD) { |
Olivier Matz | 6a669c1 | 2023-09-28 12:07:12 +0200 | [diff] [blame] | 475 | ht->hlists[hlist_idx].last = LYHT_NO_RECORD; |
Olivier Matz | c0feb68 | 2023-09-21 08:53:59 +0200 | [diff] [blame] | 476 | } |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 477 | } else { |
Olivier Matz | 75c0019 | 2023-09-21 14:35:12 +0200 | [diff] [blame] | 478 | prev_rec = lyht_get_rec(ht->recs, ht->rec_size, prev_rec_idx); |
| 479 | prev_rec->next = rec->next; |
Olivier Matz | c0feb68 | 2023-09-21 08:53:59 +0200 | [diff] [blame] | 480 | if (rec->next == LYHT_NO_RECORD) { |
Olivier Matz | 6a669c1 | 2023-09-28 12:07:12 +0200 | [diff] [blame] | 481 | ht->hlists[hlist_idx].last = prev_rec_idx; |
Olivier Matz | c0feb68 | 2023-09-21 08:53:59 +0200 | [diff] [blame] | 482 | } |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 483 | } |
| 484 | |
Olivier Matz | 75c0019 | 2023-09-21 14:35:12 +0200 | [diff] [blame] | 485 | rec->next = ht->first_free_rec; |
| 486 | ht->first_free_rec = rec_idx; |
| 487 | |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 488 | /* check size & shrink if needed */ |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 489 | --ht->used; |
| 490 | if (ht->resize == 2) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 491 | r = (ht->used * LYHT_HUNDRED_PERCENTAGE) / ht->size; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 492 | if ((r < LYHT_SHRINK_PERCENTAGE) && (ht->size > LYHT_MIN_SIZE)) { |
Michal Vasko | 5bcc33b | 2020-10-06 15:33:44 +0200 | [diff] [blame] | 493 | if (resize_val_equal) { |
| 494 | old_val_equal = lyht_set_cb(ht, resize_val_equal); |
| 495 | } |
| 496 | |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 497 | /* shrink */ |
Olivier Matz | 6ad93d9 | 2023-09-28 12:13:36 +0200 | [diff] [blame] | 498 | ret = lyht_resize(ht, -1, 1); |
Michal Vasko | 5bcc33b | 2020-10-06 15:33:44 +0200 | [diff] [blame] | 499 | |
| 500 | if (resize_val_equal) { |
| 501 | lyht_set_cb(ht, old_val_equal); |
| 502 | } |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 503 | } |
| 504 | } |
| 505 | |
| 506 | return ret; |
| 507 | } |
Michal Vasko | 5bcc33b | 2020-10-06 15:33:44 +0200 | [diff] [blame] | 508 | |
Michal Vasko | ae130f5 | 2023-04-20 14:25:16 +0200 | [diff] [blame] | 509 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | 8efac24 | 2023-03-30 08:24:56 +0200 | [diff] [blame] | 510 | lyht_remove(struct ly_ht *ht, void *val_p, uint32_t hash) |
Michal Vasko | 5bcc33b | 2020-10-06 15:33:44 +0200 | [diff] [blame] | 511 | { |
| 512 | return lyht_remove_with_resize_cb(ht, val_p, hash, NULL); |
| 513 | } |
Michal Vasko | 626196f | 2022-08-05 12:49:52 +0200 | [diff] [blame] | 514 | |
Michal Vasko | ae130f5 | 2023-04-20 14:25:16 +0200 | [diff] [blame] | 515 | LIBYANG_API_DEF uint32_t |
Michal Vasko | 626196f | 2022-08-05 12:49:52 +0200 | [diff] [blame] | 516 | lyht_get_fixed_size(uint32_t item_count) |
| 517 | { |
Olivier Matz | c0feb68 | 2023-09-21 08:53:59 +0200 | [diff] [blame] | 518 | if (item_count == 0) { |
Olivier Matz | 75c0019 | 2023-09-21 14:35:12 +0200 | [diff] [blame] | 519 | return 1; |
Olivier Matz | c0feb68 | 2023-09-21 08:53:59 +0200 | [diff] [blame] | 520 | } |
Michal Vasko | 626196f | 2022-08-05 12:49:52 +0200 | [diff] [blame] | 521 | |
Olivier Matz | 75c0019 | 2023-09-21 14:35:12 +0200 | [diff] [blame] | 522 | /* return next power of 2 (greater or equal) */ |
| 523 | item_count--; |
| 524 | item_count |= item_count >> 1; |
| 525 | item_count |= item_count >> 2; |
| 526 | item_count |= item_count >> 4; |
| 527 | item_count |= item_count >> 8; |
| 528 | item_count |= item_count >> 16; |
Michal Vasko | 626196f | 2022-08-05 12:49:52 +0200 | [diff] [blame] | 529 | |
Olivier Matz | 75c0019 | 2023-09-21 14:35:12 +0200 | [diff] [blame] | 530 | return item_count + 1; |
Michal Vasko | 626196f | 2022-08-05 12:49:52 +0200 | [diff] [blame] | 531 | } |