diff options
Diffstat (limited to 'hashtab.c')
| -rw-r--r-- | hashtab.c | 16 |
1 files changed, 16 insertions, 0 deletions
| @@ -6,6 +6,7 @@ | |||
| 6 | 6 | ||
| 7 | #include "util.h" | 7 | #include "util.h" |
| 8 | 8 | ||
| 9 | /* --8<-- hash_type */ | ||
| 9 | typedef int T; | 10 | typedef int T; |
| 10 | 11 | ||
| 11 | struct hash_item { | 12 | struct hash_item { |
| @@ -17,7 +18,9 @@ struct hash_item { | |||
| 17 | struct hash_tab { | 18 | struct 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 */ | ||
| 21 | static unsigned long | 24 | static unsigned long |
| 22 | hash_key(const unsigned char *str) | 25 | hash_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 */ | ||
| 33 | void | 38 | void |
| 34 | hash_init(struct hash_tab *ht) | 39 | hash_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 */ | ||
| 40 | static struct hash_item * | 47 | static struct hash_item * |
| 41 | hash_add(struct hash_item *next, const char *key, T data) | 48 | hash_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 */ | ||
| 63 | T * | 72 | T * |
| 64 | hash_lookup(struct hash_tab *ht, const char *key, T data, int create) | 73 | hash_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 */ | ||
| 86 | bool | 97 | bool |
| 87 | hash_delete(struct hash_tab *ht, const char *key) | 98 | hash_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 */ | ||
| 112 | void | 125 | void |
| 113 | hash_apply(struct hash_tab *ht, void (*visit)(const char *key, T data, void *cl), void *cl) | 126 | hash_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 */ | ||
| 122 | void | 137 | void |
| 123 | hash_free(struct hash_tab *ht) | 138 | hash_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 | ||
| 137 | static int | 153 | static int |
| 138 | getword(FILE *fp, char *buf, size_t size, int first(int), int rest(int)) | 154 | getword(FILE *fp, char *buf, size_t size, int first(int), int rest(int)) |
