#include "bag_array_dhash.hpp"
#include <iostream>
#include <string>
#include <fstream>

int hash( string x );

int main()
{
  //Opening file
  fstream fin;
  fin.open("words", ios::in | ios::nocreate);
  if(fin.fail())
    cout << "Error opening dictionary file";
  
  //Declaring variables
  string e = "Empty", d = "Deleted";
  bag<string, 65003, e, d> baggy;
  string temp;
  int choice;

  //Reading file into bag
  while(!fin.eof())
  {
    fin >> temp;
    fin >> ws;
    baggy.insert(temp, hash(temp));
  }

  //Closing file
  fin.close();

  //Main menu loop
  cout << "Type \"Quit\" to quit." << endl;
  while( 1 )
  {
    //Input and lookup of word
    cout << "Enter a word to lookup: ";
    cin >> temp;
    if ( temp == "Quit" )
      return( EXIT_SUCCESS );
    if( baggy.has(temp, hash(temp)) )
        cout << temp << " is in the bag." << endl;
    else
        cout << temp << " is not in the bag." << endl;
  }
}
  
int hash( string x )
{
  int i = 0;
  int hash = 0;
  int length = x.length();
  for( i; i < length; i++ )
    hash = hash + 11 * ( int( x[ length - i - 1 ] ));
  return hash;
}
