Exemplo n.º 1
0
/*
 insert the following values into a hashLink, you must create this hashLink but
 only after you confirm that this key does not already exist in the table. For example, you
 cannot have two hashLinks for the word "taco".
 
 if a hashLink already exists in the table for the key provided you should
 replace that hashLink--this requires freeing up the old memory pointed by hashLink->value
 and then pointing hashLink->value to value v.
 
 also, you must monitor the load factor and resize when the load factor is greater than
 or equal LOAD_FACTOR_THRESHOLD (defined in hashMap.h).
 */
void insertMap (struct hashMap * ht, KeyType k, ValueType v){  
	int hashIndex;
    struct hashLink * newHashLink = (struct hashLink *) malloc(sizeof(struct hashLink));
    char * newKeyType = (char *) malloc(strlen(k) + 1);     //Allocate memory for new char string

    /* Create hash index using one of two hashing function supplied */
    if(HASHING_FUNCTION == 1)
        hashIndex = stringHash1(k) % ht->tableSize;
    else
        hashIndex = stringHash2(k) % ht->tableSize;

    /* Ensure hashIndex is positive */
    if(hashIndex < 0)
        hashIndex += ht->tableSize;

    assert(newHashLink);
    
    /* Remove duplicate keys so new key replaces old key */
    if(containsKey(ht, k))
        removeKey(ht, k);

    /* Initialize new hashLink and add to appropriate hash index */
    strcpy(newKeyType, k);      //copy string stream into allocated memory reserved for this hashLink

    newHashLink->key = newKeyType;
    newHashLink->value = v;
    newHashLink->next = ht->table[hashIndex];
    ht->table[hashIndex] = newHashLink;

    ht->count++;
    
    /* Test table load and resize if necessary */
    if(tableLoad(ht) >= LOAD_FACTOR_THRESHOLD)
        _setTableSize(ht, ht->tableSize * 2);
}
Exemplo n.º 2
0
/*
 insert the following values into a hashLink, you must create this hashLink but
 only after you confirm that this key does not already exist in the table. For example, you
 cannot have two hashLinks for the word "taco".

 if a hashLink already exists in the table for the key provided you should
 replace that hashLink--this requires freeing up the old memory pointed by hashLink->value
 and then pointing hashLink->value to value v.

 also, you must monitor the load factor and resize when the load factor is greater than
 or equal LOAD_FACTOR_THRESHOLD (defined in hashMap.h).
 */
void insertMap (struct hashMap * ht, KeyType k, ValueType v)
{

	int hash;
	if(HASHING_FUNCTION == 1){
		hash = stringHash1(k);
	}
	if(HASHING_FUNCTION == 2){
		hash = stringHash2(k);
	}

	if(tableLoad(ht) >= LOAD_FACTOR_THRESHOLD){
		_setTableSize(ht, ht->tableSize *2);
	}
	int idx;
	idx = hash % ht->tableSize;

	if (containsKey(ht, k)) {
		ht->table[idx]->value++;
	}
	else {
		struct hashLink *newLink = (struct hashLink *)malloc(sizeof(struct hashLink));
		newLink->value = v;
		newLink->key = k;
		newLink->next = ht->table[idx];
		ht->table[idx] = newLink;
		ht->count++;
	}
}
Exemplo n.º 3
0
/* Method 2: Replace value, but don't delete link */
void insertMap (struct hashMap * ht, KeyType k, ValueType v) {
    ht->count++;
    if(tableLoad(ht) >= (float)LOAD_FACTOR_THRESHOLD)
        _setTableSize(ht, ht->tableSize*2);
    int i = _getHash(ht, k, hashFunc);
    struct hashLink *curr = ht->table[i];
    struct hashLink *prev = NULL;
    while(curr != NULL) {
        if(_compare(curr->key, k) == 0) {
            curr->value = v;                /* These two lines differ from Method 1 */
            return;
        }
        prev = curr;
        curr = _nextLink(curr);
    }
    curr = malloc(sizeof(struct hashLink));
    assert(curr != NULL);
    size_t keyMemBuf = (strlen(k) + 1) * sizeof(char);
    curr->key = malloc(keyMemBuf);
    memcpy(curr->key, k, keyMemBuf);
    curr->value = v;
    curr->next = NULL;
    if(prev != NULL)
        prev->next = curr;
    else
        ht->table[i] = curr;
}
Exemplo n.º 4
0
/*
 insert the following values into a hashLink, you must create this hashLink but
 only after you confirm that this key does not already exist in the table. For example, you
 cannot have two hashLinks for the word "taco".
 if a hashLink already exists in the table for the key provided you should
 replace that hashLink--this requires freeing up the old memory pointed by hashLink->value
 and then pointing hashLink->value to value v.
 also, you must monitor the load factor and resize when the load factor is greater than
 or equal LOAD_FACTOR_THRESHOLD (defined in hashMap.h).
 */
void insertMap(struct hashMap * ht, KeyType k, ValueType v)
{
    
    int hashIndex;
    
    if (HASHING_FUNCTION == 1)
    {
        hashIndex = stringHash1(k) % ht->tableSize; //reduce the index
    }
    else if (HASHING_FUNCTION == 2)
    {
        hashIndex = stringHash2(k) % ht->tableSize;
    }
    
    if (hashIndex < 0) //check to make sure indez is not negative
    {
        hashIndex = hashIndex + ht->tableSize;
    }
    
    //Check if hashLink already exists and delete if it does not
    if (containsKey(ht, k))
    {
        removeKey(ht, k);
    }
    
    //add a new link in
    hashLink *newLink = malloc(sizeof(struct hashLink));
    assert(newLink != NULL);
    
    newLink->key = k;
    newLink->value = v;
    newLink->next = NULL;
    
    if (ht->table[hashIndex] != NULL)
    {
        hashLink *temp = ht->table[hashIndex];
        
        //find the end of chain
        while (temp->next != NULL)
        {
            temp = temp->next;
        }
        
        temp->next = newLink;
    }
    else
    {
        ht->table[hashIndex] = newLink;
    }
    
    ht->count++;
    
    // monitor the load factor
    if (tableLoad(ht) >= LOAD_FACTOR_THRESHOLD)
    {
        _setTableSize(ht, ht->tableSize * 2);
    }
    
}
Exemplo n.º 5
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 = "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;
}
Exemplo n.º 6
0
/*
 insert the following values into a hashLink, you must create this hashLink but
 only after you confirm that this key does not already exist in the table. For example, you
 cannot have two hashLinks for the word "taco".

 if a hashLink already exists in the table for the key provided you should
 replace that hashLink--this requires freeing up the old memory pointed by hashLink->value
 and then pointing hashLink->value to value v.

 also, you must monitor the load factor and resize when the load factor is greater than
 or equal LOAD_FACTOR_THRESHOLD (defined in hashMap.h).
 */
void insertMap (struct hashMap * ht, KeyType k, ValueType v){
	/*write this*/
    assert (ht != 0);
    if (containsKey(ht, k))
    ht->table[_findKey(ht, k)]->value = v;
    else {
        struct hashLink *newLink = malloc(sizeof(struct hashLink));
        newLink->value = v;
        newLink->key = k;
        newLink->next = ht->table[_findKey(ht, k)];
        ht->table[_findKey(ht, k)] = newLink;
        ht->count++;
    }
    if (tableLoad(ht) > LOAD_FACTOR_THRESHOLD)
    _setTableSize(ht, ht->tableSize * 2);
}
Exemplo n.º 7
0
/*
 insert the following values into a hashLink, you must create this hashLink but
 only after you confirm that this key does not already exist in the table. For example, you
 cannot have two hashLinks for the word "taco".

 if a hashLink already exists in the table for the key provided you should
 replace that hashLink--this requires freeing up the old memory pointed by hashLink->value
 and then pointing hashLink->value to value v.

 also, you must monitor the load factor and resize when the load factor is greater than
 or equal LOAD_FACTOR_THRESHOLD (defined in hashMap.h).
 */
void insertMap (struct hashMap * ht, KeyType k, ValueType v)
{
    int index;

    if(tableLoad(ht) >= LOAD_FACTOR_THRESHOLD)
    {
        _setTableSize(ht, (2 * ht -> tableSize));
    }

    struct hashLink *link = malloc(sizeof(struct hashLink));

    index = stringHash2(k) % ht -> tableSize;

    if(index < 0)
    {
        index += ht -> tableSize;
    }

    assert(link);
    link -> next = 0;
    link -> key = k;
    link -> value = v;

    if(containsKey(ht, k))
    {
        removeKey(ht, k);
    }

    if(!ht -> table[index])
    {
        ht -> table[index] = link;
    }
    else
    {
        struct hashLink *current = ht -> table[index];

        while(current -> next)
        {
            current = current -> next;
        }
        current -> next = link;
    }

    ht -> count++;
}
Exemplo n.º 8
0
/*
 insert the following values into a hashLink, you must create this hashLink but
 only after you confirm that this key does not already exist in the table. For example, you
 cannot have two hashLinks for the word "taco".

 if a hashLink already exists in the table for the key provided you should
 replace the value for that key.  As the developer, you DO NOT FREE UP THE MEMORY FOR THE VALUE.
 We have to leave that up to the user of the hashMap to take care of since they may or may not
 have allocated the space with malloc.


 Also, you must monitor the load factor and resize when the load factor is greater than
 or equal LOAD_FACTOR_THRESHOLD (defined in hashMap.h).
 */
void insertMap (struct hashMap * ht, KeyType k, ValueType v)
{
	/*write this*/
  int index;

	if(HASHING_FUNCTION == 1){
		index = stringHash1(k) % ht->tableSize;
	}
	else if(HASHING_FUNCTION == 2){
		index = stringHash2(k) % ht->tableSize;
	}

  if(ht->table[index] != NULL) {
		struct hashLink * current = ht->table[index];
		while(strcmp(current->key, k) != 0 && current->next != NULL) {
			current = current->next;
		}
		if(current->next == NULL && strcmp(current->key, k) != 0) {
			struct hashLink * newLink = malloc(sizeof(struct hashLink));
			newLink->next = NULL;
			newLink->key = k;
			newLink->value = (int *)v;
			current->next = newLink;
			ht->count++;
		}
		else {
			current->value = v;
		}
	}

	else {
		struct hashLink * newLink = malloc(sizeof(struct hashLink));
		newLink->next = NULL;
		newLink->value = (int *)v;
		newLink->key = k;
		ht->table[index] = newLink;
		ht->count++;
	}

	if(tableLoad(ht) >= LOAD_FACTOR_THRESHOLD) {
		_setTableSize(ht, (ht->tableSize)*2);
	}
}
Exemplo n.º 9
0
/*
 insert the following values into a hashLink, you must create this hashLink but
 only after you confirm that this key does not already exist in the table. For example, you
 cannot have two hashLinks for the word "taco".
 
 if a hashLink already exists in the table for the key provided you should
 replace that hashLink--this requires freeing up the old memory pointed by hashLink->value
 and then pointing hashLink->value to value v.
 
 also, you must monitor the load factor and resize when the load factor is greater than
 or equal LOAD_FACTOR_THRESHOLD (defined in hashMap.h).
 */
void insertMap (struct hashMap * ht, KeyType k, ValueType v)
{  
	assert(ht != NULL);

	int loc = stringHash1(k) % ht->tableSize;

	if(containsKey(ht, k)){
		struct hashLink* pLink = ht->table[loc];
	        while(strcmp(pLink->key, k)){
		     pLink = pLink->next;
	        }
		pLink->value = pLink->value + v;
 
	} else {

		struct hashLink* newLink = (struct hashLink*)malloc(sizeof(struct hashLink));
		newLink->key = (char*)malloc((1 + strlen(k)) * sizeof(char));
		strcpy(newLink->key, k);
		newLink->value = v;
		newLink->next = NULL;


	        struct hashLink* pLink = ht->table[loc];
	        if(pLink == NULL){
		   ht->table[loc] = newLink;
	        } else {
		   while(pLink->next != NULL){
			pLink = pLink->next;
		   } 
		   pLink->next = newLink;
	        }
	        ht->count++;
	}

	if(tableLoad(ht) >= LOAD_FACTOR_THRESHOLD){
		_setTableSize(ht, 2 * ht->tableSize);
	}


	return;
}
Exemplo n.º 10
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;
}
Exemplo n.º 11
0
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;
}
Exemplo n.º 12
0
/*
 insert the following values into a hashLink, you must create this hashLink but
 only after you confirm that this key does not already exist in the table. For example, you
 cannot have two hashLinks for the word "taco".
 
 if a hashLink already exists in the table for the key provided you should
 replace that hashLink--this requires freeing up the old memory pointed by hashLink->value
 and then pointing hashLink->value to value v.
 
 also, you must monitor the load factor and resize when the load factor is greater than
 or equal LOAD_FACTOR_THRESHOLD (defined in hashMap.h).
 */
void insertMap (struct hashMap * ht, KeyType k, ValueType v)
{  
	int hashVal;
	hashLink * cur, * tmp, * newlink;
	assert(ht != NULL);
	//if the load factor threshold is exceeded resize the table
	if (tableLoad(ht) >= LOAD_FACTOR_THRESHOLD)   {
		_setTableSize(ht, 2*ht->tableSize);
	}

	newlink = (hashLink *) malloc (sizeof(hashLink));
	assert(newlink != NULL);
	newlink->key = k;
	newlink->value = (int*) v;
	newlink->next = NULL;

	if (HASHING_FUNCTION == 1)
			hashVal = stringHash1(k) % ht->tableSize;
	else
			hashVal = stringHash2(k) % ht->tableSize;

	if (hashVal < 0)
		hashVal += ht->tableSize;

	if (ht->table[hashVal] == NULL)
		ht->table[hashVal] = newlink;

	else {
		cur = ht->table[hashVal];
		while (cur->next != NULL) {
			cur = cur->next;
		}
		cur->next = newlink;
	}
	ht->count++;


	
	////If there is no bucket at the hashvalue currently
	//if (ht->table[hashVal] == NULL) {
	//	//fill hashlink with data
	//	newlink->key = k;
	//	newlink->value = v;
	//	newlink->next = NULL;

	//	//add new link to the table
	//	ht->table[hashVal] = newlink; 
	//	ht->count++;
	//	return;
	//}
	//
	////If a bucket exists at the hashval
	//else {
	//	//If the first link is the one that already has a key 'k', replace it
	//	if (strcmp(cur->key, k) == 0) {
	//		cur->value = v;
	//		return;
	//	}

	//	/*
	//	If there is a link after the first one that already has a key 'k', 
	//	replace it with new value
	//	*/
	//	while (cur->next != NULL) {
	//		if (strcmp(cur->next->key, k) == 0) {
	//			cur->next->value = v;
	//			return;
	//		}
	//		cur = cur->next;
	//	}

	//	/*
	//	If the earlier loop was traversed fully and there is no link with a key 'k'
	//	that means a new link with the value v and key k must be inserted
	//	*/
	//	newlink->key = k;
	//	newlink->value = v;
	//	newlink->next = NULL;
	//	cur->next = newlink;
	//	ht->count++;
	//}
	
}
Exemplo n.º 13
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;
}
Exemplo n.º 14
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;
}
Exemplo n.º 15
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;
}
Exemplo n.º 16
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;
}
Exemplo n.º 17
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;
}
Exemplo n.º 18
0
Arquivo: main.c Projeto: wsims/CS261
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;
}
Exemplo n.º 19
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;
}
Exemplo n.º 20
0
Arquivo: main.c Projeto: gpard77/cs261
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;
}
Exemplo n.º 21
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;
}
Exemplo n.º 22
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;
}
Exemplo n.º 23
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;
}
Exemplo n.º 24
0
/*
 insert the following values into a hashLink, you must create this hashLink but
 only after you confirm that this key does not already exist in the table. For example, you
 cannot have two hashLinks for the word "taco".
 
 if a hashLink already exists in the table for the key provided you should
 replace that hashLink--this requires freeing up the old memory pointed by hashLink->value
 and then pointing hashLink->value to value v.
 
 also, you must monitor the load factor and resize when the load factor is greater than
 or equal LOAD_FACTOR_THRESHOLD (defined in hashMap.h).
 */
void insertMap (struct hashMap * ht, KeyType k, ValueType v)
{  

	float lf; 
	lf = tableLoad(ht); 
	 
	int index; 
	index = stringHash1(k) %  (ht->count + 1); 		

	  
	hashLink *temp = malloc(sizeof(hashLink));
	hashLink *newLink = malloc(sizeof(hashLink));  
	if(containsKey(ht, k) == 1){
				
		temp = ht->table[index]; 
		 	
		while(temp != 0){			
			if(k == temp->key)
			{
				temp->value = temp->value + 1; 
				ht->count++;	
			}
					
			temp=temp->next;
					
		}		
}	
	else{
		newLink->key = k;
		newLink->value = v;
		newLink->next = NULL;  
		temp = ht->table[index]; 

		//ht->table[index] = newLink;  
	//	ht->count++; 

		if(ht->table[index] == 0){
		//	newLink->next = NULL; 
			ht->table[index] = newLink;  
			ht->count++; 
		}
		else{
			
		while(temp != 0){			
			
			if(temp->next == 0){ 
					temp->next = newLink; 			
					ht->count++; 
				} 
			temp = temp->next;
			}
			
				
				
		}

	}
	
	if(lf >= LOAD_FACTOR_THRESHOLD){
		_setTableSize(ht, ht->tableSize*2);  
	} 
	free(temp); 
	/*write this*/	
}
Exemplo n.º 25
0
int main (int argc, const char * argv[]) {
	const char* filename;
	struct hashMap *hashTable;
  int tableSize = 10;
	clock_t timer;
	FILE *fileptr;
  void*  key;
    /*
     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);
  printf("Successfully created hashtable\n");
	/*... concordance code goes here ...*/
    fileptr = fopen(filename, "r+");
    char* wordNext = "starter";
    while(wordNext != 0) {
        wordNext = getWord(fileptr);
        if(wordNext != 0) {
            if(containsKey(hashTable, wordNext, myCompare, hash2) == 0){
                void* v = malloc(sizeof(void*));
                *(int*)v = 1;
                insertMap(hashTable, wordNext, v, myCompare, hash2);
            }
            else {
                /*int newVal = *(int*)(atMap(hashTable, wordNext, myCompare, hash2)) + 1;
                 void* newVal2 = &newVal;
                 insertMap(hashTable, wordNext, newVal2, myCompare, hash2);*/
                *(int*)(atMap(hashTable, wordNext, myCompare, hash2)) = *(int*)atMap(hashTable, wordNext, myCompare, hash2)+1;
            }
        }
    }
	/*... concordance code ends here ...*/

	printMap(hashTable, keyPrint, valPrint);

	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\n");

  assert(containsKey(hashTable, "and", myCompare, hash2));
	removeKey(hashTable, "and", myCompare, hash2);
	removeKey(hashTable, "me", myCompare, hash2);
	removeKey(hashTable, "the", myCompare, hash2);
	
    /* printMap(hashTable); */
    printKeyValues(hashTable, keyPrint, valPrint);

    /* For Tag Cloud */
    generateTagCloudData(hashTable,"tag.csv");

    /* Free up our keys and values using our iterator!!  Also printing them as we go along */
    struct mapItr *myItr = createMapIterator(hashTable);
    while(hasNextMap(myItr)) {
        key = nextMap(myItr);
        int *value = atMap(hashTable,key, myCompare, hash2);
        printf("Freeing ...Key = %s, value = %d \n", key, *value);
        free(value);  /* To match the malloc above*/
        free(key);
    }

	deleteMap(hashTable);
	printf("\nDeleted the table\n");
	return 0;
}