2 * $Logfile: /Freespace2/code/localization/fhash.cpp $
9 * Revision 1.1 2002/05/03 03:28:09 root
13 * 5 2/23/99 9:25a Dave
14 * Stubbed out a bunch of stuff to get cfile and lcl functions in.
16 * 4 12/01/98 4:46p Dave
17 * Put in targa bitmap support (16 bit).
28 // -----------------------------------------------------------------------------------------------
33 typedef struct fhash_node {
34 char *str; // allocated dynamically
36 fhash_node *next, *prev; // for chaining in an individual hash table entry (non-circular, doubly linked list)
39 // hash table itself (with chained nodes)
40 #define HASH_TABLE_SIZE 253 // works better when not a power of 2, and is prime
41 fhash_node *Hash_table_fred[HASH_TABLE_SIZE];
43 // if the hash table is active
47 // -----------------------------------------------------------------------------------------------
48 // HASH FORWARD DECLARATIONS
51 // hash a string. return an index into the hash table where it should be inserted
52 int fhash_get_hash_index(char *str);
54 // insert a string into hash table index N, will take care of allocating/chaining everything
55 void fhash_insert(char *str, int id, int n);
58 // -----------------------------------------------------------------------------------------------
62 // initialize the hash table
65 memset(Hash_table_fred, 0, sizeof(fhash_node) * HASH_TABLE_SIZE);
68 // set the hash table to be active for parsing
74 // set the hash table to be inactive for parsing
75 void fhash_deactivate()
80 // if the hash table is active
86 // flush out the hash table, freeing up everything
90 fhash_node *moveup, *backup;
92 // go through each element
93 for(idx=0; idx<HASH_TABLE_SIZE; idx++){
94 if(Hash_table_fred[idx] != NULL){
95 moveup = Hash_table_fred[idx];
96 while(moveup != NULL){
99 moveup = moveup->next;
101 // free up this element
102 if(backup->str != NULL){
109 Hash_table_fred[idx] = NULL;
114 // add a string with the given id# to the has table
115 void fhash_add_str(char *str, int id)
119 // if the hash table isn't active, don't bother
120 Assert(Fhash_active);
125 // determine where the string goes in the has table
130 hash_index = fhash_get_hash_index(str);
132 // insert into the hash table
133 fhash_insert(str, id, hash_index);
136 // determine if the passed string exists in the table
137 // returns : -2 if the string doesn't exit, or >= -1 as the string id # otherwise
138 int fhash_string_exists(char *str)
148 // get the hash index for this string
149 hash_index = fhash_get_hash_index(str);
151 // if there are no entries, it doesn't exist
152 if(Hash_table_fred[hash_index] == NULL){
156 // otherwise compare against all strings in this code
157 moveup = Hash_table_fred[hash_index];
158 while(moveup != NULL){
159 // do a string compare on this item
160 Assert(moveup->str != NULL);
161 if(moveup->str != NULL){
162 if(!strcmp(moveup->str, str)){
168 moveup = moveup->next;
176 // -----------------------------------------------------------------------------------------------
177 // HASH FORWARD DEFINITIONS
180 // hash a string. return an index into the hash table where it should be inserted
181 int fhash_get_hash_index(char *str)
188 str_len = strlen(str);
189 for(idx=0; idx<str_len; idx++){
193 ret = abs(accum) % 253;
197 // insert a string into hash table index N, will take care of allocating/chaining everything
198 void fhash_insert(char *str, int id, int n)
200 fhash_node *new_node;
203 // allocate the new node
204 new_node = (fhash_node*)malloc(sizeof(fhash_node));
206 if(new_node == NULL){
211 new_node->str = strdup(str);
213 new_node->next = NULL;
214 new_node->prev = NULL;
216 // if this hash index is NULL, just assign it
217 if(Hash_table_fred[n] == NULL){
218 Hash_table_fred[n] = new_node;
220 moveup = Hash_table_fred[n];
221 while(moveup->next != NULL){
222 moveup = moveup->next;
224 new_node->prev = moveup;
225 moveup->next = new_node;