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