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
14extern "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
34struct 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
53struct hash_map_entry *hash_map_entry_new(void *key, void *value);
54
63void hash_map_entry_free(struct hash_map *map, struct hash_map_entry *entry);
64
75struct 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
86void hash_map_free(struct hash_map *map);
87
97int hash_map_insert(struct hash_map *map, void *key, void *value);
98
107int hash_map_find(struct hash_map *map, void *key, void **value);
108
117int hash_map_delete(struct hash_map *map, void *key);
118
126void hash_map_for_each(struct hash_map *map,
127 void (*fn)(void *key, void *value, void *ptr),
128 void *ptr);
129
137unsigned int hash(const char *data, size_t n);
138
139unsigned int sstr_key_hash(void *key);
140int sstr_key_cmp(void *a, void *b);
141void sstr_key_free(void *key);
142
143#ifdef __cplusplus
144}
145#endif
146
147#endif // HASH_MAP_H
int hash_map_find(struct hash_map *map, void *key, void **value)
find a entry in hash_map.
Definition hash_map.c:138
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:9
void hash_map_free(struct hash_map *map)
free a hash_map.
Definition hash_map.c:54
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:172
int hash_map_delete(struct hash_map *map, void *key)
remove a entry from hash_map.
Definition hash_map.c:151
int hash_map_insert(struct hash_map *map, void *key, void *value)
insert a new entry into hash_map.
Definition hash_map.c:102
unsigned int hash(const char *data, size_t n)
calculate the hash value of a key.
Definition hash_map.c:184
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:21
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:29
sstr_t are objects that represent sequences of characters.
Definition hash_map.h:28
Definition hash_map.h:34