Beispiel #1
0
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);
  }  
}
Beispiel #2
0
bool NonCollisionHashTable::insert(int key)
{
  if (empty)
  {
     setSize(1);
     generateUniversalHashFunction(1, primeNumber, hash);
     empty = false;
  }
  keys.pushFront(key);
  return simpleInsert(key);
}