示例#1
0
文件: Hash.c 项目: DaviDarkFire/PROG2
void put(HashTable* T, Node* node){
	int posicao = hashFunction1(T, node->value);
	add(T->data[posicao], node);
	(T->numberOfElements)++;


}
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;
		}
	}
}
示例#4
0
/*
	hashValue: returns the hash of the key - hash algorithm is specified
	param1: str - the string to hash
	param2: hashNum - the number of the hashing function
	pre: str is not NULL
	post: none
	return: the hash is returned
*/
int _hashValue(char *key, int hashNum) {
    if(hashNum == 1) {
        return hashFunction1(key);
    } else {
        return hashFunction2(key);
    }
}
示例#5
0
文件: Hash.c 项目: DaviDarkFire/PROG2
Node* get (HashTable* T, int n){
	int position = hashFunction1(T, n);
	Node* node = removeElement(T->data[position], n);
	(T->numberOfElements)--;
	return node;




}
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
	}
}
示例#7
0
int main()
{
    /** Variables to measure the time **/
    double start,finish;

    node *Table[MAX];
    clearTable(Table);
    char input[CHARS];

    int j,k,i,x;

    /** For Hash function 1, dispersion and time for inserting the elements**/
    start=clock();
    for(k=1;k<=200;k++)
        for(j=1;j<=10;j++)
        {
            strcpy(input,mkrndstr(j));
            x=hashFunction1(input);
            if (hasValue(Table[x],input))
            {
                /**printf("%s is in the table!\n",input);**/
            }
            else
            {
                    Table[x] = insertValue(Table[x],input);
            }
        }
    finish=clock();
    printf("\nThe time (in seconds) for 1st function=%lf \n",(finish-start)/CLOCKS_PER_SEC);

    printf("\nDispersion for 1st function:\n");
    for (i=0;i<MAX;i++)
    {
        printf("[%d]: %d \n",i,countElements(Table[i]));
    }


    /** For Hash function 2, dispersion and time for inserting the elements**/
    clearTable(Table);
    start=clock();
    for(k=1;k<=200;k++)
        for(j=1;j<=10;j++)
        {
            strcpy(input,mkrndstr(j));
            x=hashFunction2(input);
            if (hasValue(Table[x],input))
            {
                /**printf("%s is in the table!\n",input);**/
            }
            else
            {
                    Table[x] = insertValue(Table[x],input);
            }
        }
    finish=clock();
    printf("\nThe time (in seconds) for 2nd function=%lf \n",(finish-start)/CLOCKS_PER_SEC);
    printf("\nDispersion for 2nd function:\n");
    for (i=0;i<MAX;i++)
    {
        printf("[%d]: %d \n",i,countElements(Table[i]));
    }


    /**
    node *Table[MAX];
    clearTable(Table);
    char input[CHARS];
    int in=-1;
    int i;
    while(in!=0)
    {
        printf("(1)Insert\n(2)Search\n(3)Count all\n(4)List all\n(0)Escape\n\n");
        printf(" > ");
        scanf("%d",&in);
        switch(in)
        {
            case 1:
                printf("\tInserting: \n");
                printf("\t > ");
                scanf("%s",input);
                if (hasValue(Table[hashFunction1(input)],input))
                {
                    printf("%s is in the table!\n",input);
                }
                else
                {
                    Table[hashFunction1(input)] = insertValue(Table[hashFunction1(input)],input);
                }
                break;
            case 2:
                printf("\tSearching value: \n");
                printf("\t > ");
                scanf("%s", &input);
                if (hasValue(Table[hashFunction1(input)],input))
                {
                    printf("%s is in the table!\n",input);
                }
                else
                {
                    printf("%s is not in the table!\n",input);
                }
                break;
            case 4:
                for (i = 0; i < MAX; i++)
                {
                    printf("[%d] ",i);
                    printList(Table[i]);
                }
                break;
            case 3:
                for (i = 0; i < MAX; i++)
                {
                    printf("\n[%d]: %d ",i, countElements(Table[i]));
                }
                break;
        }
        printf("_______________________________________________________\n");
    }
    **/
    return 0;
}