package hash;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.*;

//----------------------------------------------------------

public class Hash {

	BufferedReader reader;
	int total_words, suspect_words;
	
//----------------------------------------------------------

	// open a file and look up all words in it
	
	void lookup_all_words(String fileName, Dictionary D, boolean doSet, boolean doHash)
	{
		long startTime = System.currentTimeMillis();
		
		total_words = suspect_words = 0;
		
		try {

			// set input and output filenames

			File inFile = new File(fileName);
			FileReader fileReader = new FileReader(inFile);	
			reader = new BufferedReader(fileReader);

			// line by line

			while (true) {

				// get entire line as one String

				String line = reader.readLine();

				// end of file?

				if (line == null)
					break;

				// split line into words 

				String[] words = line.split("[()_:;\"\\?\\!\\.\\-,\\s]+");

				// iterate over words on *this* line, writing each to output file

				for (String word : words) {
					
					if (word.length() > 0 && Character.isLetter(word.charAt(0))) {
						total_words++;
				
						if (!D.lookup(word, doSet, doHash))
							suspect_words++;
					}
				}
			}

			reader.close();

			System.out.println("Total = " + total_words + ", suspect = " + suspect_words);
			
		} catch (Exception ex) {
			System.out.println("Exception handler!");
			ex.printStackTrace();
		}	
		
		long endTime = System.currentTimeMillis();
		System.out.println("That took " + (endTime - startTime) + " milliseconds");
	}
	
//----------------------------------------------------------

	public static void main(String[] args) {

		/*
		HashSet<String> S1 = new HashSet<String>();

		S1.add("horse");
		S1.add("cow");
		S1.add("pig");
		S1.add("horse");
		S1.add("turkey");
				
		Set<String> S2 = new HashSet<String>();

		S2.add("horse");
		S2.add("cow");
		S2.add("chicken");
		S2.add("goat");
	
		
		Set<String> union = new HashSet<String>(S1);
		Set<String> intersection = new HashSet<String>(S1);
		Set<String> difference = new HashSet<String>(S1);
		
		union.addAll(S2);
		intersection.retainAll(S2);
		difference.removeAll(S2); 
		
		System.out.println("S1 = " + S1);
		System.out.println("S2 = " + S2);
		
		System.out.println("S1 v S2 = " + union);
		System.out.println("S1 ^ S2 = " + intersection);
		System.out.println("S1 - S2 = " + difference);
	
		System.exit(1);
*/
		
	
		Dictionary D = new Dictionary("words.txt");
		
		Hash H = new Hash();
	//	String fileName = "doi.txt";
		//String fileName = "bts.txt";
		String fileName = "hod.txt";
		
		System.out.println("--------------------\nArrayList\n--------------------");
		H.lookup_all_words(fileName, D, false, false);
	
		System.out.println("--------------------\nTreeSet\n--------------------");
		H.lookup_all_words(fileName, D, true, false);
		
		System.out.println("--------------------\nHashSet\n--------------------");
		H.lookup_all_words(fileName, D, true, true);

	}
}

//----------------------------------------------------------

class Dictionary {

	ArrayList<String> arrayDictionary;
	HashSet<String> hashDictionary;
	TreeSet<String> treeDictionary;
	
	BufferedReader reader;

	//----------------------------------------------------------

	boolean lookup(String word, boolean doSet, boolean doHash)
	{
		if (doSet) {
			if (doHash)
				return hashDictionary.contains(word);
			else
				return treeDictionary.contains(word);
		}
		else
			return arrayDictionary.contains(word);
	}

	//----------------------------------------------------------

	Dictionary(String fileName)
	{
		arrayDictionary = new ArrayList<String>();
		hashDictionary = new HashSet<String>();
		treeDictionary = new TreeSet<String>();

		try {

			// set input and output filenames

			File inFile = new File(fileName);
			FileReader fileReader = new FileReader(inFile);	
			reader = new BufferedReader(fileReader);

			// line by line

			while (true) {

				// get entire line as one String

				String line = reader.readLine();

				// end of file?

				if (line == null)
					break;

				// split line into words 

				String[] words = line.split("[()_:;\"\\?\\!\\.\\-,\\s]+");

				// iterate over words on *this* line, writing each to output file

				for (String word : words) {
					hashDictionary.add(word);
					arrayDictionary.add(word);
					treeDictionary.add(word);
				}
			}

			reader.close();

		} catch (Exception ex) {
			System.out.println("Exception handler!");
			ex.printStackTrace();
		}	
	}
}

