-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainSearch.java
173 lines (143 loc) · 5.73 KB
/
MainSearch.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// Test Main for AVL and Linear Hashing
// File IO is done through Stuart Reges' TextInputStream class.
//
import java.io.*;
//import java.lang.Object;
import java.lang.management.*;
public class MainSearch {
public static ThreadMXBean TMB;
public static void main(String args[]) throws IOException
{
long cputime;
if (args.length != 3) {
System.err.println("Usage: java MainBst train-file query-file LHsize");
System.exit(0);
}
String trainFile = args[0];
String queryFile = args[1];
int LHsize = Integer.parseInt(args[2]);
System.out.print(">> train: "+trainFile+", query: "+queryFile);
System.out.println(", LHsize: "+LHsize+"\n");
TMB = ManagementFactory.getThreadMXBean();
if (! TMB.isThreadCpuTimeSupported()) {
System.out.println("ThreadCpuTime is not supported.");
System.exit(0);
}
// (1) Create a plain BST and an AVL from the train set.
BST bst = new BST();
buildBST(bst, trainFile);
AVL avl = new AVL();
buildBST(avl, trainFile);
System.out.println("Number of words in the BST: "+bst.size()
+" (number of insertions: "+bst.sumFreq()+")");
//System.out.println("BST rootNode's Height: " + bst.getRoot().getHeight() );
//System.out.println("AVL rootNode's Height: " + avl.getRoot().getHeight() );
// (2) Probe the plain BST and AVL to find words from the query set.
//System.out.println("Sum of Weighted Path Lengths (BST): "
// +bst.sumWeightedPath());
bst.resetCounters();
probeBST(bst,queryFile);
//System.out.println("Sum of Weighted Path Lengths (AVL): "
// +avl.sumWeightedPath());
avl.resetCounters();
probeBST(avl,queryFile);
System.out.println();
// (3) Create a Linear Hash table from the train set.
LinearHash lhash = new LinearHash(LHsize);
buildLHash(lhash,trainFile);
// (4) Probe the Linear Hash table to find words from the query set.
probeLHash(lhash,queryFile);
// (5) Report the memory usage.
Runtime runtime = Runtime.getRuntime();
System.out.println("\nMemory consumption: "
+ (runtime.totalMemory() - runtime.freeMemory()) + " bytes\n");
}
// ************************************************************************** //
private static void buildBST(BST bst, String input)
{
TextInputStream sfs = new TextInputStream(input);
long cputime = TMB.getCurrentThreadCpuTime();
while(sfs.ready()) bst.insert(sfs.readWord());
cputime = TMB.getCurrentThreadCpuTime() - cputime;
bst.print();
String bstType = (bst instanceof AVL)? "AVL" : "BST";
System.out.println("CPU time to build a(n) "+bstType+": "
+(cputime/1000000)+" millisec");
}
private static void probeBST(BST bst, String keys)
{
TextInputStream qfs = new TextInputStream(keys);
int notfound=0;
long cputime = TMB.getCurrentThreadCpuTime();
while(qfs.ready()) {
String queryWord = qfs.readWord();
if (bst.find(queryWord)==false) {
// Uncomment the next line to make it verbose.
//System.out.println("The word `"+queryWord+"' not found.");
notfound++;
}
}
cputime = TMB.getCurrentThreadCpuTime() - cputime;
bst.print();
String bstType = (bst instanceof AVL)? "AVL" : "BST";
System.out.println("Total number of node accesses ("+bstType+"): "
+bst.sumProbes()+" (failed searches: "+notfound+")");
System.out.println("CPU time for searching keys ("+bstType+"): "
+(cputime/1000000)+" millisec");
}
// ************************************************************************** //
private static void buildLHash(LinearHash lhash, String trainFile)
{
TextInputStream dfs = new TextInputStream(trainFile);
int failedInsert = 0;
long cputime = TMB.getCurrentThreadCpuTime();
while(dfs.ready()) {
String trainWord = dfs.readWord();
//if (lhash.insert(trainWord) < 0) {
if (lhash.insertUnique(trainWord) < 0) {
// Uncomment the next line to make it verbose.
//System.out.println("Failed to insert `"+trainWord+"' into HT.");
failedInsert++;
}
}
cputime = TMB.getCurrentThreadCpuTime() - cputime;
float storageUtil = (float)lhash.wordCount()
/ (lhash.wordCount()+lhash.emptyCount());
// lhash.wordCount()/lhash.size() does not work for Linear Hashing.
lhash.print();
//lhash.printHashStat();
System.out.println("CPU time to build the Hash Table: "
+(cputime/1000000)+" millisec");
System.out.print("Words inserted: "+lhash.wordCount());
System.out.print(" (insertions failed: "+failedInsert+"); ");
System.out.print("Storage util: "+storageUtil);
System.out.println(" (hash entries: "+lhash.size()+")");
}
private static void probeLHash(LinearHash lhash, String queryFile)
{
TextInputStream qfs = new TextInputStream(queryFile);
int hdepth, nfound=0, notfound=0;
float accesses=0;
long cputime = TMB.getCurrentThreadCpuTime();
while(qfs.ready()) {
String queryWord = qfs.readWord();
if ((hdepth=lhash.lookup(queryWord)) > 0) {
// Uncomment the next line to make it verbose.
//System.out.println("The word `"+queryWord+"' found: "+hdepth);
nfound++;
accesses += (float)hdepth / 2. + .5;
} else {
// Uncomment the next line to make it verbose.
//System.out.println("The word `"+queryWord+"' not found.");
notfound++;
accesses += (float)(-hdepth);
}
}
cputime = TMB.getCurrentThreadCpuTime() - cputime;
System.out.print("Words found: "+nfound);
System.out.print(" (searches failed: "+notfound+")\n");
System.out.println("Total number of Hash Table accesses: "+accesses);
System.out.println("CPU time for searching keys in the Hash Table: "
+(cputime/1000000)+" millisec");
}
}