Пример #1
0
int main(){
 Hash<int>* h = new Hash<int>();

 h->insert("one", 1);

 std::cout << "inserted (\"one\", 1)\nresult: " << h->get("one") << std::endl;

 h->insert("one", 2);

 std::cout << "replaced 1 with 2\n" << h->get("one") << std::endl;

 h->insert("noe", 3);
 
 std::cout << "inserted (\"noe\", 3) (should create the same hash as \"one\", but with value of 3)\nresult: " << h->get("noe") << std::endl;

 h->insert("ZZZZZZzZzZzZzzzZUHcuhu", 239);
 h->insert("ZZZZZZzZzZZzzzzZUHcuhu", 13);

 std::cout << "inserted (\"ZZZZZZzZzZzZzzzZUHcuhu\", 239)\n" << h->get("ZZZZZZzZzZzZzzzZUHcuhu") << std::endl;
 
 h->remove("noe");

 std::cout << "removed (\"noe\"\n" << std::endl;
 h->print();

 return 0;
}
Пример #2
0
int main()
{
  Hash<int,string> h;

  if(h.find(111)!=0)
    cout<<*(h.find(111))<<endl;
  else
    cout<<111<<" Not exist"<<endl;

  h.insert(111,"111");
  h.insert(222,"222");
  h.insert(333,"333");

  if(h.find(111)!=0)
    cout<<*(h.find(111))<<endl;
  else
    cout<<111<<"Not exist"<<endl;
  
  if(h.find(222)!=0)
    cout<<*(h.find(222))<<endl;
  else
    cout<<222<<"Not exist"<<endl;
  
  h.remove(111);
  cout<<"Remove 111 ..."<<endl;
  
  if(h.find(111)!=0)
    cout<<*(h.find(111))<<endl;
  else
    cout<<111<<" Not exist"<<endl;

}
Пример #3
0
void testRemove() {
	Hash<int, int> h;

	for (vector<pair<int, int> >::const_iterator i = ints.begin(); i != ints.begin() + ints.size() / 2; ++i) {
		h.put(i->first, i->second);
	}

	for (vector<pair<int, int> >::const_iterator i = ints.begin(); i != ints.begin() + ints.size() / 2; ++i) {
		h.remove(i->first);
	}

	if (!h.isEmpty()) {
		cout << "testRemove failed\n";
	}

	for (vector<pair<int, int> >::const_iterator i = ints.begin(); i != ints.end(); ++i) {
		h.put(i->first, i->second);
	}

	for (map<int, int>::const_iterator i = uniqueInts.begin(); i != uniqueInts.end(); ++i) {
		if (h.get(i->first)->value != i->second) {
			cout << "Fail on remove test with number " << i->first << ". Expected: " << i->second << " Got: " << h.get(i->first)->value << endl;
		}
	}
}
Пример #4
0
int main()
{
	cout << "Welcome to this awesome Phone Book\n" << endl;

	Hash hash;
	int choice;
	string name;
	string phone;
	Node* entry;
	do
	{
		menu();
		cin >> choice;
		switch (choice)
		{
		case 1:
			getchar();
			cout << "Name: ";
			getline(cin, name);
			cout << "Phone: ";
			getline(cin, phone);
			entry = new Node();
			entry->name = name;
			entry->phone = phone;
			hash.add(entry);
			break;
		case 2:
			getchar();
			cout << "Enter full name: ";
			getline(cin, name);
			hash.search(name);
			break;
		case 3:
			getchar();
			cout << "Name: ";
			getline(cin, name);
			hash.remove(name);
			break;
		case 4:
			hash.printAll();
			break;
		case 0:
			hash.clearAll();
			break;
		default:
			cout << "Invalid entry. Please try again" << endl;
			break;
		}
	} while (choice != 0);
	
	cout << "Have a good day!" << endl;

	return 0;
}
Пример #5
0
int main(){

  Hash<int>* tab = new Hash<int>(100);
  tab->insert("chickin" , 5);
  tab->insert("car" , 1);
  tab->insert("asdf", 3);
  cout << tab->find("chickin") <<  endl;
  cout << tab->find("asdf") << endl;
  tab->remove("car");
  cout << tab->find("car");
  delete tab;
  return 0;
}
Пример #6
0
void FW::popMemOwner(void)
{
#if FW_MEM_DEBUG
    U32 threadID = Thread::getID();
    Array<const char*>* stack = s_memOwnerStacks.search(threadID);
    if (stack)
    {
        stack->removeLast();
        if (!stack->getSize())
        {
            s_memOwnerStacks.remove(threadID);
            if (!s_memOwnerStacks.getSize())
                s_memOwnerStacks.reset();
        }
    }
#endif
}
Пример #7
0
int
main ( ) {

  Hash hashTable;
  cout << setprecision ( 10 );
  cout << "Test 1 - print empty table" << endl;
  hashTable.print ( );
  cout << "-------------------------------------------------------------"
       << endl;
  cout << "Test 2 - processing input file" << endl;
  hashTable.processFile ( "dict5.txt" );
  hashTable.print ( );
  cout << "-------------------------------------------------------------"
       << endl;
  cout << "Test 3 - searching" << endl;
  if ( hashTable.search ( "heath" ) ) 
    cout << "Passed - searching for valid item" << endl;
  else
    cout << "FAILED - searching for valid item" << endl;

  if ( hashTable.search ( "hello" ) ) 
    cout << "Passed - searching for valid item" << endl;
  else
    cout << "FAILED - searching for valid item" << endl;

  if ( hashTable.search ( "ttttt" ) ) 
    cout << "Passed - searching for valid item" << endl;
  else
    cout << "FAILED - searching for valid item" << endl;

  if ( hashTable.search ( "empty" ) ) 
    cout << "FAILED - searching for invalid item" << endl;
  else
    cout << "Passed - searching for invalid item" << endl;
  cout << "-------------------------------------------------------------"
       << endl;
  cout << "Test 4 - testing remove" << endl;
  hashTable.remove ( "happy" );
  hashTable.print ( );
  cout << endl;
  hashTable.remove ( "hello" );
  hashTable.remove ( "harps" );
  hashTable.print ( );
  cout << endl;
  hashTable.remove ( "heath" );
  hashTable.remove ( "heath" );
  hashTable.remove ( "heath" );
  hashTable.print ( );
  cout << endl;
  hashTable.remove ( "rrrrr" );
  hashTable.remove ( "ooooo" );
  hashTable.print ( );
  cout << "-------------------------------------------------------------"
       << endl;
  cout << "Test 5 - output to file" << endl;
  hashTable.output ( "hash.out" );
  cout << endl << endl;
  cout << "-------------------------------------------------------------"
       << endl;
  cout << "Test 6 - Stats" << endl;
  hashTable.printStats ( );
  cout << "-------------------------------------------------------------"
       << endl;
  return 1;
}
Пример #8
0
void tst_QHash::insert1()
{
    const char *hello = "hello";
    const char *world = "world";
    const char *allo = "allo";
    const char *monde = "monde";

    {
        typedef QHash<QString, QString> Hash;
        Hash hash;
        QString key;
        for (int i = 0; i < 10; ++i) {
            key[0] = i + '0';
            for (int j = 0; j < 10; ++j) {
                key[1] = j + '0';
                hash.insert(key, "V" + key);
            }
        }

        for (int i = 0; i < 10; ++i) {
            key[0] = i + '0';
            for (int j = 0; j < 10; ++j) {
                key[1] = j + '0';
                hash.remove(key);
            }
        }
    }

    {
        typedef QHash<int, const char *> Hash;
        Hash hash;
        hash.insert(1, hello);
        hash.insert(2, world);

        QVERIFY(hash.size() == 2);
        QVERIFY(!hash.isEmpty());

        {
            Hash hash2 = hash;
            hash2 = hash;
            hash = hash2;
            hash2 = hash2;
            hash = hash;
            hash2.clear();
            hash2 = hash2;
            QVERIFY(hash2.size() == 0);
            QVERIFY(hash2.isEmpty());
        }
        QVERIFY(hash.size() == 2);

        {
            Hash hash2 = hash;
            hash2[1] = allo;
            hash2[2] = monde;

            QVERIFY(hash2[1] == allo);
            QVERIFY(hash2[2] == monde);
            QVERIFY(hash[1] == hello);
            QVERIFY(hash[2] == world);

            hash2[1] = hash[1];
            hash2[2] = hash[2];

            QVERIFY(hash2[1] == hello);
            QVERIFY(hash2[2] == world);

            hash[1] = hash[1];
	    QVERIFY(hash[1] == hello);
	}
	        {
            Hash hash2 = hash;
            hash2.detach();
            hash2.remove(1);
            QVERIFY(hash2.size() == 1);
            hash2.remove(1);
            QVERIFY(hash2.size() == 1);
            hash2.remove(0);
            QVERIFY(hash2.size() == 1);
            hash2.remove(2);
            QVERIFY(hash2.size() == 0);
            QVERIFY(hash.size() == 2);
        }

        hash.detach();

        {
            Hash::iterator it1 = hash.find(1);
            QVERIFY(it1 != hash.end());

            Hash::iterator it2 = hash.find(0);
            QVERIFY(it2 != hash.begin());
            QVERIFY(it2 == hash.end());

            *it1 = monde;
            QVERIFY(*it1 == monde);
            QVERIFY(hash[1] == monde);

            *it1 = hello;
            QVERIFY(*it1 == hello);
            QVERIFY(hash[1] == hello);

            hash[1] = monde;
            QVERIFY(it1.key() == 1);
            QVERIFY(it1.value() == monde);
            QVERIFY(*it1 == monde);
            QVERIFY(hash[1] == monde);

            hash[1] = hello;
            QVERIFY(*it1 == hello);
            QVERIFY(hash[1] == hello);
        }

        {
            const Hash hash2 = hash;

            Hash::const_iterator it1 = hash2.find(1);
            QVERIFY(it1 != hash2.end());
            QVERIFY(it1.key() == 1);
            QVERIFY(it1.value() == hello);
            QVERIFY(*it1 == hello);

            Hash::const_iterator it2 = hash2.find(2);
            QVERIFY(it1 != it2);
            QVERIFY(it1 != hash2.end());
            QVERIFY(it2 != hash2.end());

            int count = 0;
            it1 = hash2.begin();
            while (it1 != hash2.end()) {
                count++;
                ++it1;
            }
            QVERIFY(count == 2);

            count = 0;
            it1 = hash.begin();
            while (it1 != hash.end()) {
                count++;
                ++it1;
            }
            QVERIFY(count == 2);
        }

        {
            QVERIFY(hash.contains(1));
            QVERIFY(hash.contains(2));
            QVERIFY(!hash.contains(0));
            QVERIFY(!hash.contains(3));
        }

        {
            QVERIFY(hash.value(1) == hello);
            QVERIFY(hash.value(2) == world);
            QVERIFY(hash.value(3) == 0);
            QVERIFY(hash.value(1, allo) == hello);
            QVERIFY(hash.value(2, allo) == world);
            QVERIFY(hash.value(3, allo) == allo);
            QVERIFY(hash.value(0, monde) == monde);
        }

        {
            QHash<int,Foo> hash;
            for (int i = 0; i < 10; i++)
                hash.insert(i, Foo());
            QVERIFY(Foo::count == 10);
            hash.remove(7);
            QVERIFY(Foo::count == 9);

        }
        QVERIFY(Foo::count == 0);
        {
            QHash<int, int*> hash;
            QVERIFY(((const QHash<int,int*>*) &hash)->operator[](7) == 0);
        }
    }
}
int main()
{
	Hash<char> toTest (hasher, 10);

	// isEmpty() test
	cout << "\nisEmpty() Test" << endl;
	cout << "isEmpty(): " << toTest.isEmpty() << endl;
	cout << "isFull(): " << toTest.isFull() << endl;
	toTest.showStructure();

	// insert() test
	cout << "\ninsert() Test" << endl;

	for (char c = '0'; c <= 'z'; c++)
	{
		toTest.insert(c);
	}

	toTest.showStructure();

	// isFull() test
	cout << "\nisFull() Test" << endl;
	cout << "isEmpty(): " << toTest.isEmpty() << endl;
	cout << "isFull(): " << toTest.isFull() << endl;

	// remove() and retrieve() test
	cout << "\nremove() and retrieve() Test" << endl;
	char rtest = ' ';
	cout << "remove(3): " << toTest.remove(3) << endl;
	bool rreturn = toTest.retrieve(3, rtest);
	cout << "retrieve(3, rtest): " << rreturn << " | rtest = " << rtest << endl;
	toTest.showStructure();

	// clear() test
	cout << "\nclear() Test" << endl;
	toTest.clear();
	toTest.showStructure();

	// isEmpty() test
	cout << "\nisEmpty() Test" << endl;
	cout << "isEmpty(): " << toTest.isEmpty() << endl;
	cout << "isFull(): " << toTest.isFull() << endl;
	toTest.showStructure();

	// Int test version
	Hash<int> testTwo(hasher_two, 20);
	cout << "\n\nTesting with ints:" << endl;

	// insert() test
	cout << "\ninsert() Test" << endl;

	for (int i = 1; i <= 115; i++)
	{
		testTwo.insert(i);
	}

	testTwo.showStructure();

	// isFull() test
	cout << "\nisFull() Test" << endl;
	cout << "isEmpty(): " << testTwo.isEmpty() << endl;
	cout << "isFull(): " << testTwo.isFull() << endl;

	// remove() and retrieve() test
	cout << "\nremove() and retrieve() Test" << endl;
	cout << "remove(3): " << testTwo.remove(3) << endl;
	int itest;
	bool ireturn = testTwo.retrieve(3, itest);
	cout << "retrieve(3, rtest): " << ireturn << " | rtest = " << itest << endl;
	testTwo.showStructure();

	// clear() test
	cout << "\nclear() Test" << endl;
	testTwo.clear();
	testTwo.showStructure();

	system("pause");
}