123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. package Solution;
  2. import java.io.File;
  3. import java.lang.reflect.Method;
  4. import java.util.Arrays;
  5. import java.util.Collection;
  6. import java.util.HashSet;
  7. import java.util.Scanner;
  8. import java.util.TreeSet;
  9. public class UnsortedSetTest {
  10. public static void main(String[] args) throws Exception {
  11. String[] allFileNames = {"hounds.txt", "huckfinn.txt", "oz.txt", "war.txt", "ciaFactBook2008.txt"};
  12. String[] noCIA = {"hounds.txt", "huckfinn.txt", "oz.txt", "war.txt"};
  13. countWords(new BinarySearchTree<String>(), allFileNames[0]);
  14. for(String s : allFileNames) {
  15. System.out.println(s);
  16. countWordsOurUnsortedSet(s);
  17. countWordsOurBinarySearchTree(s);
  18. countWordsOurHash(s);
  19. countWordsCollection(new TreeSet<String>(), s);
  20. int[] result = countWordsCollection(new HashSet<String>(), s);
  21. System.out.println(result[0] + " total words.");
  22. System.out.println(result[1] + " distinct words.");
  23. System.out.println();
  24. }
  25. }
  26. // return total num words, and num distinct words
  27. public static int[] countWordsCollection(Collection<String> c, String fileName) throws Exception{
  28. c.clear();
  29. Scanner fileScanner = new Scanner(new File(fileName));
  30. Stopwatch st = new Stopwatch();
  31. st.start();
  32. int total = 0;
  33. while(fileScanner.hasNext()){
  34. c.add(fileScanner.next());
  35. total++;
  36. }
  37. st.stop();
  38. System.out.println("Time for " + c.getClass() + " : \n" + st);
  39. // System.out.println(c.size() + " distinct words");
  40. // System.out.println(total + " total words including duplicates: ");
  41. assert total >= c.size();
  42. System.out.println();
  43. return new int[]{total, c.size()};
  44. }
  45. // GACKY GACKY GACKY repition. Look into removing repetition with reflection
  46. // we assume there will be add and size methods
  47. public static int[] countWordsOurHash(String fileName) throws Exception {
  48. Scanner fileScanner = new Scanner(new File(fileName));
  49. Stopwatch st = new Stopwatch();
  50. UnsortedHashSet<String> c = new UnsortedHashSet<String>();
  51. st.start();
  52. int total = 0;
  53. while(fileScanner.hasNext()) {
  54. c.add(fileScanner.next());
  55. total++;
  56. }
  57. st.stop();
  58. System.out.println("Time for our hashtable (closed address hashing): \n" + st);
  59. // System.out.println(c.size() + " distinct words");
  60. // System.out.println(total + " total words including duplicates: ");
  61. assert total >= c.size();
  62. System.out.println();
  63. return new int[]{total, c.size()};
  64. }
  65. public static int[] countWordsOurUnsortedSet(String fileName) throws Exception {
  66. Scanner fileScanner = new Scanner(new File(fileName));
  67. Stopwatch st = new Stopwatch();
  68. UnsortedSet<String> c = new UnsortedSet<String>();
  69. st.start();
  70. int total = 0;
  71. while(fileScanner.hasNext()){
  72. c.add(fileScanner.next());
  73. total++;
  74. }
  75. st.stop();
  76. System.out.println("Time for our unsorted set based on ArrayList: \n" + st);
  77. // System.out.println(c.size() + " distinct words");
  78. // System.out.println(total + " total words including duplicates: ");
  79. assert total >= c.size();
  80. System.out.println();
  81. return new int[]{total, c.size()};
  82. }
  83. public static int[] countWordsOurBinarySearchTree(String fileName) throws Exception {
  84. Scanner fileScanner = new Scanner(new File(fileName));
  85. Stopwatch st = new Stopwatch();
  86. BinarySearchTree<String> c = new BinarySearchTree<String>();
  87. st.start();
  88. int total = 0;
  89. while(fileScanner.hasNext()){
  90. c.add(fileScanner.next());
  91. total++;
  92. }
  93. st.stop();
  94. System.out.println("Time for our binary search tree: \n" + st);
  95. // System.out.println(c.size() + " distinct words");
  96. // System.out.println(total + " total words including duplicates: ");
  97. assert total >= c.size();
  98. System.out.println();
  99. return new int[]{total, c.size()};
  100. }
  101. // a try at reflection. Not working on Binary Search tree from class.
  102. // Hunch. Due to add method taking in Comparable, not Object!
  103. // Alterantives: search list of methods for name?
  104. public static int[] countWords(Object c, String fileName) throws Exception {
  105. Scanner fileScanner = new Scanner(new File(fileName));
  106. Stopwatch st = new Stopwatch();
  107. System.out.println(Arrays.toString(c.getClass().getMethods()));
  108. Method addMethod = c.getClass().getMethod("add", Object.class);
  109. st.start();
  110. int total = 0;
  111. while(fileScanner.hasNext()){
  112. addMethod.invoke(c, fileScanner.next());
  113. total++;
  114. }
  115. st.stop();
  116. System.out.println("Time for " + c.getClass() + ": "+ st);
  117. Method sizeMethod = c.getClass().getMethod("size");
  118. int distictWords = (Integer) sizeMethod.invoke(c);
  119. // System.out.println(distictWords + " distinct words");
  120. // System.out.println(total + " total words including duplicates: ");
  121. System.out.println();
  122. return new int[]{total, distictWords};
  123. }
  124. }