aboutsummaryrefslogtreecommitdiff
path: root/hashtab.c
diff options
context:
space:
mode:
Diffstat (limited to 'hashtab.c')
-rw-r--r--hashtab.c16
1 files changed, 16 insertions, 0 deletions
diff --git a/hashtab.c b/hashtab.c
index 8a49baf..9f4e4a8 100644
--- a/hashtab.c
+++ b/hashtab.c
@@ -6,6 +6,7 @@
6 6
7#include "util.h" 7#include "util.h"
8 8
9/* --8<-- hash_type */
9typedef int T; 10typedef int T;
10 11
11struct hash_item { 12struct hash_item {
@@ -17,7 +18,9 @@ struct hash_item {
17struct hash_tab { 18struct hash_tab {
18 struct hash_item *table[251]; // fit for your needs... 19 struct hash_item *table[251]; // fit for your needs...
19}; 20};
21/* -->8-- */
20 22
23/* --8<-- hash_key */
21static unsigned long 24static unsigned long
22hash_key(const unsigned char *str) 25hash_key(const unsigned char *str)
23{ 26{
@@ -29,14 +32,18 @@ hash_key(const unsigned char *str)
29 32
30 return hash; 33 return hash;
31} 34}
35/* -->8-- */
32 36
37/* --8<-- hash_init */
33void 38void
34hash_init(struct hash_tab *ht) 39hash_init(struct hash_tab *ht)
35{ 40{
36 for ( size_t i = 0; i != NELEM(ht->table); ++i ) 41 for ( size_t i = 0; i != NELEM(ht->table); ++i )
37 ht->table[i] = NULL; 42 ht->table[i] = NULL;
38} 43}
44/* -->8-- */
39 45
46/* --8<-- hash_add */
40static struct hash_item * 47static struct hash_item *
41hash_add(struct hash_item *next, const char *key, T data) 48hash_add(struct hash_item *next, const char *key, T data)
42{ 49{
@@ -59,7 +66,9 @@ hash_add(struct hash_item *next, const char *key, T data)
59 66
60 return new_item; 67 return new_item;
61} 68}
69/* -->8-- */
62 70
71/* --8<-- hash_lookup */
63T * 72T *
64hash_lookup(struct hash_tab *ht, const char *key, T data, int create) 73hash_lookup(struct hash_tab *ht, const char *key, T data, int create)
65{ 74{
@@ -82,7 +91,9 @@ hash_lookup(struct hash_tab *ht, const char *key, T data, int create)
82 91
83 return item ? &item->data : NULL; 92 return item ? &item->data : NULL;
84} 93}
94/* -->8-- */
85 95
96/* --8<-- hash_delete */
86bool 97bool
87hash_delete(struct hash_tab *ht, const char *key) 98hash_delete(struct hash_tab *ht, const char *key)
88{ 99{
@@ -108,7 +119,9 @@ hash_delete(struct hash_tab *ht, const char *key)
108 119
109 return false; 120 return false;
110} 121}
122/* -->8-- */
111 123
124/* --8<-- hash_apply */
112void 125void
113hash_apply(struct hash_tab *ht, void (*visit)(const char *key, T data, void *cl), void *cl) 126hash_apply(struct hash_tab *ht, void (*visit)(const char *key, T data, void *cl), void *cl)
114{ 127{
@@ -118,7 +131,9 @@ hash_apply(struct hash_tab *ht, void (*visit)(const char *key, T data, void *cl)
118 for ( item = ht->table[i]; item; item = item->next ) 131 for ( item = ht->table[i]; item; item = item->next )
119 visit(item->key, item->data, cl); 132 visit(item->key, item->data, cl);
120} 133}
134/* -->8-- */
121 135
136/* --8<-- hash_free */
122void 137void
123hash_free(struct hash_tab *ht) 138hash_free(struct hash_tab *ht)
124{ 139{
@@ -133,6 +148,7 @@ hash_free(struct hash_tab *ht)
133 ht->table[i] = NULL; 148 ht->table[i] = NULL;
134 } 149 }
135} 150}
151/* -->8-- */
136 152
137static int 153static int
138getword(FILE *fp, char *buf, size_t size, int first(int), int rest(int)) 154getword(FILE *fp, char *buf, size_t size, int first(int), int rest(int))