-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhashmap.h
More file actions
115 lines (97 loc) · 3.05 KB
/
hashmap.h
File metadata and controls
115 lines (97 loc) · 3.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*
* Generic hashmap manipulation functions
*
* Originally by Elliot C Back - http://elliottback.com/wp/hashmap-implementation-in-c/
*
* Modified by Pete Warden to fix a serious performance problem, support strings as keys
* and removed thread synchronization - http://petewarden.typepad.com
*
* Modified by ohnx to add more features for use with nu (http://nu.masonx.ca/)
*/
#ifndef __HASHMAP_H__
#define __HASHMAP_H__
#define MAP_MISSING -4 /* No such element */
#define MAP_FULL -3 /* Hashmap is full */
#define MAP_OMEM -2 /* Out of Memory */
#define MAP_ERR -1 /* Misc error*/
#define MAP_OK 0 /* OK */
/*
* any_t is a pointer. This allows you to put arbitrary structures in
* the hashmap.
*/
typedef void *any_t;
/*
* PFany is a pointer to a function that can take two any_t arguments,
* a const char and return an integer. Returns status code..
*/
typedef int (*PFany)(any_t, const char *, any_t);
/*
* PFsingle is a pointer to a function that can take a single any_t argument.
*/
typedef void (*PFsingle)(any_t);
/*
* PFserialize is a pointer to a function that can take an any_t argument
* and return a string.
*/
typedef char *(*PFserialize)(any_t);
/*
* map_t is a pointer to an internally maintained data structure.
* Clients of this package do not need to know how hashmaps are
* represented. They see and manipulate only map_t's.
*/
typedef any_t map_t;
/*
* Return an empty hashmap. Returns NULL if empty.
*/
extern map_t hashmap_new();
/*
* Merge two hashmaps. Returns NULL on error.
*/
extern map_t hashmap_merge(map_t a, map_t b);
/*
* Iteratively call f with argument (item, data) for
* each element data in the hashmap. The function must
* return a map status code. If it returns anything other
* than MAP_OK the traversal is terminated. f must
* not reenter any hashmap functions, or deadlock may arise.
*/
extern int hashmap_iterate(map_t in, PFany f, any_t item);
/*
* Add an element to the hashmap. Return MAP_OK or MAP_OMEM.
*/
extern int hashmap_put(map_t in, const char* key, any_t value);
/*
* Get an element from the hashmap. Return MAP_OK or MAP_MISSING.
*/
extern int hashmap_get(map_t in, const char* key, any_t *arg);
/*
* Get an element from the hashmap. Returns fresh char pointer.
*/
extern any_t hashmap_get_default(map_t in, const char* key, const any_t val_default);
/*
* Remove an element from the hashmap. Return MAP_OK or MAP_MISSING.
*/
extern int hashmap_remove(map_t in, char* key);
/*
* Get any element. Return MAP_OK or MAP_MISSING.
* remove - should the element be removed from the hashmap
*/
extern int hashmap_get_one(map_t in, any_t *arg, int remove);
/*
* Free the hashmap only (not data inside)
*/
extern void hashmap_free(map_t in);
/*
* Clean up the hashmap using the given function
*/
extern int hashmap_clean(map_t in, PFsingle f);
/*
* Get the current size of a hashmap
*/
extern int hashmap_length(map_t in);
/*
* Serialize the hashmap, obtaining value string from PFserialize
* NYI
*/
/* extern char *hashmap_serialize(map_t in, PFserialize); */
#endif /* __HASHMAP_H__ */