]> icculus.org git repositories - btb/d2x.git/blob - misc/hash.c
remove rcs tags
[btb/d2x.git] / misc / hash.c
1 /*
2 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
3 SOFTWARE CORPORATION ("PARALLAX").  PARALLAX, IN DISTRIBUTING THE CODE TO
4 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
5 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
6 IN USING, DISPLAYING,  AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
7 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
8 FREE PURPOSES.  IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
9 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES.  THE END-USER UNDERSTANDS
10 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
11 COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION.  ALL RIGHTS RESERVED.
12 */
13
14 #ifdef HAVE_CONFIG_H
15 #include <conf.h>
16 #endif
17
18 #include <stdlib.h>
19 #include <stdio.h>
20 #include <string.h>
21
22 #include "u_mem.h"
23 #include "strutil.h"
24 #include "error.h"
25 #include "hash.h"
26         
27 int hashtable_init( hashtable *ht, int size )   {
28         int i;
29
30         ht->size=0;
31
32         for (i=1; i<13; i++ )   {
33                 if ( (1<<i) >= size )   {
34                         ht->bitsize = i;
35                         ht->size = 1<<i;
36                         break;
37                 }
38         }
39         size = ht->size;
40         ht->and_mask = ht->size - 1;
41         if (ht->size==0)
42                 Error( "Hashtable has size of 0" );
43
44         ht->key = (char **)d_malloc( size * sizeof(char *) );
45         if (ht->key==NULL)
46                 Error( "Not enough memory to create a hash table of size %d", size );
47
48         for (i=0; i<size; i++ )
49                 ht->key[i] = NULL;
50
51         // Use calloc cause we want zero'd array.
52         ht->value = d_malloc( size*sizeof(int) );
53         if (ht->value==NULL)    {
54                 d_free(ht->key);
55                 Error( "Not enough memory to create a hash table of size %d\n", size );
56         }
57
58         ht->nitems = 0;
59
60         return 0;
61 }
62
63 void hashtable_free( hashtable *ht )    {
64         if (ht->key != NULL )
65                 d_free( ht->key );
66         if (ht->value != NULL )
67                 d_free( ht->value );
68         ht->size = 0;
69 }
70
71 int hashtable_getkey( char *key )       {
72         int k = 0, i=0;
73
74         while( *key )   {
75                 k^=((int)(*key++))<<i;
76                 i++;
77         }
78         return k;
79 }
80
81 int hashtable_search( hashtable *ht, char *key )        {
82         int i,j,k;
83
84         strlwr( key );
85
86         k = hashtable_getkey( key );
87         i = 0;
88         
89         while(i < ht->size )    {
90                 j = (k+(i++)) & ht->and_mask;
91                 if ( ht->key[j] == NULL )
92                         return -1;
93                 if (!stricmp(ht->key[j], key ))
94                         return ht->value[j];
95         }
96         return -1;
97 }
98
99 void hashtable_insert( hashtable *ht, char *key, int value )    {
100         int i,j,k;
101
102         strlwr( key );
103         k = hashtable_getkey( key );
104         i = 0;
105
106         while(i < ht->size)     {
107                 j = (k+(i++)) & ht->and_mask;
108                 if ( ht->key[j] == NULL )       {
109                         ht->nitems++;
110                         ht->key[j] = key;
111                         ht->value[j] = value;
112                         return;
113                 } else if (!stricmp( key, ht->key[j] )) {
114                         return;
115                 }
116         }
117         Error( "Out of hash slots\n" );
118 }