-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashes.java
More file actions
321 lines (264 loc) · 11.4 KB
/
Copy pathHashes.java
File metadata and controls
321 lines (264 loc) · 11.4 KB
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
import java.util.Stack;
public class Hashes {
public String hashtable[], inputStrings[];
public int tablesize,
max,
wordCumulativeFrequency[],
letterFrequency[],
letterValues[];
public char letters[];
public Hashes(Stack<String> stack) {
tablesize = stack.size();
max = tablesize / 2;
hashtable = new String[tablesize];
addLetters(stack);
calculateLetterFrequency(stack);
this.inputStrings = new String[tablesize];
stack.toArray(inputStrings);
sortWordsByCumulativeFrequency();
//System.out.println(inputStrings);
minimalPerfectHash();
}
/**
* MinimalPerfectHash is a method that takes a string as input and outputs the hash of that string
*/
public void minimalPerfectHash() {
Stack<String> inputStack = new Stack<String>();
// Pushes the input strings onto the inputStack.
for (int i = 0; i < inputStrings.length; i++) {
inputStack.push(inputStrings[i]);
}
long startTime = System.nanoTime(); // or System.nanoTime()
boolean hashSuccess = cichellisAlgorithm(inputStack);
long endTime = System.nanoTime(); // or System.nanoTime()
long executionTime = endTime - startTime;
System.out.println("Execution time: " + executionTime + " nanoseconds");
// Prints the hash of the hash table.
if(hashSuccess) {
System.out.println("Success!");
// Print all the hashtable in the current thread.
for (int i = 0; i < hashtable.length; i++) {
System.out.println(hashtable[i]);
}
}
else {
System.out.println("Failure!");
}
}
/**
* Performs Cichellis algorithm on input stack. This method is called by #run ( Stack ) to perform the algorithm.
*
* @param inputStack - Stack of strings to be operated on
*
* @return true if the algorithm completed successfully false if it was not able to perform the algorithm ( in which case the stack is modified
*/
public boolean cichellisAlgorithm(Stack<String> inputStack) {
// Check if the stack is empty.
if(inputStack.size() == 0) {
return true;
}
String currentString = inputStack.pop();
// Returns true if the algorithm is available for the current string.
for(int firstValue = 0; firstValue < max; firstValue++) {
// Returns true if the algorithm is available for the current string.
for (int lastValue = 0; lastValue < max; lastValue++) {
int hash = computeHash(currentString.length(), firstValue, lastValue);
// Checks if the hash is available and if it is available adds the string to the input stack.
if(ishashIsAvailable(hash)) {
hashtable[hash] = currentString;
boolean success = cichellisAlgorithm(inputStack);
// Returns true if success is true.
if(success)
return success;
hashtable[hash] = null;
}
else {
inputStack.push(currentString);
return false;
}
}
}
inputStack.push(currentString);
return false;
}
/**
* Computes hash value for given parameters. This is used to determine which table is used for a given set of data.
*
* @param length - Length of data to hash. Must be greater than zero.
* @param firstValue - First value of the data range. Must be greater than zero.
* @param lastValue - Last value of the data range. Must be greater than zero.
*
* @return Hash value for given parameters as specified by #hashCode () and #tablesize ( int ) respectively
*/
public int computeHash(int length, int firstValue, int lastValue) {
return (length + firstValue + lastValue) % tablesize;
}
/**
* Returns true if the hash is available. This is used to determine if we can get a reference to an object that has been added to the hashtable
*
* @param hash - the hash to check for
*
* @return true if the hash is available false if it is not available or if the object does not have an
*/
public boolean ishashIsAvailable(int hash) {
return hashtable[hash] == null;
}
/**
* Sorts the words by cumulative frequency in ascending order. This is done by finding the sum of the first and last letters in each word
*/
public void sortWordsByCumulativeFrequency() {
wordCumulativeFrequency = new int[tablesize];
// This method calculates the cumulative frequency of the word cumulative frequency of each word in the inputStrings array.
for (int i = 0; i < wordCumulativeFrequency.length; i++) {
String currentString = inputStrings[i];
int indexOfFirstLetter = findLetter(currentString.charAt(0));
int indexOfLastLetter = findLetter(currentString.charAt(currentString.length() - 1));
wordCumulativeFrequency[i] = letterFrequency[indexOfFirstLetter] + letterFrequency[indexOfLastLetter];
}
// This method is used to calculate the cumulative frequency of the input strings.
for (int i = 0; i < wordCumulativeFrequency.length-1; i++) {
// This method is used to compute the cumulative frequency of the word cumulative frequency.
for (int j = i+1; j < wordCumulativeFrequency.length; j++) {
// This method is used to compute the cumulative frequency of the word cumulative frequency.
if(wordCumulativeFrequency[i] < wordCumulativeFrequency[j]) {
int temp = wordCumulativeFrequency[i];
wordCumulativeFrequency[i] = wordCumulativeFrequency[j];
wordCumulativeFrequency[j] = temp;
String tempString = inputStrings[i];
inputStrings[i] = inputStrings[j];
inputStrings[j] = tempString;
}
}
}
}
/**
* Calculates the letter frequency of each letter in the input string. It does this by iterating over the stack in order to find the first and last letter in each string
*
* @param inputStrings - Stack of strings to
*/
public void calculateLetterFrequency(Stack<String> inputStrings) {
Stack<String> temp = (Stack<String>) inputStrings.clone();
letterFrequency = new int[letters.length];
// This method is used to find the frequency of the first letter of the string.
while(temp.size() > 0) {
String currentString = temp.pop();
int indexOfFirstLetter = findLetter(currentString.charAt(0));
int indexOfLastLetter = findLetter(currentString.charAt(currentString.length() - 1));
letterFrequency[indexOfFirstLetter]++;
letterFrequency[indexOfLastLetter]++;
}
}
/**
* Adds letters to the array that are not already in the array. This is done to avoid duplicates in the table
*
* @param inputStrings - the stack of strings
*/
public void addLetters(Stack<String> inputStrings) {
letters = new char[tablesize*2];
int numLetters = 0;
// Sets letters to the first letter in the inputStrings array.
for (int i = 0; i < tablesize; i++) {
char firstletter = inputStrings.get(i).charAt(0);
// Adds the first letter to the letters array.
if (!isLetterInArray(firstletter)) {
letters[numLetters] = firstletter;
numLetters++;
}
}
// Sets letters to the last letter in the inputStrings array.
for (int i = 0; i < tablesize; i++) {
String currentString = inputStrings.get(i).toUpperCase();
char lastletter = currentString.charAt(currentString.length() - 1);
// Adds the last letter to the letters array.
if (!isLetterInArray(lastletter)) {
letters[numLetters] = lastletter;
numLetters++;
}
}
char[] newLetters = new char[numLetters];
// Sets the letters of the current set of letters.
for (int i = 0; i < numLetters; i++) {
newLetters[i] = letters[i];
}
letters = newLetters;
}
/**
* Checks if the letter is in the array. This is used to determine if a letter is part of the word
*
* @param letter - The letter to check for
*
* @return true if the letter is in the array false if it is not and null if the letter is not
*/
public boolean isLetterInArray(char letter) {
// Check if the letter is a letter
if(findLetter(letter) == -1) {
return false;
}
else return true;
}
/**
* Finds the index of the letter in the letters array. This is case insensitive. Returns - 1 if the letter is not found
*
* @param letter - the letter to look for
*
* @return the index of the letter in the letters array or - 1 if the letter is not found in the
*/
public int findLetter(char letter) {
// Returns the index of the first letter in the letters array.
for (int i = 0; i < letters.length; i++) {
// Returns the index of the letter in the letter array.
if (letters[i] == Character.toUpperCase(letter)) {
return i;
}
}
return -1;
}
/**
* Extracts a key from the hashtable. This is useful for debugging and to ensure that the key is removed from the hashtable after it has been extracted
*
* @param key - the key to look for
*
* @return true if the key was found false if it wasn't and null is set to the hash table
*/
public boolean extractKey(String key) {
boolean found = false;
// Find the key in the hash table.
for(int i = 0; i < max; i++) {
// Searches for the key in the hash table.
for (int j = 0; j < max; j++) {
int index = computeHash(key.length(), i, j);
// Check if the key is in the hashtable.
if(hashtable[index].equalsIgnoreCase(key)) {
found = true;
//hashtable[index] = null;
}
}
}
return found;
}
/**
* Main method for testing. Will take a list of names and print out the key that is found or not.
*
* @param args - String [] of arguments to be passed to the
*/
public static void main(String[] args) {
Stack<String> names = new Stack<String>();
names.push("John");
names.push("Mary Jane");
names.push("Sue");
names.push("Joe soap");
names.push("Bob the builder");
names.push("Bill Burr");
names.push("Janice");
names.push("Fred");
names.push("Marcus");
Hashes perfectHash = new Hashes(names);
// This method is used to extract the Jon key from the perfect hash.
if(perfectHash.extractKey("Jon")) {
System.out.println("Found!");
}
else {
System.out.println("Not found!");
}
}
}