void NonCollisionHashTable::rebuild() { clear(); setSize(keys.getSize() * keys.getSize()); generateUniversalHashFunction(table.size(), primeNumber, hash); if (!keys.isEmpty()) { MyNode<int>* it = keys.getBegin(); while (it != 0) { if (!simpleInsert(it->item)) { it = keys.getBegin(); clear(); generateUniversalHashFunction(table.size(), primeNumber, hash); } else { it = it->next; } } } needRebuild = false; }
void FixedSet::Initialize(const vector<int>& numbers) { size_t sizeOfSecondTables = 0; size_t tableSize = numbers.size(); MyList<size_t> secondTablesForRebuild; /* clock_t t1 = clock();*/ secondHashTables.resize(tableSize); // clock_t t2 = clock(); // cout << (double)(t2 - t1) / CLOCKS_PER_SEC << endl; generateUniversalHashFunction(tableSize, primeNumber, hash); for (size_t index = 0; index < tableSize; ++index) { size_t hashValue = hash(numbers[index]); if (!secondHashTables[hashValue].insert(numbers[index])) { if (!secondHashTables[hashValue].isNeedRebuild()) { secondHashTables[hashValue].setRebuild(true); secondTablesForRebuild.pushFront(hashValue); } --sizeOfSecondTables; } else { ++sizeOfSecondTables; } } MyNode<size_t>* it = secondTablesForRebuild.getBegin(); for (; it != 0 && !secondTablesForRebuild.isEmpty(); it = it->next) { size_t indexOfSecondHashTable = it->item; secondHashTables[indexOfSecondHashTable].rebuild(); size_t secondTableSize = secondHashTables[indexOfSecondHashTable].getSize(); sizeOfSecondTables += secondTableSize; } if (sizeOfSecondTables > 4 * tableSize) { for (size_t index = 0; index < tableSize; ++index) { if (!secondHashTables[index].isEmpty()) { secondHashTables[index].clear(); secondHashTables[index].deleteKeys(); secondHashTables[index].setRebuild(false); secondHashTables[index].setSize(0); secondHashTables[index].setEmpty(true); } } Initialize(numbers); } }
void NonCollisionHashTable::clear() { if (!keys.isEmpty()) { MyNode<int>* it = keys.getBegin(); bool empty = table[hash(it->item)].second; for (; it != 0 && !empty; it = it->next) { empty = table[hash(it->item)].second; table[hash(it->item)] = make_pair<int, bool>(0, true); } } }