FS2_Open
Open source remastering of the Freespace 2 engine
fhash.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) Volition, Inc. 1999. All rights reserved.
3  *
4  * All source code herein is the property of Volition, Inc. You may not sell
5  * or otherwise commercially exploit the source or things you created based on the
6  * source.
7  *
8 */
9 
10 
11 
12 #include <stdlib.h>
13 #include <memory.h>
14 #include <string.h>
15 #include "globalincs/pstypes.h"
16 #include "localization/fhash.h"
17 
18 
19 
20 // -----------------------------------------------------------------------------------------------
21 // HASH DEFINES/VARS
22 //
23 
24 // hash node
25 typedef struct fhash_node {
26  char *str; // allocated dynamically
27  int id; // can be -1
28  fhash_node *next, *prev; // for chaining in an individual hash table entry (non-circular, doubly linked list)
29 } fhash_node;
30 
31 // hash table itself (with chained nodes)
32 #define HASH_TABLE_SIZE 253 // works better when not a power of 2, and is prime
34 
35 // if the hash table is active
36 int Fhash_active = 0;
37 
38 
39 // -----------------------------------------------------------------------------------------------
40 // HASH FORWARD DECLARATIONS
41 //
42 
43 // hash a string. return an index into the hash table where it should be inserted
44 int fhash_get_hash_index(const char *str);
45 
46 // insert a string into hash table index N, will take care of allocating/chaining everything
47 void fhash_insert(const char *str, int id, int n);
48 
49 
50 // -----------------------------------------------------------------------------------------------
51 // HASH FUNCTIONS
52 //
53 
54 // initialize the hash table
55 void fhash_init()
56 {
57  memset(Hash_table_fred, 0, sizeof(fhash_node *) * HASH_TABLE_SIZE);
58 }
59 
60 // set the hash table to be active for parsing
62 {
63  Fhash_active = 1;
64 }
65 
66 // set the hash table to be inactive for parsing
68 {
69  Fhash_active = 0;
70 }
71 
72 // if the hash table is active
74 {
75  return Fhash_active;
76 }
77 
78 // flush out the hash table, freeing up everything
80 {
81  int idx;
82  fhash_node *moveup, *backup;
83 
84  // go through each element
85  for(idx=0; idx<HASH_TABLE_SIZE; idx++){
86  if(Hash_table_fred[idx] != NULL){
87  moveup = Hash_table_fred[idx];
88  while(moveup != NULL){
89  // next element
90  backup = moveup;
91  moveup = moveup->next;
92 
93  // free up this element
94  if(backup->str != NULL){
95  vm_free(backup->str);
96  }
97  vm_free(backup);
98  }
99 
100  // null this element
101  Hash_table_fred[idx] = NULL;
102  }
103  }
104 }
105 
106 // add a string with the given id# to the hash table
107 void fhash_add_str(const char *str, int id)
108 {
109  int hash_index;
110 
111  // if the hash table isn't active, don't bother
113  if(!Fhash_active){
114  return;
115  }
116 
117  // determine where the string goes in the hash table
118  Assert(str != NULL);
119  if(str == NULL){
120  return;
121  }
122  hash_index = fhash_get_hash_index(str);
123 
124  // insert into the hash table
125  fhash_insert(str, id, hash_index);
126 }
127 
128 // determine if the passed string exists in the table
129 // returns : -2 if the string doesn't exit, or >= -1 as the string id # otherwise
130 int fhash_string_exists(const char *str)
131 {
132  int hash_index;
133  fhash_node *moveup;
134 
135  Assert(str != NULL);
136  if(str == NULL){
137  return -2;
138  }
139 
140  // get the hash index for this string
141  hash_index = fhash_get_hash_index(str);
142 
143  // if there are no entries, it doesn't exist
144  if(Hash_table_fred[hash_index] == NULL){
145  return -2;
146  }
147 
148  // otherwise compare against all strings in this code
149  moveup = Hash_table_fred[hash_index];
150  while(moveup != NULL){
151  // do a string compare on this item
152  Assert(moveup->str != NULL);
153  if(moveup->str != NULL){
154  if(!strcmp(moveup->str, str)){
155  return moveup->id;
156  }
157  }
158 
159  // next item
160  moveup = moveup->next;
161  }
162 
163  // didn't find it
164  return -2;
165 }
166 
167 
168 // -----------------------------------------------------------------------------------------------
169 // HASH FORWARD DEFINITIONS
170 //
171 
172 // hash a string. return an index into the hash table where it should be inserted
173 int fhash_get_hash_index(const char *str)
174 {
175  int accum = 0;
176  int idx, str_len;
177  int ret;
178 
179  // add up the string
180  str_len = strlen(str);
181  for(idx=0; idx<str_len; idx++){
182  accum += str[idx];
183  }
184 
185  ret = abs(accum) % 253;
186  return ret;
187 }
188 
189 // insert a string into hash table index N, will take care of allocating/chaining everything
190 void fhash_insert(const char *str, int id, int n)
191 {
192  fhash_node *new_node;
193  fhash_node *moveup;
194 
195  // allocate the new node
196  new_node = (fhash_node*)vm_malloc(sizeof(fhash_node));
197  Assert(new_node);
198  if(new_node == NULL){
199  return;
200  }
201 
202  // fill in the node
203  new_node->str = vm_strdup(str);
204  new_node->id = id;
205  new_node->next = NULL;
206  new_node->prev = NULL;
207 
208  // if this hash index is NULL, just assign it
209  if(Hash_table_fred[n] == NULL){
210  Hash_table_fred[n] = new_node;
211  } else {
212  moveup = Hash_table_fred[n];
213  while(moveup->next != NULL){
214  moveup = moveup->next;
215  }
216  new_node->prev = moveup;
217  moveup->next = new_node;
218  }
219 }
#define vm_free(ptr)
Definition: pstypes.h:548
void fhash_init()
Definition: fhash.cpp:55
void fhash_flush()
Definition: fhash.cpp:79
int fhash_active()
Definition: fhash.cpp:73
#define HASH_TABLE_SIZE
Definition: fhash.cpp:32
Assert(pm!=NULL)
int fhash_string_exists(const char *str)
Definition: fhash.cpp:130
int Fhash_active
Definition: fhash.cpp:36
fhash_node * prev
Definition: fhash.cpp:28
char * str
Definition: fhash.cpp:26
fhash_node * Hash_table_fred[HASH_TABLE_SIZE]
Definition: fhash.cpp:33
#define vm_strdup(ptr)
Definition: pstypes.h:549
GLenum GLuint id
Definition: Glext.h:5156
void fhash_deactivate()
Definition: fhash.cpp:67
int fhash_get_hash_index(const char *str)
Definition: fhash.cpp:173
int idx
Definition: multiui.cpp:761
GLclampd n
Definition: Glext.h:7286
#define vm_malloc(size)
Definition: pstypes.h:547
struct fhash_node fhash_node
void fhash_activate()
Definition: fhash.cpp:61
void fhash_add_str(const char *str, int id)
Definition: fhash.cpp:107
fhash_node * next
Definition: fhash.cpp:28
void fhash_insert(const char *str, int id, int n)
Definition: fhash.cpp:190
int id
Definition: fhash.cpp:27