import java.io.IOException;

public class HashExperiment2
{
		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();

				float factors[] = new float[]{0.2f, 0.4f, 0.6f, 0.8f, 0.9f, 0.95f};

				for (int i=0; i<factors.length; i++)
				{
						System.out.println("Load factor: " + factors[i]);

				
				/* Create a HashSet with capacity for half the words and a low
							load factor */
				HashSet<HashedString> hash = 
						new HashSet<HashedString>(dataSplit[0].size(), factors[i], 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());
				}
		}
		
}