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; } } }
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // Lab Required Functions - public //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ bool Hash::insert(int x) { if (contains(x)) return false; int index = 0; std::cout << "Load factor: " << (float(active + 1) / float(m_size)) << std::endl; if ((float(active + 1) / float(m_size)) > 0.5) { Hash* tempList = new Hash(); Node* temp = m_front; for (int i = 0; i < m_size; i++) { if (temp->getValue() != -1) tempList->addBack(temp->getValue()); temp = temp->getNext(); }//for - gather hashed data tempList->addBack(x); //scrub hash scrub(); //reset active active = 0; //adjust list for new bucket size (after calculating next prime #) adjust(getPrime(m_size)); for ( ; !tempList->isEmpty(); ) { int val = tempList->pop(); for(int i = 0; i < m_size; i++) { temp = m_front; index = hash(val, i); for (int k = 0; k < index; k++) temp = temp->getNext(); if (temp->getValue() == -1) { temp->setValue(val); active++; //break loop i = m_size; } //else keep going while hash = -1 with true flag }//for each entry in the hash table (mod makes this cycle through every element) }//for - while tempList is not empty delete tempList; return true; }//if - rehash everything! else { //insert new value Node* temp = m_front; for(int i = 0; i < m_size; i++) { temp = m_front; index = hash(x, i); //std::cout << "Val: " << i << " hash: " << index << std::endl; for (int k = 0; k < index; k++) temp = temp->getNext(); if (temp->getValue() == -1) { //std::cout << "Inserting value at: " << i << std::endl; temp->setValue(x); active++; return true; } else if (temp->getValue() == x) i = m_size; // duplicate precaution - really should never happen //else keep going while hash = -1 with true flag }//for each entry in the hash table (mod makes this cycle through every element) } return false; }//insert
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"); }