-
Notifications
You must be signed in to change notification settings - Fork 3
/
RobinHood.java
154 lines (137 loc) · 3.29 KB
/
RobinHood.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
package rsn170330.sp07;
/**
* CS 5V81.001: Implementation of Data Structures and Algorithms
* Short Project SP07: Robin Hood Hashing Implementation
* @author Rahul Nalawade (rsn170330)
*
* Date: January 04, 2018
*/
public class RobinHood<T> {
Object[] table; // The hash table.
int maxDisp; // The largest displacement of any element.
int size; // The size of the table.
// Initializes new hash table using Robin Hood.
public RobinHood() {
table = new Object[1024];
maxDisp = 0;
size = 0;
}
/**
* Adds the specified element to this set if it is not already present.
*
* @param x the element to be added
* @return true if successful insertion, false otherwise
*/
public boolean add(T x) {
if (size >= table.length / 2) {
resize();
}
if (contains(x)) {
return false;
}
int loc = hash(x);
int d = 0;
while (true) {
if (table[loc] == null) {
table[loc] = x;
size++;
return true;
}
else if (displacement((T) table[loc], loc) >= d) {
d++;
loc = (loc + 1) % table.length;
maxDisp = Math.max(d, maxDisp);
}
else {
T temp = x;
x = (T) table[loc];
table[loc] = temp;
loc = (loc + 1) % table.length;
d = displacement(x, loc);
maxDisp = Math.max(d, maxDisp);
}
}
}
/**
* If x is there is the Collection.
* @param x the input element
* @return true if present, false otherwise
*/
public boolean contains(T x) {
int loc = hash(x);
for (int d = 0; d <= maxDisp; d++) {
int index = (loc + d) % table.length;
if (x.equals(table[index])) {
return true;
}
}
return false;
}
/**
* Removes the specified element from this set if it is present.
* @param x the element to be removed
* @return true, if successfully removed, false otherwise
*/
public boolean remove(T x) {
int loc = hash(x);
for (int d = 0; d <= maxDisp; d++) {
int index = (loc + d) % table.length;
if (x.equals(table[index])) {
table[index] = null;
size--;
return true;
}
}
return false;
}
// Returns the number of elements in the table.
public int size() {
return size;
}
// Resizes the table to double the size.
private void resize() {
Object[] oldTable = table;
table = new Object[table.length * 2];
size = 0;
maxDisp = 0;
for (Object x : oldTable) {
if (x != null) {
add((T) x);
}
}
}
/**
* Returns the displacement of element x at location loc.
*
* @param x the element to calculate displacement of
* @param loc the location of x in the table
* @return the displacement of element x at location loc
*/
private int displacement(T x, int loc) {
int h = hash(x);
return (loc >= h) ? (loc - h) : (table.length + loc - h);
}
/**
* Returns the hash of x.
*
* @param x the element to hash
* @return the hash of x
*/
private int hash(T x) {
int h = x.hashCode();
if (h < 0) {
return (x.hashCode() + 1) % table.length + table.length - 1;
}
return x.hashCode() % table.length;
}
/**
* 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<T> int distinctElements(T[] arr){
RobinHood<T> dist = new RobinHood<>();
for (T e : arr) { dist.add(e); }
return dist.size();
}
}