3 mhashtable_t mlookup; //Global hash table
5 unsigned int mhashCreate(unsigned int size, float loadfactor) {
6 mhashlistnode_t *nodes;
9 // Allocate space for the hash table
10 if((nodes = calloc(HASH_SIZE, sizeof(mhashlistnode_t))) == NULL) {
11 printf("Calloc error %s %d\n", __FILE__, __LINE__);
15 mlookup.table = nodes;
17 mlookup.numelements = 0; // Initial number of elements in the hash
18 mlookup.loadfactor = loadfactor;
22 // Assign to keys to bins inside hash table
23 unsigned int mhashFunction(unsigned int key) {
24 return( key % (mlookup.size));
27 // Insert value and key mapping into the hash table
28 unsigned int mhashInsert(unsigned int key, void *val) {
31 mhashlistnode_t *ptr, *node;
33 if (mlookup.numelements > (mlookup.loadfactor * mlookup.size)) {
35 newsize = 2 * mlookup.size + 1;
39 mlookup.numelements++;
41 index = mhashFunction(key);
43 printf("DEBUG -> index = %d, key = %d, val = %x\n", index, key, val);
45 if(ptr[index].next == NULL && ptr[index].key == 0) { // Insert at the first position in the hashtable
48 } else { // Insert in the beginning of linked list
49 if ((node = calloc(1, sizeof(mhashlistnode_t))) == NULL) {
50 printf("Calloc error %s, %d\n", __FILE__, __LINE__);
55 node->next = ptr[index].next;
56 ptr[index].next = node;
61 // Return val for a given key in the hash table
62 void *mhashSearch(unsigned int key) {
64 mhashlistnode_t *ptr, *node;
66 ptr = mlookup.table; // Address of the beginning of hash table
67 index = mhashFunction(key);
70 if(node->key == key) {
78 // Remove an entry from the hash table
79 unsigned int mhashRemove(unsigned int key) {
81 mhashlistnode_t *curr, *prev;
82 mhashlistnode_t *ptr, *node;
85 index = mhashFunction(key);
88 for (; curr != NULL; curr = curr->next) {
89 if (curr->key == key) { // Find a match in the hash table
90 mlookup.numelements--; // Decrement the number of elements in the global hashtable
91 if ((curr == &ptr[index]) && (curr->next == NULL)) { // Delete the first item inside the hashtable with no linked list of mhashlistnode_t
94 } else if ((curr == &ptr[index]) && (curr->next != NULL)) { //Delete the first item with a linked list of mhashlistnode_t connected
95 curr->key = curr->next->key;
96 curr->val = curr->next->val;
98 curr->next = curr->next->next;
100 } else { // Regular delete from linked listed
101 prev->next = curr->next;
112 unsigned int mhashResize(unsigned int newsize) {
113 mhashlistnode_t *node, *ptr, *curr, *next; // curr and next keep track of the current and the next mhashlistnodes in a linked list
114 unsigned int oldsize;
115 int isfirst; // Keeps track of the first element in the mhashlistnode_t for each bin in hashtable
117 mhashlistnode_t *newnode;
120 oldsize = mlookup.size;
122 if((node = calloc(newsize, sizeof(mhashlistnode_t))) == NULL) {
123 printf("Calloc error %s %d\n", __FILE__, __LINE__);
127 mlookup.table = node; //Update the global hashtable upon resize()
128 mlookup.size = newsize;
129 mlookup.numelements = 0;
131 for(i = 0; i < oldsize; i++) { //Outer loop for each bin in hash table
134 while (curr != NULL) { //Inner loop to go through linked lists
135 if (curr->key == 0) { //Exit inner loop if there the first element for a given bin/index is NULL
136 break; //key = val =0 for element if not present within the hash table
140 index = mhashFunction(curr->key);
142 printf("DEBUG(resize) -> index = %d, key = %d, val = %x\n", index, curr->key, curr->val);
144 // Insert into the new table
145 if(mlookup.table[index].next == NULL && mlookup.table[index].key == 0) {
146 mlookup.table[index].key = curr->key;
147 mlookup.table[index].val = curr->val;
148 mlookup.numelements++;
150 if((newnode = calloc(1, sizeof(mhashlistnode_t))) == NULL) {
151 printf("Calloc error %s, %d\n", __FILE__, __LINE__);
154 newnode->key = curr->key;
155 newnode->val = curr->val;
156 newnode->next = mlookup.table[index].next;
157 mlookup.table[index].next = newnode;
158 mlookup.numelements++;
161 //free the linked list of mhashlistnode_t if not the first element in the hash table
172 free(ptr); //Free the memory of the old hash table
178 vkey resize(obj_addr_table_t * table){
179 int newCapacity = 2*(table->size) + 1;
180 obj_listnode_t **old;
181 //if ((table->hash = (obj_listnode_t **) malloc(sizeof(obj_listnode_t *)*size)) == NULL) {
184 // Hashing for the Key
185 int hashKey(unsigned int key, obj_addr_table_t *table) {
187 int c2=0x27d4eb2d; // a prime or an odd constant
188 key = (key ^ 61) ^ (key >> 16);
189 key = key + (key << 3);
190 key = key ^ (key >> 4);
192 key = key ^ (key >> 15);
193 printf("The bucket number is %d\n", key % (table->size));
194 return (key % (table->size));
197 //Add key and its address to the new ob_listnode_t
198 vkey addKey(unsigned int key, objheader_t *ptr, obj_addr_table_t *table) {
200 obj_listnode_t *node;
202 table->numelements++;
203 if(table->numelements > (table->loadfactor * table->size)){
204 //TODO : check if table is nearly full and then resize
207 index = hashKey(key,table);
208 if ((node = (obj_listnode_t *) malloc(sizeof(obj_listnode_t))) == NULL) {
209 printf("Malloc error %s %d\n", __FILE__, __LINE__);
214 node->next = table->hash[index];
215 table->hash[index] = node;
218 // Get the address of the object header for a given key
219 objheader_t *findKey(unsigned int key, obj_addr_table_t *table) {
223 index = hashKey(key,table);
224 ptr = table->hash[index];
226 if (ptr->key == key) {
233 // Remove the pointer to the object header from a linked list of obj_listnode_t given an key
234 int removeKey(unsigned int key, obj_addr_table_t *table) {
236 obj_listnode_t *curr, *prev; // prev points to previous node and curr points to the node to be deleted
238 index = hashKey(key,table);
239 prev = curr = table->hash[index];
240 for (; curr != NULL; curr = curr->next) {
241 if (curr->key == key) { // Find a match in the hash table
242 table->numelements--;
243 prev->next = curr->next;
244 if (table->hash[index] == curr) { // Special case when there is one element pointed by the hash table
245 table->hash[index] = NULL;