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) {
70 chashlistnode_t *ptr, *node;
73 index = chashFunction(table, key);
76 if(node->key == key) {
84 unsigned int chashRemove(chashtable_t *table, unsigned int key) {
86 chashlistnode_t *curr, *prev;
87 chashlistnode_t *ptr, *node;
90 index = chashFunction(table,key);
93 for (; curr != NULL; curr = curr->next) {
94 if (curr->key == key) { // Find a match in the hash table
95 table->numelements--; // Decrement the number of elements in the global hashtable
96 if ((curr == &ptr[index]) && (curr->next == NULL)) { // Delete the first item inside the hashtable with no linked list of chashlistnode_t
99 } else if ((curr == &ptr[index]) && (curr->next != NULL)) { //Delete the first item with a linked list of chashlistnode_t connected
100 curr->key = curr->next->key;
101 curr->val = curr->next->val;
103 curr->next = curr->next->next;
105 } else { // Regular delete from linked listed
106 prev->next = curr->next;
116 unsigned int chashResize(chashtable_t *table, unsigned int newsize) {
117 chashlistnode_t *node, *ptr, *curr, *next; // curr and next keep track of the current and the next chashlistnodes in a linked list
118 unsigned int oldsize;
119 int isfirst; // Keeps track of the first element in the chashlistnode_t for each bin in hashtable
121 chashlistnode_t *newnode;
124 oldsize = table->size;
126 if((node = calloc(newsize, sizeof(chashlistnode_t))) == NULL) {
127 printf("Calloc error %s %d\n", __FILE__, __LINE__);
131 table->table = node; //Update the global hashtable upon resize()
132 table->size = newsize;
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 *table) {
182 chashlistnode_t *ptr, *curr, *next;
185 for(i=0 ; i<table->size ; i++) {
188 while(curr != NULL) {