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