Mstdlib-1.24.0
m_hash_u64bin.h
1/* The MIT License (MIT)
2 *
3 * Copyright (c) 2015 Monetra Technologies, LLC.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 * THE SOFTWARE.
22 */
23
24#ifndef __M_HASH_U64BIN_H__
25#define __M_HASH_U64BIN_H__
26
27/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
28
29#include <mstdlib/base/m_defs.h>
30#include <mstdlib/base/m_types.h>
31
32/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
33
34__BEGIN_DECLS
35
36/*! \addtogroup m_hash_u64bin Hashtable - uint64/Binary
37 * \ingroup m_hashtable
38 *
39 * Hashtable, meant for storing uint64 keys and binary data values.
40 *
41 * References to the data will always be read-only.
42 * All keys and values will be duplicated by the hashtable.
43 *
44 * @{
45 */
46
47struct M_hash_u64bin;
48/* Currently a direct map to M_hashtable private opaque type,
49 * simply using casting to prevent the 'wrap' overhead of mallocing when it
50 * is not necessary.
51 */
52typedef struct M_hash_u64bin M_hash_u64bin_t;
53
54struct M_hash_u64bin_enum;
55/* Used for enumerating a M_hash_u64bin. */
56typedef struct M_hash_u64bin_enum M_hash_u64bin_enum_t;
57
58
59/*! Flags for controlling the behavior of the hashtable. */
60typedef enum {
61 M_HASH_U64BIN_NONE = 0, /*!< Case sensitive single value (new values replace). */
62 M_HASH_U64BIN_KEYS_ORDERED = 1 << 0, /*!< Keys should be ordered. Default is insertion order unless the
63 sorted option is specified. */
64 M_HASH_U64BIN_KEYS_SORTASC = 1 << 1, /*!< When the keys are ordered sort them using the key_equality function. */
65 M_HASH_U64BIN_KEYS_SORTDESC = 1 << 2, /*!< When the keys are ordered sort them using the key_equality function. */
66 M_HASH_U64BIN_MULTI_VALUE = 1 << 3, /*!< Allow keys to contain multiple values.
67 Sorted in insertion order another sorting is specified. */
68 M_HASH_U64BIN_MULTI_GETLAST = 1 << 4, /*!< When using get and get_direct function get the last value from the list
69 when allowing multiple values. The default is to get the first value. */
70 M_HASH_U64BIN_STATIC_SEED = 1 << 5 /*!< Use a static seed for hash function initialization. This greatly reduces
71 the security of the hashtable and removes collision attack protections.
72 This should only be used as a performance optimization when creating
73 millions of hashtables with static data specifically for quick look up.
74 DO _NOT_ use this flag with any hashtable that could store user
75 generated data! Be very careful about duplicating a hashtable that
76 was created with this flag. All duplicates will use the static seed. */
78
79
80/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
81
82/*! Create a new hashtable.
83 *
84 * The hashtable will pre-allocate an array of buckets based on the rounded up size specified. Any hash collisions
85 * will result in those collisions being chained together via a linked list. The hashtable will auto-expand by a
86 * power of 2 when the fill percentage specified is reached. All key entries are compared in a case-insensitive
87 * fashion, and are duplicated internally. Values are duplicated. Case is preserved for both keys and values.
88 *
89 * \param[in] size Size of the hash table. If not specified as a power of 2, will
90 * be rounded up to the nearest power of 2.
91 * \param[in] fillpct The maximum fill percentage before the hash table is expanded. If
92 * 0 is specified, the hashtable will never expand, otherwise the
93 * value must be between 1 and 99 (recommended: 75).
94 * \param[in] flags M_hash_u64bin_flags_t flags for modifying behavior.
95 *
96 * \return Allocated hashtable.
97 *
98 * \see M_hash_u64bin_destroy
99 */
100M_API M_hash_u64bin_t *M_hash_u64bin_create(size_t size, M_uint8 fillpct, M_uint32 flags) M_MALLOC_ALIASED;
101
102
103/*! Destroy the hashtable.
104 *
105 * \param[in] h Hashtable to destroy
106 */
107M_API void M_hash_u64bin_destroy(M_hash_u64bin_t *h) M_FREE(1);
108
109
110/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
111
112/*! Insert an entry into the hashtable.
113 *
114 * \param[in,out] h Hashtable being referenced.
115 * \param[in] key Key to insert.
116 * \param[in] value Value to insert. Value will be duplicated. May be NULL.
117 * \param[in] value_len Size of the value being placed into the hash table.
118 *
119 * \return M_TRUE on success, or M_FALSE on failure.
120 */
121M_API M_bool M_hash_u64bin_insert(M_hash_u64bin_t *h, M_uint64 key, const M_uint8 *value, size_t value_len);
122
123
124/*! Remove an entry from the hashtable.
125 *
126 * \param[in,out] h Hashtable being referenced.
127 * \param[in] key Key to remove from the hashtable.
128 *
129 * \return M_TRUE on success, or M_FALSE if key does not exist.
130 */
131M_API M_bool M_hash_u64bin_remove(M_hash_u64bin_t *h, M_uint64 key);
132
133
134/*! Retrieve the value for a key from the hashtable.
135 *
136 * \param[in] h Hashtable being referenced.
137 * \param[in] key Key for value to retrieve from hashtable.
138 * \param[out] value Pointer to value stored in the hashtable. Optional, pass NULL if not needed.
139 * \param[out] value_len Size of the value. Optional, pass NULL if not needed.
140 *
141 * \return M_TRUE if value retrieved, M_FALSE if key does not exist.
142 */
143M_API M_bool M_hash_u64bin_get(const M_hash_u64bin_t *h, M_uint64 key, const M_uint8 **value, size_t *value_len);
144
145/*! Retrieve the value for a key from the hashtable, and return it directly as the return value.
146 *
147 * This cannot be used if you need to differentiate between a key that doesn't exist vs a key with a NULL value.
148 *
149 * \param[in] h Hashtable being referenced.
150 * \param[in] key Key for value to retrieve from the hashtable.
151 * \param[out] value_len Size of the value. Optional, pass NULL if not needed.
152 *
153 * \return NULL if key doesn't exist, otherwise the value.
154 */
155M_API const M_uint8 *M_hash_u64bin_get_direct(const M_hash_u64bin_t *h, M_uint64 key, size_t *value_len);
156
157
158/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
159
160/*! Whether the hashtable is a multi-value table.
161 *
162 * \param[in] h Hashtable being referenced.
163 *
164 * \return M_TRUE if a multi-value hashtable.
165 */
167
168
169/*! Get the number of values for a given key.
170 *
171 * \param[in] h Hashtable being referenced.
172 * \param[in] key Key for value to retrieve.
173 * \param[out] len The number of values.
174 *
175 * \return M_TRUE if length is retrieved, M_FALSE if key does not exist.
176 */
177M_API M_bool M_hash_u64bin_multi_len(const M_hash_u64bin_t *h, M_uint64 key, size_t *len);
178
179
180/*! Retrieve the value for a key from the given index when supporting muli-values.
181 *
182 * \param[in] h Hashtable being referenced.
183 * \param[in] key Key for value to retrieve.
184 * \param[in] idx The index the value resides at.
185 * \param[out] value Pointer to value stored. Optional, pass NULL if not needed.
186 * \param[out] value_len Size of the value. Optional, pass NULL if not needed.
187 *
188 * \return M_TRUE if value retrieved, M_FALSE if key does not exist.
189 */
190M_API M_bool M_hash_u64bin_multi_get(const M_hash_u64bin_t *h, M_uint64 key, size_t idx, const M_uint8 **value, size_t *value_len);
191
192
193/*! Retrieve the value for a key from the given index when supporting muli-values.
194 *
195 * \param[in] h Hashtable being referenced.
196 * \param[in] key Key for value to retrieve.
197 * \param[in] idx The index the value resides at.
198 * \param[out] value_len Size of the value. Optional, pass NULL if not needed.
199 *
200 * \return M_TRUE if value retrieved, M_FALSE if key does not exist.
201 */
202M_API const M_uint8 *M_hash_u64bin_multi_get_direct(const M_hash_u64bin_t *h, M_uint64 key, size_t idx, size_t *value_len);
203
204
205/*! Remove a value from the hashtable when supporting muli-values.
206 *
207 * If all values have been removed then the key will be removed.
208 *
209 * \param[in,out] h Hashtable being referenced.
210 * \param[in] key Key for value to retrieve.
211 * \param[in] idx The index the value resides at.
212 *
213 * \return M_TRUE if the value was removed, M_FALSE if key does not exist.
214 */
215M_API M_bool M_hash_u64bin_multi_remove(M_hash_u64bin_t *h, M_uint64 key, size_t idx);
216
217
218/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
219
220/*! Retrieve the current size (number of buckets/slots, not necessarily used).
221 *
222 * \param[in] h Hashtable being referenced.
223 *
224 * \return Size of the hashtable.
225 */
226M_API M_uint32 M_hash_u64bin_size(const M_hash_u64bin_t *h);
227
228
229/*! Retrieve the number of collisions for hashtable entries that has occurred since creation.
230 *
231 * \param[in] h Hashtable being referenced.
232 *
233 * \return Number of collisions.
234 */
236
237
238/*! Retrieve the number of expansions/rehashes since creation.
239 *
240 * \param[in] h Hashtable being referenced.
241 *
242 * \return number of expansions/rehashes.
243 */
245
246
247/*! Retrieve the number of entries in the hashtable.
248 *
249 * This is the number of keys stored.
250 *
251 * \param[in] h Hashtable being referenced.
252 *
253 * \return number of entries in the hashtable.
254 */
256
257
258/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
259
260/*! Start an enumeration of the keys within the hashtable.
261 *
262 * \param[in] h Hashtable being referenced.
263 * \param[out] hashenum Outputs an initialized state variable for starting an enumeration.
264 *
265 * \return Number of values in the hashtable.
266 *
267 * \see M_hash_u64bin_enumerate_free
268 */
270
271
272/*! Retrieve the next item from a hashtable enumeration.
273 *
274 * If multi-value, keys will appear multiple times as each value will be
275 * retrieved individually.
276 *
277 * \param[in] h Hashtable being referenced.
278 * \param[in,out] hashenum State variable for tracking the enumeration process.
279 * \param[out] key Value of next enumerated key. Optional, may be NULL
280 * \param[out] value Value of next enumerated value. Optional, may be NULL
281 * \param[out] value_len Size of next enumerated value.
282 *
283 * \return M_TRUE if enumeration succeeded, M_FALSE if no more keys
284 */
285M_API M_bool M_hash_u64bin_enumerate_next(const M_hash_u64bin_t *h, M_hash_u64bin_enum_t *hashenum, M_uint64 *key, const M_uint8 **value, size_t *value_len);
286
287
288/*! Destroy an enumeration state.
289 *
290 * \param[in] hashenum Enumeration to destroy.
291 */
293
294
295/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
296
297/*! Merge two hashtables together.
298 *
299 * The second (src) hashtable will be destroyed automatically upon completion of this function. Any key/value
300 * pointers for the hashtable will be directly copied over to the destination hashtable, they will not be
301 * duplicated. Any keys which exist in 'dest' that also exist in 'src' will be overwritten by the 'src' value.
302 *
303 * If dest and src are multi-value, all values from src will be copied into dest and the values from
304 * dest will not be removed. If dest is not multi-value and src is, then only the last value in src will
305 * be present in dest. If dest is multi-value and src is not, then the value from src will be added to dest.
306 *
307 * \param[in,out] dest Pointer by reference to the hashtable receiving the key/value pairs.
308 * if dest is NULL, the src address will simply be copied to dest.
309 * \param[in,out] src Pointer to the hashtable giving up its key/value pairs.
310 */
311M_API void M_hash_u64bin_merge(M_hash_u64bin_t **dest, M_hash_u64bin_t *src) M_FREE(2);
312
313/*! Duplicate an existing hashtable.
314 *
315 * Copying all keys and values.
316 *
317 * \param[in] h Hashtable to be copied.
318 *
319 * \return Duplicated hashtable.
320 */
322
323/*! @} */
324
325__END_DECLS
326
327#endif /* __M_HASH_U64BIN_H__ */
M_bool M_hash_u64bin_multi_remove(M_hash_u64bin_t *h, M_uint64 key, size_t idx)
M_bool M_hash_u64bin_enumerate_next(const M_hash_u64bin_t *h, M_hash_u64bin_enum_t *hashenum, M_uint64 *key, const M_uint8 **value, size_t *value_len)
const M_uint8 * M_hash_u64bin_get_direct(const M_hash_u64bin_t *h, M_uint64 key, size_t *value_len)
M_bool M_hash_u64bin_remove(M_hash_u64bin_t *h, M_uint64 key)
void M_hash_u64bin_enumerate_free(M_hash_u64bin_enum_t *hashenum)
M_bool M_hash_u64bin_get(const M_hash_u64bin_t *h, M_uint64 key, const M_uint8 **value, size_t *value_len)
struct M_hash_u64bin M_hash_u64bin_t
Definition: m_hash_u64bin.h:52
size_t M_hash_u64bin_num_expansions(const M_hash_u64bin_t *h)
size_t M_hash_u64bin_enumerate(const M_hash_u64bin_t *h, M_hash_u64bin_enum_t **hashenum)
size_t M_hash_u64bin_num_collisions(const M_hash_u64bin_t *h)
M_bool M_hash_u64bin_insert(M_hash_u64bin_t *h, M_uint64 key, const M_uint8 *value, size_t value_len)
void M_hash_u64bin_merge(M_hash_u64bin_t **dest, M_hash_u64bin_t *src) M_FREE(2)
struct M_hash_u64bin_enum M_hash_u64bin_enum_t
Definition: m_hash_u64bin.h:56
M_bool M_hash_u64bin_multi_len(const M_hash_u64bin_t *h, M_uint64 key, size_t *len)
M_bool M_hash_u64bin_is_multi(const M_hash_u64bin_t *h)
M_uint32 M_hash_u64bin_size(const M_hash_u64bin_t *h)
M_hash_u64bin_t * M_hash_u64bin_create(size_t size, M_uint8 fillpct, M_uint32 flags) M_MALLOC_ALIASED
M_hash_u64bin_t * M_hash_u64bin_duplicate(const M_hash_u64bin_t *h) M_MALLOC
size_t M_hash_u64bin_num_keys(const M_hash_u64bin_t *h)
M_hash_u64bin_flags_t
Definition: m_hash_u64bin.h:60
void M_hash_u64bin_destroy(M_hash_u64bin_t *h) M_FREE(1)
M_bool M_hash_u64bin_multi_get(const M_hash_u64bin_t *h, M_uint64 key, size_t idx, const M_uint8 **value, size_t *value_len)
const M_uint8 * M_hash_u64bin_multi_get_direct(const M_hash_u64bin_t *h, M_uint64 key, size_t idx, size_t *value_len)
@ M_HASH_U64BIN_MULTI_GETLAST
Definition: m_hash_u64bin.h:68
@ M_HASH_U64BIN_KEYS_ORDERED
Definition: m_hash_u64bin.h:62
@ M_HASH_U64BIN_MULTI_VALUE
Definition: m_hash_u64bin.h:66
@ M_HASH_U64BIN_NONE
Definition: m_hash_u64bin.h:61
@ M_HASH_U64BIN_KEYS_SORTDESC
Definition: m_hash_u64bin.h:65
@ M_HASH_U64BIN_KEYS_SORTASC
Definition: m_hash_u64bin.h:64
@ M_HASH_U64BIN_STATIC_SEED
Definition: m_hash_u64bin.h:70