-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExtendedHashSet.java
176 lines (154 loc) · 3.68 KB
/
ExtendedHashSet.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
import java.util.Collection;
import java.util.HashSet;
public class ExtendedHashSet<T> extends HashSet<T>
{
private static final long serialVersionUID = 1L;
private long productA = 1;
private long productB = 1;
private long productC = 1;
public ExtendedHashSet()
{
super();
}
public ExtendedHashSet(Collection<? extends T> collection)
{
super(collection.size());
this.addAll(collection);
}
public ExtendedHashSet(int initialCapacity, float loadFactor)
{
super(initialCapacity, loadFactor);
}
public ExtendedHashSet(int initialCapacity)
{
super(initialCapacity);
}
public boolean add(T element)
{
if(super.add(element) == true)
{
if(this.productA > 0)
{
int hashCode = element.hashCode();
int a = (hashCode & 7) + 1;
int b = ((hashCode >> 3) & 7) + 1;
int c = ((hashCode >> 6) & 7) + 1;
this.productA *= a;
this.productB *= b;
this.productC *= c;
//check if overflow happened
if(this.productA < 0 || this.productB < 0 || this.productC < 0)
{
this.productA = 0;
this.productB = 0;
this.productC = 0;
}
}
return true;
}
return false;
}
public void clear()
{
this.productA = 1;
this.productB = 1;
super.clear();
}
public Object clone()
{
ExtendedHashSet<T> newSet = new ExtendedHashSet<T>();
for(T item : this)
{
newSet.add(item);
}
return newSet;
}
public boolean remove(Object obj)
{
if(super.remove(obj) == true)
{
if(this.productA > 0)
{
int hashCode = obj.hashCode();
int a = (hashCode & 7) + 1;
int b = ((hashCode >> 3) & 7) + 1;
int c = ((hashCode >> 6) & 7) + 1;
this.productA /= a;
this.productB /= b;
this.productC /= c;
}
return true;
}
return false;
}
public boolean removeAll(Collection<?> collection)
{
boolean removed = true;
for(Object item : collection)
{
removed = removed && remove(item);
}
return removed;
}
public boolean equals(Object obj)
{
if(obj == this)
{
return true;
}
if(obj instanceof ExtendedHashSet<?> == true)
{
ExtendedHashSet<?> set = (ExtendedHashSet<?>)obj;
if(this.productA == set.productA && this.productB == set.productB && this.productC == set.productC)
{
return super.equals(obj);
}
}
return false;
}
public boolean addAll(Collection<? extends T> collection)
{
boolean added = true;
for(T item : collection)
{
added = added && add(item);
}
return added;
}
public boolean retainAll(Collection<?> collection)
{
return super.retainAll(collection);
}
//check if "this" is a subset of "set"
public boolean isSubsetOf(ExtendedHashSet<T> set)
{
//if "this" has a greater size than "set" or "this" overflows and "set" does not, can't be a subset
if(this.size() > set.size() || (this.productA == 0 && set.productA != 0))
{
return false;
}
//if "set" overflows, have to do a full check
//check if "this" products divide "set" products, if they do, we need to do a full check
if(set.productA == 0 || (set.productA % this.productA == 0 && set.productB % this.productB == 0 && set.productC % this.productC == 0))
{
return set.containsAll(this);
}
return false;
}
public boolean isSupersetOf(ExtendedHashSet<T> set)
{
return set.isSubsetOf(this);
}
public boolean isProperSubsetOf(ExtendedHashSet<T> set)
{
if(this.size() == set.size())
{
return false;
}
return this.isSubsetOf(set);
}
public boolean isProperSupersetOf(ExtendedHashSet<T> set)
{
return set.isProperSubsetOf(this);
}
}