3 chashtable_t *chashCreate(unsigned int size, float loadfactor) {
5 chashlistnode_t *nodes;
8 if((ctable = calloc(1, sizeof(chashtable_t))) == NULL) {
9 printf("Calloc error %s %d\n", __FILE__, __LINE__);
13 // Allocate space for the hash table
14 if((nodes = calloc(size, sizeof(chashlistnode_t))) == NULL) {
15 printf("Calloc error %s %d\n", __FILE__, __LINE__);
20 ctable->table = nodes;
22 ctable->numelements = 0; // Initial number of elements in the hash
23 ctable->loadfactor = loadfactor;
28 //Finds the right bin in the hash table
29 unsigned int chashFunction(chashtable_t *table, unsigned int key) {
30 return ( key % (table->size));
33 //Store objects and their pointers into hash
34 unsigned int chashInsert(chashtable_t *table, unsigned int key, void *val) {
37 chashlistnode_t *ptr, *node;
39 if(table->numelements > (table->loadfactor * table->size)) {
41 newsize = 2 * table->size + 1;
42 chashResize(table,newsize);
47 index = chashFunction(table, key);
49 printf("chashInsert(): DEBUG -> index = %d, key = %d, val = %x\n", index, key, val);
51 if(ptr[index].next == NULL && ptr[index].key == 0) { // Insert at the first position in the hashtable
54 } else { // Insert in the beginning of linked list
55 if ((node = calloc(1, sizeof(chashlistnode_t))) == NULL) {
56 printf("Calloc error %s, %d\n", __FILE__, __LINE__);
61 node->next = ptr[index].next;
62 ptr[index].next = node;
67 // Search for an address for a given oid
68 void *chashSearch(chashtable_t *table, unsigned int key) {
69 int index = chashFunction(table, key);
70 chashlistnode_t *ptr =table -> table;
71 chashlistnode_t *node = &ptr[index];
74 if(node->key == key) {
82 unsigned int chashRemove(chashtable_t *table, unsigned int key) {
84 chashlistnode_t *curr, *prev;
85 chashlistnode_t *ptr, *node;
88 index = chashFunction(table,key);
91 for (; curr != NULL; curr = curr->next) {
92 if (curr->key == key) { // Find a match in the hash table
93 table->numelements--; // Decrement the number of elements in the global hashtable
94 if ((curr == &ptr[index]) && (curr->next == NULL)) { // Delete the first item inside the hashtable with no linked list of chashlistnode_t
97 } else if ((curr == &ptr[index]) && (curr->next != NULL)) { //Delete the first item with a linked list of chashlistnode_t connected
98 curr->key = curr->next->key;
99 curr->val = curr->next->val;
101 curr->next = curr->next->next;
103 } else { // Regular delete from linked listed
104 prev->next = curr->next;
114 unsigned int chashResize(chashtable_t *table, unsigned int newsize) {
115 chashlistnode_t *node, *ptr, *curr, *next; // curr and next keep track of the current and the next chashlistnodes in a linked list
116 unsigned int oldsize;
117 int isfirst; // Keeps track of the first element in the chashlistnode_t for each bin in hashtable
119 chashlistnode_t *newnode;
122 oldsize = table->size;
124 if((node = calloc(newsize, sizeof(chashlistnode_t))) == NULL) {
125 printf("Calloc error %s %d\n", __FILE__, __LINE__);
129 table->table = node; //Update the global hashtable upon resize()
130 table->size = newsize;
131 table->numelements = 0;
133 for(i = 0; i < oldsize; i++) { //Outer loop for each bin in hash table
136 while (curr != NULL) { //Inner loop to go through linked lists
137 if (curr->key == 0) { //Exit inner loop if there the first element for a given bin/index is NULL
138 break; //key = val =0 for element if not present within the hash table
142 index = chashFunction(table, curr->key);
144 printf("DEBUG(resize) -> index = %d, key = %d, val = %x\n", index, curr->key, curr->val);
146 // Insert into the new table
147 if(table->table[index].next == NULL && table->table[index].key == 0) {
148 table->table[index].key = curr->key;
149 table->table[index].val = curr->val;
150 table->numelements++;
152 if((newnode = calloc(1, sizeof(chashlistnode_t))) == NULL) {
153 printf("Calloc error %s, %d\n", __FILE__, __LINE__);
156 newnode->key = curr->key;
157 newnode->val = curr->val;
158 newnode->next = table->table[index].next;
159 table->table[index].next = newnode;
160 table->numelements++;
163 //free the linked list of chashlistnode_t if not the first element in the hash table
173 free(ptr); //Free the memory of the old hash table
177 //Delete the entire hash table
178 void chashDelete(chashtable_t *ctable) {
180 chashlistnode_t *ptr, *curr, *next;
183 for(i=0 ; i<ctable->size ; i++) {
186 while(curr != NULL) {