bool hash::search(int key){
	for(int i = 0; i < table.size(); i++){
		int index;
		switch(i){
		case 0:
			index = hashFunction0(key);
			break;
		case 1:	
			index = hashFunction1(key);
			break;
		case 2:
			index = hashFunction2(key);
			break;
		case 3:
			index = hashFunction3(key);
			break;
		case 4:
			index = hashFunction4(key);
			break;
		}
		if(table[i][index] == key){
			return true;
		}
	}	return false;
}
void hash::remove(int key){
	for(int i = 0; i < table.size(); i++){
		int index;
		switch(i){
		case 0:
			index = hashFunction0(key);
			break;
		case 1:	
			index = hashFunction1(key);
			break;
		case 2:
			index = hashFunction2(key);
			break;
		case 3:
			index = hashFunction3(key);
			break;
		case 4:
			index = hashFunction4(key);
			break;
		}
		if(table[i][index] == key){
			numInserted--;
			table[i][index] = -1;
		}
	}
}
示例#3
0
int insertElement(char * element)
{
    //! insert an element
    //! returns the number of collisions which occurred before the element was inserted
    if (getFillFactor()>MAX_FILL_FACTOR){
        resizeHashTable();
    }
    int hash = hashFunction3(element, 0);
    int nrCol = 0;
    while (nrCol<size && hashTable[hash]!=NULL){
        nrCol++;
        hash = hashFunction3(element, nrCol);
    }
    hashTable[hash] = (char*)malloc(MAX_STRING_LENGTH+1);
    strcpy(hashTable[hash], element);
    return nrCol;
}
void hash::insert(int key){
	int numMoves = 0;
	bool placed = false;

	if(search(key)){
		return; //Does not allow duplicate input
	}

	while(!placed && numMoves < 50/*<-- Have the user set maxLoop in a function. Default should be 50*/){
		int i = numMoves%table.size(); // specifies table column
		int index; //specifies a table cell
		switch(i){
		case 0:
			index = hashFunction0(key);
			break; 	
		case 1:	
			index = hashFunction1(key);
			break;
		case 2:
			index = hashFunction2(key);
			break;
		case 3:
			index = hashFunction3(key);
			break;
		case 4:
			index = hashFunction4(key);
			break;
		}
		if(table[i][index] == -1){ //spot 
			numInserted++;
			table[i][index] = key;
			placed = true;
		}
		else{ //spot to put it in has data -> evict a cuckoo
			int tempKey = key;
			key = table[i][index];
			table[i][index] = tempKey;
			numMoves++;
		}
	}
	if(!placed){
		//numMoves == 50 and we need to rehash table
		//need to resize -> could premeditatingly do this
		rehash(); //Resize and rehash the table
		insert(key); //Try inserting key again
	}
}