| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
#include <stdlib.h> |
| 22 |
#include <string.h> |
| 23 |
|
| 24 |
#include "collection.h" |
| 25 |
#include "logging.h" |
| 26 |
#include "string-macros.h" |
| 27 |
#include "xalloc.h" |
| 28 |
|
| 29 |
#define HASHSIZE 101 |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
static unsigned |
| 34 |
hash (const char *s) |
| 35 |
{ |
| 36 |
unsigned hashval; |
| 37 |
|
| 38 |
for (hashval = 0; *s != '\0'; s++) |
| 39 |
hashval = *s + 31 * hashval; |
| 40 |
|
| 41 |
return hashval % HASHSIZE; |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
hashtable_t * |
| 47 |
counter_create (void) |
| 48 |
{ |
| 49 |
hashtable_t *hashtable = xmalloc (sizeof (hashtable_t)); |
| 50 |
hashable_t **table = xnmalloc (HASHSIZE, sizeof (hashable_t *)); |
| 51 |
|
| 52 |
hashtable->capacity = HASHSIZE; |
| 53 |
hashtable->elements = hashtable->uniq = 0; |
| 54 |
hashtable->table = table; |
| 55 |
hashtable->keys = xmalloc (sizeof (*(hashtable->keys))); |
| 56 |
|
| 57 |
return hashtable; |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
hashable_t * |
| 63 |
counter_lookup (const hashtable_t * hashtable, const char *key) |
| 64 |
{ |
| 65 |
hashable_t *np; |
| 66 |
|
| 67 |
dbg ("hashtable lookup for key \"%s\"\n", key); |
| 68 |
for (np = hashtable->table[hash (key)]; np != NULL; np = np->next) |
| 69 |
if (STREQ (key, np->key)) |
| 70 |
{ |
| 71 |
dbg ("hashtable lookup: found key \"%s\"\n", key); |
| 72 |
return np; |
| 73 |
} |
| 74 |
|
| 75 |
dbg ("hashtable lookup: key \"%s\" not found\n", key); |
| 76 |
return NULL; |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
hashable_t * |
| 84 |
counter_put (hashtable_t * hashtable, const char *key, unsigned long increment) |
| 85 |
{ |
| 86 |
hashable_t *np; |
| 87 |
|
| 88 |
if ((np = counter_lookup (hashtable, key)) == NULL) |
| 89 |
{ |
| 90 |
dbg ("hashtable: adding a new key \"%s\"\n", key); |
| 91 |
np = xmalloc (sizeof (*np)); |
| 92 |
np->key = xstrdup (key); |
| 93 |
|
| 94 |
unsigned int hashval = hash (key); |
| 95 |
np->count = increment; |
| 96 |
np->next = hashtable->table[hashval]; |
| 97 |
hashtable->table[hashval] = np; |
| 98 |
|
| 99 |
unsigned long new_size = |
| 100 |
sizeof (*(hashtable->keys)) * (hashtable->uniq + 1); |
| 101 |
hashtable->keys = xrealloc (hashtable->keys, new_size); |
| 102 |
dbg ("xrealloc'ed hashtable->keys to %lu bytes\n", new_size); |
| 103 |
|
| 104 |
(hashtable->keys)[hashtable->uniq] = np->key; |
| 105 |
dbg ("(hashtable->keys)[%u] = \"%s\"\n", hashtable->uniq, |
| 106 |
(hashtable->keys)[hashtable->uniq]); |
| 107 |
|
| 108 |
hashtable->uniq++; |
| 109 |
} |
| 110 |
else |
| 111 |
{ |
| 112 |
|
| 113 |
dbg ("hashtable: the key \"%s\" is already present, " |
| 114 |
"counter incremented: %lu\n", key, np->count); |
| 115 |
np->count += increment; |
| 116 |
} |
| 117 |
|
| 118 |
hashtable->elements++; |
| 119 |
|
| 120 |
return np; |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
unsigned int |
| 126 |
counter_get_elements (const hashtable_t * hashtable) |
| 127 |
{ |
| 128 |
return hashtable->elements; |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
unsigned int |
| 134 |
counter_get_unique_elements (const hashtable_t * hashtable) |
| 135 |
{ |
| 136 |
return hashtable->uniq; |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
char ** |
| 143 |
counter_keys (hashtable_t * hashtable) |
| 144 |
{ |
| 145 |
return (hashtable->uniq < 1) ? NULL : hashtable->keys; |
| 146 |
} |
| 147 |
|
| 148 |
void |
| 149 |
counter_free (hashtable_t * hashtable) |
| 150 |
{ |
| 151 |
hashable_t *np, *np2; |
| 152 |
|
| 153 |
for (int i = 0; i < HASHSIZE; i++) |
| 154 |
{ |
| 155 |
np = hashtable->table[i]; |
| 156 |
while (np != NULL) |
| 157 |
{ |
| 158 |
np2 = np; |
| 159 |
np = np->next; |
| 160 |
free (np2); |
| 161 |
} |
| 162 |
} |
| 163 |
free (hashtable->table); |
| 164 |
free (hashtable->keys); |
| 165 |
free (hashtable); |
| 166 |
} |