2 * $Id: hashtab.c,v 1.2 2007/07/22 13:33:26 khansen Exp $
4 * Revision 1.2 2007/07/22 13:33:26 khansen
5 * convert tabs to whitespaces
7 * Revision 1.1 2004/06/30 07:55:43 kenth
13 * (C) 2004 Kent Hansen
15 * The XORcyst is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
20 * The XORcyst is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with The XORcyst; if not, write to the Free Software
27 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31 * This file contains an implementation of a hash table.
32 * Both keys and values are of type void *.
33 * To implement hashing and key comparison, you have to supply two functions
34 * for doing this job. If keys are strings, then you can use the default
42 #define SAFE_FREE(a) if (a) { free(a); a = NULL; }
44 /*---------------------------------------------------------------------------*/
47 * Supplies default hashing function when key is a string.
49 int HASHTAB_STRKEYHSH(void *key
)
52 unsigned char *s
= (unsigned char *)key
;
54 for (i
=0; s
[i
] != '\0'; i
++) {
61 * Supplies default key comparison function when key is a string.
63 int HASHTAB_STRKEYCMP(void *key1
, void *key2
)
65 return (strcmp((char *)key1
, (char *)key2
) == 0) ? 1 : 0;
68 /*---------------------------------------------------------------------------*/
71 * Creates a hash table.
72 * @param size Number of slots
73 * @param keyhsh Procedure which computes h(k)
74 * @param keycmp Procedure which compares two keys
75 * @return The newly created hash table
77 hashtab
*hashtab_create(int size
, keyhashproc keyhsh
, keycompareproc keycmp
)
80 /* Allocate space for hash table */
81 hashtab
*ht
= (hashtab
*)malloc( sizeof(hashtab
) );
83 /* Allocate space for slots */
85 ht
->slots
= (hashtab_slot
**)malloc( sizeof(hashtab_slot
*) * size
);
86 if (ht
->slots
!= NULL
) {
87 /* Set the hash and comparator procedures */
90 /* Initialize the slots */
91 for (i
=0; i
<size
; i
++) {
96 /* Return the created hash table */
101 * Puts something in a hash table.
102 * @param ht Hash table
106 void hashtab_put(hashtab
*ht
, void *key
, void *data
)
111 /* Create a new entry */
112 fresh
= (hashtab_slot
*)malloc( sizeof(hashtab_slot
) );
117 /* Figure out in which slot to put it */
118 i
= ht
->keyhsh(key
) % ht
->size
;
120 if (ht
->slots
[i
] == NULL
) {
122 ht
->slots
[i
] = fresh
;
125 /* Add to end of list */
126 for (s
= ht
->slots
[i
]; s
->next
!= NULL
; s
= s
->next
) ;
133 * Gets the data associated with given key.
134 * @param ht Hash table
136 * @return The data associated with key, or <code>NULL</code> if key not found
138 void *hashtab_get(hashtab
*ht
, void *key
)
141 int i
= ht
->keyhsh(key
) % ht
->size
;
142 for (s
= ht
->slots
[i
]; s
!= NULL
; s
= s
->next
) {
143 if (ht
->keycmp(key
, s
->key
)) {
151 * Removes something from hash table.
152 * @param ht Hash table
154 * @return Data associated with key
156 void *hashtab_remove(hashtab
*ht
, void *key
)
159 int i
= ht
->keyhsh(key
) % ht
->size
;
160 for (s
= ht
->slots
[i
]; s
!= NULL
; s
= s
->next
) {
161 if (ht
->keycmp(key
, s
->key
)) {
170 * Finalizes hash table.
171 * @param ht The hash table to finalize
173 void hashtab_finalize(hashtab
*ht
)
178 for (i
=0; i
<ht
->size
; i
++) {
186 SAFE_FREE(ht
->slots
);