4 * Copyright Metaparadigm Pte. Ltd. 2004.
5 * Michael Clark <michael@metaparadigm.com>
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public (LGPL)
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details: http://www.gnu.org/
24 #include "libjson/linkhash.h"
27 void lh_abort(const char *msg, ...)
36 unsigned long lh_ptr_hash(void *k)
38 return ((long)k * LH_PRIME) >> 4;
42 int lh_ptr_equal(void *k1, void *k2)
48 unsigned long lh_char_hash(void *k)
53 while( *data!=0 ) h = h*129 + (unsigned int)(*data++) + LH_PRIME;
59 int lh_char_equal(void *k1, void *k2)
61 return (strcmp((char*)k1, (char*)k2) == 0);
65 struct lh_table* lh_table_new(int size, char *name,
66 lh_entry_free_fn *free_fn,
68 lh_equal_fn *equal_fn)
73 t = calloc(1, sizeof(struct lh_table));
74 if(!t) lh_abort("lh_table_new: calloc failed\n");
78 t->table = calloc(size, sizeof(struct lh_entry));
79 if(!t->table) lh_abort("lh_table_new: calloc failed\n");
82 t->equal_fn = equal_fn;
83 for(i = 0; i < size; i++) t->table[i].k = LH_EMPTY;
88 struct lh_table* lh_kchar_table_new(int size, char *name,
89 lh_entry_free_fn *free_fn)
91 return lh_table_new(size, name, free_fn, lh_char_hash, lh_char_equal);
95 struct lh_table* lh_kptr_table_new(int size, char *name,
96 lh_entry_free_fn *free_fn)
98 return lh_table_new(size, name, free_fn, lh_ptr_hash, lh_ptr_equal);
102 void lh_table_resize(struct lh_table *t, int new_size)
104 struct lh_table *new_t;
105 struct lh_entry *ent;
107 new_t = lh_table_new(new_size, t->name, NULL, t->hash_fn, t->equal_fn);
110 lh_table_insert(new_t, ent->k, ent->v);
114 t->table = new_t->table;
116 t->head = new_t->head;
117 t->tail = new_t->tail;
123 void lh_table_free(struct lh_table *t)
126 for(c = t->head; c != NULL; c = c->next) {
136 int lh_table_insert(struct lh_table *t, void *k, void *v)
141 if(t->count > t->size * 0.66) lh_table_resize(t, t->size * 2);
147 if(t->table[n].k == LH_EMPTY || t->table[n].k == LH_FREED) break;
149 if(++n == t->size) n = 0;
156 if(t->head == NULL) {
157 t->head = t->tail = &t->table[n];
158 t->table[n].next = t->table[n].prev = NULL;
160 t->tail->next = &t->table[n];
161 t->table[n].prev = t->tail;
162 t->table[n].next = NULL;
163 t->tail = &t->table[n];
170 struct lh_entry* lh_table_lookup_entry(struct lh_table *t, void *k)
172 unsigned long h = t->hash_fn(k);
173 unsigned long n = h % t->size;
177 if(t->table[n].k == LH_EMPTY) return NULL;
178 if(t->table[n].k != LH_FREED &&
179 t->equal_fn(t->table[n].k, k)) return &t->table[n];
180 if(++n == t->size) n = 0;
186 void* lh_table_lookup(struct lh_table *t, void *k)
188 struct lh_entry *e = lh_table_lookup_entry(t, k);
194 int lh_table_delete_entry(struct lh_table *t, struct lh_entry *e)
196 int n = e - t->table;
197 if(t->table[n].k == LH_EMPTY || t->table[n].k == LH_FREED) return -1;
199 if(t->free_fn) t->free_fn(e);
200 t->table[n].v = NULL;
201 t->table[n].k = LH_FREED;
202 if(t->tail == &t->table[n] && t->head == &t->table[n]) {
203 t->head = t->tail = NULL;
204 } else if (t->head == &t->table[n]) {
205 t->head->next->prev = NULL;
206 t->head = t->head->next;
207 } else if (t->tail == &t->table[n]) {
208 t->tail->prev->next = NULL;
209 t->tail = t->tail->prev;
211 t->table[n].prev->next = t->table[n].next;
212 t->table[n].next->prev = t->table[n].prev;
214 t->table[n].next = t->table[n].prev = NULL;
219 int lh_table_delete(struct lh_table *t, void *k)
221 struct lh_entry *e = lh_table_lookup_entry(t, k);
223 return lh_table_delete_entry(t, e);