void HashTable::add(string key, int amount)
{
  int hash = hashFunction->getHash(key) % size;
  all += amount;
  if (!listArray[hash].find(key))
    occupied++;
  listArray[hash].add(hash, key, amount);

  if (loadFactor() > 5)
  {
    int newSize = size * 2;

    List *tempArray = new List[newSize];
    for (int i = 0; i < size; i++)
    {
      List::ListElement *curElement = listArray[i].head->next;
      while (curElement != nullptr)
      {
        tempArray[hashFunction->getHash(curElement->key) % newSize].add(hashFunction->getHash(curElement->key), curElement->key, curElement->amount);
        curElement = curElement->next;
      }
    }

    delete[] listArray;
    listArray = tempArray;
    size = newSize;
  }
}
Example #2
0
void printStat(HashTable *hashT)
{
	printf("\n****************************************************************\n");
	printf("%d elements\n", HTLoad(hashT));
	printf("%.5f load-factor\n", loadFactor(hashT));
	printf("%d filled cells\n", HTLoad(hashT));
	printf("%.5f collision level\n", (double)elemQuant(hashT) / (double)HTLoad(hashT));
	printf("****************************************************************\n");
}
Example #3
0
bool algot::HashTable::add(std::string str){
  unsigned int hashNum = useHash(str, tableSize);
  algot::DLL<std::string>* list = table + hashNum;
  if(list->isEmpty()){
    usedCells++;
  }
  else if(list->get(str)){
    return false;
  }
  list->add(str);
  if(loadFactor() > 0.75){
    resizeTo((tableSize-1)*2 + 1); 
    //The plus one is to avoid the tableSize being a direct power of two all the time.
  }

  return true;
}
Example #4
0
//Insert the Item (key, v) in the table
//If key already exists in the table then change the associated value to v
//Re-hash if the table becomes 50% full
// IMPLEMENT
void HashTable::insert(string key, int v)
{
    int index = h(key, size);

    if(hTable[index] != nullptr)
    {
        if(hTable[index]->key != key)
        {
            while(hTable[index] != nullptr)
            {
                if(index == (size - 1))
                {
                    index = 0;
                }
                else
                {
                    index++;
                }
            }
            hTable[index] = new Item(key, v);
            nItems++;
        }
        else
        {
            hTable[index]->value += v;
        }
    }
    else
    {
        hTable[index] = new Item(key, v);
        nItems++;
    }

    if( (loadFactor()) >= MAX_LOAD_FACTOR)
    {
        reHash();
    }
}
Example #5
0
void Commands::handleCommands()
{

	std::string line, first, second, third;
	do
	{
		std::string temp;
		int count = 0;
		first = second = third = "";
		std::getline(std::cin, line);
		std::stringstream lineStream1(line);
		std::stringstream lineStream2(line);

		while (lineStream1 >> temp)
			count++;

		if (count>3)
		{
			std::cout << "INVALID" << std::endl;
			continue;
		}

		lineStream2 >> first;
		lineStream2 >> second;
		lineStream2 >> third;

		line = first + " " + second + " " + third;

		if (line.find(" ") == -1)
			first = line;
		else
		{
			first = line.substr(0, line.find(" "));
			line.erase(0, line.find(" ") + 1);
			if (line.find(" ") == -1)
				second = line;
			else
			{
				second = line.substr(0, line.find(" "));
				line.erase(0, line.find(" ") + 1);
				third = line;
			}
		}
		if (first == "CREATE" && second != "" && third != "")
			create(second, third);
		else if (first == "LOGIN" && second != "" && third != "")
			login(second, third);
		else if (first == "LOGIN" && second == "COUNT" && third == "")
			loginCount();
		else if (first == "REMOVE")
			remove(second);
		else if (first == "DEBUG" && (second == "ON" || second == "OFF"))
			debug(debugFlag, second);
		else if (first == "BUCKET" && second == "COUNT")
			bucketCount();
		else if (first == "LOAD" && second == "FACTOR")
			loadFactor();
		else if (first == "CLEAR")
		{
			logins.deleteBucket();
			std::cout << "CLEARED" << std::endl;
		}
		else if (first == "QUIT")
			std::cout << "GOODBYE" << std::endl;

		else if (first == "MAX" && second == "BUCKET" && third == "SIZE")
			maxBucketSize();
		else
			std::cout << "INVALID" << std::endl;
	} while (first != "QUIT");
}