-
Notifications
You must be signed in to change notification settings - Fork 5
/
linked_list_hashmap.h
156 lines (131 loc) · 2.94 KB
/
linked_list_hashmap.h
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#ifndef LINKED_LIST_HASHMAP_H
#define LINKED_LIST_HASHMAP_H
typedef unsigned long (*func_longhash_f) (const void *);
typedef long (*func_longcmp_f) (const void *, const void *);
typedef struct
{
void *key;
void *val;
} hashmap_entry_t;
typedef struct
{
int count;
int arraySize;
void *array;
func_longhash_f hash;
func_longcmp_f compare;
} hashmap_t;
typedef struct
{
int cur;
void *cur_linked;
} hashmap_iterator_t;
hashmap_t *hashmap_new(
func_longhash_f hash,
func_longcmp_f cmp,
unsigned int initial_capacity
);
/**
* @return number of items within hash */
int hashmap_count(const hashmap_t * hmap);
/**
* @return size of the array used within hash */
int hashmap_size(
hashmap_t * hmap
);
/**
* Empty this hash. */
void hashmap_clear(
hashmap_t * hmap
);
/**
* Free all the memory related to this hash. */
void hashmap_free(
hashmap_t * hmap
);
/**
* Free all the memory related to this hash.
* This includes the actual h itself. */
void hashmap_freeall(
hashmap_t * hmap
);
/**
* Get this key's value.
* @return key's item, otherwise NULL */
void *hashmap_get(
hashmap_t * hmap,
const void *key
);
/**
* Is this key inside this map?
* @return 1 if key is in hash, otherwise 0 */
int hashmap_contains_key(
hashmap_t * hmap,
const void *key
);
/**
* Remove the value refrenced by this key from the hash. */
void hashmap_remove_entry(
hashmap_t * hmap,
hashmap_entry_t * entry,
const void *key
);
/**
* Remove this key and value from the map.
* @return value of key, or NULL on failure */
void *hashmap_remove(
hashmap_t * hmap,
const void *key
);
/**
* Associate key with val.
* Does not insert key if an equal key exists.
* @return previous associated val; otherwise NULL */
void *hashmap_put(
hashmap_t * hmap,
void *key,
void *val
);
/**
* Put this key/value entry into the hash */
void hashmap_put_entry(
hashmap_t * hmap,
hashmap_entry_t * entry
);
void* hashmap_iterator_peek(
hashmap_t * hmap,
hashmap_iterator_t * iter);
void* hashmap_iterator_peek_value(
hashmap_t * hmap,
hashmap_iterator_t * iter);
int hashmap_iterator_has_next(
hashmap_t * hmap,
hashmap_iterator_t * iter
);
/**
* Iterate to the next item on a hash iterator
* @return next item key from iterator */
void *hashmap_iterator_next(
hashmap_t * hmap,
hashmap_iterator_t * iter
);
/**
* Iterate to the next item on a hash iterator
* @return next item value from iterator */
void *hashmap_iterator_next_value(
hashmap_t * hmap,
hashmap_iterator_t * iter);
/**
* Initialise a new hash iterator over this hash
* It is safe to remove items while iterating. */
void hashmap_iterator(
hashmap_t * hmap,
hashmap_iterator_t * iter
);
/**
* Increase hash capacity.
* @param factor : increase by this factor */
void hashmap_increase_capacity(
hashmap_t * hmap,
unsigned int factor);
#endif /* LINKED_LIST_HASHMAP_H */