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>
6 * Copyright (c) 2009 Hewlett-Packard Development Company, L.P.
8 * This library is free software; you can redistribute it and/or modify
9 * it under the terms of the MIT license. See COPYING for details.
22 void lh_abort(const char *msg, ...)
31 unsigned long lh_ptr_hash(const void *k)
33 /* CAW: refactored to be 64bit nice */
34 return (unsigned long)((((ptrdiff_t)k * LH_PRIME) >> 4) & ULONG_MAX);
37 int lh_ptr_equal(const void *k1, const void *k2)
42 unsigned long lh_char_hash(const void *k)
45 const char* data = (const char*)k;
47 while( *data!=0 ) h = h*129 + (unsigned int)(*data++) + LH_PRIME;
52 int lh_char_equal(const void *k1, const void *k2)
54 return (strcmp((const char*)k1, (const char*)k2) == 0);
57 struct lh_table* lh_table_new(int size, const char *name,
58 lh_entry_free_fn *free_fn,
60 lh_equal_fn *equal_fn)
65 t = (struct lh_table*)calloc(1, sizeof(struct lh_table));
66 if(!t) lh_abort("lh_table_new: calloc failed\n");
70 t->table = (struct lh_entry*)calloc(size, sizeof(struct lh_entry));
71 if(!t->table) lh_abort("lh_table_new: calloc failed\n");
74 t->equal_fn = equal_fn;
75 for(i = 0; i < size; i++) t->table[i].k = LH_EMPTY;
79 struct lh_table* lh_kchar_table_new(int size, const char *name,
80 lh_entry_free_fn *free_fn)
82 return lh_table_new(size, name, free_fn, lh_char_hash, lh_char_equal);
85 struct lh_table* lh_kptr_table_new(int size, const char *name,
86 lh_entry_free_fn *free_fn)
88 return lh_table_new(size, name, free_fn, lh_ptr_hash, lh_ptr_equal);
91 void lh_table_resize(struct lh_table *t, int new_size)
93 struct lh_table *new_t;
96 new_t = lh_table_new(new_size, t->name, NULL, t->hash_fn, t->equal_fn);
99 lh_table_insert(new_t, ent->k, ent->v);
103 t->table = new_t->table;
105 t->head = new_t->head;
106 t->tail = new_t->tail;
111 void lh_table_free(struct lh_table *t)
114 for(c = t->head; c != NULL; c = c->next) {
124 int lh_table_insert(struct lh_table *t, void *k, const void *v)
129 if(t->count >= t->size * LH_LOAD_FACTOR) lh_table_resize(t, t->size * 2);
135 if(t->table[n].k == LH_EMPTY || t->table[n].k == LH_FREED) break;
137 if(++n == t->size) n = 0;
144 if(t->head == NULL) {
145 t->head = t->tail = &t->table[n];
146 t->table[n].next = t->table[n].prev = NULL;
148 t->tail->next = &t->table[n];
149 t->table[n].prev = t->tail;
150 t->table[n].next = NULL;
151 t->tail = &t->table[n];
158 struct lh_entry* lh_table_lookup_entry(struct lh_table *t, const void *k)
160 unsigned long h = t->hash_fn(k);
161 unsigned long n = h % t->size;
165 while( count < t->size ) {
166 if(t->table[n].k == LH_EMPTY) return NULL;
167 if(t->table[n].k != LH_FREED &&
168 t->equal_fn(t->table[n].k, k)) return &t->table[n];
169 if(++n == t->size) n = 0;
176 const void* lh_table_lookup(struct lh_table *t, const void *k)
179 lh_table_lookup_ex(t, k, &result);
183 json_bool lh_table_lookup_ex(struct lh_table* t, const void* k, void **v)
185 struct lh_entry *e = lh_table_lookup_entry(t, k);
187 if (v != NULL) *v = (void *)e->v;
188 return TRUE; /* key found */
190 if (v != NULL) *v = NULL;
191 return FALSE; /* key not found */
194 int lh_table_delete_entry(struct lh_table *t, struct lh_entry *e)
196 ptrdiff_t n = (ptrdiff_t)(e - t->table); /* CAW: fixed to be 64bit nice, still need the crazy negative case... */
198 /* CAW: this is bad, really bad, maybe stack goes other direction on this machine... */
199 if(n < 0) { return -2; }
201 if(t->table[n].k == LH_EMPTY || t->table[n].k == LH_FREED) return -1;
203 if(t->free_fn) t->free_fn(e);
204 t->table[n].v = NULL;
205 t->table[n].k = LH_FREED;
206 if(t->tail == &t->table[n] && t->head == &t->table[n]) {
207 t->head = t->tail = NULL;
208 } else if (t->head == &t->table[n]) {
209 t->head->next->prev = NULL;
210 t->head = t->head->next;
211 } else if (t->tail == &t->table[n]) {
212 t->tail->prev->next = NULL;
213 t->tail = t->tail->prev;
215 t->table[n].prev->next = t->table[n].next;
216 t->table[n].next->prev = t->table[n].prev;
218 t->table[n].next = t->table[n].prev = NULL;
223 int lh_table_delete(struct lh_table *t, const void *k)
225 struct lh_entry *e = lh_table_lookup_entry(t, k);
227 return lh_table_delete_entry(t, e);