4 __thread chashlistnode_t *c_table;
5 __thread chashlistnode_t *c_list;
6 __thread unsigned int c_size;
7 __thread unsigned INTPTR c_mask;
8 __thread unsigned int c_numelements;
9 __thread unsigned int c_threshold;
10 __thread double c_loadfactor;
11 __thread cliststruct_t *c_structs;
14 __thread chashlistnode_t *dc_c_table;
15 __thread chashlistnode_t *dc_c_list;
16 __thread unsigned int dc_c_size;
17 __thread unsigned INTPTR dc_c_mask;
18 __thread unsigned int dc_c_numelements;
19 __thread unsigned int dc_c_threshold;
20 __thread double dc_c_loadfactor;
21 __thread cliststruct_t *dc_c_structs;
23 void dc_t_chashCreate(unsigned int size, double loadfactor) {
25 chashlistnode_t *nodes;
28 // Allocate space for the hash table
30 dc_c_table = calloc(size, sizeof(chashlistnode_t));
31 dc_c_loadfactor = loadfactor;
33 dc_c_threshold=size*loadfactor;
34 dc_c_mask = (size << 4)-1;
35 dc_c_structs=calloc(1, sizeof(cliststruct_t));
36 dc_c_numelements = 0; // Initial number of elements in the hash
40 void dc_t_chashreset() {
41 chashlistnode_t *ptr = dc_c_table;
44 if (dc_c_numelements<(dc_c_size>>4)) {
45 chashlistnode_t *top=&ptr[dc_c_size];
46 chashlistnode_t *tmpptr=dc_c_list;
48 chashlistnode_t *next=tmpptr->lnext;
49 if (tmpptr>=ptr&&tmpptr<top) {
57 bzero(dc_c_table, sizeof(chashlistnode_t)*dc_c_size);
59 while(dc_c_structs->next!=NULL) {
60 cliststruct_t *next=dc_c_structs->next;
64 dc_c_structs->num = 0;
69 //Store objects and their pointers into hash
70 void dc_t_chashInsertOnce(void * key, void *val) {
74 if(dc_c_numelements > (dc_c_threshold)) {
76 unsigned int newsize = dc_c_size << 1;
77 dc_t_chashResize(newsize);
80 ptr = &dc_c_table[(((unsigned INTPTR)key)&dc_c_mask)>>4];
88 } else { // Insert in the beginning of linked list
89 chashlistnode_t * node;
90 chashlistnode_t *search=ptr;
92 //make sure it isn't here
94 if(search->key == key) {
98 } while(search != NULL);
101 if (dc_c_structs->num<NUMCLIST) {
102 node=&dc_c_structs->array[dc_c_structs->num];
106 cliststruct_t *tcl=calloc(1,sizeof(cliststruct_t));
107 tcl->next=dc_c_structs;
114 node->next = ptr->next;
116 node->lnext=dc_c_list;
121 unsigned int dc_t_chashResize(unsigned int newsize) {
122 chashlistnode_t *node, *ptr, *curr; // curr and next keep track of the current and the next chashlistnodes in a linked list
123 unsigned int oldsize;
124 int isfirst; // Keeps track of the first element in the chashlistnode_t for each bin in hashtable
125 unsigned int i,index;
132 if((node = calloc(newsize, sizeof(chashlistnode_t))) == NULL) {
133 printf("Calloc error %s %d\n", __FILE__, __LINE__);
137 dc_c_table = node; //Update the global hashtable upon resize()
139 dc_c_threshold = newsize * dc_c_loadfactor;
140 mask=dc_c_mask = (newsize << 4)-1;
142 for(i = 0; i < oldsize; i++) { //Outer loop for each bin in hash table
145 do { //Inner loop to go through linked lists
147 chashlistnode_t *tmp,*next;
149 if ((key=curr->key) == 0) { //Exit inner loop if there the first element is 0
150 break; //key = val =0 for element if not present within the hash table
152 index = (((unsigned INTPTR)key) & mask) >>4;
155 // Insert into the new table
158 tmp->val = curr->val;
159 tmp->lnext=dc_c_list;
162 NOTE: Add this case if you change this...
163 This case currently never happens because of the way things rehash....
165 chashlistnode_t *newnode= calloc(1, sizeof(chashlistnode_t));
166 newnode->key = curr->key;
167 newnode->val = curr->val;
168 newnode->next = tmp->next;
172 curr->next=tmp->next;
174 curr->lnext=dc_c_list;
183 free(ptr); //Free the memory of the old hash table
187 //Delete the entire hash table
188 void dc_t_chashDelete() {
190 cliststruct_t *ptr=dc_c_structs;
192 cliststruct_t *next=ptr->next;
203 void t_chashCreate(unsigned int size, double loadfactor) {
204 chashtable_t *ctable;
205 chashlistnode_t *nodes;
208 // Allocate space for the hash table
211 c_table = calloc(size, sizeof(chashlistnode_t));
212 c_loadfactor = loadfactor;
214 c_threshold=size*loadfactor;
215 c_mask = (size << 4)-1;
216 c_structs=calloc(1, sizeof(cliststruct_t));
217 c_numelements = 0; // Initial number of elements in the hash
221 void t_chashreset() {
222 chashlistnode_t *ptr = c_table;
225 if (c_numelements<(c_size>>4)) {
226 chashlistnode_t *top=&ptr[c_size];
227 chashlistnode_t *tmpptr=c_list;
228 while(tmpptr!=NULL) {
229 chashlistnode_t *next=tmpptr->lnext;
230 if (tmpptr>=ptr&&tmpptr<top) {
238 bzero(c_table, sizeof(chashlistnode_t)*c_size);
240 while(c_structs->next!=NULL) {
241 cliststruct_t *next=c_structs->next;
250 //Store objects and their pointers into hash
251 void t_chashInsert(void * key, void *val) {
252 chashlistnode_t *ptr;
255 if(c_numelements > (c_threshold)) {
257 unsigned int newsize = c_size << 1;
258 t_chashResize(newsize);
261 ptr = &c_table[(((unsigned INTPTR)key)&c_mask)>>4];
269 } else { // Insert in the beginning of linked list
270 chashlistnode_t * node;
271 if (c_structs->num<NUMCLIST) {
272 node=&c_structs->array[c_structs->num];
276 cliststruct_t *tcl=calloc(1,sizeof(cliststruct_t));
284 node->next = ptr->next;
291 // Search for an address for a given oid
292 INLINE void * t_chashSearch(void * key) {
293 //REMOVE HASH FUNCTION CALL TO MAKE SURE IT IS INLINED HERE
294 chashlistnode_t *node = &c_table[(((unsigned INTPTR)key) & c_mask)>>4];
297 if(node->key == key) {
301 } while(node != NULL);
306 unsigned int t_chashResize(unsigned int newsize) {
307 chashlistnode_t *node, *ptr, *curr; // curr and next keep track of the current and the next chashlistnodes in a linked list
308 unsigned int oldsize;
309 int isfirst; // Keeps track of the first element in the chashlistnode_t for each bin in hashtable
310 unsigned int i,index;
317 if((node = calloc(newsize, sizeof(chashlistnode_t))) == NULL) {
318 printf("Calloc error %s %d\n", __FILE__, __LINE__);
322 c_table = node; //Update the global hashtable upon resize()
324 c_threshold = newsize * c_loadfactor;
325 mask=c_mask = (newsize << 4)-1;
327 for(i = 0; i < oldsize; i++) { //Outer loop for each bin in hash table
330 do { //Inner loop to go through linked lists
332 chashlistnode_t *tmp,*next;
334 if ((key=curr->key) == 0) { //Exit inner loop if there the first element is 0
335 break; //key = val =0 for element if not present within the hash table
337 index = (((unsigned INTPTR)key) & mask) >>4;
340 // Insert into the new table
343 tmp->val = curr->val;
347 NOTE: Add this case if you change this...
348 This case currently never happens because of the way things rehash....
350 chashlistnode_t *newnode= calloc(1, sizeof(chashlistnode_t));
351 newnode->key = curr->key;
352 newnode->val = curr->val;
353 newnode->next = tmp->next;
357 curr->next=tmp->next;
368 free(ptr); //Free the memory of the old hash table
372 //Delete the entire hash table
373 void t_chashDelete() {
375 cliststruct_t *ptr=c_structs;
377 cliststruct_t *next=ptr->next;