blob: 88c0a7cfe8db54fdcaf4651690988d81aa936717 [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
12#include <stdio.h>
13#include <string.h>
14#include <stdlib.h>
15#include <stdarg.h>
Michael Clark4504df72007-03-13 08:26:20 +000016#include <stddef.h>
17#include <limits.h>
Michael Clarkf0d08882007-03-13 08:26:18 +000018
19#include "linkhash.h"
20
Michael Clarkf0d08882007-03-13 08:26:18 +000021void lh_abort(const char *msg, ...)
22{
23 va_list ap;
24 va_start(ap, msg);
25 vprintf(msg, ap);
Michael Clark8cdac642009-01-05 03:57:59 +000026 va_end(ap);
Michael Clarkf0d08882007-03-13 08:26:18 +000027 exit(1);
28}
29
Michael Clark68cafad2009-01-06 22:56:57 +000030unsigned long lh_ptr_hash(const void *k)
Michael Clarkf0d08882007-03-13 08:26:18 +000031{
Michael Clark4504df72007-03-13 08:26:20 +000032 /* CAW: refactored to be 64bit nice */
33 return (unsigned long)((((ptrdiff_t)k * LH_PRIME) >> 4) & ULONG_MAX);
Michael Clarkf0d08882007-03-13 08:26:18 +000034}
35
Michael Clark68cafad2009-01-06 22:56:57 +000036int lh_ptr_equal(const void *k1, const void *k2)
Michael Clarkf0d08882007-03-13 08:26:18 +000037{
38 return (k1 == k2);
39}
40
Michael Clark68cafad2009-01-06 22:56:57 +000041unsigned long lh_char_hash(const void *k)
Michael Clarkf0d08882007-03-13 08:26:18 +000042{
43 unsigned int h = 0;
Michael Clarkaaec1ef2009-02-25 02:31:32 +000044 const char* data = (const char*)k;
Michael Clarkf0d08882007-03-13 08:26:18 +000045
46 while( *data!=0 ) h = h*129 + (unsigned int)(*data++) + LH_PRIME;
47
48 return h;
49}
50
Michael Clark68cafad2009-01-06 22:56:57 +000051int lh_char_equal(const void *k1, const void *k2)
Michael Clarkf0d08882007-03-13 08:26:18 +000052{
Michael Clark68cafad2009-01-06 22:56:57 +000053 return (strcmp((const char*)k1, (const char*)k2) == 0);
Michael Clarkf0d08882007-03-13 08:26:18 +000054}
55
Michael Clark68cafad2009-01-06 22:56:57 +000056struct lh_table* lh_table_new(int size, const char *name,
Michael Clarkf0d08882007-03-13 08:26:18 +000057 lh_entry_free_fn *free_fn,
58 lh_hash_fn *hash_fn,
59 lh_equal_fn *equal_fn)
60{
61 int i;
62 struct lh_table *t;
63
Michael Clarkaaec1ef2009-02-25 02:31:32 +000064 t = (struct lh_table*)calloc(1, sizeof(struct lh_table));
Michael Clarkf0d08882007-03-13 08:26:18 +000065 if(!t) lh_abort("lh_table_new: calloc failed\n");
66 t->count = 0;
67 t->size = size;
68 t->name = name;
Michael Clarkaaec1ef2009-02-25 02:31:32 +000069 t->table = (struct lh_entry*)calloc(size, sizeof(struct lh_entry));
Michael Clarkf0d08882007-03-13 08:26:18 +000070 if(!t->table) lh_abort("lh_table_new: calloc failed\n");
71 t->free_fn = free_fn;
72 t->hash_fn = hash_fn;
73 t->equal_fn = equal_fn;
74 for(i = 0; i < size; i++) t->table[i].k = LH_EMPTY;
75 return t;
76}
77
Michael Clark68cafad2009-01-06 22:56:57 +000078struct lh_table* lh_kchar_table_new(int size, const char *name,
Michael Clarkf0d08882007-03-13 08:26:18 +000079 lh_entry_free_fn *free_fn)
80{
81 return lh_table_new(size, name, free_fn, lh_char_hash, lh_char_equal);
82}
83
Michael Clark68cafad2009-01-06 22:56:57 +000084struct lh_table* lh_kptr_table_new(int size, const char *name,
Michael Clarkf0d08882007-03-13 08:26:18 +000085 lh_entry_free_fn *free_fn)
86{
87 return lh_table_new(size, name, free_fn, lh_ptr_hash, lh_ptr_equal);
88}
89
Michael Clarkf0d08882007-03-13 08:26:18 +000090void lh_table_resize(struct lh_table *t, int new_size)
91{
92 struct lh_table *new_t;
93 struct lh_entry *ent;
94
95 new_t = lh_table_new(new_size, t->name, NULL, t->hash_fn, t->equal_fn);
96 ent = t->head;
97 while(ent) {
98 lh_table_insert(new_t, ent->k, ent->v);
99 ent = ent->next;
100 }
101 free(t->table);
102 t->table = new_t->table;
103 t->size = new_size;
104 t->head = new_t->head;
105 t->tail = new_t->tail;
106 t->resizes++;
107 free(new_t);
108}
109
Michael Clarkf0d08882007-03-13 08:26:18 +0000110void lh_table_free(struct lh_table *t)
111{
112 struct lh_entry *c;
113 for(c = t->head; c != NULL; c = c->next) {
114 if(t->free_fn) {
115 t->free_fn(c);
116 }
117 }
118 free(t->table);
119 free(t);
120}
121
122
Michael Clark68cafad2009-01-06 22:56:57 +0000123int lh_table_insert(struct lh_table *t, void *k, const void *v)
Michael Clarkf0d08882007-03-13 08:26:18 +0000124{
125 unsigned long h, n;
126
127 t->inserts++;
Eric Haszlakiewicz64c0ca32012-03-31 17:33:58 -0500128 if(t->count >= t->size * LH_LOAD_FACTOR) lh_table_resize(t, t->size * 2);
Michael Clarkf0d08882007-03-13 08:26:18 +0000129
130 h = t->hash_fn(k);
131 n = h % t->size;
132
133 while( 1 ) {
134 if(t->table[n].k == LH_EMPTY || t->table[n].k == LH_FREED) break;
135 t->collisions++;
136 if(++n == t->size) n = 0;
137 }
138
139 t->table[n].k = k;
140 t->table[n].v = v;
141 t->count++;
142
143 if(t->head == NULL) {
144 t->head = t->tail = &t->table[n];
145 t->table[n].next = t->table[n].prev = NULL;
146 } else {
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];
151 }
152
153 return 0;
154}
155
156
Michael Clark68cafad2009-01-06 22:56:57 +0000157struct lh_entry* lh_table_lookup_entry(struct lh_table *t, const void *k)
Michael Clarkf0d08882007-03-13 08:26:18 +0000158{
159 unsigned long h = t->hash_fn(k);
160 unsigned long n = h % t->size;
Michael Clarkf5dd43a2009-08-27 06:40:00 +0000161 int count = 0;
Michael Clarkf0d08882007-03-13 08:26:18 +0000162
163 t->lookups++;
Michael Clarkf5dd43a2009-08-27 06:40:00 +0000164 while( count < t->size ) {
Michael Clarkf0d08882007-03-13 08:26:18 +0000165 if(t->table[n].k == LH_EMPTY) return NULL;
166 if(t->table[n].k != LH_FREED &&
167 t->equal_fn(t->table[n].k, k)) return &t->table[n];
168 if(++n == t->size) n = 0;
Michael Clarkf5dd43a2009-08-27 06:40:00 +0000169 count++;
Michael Clarkf0d08882007-03-13 08:26:18 +0000170 }
171 return NULL;
172}
173
174
Michael Clark68cafad2009-01-06 22:56:57 +0000175const void* lh_table_lookup(struct lh_table *t, const void *k)
Michael Clarkf0d08882007-03-13 08:26:18 +0000176{
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
Michael Clark68cafad2009-01-06 22:56:57 +0000212int lh_table_delete(struct lh_table *t, const void *k)
Michael Clarkf0d08882007-03-13 08:26:18 +0000213{
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