Exemplo n.º 1
0
int main(int argc, char* argv[]) {
	HashTable hashTable;
	hashTable.Linkinit();
	hashTable.HashTableInit("Com_names1.txt");
	hashTable.HashTableInit("Com_names2.txt");

	string command = "";

	cout << "r - 탐색\td - 삭제\tt-일괄삭제\tp-성능측정" << endl;
	cout << "명령은? ";
	getline(cin, command);

	while (command != "e") {
		if (command[0] == 'r') {
			char recName[100];
			CharSplit(command, recName);
			int index;
			int prob = 0;
			hashTable.Retrieve(recName, &index, &prob);
			if(index != -1)
				cout << "저장위치= " << index << ",  Probe 수= " << prob << endl;
			else
				cout << "없는 데이터 입니다." << endl;
			//해시테이블 탐색
		} else if (command[0] == 'd') {
			//해시테이블 데이터 삭제
			char recName[100];
			CharSplit(command, recName);
			int index;
			int relocation;
			hashTable.Delete(recName, &index, &relocation);
			if(index != -1)
				cout << "저장위치= " << index << ", Relocation 수= " << relocation
					<< endl;
			else
				cout << "없는 데이터 입니다." << endl;
		} else if (command[0] == 't') {
			//해시테이블 일괄 삭제
			int count = 0;
			count = hashTable.TotalDelete("Com_names1.txt");
			count += hashTable.TotalDelete("Com_names2.txt");
			cout << "Total Relocation 수= " << count << endl;
		} else if (command[0] == 'p') {
			//해시테이블 성능 측정
			double avg = hashTable.PerformanceMeasurement();
			cout << "Average probe number = " << avg << endl;
		}
		cout << "명령은? ";
		getline(cin, command);
	}
	cout << "실험을 종료합니다." << endl;
	cout << hashTable.GetLength() << endl;
	return 0;
}
Exemplo n.º 2
0
int main(int argc, const char * argv[])
{
    HashTable * ht = iHashTable.Create(64, sizeof(int));

    const char test_key0[] = "test_key0";
    const char test_key1[] = "test_key1";
    const char test_key2[] = "test_key2";
    const char invalid_key[] = "invalid_key";

    int a = 1;
    int b = 2;
    int c = 3;

    // Test Insert & Retrieve

    printf ("Test Insert & Retrieve...\n");
    
    ht->Insert(ht, (void*)test_key0, strlen(test_key0), &a);
    ht->Insert(ht, (void*)test_key1, strlen(test_key1), &b);
    ht->Insert(ht, (void*)test_key2, strlen(test_key2), &c);

    assert(ht->cnt == 3);

    int data;

    ht->Retrieve(ht, (void*)test_key0, strlen(test_key0), &data);
    assert(data == a);
    
    ht->Retrieve(ht, (void*)test_key1, strlen(test_key1), &data);
    assert(data == b);

    ht->Retrieve(ht, (void*)test_key2, strlen(test_key2), &data);
    assert(data == c);

    int ret = ht->Retrieve(ht, (void*)invalid_key, strlen(invalid_key), &data);

    assert(ret == E_NOT_FOUND);
    assert(ht->cnt == 3);

    // Test replacement

    printf ("Test Replace...\n");

    ht->Insert(ht, (void*)test_key0, strlen(test_key0), &b);
    assert(ht->cnt == 3);

    ht->Retrieve(ht, (void*)test_key0, strlen(test_key0), &data);
    assert(data == b);

    // Test Continas, Find & Erase

    printf ("Test Contains, Find, Erase...\n");

    void *key_ret = NULL;
    size_t key_len;

    assert(ht->Contains(ht, &c) == RET_OK);
    
    ht->Find(ht, &c, &key_ret, &key_len);
    assert(key_ret != NULL && key_len != 0);
    ht->Erase(ht, key_ret, key_len);

    free (key_ret);

    assert(ht->cnt == 2);

    assert(ht->Contains(ht, &c) == E_NOT_FOUND);
    ret = ht->Retrieve(ht, (void*)test_key2, strlen(test_key2), &data);
    assert(ret == E_NOT_FOUND);

    // Test destroy
    printf ("Test Destroy...\n");
 
    ht->Destroy(ht);

    printf("All tests passed\n");
    
}