3 cachehashtable_t *cachehashCreate(unsigned int size, float loadfactor) {
4 cachehashtable_t *ctable;
5 cachehashlistnode_t *nodes;
8 if((ctable = calloc(1, sizeof(cachehashtable_t))) == NULL) {
9 printf("Calloc error %s %d\n", __FILE__, __LINE__);
13 // Allocate space for the hash table
14 if((nodes = calloc(HASH_SIZE, sizeof(cachehashlistnode_t))) == NULL) {
15 printf("Calloc error %s %d\n", __FILE__, __LINE__);
19 ctable->table = nodes;
21 ctable->numelements = 0; // Initial number of elements in the hash
22 ctable->loadfactor = loadfactor;
27 //Finds the right bin in the hash table
28 unsigned int cachehashFunction(cachehashtable_t *table, unsigned int key) {
29 return ( key % (table->size));
32 //Store objects and their pointers into hash
33 unsigned int cachehashInsert(cachehashtable_t *table, unsigned int key, void *val) {
36 cachehashlistnode_t *ptr, *node;
38 if(table->numelements > (table->loadfactor * table->size)) {
40 newsize = 2 * table->size + 1;
41 cachehashResize(table,newsize);
46 index = cachehashFunction(table, key);
48 printf("DEBUG -> index = %d, key = %d, val = %x\n", index, key, val);
50 if(ptr[index].next == NULL && ptr[index].key == 0) { // Insert at the first position in the hashtable
53 } else { // Insert in the beginning of linked list
54 if ((node = calloc(1, sizeof(cachehashlistnode_t))) == NULL) {
55 printf("Calloc error %s, %d\n", __FILE__, __LINE__);
60 node->next = ptr[index].next;
61 ptr[index].next = node;
66 // Search for an address for a given oid
67 void *cachehashSearch(cachehashtable_t *table, unsigned int key) {
69 cachehashlistnode_t *ptr, *node;
72 index = cachehashFunction(table, key);
75 if(node->key == key) {
83 unsigned int cachehashRemove(cachehashtable_t *table, unsigned int key) {
85 cachehashlistnode_t *curr, *prev;
86 cachehashlistnode_t *ptr, *node;
89 index = cachehashFunction(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 cachehashlistnode_t
98 } else if ((curr == &ptr[index]) && (curr->next != NULL)) { //Delete the first item with a linked list of cachehashlistnode_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 cachehashResize(cachehashtable_t *table, unsigned int newsize) {
116 cachehashlistnode_t *node, *ptr, *curr, *next; // curr and next keep track of the current and the next cachehashlistnodes in a linked list
117 unsigned int oldsize;
118 int isfirst; // Keeps track of the first element in the cachehashlistnode_t for each bin in hashtable
120 cachehashlistnode_t *newnode;
123 oldsize = table->size;
125 if((node = calloc(newsize, sizeof(cachehashlistnode_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->numelements = 0;
134 for(i = 0; i < oldsize; i++) { //Outer loop for each bin in hash table
137 while (curr != NULL) { //Inner loop to go through linked lists
138 if (curr->key == 0) { //Exit inner loop if there the first element for a given bin/index is NULL
139 break; //key = val =0 for element if not present within the hash table
143 index = cachehashFunction(table, curr->key);
145 printf("DEBUG(resize) -> index = %d, key = %d, val = %x\n", index, curr->key, curr->val);
147 // Insert into the new table
148 if(table->table[index].next == NULL && table->table[index].key == 0) {
149 table->table[index].key = curr->key;
150 table->table[index].val = curr->val;
151 table->numelements++;
153 if((newnode = calloc(1, sizeof(cachehashlistnode_t))) == NULL) {
154 printf("Calloc error %s, %d\n", __FILE__, __LINE__);
157 newnode->key = curr->key;
158 newnode->val = curr->val;
159 newnode->next = table->table[index].next;
160 table->table[index].next = newnode;
161 table->numelements++;
164 //free the linked list of cachehashlistnode_t if not the first element in the hash table
174 free(ptr); //Free the memory of the old hash table
178 //Delete the entire hash table
179 void cachehashDelete(cachehashtable_t *table) {
181 cachehashlistnode_t *ptr, *curr, *next;
184 for(i=0 ; i<table->size ; i++) {
187 while(curr != NULL) {