3
3
#include "./zhash.h"
4
4
5
5
// helper functions
6
- static size_t next_size_index (size_t size_index );
7
- static size_t previous_size_index (size_t size_index );
6
+ static struct ZHashEntry * zcreate_entry (char * key , void * val );
7
+ static void zfree_entry (struct ZHashEntry * entry , bool recursive );
8
+ static size_t zgenerate_hash (struct ZHashTable * hash_table , char * key );
9
+ static void zhash_rehash (struct ZHashTable * hash_table , size_t size_index );
10
+ static size_t znext_size_index (size_t size_index );
11
+ static size_t zprevious_size_index (size_t size_index );
8
12
static struct ZHashTable * zcreate_hash_table_with_size (size_t size_index );
9
13
static void * zmalloc (size_t size );
10
14
static void * zcalloc (size_t num , size_t size );
@@ -21,19 +25,6 @@ struct ZHashTable *zcreate_hash_table(void)
21
25
return zcreate_hash_table_with_size (0 );
22
26
}
23
27
24
- static struct ZHashTable * zcreate_hash_table_with_size (size_t size_index )
25
- {
26
- struct ZHashTable * hash_table ;
27
-
28
- hash_table = zmalloc (sizeof (struct ZHashTable ));
29
-
30
- hash_table -> size_index = size_index ;
31
- hash_table -> entry_count = 0 ;
32
- hash_table -> entries = zcalloc (hash_sizes [size_index ], sizeof (void * ));
33
-
34
- return hash_table ;
35
- }
36
-
37
28
void zfree_hash_table (struct ZHashTable * hash_table )
38
29
{
39
30
size_t size , ii ;
@@ -75,7 +66,7 @@ void zhash_set(struct ZHashTable *hash_table, char *key, void *val)
75
66
size = hash_sizes [hash_table -> size_index ];
76
67
77
68
if (hash_table -> entry_count > size / 2 ) {
78
- zhash_rehash (hash_table , next_size_index (hash_table -> size_index ));
69
+ zhash_rehash (hash_table , znext_size_index (hash_table -> size_index ));
79
70
}
80
71
}
81
72
@@ -126,7 +117,7 @@ void *zhash_delete(struct ZHashTable *hash_table, char *key)
126
117
size = hash_sizes [hash_table -> size_index ];
127
118
128
119
if (hash_table -> entry_count < size / 8 ) {
129
- zhash_rehash (hash_table , previous_size_index (hash_table -> size_index ));
120
+ zhash_rehash (hash_table , zprevious_size_index (hash_table -> size_index ));
130
121
}
131
122
132
123
return val ;
@@ -145,7 +136,20 @@ bool zhash_exists(struct ZHashTable *hash_table, char *key)
145
136
return entry ? true : false;
146
137
}
147
138
148
- struct ZHashEntry * zcreate_entry (char * key , void * val )
139
+ static struct ZHashTable * zcreate_hash_table_with_size (size_t size_index )
140
+ {
141
+ struct ZHashTable * hash_table ;
142
+
143
+ hash_table = zmalloc (sizeof (struct ZHashTable ));
144
+
145
+ hash_table -> size_index = size_index ;
146
+ hash_table -> entry_count = 0 ;
147
+ hash_table -> entries = zcalloc (hash_sizes [size_index ], sizeof (void * ));
148
+
149
+ return hash_table ;
150
+ }
151
+
152
+ static struct ZHashEntry * zcreate_entry (char * key , void * val )
149
153
{
150
154
struct ZHashEntry * entry ;
151
155
char * key_cpy ;
@@ -160,15 +164,15 @@ struct ZHashEntry *zcreate_entry(char *key, void *val)
160
164
return entry ;
161
165
}
162
166
163
- void zfree_entry (struct ZHashEntry * entry , bool recursive )
167
+ static void zfree_entry (struct ZHashEntry * entry , bool recursive )
164
168
{
165
169
if (recursive && entry -> next ) zfree_entry (entry -> next , recursive );
166
170
167
171
zfree (entry -> key );
168
172
zfree (entry );
169
173
}
170
174
171
- size_t zgenerate_hash (struct ZHashTable * hash_table , char * key )
175
+ static size_t zgenerate_hash (struct ZHashTable * hash_table , char * key )
172
176
{
173
177
size_t size , hash ;
174
178
char ch ;
@@ -181,7 +185,7 @@ size_t zgenerate_hash(struct ZHashTable *hash_table, char *key)
181
185
return hash ;
182
186
}
183
187
184
- void zhash_rehash (struct ZHashTable * hash_table , size_t size_index )
188
+ static void zhash_rehash (struct ZHashTable * hash_table , size_t size_index )
185
189
{
186
190
size_t hash , size , ii ;
187
191
struct ZHashEntry * * entries ;
@@ -213,14 +217,14 @@ void zhash_rehash(struct ZHashTable *hash_table, size_t size_index)
213
217
zfree (entries );
214
218
}
215
219
216
- static size_t next_size_index (size_t size_index )
220
+ static size_t znext_size_index (size_t size_index )
217
221
{
218
- if (size_index == COUNT_OF (hash_sizes )) return size_index ;
222
+ if (size_index == ZCOUNT_OF (hash_sizes )) return size_index ;
219
223
220
224
return size_index + 1 ;
221
225
}
222
226
223
- static size_t previous_size_index (size_t size_index )
227
+ static size_t zprevious_size_index (size_t size_index )
224
228
{
225
229
if (size_index == 0 ) return size_index ;
226
230
0 commit comments