json-gen-c  0.1.5
json-gen-c generate C code for json manipulation
hash_map.h
Go to the documentation of this file.
1 
6 #ifndef HASH_MAP_H
7 #define HASH_MAP_H
8 
9 #include <stddef.h>
10 
11 #include "utils/sstr.h"
12 
13 #ifdef __cplusplus
14 extern "C" {
15 #endif
16 
21 // operation success
22 #define HASH_MAP_OK 0
23 // duplicate key
24 #define HASH_MAP_DUPLICATE_KEY 1
25 // malloc failed
26 #define HASH_MAP_ERROR -1
27 
29  void *key;
30  void *value;
31  struct hash_map_entry *next;
32 };
33 
34 struct hash_map {
35  struct hash_map_entry **buckets;
36  int bucket_count;
37  int size;
38  unsigned int (*hash_func)(void *);
39  int (*key_cmp_func)(void *, void *);
40  void (*key_free_func)(void *);
41  void (*value_free_func)(void *);
42 };
43 
53 struct hash_map_entry *hash_map_entry_new(void *key, void *value);
54 
63 void hash_map_entry_free(struct hash_map *map, struct hash_map_entry *entry);
64 
75 struct hash_map *hash_map_new(int bucket_count,
76  unsigned int (*hash_func)(void *),
77  int (*key_cmp_func)(void *, void *),
78  void (*key_free_func)(void *),
79  void (*value_free_func)(void *));
80 
86 void hash_map_free(struct hash_map *map);
87 
97 int hash_map_insert(struct hash_map *map, void *key, void *value);
98 
107 int hash_map_find(struct hash_map *map, void *key, void **value);
108 
117 int hash_map_delete(struct hash_map *map, void *key);
118 
126 void hash_map_for_each(struct hash_map *map,
127  void (*fn)(void *key, void *value, void *ptr),
128  void *ptr);
129 
137 unsigned int hash(const char *data, size_t n);
138 
139 unsigned int sstr_key_hash(void *key);
140 int sstr_key_cmp(void *a, void *b);
141 void sstr_key_free(void *key);
142 
143 #ifdef __cplusplus
144 }
145 #endif
146 
147 #endif // HASH_MAP_H
hash_map_free
void hash_map_free(struct hash_map *map)
free a hash_map.
Definition: hash_map.c:51
hash_map_insert
int hash_map_insert(struct hash_map *map, void *key, void *value)
insert a new entry into hash_map.
Definition: hash_map.c:66
hash_map_entry_free
void hash_map_entry_free(struct hash_map *map, struct hash_map_entry *entry)
free a hash_map_entry. the key and value will be freed by key_free_func and value_free_func of map.
Definition: hash_map.c:18
hash_map_for_each
void hash_map_for_each(struct hash_map *map, void(*fn)(void *key, void *value, void *ptr), void *ptr)
for each key-value pair in hash_map, call fn(key, value).
Definition: hash_map.c:119
hash_map
Definition: hash_map.h:34
sstr.h
sstr_t are objects that represent sequences of characters.
hash_map_entry
Definition: hash_map.h:28
hash_map_find
int hash_map_find(struct hash_map *map, void *key, void **value)
find a entry in hash_map.
Definition: hash_map.c:85
hash_map_new
struct hash_map * hash_map_new(int bucket_count, unsigned int(*hash_func)(void *), int(*key_cmp_func)(void *, void *), void(*key_free_func)(void *), void(*value_free_func)(void *))
create a new hash_map.
Definition: hash_map.c:26
hash
unsigned int hash(const char *data, size_t n)
calculate the hash value of a key.
Definition: hash_map.c:131
hash_map_delete
int hash_map_delete(struct hash_map *map, void *key)
remove a entry from hash_map.
Definition: hash_map.c:98
hash_map_entry_new
struct hash_map_entry * hash_map_entry_new(void *key, void *value)
create a new hash_map_entry with key and value.
Definition: hash_map.c:6