Example #1
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;
    
}
Example #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);
}
Example #3
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;
  }
}
Example #4
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;
}
Example #5
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;
}
Example #6
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;
    
}
Example #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 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++;
	}
}
Example #8
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;
}
/*
 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)
{  
   assert(ht != NULL);
   int loc = stringHash1(k) % ht->tableSize;
   if(containsKey(ht, k)){
      
      struct hashLink* pLink = ht->table[loc];
      struct hashLink* pLink2 = pLink;
      while(strcmp(pLink->key,k)){
	 pLink2 = pLink;
	 pLink = pLink->next;
      }

      if(pLink == pLink2){
	  ht->table[loc] = pLink->next;
      } else {
	  pLink2->next = pLink->next;
      }	 
      free(pLink->key);
      free(pLink);

   
   } else {
      //Do nothing	
   }
}
Example #10
0
/* 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 );
}
Example #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)
{ 
	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;
	}
}
Example #12
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;
}
Example #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);
    }
    
}
Example #14
0
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;
}
Example #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);
}
Example #16
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++;
}
Example #17
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 index; 
	if(ht->count == 0)
	{	index = stringHash1(k);}
	else	
	{	index = stringHash1(k) % ht->count; }		


	  
//	if(index < 0) index += ht->count; 
//	printf("Index is: %ld\n", index); 
//	hashLink *temp = malloc(sizeof(hashLink));
//	temp = ht->table[index];  
	
//		free(ht->table[index]); 
		ht->table[index] = ht->table[index]->next;  		
	//	ht->table[index] = NULL; 
		ht->count--;
	

	/*write this*/	
}
Example #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);
    
}
Example #19
0
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++;
    
}
Example #20
0
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;
    }
    
}
/*
 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 loc = stringHash1(k) % ht->tableSize;

	struct hashLink* pLink = ht->table[loc];
        while(pLink != NULL && strcmp(pLink->key,k)){
	    pLink = pLink->next;
	}

	if(pLink == NULL){
	    return 0;
	} else {
	    return 1;
	}
	return 0;
}
Example #22
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--;
  		}
  	}
}
Example #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;
	}
}
/*
 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)
{ 
	assert(ht != NULL);

	int loc = stringHash1(k);

	if(containsKey(ht, k)){
		struct hashLink* pLink = ht->table[loc];
		while(strcmp(pLink->key, k)){
			pLink = pLink->next;
		}
		ValueType* temp;
		temp = &(pLink->value);
		return temp;
	} else {
		return NULL;
	}
}
Example #25
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);
	}
}
Example #26
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 index; 
	index = stringHash1(k) %  (ht->count + 1); 		

	hashLink *temp = malloc(sizeof(hashLink));
	temp = ht->table[index]; 
		while(temp != 0){			
			if(k == temp->key)
			{
				return 1; 
			}
			else{
			temp=temp->next;
			}
		}
	
	
	return 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;
}
Example #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;
    }
}
Example #29
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;
		}
	}
}
Example #30
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;
}