Mstdlib-1.24.0
m_hash_stru64.h
1/* The MIT License (MIT)
2 *
3 * Copyright (c) 2017 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_STRU64_H__
25#define __M_HASH_STRU64_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_stru64 Hashtable - String/uint64
37 * \ingroup m_hashtable
38 *
39 * Hashtable, meant for storing string keys and uint64 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_stru64;
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 */
51typedef struct M_hash_stru64 M_hash_stru64_t;
52
53struct M_hash_stru64_enum;
54/* Used for enumerating a M_hash_stru64. */
55typedef struct M_hash_stru64_enum M_hash_stru64_enum_t;
56
57
58/*! Flags for controlling the behavior of the hashtable. */
59typedef enum {
60 M_HASH_STRU64_NONE = 0, /*!< Case sensitive single value (new values replace). */
61 M_HASH_STRU64_CASECMP = 1 << 0, /*!< Key compare is case insensitive. */
62 M_HASH_STRU64_KEYS_UPPER = 1 << 1, /*!< Keys will be upper cased before being inserted. Should be used
63 in conjunction with M_HASH_STRU64_CASECMP. */
64 M_HASH_STRU64_KEYS_LOWER = 1 << 2, /*!< Keys will be lower cased before being inserted. Should be used
65 in conjunction with M_HASH_STRU64_CASECMP. */
66 M_HASH_STRU64_KEYS_ORDERED = 1 << 3, /*!< Keys should be ordered. Default is insertion order unless the
67 sorted option is specified. */
68 M_HASH_STRU64_KEYS_SORTASC = 1 << 4, /*!< When the keys are ordered sort them using the key_equality function. */
69 M_HASH_STRU64_KEYS_SORTDESC = 1 << 5, /*!< When the keys are ordered sort them using the key_equality function. */
70 M_HASH_STRU64_MULTI_VALUE = 1 << 6, /*!< Allow keys to contain multiple values.
71 Sorted in insertion order another sorting is specified. */
72 M_HASH_STRU64_MULTI_GETLAST = 1 << 7, /*!< When using get and get_direct function get the last value from the list
73 when allowing multiple values. The default is to get the first value. */
74 M_HASH_STRU64_STATIC_SEED = 1 << 8 /*!< Use a static seed for hash function initialization. This greatly reduces
75 the security of the hashtable and removes collision attack protections.
76 This should only be used as a performance optimization when creating
77 millions of hashtables with static data specifically for quick look up.
78 DO _NOT_ use this flag with any hashtable that could store user
79 generated data! Be very careful about duplicating a hashtable that
80 was created with this flag. All duplicates will use the static seed. */
82
83
84/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
85
86/*! Create a new hashtable.
87 *
88 * The hashtable will pre-allocate an array of buckets based on the rounded up size specified. Any hash collisions
89 * will result in those collisions being chained together via a linked list. The hashtable will auto-expand by a
90 * power of 2 when the fill percentage specified is reached. All key entries are compared in a case-insensitive
91 * fashion, and are duplicated internally. Values are duplicated. Case is preserved for both keys and values.
92 *
93 * \param[in] size Size of the hash table. If not specified as a power of 2, will
94 * be rounded up to the nearest power of 2.
95 * \param[in] fillpct The maximum fill percentage before the hash table is expanded. If
96 * 0 is specified, the hashtable will never expand, otherwise the
97 * value must be between 1 and 99 (recommended: 75).
98 * \param[in] flags M_hash_stru64_flags_t flags for modifying behavior.
99 *
100 * \return Allocated hashtable.
101 *
102 * \see M_hash_stru64_destroy
103 */
104M_API M_hash_stru64_t *M_hash_stru64_create(size_t size, M_uint8 fillpct, M_uint32 flags) M_MALLOC;
105
106
107/*! Destroy the hashtable.
108 *
109 * \param[in] h Hashtable to destroy
110 */
111M_API void M_hash_stru64_destroy(M_hash_stru64_t *h) M_FREE(1);
112
113
114/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
115
116/*! Insert an entry into the hashtable.
117 *
118 * \param[in,out] h Hashtable being referenced.
119 * \param[in] key Key to insert.
120 * A NULL or empty string is explicity disallowed.
121 * \param[in] value Value to insert into hashtable.
122 *
123 * \return M_TRUE on success, or M_FALSE on failure.
124 */
125M_API M_bool M_hash_stru64_insert(M_hash_stru64_t *h, const char *key, M_uint64 value);
126
127
128/*! Remove an entry from the hashtable.
129 *
130 * \param[in,out] h Hashtable being referenced.
131 * \param[in] key Key to remove from the hashtable.
132 * A NULL or empty string is explicitly disallowed.
133 *
134 * \return M_TRUE on success, or M_FALSE if key does not exist.
135 */
136M_API M_bool M_hash_stru64_remove(M_hash_stru64_t *h, const char *key);
137
138
139/*! Retrieve the value for a key from the hashtable.
140 *
141 * \param[in] h Hashtable being referenced.
142 * \param[in] key Key for value.
143 * A NULL or empty string is explicitly disallowed.
144 * \param[out] value Pointer to value stored in the hashtable. Optional, pass NULL if not needed.
145 *
146 * \return M_TRUE if value retrieved, M_FALSE if key does not exist.
147 */
148M_API M_bool M_hash_stru64_get(const M_hash_stru64_t *h, const char *key, M_uint64 *value);
149
150
151/*! Retrieve the value for a key from the hashtable, and return it directly as the return value.
152 *
153 * This cannot be used if you need to differentiate between a key that doesn't exist vs a key with a 0 value.
154 *
155 * \param[in] h Hashtable being referenced.
156 * \param[in] key Key for value to retrieve from the hashtable.
157 * A NULL or empty string is explicitly disallowed.
158 *
159 * \return NULL if key doesn't exist or NULL value on file, otherwise the value.
160 */
161M_API M_uint64 M_hash_stru64_get_direct(const M_hash_stru64_t *h, const char *key);
162
163
164/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
165
166/*! Wether the hashtable a multi value table.
167 *
168 * \param[in] h Hashtable being referenced.
169 *
170 * \return M_TRUE if a multi value hashtable.
171 */
173
174
175/*! Get the number of values for a given key.
176 *
177 * \param[in] h Hashtable being referenced.
178 * \param[in] key Key for value to retrieve.
179 * \param[out] len The number of values.
180 *
181 * \return M_TRUE if length is retrieved, M_FALSE if key does not exist.
182 */
183M_API M_bool M_hash_stru64_multi_len(const M_hash_stru64_t *h, const char *key, size_t *len);
184
185
186/*! Retrieve the value for a key from the given index when supporting muli-values.
187 *
188 * \param[in] h Hashtable being referenced.
189 * \param[in] key Key for value to retrieve.
190 * \param[in] idx The index the value resides at.
191 * \param[out] value Pointer to value stored. Optional, pass NULL if not needed.
192 *
193 * \return M_TRUE if value retrieved, M_FALSE if key does not exist
194 */
195M_API M_bool M_hash_stru64_multi_get(const M_hash_stru64_t *h, const char *key, size_t idx, M_uint64 *value);
196
197
198/*! Retrieve the value for a key from the given index when supporting muli-values.
199 *
200 * \param[in] h Hashtable being referenced.
201 * \param[in] key Key for value to retrieve.
202 * \param[in] idx The index the value resides at.
203 *
204 * \return M_TRUE if value retrieved, M_FALSE if key does not exist.
205 */
206M_API M_uint64 M_hash_stru64_multi_get_direct(const M_hash_stru64_t *h, const char *key, size_t idx);
207
208
209/*! Remove a value from the hashtable when supporting muli-values.
210 *
211 * If all values have been removed then the key will be removed.
212 *
213 * \param[in,out] h Hashtable being referenced
214 * \param[in] key Key for value to retrieve.
215 * \param[in] idx The index the value resides at.
216 *
217 * \return M_TRUE if the value was removed, M_FALSE if key does not exist.
218 */
219M_API M_bool M_hash_stru64_multi_remove(M_hash_stru64_t *h, const char *key, size_t idx);
220
221
222/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
223
224/*! Retrieve the current size (number of buckets/slots, not necessarily used).
225 *
226 * \param[in] h Hashtable being referenced.
227 *
228 * \return Size of the hashtable
229 */
230M_API M_uint32 M_hash_stru64_size(const M_hash_stru64_t *h);
231
232
233/*! Retrieve the number of collisions for hashtable entries that has occurred since creation.
234 *
235 * \param[in] h Hashtable being referenced.
236 *
237 * \return Number of collisions.
238 */
240
241
242/*! Retrieve the number of expansions/rehashes since creation.
243 *
244 * \param[in] h Hashtable being referenced.
245 *
246 * \return number of expansions/rehashes.
247 */
249
250
251/*! Retrieve the number of entries in the hashtable.
252 *
253 * This is the number of keys stored.
254 *
255 * \param[in] h Hashtable being referenced.
256 *
257 * \return number of entries in the hashtable.
258 */
260
261
262/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
263
264/*! Start an enumeration of the keys within a hashtable.
265 *
266 * \param[in] h Hashtable being referenced.
267 * \param[out] hashenum Outputs an initialized state variable for starting an enumeration.
268 *
269 * \return Number of items in the hashtable
270 *
271 * \see M_hash_stru64_enumerate_free
272 */
274
275
276/*! Retrieve the next item from a hashtable enumeration.
277 *
278 * If multi-value, keys will appear multiple times as each value will be
279 * retrieved individually.
280 *
281 * \param[in] h Hashtable being referenced.
282 * \param[in,out] hashenum State variable for tracking the enumeration process.
283 * \param[out] key Value of next enumerated key. Optional, pass NULL if not needed.
284 * \param[out] value Value of next enumerated value. Optional, pass NULL if not needed.
285 *
286 * \return M_TRUE if enumeration succeeded, M_FALSE if no more keys.
287 */
288M_API M_bool M_hash_stru64_enumerate_next(const M_hash_stru64_t *h, M_hash_stru64_enum_t *hashenum, const char **key, M_uint64 *value);
289
290
291/*! Destroy an enumeration state.
292 *
293 * \param[in] hashenum Enumeration to destroy.
294 */
296
297
298/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
299
300/*! Merge two hashtables together.
301 *
302 * The second (src) hashtable will be destroyed automatically upon completion of this function. Any key/value
303 * pointers for the hashtable will be directly copied over to the destination hashtable, they will not be
304 * duplicated. Any keys which exist in 'dest' that also exist in 'src' will be overwritten by the 'src' value.
305 *
306 * If dest and src are multi-value, all values from src will be copied into dest and the values from
307 * dest will not be removed. If dest is not multi-value and src is, then only the last value in src will
308 * be present in dest. If dest is multi-value and src is not, then the value from src will be added to dest.
309 *
310 * \param[in,out] dest Pointer by reference to the hashtable receiving the key/value pairs.
311 * if dest is NULL, the src address will simply be copied to dest.
312 * \param[in,out] src Pointer to the hashtable giving up its key/value pairs.
313 */
314M_API void M_hash_stru64_merge(M_hash_stru64_t **dest, M_hash_stru64_t *src) M_FREE(2);
315
316/*! Duplicate an existing hashtable.
317 *
318 * Copying all keys and values.
319 *
320 * \param[in] h Hashtable to be copied.
321 *
322 * \return Duplicated hashtable.
323 */
325
326/*! @} */
327
328__END_DECLS
329
330#endif /* __M_HASH_STRU64_H__ */
331
struct M_hash_stru64_enum M_hash_stru64_enum_t
Definition: m_hash_stru64.h:55
M_bool M_hash_stru64_insert(M_hash_stru64_t *h, const char *key, M_uint64 value)
M_bool M_hash_stru64_multi_len(const M_hash_stru64_t *h, const char *key, size_t *len)
M_hash_stru64_flags_t
Definition: m_hash_stru64.h:59
M_bool M_hash_stru64_remove(M_hash_stru64_t *h, const char *key)
M_hash_stru64_t * M_hash_stru64_duplicate(const M_hash_stru64_t *h) M_MALLOC
M_bool M_hash_stru64_get(const M_hash_stru64_t *h, const char *key, M_uint64 *value)
size_t M_hash_stru64_num_expansions(const M_hash_stru64_t *h)
M_uint64 M_hash_stru64_multi_get_direct(const M_hash_stru64_t *h, const char *key, size_t idx)
size_t M_hash_stru64_enumerate(const M_hash_stru64_t *h, M_hash_stru64_enum_t **hashenum)
M_bool M_hash_stru64_multi_get(const M_hash_stru64_t *h, const char *key, size_t idx, M_uint64 *value)
void M_hash_stru64_merge(M_hash_stru64_t **dest, M_hash_stru64_t *src) M_FREE(2)
M_uint32 M_hash_stru64_size(const M_hash_stru64_t *h)
struct M_hash_stru64 M_hash_stru64_t
Definition: m_hash_stru64.h:51
size_t M_hash_stru64_num_keys(const M_hash_stru64_t *h)
void M_hash_stru64_enumerate_free(M_hash_stru64_enum_t *hashenum)
M_hash_stru64_t * M_hash_stru64_create(size_t size, M_uint8 fillpct, M_uint32 flags) M_MALLOC
M_bool M_hash_stru64_enumerate_next(const M_hash_stru64_t *h, M_hash_stru64_enum_t *hashenum, const char **key, M_uint64 *value)
M_bool M_hash_stru64_is_multi(const M_hash_stru64_t *h)
size_t M_hash_stru64_num_collisions(const M_hash_stru64_t *h)
M_bool M_hash_stru64_multi_remove(M_hash_stru64_t *h, const char *key, size_t idx)
M_uint64 M_hash_stru64_get_direct(const M_hash_stru64_t *h, const char *key)
void M_hash_stru64_destroy(M_hash_stru64_t *h) M_FREE(1)
@ M_HASH_STRU64_KEYS_SORTDESC
Definition: m_hash_stru64.h:69
@ M_HASH_STRU64_KEYS_ORDERED
Definition: m_hash_stru64.h:66
@ M_HASH_STRU64_STATIC_SEED
Definition: m_hash_stru64.h:74
@ M_HASH_STRU64_CASECMP
Definition: m_hash_stru64.h:61
@ M_HASH_STRU64_KEYS_UPPER
Definition: m_hash_stru64.h:62
@ M_HASH_STRU64_KEYS_LOWER
Definition: m_hash_stru64.h:64
@ M_HASH_STRU64_KEYS_SORTASC
Definition: m_hash_stru64.h:68
@ M_HASH_STRU64_MULTI_VALUE
Definition: m_hash_stru64.h:70
@ M_HASH_STRU64_NONE
Definition: m_hash_stru64.h:60
@ M_HASH_STRU64_MULTI_GETLAST
Definition: m_hash_stru64.h:72