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 | 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 | |
| 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 <stddef.h> |
| 20 | #include <stdint.h> |
| 21 | |
Michal Vasko | ae130f5 | 2023-04-20 14:25:16 +0200 | [diff] [blame] | 22 | #ifdef __cplusplus |
| 23 | extern "C" { |
| 24 | #endif |
| 25 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 26 | #include "log.h" |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 27 | |
| 28 | /** |
Michal Vasko | b2c2680 | 2023-05-24 14:57:03 +0200 | [diff] [blame] | 29 | * @struct ly_ht |
| 30 | * @brief libyang hash table. |
| 31 | */ |
| 32 | struct ly_ht; |
| 33 | |
| 34 | /** |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 35 | * @brief Compute hash from (several) string(s). |
| 36 | * |
| 37 | * Usage: |
| 38 | * - init hash to 0 |
Michal Vasko | ae130f5 | 2023-04-20 14:25:16 +0200 | [diff] [blame] | 39 | * - repeatedly call ::lyht_hash_multi(), provide hash from the last call |
| 40 | * - call ::lyht_hash_multi() with key_part = NULL to finish the hash |
| 41 | * |
| 42 | * @param[in] hash Previous hash. |
| 43 | * @param[in] key_part Next key to hash, |
| 44 | * @param[in] len Length of @p key_part. |
| 45 | * @return Hash with the next key. |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 46 | */ |
Michal Vasko | ae130f5 | 2023-04-20 14:25:16 +0200 | [diff] [blame] | 47 | LIBYANG_API_DECL uint32_t 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] | 48 | |
Michal Vasko | a655fca | 2022-09-05 15:48:31 +0200 | [diff] [blame] | 49 | /** |
Radek Krejci | f2dc4c5 | 2018-11-08 09:04:13 +0100 | [diff] [blame] | 50 | * @brief Compute hash from a string. |
Michal Vasko | ae130f5 | 2023-04-20 14:25:16 +0200 | [diff] [blame] | 51 | * |
| 52 | * Bob Jenkin's one-at-a-time hash |
| 53 | * http://www.burtleburtle.net/bob/hash/doobs.html |
| 54 | * |
| 55 | * Spooky hash is faster, but it works only for little endian architectures. |
| 56 | * |
| 57 | * @param[in] key Key to hash. |
| 58 | * @param[in] len Length of @p key. |
| 59 | * @return Hash of the key. |
Radek Krejci | f2dc4c5 | 2018-11-08 09:04:13 +0100 | [diff] [blame] | 60 | */ |
Michal Vasko | ae130f5 | 2023-04-20 14:25:16 +0200 | [diff] [blame] | 61 | LIBYANG_API_DECL uint32_t lyht_hash(const char *key, size_t len); |
Radek Krejci | f2dc4c5 | 2018-11-08 09:04:13 +0100 | [diff] [blame] | 62 | |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 63 | /** |
| 64 | * @brief Callback for checking hash table values equivalence. |
| 65 | * |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 66 | * @param[in] val1_p Pointer to the first value, the one being searched (inserted/removed). |
| 67 | * @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] | 68 | * @param[in] mod Whether the operation modifies the hash table (insert or remove) or not (find). |
| 69 | * @param[in] cb_data User callback data. |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 70 | * @return false (non-equal) or true (equal). |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 71 | */ |
Michal Vasko | 62524a9 | 2021-02-26 10:08:50 +0100 | [diff] [blame] | 72 | 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] | 73 | |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 74 | /** |
| 75 | * @brief Create new hash table. |
| 76 | * |
| 77 | * @param[in] size Starting size of the hash table (capacity of values), must be power of 2. |
| 78 | * @param[in] val_size Size in bytes of value (the stored hashed item). |
| 79 | * @param[in] val_equal Callback for checking value equivalence. |
Michal Vasko | a655fca | 2022-09-05 15:48:31 +0200 | [diff] [blame] | 80 | * @param[in] cb_data User data always passed to @p val_equal. |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 81 | * @param[in] resize Whether to resize the table on too few/too many records taken. |
| 82 | * @return Empty hash table, NULL on error. |
| 83 | */ |
Michal Vasko | ae130f5 | 2023-04-20 14:25:16 +0200 | [diff] [blame] | 84 | LIBYANG_API_DECL struct ly_ht *lyht_new(uint32_t size, uint16_t val_size, lyht_value_equal_cb val_equal, void *cb_data, |
| 85 | uint16_t resize); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 86 | |
| 87 | /** |
| 88 | * @brief Set hash table value equal callback. |
| 89 | * |
| 90 | * @param[in] ht Hash table to modify. |
| 91 | * @param[in] new_val_equal New callback for checking value equivalence. |
| 92 | * @return Previous callback for checking value equivalence. |
| 93 | */ |
Michal Vasko | ae130f5 | 2023-04-20 14:25:16 +0200 | [diff] [blame] | 94 | LIBYANG_API_DECL lyht_value_equal_cb 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] | 95 | |
| 96 | /** |
| 97 | * @brief Set hash table value equal callback user data. |
| 98 | * |
| 99 | * @param[in] ht Hash table to modify. |
| 100 | * @param[in] new_cb_data New data for values callback. |
| 101 | * @return Previous data for values callback. |
| 102 | */ |
Michal Vasko | ae130f5 | 2023-04-20 14:25:16 +0200 | [diff] [blame] | 103 | LIBYANG_API_DECL void *lyht_set_cb_data(struct ly_ht *ht, void *new_cb_data); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 104 | |
| 105 | /** |
| 106 | * @brief Make a duplicate of an existing hash table. |
| 107 | * |
| 108 | * @param[in] orig Original hash table to duplicate. |
Michal Vasko | a655fca | 2022-09-05 15:48:31 +0200 | [diff] [blame] | 109 | * @return Duplicated hash table @p orig, NULL on error. |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 110 | */ |
Michal Vasko | ae130f5 | 2023-04-20 14:25:16 +0200 | [diff] [blame] | 111 | LIBYANG_API_DECL struct ly_ht *lyht_dup(const struct ly_ht *orig); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 112 | |
| 113 | /** |
| 114 | * @brief Free a hash table. |
| 115 | * |
| 116 | * @param[in] ht Hash table to be freed. |
Michal Vasko | 88ccd58 | 2023-03-30 11:50:57 +0200 | [diff] [blame] | 117 | * @param[in] val_free Optional callback for freeing all the stored values, @p val_p is a pointer to a stored value. |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 118 | */ |
Michal Vasko | ae130f5 | 2023-04-20 14:25:16 +0200 | [diff] [blame] | 119 | LIBYANG_API_DECL void lyht_free(struct ly_ht *ht, void (*val_free)(void *val_p)); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 120 | |
| 121 | /** |
| 122 | * @brief Find a value in a hash table. |
| 123 | * |
| 124 | * @param[in] ht Hash table to search in. |
| 125 | * @param[in] val_p Pointer to the value to find. |
| 126 | * @param[in] hash Hash of the stored value. |
| 127 | * @param[out] match_p Pointer to the matching value, optional. |
Michal Vasko | da85903 | 2020-07-14 12:20:14 +0200 | [diff] [blame] | 128 | * @return LY_SUCCESS if value was found, |
| 129 | * @return LY_ENOTFOUND if not found. |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 130 | */ |
Michal Vasko | f1e0941 | 2023-10-23 10:03:21 +0200 | [diff] [blame] | 131 | LIBYANG_API_DECL LY_ERR lyht_find(const struct ly_ht *ht, void *val_p, uint32_t hash, void **match_p); |
| 132 | |
| 133 | /** |
| 134 | * @brief Find a value in a hash table but use a custom val_equal callback. |
| 135 | * |
| 136 | * @param[in] ht Hash table to search in. |
| 137 | * @param[in] val_p Pointer to the value to find. |
| 138 | * @param[in] hash Hash of the stored value. |
| 139 | * @param[in] val_equal Callback for checking value equivalence. |
| 140 | * @param[out] match_p Pointer to the matching value, optional. |
| 141 | * @return LY_SUCCESS if value was found, |
| 142 | * @return LY_ENOTFOUND if not found. |
| 143 | */ |
| 144 | LIBYANG_API_DECL LY_ERR lyht_find_with_val_cb(const struct ly_ht *ht, void *val_p, uint32_t hash, |
| 145 | lyht_value_equal_cb val_equal, void **match_p); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 146 | |
| 147 | /** |
| 148 | * @brief Find another equal value in the hash table. |
| 149 | * |
| 150 | * @param[in] ht Hash table to search in. |
Michal Vasko | a655fca | 2022-09-05 15:48:31 +0200 | [diff] [blame] | 151 | * @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] | 152 | * @param[in] hash Hash of the previously found value. |
| 153 | * @param[out] match_p Pointer to the matching value, optional. |
Michal Vasko | da85903 | 2020-07-14 12:20:14 +0200 | [diff] [blame] | 154 | * @return LY_SUCCESS if value was found, |
| 155 | * @return LY_ENOTFOUND if not found. |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 156 | */ |
Michal Vasko | f1e0941 | 2023-10-23 10:03:21 +0200 | [diff] [blame] | 157 | LIBYANG_API_DECL LY_ERR lyht_find_next(const 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] | 158 | |
| 159 | /** |
Michal Vasko | 6374de2 | 2022-09-05 15:48:48 +0200 | [diff] [blame] | 160 | * @brief Find another equal value in the hash table. Same functionality as ::lyht_find_next() |
| 161 | * but allows to specify a collision val equal callback to be used for checking for matching colliding values. |
| 162 | * |
| 163 | * @param[in] ht Hash table to search in. |
| 164 | * @param[in] val_p Pointer to the previously found value in @p ht. |
| 165 | * @param[in] hash Hash of the previously found value. |
| 166 | * @param[in] collision_val_equal Val equal callback to use for checking collisions. |
| 167 | * @param[out] match_p Pointer to the matching value, optional. |
| 168 | * @return LY_SUCCESS if value was found, |
| 169 | * @return LY_ENOTFOUND if not found. |
| 170 | */ |
Michal Vasko | f1e0941 | 2023-10-23 10:03:21 +0200 | [diff] [blame] | 171 | LIBYANG_API_DECL LY_ERR 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] | 172 | lyht_value_equal_cb collision_val_equal, void **match_p); |
| 173 | |
| 174 | /** |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 175 | * @brief Insert a value into a hash table. |
| 176 | * |
| 177 | * @param[in] ht Hash table to insert into. |
| 178 | * @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] | 179 | * are pointers, @p val_p must be a pointer to a pointer. |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 180 | * @param[in] hash Hash of the stored value. |
| 181 | * @param[out] match_p Pointer to the stored value, optional |
Michal Vasko | 4a4c7ed | 2020-07-17 09:30:12 +0200 | [diff] [blame] | 182 | * @return LY_SUCCESS on success, |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 183 | * @return LY_EEXIST in case the value is already present. |
| 184 | * @return LY_EMEM in case of memory allocation failure. |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 185 | */ |
Michal Vasko | ae130f5 | 2023-04-20 14:25:16 +0200 | [diff] [blame] | 186 | LIBYANG_API_DECL LY_ERR 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] | 187 | |
| 188 | /** |
Olivier Matz | 6ad93d9 | 2023-09-28 12:13:36 +0200 | [diff] [blame] | 189 | * @brief Insert a value into a hash table, without checking whether the value has already been inserted. |
| 190 | * |
| 191 | * @param[in] ht Hash table to insert into. |
| 192 | * @param[in] val_p Pointer to the value to insert. Be careful, if the values stored in the hash table |
| 193 | * are pointers, @p val_p must be a pointer to a pointer. |
| 194 | * @param[in] hash Hash of the stored value. |
| 195 | * @param[out] match_p Pointer to the stored value, optional |
| 196 | * @return LY_SUCCESS on success, |
| 197 | * @return LY_EMEM in case of memory allocation failure. |
| 198 | */ |
| 199 | LIBYANG_API_DECL LY_ERR lyht_insert_no_check(struct ly_ht *ht, void *val_p, uint32_t hash, void **match_p); |
| 200 | |
| 201 | /** |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame] | 202 | * @brief Insert a value into hash table. Same functionality as ::lyht_insert() |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 203 | * but allows to specify a temporary val equal callback to be used in case the hash table |
| 204 | * will be resized after successful insertion. |
| 205 | * |
| 206 | * @param[in] ht Hash table to insert into. |
| 207 | * @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] | 208 | * are pointers, @p val_p must be a pointer to a pointer. |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 209 | * @param[in] hash Hash of the stored value. |
| 210 | * @param[in] resize_val_equal Val equal callback to use for resizing. |
| 211 | * @param[out] match_p Pointer to the stored value, optional |
Michal Vasko | 4a4c7ed | 2020-07-17 09:30:12 +0200 | [diff] [blame] | 212 | * @return LY_SUCCESS on success, |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 213 | * @return LY_EEXIST in case the value is already present. |
| 214 | * @return LY_EMEM in case of memory allocation failure. |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 215 | */ |
Michal Vasko | ae130f5 | 2023-04-20 14:25:16 +0200 | [diff] [blame] | 216 | LIBYANG_API_DECL LY_ERR lyht_insert_with_resize_cb(struct ly_ht *ht, void *val_p, uint32_t hash, |
| 217 | lyht_value_equal_cb resize_val_equal, void **match_p); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 218 | |
| 219 | /** |
| 220 | * @brief Remove a value from a hash table. |
| 221 | * |
| 222 | * @param[in] ht Hash table to remove from. |
Michal Vasko | 5bcc33b | 2020-10-06 15:33:44 +0200 | [diff] [blame] | 223 | * @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] | 224 | * are pointers, @p val_p must be a pointer to a pointer. |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 225 | * @param[in] hash Hash of the stored value. |
Michal Vasko | 4a4c7ed | 2020-07-17 09:30:12 +0200 | [diff] [blame] | 226 | * @return LY_SUCCESS on success, |
| 227 | * @return LY_ENOTFOUND if value was not found. |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 228 | */ |
Michal Vasko | ae130f5 | 2023-04-20 14:25:16 +0200 | [diff] [blame] | 229 | LIBYANG_API_DECL LY_ERR lyht_remove(struct ly_ht *ht, void *val_p, uint32_t hash); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 230 | |
Michal Vasko | 5bcc33b | 2020-10-06 15:33:44 +0200 | [diff] [blame] | 231 | /** |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame] | 232 | * @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] | 233 | * but allows to specify a temporary val equal callback to be used in case the hash table |
| 234 | * will be resized after successful removal. |
| 235 | * |
| 236 | * @param[in] ht Hash table to remove from. |
| 237 | * @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] | 238 | * are pointers, @p val_p must be a pointer to a pointer. |
Michal Vasko | 5bcc33b | 2020-10-06 15:33:44 +0200 | [diff] [blame] | 239 | * @param[in] hash Hash of the stored value. |
| 240 | * @param[in] resize_val_equal Val equal callback to use for resizing. |
| 241 | * @return LY_SUCCESS on success, |
| 242 | * @return LY_ENOTFOUND if value was not found. |
| 243 | */ |
Michal Vasko | ae130f5 | 2023-04-20 14:25:16 +0200 | [diff] [blame] | 244 | LIBYANG_API_DECL LY_ERR lyht_remove_with_resize_cb(struct ly_ht *ht, void *val_p, uint32_t hash, |
| 245 | lyht_value_equal_cb resize_val_equal); |
Michal Vasko | 5bcc33b | 2020-10-06 15:33:44 +0200 | [diff] [blame] | 246 | |
Michal Vasko | 626196f | 2022-08-05 12:49:52 +0200 | [diff] [blame] | 247 | /** |
| 248 | * @brief Get suitable size of a hash table for a fixed number of items. |
| 249 | * |
| 250 | * @param[in] item_count Number of stored items. |
| 251 | * @return Hash table size. |
| 252 | */ |
Michal Vasko | ae130f5 | 2023-04-20 14:25:16 +0200 | [diff] [blame] | 253 | LIBYANG_API_DECL uint32_t lyht_get_fixed_size(uint32_t item_count); |
| 254 | |
| 255 | #ifdef __cplusplus |
| 256 | } |
| 257 | #endif |
Michal Vasko | 626196f | 2022-08-05 12:49:52 +0200 | [diff] [blame] | 258 | |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 259 | #endif /* LY_HASH_TABLE_H_ */ |