• 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-2022 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(
56   int (*cmp_fn)(const void*, const void*),
57   uint32_t (*hash_key_fn)(const void*),
58   void (*kv_free_fn)(void*, void*));
59 
60 IW_EXPORT IWHMAP* iwhmap_create_ptr(void (*kv_free_fn)(void*, void*));
61 
62 IW_EXPORT IWHMAP* iwhmap_create_u64(void (*kv_free_fn)(void*, void*));
63 
64 IW_EXPORT IWHMAP* iwhmap_create_u32(void (*kv_free_fn)(void*, void*));
65 
66 IW_EXPORT IWHMAP* iwhmap_create_str(void (*kv_free_fn)(void*, void*));
67 
68 IW_EXPORT iwrc iwhmap_put(IWHMAP *hm, void *key, void *val);
69 
70 IW_EXPORT iwrc iwhmap_put_u32(IWHMAP *hm, uint32_t key, void *val);
71 
72 IW_EXPORT iwrc iwhmap_put_u64(IWHMAP *hm, uint64_t key, void *val);
73 
74 /// Makes copy of key (strdup) then puts key value pair into map.
75 /// @note Key memory expected to be released by `kv_free_fn` function given to iwhmap_create_xxx.
76 IW_EXPORT iwrc iwhmap_put_str(IWHMAP *hm, const char *key, void *val);
77 
78 IW_EXPORT iwrc iwhmap_rename(IWHMAP *hm, const void *key_old, void *key_new);
79 
80 IW_EXPORT bool iwhmap_remove(IWHMAP *hm, const void *key);
81 
82 IW_EXPORT bool iwhmap_remove_u64(IWHMAP *hm, uint64_t key);
83 
84 IW_EXPORT bool iwhmap_remove_u32(IWHMAP *hm, uint32_t key);
85 
86 IW_EXPORT void* iwhmap_get(IWHMAP *hm, const void *key);
87 
88 IW_EXPORT void* iwhmap_get_u64(IWHMAP *hm, uint64_t key);
89 
90 IW_EXPORT void* iwhmap_get_u32(IWHMAP *hm, uint32_t key);
91 
92 IW_EXPORT uint32_t iwhmap_count(IWHMAP *hm);
93 
94 IW_EXPORT void iwhmap_clear(IWHMAP *hm);
95 
96 IW_EXPORT void iwhmap_iter_init(IWHMAP *hm, IWHMAP_ITER *iter);
97 
98 IW_EXPORT bool iwhmap_iter_next(IWHMAP_ITER *iter);
99 
100 IW_EXPORT void iwhmap_destroy(IWHMAP *hm);
101 
102 typedef bool (*iwhmap_lru_eviction_needed)(IWHMAP *hm, void *user_data);
103 
104 IW_EXPORT bool iwhmap_lru_eviction_max_count(IWHMAP *hm, void *max_count_val);
105 
106 /// Init LRU eviction mode for given `hm` map.
107 /// @param ev Returns `true` if needed to evict the next least recently used element.
108 /// @param ev_user_data Arbitrary user data from `ev` function.
109 IW_EXPORT void iwhmap_lru_init(IWHMAP *hm, iwhmap_lru_eviction_needed ev, void *ev_user_data);
110 
111 IW_EXTERN_C_END
112 #endif
113