-
Notifications
You must be signed in to change notification settings - Fork 0
/
RunTimeTest.java
128 lines (116 loc) · 3.6 KB
/
RunTimeTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package shake_n_bacon;
import java.io.IOException;
import providedCode.*;
/**
* An executable that counts the words in a files and prints out the counts in
* descending order. You will need to modify this file.
*/
public class RunTimeTest {
/*
* Param counter is the given data counter to be used
* This is used to get all of the words from the counter, and how many
* times each word was used (count), storing them in an orderly manner.
*/
private static DataCount[] getCountsArray(DataCounter counter) {
if (counter.getSize() <= 0) {
return new DataCount[0];
}
DataCount[] countArray = new DataCount[counter.getSize()];
SimpleIterator itr = counter.getIterator();
int i = 0;
while (itr.hasNext()) {
DataCount next = itr.next();
countArray[i] = new DataCount(next.data, next.count);
i++;
}
return countArray;
}
// ////////////////////////////////////////////////////////////////////////
// /////////////// DO NOT MODIFY ALL THE METHODS BELOW ///////////////////
// ////////////////////////////////////////////////////////////////////////
private static void countWords(String file, DataCounter counter) {
try {
FileWordReader reader = new FileWordReader(file);
String word = reader.nextWord();
while (word != null) {
counter.incCount(word);
word = reader.nextWord();
}
} catch (IOException e) {
System.err.println("Error processing " + file + " " + e);
System.exit(1);
}
}
// IMPORTANT: Used for grading. Do not modify the printing *format*!
private static void printDataCount(DataCount[] counts) {
for (DataCount c : counts) {
System.out.println(c.count + "\t" + c.data);
}
}
/*
* Sort the count array in descending order of count. If two elements have
* the same count, they should be ordered according to the comparator. This
* code uses insertion sort. The code is generic, but in this project we use
* it with DataCount and DataCountStringComparator.
*
* @param counts array to be sorted.
*
* @param comparator for comparing elements.
*/
private static <E> void insertionSort(E[] array, Comparator<E> comparator) {
for (int i = 1; i < array.length; i++) {
E x = array[i];
int j;
for (j = i - 1; j >= 0; j--) {
if (comparator.compare(x, array[j]) >= 0) {
break;
}
array[j + 1] = array[j];
}
array[j + 1] = x;
}
}
/*
* Print error message and exit
*/
private static void usage() {
System.err
.println("Usage: [-s | -o] <filename of document to analyze>");
System.err.println("-s for hashtable with separate chaining");
System.err.println("-o for hashtable with open addressing");
System.exit(1);
}
/**
* Entry of the program
*
* @param args
* the input arguments of this program
*/
public static void main(String[] args) {
if (args.length != 2) {
usage();
}
String firstArg = args[0].toLowerCase();
DataCounter counter = null;
if (firstArg.equals("-s")) {
counter = new HashTable_SC(new StringComparator(),
new StringHasher());
} else if (firstArg.equals("-o")) {
counter = new HashTable_OA(new StringComparator(),
new StringHasher());
} else {
usage();
}
for (int timing = 0; timing < 10; ++timing) {
long startTime = System.nanoTime();
countWords(args[1], counter);
DataCount[] counts = getCountsArray(counter);
//insertionSort(counts, new DataCountStringComparator());
//printDataCount(counts);
long endTime = System.nanoTime();
long elapsedTime = endTime - startTime;
System.out.println(elapsedTime + " nanoseconds or "
+ elapsedTime/(1000000000.0) + " seconds elapsed of insert time");
}
}
}