1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
|
/* hashtable.c 0.1 1999/1/16 Tom Wheeley <tomw@tsys.demon.co.uk> */
/* This code is placed under the GNU Public Licence */
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include "chunk.h"
#include "hashtable.h"
static size_t ht_hash_code(char *key, unsigned max)
{
register unsigned long g;
register unsigned long h;
if (!key) return 0;
h = 0;
while (*key) {
h = (h << 4) + *key++;
if ((g = h & 0xf0000000))
h ^= g >> 24;
h &= ~g;
}
return h % max;
}
ht_t *ht_new(size_t size)
{
ht_t *ht;
if (size <= 0)
return NULL;
ht = malloc(sizeof (ht_t));
if (!ht)
return NULL;
ht->size = size;
ht->table = calloc(size, sizeof (ht_entry_t *));
if (!ht->table) {
free(ht);
return NULL;
}
ht->chunk = ch_new(size * sizeof (ht_entry_t));
if (!ht->chunk) {
free(ht->table);
free(ht);
return NULL;
}
return ht;
}
int ht_add(char *key, void *data, ht_t *ht)
{
size_t hashval;
ht_entry_t *node;
if (!ht || !ht->table || ht->size <= 0) {
errno = ENOMEM;
return -1;
}
hashval = ht_hash_code(key, ht->size);
/* check to see if this already exists. Waste of time? If we remove this
* check then a sort of `stack' would be created. Hmm, sounds good
*/
#if 0
for (node=ht->table[hashval]; node; node=node->next) {
if (!strcmp(key, node->key)) {
errno = EBUSY; /* key in use */
return -1;
}
}
#endif
node = ch_malloc(sizeof (ht_entry_t), ht->chunk);
if (!node) {
return -1;
}
/* add to the beginning of the chain - easiest way and gives us this
* nifty stack type mode
*/
node->key = key;
node->data = data;
node->next = ht->table[hashval];
ht->table[hashval] = node;
return 0;
}
void *ht_match(char *key, ht_t *ht)
{
ht_entry_t *node;
unsigned hashval;
if (!ht || !ht->table) {
return NULL;
}
hashval = ht_hash_code(key, ht->size);
for (node = ht->table[hashval]; node; node = node->next) {
if (!strcmp(key, node->key)) {
return node->data;
}
}
errno = ENOENT;
return NULL;
}
int ht_remove(char *key, ht_t *ht)
{
ht_entry_t **pn;
unsigned hashval;
if (!ht || !ht->table) {
return -1;
}
hashval = ht_hash_code(key, ht->size);
for (pn = &(ht->table[hashval]); *pn; pn = &((*pn)->next)) {
if (!strcmp(key, (*pn)->key)) {
(*pn) = (*pn)->next;
return 0;
}
}
errno = ENOENT;
return -1;
}
int ht_update(char *key, void *data, ht_t *ht)
{
while (ht_match(key, ht) != NULL) {
ht_remove(key, ht);
}
return ht_add(key, data, ht);
}
void ht_free(ht_t **ht)
{
if (!ht || !(*ht))
return;
if ((*ht)->table)
free((*ht)->table);
if ((*ht)->chunk)
ch_free((*ht)->chunk);
free((*ht));
(*ht) = NULL;
}
/* end of hashtable.c */
|