9 date 2007.07.22.13.33.26; author khansen; state Exp;
14 date 2004.06.30.07.55.43; author kenth; state Exp;
25 @convert tabs to whitespaces
29 * $Id: hashtab.c,v 1.1 2004/06/30 07:55:43 kenth Exp $
31 * Revision 1.1 2004/06/30 07:55:43 kenth
37 * (C) 2004 Kent Hansen
39 * The XORcyst is free software; you can redistribute it and/or modify
40 * it under the terms of the GNU General Public License as published by
41 * the Free Software Foundation; either version 2 of the License, or
42 * (at your option) any later version.
44 * The XORcyst is distributed in the hope that it will be useful,
45 * but WITHOUT ANY WARRANTY; without even the implied warranty of
46 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
47 * GNU General Public License for more details.
49 * You should have received a copy of the GNU General Public License
50 * along with The XORcyst; if not, write to the Free Software
51 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
55 * This file contains an implementation of a hash table.
56 * Both keys and values are of type void *.
57 * To implement hashing and key comparison, you have to supply two functions
58 * for doing this job. If keys are strings, then you can use the default
66 #define SAFE_FREE(a) if (a) { free(a); a = NULL; }
68 /*---------------------------------------------------------------------------*/
71 * Supplies default hashing function when key is a string.
73 int HASHTAB_STRKEYHSH(void *key)
76 unsigned char *s = (unsigned char *)key;
78 for (i=0; s[i] != '\0'; i++) {
85 * Supplies default key comparison function when key is a string.
87 int HASHTAB_STRKEYCMP(void *key1, void *key2)
89 return (strcmp((char *)key1, (char *)key2) == 0) ? 1 : 0;
92 /*---------------------------------------------------------------------------*/
95 * Creates a hash table.
96 * @@param size Number of slots
97 * @@param keyhsh Procedure which computes h(k)
98 * @@param keycmp Procedure which compares two keys
99 * @@return The newly created hash table
101 hashtab *hashtab_create(int size, keyhashproc keyhsh, keycompareproc keycmp)
104 /* Allocate space for hash table */
105 hashtab *ht = (hashtab *)malloc( sizeof(hashtab) );
107 /* Allocate space for slots */
109 ht->slots = (hashtab_slot **)malloc( sizeof(hashtab_slot *) * size );
110 if (ht->slots != NULL) {
111 /* Set the hash and comparator procedures */
114 /* Initialize the slots */
115 for (i=0; i<size; i++) {
120 /* Return the created hash table */
125 * Puts something in a hash table.
126 * @@param ht Hash table
130 void hashtab_put(hashtab *ht, void *key, void *data)
135 /* Create a new entry */
136 fresh = (hashtab_slot *)malloc( sizeof(hashtab_slot) );
141 /* Figure out in which slot to put it */
142 i = ht->keyhsh(key) % ht->size;
144 if (ht->slots[i] == NULL) {
146 ht->slots[i] = fresh;
149 /* Add to end of list */
150 for (s = ht->slots[i]; s->next != NULL; s = s->next) ;
157 * Gets the data associated with given key.
158 * @@param ht Hash table
160 * @@return The data associated with key, or <code>NULL</code> if key not found
162 void *hashtab_get(hashtab *ht, void *key)
165 int i = ht->keyhsh(key) % ht->size;
166 for (s = ht->slots[i]; s != NULL; s = s->next) {
167 if (ht->keycmp(key, s->key)) {
175 * Removes something from hash table.
176 * @@param ht Hash table
178 * @@return Data associated with key
180 void *hashtab_remove(hashtab *ht, void *key)
183 int i = ht->keyhsh(key) % ht->size;
184 for (s = ht->slots[i]; s != NULL; s = s->next) {
185 if (ht->keycmp(key, s->key)) {
194 * Finalizes hash table.
195 * @@param ht The hash table to finalize
197 void hashtab_finalize(hashtab *ht)
202 for (i=0; i<ht->size; i++) {
210 SAFE_FREE(ht->slots);
228 unsigned char *s = (unsigned char *)key;
230 for (i=0; s[i] != '\0'; i++) {
236 return (strcmp((char *)key1, (char *)key2) == 0) ? 1 : 0;
240 /* Allocate space for hash table */
241 hashtab *ht = (hashtab *)malloc( sizeof(hashtab) );
243 /* Allocate space for slots */
245 ht->slots = (hashtab_slot **)malloc( sizeof(hashtab_slot *) * size );
246 if (ht->slots != NULL) {
247 /* Set the hash and comparator procedures */
250 /* Initialize the slots */
251 for (i=0; i<size; i++) {
256 /* Return the created hash table */
263 /* Create a new entry */
264 fresh = (hashtab_slot *)malloc( sizeof(hashtab_slot) );
269 /* Figure out in which slot to put it */
270 i = ht->keyhsh(key) % ht->size;
272 if (ht->slots[i] == NULL) {
274 ht->slots[i] = fresh;
277 /* Add to end of list */
278 for (s = ht->slots[i]; s->next != NULL; s = s->next) ;
285 int i = ht->keyhsh(key) % ht->size;
286 for (s = ht->slots[i]; s != NULL; s = s->next) {
287 if (ht->keycmp(key, s->key)) {
295 int i = ht->keyhsh(key) % ht->size;
296 for (s = ht->slots[i]; s != NULL; s = s->next) {
297 if (ht->keycmp(key, s->key)) {
308 for (i=0; i<ht->size; i++) {
316 SAFE_FREE(ht->slots);