Esempio n. 1
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;
}
Esempio 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 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);
}
Esempio n. 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++;
	}
}
Esempio 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);
    }
    
}
Esempio n. 5
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);
}
Esempio 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)
{
    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++;
}
Esempio 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 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);
	}
}
Esempio 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 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*/
	int index = HASH(k) % ht->tableSize;
	if (containsKey(ht, k))
	{
		// DO NOT removeKey(ht, k) and add newLink; Instead, only replace value and return
		*atMap(ht, k) = v;
		return;
	}
	struct hashLink * newLink = (struct hashLink *) malloc(sizeof(struct hashLink));
	assert(newLink != 0);
	newLink->key = k;
	newLink->value = v;
	newLink->next = ht->table[index];
	ht->table[index] = newLink;
	ht->count++;
	if ((float)ht->count / (float)ht->tableSize > LOAD_FACTOR_THRESHOLD)
		_setTableSize(ht, 2 * ht->tableSize);
}
Esempio 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;
}
Esempio n. 10
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, void* k, void* v, comparator keyCompare, hashFuncPtr hashFunc) {
    
	/*write this*/
    int idx = (*hashFunc)(k) % ht->tableSize;
    float lf;
    hashLink *temp = ht->table[idx];
    printf("Inserting '%s' into bucket %d\n", (char*) k, idx);
    if (temp == NULL){
        temp = malloc(sizeof(hashLink*));
        assert(temp!=0);
        temp->value = v;
        temp->key = k;
        temp->next = NULL;
    }else{
        while (temp){
            if ((*keyCompare)(temp->key, k) == 0) {
                temp->value = v;
                return;
            }

            temp = temp->next;
        }
        temp = ht->table[idx];
        while (temp->next){
            temp = temp->next;
        }
        hashLink *newlink = malloc(sizeof(hashLink*));
        assert(newlink!=0);
        newlink->value = v;
        newlink->key = k;
        temp->next = newlink;
    }
    ht->count++;
   

    // check the load factor and see if resize is needed
    lf = ((float)ht->count)/ht->tableSize;
    if (lf >= LOAD_FACTOR_THRESHOLD)
        _setTableSize(ht, ht->tableSize*2, keyCompare, hashFunc);
}
Esempio n. 11
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++;
	//}
	
}
Esempio 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)
{  

	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*/	
}