-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathHashingDriver1.java
131 lines (116 loc) · 3.32 KB
/
HashingDriver1.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
package rsn170330.sp07;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Set;
import java.util.HashSet;
import java.util.Scanner;
/**
* CS 5V81.001: Implementation of Data Structures and Algorithms
* Short Project SP07: Comparison of Hashing Implementations
* @author Rahul Nalawade
*
* Date: January 4, 2019
*/
public class HashingDriver1 {
static DoubleHashing<Integer> dh = new DoubleHashing<>();
static RobinHood<Integer> rh = new RobinHood<>();
static Cuckoo<Integer> ch = new Cuckoo<>();
static Set<Integer> hs = new HashSet<>();
public static void main(String[] args) throws FileNotFoundException {
Scanner sc;
File file = new File(args[0]);
sc = new Scanner(file);
String operation;
int operand;
long modValue = 1000000009;
long result = 0;
boolean succeed = false;
int choice = 0;
boolean verbose = false;
if (args.length > 1) {
choice = Integer.parseInt(args[1]);
}
if (args.length > 2) {
verbose = Boolean.parseBoolean(args[2]);
System.out.format("%-10.10s%-10.10s%.10s%1.1s", "Operation ", "Operand ", "Result", "\n");
}
Timer timer = new Timer();
while (!((operation = sc.next()).equals("End"))) {
switch (operation) {
case "Add": {
operand = sc.nextInt();
if (choice == 1) {
succeed = dh.add(operand);
} else if (choice == 2) {
succeed = rh.add(operand);
} else if (choice == 3) {
succeed = ch.add(operand);
} else {
succeed = hs.add(operand);
}
if(succeed) {
result = (result + 1) % modValue;
}
if (verbose) {
System.out.format("%-10.10s%-10.10s%.10s%1.1s", "Add ", " "+operand, " "+result, "\n");
}
break;
}
case "Remove": {
operand = sc.nextInt();
if (choice == 1) {
succeed = dh.remove(operand);
} else if (choice == 2) {
succeed = rh.remove(operand);
} else if (choice == 3) {
succeed = ch.remove(operand);
} else {
succeed = hs.remove(operand);
}
if(succeed) {
result = (result + 1) % modValue;
}
if (verbose) {
System.out.format("%-10.10s%-10.10s%.10s%1.1s", "Remove ", " "+operand, " "+result, "\n");
}
break;
}
case "Contains":{
operand = sc.nextInt();
if (choice == 1) {
succeed = dh.contains(operand);
} else if (choice == 2) {
succeed = rh.contains(operand);
} else if (choice == 3) {
succeed = ch.contains(operand);
} else {
succeed = hs.contains(operand);
}
if(succeed) {
result = (result + 1) % modValue;
}
if (verbose) {
System.out.format("%-10.10s%-10.10s%.10s%1.1s", "Contains ", " "+operand, " "+result, "\n");
}
break;
}
default:
break;
}
}
if (choice == 1) {
System.out.println("Double Hashing result: " + result);
System.out.println("Double Hashing size: " + dh.size());
} else if (choice == 2) {
System.out.println("Robin Hood result: " + result);
System.out.println("Robin Hood size: " + rh.size());
} else if (choice == 3) {
System.out.println("Cuckoo Hashing result: " + result);
System.out.println("Cuckoo Hashing size: " + ch.size());
} else {
System.out.println("HashSet result: " + result);
System.out.println("HashSet size: " + hs.size());
}
System.out.println(timer.end());
}
}