/*
  Travis Gadberry
  Patrick Hesser
  Chris Ladewig

  HashTable.cpp
  21Mar05
*/

#include "HashTable.h"

using namespace std;

HashTable::HashTable(int * A, int c){
  time_t t1, t2;
  int col=0;
  t1 = clock();
  int b=1;
  if (c==10)
  b=c+1;
  else if (c==1000)
  b=c+9;
  else if (c==10000)
  b=c+7;
  setM(b);
  int j=0, i = 0, k=0;

  table = new int[b];

  for(int l=0; l<b; l++)
    table[l]=-1;

  do {
    k = h(A[i], j);
    if (creationincrements++, table[k] == EmptySlot)
    {
		table[k] = A[i];
		i++;
		j = 0;
    } 
    else {
      j++;
    }
  } while (i < b);
  
  t2 = clock();
  creationtime = (t2 - t1)/CLOCKS_PER_SEC;
  searchtime=0;

}

HashTable::HashTable(vector<int> V){
  int c = V.size();
  int * A = new int[c];
  for(int i=0; i<c; i++)
    A[i] = V.at(i);

  time_t t1, t2;
  int col=0;
  t1 = clock();
  int b=1;
  if (c==10)
  b=c+1;
  else if (c==1000)
  b=c+9;
  else if (c==10000)
  b=c+7;
  setM(b);
  int j=0, i = 0, k=0;

  table = new int[b];

  for(int l=0; l<b; l++)
    table[l]=-1;

  do {
    k = h(A[i], j);
    if (creationincrements++, table[k] == EmptySlot)
    {
		table[k] = A[i];
		i++;
		j = 0;
    } 
    else {
      j++;
    }
  } while (i < b);
  
  t2 = clock();
  creationtime = (t2 - t1)/CLOCKS_PER_SEC;
  searchtime=0;

}

int HashTable::h(int k, int i) const{
  int hp=((k%m)+(i*(1+(k%(m-1)))))%m;
  return hp;
}

bool HashTable::search(int a){
//	  for(int k = 0; k < 10; k++)
//	  cout << k << " : " << table[k] << endl;

  time_t t1, t2;
  t1 = clock();
  bool found = false;
  int j, i = 0;
  do {
    j = h(a,i);
    if (searchincrements++, table[j] == a)
	{
		found = true;
		
	}
  
    i++;
    
  } while ((table[j] != EmptySlot) && (i < m) && (found==false));
  
  t2 = clock();
  searchtime = (t2 - t1)/CLOCKS_PER_SEC;
  return found;
}
  
int * HashTable::getTable(){
  return table;
}

double HashTable::getCreationTime() const{
  return creationtime;
}

void HashTable::setCreationTime(int a){
  creationtime=a;
}

int HashTable::getCreationIncrements() const{
  return creationincrements;
}

double HashTable::getSearchTime() const{
  return searchtime;
}

void HashTable::setSearchTime(int a){
  searchtime=a;
}

int HashTable::getSearchIncrements() const{
  return searchincrements;
}

int HashTable::getM() const{
  return m;
}

void HashTable::setM(int a){
 m=a;
}

