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
53extern struct hash_map_entry *hash_map_entry_new(void *key, void *value);
54
63extern void hash_map_entry_free(struct hash_map *map, struct hash_map_entry *entry);
64
75extern 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
86extern void hash_map_free(struct hash_map *map);
87
97extern int hash_map_insert(struct hash_map *map, void *key, void *value);
98
107extern int hash_map_find(struct hash_map *map, void *key, void **value);
108
117extern int hash_map_delete(struct hash_map *map, void *key);
118
126extern void hash_map_for_each(struct hash_map *map,
127 void (*fn)(void *key, void *value, void *ptr),
128 void *ptr);
129
137extern unsigned int hash(const char *data, size_t n);
138
139extern unsigned int sstr_key_hash(void *key);
140extern int sstr_key_cmp(void *a, void *b);
141extern void 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:164
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:10
void hash_map_free(struct hash_map *map)
free a hash_map.
Definition hash_map.c:55
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:198
int hash_map_delete(struct hash_map *map, void *key)
remove a entry from hash_map.
Definition hash_map.c:177
int hash_map_insert(struct hash_map *map, void *key, void *value)
insert a new entry into hash_map.
Definition hash_map.c:128
unsigned int hash(const char *data, size_t n)
calculate the hash value of a key.
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:22
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:30
sstr_t are objects that represent sequences of characters.
Definition hash_map.h:28
Definition hash_map.h:34