2 #define INLINE inline __attribute__((always_inline))
4 chashtable_t *chashCreate(unsigned int size, float 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;
23 ctable->mask = (size << 1)-1;
24 ctable->numelements = 0; // Initial number of elements in the hash
25 ctable->loadfactor = loadfactor;
30 //Finds the right bin in the hash table
31 static INLINE unsigned int chashFunction(chashtable_t *table, unsigned int key) {
32 return ( key & (table->mask))>>1;//throw away low order bit
35 //Store objects and their pointers into hash
36 unsigned int chashInsert(chashtable_t *table, unsigned int key, void *val) {
39 chashlistnode_t *ptr, *node;
41 if(table->numelements > (table->loadfactor * table->size)) {
43 newsize = table->size << 1;
44 chashResize(table,newsize);
49 index = chashFunction(table, key);
51 printf("chashInsert(): DEBUG -> index = %d, key = %d, val = %x\n", index, key, val);
53 if(ptr[index].next == NULL && ptr[index].key == 0) { // Insert at the first position in the hashtable
56 } else { // Insert in the beginning of linked list
57 if ((node = calloc(1, sizeof(chashlistnode_t))) == NULL) {
58 printf("Calloc error %s, %d\n", __FILE__, __LINE__);
63 node->next = ptr[index].next;
64 ptr[index].next = node;
69 // Search for an address for a given oid
70 INLINE void * chashSearch(chashtable_t *table, unsigned int key) {
71 //REMOVE HASH FUNCTION CALL TO MAKE SURE IT IS INLINED HERE
72 chashlistnode_t *node = &table->table[(key & table->mask)>>1];
75 if(node->key == key) {
83 unsigned int chashRemove(chashtable_t *table, unsigned int key) {
85 chashlistnode_t *curr, *prev;
86 chashlistnode_t *ptr, *node;
89 index = chashFunction(table,key);
92 for (; curr != NULL; curr = curr->next) {
93 if (curr->key == key) { // Find a match in the hash table
94 table->numelements--; // Decrement the number of elements in the global hashtable
95 if ((curr == &ptr[index]) && (curr->next == NULL)) { // Delete the first item inside the hashtable with no linked list of chashlistnode_t
98 } else if ((curr == &ptr[index]) && (curr->next != NULL)) { //Delete the first item with a linked list of chashlistnode_t connected
99 curr->key = curr->next->key;
100 curr->val = curr->next->val;
102 curr->next = curr->next->next;
104 } else { // Regular delete from linked listed
105 prev->next = curr->next;
115 unsigned int chashResize(chashtable_t *table, unsigned int newsize) {
116 chashlistnode_t *node, *ptr, *curr, *next; // curr and next keep track of the current and the next chashlistnodes in a linked list
117 unsigned int oldsize;
118 int isfirst; // Keeps track of the first element in the chashlistnode_t for each bin in hashtable
120 chashlistnode_t *newnode;
123 oldsize = table->size;
125 if((node = calloc(newsize, sizeof(chashlistnode_t))) == NULL) {
126 printf("Calloc error %s %d\n", __FILE__, __LINE__);
130 table->table = node; //Update the global hashtable upon resize()
131 table->size = newsize;
132 table->mask = (newsize << 1)-1;
133 table->numelements = 0;
135 for(i = 0; i < oldsize; i++) { //Outer loop for each bin in hash table
138 while (curr != NULL) { //Inner loop to go through linked lists
139 if (curr->key == 0) { //Exit inner loop if there the first element for a given bin/index is NULL
140 break; //key = val =0 for element if not present within the hash table
144 index = chashFunction(table, curr->key);
146 printf("DEBUG(resize) -> index = %d, key = %d, val = %x\n", index, curr->key, curr->val);
148 // Insert into the new table
149 if(table->table[index].next == NULL && table->table[index].key == 0) {
150 table->table[index].key = curr->key;
151 table->table[index].val = curr->val;
152 table->numelements++;
154 if((newnode = calloc(1, sizeof(chashlistnode_t))) == NULL) {
155 printf("Calloc error %s, %d\n", __FILE__, __LINE__);
158 newnode->key = curr->key;
159 newnode->val = curr->val;
160 newnode->next = table->table[index].next;
161 table->table[index].next = newnode;
162 table->numelements++;
165 //free the linked list of chashlistnode_t if not the first element in the hash table
175 free(ptr); //Free the memory of the old hash table
179 //Delete the entire hash table
180 void chashDelete(chashtable_t *ctable) {
182 chashlistnode_t *ptr, *curr, *next;
185 for(i=0 ; i<ctable->size ; i++) {
188 while(curr != NULL) {