1 /*-----------------------------------------------------------------
2 SDCChast.c - contains support routines for hashtables
4 Written By - Sandeep Dutta . sandeep.dutta@usa.net (1998)
6 This program is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 In other words, you are welcome to use, share and improve this program.
21 You are forbidden to forbid anyone else to use, share and improve
22 what you give them. Help stamp out software-hoarding!
23 -------------------------------------------------------------------------*/
28 #include "SDCChasht.h"
31 #define DEFAULT_HTAB_SIZE 128
33 /*-----------------------------------------------------------------*/
34 /* newHashtItem - creates a new hashtable Item */
35 /*-----------------------------------------------------------------*/
36 hashtItem *newHashtItem (int key, void *item)
40 ALLOC(htip,sizeof(hashtItem));
48 /*-----------------------------------------------------------------*/
49 /* newHashTable - allocates a new hashtable of size */
50 /*-----------------------------------------------------------------*/
51 hTab *newHashTable (int size)
55 ALLOC(htab,sizeof(hTab));
57 if (!(htab->table = GC_malloc((size +1)* sizeof(hashtItem *)))) {
58 fprintf(stderr,"out of virtual memory %s %d\n",
59 __FILE__,(size +1)* sizeof(hashtItem *));
62 htab->minKey = htab->size = size ;
67 /*-----------------------------------------------------------------*/
68 /* hTabAddItem - adds an item to the hash table */
69 /*-----------------------------------------------------------------*/
70 void hTabAddItem (hTab **htab, int key, void *item )
76 *htab = newHashTable ( DEFAULT_HTAB_SIZE );
78 if (key > (*htab)->size ) {
80 (*htab)->table = GC_realloc ((*htab)->table,
81 (key*2 + 2)*sizeof(hashtItem *));
82 for ( i = (*htab)->size +1; i <= (key*2 + 1); i++ )
83 (*htab)->table[i] = NULL ;
84 (*htab)->size = key*2 + 1;
88 if ((*htab)->maxKey < key )
89 (*htab)->maxKey = key ;
91 if ((*htab)->minKey > key )
92 (*htab)->minKey = key ;
95 htip = newHashtItem (key,item);
97 /* if there is a clash then goto end of chain */
98 if ((last = (*htab)->table[key])) {
103 /* else just add it */
104 (*htab)->table[key] = htip ;
108 /*-----------------------------------------------------------------*/
109 /* hTabDeleteItem - either delete an item */
110 /*-----------------------------------------------------------------*/
111 void hTabDeleteItem (hTab **htab, int key ,
112 void *item, int action ,
113 int (*compareFunc)(void *,void *))
115 hashtItem *htip, **htipp ;
120 /* first check if anything exists in the slot */
121 if (! (*htab)->table[key] )
124 /* if delete chain */
125 if ( action == DELETE_CHAIN )
126 (*htab)->table[key] = NULL ;
129 /* delete specific item */
130 /* if a compare function is given then use the compare */
131 /* function to find the item, else just compare the items */
133 htipp = &((*htab)->table[key]);
134 htip = (*htab)->table[key];
135 for (; htip; htip = htip->next) {
137 if (compareFunc ? (*compareFunc)(item,htip->item) :
138 (item == htip->item) ) {
150 if (!(*htab)->nItems) {
155 /*-----------------------------------------------------------------*/
156 /* hTabDeleteAll - deletes all items in a hash table to reduce mem */
157 /* leaks written by */
158 /* "BESSIERE Jerome" <BESSIERE_Jerome@stna.dgac.fr> */
159 /*-----------------------------------------------------------------*/
160 void hTabDeleteAll(hTab * p)
164 register hashtItem *jc,*jn;
165 for (i=0;i<p->size;i++) {
167 if (!(jc = p->table[i])) continue;
171 if((jc=jn)) jn = jc->next;
179 /*-----------------------------------------------------------------*/
180 /* hTabClearAll - clear all entries inthe table (does not free) */
181 /*-----------------------------------------------------------------*/
182 void hTabClearAll (hTab *htab)
185 if (!htab || !htab->table) {
186 printf("null table\n");
189 memset(htab->table, 0, htab->size*sizeof(hashtItem *));
191 htab->minKey = htab->size;
192 htab->currKey = htab->nItems = htab->maxKey = 0;
195 /*-----------------------------------------------------------------*/
196 /* hTabIsInTable - will determine if an Item is in the hasht */
197 /*-----------------------------------------------------------------*/
198 int hTabIsInTable (hTab *htab, int key,
199 void *item , int (*compareFunc)(void *,void *))
203 for (htip = htab->table[key] ; htip ; htip = htip->next ) {
204 /* if a compare function is given use it */
205 if (compareFunc && (*compareFunc)(item,htip->item))
208 if (item == htip->item)
217 /*-----------------------------------------------------------------*/
218 /* hTabFirstItem - returns the first Item in the hTab */
219 /*-----------------------------------------------------------------*/
220 void *hTabFirstItem (hTab *htab, int *k)
227 for ( key = htab->minKey ; key <= htab->maxKey ; key++ ) {
228 if (htab->table[key]) {
229 htab->currItem = htab->table[key];
230 htab->currKey = key ;
232 return htab->table[key]->item;
238 /*-----------------------------------------------------------------*/
239 /* hTabNextItem - returns the next item in the hTab */
240 /*-----------------------------------------------------------------*/
241 void *hTabNextItem (hTab *htab, int *k)
248 /* if this chain not ended then */
249 if (htab->currItem->next) {
250 *k = htab->currItem->key ;
251 return (htab->currItem = htab->currItem->next)->item ;
254 /* find the next chain which has something */
255 for ( key = htab->currKey + 1; key <= htab->maxKey ; key++ ) {
256 if (htab->table[key]) {
257 htab->currItem = htab->table[key];
258 *k = htab->currKey = key ;
259 return htab->table[key]->item;
266 /*-----------------------------------------------------------------*/
267 /* hTabFirstItemWK - returns the first Item in the hTab for a key */
268 /*-----------------------------------------------------------------*/
269 void *hTabFirstItemWK (hTab *htab, int wk)
275 if (wk < htab->minKey || wk > htab->maxKey )
278 htab->currItem = htab->table[wk] ;
281 return (htab->table[wk] ? htab->table[wk]->item : NULL);
284 /*-----------------------------------------------------------------*/
285 /* hTabNextItem - returns the next item in the hTab for a key */
286 /*-----------------------------------------------------------------*/
287 void *hTabNextItemWK (hTab *htab )
293 /* if this chain not ended then */
294 if (htab->currItem->next) {
295 return (htab->currItem = htab->currItem->next)->item ;
301 /*-----------------------------------------------------------------*/
302 /* hTabFromTable - hash Table from a hash table */
303 /*-----------------------------------------------------------------*/
304 hTab *hTabFromTable (hTab *htab)
313 nhtab = newHashTable(htab->size);
315 for (key = htab->minKey; key <= htab->maxKey ; key++ ) {
317 for (htip = htab->table[key] ; htip ; htip = htip->next)
318 hTabAddItem (&nhtab, htip->key, htip->item);
324 /*-----------------------------------------------------------------*/
325 /* isHtabsEqual - returns 1 if all items in htab1 is found in htab2*/
326 /*-----------------------------------------------------------------*/
327 int isHtabsEqual (hTab *htab1, hTab *htab2,
328 int (*compareFunc)(void *,void *))
333 if ( htab1 == htab2 )
336 if (htab1 == NULL || htab2 == NULL )
339 /* if they are different sizes then */
340 if ( htab1->nItems != htab2->nItems)
343 /* now do an item by item check */
344 for ( item = hTabFirstItem (htab1,&key) ;item;
345 item = hTabNextItem (htab1,&key))
346 if (!hTabIsInTable (htab2, key, item, compareFunc))
353 /*-----------------------------------------------------------------*/
354 /* hTabSearch - returns the first Item with the specified key */
355 /*-----------------------------------------------------------------*/
356 hashtItem *hTabSearch (hTab *htab, int key )
361 if ((key < htab->minKey)||(key>htab->maxKey))
364 if (!htab->table[key])
367 return htab->table[key] ;
370 /*-----------------------------------------------------------------*/
371 /* hTabItemWithKey - returns the first item with the gievn key */
372 /*-----------------------------------------------------------------*/
373 void *hTabItemWithKey (hTab *htab, int key )
377 if (!(htip = hTabSearch(htab,key)))
383 /*-----------------------------------------------------------------*/
384 /*hTabAddItemIfNotP - adds an item with nothing found with key */
385 /*-----------------------------------------------------------------*/
386 void hTabAddItemIfNotP (hTab **htab, int key, void *item)
389 hTabAddItem (htab,key,item);
393 if (hTabItemWithKey(*htab,key))
396 hTabAddItem(htab,key,item);