blob: fc72bf667e90782092a849adf4f5cae9d25848b5 [file] [log] [blame]
Michael Clarkf0d08882007-03-13 08:26:18 +00001/*
Michael Clarkf6a6e482007-03-13 08:26:23 +00002 * $Id: linkhash.c,v 1.4 2006/01/26 02:16:28 mclark Exp $
Michael Clarkf0d08882007-03-13 08:26:18 +00003 *
Michael Clarkf6a6e482007-03-13 08:26:23 +00004 * Copyright (c) 2004, 2005 Metaparadigm Pte. Ltd.
Michael Clarkf0d08882007-03-13 08:26:18 +00005 * Michael Clark <michael@metaparadigm.com>
6 *
Michael Clarkf6a6e482007-03-13 08:26:23 +00007 * 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.
Michael Clarkf0d08882007-03-13 08:26:18 +00009 *
10 */
11
Michael Clark4504df72007-03-13 08:26:20 +000012#include "config.h"
13
Michael Clarkf0d08882007-03-13 08:26:18 +000014#include <stdio.h>
15#include <string.h>
16#include <stdlib.h>
17#include <stdarg.h>
Michael Clark4504df72007-03-13 08:26:20 +000018#include <stddef.h>
19#include <limits.h>
Michael Clarkf0d08882007-03-13 08:26:18 +000020
21#include "linkhash.h"
22
Michael Clarkf0d08882007-03-13 08:26:18 +000023void lh_abort(const char *msg, ...)
24{
25 va_list ap;
26 va_start(ap, msg);
27 vprintf(msg, ap);
Michael Clark8cdac642009-01-05 03:57:59 +000028 va_end(ap);
Michael Clarkf0d08882007-03-13 08:26:18 +000029 exit(1);
30}
31
Michael Clarkf0d08882007-03-13 08:26:18 +000032unsigned long lh_ptr_hash(void *k)
33{
Michael Clark4504df72007-03-13 08:26:20 +000034 /* CAW: refactored to be 64bit nice */
35 return (unsigned long)((((ptrdiff_t)k * LH_PRIME) >> 4) & ULONG_MAX);
Michael Clarkf0d08882007-03-13 08:26:18 +000036}
37
Michael Clarkf0d08882007-03-13 08:26:18 +000038int lh_ptr_equal(void *k1, void *k2)
39{
40 return (k1 == k2);
41}
42
Michael Clarkf0d08882007-03-13 08:26:18 +000043unsigned long lh_char_hash(void *k)
44{
45 unsigned int h = 0;
46 const char* data = k;
47
48 while( *data!=0 ) h = h*129 + (unsigned int)(*data++) + LH_PRIME;
49
50 return h;
51}
52
Michael Clarkf0d08882007-03-13 08:26:18 +000053int lh_char_equal(void *k1, void *k2)
54{
55 return (strcmp((char*)k1, (char*)k2) == 0);
56}
57
Michael Clarkf0d08882007-03-13 08:26:18 +000058struct lh_table* lh_table_new(int size, char *name,
59 lh_entry_free_fn *free_fn,
60 lh_hash_fn *hash_fn,
61 lh_equal_fn *equal_fn)
62{
63 int i;
64 struct lh_table *t;
65
66 t = calloc(1, sizeof(struct lh_table));
67 if(!t) lh_abort("lh_table_new: calloc failed\n");
68 t->count = 0;
69 t->size = size;
70 t->name = name;
71 t->table = calloc(size, sizeof(struct lh_entry));
72 if(!t->table) lh_abort("lh_table_new: calloc failed\n");
73 t->free_fn = free_fn;
74 t->hash_fn = hash_fn;
75 t->equal_fn = equal_fn;
76 for(i = 0; i < size; i++) t->table[i].k = LH_EMPTY;
77 return t;
78}
79
Michael Clarkf0d08882007-03-13 08:26:18 +000080struct lh_table* lh_kchar_table_new(int size, char *name,
81 lh_entry_free_fn *free_fn)
82{
83 return lh_table_new(size, name, free_fn, lh_char_hash, lh_char_equal);
84}
85
Michael Clarkf0d08882007-03-13 08:26:18 +000086struct lh_table* lh_kptr_table_new(int size, char *name,
87 lh_entry_free_fn *free_fn)
88{
89 return lh_table_new(size, name, free_fn, lh_ptr_hash, lh_ptr_equal);
90}
91
Michael Clarkf0d08882007-03-13 08:26:18 +000092void lh_table_resize(struct lh_table *t, int new_size)
93{
94 struct lh_table *new_t;
95 struct lh_entry *ent;
96
97 new_t = lh_table_new(new_size, t->name, NULL, t->hash_fn, t->equal_fn);
98 ent = t->head;
99 while(ent) {
100 lh_table_insert(new_t, ent->k, ent->v);
101 ent = ent->next;
102 }
103 free(t->table);
104 t->table = new_t->table;
105 t->size = new_size;
106 t->head = new_t->head;
107 t->tail = new_t->tail;
108 t->resizes++;
109 free(new_t);
110}
111
Michael Clarkf0d08882007-03-13 08:26:18 +0000112void lh_table_free(struct lh_table *t)
113{
114 struct lh_entry *c;
115 for(c = t->head; c != NULL; c = c->next) {
116 if(t->free_fn) {
117 t->free_fn(c);
118 }
119 }
120 free(t->table);
121 free(t);
122}
123
124
125int lh_table_insert(struct lh_table *t, void *k, void *v)
126{
127 unsigned long h, n;
128
129 t->inserts++;
130 if(t->count > t->size * 0.66) lh_table_resize(t, t->size * 2);
131
132 h = t->hash_fn(k);
133 n = h % t->size;
134
135 while( 1 ) {
136 if(t->table[n].k == LH_EMPTY || t->table[n].k == LH_FREED) break;
137 t->collisions++;
138 if(++n == t->size) n = 0;
139 }
140
141 t->table[n].k = k;
142 t->table[n].v = v;
143 t->count++;
144
145 if(t->head == NULL) {
146 t->head = t->tail = &t->table[n];
147 t->table[n].next = t->table[n].prev = NULL;
148 } else {
149 t->tail->next = &t->table[n];
150 t->table[n].prev = t->tail;
151 t->table[n].next = NULL;
152 t->tail = &t->table[n];
153 }
154
155 return 0;
156}
157
158
159struct lh_entry* lh_table_lookup_entry(struct lh_table *t, void *k)
160{
161 unsigned long h = t->hash_fn(k);
162 unsigned long n = h % t->size;
163
164 t->lookups++;
165 while( 1 ) {
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;
170 }
171 return NULL;
172}
173
174
175void* lh_table_lookup(struct lh_table *t, void *k)
176{
177 struct lh_entry *e = lh_table_lookup_entry(t, k);
178 if(e) return e->v;
179 return NULL;
180}
181
182
183int lh_table_delete_entry(struct lh_table *t, struct lh_entry *e)
184{
Michael Clark4504df72007-03-13 08:26:20 +0000185 ptrdiff_t n = (ptrdiff_t)(e - t->table); /* CAW: fixed to be 64bit nice, still need the crazy negative case... */
186
187 /* CAW: this is bad, really bad, maybe stack goes other direction on this machine... */
188 if(n < 0) { return -2; }
189
Michael Clarkf0d08882007-03-13 08:26:18 +0000190 if(t->table[n].k == LH_EMPTY || t->table[n].k == LH_FREED) return -1;
191 t->count--;
192 if(t->free_fn) t->free_fn(e);
193 t->table[n].v = NULL;
194 t->table[n].k = LH_FREED;
195 if(t->tail == &t->table[n] && t->head == &t->table[n]) {
196 t->head = t->tail = NULL;
197 } else if (t->head == &t->table[n]) {
198 t->head->next->prev = NULL;
199 t->head = t->head->next;
200 } else if (t->tail == &t->table[n]) {
201 t->tail->prev->next = NULL;
202 t->tail = t->tail->prev;
203 } else {
204 t->table[n].prev->next = t->table[n].next;
205 t->table[n].next->prev = t->table[n].prev;
206 }
207 t->table[n].next = t->table[n].prev = NULL;
208 return 0;
209}
210
211
212int lh_table_delete(struct lh_table *t, void *k)
213{
214 struct lh_entry *e = lh_table_lookup_entry(t, k);
215 if(!e) return -1;
216 return lh_table_delete_entry(t, e);
217}
218