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