Skip to content

Commit a24c758

Browse files
committed
change remaining helper functions to static
1 parent 4032f77 commit a24c758

File tree

2 files changed

+29
-33
lines changed

2 files changed

+29
-33
lines changed

src/zhash.c

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,12 @@
33
#include "./zhash.h"
44

55
// 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);
812
static struct ZHashTable *zcreate_hash_table_with_size(size_t size_index);
913
static void *zmalloc(size_t size);
1014
static void *zcalloc(size_t num, size_t size);
@@ -21,19 +25,6 @@ struct ZHashTable *zcreate_hash_table(void)
2125
return zcreate_hash_table_with_size(0);
2226
}
2327

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-
3728
void zfree_hash_table(struct ZHashTable *hash_table)
3829
{
3930
size_t size, ii;
@@ -75,7 +66,7 @@ void zhash_set(struct ZHashTable *hash_table, char *key, void *val)
7566
size = hash_sizes[hash_table->size_index];
7667

7768
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));
7970
}
8071
}
8172

@@ -126,7 +117,7 @@ void *zhash_delete(struct ZHashTable *hash_table, char *key)
126117
size = hash_sizes[hash_table->size_index];
127118

128119
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));
130121
}
131122

132123
return val;
@@ -145,7 +136,20 @@ bool zhash_exists(struct ZHashTable *hash_table, char *key)
145136
return entry ? true : false;
146137
}
147138

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)
149153
{
150154
struct ZHashEntry *entry;
151155
char *key_cpy;
@@ -160,15 +164,15 @@ struct ZHashEntry *zcreate_entry(char *key, void *val)
160164
return entry;
161165
}
162166

163-
void zfree_entry(struct ZHashEntry *entry, bool recursive)
167+
static void zfree_entry(struct ZHashEntry *entry, bool recursive)
164168
{
165169
if (recursive && entry->next) zfree_entry(entry->next, recursive);
166170

167171
zfree(entry->key);
168172
zfree(entry);
169173
}
170174

171-
size_t zgenerate_hash(struct ZHashTable *hash_table, char *key)
175+
static size_t zgenerate_hash(struct ZHashTable *hash_table, char *key)
172176
{
173177
size_t size, hash;
174178
char ch;
@@ -181,7 +185,7 @@ size_t zgenerate_hash(struct ZHashTable *hash_table, char *key)
181185
return hash;
182186
}
183187

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)
185189
{
186190
size_t hash, size, ii;
187191
struct ZHashEntry **entries;
@@ -213,14 +217,14 @@ void zhash_rehash(struct ZHashTable *hash_table, size_t size_index)
213217
zfree(entries);
214218
}
215219

216-
static size_t next_size_index(size_t size_index)
220+
static size_t znext_size_index(size_t size_index)
217221
{
218-
if (size_index == COUNT_OF(hash_sizes)) return size_index;
222+
if (size_index == ZCOUNT_OF(hash_sizes)) return size_index;
219223

220224
return size_index + 1;
221225
}
222226

223-
static size_t previous_size_index(size_t size_index)
227+
static size_t zprevious_size_index(size_t size_index)
224228
{
225229
if (size_index == 0) return size_index;
226230

src/zhash.h

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
// keys are strings
88
// values are void *pointers
99

10-
#define COUNT_OF(arr) (sizeof(arr) / sizeof(*arr))
10+
#define ZCOUNT_OF(arr) (sizeof(arr) / sizeof(*arr))
1111
#define zfree free
1212

1313
// struct representing an entry in the hash table
@@ -35,12 +35,4 @@ void *zhash_get(struct ZHashTable *hash_table, char *key);
3535
void *zhash_delete(struct ZHashTable *hash_table, char *key);
3636
bool zhash_exists(struct ZHashTable *hash_table, char *key);
3737

38-
// hash entry creation and destruction
39-
struct ZHashEntry *zcreate_entry(char *key, void *val);
40-
void zfree_entry(struct ZHashEntry *entry, bool recursive);
41-
42-
// other functions
43-
size_t zgenerate_hash(struct ZHashTable *hash, char *key);
44-
void zhash_rehash(struct ZHashTable *hash_table, size_t size_index);
45-
4638
#endif

0 commit comments

Comments
 (0)