#include "hash.h" #include #include #include #include "mem.h" #define GARBAGE 0xA3 //#define GARBAGE dmalloc(sizeof(struct anode *)); //#define GARBAGE -1549556829; int hash_fcn(int k){ return(k % 97); } struct hash make_table(int s){ struct hash HT; HT.size = s; HT.table = dmalloc(s * sizeof(struct anode **)); if(HT.table == NULL){ printf("make_table: out of memory sucka\n"); abort(); } //Thanks to dmalloc for filling pointers with garbage instead //of NULL. for(int i = 0; i < s; i++){ HT.table[i] = NULL; } return(HT); } char *search(struct hash T, int k){ int index = hash_fcn(k); if (index > T.size){ printf("search: index: %d out of scope\n", index); } else{ struct anode *temp; temp = T.table[index]; while(temp != 9 && temp != 8){ if (temp->key == k){ return(temp->value); } else{ temp = temp->next; } } } return(NULL); } void printword2 (char *word){ printf(">>"); if (word != NULL){ int len = strlen(word); for(int i = 0; i < len; i++) { printf("%c", word[i]); } } printf("<<\n"); } void add(struct hash T, int k, char *v){ int index = hash_fcn(k); if (index < T.size){ struct anode *temp; if (T.table[index] != NULL){ temp = T.table[index]; struct anode *temp2; //printf("here\n"); while((temp->next != 9 || temp->next == 8) && temp->key != k){ printf("%x\n",temp->next->next); temp2 = temp; temp = temp->next; } //printf("here\n"); if (temp->next == 9 || temp->next == 8){ //printword2(temp2->value); dfree(temp2->next); temp = dmalloc(sizeof(struct anode)); temp2->next = temp; temp->value = dmalloc(sizeof(v) + (sizeof(int) * 2)); temp->next = dmalloc(sizeof(struct anode *)); //temp->next = NULL; } else{ dfree(temp2->value); temp2->value = dmalloc(sizeof(v) + (sizeof(int) * 2)); temp = temp2; } temp->key = k; strcpy(temp->value,v); } else{ temp = dmalloc(sizeof(struct anode)); temp->value = dmalloc(sizeof(v) + (sizeof(int) * 2)); // + 2 int for NULL character for string temp->next = dmalloc(sizeof(struct anode *)); T.table[index] = temp; temp->key = k; //printf("%d\n",temp->next); //temp->next = NULL; strcpy(temp->value,v); //printf("%d\n",temp->next->next); //printf("%d\n",GARBAGE); //char *Gargar = GARBAGE; //printf("%d\n",temp->next == GARBAGE); //dfree(temp->next); } } else{ printf("add: key is out of scope of table\n"); } } void free_table(struct hash T){ struct anode *temp, *temp2; for (int i = 0; i < T.size; i++){ if(T.table[i] == NULL) continue; temp = T.table[i]; while(temp->next != 8 && temp->next != 9){ printf("%x\n",temp->next); temp2 = temp; temp = temp->next; dfree(temp2->value); dfree(temp2); } dfree(temp); // temp = GARBAGE; // printf("%d\n",temp); // printf("%x\n",GARBAGE * 0xFF * (sizeof(struct anode *) -1)); // printf("%d\n",GARBAGE); // dfree(temp->value); // printf("%d\n",temp->next); //if (temp->next == NULL) // temp->next = 1; // dfree(temp->next); // dfree(temp); //dfree(T.table[i]->next); //dfree(T.table[i]); } dfree(T.table); }