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