File: hash.c

package info (click to toggle)
xmhtml 1.1.7-7
  • links: PTS
  • area: main
  • in suites: woody
  • size: 6,104 kB
  • ctags: 8,347
  • sloc: ansic: 68,063; makefile: 496; sh: 161; perl: 36
file content (386 lines) | stat: -rw-r--r-- 8,346 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
#ifndef production
static char rcsId[]="$Header$";
#endif
/*****
* hash.c : Generic hashing routines
*
* This file Version	$Revision$
*
* Creation date:		Sat Oct 24 01:09:35 CEST 1998
* Last modification: 	$Date$
* By:					$Author$
* Current State:		$State$
*
* Author:				XmHTML Developers Account
*
* Copyright (C) 1994-1998 by Ripley Software Development 
* All Rights Reserved
*
* This file is part of the XmHTML Widget Library
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the Free
* Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*****/
/*****
* ChangeLog 
* $Log$
*****/ 
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#ifdef WITH_DMALLOC
#include <dmalloc.h>
#endif

#include "hash.h"

/*** External Function Prototype Declarations ***/

/*** Public Variable Declarations ***/

/*** Private Datatype Declarations ****/

/* a single entry in the hashtable */
typedef struct _HashEntry{
	struct _HashEntry *nptr;
	struct _HashEntry *pptr;
	unsigned long key;
	unsigned long data;
	struct _HashEntry *next;	/* next on collision list */
}HashEntry;

/* The hashtable itself */
struct _HashTable{
	int elements;				/* elements in table		*/
	int size;					/* size of table			*/
	HashEntry **table;
	HashEntry *last;			/* last on the linked list	*/
	HashCompareFunc comparer;	/* data comparison function	*/
};

/*** Private Function Prototype Declarations ****/

/*** Private Variable Declarations ***/

/*****
* Private routines.
*****/

/*****
* Name: 		hashDestroy
* Return Type: 	void
* Description: 	frees the table of a given hashtable. Only used when table
*				is being rebuild.
* In: 
*	table:		table to be destroyed;
* Returns:
*	nothing.
*****/
static void
hashDestroy(HashTable *table)
{
	HashEntry *entry, *next;
	int i;

	for (i=0; i<table->size; i++)
	{
		entry=table->table[i];
		while (entry)
		{
			next = entry->next;
			entry = next;
		}
	}
	free(table->table);
}

/*****
* Name: 		hashRemoveEntry
* Return Type: 	HashEntry
* Description: 	deletes a given entry from the given hashtable.
* In: 
*	table:		table from which an entry should be deleted.
*	entry:		entry to be deleted;
*	key:		entry identifier
* Returns:
*	entry following the deleted entry. This can be non-null if a hashvalue
*	contains multiple keys.
*****/
static HashEntry *
hashRemoveEntry(HashTable *table, HashEntry *entry, unsigned long key)
{
	HashEntry *next;

	if(entry==NULL)
		return NULL;

	if((table->comparer && (*table->comparer)(entry->key, key)) ||
		entry->key == key)
	{
		if(table->last == entry)
			table->last = entry->pptr;
		if(entry->nptr)
			entry->nptr->pptr = entry->pptr;
		if(entry->pptr)
			entry->pptr->nptr = entry->nptr;
		next = entry->next;
		free(entry);
		return next;
	}
	entry->next = hashRemoveEntry(table, entry->next, key);
	return entry;
}

/*****
* Name: 		hashRebuild
* Return Type: 	void
* Description: 	enlarges & rebuilds the given hashtable. Used when the
*				size of the current hashtable is becoming to small to store
*				new info efficiently.
* In: 
*	table:		table to rebuild
* Returns:
*	nothing.
*****/
static void
hashRebuild(HashTable *table)
{
	HashTable newtable;
	HashEntry *entry;
	int i;

	newtable.last = NULL;
	newtable.elements = 0;
	newtable.size = table->size*2;
	newtable.table = (HashEntry**)malloc(newtable.size * sizeof(HashEntry*));
	memset(newtable.table, 0, newtable.size * sizeof(HashEntry*));
	for (i=0; i<table->size; i++)
	{
		entry = table->table[i];
		while(entry)
		{
			HashPut(&newtable, entry->key, entry->data);
			entry=entry->next;
		}
	}
	hashDestroy(table);
	table->elements = newtable.elements;
	table->size = newtable.size;
	table->table = newtable.table;
}


/*************
****** Hashing 
*************/

/*****
* Name: 		HashCreate
* Return Type: 	HashTable*
* Description: 	creates & initializes a new hashing table.
* In: 
*	hsize:		initial hashtable size;
*	comparer:	key comparison function, optional. If present, a positive
*				match should return a non-zero value and a negative match
*				should return a zero value.
* Returns:
*	The newly created hashtable.
*****/
HashTable*
HashCreate(int hsize, HashCompareFunc comparer)
{
	static HashTable *table;

	/* allocate a new table */
	if((table = (HashTable*)malloc(sizeof(HashTable))) == NULL)
		return(NULL);

	/* initialize & return to caller */
	return(HashInit(table, hsize, comparer));
}

/*****
* Name: 		HashInit
* Return Type: 	HashTable
* Description: 	Initializes a hashtable with an initial size hsize.
*				The table must already be allocated.
* In: 
*	table:		hashtable to be initialized;
*	hsize:		size of hashtable.
*	comparer:	key comparison function, optional.
* Returns:
*	initialized table.
*****/
HashTable *
HashInit(HashTable *table, int hsize, HashCompareFunc comparer)
{
	table->elements = 0;
	table->size  = hsize;
	table->table = (HashEntry**)malloc(hsize*sizeof(HashEntry*));
	table->last  = NULL;
	table->comparer = comparer;
	memset(table->table, 0, hsize * sizeof(HashEntry*));

	return(table);
}

/*****
* Name: 		HashPut
* Return Type: 	void
* Description: 	puts a new entry in the hash table
* In: 
*	key:		handle to data to be stored;
*	data:		data to be stored;
* Returns:
*	nothing.
*****/
void
HashPut(HashTable *table, unsigned long key, unsigned long data)
{
	unsigned long hkey;
	HashEntry *nentry;
    
	nentry = (HashEntry*)malloc(sizeof(HashEntry));

	nentry->key = key;
	nentry->data = data;
	hkey = key % table->size;

	/* Aaie, collided */
	if (table->table[hkey]!=NULL)
	{
		nentry->next = table->table[hkey];
		table->table[hkey] = nentry;
    }
	else
	{
		nentry->next = NULL;
		table->table[hkey] = nentry;
    }
	table->elements++;
    
	nentry->nptr = NULL;
	nentry->pptr = table->last;
	if(table->last)
		table->last->nptr = nentry;
	table->last = nentry;
    
	if(table->elements > (table->size*3)/2)
	{
		/* humpf, table getting too small, resize and rebuild. */
		hashRebuild(table);
    }
}

/*****
* Name: 		HashGet
* Return Type: 	int
* Description: 	retrieves a hash entry.
* In: 
*	key:		id of entry to retrieve;
*	*data:		object in which to store data reference;
* Returns:
*	True when entry was found, False if not.
*****/
int
HashGet(HashTable *table, unsigned long key, unsigned long *data)
{
	unsigned long hkey;
	HashEntry *entry;

	hkey = key % table->size;
	entry = table->table[hkey];

	/* split for performance reasons */
	if(table->comparer)
	{
		while (entry!=NULL)
		{
			if((table->comparer(entry->key, key)))
			{
				*data=entry->data;
				return(1);
			}
			entry = entry->next;
		}
	}
	else
	{
		while (entry!=NULL)
		{
			if(entry->key==key)
			{
				*data=entry->data;
				return(1);
			}
			entry = entry->next;
		}
	}
	return(0);
}

/*****
* Name: 		HashDelete
* Return Type: 	void
* Description: 	deletes the hash entry for the given key.
* In: 
*	table:		hashtable from which to delete an entry;
*	key:		id of entry to be deleted.
* Returns:
*	nothing.
*****/
void
HashDelete(HashTable *table, unsigned long key)
{
    unsigned long hkey;
    
    hkey = key % table->size;
    table->table[hkey] = hashRemoveEntry(table, table->table[hkey], key);
    table->elements--;
}

/*****
* Name: 		HashDestroy
* Return Type: 	void
* Description: 	completely destroys the given hashtable contents. Table
*				and contents are not destroyed.
* In: 
*	table:		table to be destroyed;
* Returns:
*	nothing.
*****/
void
HashDestroy(HashTable *table)
{
	int i;

	/* first remove all entries in the hash table */
	for(i = 0; i < table->size; i++)
	{
		if(table->table[i]!=NULL)
			while((table->table[i] = hashRemoveEntry(table, table->table[i],
				table->table[i]->key)) != NULL);
	}
	/* delete table */
	free(table->table);

	/* sanity */
	table->table = NULL;
}