/* Resizes the hash table to be the size newTableSize */ void _setTableSize(struct hashMap * ht, int newTableSize){ int i = 0; struct hashMap * newHM, * oldHM; struct hashLink * currLink; /* Create new hash table of size newTableSize */ newHM = createMap(newTableSize); oldHM = ht; /* Iterate through all hashLinks in old hashMap and copy them into new hashMap */ while(i < ht->tableSize){ currLink = ht->table[i]; while(currLink != 0){ insertMap(newHM, currLink->key, currLink->value); currLink = currLink->next; } i++; } /* Replace hashMap pointer with new hashMap and free old hashMap */ _freeMap(oldHM); ht->table = newHM->table; ht->tableSize = newHM->tableSize; ht->count = newHM->count; }
void loadDictionary(FILE* file, struct hashMap* ht) { /* You will write this*/ //open dictionary file printf( "//TESTING: OPENING DICTIONARY\n"); file = fopen ("dictionary.txt", "r"); //open the file (r: read from file) //assert (file != 0); while(file != 0) //ensure it's open { char * word = getWord(file); printf("//TESTING Word: %s \n", word); while(word) { printf( "//TESTING: INSERTING\n"); insertMap(ht, word, 1); printf( "//TESTING: INSERTED\n"); } } if (file == 0) { printf( "%s doesn't exist or is protected.\n", "dictionary.txt"); } fclose(file); }
int main (int argc, const char * argv[]) { const char* filename; struct hashMap *hashTable; int tableSize = 10; clock_t timer; FILE *fileptr; /* this part is using command line arguments, you can use them if you wish but it is not required. DO NOT remove this code though, we will use it for testing your program. if you wish not to use command line arguments manually type in your filename and path in the else case. */ if(argc == 2) filename = argv[1]; else filename = "input2.txt"; /*specify your input text file here*/ printf("opening file: %s\n", filename); timer = clock(); hashTable = createMap(tableSize); /*... concordance code goes here ...*/ FILE *ifp; char *mode = "r"; char *word, key; ifp = fopen(filename, mode); assert(ifp); while((word = getWord(ifp)) != NULL){ if(word) insertMap(hashTable, word, 1); } fclose(ifp); /*... concordance code ends here ...*/ printMap(hashTable); timer = clock() - timer; printf("\nconcordance ran in %f seconds\n", (float)timer / (float)CLOCKS_PER_SEC); printf("Table emptyBuckets = %d\n", emptyBuckets(hashTable)); printf("Table count = %d\n", size(hashTable)); printf("Table capacity = %d\n", capacity(hashTable)); printf("Table load = %f\n", tableLoad(hashTable)); printf("Deleting keys\n"); removeKey(hashTable, "bitter"); removeKey(hashTable, "me"); removeKey(hashTable, "the"); // printMap(hashTable); deleteMap(hashTable); printf("\nDeleted the table\n"); return 0; }
void loadDictionary(FILE* file, struct hashMap* ht){ file = fopen("Dictionary.txt", "r"); while(!feof(file)){ char *word = getWord(file); if(word) insertMap(ht, word, 1); } fclose(file); }
void processFile(FILE *file) { int i; char *word; struct hashMap *map; struct hashLink *cur; word = 0; map = (struct hashMap *) malloc(sizeof(struct hashMap)); initMap(map, 100); while(1) { word = getWord(file); if(!word) break; /*printf("Words! %s\n", word);*/ if(containsKey(map, word)) insertMap(map, word, *atMap(map, word) + 1); else insertMap(map, word, 1); } i = 0; for(i = 0; i < map->tableSize; i++) { cur = map->table[i]; while(cur) { printf("%s: %d\n", cur->key, cur->value); cur = cur->next; } } freeMap(map); free(map); }
/* _reSizeTable: Resizes the hash map to the size, newCap. Rehashes all of the current keys. param1: hashMap - the map param2: newCap - the new capacity pre: ht is not empty pre: newCap is > 0 post: ht now has tableSize, newCap. post: all keys have been re-hashed and inserted into ht HINT - use insertMap to re-hash the keys. Everything has been completed for you except the copy/re-hash. Free the temp data as you copy/re-hash. */ void _reSizeTable(struct hashMap *ht, int newCap) { struct hashLink *cur, *del; /* Used to free the old hash links and iterate through them */ struct hashLink **temp = ht->table; /* pointer to the old table */ int tempSize = ht->tableSize; /* size of the old table */ _initMap(ht, newCap, ht->hashID); /* Re-initialize the table */ int i; ValueType tempVal; KeyType tempKey; struct hashLink *tempHash; /* FIX ME */ for(i =0; i< tempSize; i++){ if(temp[i]!=NULL){ tempVal = temp[i]->value; tempKey = temp[i]->key; insertMap(ht, tempKey, tempVal); tempHash = temp[i]->next; while(tempHash!= NULL){ tempVal = tempHash ->value; tempKey = tempHash->key; insertMap(ht, tempKey, tempVal); tempHash=tempHash->next; } } else{ ;//do nothing } } free(temp); free(tempHash); free(cur); free(del); return; }
void loadDictionary(FILE* file, struct hashMap* ht) { char *curr; ValueType *val; while((curr = getWord(file)) != NULL) { if((val = atMap(ht, curr)) != NULL) (*val)++; else insertMap(ht, curr, 1); free(curr); } }
/* Resizes the hash table to be the size newTableSize */ void _setTableSize(struct hashMap * ht, int newTableSize) { /*write this*/ int i; struct hashLink * current; hashMap* newTable = createMap(newTableSize); for(i=0; i < ht->tableSize; ++i){ if(ht->table!=NULL){ current = ht->table[i]; while(current != NULL){ insertMap(newTable, current->key, current->value); current= current->next; } } } //free original links _freeLinks(ht->table, newTableSize); ht->tableSize = newTableSize; ht->table = newTable->table; }
/* Loads the contents of a text file into a hash table. * Recieves a file from the user. * The file must have been opened before calling this function and * will close the file once its entire contents have been loaded into the table. */ void loadDictionary(FILE* file, struct hashMap* ht) { if (file != NULL) //make sure a valid file was passed to the function { //while there is still content in the file to read, get the next word and insert it into the hash table while (!feof(file)) { char* word = getWord(file); if (word != NULL) { insertMap(ht, word, 0); } } fclose(file); //close the given file } else { fprintf(stderr, "Could not open file."); //print an error message if the given file could not be opened } }
/* Resizes the hash table to be the size newTableSize */ void _setTableSize(struct hashMap * ht, int newTableSize) { /*write this*/ assert(ht != 0); assert(newTableSize > ht->tableSize); struct hashMap *newht = createMap(newTableSize); int index; struct hashLink *templink; for (index = 0; index < ht->tableSize; index++) { templink = ht->table[index]; while (templink != 0) { insertMap(newht, templink->key, templink->value); templink = templink->next; } } free(ht->table); ht->table = newht->table; ht->tableSize = newTableSize; ht->count = newht->count; }
/* Resizes the hash table to be the size newTableSize */ void _setTableSize(struct hashMap * ht, int newTableSize) { struct hashLink *last, *current; _initMap(ht, newTableSize); for (int i = 0; i < ht -> tableSize; i++) { current = ht -> table[i]; while(current != 0) { insertMap(ht, current -> key, current -> value); last = current; current = current -> next; free(last); } } }
/* Resizes the hash table to be the size newTableSize */ void _setTableSize(struct hashMap * ht, int newTableSize) { /*write this*/ printf("========Resizing..From:%d===To:%d=====\n",ht->tableSize, newTableSize); //printf("COUNT %d\n", ht->count); int oldSize = ht->tableSize; struct hashMap oldHt; int u; _initMap(&oldHt, newTableSize); for(u=0; u < ht->tableSize; ++u){ oldHt.table[u] = ht->table[u]; } struct hashLink *current; int i; _initMap(ht, 2*oldSize); for(i = 0; i < oldSize; i++){ current = oldHt.table[i]; while(current != 0){ insertMap(ht, current->key, current->value); current = current->next; } } }
/* Resizes the hash table to be the size newTableSize */ void _setTableSize(struct hashMap * ht, int newTableSize) //group 5 worksheet 38 { hashLink **oldTable = ht->table; _initMap(ht, newTableSize); for (int i = 0; i < ht->tableSize; i++) { hashLink *currentLink = ht->table[i]; hashLink *nextLink; while (currentLink != NULL) { insertMap(ht, currentLink->key, currentLink->value); nextLink = currentLink->next; currentLink = nextLink; } } free(oldTable); }
/* Resizes the hash table to be the size newTableSize */ void _setTableSize(struct hashMap * ht, int newTableSize) { /*write this*/ int i; hashLink **oldTable = ht->table; ht->table = (hashLink**)malloc(newTableSize * sizeof(hashLink*)); int oldts = ht->tableSize; ht->tableSize = newTableSize; ht->count = 0; for(i=0; i < ht->tableSize; i++) { ht->table[i] = NULL; } for(i=0; i < oldts; i++){ hashLink *cur = oldTable[i]; hashLink *next; while(cur != NULL){ insertMap(ht, cur->key, cur->value); next = cur; cur = cur->next; free (next); } } free(oldTable); }
int main (int argc, const char * argv[]) { const char* filename; struct hashMap *hashTable; int tableSize = 10; clock_t timer; /* this part is using command line arguments, you can use them if you wish but it is not required. DO NOT remove this code though, we will use it for testing your program. if you wish not to use command line arguments manually type in your filename and path in the else case. */ if(argc == 2) filename = argv[1]; else filename = "input1.txt"; /*specify your input text file here*/ printf("opening file: %s\n", filename); timer = clock(); hashTable = createMap(tableSize); /*... concordance code goes here ...*/ FILE *file; file = fopen(filename, "r"); /* if there is no file named that */ if(!file){ printf("Unable to open file. \n"); } /* loops until the end of the file */ while(!feof(file)){ char *word = getWord(file); if(word){ if(containsKey(hashTable, word)){ int *value = atMap(hashTable, word); ++(*value); } else{ insertMap(hashTable, word, 1); } } } /*... concordance code ends here ...*/ printMap(hashTable); timer = clock() - timer; printf("\nconcordance ran in %f seconds\n", (float)timer / (float)CLOCKS_PER_SEC); printf("Table emptyBuckets = %d\n", emptyBuckets(hashTable)); printf("Table count = %d\n", size(hashTable)); printf("Table capacity = %d\n", capacity(hashTable)); printf("Table load = %f\n", tableLoad(hashTable)); printf("Deleting keys\n"); removeKey(hashTable, "and"); removeKey(hashTable, "me"); removeKey(hashTable, "the"); printMap(hashTable); deleteMap(hashTable); printf("\nDeleted the table\n"); return 0; }
int main (int argc, const char * argv[]) { const char* filename; struct hashMap *hashTable; int tableSize = 10; clock_t timer; FILE *fileptr; /* Optional command line argument usage for filename if(argc == 2) // filename = argv[1]; //else*/ filename = "input2.txt"; /*specify your input text file here*/ printf("opening file: %s\n", filename); timer = clock(); /*Used to calculate efficiency*/ hashTable = createMap(tableSize); /*Create a new hashMap*/ fileptr = fopen(filename, "r");/*Open the file*/ assert(fileptr != NULL);/*Check that the file opened properly*/ int * value;/*Used to receive the value at the key*/ for (char* word = getWord(fileptr); word != NULL; word = getWord(fileptr)) /*While the file hasn't reached the end*/ { if (containsKey(hashTable, word) == 1) { /*If the key is already in the hash table, get the current value at that key and increment it by 1. Then reinsert that key with the new value*/ value = atMap(hashTable, word); *value+=1; insertMap(hashTable, word, *value); } else insertMap(hashTable, word, 1); /*else insert the key with a value of 1*/ } fclose(fileptr);/*close the file*/ printMap(hashTable);/*Print the keys and values in the hashMap*/ timer = clock() - timer; /*Print statements for testing purposes*/ printf("\nconcordance ran in %f seconds\n", (float)timer / (float)CLOCKS_PER_SEC); printf("Table emptyBuckets = %d\n", emptyBuckets(hashTable)); printf("Table count = %d\n", size(hashTable)); printf("Table capacity = %d\n", capacity(hashTable)); printf("Table load = %f\n", tableLoad(hashTable)); /*Test the removeKey function*/ printf("Deleting keys\n"); removeKey(hashTable, "and"); removeKey(hashTable, "me"); removeKey(hashTable, "the"); printf("Table emptyBuckets = %d\n", emptyBuckets(hashTable)); printf("Table count = %d\n", size(hashTable)); printf("Table capacity = %d\n", capacity(hashTable)); printf("Table load = %f\n", tableLoad(hashTable)); printMap(hashTable); /*Delete the hashMap*/ deleteMap(hashTable); printf("\nDeleted the table\n"); return 0; }
int main (int argc, const char * argv[]) { const char* filename; struct hashMap *hashTable; int tableSize = 10; clock_t timer; FILE *fileptr; /* this part is using command line arguments, you can use them if you wish but it is not required. DO NOT remove this code though, we will use it for testing your program. if you wish not to use command line arguments manually type in your filename and path in the else case. */ if(argc == 2) filename = argv[1]; else filename = "input1.txt"; /*specify your input text file here*/ printf("opening file: %s\n", filename); timer = clock(); hashTable = createMap(tableSize); /*... concordance code goes here ...*/ fileptr = fopen(filename, "r"); char* word; char words[95][16]; // store words here to print later int count = 0; // loop over words in filename adding them to the map while (( word = getWord(fileptr))) { //words[count] = malloc(strlen(word)); strcpy(words[count],word); count++; if (containsKey(hashTable, word)) { ValueType *val = atMap(hashTable, word); (*val)++; free(word); } else { insertMap(hashTable, word, 1); } } fclose(fileptr); // loop over words printf("word count: %d\n",count); printf("table size: %d\n",hashTable->tableSize); for (int i=0; i < count; i++) { //int v = val; word = words[i]; //printf("%d: ",i); printf("%s: ",word); printf("%d \n",(*atMap(hashTable,word))); } /*... concordance code ends here ...*/ printMap(hashTable); timer = clock() - timer; printf("\nconcordance ran in %f seconds\n", (float)timer / (float)CLOCKS_PER_SEC); printf("Table emptyBuckets = %d\n", emptyBuckets(hashTable)); printf("Table count = %d\n", size(hashTable)); printf("Table capacity = %d\n", capacity(hashTable)); printf("Table load = %f\n", tableLoad(hashTable)); printf("Deleting keys\n"); removeKey(hashTable, "and"); removeKey(hashTable, "me"); removeKey(hashTable, "the"); printMap(hashTable); deleteMap(hashTable); printf("\nDeleted the table\n"); return 0; }
int main() { FILE *dictionaryFile; char line[100]; char *nlptr; struct hashMap *dictionaryMap; clock_t begin, end; int count = 0; int size = 1000; // The number of buckets in our hashmap. You can change // this to time how long it takes to build hashmaps with very small or // very large numbers of buckets. (Try dropping it to 100, or increasing it // to 10,000.) // Create a hashMap to hold our dictionary dictionaryMap = createHashMap(size); // Open the textfile containing our dictionary words, read each word, // and add it to our hashmap. // You'll need to copy the "dictionary.txt" file to the same directory // as your executable for this to work! dictionaryFile = fopen("dictionary.txt", "r"); // (Fun fact not at all related to this lab: we're using the 1934 edition // of Webster's dictionary. Why 1934? Because the copyright lapsed. Most // UNIX systems and Mac OS X use this same dictonary for their built-in // spell checking support) assert(dictionaryFile != NULL); begin = clock(); while(fgets(line, sizeof(line), dictionaryFile)) { // Get rid of the newline character nlptr = strchr(line, '\n'); if (nlptr) { *nlptr = '\0'; } // If the word isn't already in the hash table, add it if (!containsKey(dictionaryMap, line)) { insertMap(dictionaryMap, line, 1); } count++; } end = clock(); printf("It took %0.3f second(s) to build our hashmap.\n", (end - begin) / (double)CLOCKS_PER_SEC); printf("With %d words in %d buckets, the hashmap's load is %0.3f\n", count, size, tableLoad(dictionaryMap)); // Close our file pointer fclose(dictionaryFile); // Now let the user check if a word is spelled correctly. printf("Enter a word (or press 'Return' to exit): "); while(fgets(line, sizeof(line), stdin)) { // Get rid of the newline character nlptr = strchr(line, '\n'); if (nlptr) { *nlptr = '\0'; } // If the line is empty, exit the program. if (strlen(line) == 0) { printf("Goodbye!\n"); break; } // Check to see if the user's word exists in our hashmap. If it does, // tell them that it's spelled correctly. Otherwise, tell them it's // misspelled. // FIXME: you get to implement this // Prompt the user to enter another word printf("Enter a word (or press 'Return' to exit): "); } return 0; }
int main (int argc, const char * argv[]) { const char* filename; struct hashMap *hashTable; int tableSize = 10; clock_t timer; FILE *fileptr; /* this part is using command line arguments, you can use them if you wish but it is not required. DO NOT remove this code though, we will use it for testing your program. if you wish not to use command line arguments manually type in your filename and path in the else case. */ if(argc == 2) filename = argv[1]; else filename = "input1.txt"; /*specify your input text file here*/ printf("opening file: %s\n", filename); timer = clock(); hashTable = createMap(tableSize); /*... concordance code goes here ...*/ fileptr = fopen(filename, "r"); if (fileptr != NULL) { int *val, *x; char *word; while ((word = getWord(fileptr))) { if (containsKey(hashTable, word)) { val = (int *) atMap(hashTable, word); (*val) ++; free(word); } else { x = malloc(sizeof(int)); *x = 1; insertMap(hashTable, word, x); } } fclose(fileptr); } else printf("Error opening file.\n"); /*... concordance code ends here ...*/ printMap(hashTable, keyPrint, valPrint); timer = clock() - timer; printf("\nconcordance ran in %f seconds\n", (float)timer / (float)CLOCKS_PER_SEC); printf("Table emptyBuckets = %d\n", emptyBuckets(hashTable)); printf("Table count = %d\n", size(hashTable)); printf("Table capacity = %d\n", capacity(hashTable)); printf("Table load = %f\n", tableLoad(hashTable)); printf("Deleting keys\n"); removeKey(hashTable, "and"); removeKey(hashTable, "me"); removeKey(hashTable, "the"); printMap(hashTable, keyPrint, valPrint); printKeyValues(hashTable, keyPrint, valPrint); /* Test out the iterator */ #ifdef ITERATOR_IN struct mapItr *myItr; myItr = createMapIterator(hashTable); KeyType key; /* Free up our keys and values using our iterator!! Also printing them as we go along */ while(hasNextMap(myItr)) { key = nextMap(myItr); int *value = atMap(hashTable,key); printf("Freeing ...Key = %s, value = %d \n", key, *value); free(value); /* To match the malloc above*/ free(key); } #endif deleteMap(hashTable); printf("\nDeleted the table\n"); return 0; }
int main (int argc, const char * argv[]) { const char* filename; struct hashMap hashTable; int tableSize = 1000; clock_t timer; /* this part is using command line arguments, you can use them if you wish but it is not required. DO NOT remove this code though, we will use it for testing your program. if you wish not to use command line arguments manually type in your filename and path in the else case. */ if(argc == 2) filename = argv[1]; else filename = "input.txt"; /*specify your input text file here*/ printf("opening file: %s\n", filename); timer = clock(); initMap(&hashTable, tableSize); /* ... concordance code goes here ... */ FILE *inputFile; inputFile = fopen(filename, "r"); //open the file if(!inputFile) printf("Failed to open input file.\n"); while(!feof(inputFile)){ char *word = getWord(inputFile); if(word){ if(containsKey(&hashTable, word)){ int* value = atMap(&hashTable, word); ++(*value); } else { insertMap(&hashTable, word, 1); } } } /* end concordance code... */ timer = clock() - timer; printf("concordance ran in %f seconds\n", (float)timer / (float)CLOCKS_PER_SEC); /*print the hashMap */ struct hashLink* cur; for (int i = 0; i < hashTable.tableSize; i++){ cur = hashTable.table[i]; while (cur){ printf("%s:%d\n", cur->key, cur->value); cur = cur->next; } } freeMap(&hashTable); fclose(inputFile); return 0; }
int main (int argc, const char * argv[]) { char *word; ValueType *value; const char* filename; struct hashMap *hashTable; int tableSize = 10; clock_t timer; FILE *fileptr; /* this part is using command line arguments, you can use them if you wish but it is not required. DO NOT remove this code though, we will use it for testing your program. if you wish not to use command line arguments manually type in your filename and path in the else case. */ if(argc == 2) filename = argv[1]; else filename = "input1.txt"; /*specify your input text file here*/ printf("opening file: %s\n", filename); timer = clock(); hashTable = createMap(tableSize); /*... concordance code goes here ...*/ fileptr = fopen(filename, "r"); while(1){ word = getWord(fileptr); if(word == NULL){ break; } value = atMap(hashTable, word); if(value == NULL) { value = malloc(sizeof(int*)); *((int*)value) = 1; } else if(value != NULL) { *((int *)value) += 1; } insertMap(hashTable, word, ((void *)value)); } /*... concordance code ends here ...*/ printMap(hashTable); timer = clock() - timer; printf("\nconcordance ran in %f seconds\n", (float)timer / (float)CLOCKS_PER_SEC); printf("Table emptyBuckets = %d\n", emptyBuckets(hashTable)); printf("Table count = %d\n", size(hashTable)); printf("Table capacity = %d\n", capacity(hashTable)); printf("Table load = %f\n", tableLoad(hashTable)); printf("Deleting keys\n"); removeKey(hashTable, "and"); removeKey(hashTable, "me"); removeKey(hashTable, "the"); printMap(hashTable); deleteMap(hashTable); printf("\nDeleted the table\n"); return 0; }
int main (int argc, const char * argv[]) { const char* filename; struct hashMap *hashTable; int tableSize = 10; clock_t timer; FILE *fileptr; /* this part is using command line arguments, you can use them if you wish but it is not required. DO NOT remove this code though, we will use it for testing your program. if you wish not to use command line arguments manually type in your filename and path in the else case. */ if(argc == 2) filename = argv[1]; else filename = "input1.txt"; /*specify your input text file here*/ printf("opening file: %s\n", filename); timer = clock(); hashTable = createMap(tableSize); /*... concordance code goes here ...*/ /*open file*/ fileptr = fopen(filename, "r"); /*read words from file*/ char* word; while((word = getWord(fileptr))){ //make word lowercase /*if the occurance is already in the ht, val++*/ if(containsKey(hashTable, word)){ ValueType* value = atMap(hashTable, word); *value += 1; insertMap(hashTable, word, *value); } /*else, add it*/ else{ insertMap(hashTable, word, 1); } } free(word); /*close file*/ fclose(fileptr); /*... concordance code ends here ...*/ printMap(hashTable); timer = clock() - timer; printf("\nconcordance ran in %f seconds\n", (float)timer / (float)CLOCKS_PER_SEC); printf("Table emptyBuckets = %d\n", emptyBuckets(hashTable)); printf("Table count = %d\n", size(hashTable)); printf("Table capacity = %d\n", capacity(hashTable)); printf("Table load = %f\n", tableLoad(hashTable)); printf("Deleting keys\n"); removeKey(hashTable, "and"); removeKey(hashTable, "me"); removeKey(hashTable, "the"); printMap(hashTable); deleteMap(hashTable); printf("\nDeleted the table\n"); return 0; }
int main (int argc, const char * argv[]) { const char* filename; struct hashMap *hashTable; int tableSize = 97; clock_t timer; FILE *fileptr; /* this part is using command line arguments, you can use them if you wish but it is not required. DO NOT remove this code though, we will use it for testing your program. if you wish not to use command line arguments manually type in your filename and path in the else case. */ if(argc == 2) filename = argv[1]; else filename = "input2.txt"; /*specify your input text file here*/ printf("opening file: %s\n", filename); timer = clock(); hashTable = createMap(tableSize); /*... concordance code goes here ...*/ fileptr = fopen(filename, "r"); char* word = getWord(fileptr); do { //if the word is in the hashTable, increment the val //otherwise, create the entry with value set to 1 if (containsKey(hashTable, word)) { //atMap is useless? int *val = atMap(hashTable, word); *val += 1; int i = 0; } else { int val = 1; insertMap(hashTable, word, val); } word = getWord(fileptr); } while (word != NULL); fclose(fileptr); /*... concordance code ends here ...*/ printMap(hashTable); timer = clock() - timer; printf("\nconcordance ran in %f seconds\n", (float)timer / (float)CLOCKS_PER_SEC); printf("Table emptyBuckets = %d\n", emptyBuckets(hashTable)); printf("Table count = %d\n", size(hashTable)); printf("Table capacity = %d\n", capacity(hashTable)); printf("Table load = %f\n", tableLoad(hashTable)); printf("Deleting keys\n"); removeKey(hashTable, "and"); removeKey(hashTable, "me"); removeKey(hashTable, "the"); printMap(hashTable); deleteMap(hashTable); printf("\nDeleted the table\n"); return 0; }
int main (int argc, const char * argv[]) { const char* filename; struct hashMap *hashTable; char *word; int tableSize = 10; clock_t timer; FILE *fileptr; int *numWordOccurances; /* this part is using command line arguments, you can use them if you wish but it is not required. DO NOT remove this code though, we will use it for testing your program. if you wish not to use command line arguments manually type in your filename and path in the else case. */ if(argc == 2) filename = argv[1]; else filename = "input1.txt"; /*specify your input text file here*/ printf("opening file: %s\n", filename); timer = clock(); hashTable = createMap(tableSize); // generate concordance fileptr = fopen(filename,"r"); while (!feof(fileptr)) { word = getWord(fileptr); if(!word){ break; } numWordOccurances = (int *)atMap(hashTable, word); /* cast return of atMap to int*/ if(numWordOccurances!=0){ (*numWordOccurances)++; } else { /* need to malloc numWordOccurances before insertMap*/ numWordOccurances = (int *) malloc(sizeof(int)); *numWordOccurances = 1; insertMap(hashTable, word, numWordOccurances); } } // print concordance printMap(hashTable); // print hashmap statistices fclose(fileptr); timer = clock() - timer; printf("\nconcordance ran in %f seconds\n", (float)timer / (float)CLOCKS_PER_SEC); printf("Table emptyBuckets = %d\n", emptyBuckets(hashTable)); printf("Table count = %d\n", size(hashTable)); printf("Table capacity = %d\n", capacity(hashTable)); printf("Table load = %f\n", tableLoad(hashTable)); printf("Deleting keys: \"and\", \"me\", and \"the\"\n"); removeKey(hashTable, "and"); removeKey(hashTable, "me"); removeKey(hashTable, "the"); printMap(hashTable); deleteMap(hashTable); printf("\nDeleted the table\n"); return 0; }
/* concordance: create a hash map which contains word frequencies param1: ht - the hash map param2: word - the current word to hash pre: ht is not NULL post: key and value is now updated if the key already exists, add 1 to the value HINT - this is a 'normal' insert, the difference is that the value is not always 1 use valAtKey() and insertMap() */ void concordance(struct hashMap *ht, char *word) { /*insertMap(ht, word, valAtKey(ht, word));*/ insertMap(ht, word, 1); }
int main (int argc, const char * argv[]) { const char* filename; struct hashMap *hashTable; int tableSize = 10; clock_t timer; FILE *fileptr; /* this part is using command line arguments, you can use them if you wish but it is not required. DO NOT remove this code though, we will use it for testing your program. if you wish not to use command line arguments manually type in your filename and path in the else case. */ if(argc == 2) filename = argv[1]; else filename = "input1.txt"; /*specify your input text file here*/ printf("opening file: %s\n", filename); timer = clock(); hashTable = createMap(tableSize); /*... concordance code goes here ...*/ fileptr = fopen(filename, "r"); if (!fileptr) { printf("Error Opening File.\n"); } char *word = getWord(fileptr); //ValueType val; while (word != 0) { //char *word = getWord(fileptr); // Check table for word, insert if not found if (containsKey(hashTable, word)== 0) { insertMap(hashTable, word, 1); } else { (*atMap(hashTable, word))++; } word = 0; free(word); word = getWord(fileptr); } fclose(fileptr); printf("\n"); hashLink * temp; for (int i = 0; i < hashTable->tableSize; i++) { temp = hashTable->table[i]; while (temp != 0) { printf("%s: %d\n", temp->key, temp->value); temp = temp->next; } } free(temp); /*... concordance code ends here ...*/ printMap(hashTable); timer = clock() - timer; printf("\nconcordance ran in %f seconds\n", (float)timer / (float)CLOCKS_PER_SEC); printf("Table emptyBuckets = %d\n", emptyBuckets(hashTable)); printf("Table count = %d\n", size(hashTable)); printf("Table capacity = %d\n", capacity(hashTable)); printf("Table load = %f\n", tableLoad(hashTable)); printf("Deleting keys\n"); removeKey(hashTable, "and"); removeKey(hashTable, "me"); removeKey(hashTable, "the"); printMap(hashTable); deleteMap(hashTable); printf("\nDeleted the table\n"); return 0; }
int main (int argc, const char * argv[]) { const char* filename; struct hashMap *hashTable; int tableSize = 10; clock_t timer; FILE *fileptr; /* this part is using command line arguments, you can use them if you wish but it is not required. DO NOT remove this code though, we will use it for testing your program. if you wish not to use command line arguments manually type in your filename and path in the else case. */ if(argc >= 2) filename = argv[1]; else filename = "input1.txt"; /*specify your input text file here*/ printf("opening file: %s\n", filename); fileptr = fopen(filename, "r"); if(fileptr == NULL) { char err[255]; sprintf(err, "Failure opening file %s; exiting.\n", filename); perror(err); exit(EXIT_FAILURE); } timer = clock(); hashTable = createMap(tableSize); char *curr; ValueType *val; while((curr = getWord(fileptr)) != NULL) { if((val = atMap(hashTable, curr)) != NULL) (*val)++; //insertMap(hashTable, curr, (*val)+1); else insertMap(hashTable, curr, 1); free(curr); } fclose(fileptr); //fclose(outfileptr); printMap(hashTable); timer = clock() - timer; printf("\nconcordance ran in %f seconds\n", (float)timer / (float)CLOCKS_PER_SEC); printf("Table emptyBuckets = %d\n", emptyBuckets(hashTable)); printf("Table count = %d\n", size(hashTable)); printf("Table capacity = %d\n", capacity(hashTable)); printf("Table load = %f\n", tableLoad(hashTable)); printf("Deleting keys\n"); removeKey(hashTable, "and"); removeKey(hashTable, "me"); removeKey(hashTable, "the"); //printMap(hashTable); deleteMap(hashTable); printf("\nDeleted the table\n"); return 0; }
int main (int argc, const char * argv[]) { char *fn; /* File name */ struct hashMap *hashTable, *hashTable2; FILE *filePtr; fn = "text1.txt";/* the file name and path */ printf("Opening file: %s \n", fn); filePtr = fopen(fn, "r"); hashTable = createMap(40, 1); char *word; while((word = getWord(filePtr)) != '\0') { insertMap(hashTable, word, 1); } printf("--------------- Testing contains --------------- \n"); assertTrue(containsKey(hashTable, "it") == 1, "Search for 'it'"); assertTrue(containsKey(hashTable, "comparison") == 1, "Search for 'comparison'"); assertTrue(containsKey(hashTable, "period") == 1, "Search for 'period'"); assertTrue(containsKey(hashTable, "despair") == 1, "Search for 'despair'"); assertTrue(containsKey(hashTable, "deriop") == 0, "Search for 'deriop'"); assertTrue(containsKey(hashTable, "yuck") == 0, "Search for 'yuck'"); printf("--------------- Testing table stats --------------- \n"); assertTrue(hashTable->tableSize == 40, "Test table size"); assertTrue(fullBuckets(hashTable) == 30, "Test full buckets"); assertTrue(emptyBuckets(hashTable) == 10, "Test empty buckets"); assertTrue(linkCount(hashTable) == 59, "Test link count"); printf("--------------- Testing remove --------------- \n"); removeKey(hashTable, "yuck"); /* Should print some type of 'not found' message */ removeKey(hashTable, "despair"); assertTrue(containsKey(hashTable, "despair") == 0, "Search for 'despair'"); printf("--------------- Printing hash table --------------- \n"); printMap(hashTable); deleteMap(hashTable); printf("--------------- New table - same text file - new hash --------------- \n"); fn = "text1.txt";/* the file name and path */ printf("Opening file: %s \n", fn); filePtr = fopen(fn, "r"); hashTable2 = createMap(40, 2); while((word = getWord(filePtr)) != '\0') { insertMap(hashTable2, word, 1); } printf("--------------- Testing table stats 2 --------------- \n"); assertTrue(hashTable2->tableSize == 80, "Test table size"); assertTrue(fullBuckets(hashTable2) == 38, "Test full buckets"); assertTrue(emptyBuckets(hashTable2) == 42, "Test empty buckets"); assertTrue(linkCount(hashTable2) == 59, "Test link count"); printf("Closing file: %s \n", fn); fclose(filePtr); /* Concordance testing */ struct hashMap * concord; fn = "text2.txt"; printf("Opening file: %s \n", fn); filePtr = fopen(fn, "r"); concord = createMap(10, 2); while((word = getWord(filePtr)) != '\0') { concordance(concord, word); } printf("--------------- Concordance table stats --------------- \n"); printf("table size: %d \n", concord->tableSize); printf("full buckets: %d \n", fullBuckets(concord)); printf("empty buckets: %d \n", emptyBuckets(concord)); printf("link count: %d \n", linkCount(concord)); /*Test further on your own */ return 0; }
int main(int argc, const char * argv[]) { const char* filename; struct hashMap *hashTable; int tableSize = 63; clock_t timer; FILE *fileptr; /* this part is using command line arguments, you can use them if you wish but it is not required. DO NOT remove this code though, we will use it for testing your program. if you wish not to use command line arguments manually type in your filename and path in the else case. */ if (argc == 2) filename = argv[1]; else filename = "input1.txt"; /*specify your input text file here*/ printf("opening file: %s\n", filename); timer = clock(); hashTable = createMap(tableSize); /*... concordance code goes here ...*/ fileptr = fopen(filename, "r"); //open the given file if (fileptr != NULL) //if the file was opened sucessfully... { /* while the end of the file has not yet been reached, get the next word in the file, * adding it to the hash table or updating the number of times it has appeared in the document. * the alogrithm is case sensitive */ while (!feof(fileptr)) { char* word = getWord(fileptr); if (word != NULL) { int* wordCount = atMap(hashTable, word); if (wordCount == NULL) { insertMap(hashTable, word, 1); } else { insertMap(hashTable, word, (*wordCount) + 1); } } } } else { fprintf(stderr, "Could not open file."); //print an error message if the file could not be opened } fclose(fileptr); //close the file //print each word in the document along with the number of times it has appeared. for (int i = 0; i < hashTable->tableSize; i++) { hashLink* curLink = hashTable->table[i]; while (curLink != NULL) { printf("%s: %d\n", curLink->key, curLink->value); curLink = curLink->next; } } /*... concordance code ends here ...*/ printMap(hashTable); timer = clock() - timer; printf("\nconcordance ran in %f seconds\n", (float)timer / (float)CLOCKS_PER_SEC); printf("Table emptyBuckets = %d\n", emptyBuckets(hashTable)); printf("Table count = %d\n", size(hashTable)); printf("Table capacity = %d\n", capacity(hashTable)); printf("Table load = %f\n", tableLoad(hashTable)); printf("Deleting keys\n"); removeKey(hashTable, "and"); removeKey(hashTable, "me"); removeKey(hashTable, "the"); printMap(hashTable); deleteMap(hashTable); printf("\nDeleted the table\n"); return 0; }
int main (int argc, const char * argv[]) { const char* filename; struct hashMap *hashTable; int tableSize = 1000; clock_t timer; FILE *fileptr; /* this part is using command line arguments, you can use them if you wish but it is not required. DO NOT remove this code though, we will use it for testing your program. if you wish not to use command line arguments manually type in your filename and path in the else case. */ if(argc == 2) filename = argv[1]; else filename = "input1.txt"; /*specify your input text file here*/ printf("opening file: %s\n", filename); timer = clock(); /*... concordance code goes here ...*/ hashTable = createMap(tableSize); fileptr = fopen(filename, "r"); // char *character = ""; // ValueType* val; // val = atMap(hashTable, character); /* * I couldnt figure out how to properly set up the concordance*/ insertMap(hashTable, getWord(fileptr), 0); insertMap(hashTable, getWord(fileptr), 0); insertMap(hashTable, getWord(fileptr), 0); insertMap(hashTable, getWord(fileptr), 0); fclose(fileptr); /*... concordance code ends here ...*/ printMap(hashTable); timer = clock() - timer; printf("Table emptyBuckets = %d\n", emptyBuckets(hashTable)); printf("Table count = %d\n", size(hashTable)); printf("Table load = %f\n", tableLoad(hashTable)); printf("Deleting keys\n"); removeKey(hashTable, "and"); removeKey(hashTable, "me"); removeKey(hashTable, "the"); printMap(hashTable); deleteMap(hashTable); printf("\nDeleted the table\n"); return 0; }