-
Notifications
You must be signed in to change notification settings - Fork 3
/
HashingDriver2.java
142 lines (126 loc) · 3.41 KB
/
HashingDriver2.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
package rsn170330.sp07;
import java.util.Set;
import java.util.HashSet;
import java.util.Random;
/**
* 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 HashingDriver2 {
public static Random random = new Random();
public static int numTrials = 10;
/**
* Calculate distinct elements in an array
* @param arr: Array of Integers which may or may not have duplicates.
* @return: returns the count of distinct elements in the provided array.
*/
public static int distinctElements(Integer[] arr){
Set<Integer> hs = new HashSet<>();
for (Integer e : arr) { hs.add(e); }
return hs.size();
}
public static void main(String[] args) {
int Million = 1000000;
int n = Million;
int choice = 0;
long result = 0;
if (args.length > 0) {
n = Integer.parseInt(args[0]);
}
if (args.length > 1) {
choice = Integer.parseInt(args[1]);
}
Integer[] arr = new Integer[n];
for (int i = 0; i < n; i++) {
arr[i] = random.nextInt(n);
}
Timer timer = new Timer();
switch (choice) {
case 1:
//numTrials = 1; // Uncomment to Test
for (int i = 0; i < numTrials; i++) {
Shuffle.shuffle(arr);
result = DoubleHashing.distinctElements(arr);
}
//System.out.println("Double Hashing: " + result);
break;
case 2:
//numTrials = 1; // Uncomment to Test
for (int i = 0; i < numTrials; i++) {
Shuffle.shuffle(arr);
result = RobinHood.distinctElements(arr);
}
//System.out.println("Robin Hood Hashing: " + result);
break;
case 3:
//numTrials = 1; // Uncomment to Test
for (int i = 0; i < numTrials; i++) {
Shuffle.shuffle(arr);
result = Cuckoo.distinctElements(arr);
}
//System.out.println("Cuckoo Hashing: " + result);
break;
default:
//numTrials = 1;
for (int i = 0; i < numTrials; i++) {
Shuffle.shuffle(arr);
result = distinctElements(arr);
}
//System.out.println("Java HashSet: " + result);
break;
}
timer.end();
timer.scale(numTrials);
System.out.println("Choice: " + choice + "\n" + timer);
}
/**
* Shuffle the elements of an array arr[from..to] randomly
* @author rbk : based on algorithm described in CLRS book
*/
public static class Shuffle {
public static void shuffle(int[] arr) {
shuffle(arr, 0, arr.length - 1);
}
public static <T> void shuffle(T[] arr) {
shuffle(arr, 0, arr.length - 1);
}
public static void shuffle(int[] arr, int from, int to) {
int n = to - from + 1;
for (int i = 1; i < n; i++) {
int j = random.nextInt(i);
swap(arr, i + from, j + from);
}
}
public static <T> void shuffle(T[] arr, int from, int to) {
int n = to - from + 1;
Random random = new Random();
for (int i = 1; i < n; i++) {
int j = random.nextInt(i);
swap(arr, i + from, j + from);
}
}
static void swap(int[] arr, int x, int y) {
int tmp = arr[x];
arr[x] = arr[y];
arr[y] = tmp;
}
static <T> void swap(T[] arr, int x, int y) {
T tmp = arr[x];
arr[x] = arr[y];
arr[y] = tmp;
}
public static <T> void printArray(T[] arr, String message) {
printArray(arr, 0, arr.length - 1, message);
}
public static <T> void printArray(T[] arr, int from, int to, String message) {
System.out.print(message);
for (int i = from; i <= to; i++) {
System.out.print(" " + arr[i]);
}
System.out.println();
}
}
}