2 * $Id: linkhash.c,v 1.4 2006/01/26 02:16:28 mclark Exp $
4 * Copyright (c) 2004, 2005 Metaparadigm Pte. Ltd.
5 * Michael Clark <michael@metaparadigm.com>
7 * This library is free software; you can redistribute it and/or modify
8 * it under the terms of the MIT license. See COPYING for details.
21 void lh_abort(const char *msg, ...)
30 unsigned long lh_ptr_hash(const void *k)
32 /* CAW: refactored to be 64bit nice */
33 return (unsigned long)((((ptrdiff_t)k * LH_PRIME) >> 4) & ULONG_MAX);
36 int lh_ptr_equal(const void *k1, const void *k2)
41 unsigned long lh_char_hash(const void *k)
44 const char* data = (const char*)k;
46 while( *data!=0 ) h = h*129 + (unsigned int)(*data++) + LH_PRIME;
51 int lh_char_equal(const void *k1, const void *k2)
53 return (strcmp((const char*)k1, (const char*)k2) == 0);
56 struct lh_table* lh_table_new(int size, const char *name,
57 lh_entry_free_fn *free_fn,
59 lh_equal_fn *equal_fn)
64 t = (struct lh_table*)calloc(1, sizeof(struct lh_table));
65 if(!t) lh_abort("lh_table_new: calloc failed\n");
69 t->table = (struct lh_entry*)calloc(size, sizeof(struct lh_entry));
70 if(!t->table) lh_abort("lh_table_new: calloc failed\n");
73 t->equal_fn = equal_fn;
74 for(i = 0; i < size; i++) t->table[i].k = LH_EMPTY;
78 struct lh_table* lh_kchar_table_new(int size, const char *name,
79 lh_entry_free_fn *free_fn)
81 return lh_table_new(size, name, free_fn, lh_char_hash, lh_char_equal);
84 struct lh_table* lh_kptr_table_new(int size, const char *name,
85 lh_entry_free_fn *free_fn)
87 return lh_table_new(size, name, free_fn, lh_ptr_hash, lh_ptr_equal);
90 void lh_table_resize(struct lh_table *t, int new_size)
92 struct lh_table *new_t;
95 new_t = lh_table_new(new_size, t->name, NULL, t->hash_fn, t->equal_fn);
98 lh_table_insert(new_t, ent->k, ent->v);
102 t->table = new_t->table;
104 t->head = new_t->head;
105 t->tail = new_t->tail;
110 void lh_table_free(struct lh_table *t)
113 for(c = t->head; c != NULL; c = c->next) {
123 int lh_table_insert(struct lh_table *t, void *k, const void *v)
128 if(t->count > t->size * 0.66) lh_table_resize(t, t->size * 2);
134 if(t->table[n].k == LH_EMPTY || t->table[n].k == LH_FREED) break;
136 if(++n == t->size) n = 0;
143 if(t->head == NULL) {
144 t->head = t->tail = &t->table[n];
145 t->table[n].next = t->table[n].prev = NULL;
147 t->tail->next = &t->table[n];
148 t->table[n].prev = t->tail;
149 t->table[n].next = NULL;
150 t->tail = &t->table[n];
157 struct lh_entry* lh_table_lookup_entry(struct lh_table *t, const void *k)
159 unsigned long h = t->hash_fn(k);
160 unsigned long n = h % t->size;
164 if(t->table[n].k == LH_EMPTY) return NULL;
165 if(t->table[n].k != LH_FREED &&
166 t->equal_fn(t->table[n].k, k)) return &t->table[n];
167 if(++n == t->size) n = 0;
173 const void* lh_table_lookup(struct lh_table *t, const void *k)
175 struct lh_entry *e = lh_table_lookup_entry(t, k);
181 int lh_table_delete_entry(struct lh_table *t, struct lh_entry *e)
183 ptrdiff_t n = (ptrdiff_t)(e - t->table); /* CAW: fixed to be 64bit nice, still need the crazy negative case... */
185 /* CAW: this is bad, really bad, maybe stack goes other direction on this machine... */
186 if(n < 0) { return -2; }
188 if(t->table[n].k == LH_EMPTY || t->table[n].k == LH_FREED) return -1;
190 if(t->free_fn) t->free_fn(e);
191 t->table[n].v = NULL;
192 t->table[n].k = LH_FREED;
193 if(t->tail == &t->table[n] && t->head == &t->table[n]) {
194 t->head = t->tail = NULL;
195 } else if (t->head == &t->table[n]) {
196 t->head->next->prev = NULL;
197 t->head = t->head->next;
198 } else if (t->tail == &t->table[n]) {
199 t->tail->prev->next = NULL;
200 t->tail = t->tail->prev;
202 t->table[n].prev->next = t->table[n].next;
203 t->table[n].next->prev = t->table[n].prev;
205 t->table[n].next = t->table[n].prev = NULL;
210 int lh_table_delete(struct lh_table *t, const void *k)
212 struct lh_entry *e = lh_table_lookup_entry(t, k);
214 return lh_table_delete_entry(t, e);