blob: ac42631cd5eb9d71be8144920e702779021cf5a8 [file] [log] [blame]
Radek Krejci5aeea3a2018-09-05 13:29:36 +02001/**
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 *
7 * Copyright (c) 2015 - 2018 CESNET, z.s.p.o.
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 Krejci5aeea3a2018-09-05 13:29:36 +020019#include "common.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020020
21#include <pthread.h>
22#include <stddef.h>
23#include <stdint.h>
24
25#include "log.h"
Radek Krejci5aeea3a2018-09-05 13:29:36 +020026
27/**
28 * @brief Compute hash from (several) string(s).
29 *
30 * Usage:
31 * - init hash to 0
32 * - repeatedly call dict_hash_multi(), provide hash from the last call
33 * - call dict_hash_multi() with key_part = NULL to finish the hash
34 */
35uint32_t dict_hash_multi(uint32_t hash, const char *key_part, size_t len);
36
Radek Krejcif2dc4c52018-11-08 09:04:13 +010037/*
38 * @brief Compute hash from a string.
39 */
40uint32_t dict_hash(const char *key, size_t len);
41
Radek Krejci5aeea3a2018-09-05 13:29:36 +020042/**
43 * @brief Callback for checking hash table values equivalence.
44 *
45 * @param[in] val1_p Pointer to the first value.
46 * @param[in] val2_p Pointer to the second value.
47 * @param[in] mod Whether the operation modifies the hash table (insert or remove) or not (find).
48 * @param[in] cb_data User callback data.
49 * @return 0 on non-equal, non-zero on equal.
50 */
51typedef int (*values_equal_cb)(void *val1_p, void *val2_p, int mod, void *cb_data);
52
53/** when the table is at least this much percent full, it is enlarged (double the size) */
54#define LYHT_ENLARGE_PERCENTAGE 75
55
56/** only once the table is this much percent full, enable shrinking */
57#define LYHT_FIRST_SHRINK_PERCENTAGE 50
58
59/** when the table is less than this much percent full, it is shrunk (half the size) */
60#define LYHT_SHRINK_PERCENTAGE 25
61
62/** never shrink beyond this size */
63#define LYHT_MIN_SIZE 8
64
65/**
66 * @brief Generic hash table record.
67 */
68struct ht_rec {
69 uint32_t hash; /* hash of the value */
70 int32_t hits; /* collision/overflow value count - 1 (a filled entry has 1 hit,
71 * special value -1 means a deleted record) */
72 unsigned char val[1]; /* arbitrary-size value */
73} _PACKED;
74
75/**
76 * @brief (Very) generic hash table.
77 *
78 * Hash table with open addressing collision resolution and
79 * linear probing of interval 1 (next free record is used).
80 * Removal is lazy (removed records are only marked), but
81 * if possible, they are fully emptied.
82 */
83struct hash_table {
84 uint32_t used; /* number of values stored in the hash table (filled records) */
85 uint32_t size; /* always holds 2^x == size (is power of 2), actually number of records allocated */
86 values_equal_cb val_equal; /* callback for testing value equivalence */
87 void *cb_data; /* user data callback arbitrary value */
88 uint16_t resize; /* 0 - resizing is disabled, *
89 * 1 - enlarging is enabled, *
90 * 2 - both shrinking and enlarging is enabled */
91 uint16_t rec_size; /* real size (in bytes) of one record for accessing recs array */
92 unsigned char *recs; /* pointer to the hash table itself (array of struct ht_rec) */
93};
94
95struct dict_rec {
96 char *value;
97 uint32_t refcount;
98};
99
100/**
101 * dictionary to store repeating strings
102 */
103struct dict_table {
104 struct hash_table *hash_tab;
105 pthread_mutex_t lock;
106};
107
108/**
109 * @brief Initiate content (non-zero values) of the dictionary
110 *
111 * @param[in] dict Dictionary table to initiate
112 */
113void lydict_init(struct dict_table *dict);
114
115/**
116 * @brief Cleanup the dictionary content
117 *
118 * @param[in] dict Dictionary table to cleanup
119 */
120void lydict_clean(struct dict_table *dict);
121
122/**
123 * @brief Create new hash table.
124 *
125 * @param[in] size Starting size of the hash table (capacity of values), must be power of 2.
126 * @param[in] val_size Size in bytes of value (the stored hashed item).
127 * @param[in] val_equal Callback for checking value equivalence.
128 * @param[in] cb_data User data always passed to \p val_equal.
129 * @param[in] resize Whether to resize the table on too few/too many records taken.
130 * @return Empty hash table, NULL on error.
131 */
132struct hash_table *lyht_new(uint32_t size, uint16_t val_size, values_equal_cb val_equal, void *cb_data, int resize);
133
134/**
135 * @brief Set hash table value equal callback.
136 *
137 * @param[in] ht Hash table to modify.
138 * @param[in] new_val_equal New callback for checking value equivalence.
139 * @return Previous callback for checking value equivalence.
140 */
141values_equal_cb lyht_set_cb(struct hash_table *ht, values_equal_cb new_val_equal);
142
143/**
144 * @brief Set hash table value equal callback user data.
145 *
146 * @param[in] ht Hash table to modify.
147 * @param[in] new_cb_data New data for values callback.
148 * @return Previous data for values callback.
149 */
150void *lyht_set_cb_data(struct hash_table *ht, void *new_cb_data);
151
152/**
153 * @brief Make a duplicate of an existing hash table.
154 *
155 * @param[in] orig Original hash table to duplicate.
156 * @return Duplicated hash table \p orig, NULL on error.
157 */
158struct hash_table *lyht_dup(const struct hash_table *orig);
159
160/**
161 * @brief Free a hash table.
162 *
163 * @param[in] ht Hash table to be freed.
164 */
165void lyht_free(struct hash_table *ht);
166
167/**
168 * @brief Find a value in a hash table.
169 *
170 * @param[in] ht Hash table to search in.
171 * @param[in] val_p Pointer to the value to find.
172 * @param[in] hash Hash of the stored value.
173 * @param[out] match_p Pointer to the matching value, optional.
174 * @return 0 on success, 1 on not found.
175 */
176int lyht_find(struct hash_table *ht, void *val_p, uint32_t hash, void **match_p);
177
178/**
179 * @brief Find another equal value in the hash table.
180 *
181 * @param[in] ht Hash table to search in.
182 * @param[in] val_p Pointer to the previously found value in \p ht.
183 * @param[in] hash Hash of the previously found value.
184 * @param[out] match_p Pointer to the matching value, optional.
185 * @return 0 on success, 1 on not found.
186 */
187int lyht_find_next(struct hash_table *ht, void *val_p, uint32_t hash, void **match_p);
188
189/**
190 * @brief Insert a value into a hash table.
191 *
192 * @param[in] ht Hash table to insert into.
193 * @param[in] val_p Pointer to the value to insert. Be careful, if the values stored in the hash table
194 * are pointers, \p val_p must be a pointer to a pointer.
195 * @param[in] hash Hash of the stored value.
196 * @param[out] match_p Pointer to the stored value, optional
Radek Krejci0ae092d2018-09-20 16:43:19 +0200197 * @return LY_ERR value (LY_EEXIST if the value is already present).
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200198 */
Radek Krejci0ae092d2018-09-20 16:43:19 +0200199LY_ERR lyht_insert(struct hash_table *ht, void *val_p, uint32_t hash, void **match_p);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200200
201/**
202 * @brief Insert a value into hash table. Same functionality as lyht_insert()
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
208 * are pointers, \p val_p must be a pointer to a pointer.
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
212 * @return LY_ERR return value (LY_EEXIST if the value is already present).
213 */
214LY_ERR lyht_insert_with_resize_cb(struct hash_table *ht, void *val_p, uint32_t hash, values_equal_cb resize_val_equal,
215 void **match_p);
216
217/**
218 * @brief Remove a value from a hash table.
219 *
220 * @param[in] ht Hash table to remove from.
221 * @param[in] value_p Pointer to value to be removed. Be careful, if the values stored in the hash table
222 * are pointers, \p value_p must be a pointer to a pointer.
223 * @param[in] hash Hash of the stored value.
224 * @return LY_ERR return value (LY_EINVAL if value was not found).
225 */
226LY_ERR lyht_remove(struct hash_table *ht, void *val_p, uint32_t hash);
227
228#endif /* LY_HASH_TABLE_H_ */