예제 #1
0
/*
 a simple yes/no if the key is in the hashtable.
 0 is no, all other values are yes.
 */
int containsKey (struct hashMap * ht, KeyType k)
{
	/*write this*/

	int hash;
	if(HASHING_FUNCTION == 1){
		hash = stringHash1(k);
	}
	if(HASHING_FUNCTION == 2){
		hash = stringHash2(k);
	}
	struct hashLink* cur;
	int idx = hash % ht->tableSize;

	cur = ht->table[idx];
	while(cur != NULL)
	{
		if(strcmp(cur->key, k)== 0){
			return 1;
		}
		else{
			cur = cur->next;
		}
	}

	return 0;
}
예제 #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 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);
}
예제 #3
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++;
	}
}
예제 #4
0
파일: hashMap.c 프로젝트: sreil/CS-261
/*
 find the hashlink for the supplied key and remove it, also freeing the memory
 for that hashlink. it is not an error to be unable to find the hashlink, if it
 cannot be found do nothing (or print a message) but do not use an assert which
 will end your program.
 */
void removeKey (struct hashMap * ht, KeyType k)
{
	int index;
    index = stringHash2(k) % ht -> tableSize;

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

	struct hashLink *current = ht -> table[index];

	struct hashLink *previous = ht -> table[index];

	while (current != 0)
	{
		if (current -> key == k)
		{
			previous -> next = current -> next;

			free(current);

			return;
		}

		previous = current;
		current = current -> next;
	}
}
예제 #5
0
/*
 this returns the value (which is void*) stored in a hashLink specified by the key k.
 
 if the user supplies the key "taco" you should find taco in the hashTable, then
 return the value member of the hashLink that represents taco.
 
 if the supplied key is not in the hashtable return NULL.
 */
ValueType atMap (struct hashMap * ht, KeyType k)
{ 
	hashLink * cur;
	int hashVal;
	assert(ht != NULL);
	if (!containsKey(ht, k))
		return NULL;

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

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

		cur = ht->table[hashVal];

		while (strcmp(cur->key, k) != 0)
			cur = cur->next;

		return cur->value;
	}
}
예제 #6
0
파일: hashMap.c 프로젝트: jonese8/cs261
/* Desc: Helper function for finding the index of a key
 * Pre:  ht is not null
 * Post: index of k returned
 */
int findKeyIndex( struct hashMap * ht, KeyType k )
{
    assert( ht != NULL );
    int key_index;

    /* Run the specified has function */
    if( HASHING_FUNCTION == 1 )
    {
        key_index = stringHash1( k );
    }
    else if( HASHING_FUNCTION == 2 )
    {
        key_index = stringHash2( k );
    }

    /* Reduce index to valid table index */
    key_index %= ht->tableSize;

    /* Ensure the index is positive */
    if( key_index < 0 )
    {
        key_index += ht->tableSize;
    }
    return( key_index );
}
예제 #7
0
/*
 this returns the value (which is void*) stored in a hashLink specified by the key k.
 if the user supplies the key "taco" you should find taco in the hashTable, then
 return the value member of the hashLink that represents taco.
 if the supplied key is not in the hashtable return NULL.
 */
ValueType* atMap(struct hashMap * ht, KeyType k)
{
    int hashIndex;
    
    if (HASHING_FUNCTION == 1)
    {
        hashIndex = stringHash1(k) % ht->tableSize; //reduce index
    }
    else if (HASHING_FUNCTION == 2)
    {
        hashIndex = stringHash2(k) % ht->tableSize;
    }
    
    if (hashIndex < 0) //check that index is not negative
    {
        hashIndex = hashIndex + ht->tableSize;
    }
    
    hashLink* currentLink = ht->table[hashIndex];
    
    while (currentLink != NULL)
    {
        if (strcmp(currentLink->key, k) == 0)
        {
            return (&(currentLink->value));
        }
        else
        {
            currentLink = currentLink->next;
        }
    }
    
    return NULL;
    
}
예제 #8
0
/*
 a simple yes/no if the key is in the hashtable.
 0 is no, all other values are yes.
 */
int containsKey(struct hashMap * ht, KeyType k)
{
    int hashIndex;
    
    if (HASHING_FUNCTION == 1)
    {
        hashIndex = stringHash1(k) % ht->tableSize; //reduce index
    }
    else if (HASHING_FUNCTION == 2)
    {
        hashIndex = stringHash2(k) % ht->tableSize;
    }
    
    if (hashIndex < 0) //check that index is not negative
    {
        hashIndex = hashIndex + ht->tableSize;
    }
    
    hashLink* currentLink = ht->table[hashIndex];
    
    while (currentLink != NULL)
    {
        if (strcmp(currentLink->key, k) == 0)
        {
            return 1; //yes, the key is in the hashtable
        }
        else
        {
            currentLink = currentLink->next;
        }
    }
    
    return 0;
    
}
예제 #9
0
/*
 a simple yes/no if the key is in the hashtable. 
 0 is no, all other values are yes.
 */
int containsKey (struct hashMap * ht, KeyType k)
{  
	int hashVal;
	hashLink * cur;
	assert(ht != NULL);

	//Get the index of the bucket using the hash function
	if (HASHING_FUNCTION == 1)
		hashVal = stringHash1(k) % ht->tableSize;
	else
		hashVal = stringHash2(k) % ht->tableSize;

	//ensure the index is positive
	if (hashVal < 0)
		hashVal += ht->tableSize;

	//Set cur equal to the proper bucket (the one at the index from the hash function
	cur = ht->table[hashVal];

	//traverse the bucket to find the key
	while (cur != NULL) {
		//If the key is found return 1
		if (strcmp(cur->key, k) == 0)
			return 1;

		cur = cur->next;
	}
	/*
	if the entire bucket is traversed and the key is not found return 0
	*/
	return 0;
}
예제 #10
0
/*
 a simple yes/no if the key is in the hashtable.
 0 is no, all other values are yes.
 */
int containsKey (struct hashMap * ht, KeyType k)
{
	/*write this*/
  int index;
	struct hashLink * current;
	if(HASHING_FUNCTION == 1){
		index = stringHash1(k) % ht->tableSize;
	}
	else if(HASHING_FUNCTION == 2){
		index = stringHash2(k) % ht->tableSize;
	}
	if(ht->table[index] == NULL) {
    return 0;}
	else{
		current = ht->table[index];
		while(current != NULL) {
			if (strcmp(current->key, k) == 0 ) {
				return 1;
			}
			current = current->next;
		}

	return 0;
  }
}
예제 #11
0
/*
 this returns the value (which is void*) stored in a hashLink specified by the key k.

 if the user supplies the key "taco" you should find taco in the hashTable, then
 return the value member of the hashLink that represents taco.

 if the supplied key is not in the hashtable return NULL.
 */
ValueType atMap (struct hashMap * ht, KeyType k)
{
	/*write this*/
  int index;
	if(HASHING_FUNCTION == 1){
		index = stringHash1(k) % ht->tableSize;
	}
	else if(HASHING_FUNCTION == 2){
		index = stringHash2(k) % ht->tableSize;
	}

	//access the map at that index, and return the value
	if(ht->table[index] != NULL) {
		struct hashLink * current = ht->table[index];
		while(1 > 0) {
			if (current->next == NULL) {
				break;
			}
			else if (strcmp(current->key, k) == 0 ){
				return current->value;
			}
			else{
				current = current->next;
			}
		}

		return current->value;
	}
	return 0;
}
예제 #12
0
/*
 this returns the value (which is void*) stored in a hashLink specified by the key k.

 if the user supplies the key "taco" you should find taco in the hashTable, then
 return the value member of the hashLink that represents taco.

 if the supplied key is not in the hashtable return NULL.
 */
ValueType atMap (struct hashMap *ht, KeyType k)
{
	/*write this*/
	int hash;
	int idx;
	struct hashLink *link;
	//printf("TableSize:%d\n",ht->tableSize);

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

	idx = hash % ht->tableSize;
	//printf("hash:%d\nIndex:%d\n", hash, Index);
	if(containsKey(ht,k)!=0){
		link = ht->table[idx];
		while (link != NULL){
			if (strcmp(link->key, k) == 0){
				//printf("Found It\n");
				//printf("hash:%d\nIndex:%d\n", hash, Index);
				return link->value;
			}
			else{
				link = link->next;
				//printf("Checking Next\n");
			}
		}
	}
	//printf("Exiting AtMap now\n");
	//assert(0);
	return NULL;
}
예제 #13
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);
    }
    
}
예제 #14
0
파일: hashMap.c 프로젝트: vb3/homework
ValueType* atMap (struct hashMap * ht, KeyType k)
{
    int hashIndex = (HASHING_FUNCTION == 2 ? stringHash2(k) : stringHash1(k)) % ht->tableSize;
    hashLink * cur = ht->table[hashIndex];
    while(cur != 0){
        if(strcmp(cur->key, k) == 0) return &cur->value;
        else cur = cur->next;
    }
    return 0;
}
예제 #15
0
/*
 this is useful for you in answering the questions. if your write your code such
 that you call stringHash1 when HASHING_FUNCTION==1 and stringHash2 when
 HASHING_FUNCTION==2 then you only need to change this value in order to switch
 hashing functions that your code uses. you are not required to use this value
 though.
 */
int _findKey (struct hashMap *hash, KeyType str){
    assert (hash != NULL);
    int index;
    if (HASHING_FUNCTION == 1)
        index = stringHash1(str) % hash->tableSize; //http://stackoverflow.com/questions/1504420/c-what-does-the-percentage-sign-mean
    else if (HASHING_FUNCTION == 2)
        index = stringHash2(str) % hash->tableSize;
    if (index < 0)
        index += hash->tableSize;
    return (index);
}
예제 #16
0
파일: hashMap.c 프로젝트: FTchips/FT_test
ValueType* atMap (struct hashMap * ht, KeyType k)
{
    struct hashLink* temp;
    int index = stringHash2(k) % ht->tableSize;
    temp = ht->table[index];
    while((temp->key != k) && (temp != 0))
    {
        temp = temp->next;
    }
    return temp;
}
예제 #17
0
void insertMapNoResize (struct hashMap * ht, KeyType k, ValueType v)
{  
	int hashVal;
	hashLink * cur, * tmp, * newlink;
	assert(ht != NULL);

	newlink = (hashLink *) malloc (sizeof(hashLink));

	if (HASHING_FUNCTION == 1)
			hashVal = stringHash1(k) % ht->tableSize;
	else
			hashVal = stringHash2(k) % 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++;
}
예제 #18
0
/*
 find the hashlink for the supplied key and remove it, also freeing the memory
 for that hashlink. it is not an error to be unable to find the hashlink, if it
 cannot be found do nothing (or print a message) but do not use an assert which
 will end your program.
 */
void removeKey(struct hashMap * ht, KeyType k)
{
    int hashIndex;
    
    if (HASHING_FUNCTION == 1)
    {
        hashIndex = stringHash1(k) % ht->tableSize; //reduce index
    }
    else if (HASHING_FUNCTION == 2)
    {
        hashIndex = stringHash2(k) % ht->tableSize;
    }
    
    if (hashIndex < 0) //check that index is not negative
    {
        hashIndex = hashIndex + ht->tableSize;
    }
    
    hashLink *currentLink = ht->table[hashIndex];
    hashLink *previousLink = NULL;
    
    if (currentLink == NULL)//no key
    {
        return;
    }
    
    while (strcmp(currentLink->key, k) != 0) //key exists, so loop to find it
    {
        previousLink = currentLink;
        currentLink = currentLink->next;
        
        if (currentLink == NULL)//no key
        {
            return;
        }
    }
    
    if (previousLink != NULL)
    {
        previousLink->next = currentLink->next;
    }
    else
    {
        ht->table[hashIndex] = 0;
    }
    
    free(currentLink);
    
}
예제 #19
0
파일: hashMap.c 프로젝트: vb3/homework
void insertMap (struct hashMap * ht, KeyType k, ValueType v)
{
    int hashIndex = (HASHING_FUNCTION == 2 ? stringHash2(k) : stringHash1(k)) % ht->tableSize;
    if (hashIndex < 0) hashIndex += ht->tableSize;
    
    hashLink * newLink = (hashLink*) malloc(sizeof(hashLink));
    assert(newLink);
    newLink->key = k;
    newLink->value = v;
    newLink->next = ht->table[hashIndex];
    ht->table[hashIndex] = newLink;
    
    ht->count++;
    
}
예제 #20
0
파일: hashMap.c 프로젝트: vb3/homework
void removeKey (struct hashMap * ht, KeyType k)
{
    int hashIndex = (HASHING_FUNCTION == 2 ? stringHash2(k) : stringHash1(k)) % ht->tableSize;
    hashLink * cur = ht->table[hashIndex];
    hashLink * prev = ht->table[hashIndex];
    while(cur != 0){
        if(cur->key == k){
            prev->next = cur->next;
            free(cur);
            return;
        }
        prev = cur;
        cur = cur->next;
    }
    
}
예제 #21
0
파일: hashMap.c 프로젝트: FTchips/FT_test
void removeKey (struct hashMap * ht, KeyType k)
{
    struct hashLink* curr, prev;
    if(containsKey(k))
    {
        curr = ht->table[stringHash2(k) % ht->tableSize];
        while(curr->key != k)
        {
            prev = curr;
            curr = curr->next;
        }
        prev->next = curr->next;
        free(curr);
        ht->count --;
    }
}
예제 #22
0
파일: hashMap.c 프로젝트: sreil/CS-261
/*
 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++;
}
예제 #23
0
/*
 find the hashlink for the supplied key and remove it, also freeing the memory
 for that hashlink. it is not an error to be unable to find the hashlink, if it
 cannot be found do nothing (or print a message) but do not use an assert which
 will end your program.
 */
void removeKey (struct hashMap * ht, KeyType k)
{ 
	hashLink * cur, * temp;
	assert(ht != NULL);
	if (containsKey(ht, k)) {
		int hashVal;
		if (HASHING_FUNCTION == 1)
			hashVal = stringHash1(k) % ht->tableSize;
		else if (HASHING_FUNCTION == 2)
			hashVal = stringHash2(k) % ht->tableSize;

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

		cur = ht->table[hashVal];
		
		//if node to remove is at a parent link
		if (strcmp(cur->key, k) == 0) {
			temp = cur->next;
			free(cur->key);
			free(cur);
			ht->table[hashVal] = temp;
			ht->count--;
			return;
		}

		//If node to remove is at a child link
		while (cur->next != NULL) {
			if (strcmp(cur->next->key, k) == 0) {
				temp = cur->next->next;
				free(cur->next->key);
				free(cur->next);
				cur->next = temp;
				ht->count--;
				return;
			}
			cur = cur->next;
		}
	}
	
	else {
		printf("\nError: tried to remove a key that is not in the map.");
		return;
	}
}
예제 #24
0
/*
 find the hashlink for the supplied key and remove it, also freeing the memory
 for that hashlink. it is not an error to be unable to find the hashlink, if it
 cannot be found do nothing (or print a message) but do not use an assert which
 will end your program.
 */
void removeKey (struct hashMap * ht, KeyType k)
{
	/*write this*/

  int idx;
  	struct hashLink *itr;
  	if(HASHING_FUNCTION == 1){
  		idx = stringHash1(k) % ht->tableSize;
  	}
  	else if(HASHING_FUNCTION == 2){
  		idx = stringHash2(k) % ht->tableSize;
  	}

  	if(ht->table[idx] != NULL) {
  		itr = ht->table[idx];
  		//seperate case
  		if(strcmp(itr->key, k) == 0){
  			struct hashLink * del = itr;
  			ht->table[idx] = del->next;
  			free(del);
  			ht->count--;
  			return;
  		}
  		while(1){
  			//seperate checks to see if the first one is true, then the second one will segfault

  			if (itr->next == NULL){
  				break;
  			}
  			else if (strcmp(itr->next->key, k) == 0) {
  				break;
  			}
  			else{
  				itr = itr->next;
  			}
  		}
  		if(itr->next == NULL) { return;}
  		else{
  			struct hashLink * del = itr->next;
  			itr->next = del->next;
  			free(del);
  			ht->count--;
  		}
  	}
}
예제 #25
0
파일: hashMap.c 프로젝트: sreil/CS-261
/*
 this returns the value (which is void*) stored in a hashLink specified by the key k.

 if the user supplies the key "taco" you should find taco in the hashTable, then
 return the value member of the hashLink that represents taco.

 if the supplied key is not in the hashtable return NULL.
 */
ValueType* atMap (struct hashMap * ht, KeyType k)
{
    int index;
    index = stringHash2(k) % ht -> tableSize;

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

    struct hashLink *current = ht -> table[index];

    while(strcmp(current -> key, k) != 0)
    {
        current = current -> next;
    }

    return &current -> value;
}
예제 #26
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);
	}
}
예제 #27
0
/*
 find the hashlink for the supplied key and remove it, also freeing the memory
 for that hashlink. it is not an error to be unable to find the hashlink, if it
 cannot be found do nothing (or print a message) but do not use an assert which
 will end your program.
 */
void removeKey (struct hashMap * ht, KeyType k)
{
	/*write this*/



	int hash;
	if(HASHING_FUNCTION == 1){
		hash = stringHash1(k);
	}
	if(HASHING_FUNCTION == 2){
		hash = stringHash2(k);
	}
	struct hashLink *cur, *prev;
	int idx = hash % ht->tableSize;

	prev = 0;
	cur = ht->table[idx];
	if(containsKey(ht, k)!=0){
		while(cur)
		{
			if(strcmp(cur->key, k)==0)
			{
				printf("###########Removing:%s ##############\n", cur->key);
				if(!prev){
					ht->table[idx] = cur->next;
				}
				else{
					prev->next = cur->next;
				}
				free(cur);
				ht->count--;
				return;
			}

			prev = cur;
			cur = cur->next;
		}
	}
}
예제 #28
0
/*
 find the hashlink for the supplied key and remove it, also freeing the memory
 for that hashlink. it is not an error to be unable to find the hashlink, if it
 cannot be found do nothing (or print a message) but do not use an assert which
 will end your program.
 */
void removeKey (struct hashMap * ht, KeyType k){  
	int hashIndex;
    struct hashLink * lastLink, * currLink;

    /* Determine hash index using the supplied hash functions */
    if(HASHING_FUNCTION == 1)
        hashIndex = stringHash1(k) % ht->tableSize;
    else
        hashIndex = stringHash2(k) % ht->tableSize;
    
    /* Set pointer to head of linked list at hash index of table */
    currLink = ht->table[hashIndex];
    lastLink = currLink;
    /* Check each member in linked list for the requested KeyType and return value */
    while(currLink != 0){
        if(strcmp(k, currLink->key) == 0){
            if(lastLink == currLink){
                lastLink = currLink->next;
                ht->table[hashIndex] = lastLink;
            }
            else
                lastLink->next = currLink->next;
                
            
            /* "Free"-ing the memory occupied by key and value before freeing hashLink */
            //free(currLink->key);    //Free char*
            currLink->value = 0;

            /* Free hashLink and decrement hashLink counter */
            free(currLink);

            /* Decrement hashLink counter */
            ht->count--;
            
            /* Break from while loop */
            break;
        }
        currLink = currLink->next;
    }
}
예제 #29
0
/*
 NOTE: The return value for this function has changed. 
 This returns the value (which is NOT void*) stored in a hashLink specified by the key k.
 
 if the user supplies the key "taco" you should find taco in the hashTable, then
 return the value member of the hashLink that represents taco.
 
 if the supplied key is not in the hashtable return NULL.
 */
ValueType atMap (struct hashMap * ht, KeyType k){ 
	int hashIndex;
    struct hashLink * currLink;

    /* Determine hash index using the supplied hash functions */
    if(HASHING_FUNCTION == 1)
        hashIndex = stringHash1(k) % ht->tableSize;
    else
        hashIndex = stringHash2(k) % ht->tableSize;
    
    /* Set pointer to head of linked list at hash index of table */
    currLink = ht->table[hashIndex];
    /* Check each member in linked list for the requested KeyType and return value */
    while(currLink != 0){
        if(strcmp(k, currLink->key) == 0)
            return currLink->value;
        currLink = currLink->next;
    }

    /* If search of linked list did not return a match, return NULL */
	return 0;
}
예제 #30
0
파일: hashMap.c 프로젝트: FTchips/FT_test
void insertMap (struct hashMap * ht, KeyType k, ValueType v)
{
    struct hashLink* temp, temp2;
    temp = atMap(ht, k);
    int index = stringHash2(k) % ht->tableSize;

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

        }
        ht->count++;
    }
}