import java.io.IOException;


/**
 * An experimental apparatus for string hashing. The main method reads
 * a list of words, splits it in half, creates a HashSet of
 * HashedStrings and inserts half the words into it. Then it measures
 * the average number of probes to query those words (successfully)
 * and query the other words (unsuccesfully).
 *
 * @author Jerod Weinman
 * @version 1.0
 * @date 20 March 2009
 */
public class HashExperiment 
{
  public static void main(String[] args)
  {
    /* Expects the file name of word list as its first argument */
    String wordFileName = args[0];

    WordData dataAll = null;
    WordData[] dataSplit;

    /* Try to load words */
    try 
    {
      dataAll = new WordData(wordFileName);
    } 
    catch (IOException e)
    {
      System.err.println(e);
      System.exit(1);
    }

    /* Randomly split words in half */
    dataSplit = dataAll.randomSplit();

    /* Create a HashSet with capacity for half the words and a low
       load factor */
    HashSet<HashedString> hash = 
      new HashSet<HashedString>(dataSplit[0].size(), 0.2f, false);

    /* Add the words from the first half to the hash set */
    for (String word : dataSplit[0] )
      hash.add(new HashedString(word));

    /* Reset the number of probes for successful searches */
    hash.resetNumProbes();

    /* Search for the words from the first half in the hash set */
    for (String word : dataSplit[0] )
      if (!hash.contains(new HashedString(word)))
        System.err.println("ERROR: Word \"" + word + "\" missing from HashSet");
    
    /* Print average number of probes */
    System.out.println("Average probes per successful query: " + 
                       (double)hash.getNumProbes()/dataSplit[0].size());

    /* Reset the number of probes for successful searches */
    hash.resetNumProbes();

    /* Search for the words from the second half in the hash set */
    for (String word : dataSplit[1] )
      if (hash.contains(new HashedString(word)))
        System.err.println("ERROR: Word \"" + word + "\" found in HashSet");
   
    /* Print average number of probes */
    System.out.println("Average probes per unsuccessful query: " + 
                       (double)hash.getNumProbes()/dataSplit[1].size());

  }
  
}