Exemplo n.º 1
0
void InputData(HashTable& table, string& info)
{
    string inputFileName;
    cout << "Input data file (no .txt): ";
    cin >> inputFileName;
    inputFileName += ".txt";

    ifstream infile(inputFileName.c_str());

    if (!infile.is_open())
    {
    	cerr << inputFileName << " cannot be opened" << endl;
    	cerr << "Exit!" << endl;
    	exit(EXIT_FAILURE);
    }

    getline(infile, info);
    cout << "INFO: " << info << endl;

    string temp;
    getline(infile, temp);
    while (infile.good())
    {
        table.Insert(temp);
        getline(infile, temp);
    }
    infile.close();

    cout << "Input data successfully" << endl << endl;
}
Exemplo n.º 2
0
	//增大顺序表的容量
	void ExpandCapacity()
	{
		HashTable<K, V> tmp;
		if (_size == _table.size())
		{
			size_t i = 0;
			while (i<_PrimeSize)
			{
				if (_table.size() >= _PrimeList[i])
					i++;
				else
				{
					tmp._table.resize(_PrimeList[i]);
					break;
				}
			}
			for (size_t i = 0; i < _table.size(); ++i)
			{
				Node *cur = _table[i];
				while (cur)
				{
					tmp.Insert(cur->_key, cur->_value);
					cur = cur->_next;
				}
			}
			//swap(_table, tmp._table);
			//swap(_size, tmp._size);
			Swap(tmp);
		}
	}
Exemplo n.º 3
0
int main()
{

    HashTable<Foo> ints;

    ints.Insert(new Foo());

    return 0;
}
Exemplo n.º 4
0
	//拷贝构造
	HashTable(const Table &ht)
	{
		HashTable tmp;
		tmp._table.resize(ht._table.size());
		for (size_t i = 0; i < tmp._table.size(); ++i)
		{
			Node *cur = ht._table[i];
			while (cur)
			{
				tmp.Insert(cur->_key, cur->_value);
				cur = cur->_next;
			}
		}
		//swap(_table, tmp._table);
		//swap(_size, tmp._size);
		Swap(tmp);
	}
Exemplo n.º 5
0
		void _CheckSize()
		{
			if (_tables.size() == _size)
			{
				size_t NewSize = GetNewSize();


				//现代写法
				HashTable<K, V> ht;
				ht._tables.resize(NewSize);
				for (size_t i = 0; i < _tables.size(); i++)
				{
					ht.Insert(_tables[i]->_Key, _tables[i]->_Value);
				}
				_Swap(ht);
			}
		}
int main() {
	int d = 11;
	HashTable<int, int> *ht = new HashTable<int, int>(d);
	int a[] = {83, 14, 29, 70, 10, 55, 72, 45, 11, 67, 90, 24, 3, 72, 46};
	int length = sizeof(a)/sizeof(a[0]);
	
	for(int i = 0; i < length; i++) {
		cout << "Inserting " << a[i] << "... ";
		cout << (ht->Insert(a[i]) ? " Done.\n" : "");
			
	}

	for(int i = 0; i < length; i++) {
		int s = a[i];
		cout << "Searching for " << s << "..." << endl;
		cout << (ht->Search(s, s) ? "Found " : "Could not find " ) << s << "." << endl;
	}

	return 0;
}
Exemplo n.º 7
0
int main()
{
  HashTable* table = new HashTable;

  table->Insert("Test", 1);
  table->Insert("Test", 1);
  table->Insert("Test22", 133);
  table->Insert("Test3", 14);
  table->Insert("Test4", 144);
  table->Insert("Tes33", 134);
  table->Insert("Tes22", 1111);
  table->Insert("Tes22", 19);
  table->Insert("Tes31", 19);

  cout << "HAHA " << table->Value("Test3") << endl;
  cout << "MEH? " << table->Value("eee") << endl;
  cout << "HMM  " << table->Value("Tes22") << endl;
  cout << "HMM  " << table->Value("Tes31") << endl;
  return 0;
}
Exemplo n.º 8
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");
    
}
Exemplo n.º 9
0
void main(){

	setlocale (LC_ALL,"Russian");
	int z=0;		//для switch
	PList l1;		//полином(один для всех записей)
	l1.Insert(Monom(1,111));
	
	cout<<"Выберите таблицу:"<<endl<<"1)Сортированная"<<endl<<"2)Хеш-таблица"<<endl<<"3)Поисковое дерево"<<endl;
	cin>>z;
	switch(z){
	case 3:{
		//Тестирование таблиц на деревьях
		int kol=0;
		
		TreeTable tr;
		cout<<"Введите количество записей, которое вы хотите добавить:";
			cin>>kol;
			for(int i=0;i<kol;i++){
				NodeTree trNode;
				trNode.poly=&l1;
				string a;
				cout<<"Введите ключ:";
				cin>>a;
				trNode.key=a;
				tr.Insert(trNode);
			}
			cout<<endl;
		tr.Print(tr.root);
		break;
		   }

	case 1:
		{
			//Тестирование таблиц
			//NodeTable tab("abc",&l1);
			SortedTable tabs;
			int kol=0;
			string a;
			cout<<"Введите количество записей, которое вы хотите добавить:";
			cin>>kol;
			for(int i=0;i<kol;i++){
				cout<<"Введите ключ:";
				cin>>a;
				tabs.Insert(a,&l1);}
			cout<<endl<<"Сортированная таблица:"<<endl<<tabs<<endl;	
			break;
		}
	case 2:
		{
			HashTable t;
			string a;
			int kol=0;
			cout<<"Введите количество записей, которое вы хотите добавить:";
			cin>>kol;
			for(int i=0;i<kol;i++){
				cout<<"Введите ключ:";
				cin>>a;
				t.Insert(a,&l1);}
			cout<<endl<<"Хеш-таблица:"<<endl;
			//t.Delete("bc");
			//cout<<t.GetNode("a")->head->next->data.deg<<endl;
			t.print();
			break;
		}
	}

	getch();
}