2 #define INLINE inline __attribute__((always_inline))
4 chashtable_t *chashCreate(unsigned int size, double loadfactor) {
6 chashlistnode_t *nodes;
9 if((ctable = calloc(1, sizeof(chashtable_t))) == NULL) {
10 printf("Calloc error %s %d\n", __FILE__, __LINE__);
14 // Allocate space for the hash table
15 if((nodes = calloc(size, sizeof(chashlistnode_t))) == NULL) {
16 printf("Calloc error %s %d\n", __FILE__, __LINE__);
21 ctable->table = nodes;
22 ctable->loadfactor = loadfactor;
24 ctable->threshold=size*loadfactor;
25 ctable->mask = (size << 1)-1;
26 ctable->numelements = 0; // Initial number of elements in the hash
32 //Finds the right bin in the hash table
33 static INLINE unsigned int chashFunction(chashtable_t *table, unsigned int key) {
34 return ( key & (table->mask))>>1; //throw away low order bit
37 //Store objects and their pointers into hash
38 unsigned int chashInsert(chashtable_t *table, unsigned int key, void *val) {
41 chashlistnode_t *ptr, *node;
43 if(table->numelements > (table->threshold)) {
45 newsize = table->size << 1;
46 chashResize(table,newsize);
51 index = chashFunction(table, key);
53 printf("chashInsert(): DEBUG -> index = %d, key = %d, val = %x\n", index, key, val);
55 if(ptr[index].next == NULL && ptr[index].key == 0) { // Insert at the first position in the hashtable
58 } else { // Insert in the beginning of linked list
59 if ((node = calloc(1, sizeof(chashlistnode_t))) == NULL) {
60 printf("Calloc error %s, %d\n", __FILE__, __LINE__);
65 node->next = ptr[index].next;
66 ptr[index].next = node;
71 // Search for an address for a given oid
72 INLINE void * chashSearch(chashtable_t *table, unsigned int key) {
73 //REMOVE HASH FUNCTION CALL TO MAKE SURE IT IS INLINED HERE
74 chashlistnode_t *node = &table->table[(key & table->mask)>>1];
77 if(node->key == key) {
81 } while(node != NULL);
86 unsigned int chashRemove(chashtable_t *table, unsigned int key) {
87 return chashRemove2(table, key)==NULL;
91 void * chashRemove2(chashtable_t *table, unsigned int key) {
93 chashlistnode_t *curr, *prev;
94 chashlistnode_t *ptr, *node;
98 index = chashFunction(table,key);
101 for (; curr != NULL; curr = curr->next) {
102 if (curr->key == key) { // Find a match in the hash table
103 table->numelements--; // Decrement the number of elements in the global hashtable
104 if ((curr == &ptr[index]) && (curr->next == NULL)) { // Delete the first item inside the hashtable with no linked list of chashlistnode_t
108 } else if ((curr == &ptr[index]) && (curr->next != NULL)) { //Delete the first item with a linked list of chashlistnode_t connected
109 curr->key = curr->next->key;
111 curr->val = curr->next->val;
113 curr->next = curr->next->next;
115 } else { // Regular delete from linked listed
116 prev->next = curr->next;
127 unsigned int chashResize(chashtable_t *table, unsigned int newsize) {
128 chashlistnode_t *node, *ptr, *curr, *next; // curr and next keep track of the current and the next chashlistnodes in a linked list
129 unsigned int oldsize;
130 int isfirst; // Keeps track of the first element in the chashlistnode_t for each bin in hashtable
132 chashlistnode_t *newnode;
135 oldsize = table->size;
137 if((node = calloc(newsize, sizeof(chashlistnode_t))) == NULL) {
138 printf("Calloc error %s %d\n", __FILE__, __LINE__);
142 table->table = node; //Update the global hashtable upon resize()
143 table->size = newsize;
144 table->threshold = newsize * table->loadfactor;
145 table->mask = (newsize << 1)-1;
147 for(i = 0; i < oldsize; i++) { //Outer loop for each bin in hash table
150 while (curr != NULL) { //Inner loop to go through linked lists
151 if (curr->key == 0) { //Exit inner loop if there the first element for a given bin/index is NULL
152 break; //key = val =0 for element if not present within the hash table
156 index = chashFunction(table, curr->key);
158 printf("DEBUG(resize) -> index = %d, key = %d, val = %x\n", index, curr->key, curr->val);
160 // Insert into the new table
161 if(table->table[index].next == NULL && table->table[index].key == 0) {
162 table->table[index].key = curr->key;
163 table->table[index].val = curr->val;
164 table->numelements++;
166 if((newnode = calloc(1, sizeof(chashlistnode_t))) == NULL) {
167 printf("Calloc error %s, %d\n", __FILE__, __LINE__);
170 newnode->key = curr->key;
171 newnode->val = curr->val;
172 newnode->next = table->table[index].next;
173 table->table[index].next = newnode;
174 table->numelements++;
177 //free the linked list of chashlistnode_t if not the first element in the hash table
187 free(ptr); //Free the memory of the old hash table
191 //Delete the entire hash table
192 void chashDelete(chashtable_t *ctable) {
194 chashlistnode_t *ptr, *curr, *next;
197 for(i=0 ; i<ctable->size ; i++) {
200 while(curr != NULL) {