Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 1 | /* |
Michael Clark | f6a6e48 | 2007-03-13 08:26:23 +0000 | [diff] [blame] | 2 | * $Id: linkhash.c,v 1.4 2006/01/26 02:16:28 mclark Exp $ |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 3 | * |
Michael Clark | f6a6e48 | 2007-03-13 08:26:23 +0000 | [diff] [blame] | 4 | * Copyright (c) 2004, 2005 Metaparadigm Pte. Ltd. |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 5 | * Michael Clark <michael@metaparadigm.com> |
| 6 | * |
Michael Clark | f6a6e48 | 2007-03-13 08:26:23 +0000 | [diff] [blame] | 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. |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 9 | * |
| 10 | */ |
| 11 | |
Michael Clark | 4504df7 | 2007-03-13 08:26:20 +0000 | [diff] [blame] | 12 | #include "config.h" |
| 13 | |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 14 | #include <stdio.h> |
| 15 | #include <string.h> |
| 16 | #include <stdlib.h> |
| 17 | #include <stdarg.h> |
Michael Clark | 4504df7 | 2007-03-13 08:26:20 +0000 | [diff] [blame] | 18 | #include <stddef.h> |
| 19 | #include <limits.h> |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 20 | |
| 21 | #include "linkhash.h" |
| 22 | |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 23 | void lh_abort(const char *msg, ...) |
| 24 | { |
| 25 | va_list ap; |
| 26 | va_start(ap, msg); |
| 27 | vprintf(msg, ap); |
Michael Clark | 8cdac64 | 2009-01-05 03:57:59 +0000 | [diff] [blame] | 28 | va_end(ap); |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 29 | exit(1); |
| 30 | } |
| 31 | |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 32 | unsigned long lh_ptr_hash(void *k) |
| 33 | { |
Michael Clark | 4504df7 | 2007-03-13 08:26:20 +0000 | [diff] [blame] | 34 | /* CAW: refactored to be 64bit nice */ |
| 35 | return (unsigned long)((((ptrdiff_t)k * LH_PRIME) >> 4) & ULONG_MAX); |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 36 | } |
| 37 | |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 38 | int lh_ptr_equal(void *k1, void *k2) |
| 39 | { |
| 40 | return (k1 == k2); |
| 41 | } |
| 42 | |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 43 | unsigned 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 Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 53 | int lh_char_equal(void *k1, void *k2) |
| 54 | { |
| 55 | return (strcmp((char*)k1, (char*)k2) == 0); |
| 56 | } |
| 57 | |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 58 | struct 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 Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 80 | struct 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 Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 86 | struct 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 Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 92 | void 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 Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 112 | void 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 | |
| 125 | int 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 | |
| 159 | struct 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 | |
| 175 | void* 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 | |
| 183 | int lh_table_delete_entry(struct lh_table *t, struct lh_entry *e) |
| 184 | { |
Michael Clark | 4504df7 | 2007-03-13 08:26:20 +0000 | [diff] [blame] | 185 | 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 Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 190 | 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 | |
| 212 | int 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 | |