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


csm
Author(s): Andrea Censi
autogenerated on Tue May 11 2021 02:18:23