EII Message Bus C Reference
hashmap.h
Go to the documentation of this file.
1 // Copyright (c) 2019 Intel Corporation.
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining a copy
4 // of this software and associated documentation files (the "Software"), to
5 // deal in the Software without restriction, including without limitation the
6 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7 // sell copies of the Software, and to permit persons to whom the Software is
8 // furnished to do so, subject to the following conditions:
9 //
10 // The above copyright notice and this permission notice shall be included in
11 // all copies or substantial portions of the Software.
12 //
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18 // FROM,OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
19 // IN THE SOFTWARE.
20 
26 #ifndef _EII_MSGENV_HASHMAP_H
27 #define _EII_MSGENV_HASHMAP_H
28 
29 #include <stdlib.h>
30 #include <stdbool.h>
31 
32 #ifdef __cplusplus
33 extern "C" {
34 #endif // __cplusplus
35 
39 typedef enum {
40  MAP_SUCCESS = 0,
41  MAP_FULL = -1,
42  MAP_OMEM = -2,
43  MAP_KEY_EXISTS = -3,
44  MAP_KEY_NOT_EXISTS = -4,
46 
50 typedef struct {
51  // Element key
52  char* key;
53 
54  // Length of the key
55  size_t key_len;
56 
57  // Whether or not the element slot is currently in use (i.e. value != NULL)
58  bool in_use;
59 
60  // Value of the (key, value) pair
61  void* value;
62 
63  // Free function for the value
64  void (*free)(void*);
66 
70 typedef struct {
71  // Initial size of the hashmap (used for growing the hashmap).
72  int init_size;
73 
74  // Current maximum number of keys (currently allocated memory)
75  int max_size;
76 
77  // Number of assigned slots
78  int size;
79 
80  // Hashmap elements
81  hashmap_elem_t* elems;
82 } hashmap_t;
83 
99 #define HASHMAP_LOOP(MAP, VALUE_TYPE, CODE) \
100  for(int i = 0; i < MAP->max_size; i++) { \
101  if(MAP->elems[i].in_use) { \
102  VALUE_TYPE* value = MAP->elems[i].value; \
103  const char* key = MAP->elems[i].key; \
104  CODE \
105  } \
106  }
107 
117 hashmap_t* hashmap_new(size_t init_size);
118 
129  hashmap_t* map, const char* key, void* val, void (*free_fn)(void*));
130 
138 void* hashmap_get(hashmap_t* map, const char* key);
139 
147 hashmap_ret_t hashmap_remove(hashmap_t* map, const char* key);
148 
154 void hashmap_destroy(hashmap_t* map);
155 
156 #ifdef __cplusplus
157 }
158 #endif // __cplusplus
159 
160 #endif // _EII_MSGENV_HASHMAP_H
hashmap_ret_t
hashmap_ret_t
Definition: hashmap.h:39
hashmap_remove
hashmap_ret_t hashmap_remove(hashmap_t *map, const char *key)
hashmap_new
hashmap_t * hashmap_new(size_t init_size)
hashmap_destroy
void hashmap_destroy(hashmap_t *map)
hashmap_t
Definition: hashmap.h:70
hashmap_put
hashmap_ret_t hashmap_put(hashmap_t *map, const char *key, void *val, void(*free_fn)(void *))
hashmap_get
void * hashmap_get(hashmap_t *map, const char *key)
hashmap_elem_t
Definition: hashmap.h:50