1 /* $Id: hash.c,v 1.3 2003-02-18 20:35:35 btb Exp $ */
3 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
4 SOFTWARE CORPORATION ("PARALLAX"). PARALLAX, IN DISTRIBUTING THE CODE TO
5 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
6 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
7 IN USING, DISPLAYING, AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
8 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
9 FREE PURPOSES. IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
10 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES. THE END-USER UNDERSTANDS
11 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
12 COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED.
20 static char rcsid[] = "$Id: hash.c,v 1.3 2003-02-18 20:35:35 btb Exp $";
32 int hashtable_init( hashtable *ht, int size ) {
37 for (i=1; i<13; i++ ) {
38 if ( (1<<i) >= size ) {
45 ht->and_mask = ht->size - 1;
47 Error( "Hashtable has size of 0" );
49 ht->key = (char **)d_malloc( size * sizeof(char *) );
51 Error( "Not enough memory to create a hash table of size %d", size );
53 for (i=0; i<size; i++ )
56 // Use calloc cause we want zero'd array.
57 ht->value = d_malloc( size*sizeof(int) );
58 if (ht->value==NULL) {
60 Error( "Not enough memory to create a hash table of size %d\n", size );
68 void hashtable_free( hashtable *ht ) {
71 if (ht->value != NULL )
76 int hashtable_getkey( char *key ) {
80 k^=((int)(*key++))<<i;
86 int hashtable_search( hashtable *ht, char *key ) {
91 k = hashtable_getkey( key );
94 while(i < ht->size ) {
95 j = (k+(i++)) & ht->and_mask;
96 if ( ht->key[j] == NULL )
98 if (!stricmp(ht->key[j], key ))
104 void hashtable_insert( hashtable *ht, char *key, int value ) {
108 k = hashtable_getkey( key );
111 while(i < ht->size) {
112 j = (k+(i++)) & ht->and_mask;
113 if ( ht->key[j] == NULL ) {
116 ht->value[j] = value;
118 } else if (!stricmp( key, ht->key[j] )) {
122 Error( "Out of hash slots\n" );