Mstdlib-1.24.0
m_hash_multi.h
1/* The MIT License (MIT)
2 *
3 * Copyright (h) 2016 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_MULTI_H__
25#define __M_HASH_MULTI_H__
26
27/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
28
29#include <mstdlib/base/m_defs.h>
30#include <mstdlib/base/m_types.h>
31#include <mstdlib/base/m_hashtable.h>
32
33/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
34
35__BEGIN_DECLS
36
37/*! \addtogroup m_hash_multi Hashtable - Multi
38 * \ingroup m_hashtable
39 *
40 * Hashtable, meant for storing a variety of key and value types.
41 *
42 * All data except void pointers will be duplicated.
43 *
44 * @{
45 */
46
47struct M_hash_multi;
48typedef struct M_hash_multi M_hash_multi_t;
49
50/*! Flags for controlling the behavior of the hash_multi. */
51typedef enum {
52 M_HASH_MULTI_NONE = 0, /*!< String key compare is case sensitive. */
53 M_HASH_MULTI_STR_CASECMP = 1 << 0 /*!< String key compare is case insensitive. */
55
56
57/* Types of data that can be uses as values. */
58typedef enum {
59 M_HASH_MULTI_VAL_TYPE_UNKNOWN = 0, /*!< Unknown. */
61 M_HASH_MULTI_VAL_TYPE_INT, /*!< Integer. */
64 M_HASH_MULTI_VAL_TYPE_VP /*!< Void pointer. Could be any data. */
66
67
68/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
69
70/*! Callback for freeing void pointer data. */
71typedef void (*M_hash_multi_free_func)(void *);
72
73
74/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
75
76/*! Create a new multi hashtable.
77 *
78 * \param[in] flags M_hash_multi_flags_t flags for modifying behavior.
79 *
80 * return multi table.
81 */
82M_API M_hash_multi_t *M_hash_multi_create(M_uint32 flags) M_MALLOC;
83
84
85/*! Destroy the hashtable.
86 *
87 * \param[in] h Hashtable to destroy.
88 */
89M_API void M_hash_multi_destroy(M_hash_multi_t *h) M_FREE(1);
90
91
92/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
93
94/*! Insert bool with an integer key.
95 *
96 * \param[in,out] h Hashtable.
97 * \param[in] key Integer key.
98 * \param[in] val Boolean value.
99 *
100 * \return M_TRUE if insert was successful. Otherwise M_FALSE.
101 */
102M_API M_bool M_hash_multi_u64_insert_bool(M_hash_multi_t *h, M_uint64 key, M_bool val);
103
104
105/*! Insert signed integer with an integer key.
106 *
107 * \param[in,out] h Hashtable.
108 * \param[in] key Integer key.
109 * \param[in] val Signed integer val.
110 *
111 * \return M_TRUE if insert was successful. Otherwise M_FALSE.
112 */
113M_API M_bool M_hash_multi_u64_insert_int(M_hash_multi_t *h, M_uint64 key, M_int64 val);
114
115
116/*! Insert unsigned integer with an integer key.
117 *
118 * \param[in,out] h Hashtable.
119 * \param[in] key Integer key.
120 * \param[in] val Unsigned integer val.
121 *
122 * \return M_TRUE if insert was successful. Otherwise M_FALSE.
123 */
124M_API M_bool M_hash_multi_u64_insert_uint(M_hash_multi_t *h, M_uint64 key, M_uint64 val);
125
126
127/*! Insert string with an integer key.
128 *
129 * \param[in,out] h Hashtable.
130 * \param[in] key Integer key.
131 * \param[in] val NULL terminated string.
132 *
133 * \return M_TRUE if insert was successful. Otherwise M_FALSE.
134 */
135M_API M_bool M_hash_multi_u64_insert_str(M_hash_multi_t *h, M_uint64 key, const char *val);
136
137
138/*! Insert binary data with an integer key.
139 *
140 * \param[in,out] h Hashtable.
141 * \param[in] key Integer key.
142 * \param[in] val Binary data.
143 * \param[in] len Length of binary data.
144 *
145 * \return M_TRUE if insert was successful. Otherwise M_FALSE.
146 */
147M_API M_bool M_hash_multi_u64_insert_bin(M_hash_multi_t *h, M_uint64 key, const unsigned char *val, size_t len);
148
149
150/*! Insert a void pointer with an integer key.
151 *
152 * This will not duplicate the value. It only stores the memory address of the data.
153 * If a value exists at the given key, it will be destroyed if it was inserted with a
154 * value free function.
155 *
156 * \param[in,out] h Hashtable.
157 * \param[in] key Integer key.
158 * \param[in] val The memory location of the data.
159 * \param[in] val_free Callback for freeing the data.
160 *
161 * \return M_TRUE if insert was successful. Otherwise M_FALSE.
162 */
163M_API M_bool M_hash_multi_u64_insert_vp(M_hash_multi_t *h, M_uint64 key, void *val, M_hash_multi_free_func val_free);
164
165
166/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
167
168/*! Get a bool value with a string key.
169 *
170 * \param[in] h Hashtable.
171 * \param[in] key Integer key.
172 * \param[in,out] val The value to get.
173 *
174 * \return M_TRUE if insert was successful. Otherwise M_FALSE.
175 */
176M_API M_bool M_hash_multi_u64_get_bool(const M_hash_multi_t *h, M_uint64 key, M_bool *val);
177
178
179/*! Get a signed integer value with an integer key.
180 *
181 * \param[in,out] h Hashtable.
182 * \param[in] key Integer key.
183 * \param[in,out] val The value to get.
184 *
185 * \return M_TRUE if insert was successful. Otherwise M_FALSE.
186 */
187M_API M_bool M_hash_multi_u64_get_int(const M_hash_multi_t *h, M_uint64 key, M_int64 *val);
188
189
190/*! Get an unsigned integer value with an integer key.
191 *
192 * \param[in] h Hashtable.
193 * \param[in] key Integer key.
194 * \param[in,out] val The value to get.
195 *
196 * \return M_TRUE if insert was successful. Otherwise M_FALSE.
197 */
198M_API M_bool M_hash_multi_u64_get_uint(const M_hash_multi_t *h, M_uint64 key, M_uint64 *val);
199
200
201/*! Get a string value with an integer key.
202 *
203 * \param[in] h Hashtable.
204 * \param[in] key Integer key.
205 * \param[in,out] val The value to get.
206 *
207 * \return M_TRUE if insert was successful. Otherwise M_FALSE.
208 */
209M_API M_bool M_hash_multi_u64_get_str(const M_hash_multi_t *h, M_uint64 key, const char **val);
210
211
212/*! Get binary data with an integer key.
213 *
214 * \param[in] h Hashtable.
215 * \param[in] key Integer key.
216 * \param[in,out] val The value to get.
217 * \param[in,out] len The value length.
218 *
219 * \return M_TRUE if insert was successful. Otherwise M_FALSE.
220 */
221M_API M_bool M_hash_multi_u64_get_bin(const M_hash_multi_t *h, M_uint64 key, const unsigned char **val, size_t *len);
222
223
224/*! Get a void pointer value with an integer key.
225 *
226 * \param[in] h Hashtable.
227 * \param[in] key Integer key.
228 * \param[in,out] val The value to get.
229 *
230 * \return M_TRUE if insert was successful. Otherwise M_FALSE.
231 */
232M_API M_bool M_hash_multi_u64_get_vp(const M_hash_multi_t *h, M_uint64 key, void **val);
233
234
235/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
236
237/*! Remove a value with an integer key.
238 *
239 * \param[in,out] h Hashtable.
240 * \param[in] key Integer key.
241 * \param[in] destroy_vp If the value is a void pointer M_TRUE if the associated (if set)
242 * value free callback should be called.
243 *
244 * \return M_TRUE if insert was successful. Otherwise M_FALSE.
245 */
246M_API M_bool M_hash_multi_u64_remove(M_hash_multi_t *h, M_uint64 key, M_bool destroy_vp);
247
248
249/*! Get the type of data stored in with an integer key.
250 *
251 * \param[in] h Hashtable.
252 * \param[in] key Integer key.
253 *
254 * \return The type.
255 */
257
258
259/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
260
261/*! Insert bool with an string key.
262 *
263 * \param[in,out] h Hashtable.
264 * \param[in] key String key.
265 * \param[in] val Boolean value.
266 *
267 * \return M_TRUE if insert was successful. Otherwise M_FALSE.
268 */
269M_API M_bool M_hash_multi_str_insert_bool(M_hash_multi_t *h, const char *key, M_bool val);
270
271
272/*! Insert signed integer with a string key.
273 *
274 * \param[in,out] h Hashtable.
275 * \param[in] key Integer key.
276 * \param[in] val Signed integer val
277 *
278 * \return M_TRUE if insert was successful. Otherwise M_FALSE.
279 */
280M_API M_bool M_hash_multi_str_insert_int(M_hash_multi_t *h, const char *key, M_int64 val);
281
282
283/*! Insert unsigned integer with a string key.
284 *
285 * \param[in,out] h Hashtable.
286 * \param[in] key String key.
287 * \param[in] val Unsigned integer val.
288 *
289 * \return M_TRUE if insert was successful. Otherwise M_FALSE.
290 */
291M_API M_bool M_hash_multi_str_insert_uint(M_hash_multi_t *h, const char *key, M_uint64 val);
292
293
294/*! Insert string with a string key.
295 *
296 * \param[in,out] h Hashtable.
297 * \param[in] key String key.
298 * \param[in] val NULL terminated string.
299 *
300 * \return M_TRUE if insert was successful. Otherwise M_FALSE.
301 */
302M_API M_bool M_hash_multi_str_insert_str(M_hash_multi_t *h, const char *key, const char *val);
303
304
305/*! Insert binary data with a string key.
306 *
307 * \param[in,out] h Hashtable.
308 * \param[in] key String key.
309 * \param[in] val Binary data.
310 * \param[in] len Length of binary data.
311 *
312 * \return M_TRUE if insert was successful. Otherwise M_FALSE.
313 */
314M_API M_bool M_hash_multi_str_insert_bin(M_hash_multi_t *h, const char *key, const unsigned char *val, size_t len);
315
316
317/*! Insert a void pointer with a string key.
318 *
319 * This will not duplicate the value. It only stores the memory address of the data.
320 * The if a value exists at the given key it will be destroyed if it was inserted with a
321 * value free function.
322 *
323 * \param[in,out] h Hashtable.
324 * \param[in] key String key.
325 * \param[in] val The memory location of the data.
326 * \param[in] val_free Callback for freeing the data.
327 *
328 * \return M_TRUE if insert was successful. Otherwise M_FALSE.
329 */
330M_API M_bool M_hash_multi_str_insert_vp(M_hash_multi_t *h, const char *key, void *val, M_hash_multi_free_func val_free);
331
332
333/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
334
335/*! Get a bool value with a string key.
336 *
337 * \param[in,out] h Hashtable.
338 * \param[in] key Integer key.
339 * \param[in,out] val The value to get.
340 *
341 * \return M_TRUE if insert was successful. Otherwise M_FALSE.
342 */
343M_API M_bool M_hash_multi_str_get_bool(const M_hash_multi_t *h, const char *key, M_bool *val);
344
345
346/*! Get a signed integer value with a string key.
347 *
348 * \param[in,out] h Hashtable.
349 * \param[in] key Integer key.
350 * \param[in,out] val The value to get.
351 *
352 * \return M_TRUE if insert was successful. Otherwise M_FALSE.
353 */
354M_API M_bool M_hash_multi_str_get_int(const M_hash_multi_t *h, const char *key, M_int64 *val);
355
356
357/*! Get an unsigned integer value with a string key.
358 *
359 * \param[in,out] h Hashtable.
360 * \param[in] key String key.
361 * \param[in,out] val The value to get.
362 *
363 * \return M_TRUE if insert was successful. Otherwise M_FALSE.
364 */
365M_API M_bool M_hash_multi_str_get_uint(const M_hash_multi_t *h, const char *key, M_uint64 *val);
366
367
368/*! Get a string value with a string key.
369 *
370 * \param[in,out] h Hashtable.
371 * \param[in] key String key.
372 * \param[in,out] val The value to get.
373 *
374 * \return M_TRUE if insert was successful. Otherwise M_FALSE.
375 */
376M_API M_bool M_hash_multi_str_get_str(const M_hash_multi_t *h, const char *key, const char **val);
377
378
379/*! Get binary data with a string key.
380 *
381 * \param[in,out] h Hashtable.
382 * \param[in] key String key.
383 * \param[in,out] val The value to get.
384 * \param[in,out] len The value length.
385 *
386 * \return M_TRUE if insert was successful. Otherwise M_FALSE.
387 */
388M_API M_bool M_hash_multi_str_get_bin(const M_hash_multi_t *h, const char *key, const unsigned char **val, size_t *len);
389
390
391/*! Get a void pointer value with a string key.
392 *
393 * \param[in,out] h Hashtable.
394 * \param[in] key String key.
395 * \param[in,out] val The value to get.
396 *
397 * \return M_TRUE if insert was successful. Otherwise M_FALSE.
398 */
399M_API M_bool M_hash_multi_str_get_vp(const M_hash_multi_t *h, const char *key, void **val);
400
401
402/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
403
404/*! Remove a value with a string key.
405 *
406 * \param[in,out] h Hashtable.
407 * \param[in] key String key.
408 * \param[in] destroy_vp If the value is a void pointer M_TRUE if the associated (if set)
409 * value free callback should be called.
410 *
411 * \return M_TRUE if insert was successful. Otherwise M_FALSE.
412 */
413M_API M_bool M_hash_multi_str_remove(M_hash_multi_t *h, const char *key, M_bool destroy_vp);
414
415
416/*! Get the type of data stored in with a string key.
417 *
418 * \param[in] h Hashtable.
419 * \param[in] key String key.
420 *
421 * \return The type.
422 */
424
425/*! @} */
426
427__END_DECLS
428
429#endif /* __M_HASH_MULTI_H__ */
430
M_bool M_hash_multi_str_get_str(const M_hash_multi_t *h, const char *key, const char **val)
struct M_hash_multi M_hash_multi_t
Definition: m_hash_multi.h:48
M_bool M_hash_multi_u64_get_bool(const M_hash_multi_t *h, M_uint64 key, M_bool *val)
M_hash_multi_val_type_t M_hash_multi_u64_type(const M_hash_multi_t *h, M_uint64 key)
M_bool M_hash_multi_str_get_bool(const M_hash_multi_t *h, const char *key, M_bool *val)
M_bool M_hash_multi_u64_insert_vp(M_hash_multi_t *h, M_uint64 key, void *val, M_hash_multi_free_func val_free)
M_bool M_hash_multi_u64_remove(M_hash_multi_t *h, M_uint64 key, M_bool destroy_vp)
M_bool M_hash_multi_u64_insert_int(M_hash_multi_t *h, M_uint64 key, M_int64 val)
M_bool M_hash_multi_str_insert_uint(M_hash_multi_t *h, const char *key, M_uint64 val)
M_bool M_hash_multi_str_remove(M_hash_multi_t *h, const char *key, M_bool destroy_vp)
M_bool M_hash_multi_u64_get_bin(const M_hash_multi_t *h, M_uint64 key, const unsigned char **val, size_t *len)
void(* M_hash_multi_free_func)(void *)
Definition: m_hash_multi.h:71
M_bool M_hash_multi_str_insert_bool(M_hash_multi_t *h, const char *key, M_bool val)
M_bool M_hash_multi_u64_get_int(const M_hash_multi_t *h, M_uint64 key, M_int64 *val)
M_bool M_hash_multi_u64_insert_bool(M_hash_multi_t *h, M_uint64 key, M_bool val)
M_bool M_hash_multi_u64_insert_str(M_hash_multi_t *h, M_uint64 key, const char *val)
M_bool M_hash_multi_str_get_vp(const M_hash_multi_t *h, const char *key, void **val)
M_bool M_hash_multi_str_insert_bin(M_hash_multi_t *h, const char *key, const unsigned char *val, size_t len)
M_hash_multi_val_type_t
Definition: m_hash_multi.h:58
M_bool M_hash_multi_str_get_uint(const M_hash_multi_t *h, const char *key, M_uint64 *val)
M_bool M_hash_multi_u64_get_str(const M_hash_multi_t *h, M_uint64 key, const char **val)
M_bool M_hash_multi_str_insert_str(M_hash_multi_t *h, const char *key, const char *val)
M_bool M_hash_multi_u64_insert_uint(M_hash_multi_t *h, M_uint64 key, M_uint64 val)
void M_hash_multi_destroy(M_hash_multi_t *h) M_FREE(1)
M_hash_multi_flags_t
Definition: m_hash_multi.h:51
M_bool M_hash_multi_str_insert_int(M_hash_multi_t *h, const char *key, M_int64 val)
M_bool M_hash_multi_u64_get_uint(const M_hash_multi_t *h, M_uint64 key, M_uint64 *val)
M_bool M_hash_multi_str_insert_vp(M_hash_multi_t *h, const char *key, void *val, M_hash_multi_free_func val_free)
M_hash_multi_t * M_hash_multi_create(M_uint32 flags) M_MALLOC
M_bool M_hash_multi_u64_get_vp(const M_hash_multi_t *h, M_uint64 key, void **val)
M_hash_multi_val_type_t M_hash_multi_str_type(const M_hash_multi_t *h, const char *key)
M_bool M_hash_multi_str_get_int(const M_hash_multi_t *h, const char *key, M_int64 *val)
M_bool M_hash_multi_str_get_bin(const M_hash_multi_t *h, const char *key, const unsigned char **val, size_t *len)
M_bool M_hash_multi_u64_insert_bin(M_hash_multi_t *h, M_uint64 key, const unsigned char *val, size_t len)
@ M_HASH_MULTI_VAL_TYPE_STR
Definition: m_hash_multi.h:62
@ M_HASH_MULTI_VAL_TYPE_UNKNOWN
Definition: m_hash_multi.h:59
@ M_HASH_MULTI_VAL_TYPE_BIN
Definition: m_hash_multi.h:63
@ M_HASH_MULTI_VAL_TYPE_INT
Definition: m_hash_multi.h:61
@ M_HASH_MULTI_VAL_TYPE_VP
Definition: m_hash_multi.h:64
@ M_HASH_MULTI_VAL_TYPE_BOOL
Definition: m_hash_multi.h:60
@ M_HASH_MULTI_STR_CASECMP
Definition: m_hash_multi.h:53
@ M_HASH_MULTI_NONE
Definition: m_hash_multi.h:52