-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclosedHashing.java
200 lines (167 loc) · 5.59 KB
/
closedHashing.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
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
package closedHashing;
//Using closed address hashing, compare between hash tables of prime and nonprime sizes (Group 2).
import java.util.*;
import java.lang.Iterable;
public class closedHashing {
private static final int TABLE_SIZE = 2053;
private static double LOAD_FACTOR = 2;
private static float comparison = 0;
private static int numOfTimes = 0;
private static int numOfData = 0;
public static class HTnode { // Node structure
public Integer key;
public Integer value;
}
private static LinkedList<HTnode>[] Ctable = new LinkedList[TABLE_SIZE];
// Creating a linked list of HTnode
public closedHashing() {
// initalize null values in hashtable
for (int i = 0; i < TABLE_SIZE; i++) {
Ctable[i] = null;
}
}
public static void addKey(Integer key, Integer value, Integer index) {
/* int index = key.hashCode() % TABLE_SIZE; */
LinkedList<HTnode> items = Ctable[index];
if (items == null) {
// if nothing at location create LL and add item in
items = new LinkedList<HTnode>();
HTnode node = new HTnode();
node.key = key;
node.value = value;
items.add(node);
Ctable[index] = items;
} else {
// add item into existing LL
HTnode node = new HTnode();
node.key = key;
node.value = value;
items.add(node);
}
numOfData++;
}
public static Integer search(Integer key, Integer index) {
boolean status = false;
LinkedList<HTnode> items = Ctable[index];
Integer key2 = key;
numOfTimes++;
for (HTnode i : items) {
if (i.key.equals(key)) {
status = true;
} else
comparison++;
}
if (status) {
for (HTnode item : items) {
if (item.key.equals(key2)) {
return item.value;
}
}
}
return -1;
}
public static float avgComparisons() {
float avgComparisons = 0;
if (numOfTimes == 0)
return -1;
avgComparisons = comparison / numOfTimes;
return avgComparisons;
}
public static void comparisons_all(HashMap<Integer,Integer> kvMap) {
for (Integer i : kvMap.keySet()) {
//System.out.println("Postal Code: " + i + " Population Size: " + kvMap.get(i));
search(i, IndexOf(i));
}
System.out.println("Number of Data: " + numOfTimes + "\nNumber of comparisons: " + comparison+"\nAvg Comparisons: "+ comparison/numOfTimes);
return;
}
public static double avgCPUTime(HashMap<Integer, Integer> HashMap) {
//As the JVM warms up the amount of time taken will vary
// The second time you run this will always be faster than the first
//(The first time it has to load classes and call static blocks)
//After you have run the method 10,000 times it will be faster again
long start=0;
int runs = 10000; // enough to run for 2-10 seconds.
for (int j = -10000; j < runs; j++) {
if (j == 0)
start = System.nanoTime(); //records time at start of loop
for (Integer i : HashMap.keySet()) {
search(i, IndexOf(i)); //searches through entire list of postal codes
}
}
long time = System.nanoTime() - start; //finds time taken to finish loop
return (time / (runs*numOfData)); //avg time taken to do ONE search
}
public static int IndexOf(int key) {
return (key % TABLE_SIZE);
}
public static void main(String[] args) {
// Generate a key-value pair
DataGen kvm = new DataGen(TABLE_SIZE, LOAD_FACTOR);
HashMap<Integer, Integer> kvMap = new HashMap<Integer, Integer>();
kvMap = kvm.getHash(); // SAVED into kvMap
for (Integer i : kvMap.keySet()) {
//System.out.println("Postal Code: " + i + " Population Size: " + kvMap.get(i));
addKey(i, kvMap.get(i), IndexOf(i));
}
System.out.println("Number of Data: " + numOfData);
// for (int i = 0; i < TABLE_SIZE; i++) {
// if (Ctable[i] != null) {
// System.out.println(((Ctable[i].element().getkey())));
// }
// }
// End of KEY Gen
int choice;
do {
Integer key;
Scanner sc = new Scanner(System.in);
System.out.println("-------- Example Class 2 Question 2 --------");
System.out.println("| 1. Search");
System.out.println("| 2. Add");
System.out.println("| 3. Display Average CPU time");
System.out.println("| 4. Display Average Key Comparison");
System.out.println("| 5. Load Factor");
System.out.println("| 6. Exit");
choice = sc.nextInt();
switch (choice) {
case 1:// Search
System.out.println("Please enter your key: ");
key = sc.nextInt();
if (search(key, IndexOf(key)) == -1)
System.out.println("Postal Code not found.");
else
System.out.println("The value is: " + search(key, IndexOf(key)));
break;
case 2:// Add
System.out.println("Enter the key: ");
key = sc.nextInt();
System.out.println("Enter the value: ");
Integer value = sc.nextInt();
addKey(key, value, IndexOf(key));
// value = sc.NextInt();
case 3:// Display Average CPU time
System.out.println("Average CPU time (ns) is: "+avgCPUTime(kvMap));
break;
case 4:// Display Average Key Comparison
// System.out.println("comparisons: " + comparison + " Number of Times: " + numOfTimes);
// if (avgComparisons() == -1)
// System.out.println("No Comparisons made yet.");
// else
// System.out.printf("Average Key Comparison: %.2f\n", avgComparisons());
comparisons_all(kvMap);
break;
case 5:// Load Factor
break;
case 6:// Exit
System.out.println("Thank you!!");
break;
}
} while (choice < 6);
/**
* Number of keys = tablesize * load factor loop call addkey
*/
/**
* collect timing collect comparision timings
*/
}
}