• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #pragma once
2 #ifndef IWHMAP_H
3 #define IWHMAP_H
4 
5 /**************************************************************************************************
6  * Hashmap implementation.
7  *
8  * IOWOW library
9  *
10  * MIT License
11  *
12  * Copyright (c) 2012-2020 Softmotions Ltd <info@softmotions.com>
13  *
14  * Permission is hereby granted, free of charge, to any person obtaining a copy
15  * of this software and associated documentation files (the "Software"), to deal
16  * in the Software without restriction, including without limitation the rights
17  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
18  *  copies of the Software, and to permit persons to whom the Software is
19  * furnished to do so, subject to the following conditions:
20  *
21  * The above copyright notice and this permission notice shall be included in all
22  * copies or substantial portions of the Software.
23  *
24  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30  * SOFTWARE.
31  *************************************************************************************************/
32 
33 #include "basedefs.h"
34 IW_EXTERN_C_START
35 
36 struct _IWHMAP;
37 typedef struct _IWHMAP IWHMAP;
38 
39 typedef struct {
40   IWHMAP *hm;
41   const void *key;
42   const void *val;
43   uint32_t bucket;
44   int32_t entry;
45 } IWHMAP_ITER;
46 
47 /**
48  * @brief Key/Value free callback which uses standard `free()` deallocation.
49  *
50  * @param key Pointer to key or zero.
51  * @param val Pointer to value of zero.
52  */
53 IW_EXPORT void iwhmap_kv_free(void *key, void *val);
54 
55 IW_EXPORT IWHMAP *iwhmap_create(int (*cmp_fn)(const void *, const void *),
56                                 uint32_t (*hash_key_fn)(const void *),
57                                 void (*kv_free_fn)(void *, void *));
58 
59 IW_EXPORT IWHMAP *iwhmap_create_i64(void (*kv_free_fn)(void *, void *));
60 
61 IW_EXPORT IWHMAP *iwhmap_create_i32(void (*kv_free_fn)(void *, void *));
62 
63 IW_EXPORT IWHMAP *iwhmap_create_str(void (*kv_free_fn)(void *, void *));
64 
65 IW_EXPORT iwrc iwhmap_put(IWHMAP *hm, void *key, void *val);
66 
67 IW_EXPORT void iwhmap_remove(IWHMAP *hm, const void *key);
68 
69 IW_EXPORT void *iwhmap_get(IWHMAP *hm, const void *key);
70 
71 IW_EXPORT int iwhmap_count(IWHMAP *hm);
72 
73 IW_EXPORT void iwhmap_clear(IWHMAP *hm);
74 
75 IW_EXPORT void iwhmap_iter_init(IWHMAP *hm, IWHMAP_ITER *iter);
76 
77 IW_EXPORT bool iwhmap_iter_next(IWHMAP_ITER *iter);
78 
79 IW_EXPORT void iwhmap_destroy(IWHMAP *hm);
80 
81 IW_EXTERN_C_END
82 #endif
83