Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @file hash_table.h |
| 3 | * @author Radek Krejci <rkrejci@cesnet.cz> |
| 4 | * @author Michal Vasko <mvasko@cesnet.cz> |
| 5 | * @brief libyang hash table |
| 6 | * |
Michal Vasko | a655fca | 2022-09-05 15:48:31 +0200 | [diff] [blame] | 7 | * Copyright (c) 2015 - 2022 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 | |
| 16 | #ifndef LY_HASH_TABLE_H_ |
| 17 | #define LY_HASH_TABLE_H_ |
| 18 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 19 | #include <pthread.h> |
| 20 | #include <stddef.h> |
| 21 | #include <stdint.h> |
| 22 | |
Michal Vasko | c5a2283 | 2020-08-20 13:21:33 +0200 | [diff] [blame] | 23 | #include "compat.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 24 | #include "log.h" |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 25 | |
| 26 | /** |
| 27 | * @brief Compute hash from (several) string(s). |
| 28 | * |
| 29 | * Usage: |
| 30 | * - init hash to 0 |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame] | 31 | * - repeatedly call ::dict_hash_multi(), provide hash from the last call |
| 32 | * - call ::dict_hash_multi() with key_part = NULL to finish the hash |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 33 | */ |
| 34 | uint32_t dict_hash_multi(uint32_t hash, const char *key_part, size_t len); |
| 35 | |
Michal Vasko | a655fca | 2022-09-05 15:48:31 +0200 | [diff] [blame] | 36 | /** |
Radek Krejci | f2dc4c5 | 2018-11-08 09:04:13 +0100 | [diff] [blame] | 37 | * @brief Compute hash from a string. |
| 38 | */ |
| 39 | uint32_t dict_hash(const char *key, size_t len); |
| 40 | |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 41 | /** |
| 42 | * @brief Callback for checking hash table values equivalence. |
| 43 | * |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 44 | * @param[in] val1_p Pointer to the first value, the one being searched (inserted/removed). |
| 45 | * @param[in] val2_p Pointer to the second value, the one stored in the hash table. |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 46 | * @param[in] mod Whether the operation modifies the hash table (insert or remove) or not (find). |
| 47 | * @param[in] cb_data User callback data. |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 48 | * @return false (non-equal) or true (equal). |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 49 | */ |
Michal Vasko | 62524a9 | 2021-02-26 10:08:50 +0100 | [diff] [blame] | 50 | typedef ly_bool (*lyht_value_equal_cb)(void *val1_p, void *val2_p, ly_bool mod, void *cb_data); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 51 | |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 52 | /** reference value for 100% */ |
| 53 | #define LYHT_HUNDRED_PERCENTAGE 100 |
| 54 | |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 55 | /** when the table is at least this much percent full, it is enlarged (double the size) */ |
| 56 | #define LYHT_ENLARGE_PERCENTAGE 75 |
| 57 | |
| 58 | /** only once the table is this much percent full, enable shrinking */ |
| 59 | #define LYHT_FIRST_SHRINK_PERCENTAGE 50 |
| 60 | |
| 61 | /** when the table is less than this much percent full, it is shrunk (half the size) */ |
| 62 | #define LYHT_SHRINK_PERCENTAGE 25 |
| 63 | |
Michal Vasko | dc95d9c | 2021-04-12 15:11:48 +0200 | [diff] [blame] | 64 | /** when the table has less than this much percent empty records, it is rehashed to get rid of all the invalid records */ |
| 65 | #define LYHT_REHASH_PERCENTAGE 2 |
| 66 | |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 67 | /** never shrink beyond this size */ |
| 68 | #define LYHT_MIN_SIZE 8 |
| 69 | |
| 70 | /** |
| 71 | * @brief Generic hash table record. |
| 72 | */ |
| 73 | struct ht_rec { |
| 74 | uint32_t hash; /* hash of the value */ |
| 75 | int32_t hits; /* collision/overflow value count - 1 (a filled entry has 1 hit, |
| 76 | * special value -1 means a deleted record) */ |
| 77 | unsigned char val[1]; /* arbitrary-size value */ |
| 78 | } _PACKED; |
| 79 | |
| 80 | /** |
| 81 | * @brief (Very) generic hash table. |
| 82 | * |
| 83 | * Hash table with open addressing collision resolution and |
| 84 | * linear probing of interval 1 (next free record is used). |
| 85 | * Removal is lazy (removed records are only marked), but |
| 86 | * if possible, they are fully emptied. |
| 87 | */ |
| 88 | struct hash_table { |
| 89 | uint32_t used; /* number of values stored in the hash table (filled records) */ |
| 90 | uint32_t size; /* always holds 2^x == size (is power of 2), actually number of records allocated */ |
Michal Vasko | dc95d9c | 2021-04-12 15:11:48 +0200 | [diff] [blame] | 91 | uint32_t invalid; /* number of invalid records (deleted) */ |
Michal Vasko | 62524a9 | 2021-02-26 10:08:50 +0100 | [diff] [blame] | 92 | lyht_value_equal_cb val_equal; /* callback for testing value equivalence */ |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 93 | void *cb_data; /* user data callback arbitrary value */ |
| 94 | uint16_t resize; /* 0 - resizing is disabled, * |
| 95 | * 1 - enlarging is enabled, * |
| 96 | * 2 - both shrinking and enlarging is enabled */ |
| 97 | uint16_t rec_size; /* real size (in bytes) of one record for accessing recs array */ |
| 98 | unsigned char *recs; /* pointer to the hash table itself (array of struct ht_rec) */ |
| 99 | }; |
| 100 | |
| 101 | struct dict_rec { |
| 102 | char *value; |
| 103 | uint32_t refcount; |
| 104 | }; |
| 105 | |
| 106 | /** |
| 107 | * dictionary to store repeating strings |
| 108 | */ |
| 109 | struct dict_table { |
| 110 | struct hash_table *hash_tab; |
| 111 | pthread_mutex_t lock; |
| 112 | }; |
| 113 | |
| 114 | /** |
| 115 | * @brief Initiate content (non-zero values) of the dictionary |
| 116 | * |
| 117 | * @param[in] dict Dictionary table to initiate |
| 118 | */ |
| 119 | void lydict_init(struct dict_table *dict); |
| 120 | |
| 121 | /** |
| 122 | * @brief Cleanup the dictionary content |
| 123 | * |
| 124 | * @param[in] dict Dictionary table to cleanup |
| 125 | */ |
| 126 | void lydict_clean(struct dict_table *dict); |
| 127 | |
| 128 | /** |
| 129 | * @brief Create new hash table. |
| 130 | * |
| 131 | * @param[in] size Starting size of the hash table (capacity of values), must be power of 2. |
| 132 | * @param[in] val_size Size in bytes of value (the stored hashed item). |
| 133 | * @param[in] val_equal Callback for checking value equivalence. |
Michal Vasko | a655fca | 2022-09-05 15:48:31 +0200 | [diff] [blame] | 134 | * @param[in] cb_data User data always passed to @p val_equal. |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 135 | * @param[in] resize Whether to resize the table on too few/too many records taken. |
| 136 | * @return Empty hash table, NULL on error. |
| 137 | */ |
Michal Vasko | 62524a9 | 2021-02-26 10:08:50 +0100 | [diff] [blame] | 138 | struct hash_table *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] | 139 | |
| 140 | /** |
| 141 | * @brief Set hash table value equal callback. |
| 142 | * |
| 143 | * @param[in] ht Hash table to modify. |
| 144 | * @param[in] new_val_equal New callback for checking value equivalence. |
| 145 | * @return Previous callback for checking value equivalence. |
| 146 | */ |
Michal Vasko | 62524a9 | 2021-02-26 10:08:50 +0100 | [diff] [blame] | 147 | lyht_value_equal_cb lyht_set_cb(struct hash_table *ht, lyht_value_equal_cb new_val_equal); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 148 | |
| 149 | /** |
| 150 | * @brief Set hash table value equal callback user data. |
| 151 | * |
| 152 | * @param[in] ht Hash table to modify. |
| 153 | * @param[in] new_cb_data New data for values callback. |
| 154 | * @return Previous data for values callback. |
| 155 | */ |
| 156 | void *lyht_set_cb_data(struct hash_table *ht, void *new_cb_data); |
| 157 | |
| 158 | /** |
| 159 | * @brief Make a duplicate of an existing hash table. |
| 160 | * |
| 161 | * @param[in] orig Original hash table to duplicate. |
Michal Vasko | a655fca | 2022-09-05 15:48:31 +0200 | [diff] [blame] | 162 | * @return Duplicated hash table @p orig, NULL on error. |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 163 | */ |
| 164 | struct hash_table *lyht_dup(const struct hash_table *orig); |
| 165 | |
| 166 | /** |
| 167 | * @brief Free a hash table. |
| 168 | * |
| 169 | * @param[in] ht Hash table to be freed. |
Michal Vasko | 77b7f90a | 2023-01-31 15:42:41 +0100 | [diff] [blame] | 170 | * @param[in] val_free Optional callback for freeing allthe stored values, @p val_p is a pointer to a stored value. |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 171 | */ |
Michal Vasko | 77b7f90a | 2023-01-31 15:42:41 +0100 | [diff] [blame] | 172 | void lyht_free(struct hash_table *ht, void (*val_free)(void *val_p)); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 173 | |
| 174 | /** |
| 175 | * @brief Find a value in a hash table. |
| 176 | * |
| 177 | * @param[in] ht Hash table to search in. |
| 178 | * @param[in] val_p Pointer to the value to find. |
| 179 | * @param[in] hash Hash of the stored value. |
| 180 | * @param[out] match_p Pointer to the matching value, optional. |
Michal Vasko | da85903 | 2020-07-14 12:20:14 +0200 | [diff] [blame] | 181 | * @return LY_SUCCESS if value was found, |
| 182 | * @return LY_ENOTFOUND if not found. |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 183 | */ |
Michal Vasko | da85903 | 2020-07-14 12:20:14 +0200 | [diff] [blame] | 184 | LY_ERR lyht_find(struct hash_table *ht, void *val_p, uint32_t hash, void **match_p); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 185 | |
| 186 | /** |
| 187 | * @brief Find another equal value in the hash table. |
| 188 | * |
| 189 | * @param[in] ht Hash table to search in. |
Michal Vasko | a655fca | 2022-09-05 15:48:31 +0200 | [diff] [blame] | 190 | * @param[in] val_p Pointer to the previously found value in @p ht. |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 191 | * @param[in] hash Hash of the previously found value. |
| 192 | * @param[out] match_p Pointer to the matching value, optional. |
Michal Vasko | da85903 | 2020-07-14 12:20:14 +0200 | [diff] [blame] | 193 | * @return LY_SUCCESS if value was found, |
| 194 | * @return LY_ENOTFOUND if not found. |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 195 | */ |
Michal Vasko | da85903 | 2020-07-14 12:20:14 +0200 | [diff] [blame] | 196 | LY_ERR lyht_find_next(struct hash_table *ht, void *val_p, uint32_t hash, void **match_p); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 197 | |
| 198 | /** |
Michal Vasko | 6374de2 | 2022-09-05 15:48:48 +0200 | [diff] [blame] | 199 | * @brief Find another equal value in the hash table. Same functionality as ::lyht_find_next() |
| 200 | * but allows to specify a collision val equal callback to be used for checking for matching colliding values. |
| 201 | * |
| 202 | * @param[in] ht Hash table to search in. |
| 203 | * @param[in] val_p Pointer to the previously found value in @p ht. |
| 204 | * @param[in] hash Hash of the previously found value. |
| 205 | * @param[in] collision_val_equal Val equal callback to use for checking collisions. |
| 206 | * @param[out] match_p Pointer to the matching value, optional. |
| 207 | * @return LY_SUCCESS if value was found, |
| 208 | * @return LY_ENOTFOUND if not found. |
| 209 | */ |
| 210 | LY_ERR lyht_find_next_with_collision_cb(struct hash_table *ht, void *val_p, uint32_t hash, |
| 211 | lyht_value_equal_cb collision_val_equal, void **match_p); |
| 212 | |
| 213 | /** |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 214 | * @brief Insert a value into a hash table. |
| 215 | * |
| 216 | * @param[in] ht Hash table to insert into. |
| 217 | * @param[in] val_p Pointer to the value to insert. Be careful, if the values stored in the hash table |
Michal Vasko | a655fca | 2022-09-05 15:48:31 +0200 | [diff] [blame] | 218 | * are pointers, @p val_p must be a pointer to a pointer. |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 219 | * @param[in] hash Hash of the stored value. |
| 220 | * @param[out] match_p Pointer to the stored value, optional |
Michal Vasko | 4a4c7ed | 2020-07-17 09:30:12 +0200 | [diff] [blame] | 221 | * @return LY_SUCCESS on success, |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 222 | * @return LY_EEXIST in case the value is already present. |
| 223 | * @return LY_EMEM in case of memory allocation failure. |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 224 | */ |
Radek Krejci | 0ae092d | 2018-09-20 16:43:19 +0200 | [diff] [blame] | 225 | LY_ERR lyht_insert(struct hash_table *ht, void *val_p, uint32_t hash, void **match_p); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 226 | |
| 227 | /** |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame] | 228 | * @brief Insert a value into hash table. Same functionality as ::lyht_insert() |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 229 | * but allows to specify a temporary val equal callback to be used in case the hash table |
| 230 | * will be resized after successful insertion. |
| 231 | * |
| 232 | * @param[in] ht Hash table to insert into. |
| 233 | * @param[in] val_p Pointer to the value to insert. Be careful, if the values stored in the hash table |
Michal Vasko | a655fca | 2022-09-05 15:48:31 +0200 | [diff] [blame] | 234 | * are pointers, @p val_p must be a pointer to a pointer. |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 235 | * @param[in] hash Hash of the stored value. |
| 236 | * @param[in] resize_val_equal Val equal callback to use for resizing. |
| 237 | * @param[out] match_p Pointer to the stored value, optional |
Michal Vasko | 4a4c7ed | 2020-07-17 09:30:12 +0200 | [diff] [blame] | 238 | * @return LY_SUCCESS on success, |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 239 | * @return LY_EEXIST in case the value is already present. |
| 240 | * @return LY_EMEM in case of memory allocation failure. |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 241 | */ |
Michal Vasko | 62524a9 | 2021-02-26 10:08:50 +0100 | [diff] [blame] | 242 | LY_ERR lyht_insert_with_resize_cb(struct hash_table *ht, void *val_p, uint32_t hash, lyht_value_equal_cb resize_val_equal, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 243 | void **match_p); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 244 | |
| 245 | /** |
| 246 | * @brief Remove a value from a hash table. |
| 247 | * |
| 248 | * @param[in] ht Hash table to remove from. |
Michal Vasko | 5bcc33b | 2020-10-06 15:33:44 +0200 | [diff] [blame] | 249 | * @param[in] val_p Pointer to value to be removed. Be careful, if the values stored in the hash table |
Michal Vasko | a655fca | 2022-09-05 15:48:31 +0200 | [diff] [blame] | 250 | * are pointers, @p val_p must be a pointer to a pointer. |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 251 | * @param[in] hash Hash of the stored value. |
Michal Vasko | 4a4c7ed | 2020-07-17 09:30:12 +0200 | [diff] [blame] | 252 | * @return LY_SUCCESS on success, |
| 253 | * @return LY_ENOTFOUND if value was not found. |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 254 | */ |
| 255 | LY_ERR lyht_remove(struct hash_table *ht, void *val_p, uint32_t hash); |
| 256 | |
Michal Vasko | 5bcc33b | 2020-10-06 15:33:44 +0200 | [diff] [blame] | 257 | /** |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame] | 258 | * @brief Remove a value from a hash table. Same functionality as ::lyht_remove() |
Michal Vasko | 5bcc33b | 2020-10-06 15:33:44 +0200 | [diff] [blame] | 259 | * but allows to specify a temporary val equal callback to be used in case the hash table |
| 260 | * will be resized after successful removal. |
| 261 | * |
| 262 | * @param[in] ht Hash table to remove from. |
| 263 | * @param[in] val_p Pointer to value to be removed. Be careful, if the values stored in the hash table |
Michal Vasko | a655fca | 2022-09-05 15:48:31 +0200 | [diff] [blame] | 264 | * are pointers, @p val_p must be a pointer to a pointer. |
Michal Vasko | 5bcc33b | 2020-10-06 15:33:44 +0200 | [diff] [blame] | 265 | * @param[in] hash Hash of the stored value. |
| 266 | * @param[in] resize_val_equal Val equal callback to use for resizing. |
| 267 | * @return LY_SUCCESS on success, |
| 268 | * @return LY_ENOTFOUND if value was not found. |
| 269 | */ |
Michal Vasko | 62524a9 | 2021-02-26 10:08:50 +0100 | [diff] [blame] | 270 | LY_ERR lyht_remove_with_resize_cb(struct hash_table *ht, void *val_p, uint32_t hash, lyht_value_equal_cb resize_val_equal); |
Michal Vasko | 5bcc33b | 2020-10-06 15:33:44 +0200 | [diff] [blame] | 271 | |
Michal Vasko | 626196f | 2022-08-05 12:49:52 +0200 | [diff] [blame] | 272 | /** |
| 273 | * @brief Get suitable size of a hash table for a fixed number of items. |
| 274 | * |
| 275 | * @param[in] item_count Number of stored items. |
| 276 | * @return Hash table size. |
| 277 | */ |
| 278 | uint32_t lyht_get_fixed_size(uint32_t item_count); |
| 279 | |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 280 | #endif /* LY_HASH_TABLE_H_ */ |