- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleHashSet.java
More file actions
Latest commit
218 lines (187 loc) · 6.11 KB
/
Copy pathSimpleHashSet.java
File metadata and controls
218 lines (187 loc) · 6.11 KB
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/**
* A superclass for implementations of hash-sets implementing the SimpleSet interface.
* @author avishavtal
*/
publicabstractclassSimpleHashSetimplementsSimpleSet {
/**
* Describes the higer load factor of a newly created hash set
*/
protectedstaticfinalfloatDEFAULT_HIGHER_CAPACITY = 0.75f;
/**
* Describes the lower load factor of a newly created hash set
*/
protectedstaticfinalfloatDEFAULT_LOWER_CAPACITY = 0.25f;
/**
* Describes the capacity of a newly created hash set
*/
protectedstaticfinalintINITIAL_CAPACITY = 16;
/*upper load factor of this set.*/
privatefloatupperLoadFactor;
/*lower load factor of this set.*/
privatefloatlowerLoadFactor;
// counter of the elements in the set.
privateintsize;
/**
* Constructs a new hash set with the default capacities given in DEFAULT_LOWER_CAPACITY
* and DEFAULT_HIGHER_CAPACITY
*/
protectedSimpleHashSet(){
upperLoadFactor = DEFAULT_HIGHER_CAPACITY;
lowerLoadFactor = DEFAULT_LOWER_CAPACITY;
}
/**
* Constructs a new hash set with capacity INITIAL_CAPACITY
* @param upperLoadFactor the upper load factor before rehashing
* @param lowerLoadFactor the lower load factor before rehashing
*/
protectedSimpleHashSet(floatupperLoadFactor, floatlowerLoadFactor){
this.upperLoadFactor = upperLoadFactor;
this.lowerLoadFactor = lowerLoadFactor;
}
/**
* Clamps hashing indices to fit within the current table capacity
* (see the exercise description for details)
* @param index the index before clamping
* @return an index properly clamped
*/
protectedintclamp(intindex){
returnclampHelper(index, null);
}
/* find the index of the string if given, else return the location to put ut there. */
privateintclampHelper(intindex, StringsearchVal){
inti = 0;
intnewIndex;
while (i < capacity()){
newIndex = (index+(i+i*i)/2)&(capacity()-1);
if (searchVal != null) {
if (isThere(newIndex, searchVal)) {
returnnewIndex;
} elseif (isFreeCell(newIndex)&&!isDelete(newIndex)){
return -1;
}
} elseif (isFreeCell(newIndex)){
returnnewIndex;
}
i++;
}
return -1;
}
/**
* check if was value in the cell with the given index.
*
* @param newIndex the index of the cell we want to check.
* @return true iff was value in the cell with the given index and we care about it.
*/
protectedbooleanisDelete(intnewIndex) {
returnfalse;
}
/**
* check if the given value in the cell with the given index.
* @param newIndex the index we want to check.
* @param searchVal the value we want to check.
* @return true if the given value in the cell with the given index, false otherwise.
*/
protectedabstractbooleanisThere(intnewIndex, StringsearchVal);
/**
* @return The lower load factor of the table.
*/
protectedfloatgetLowerLoadFactor(){
returnlowerLoadFactor;
}
/**
* @return The higher load factor of the table.
*/
protectedfloatgetUpperLoadFactor(){
returnupperLoadFactor;
}
/**
* @return The current capacity (number of cells) of the table.
*/
publicabstractintcapacity();
/**
* check if the cell with the given index can get more value.
* @param index the index of the cell we check.
* @return true if the cell with the given index can get more value, false otherwise.
*/
protectedabstractbooleanisFreeCell(intindex);
/**
* Look for a specified value in the set.
* @param newValue New value to add to the set
* @return False iff newValue already exists in the set
*/
@Override
publicbooleanadd(StringnewValue) {
if (contains(newValue)){
returnfalse;
}
if ((float)(size+1)/capacity() > upperLoadFactor){
reHash(false);
}
intindex = clamp(newValue.hashCode());
insert(newValue, index);
size++;
returntrue;
}
/**
* @param searchVal Value to search for.
* @return true if searchVal in the set, false otherwise.
*/
@Override
publicbooleancontains(StringsearchVal) {
intindex = searchVal.hashCode();
intnewIndex = clampHelper(index, searchVal);
returnnewIndex >= 0;
}
/**
* insert the given value to the set.
* @param newValue New value to add to the set.
* @param index the index of the new value in the table.
*/
protectedabstractvoidinsert(StringnewValue, intindex);
/**
* Remove the input element from the set.
* @param toDelete Value to delete.
* @return True iff toDelete is found and deleted.
*/
@Override
publicbooleandelete(StringtoDelete) {
intindex = toDelete.hashCode();
intnewIndex = clampHelper(index,toDelete);
if (newIndex < 0){
returnfalse;
}
remove(toDelete, newIndex);
size--;
if (((float)size/capacity()) < lowerLoadFactor){
reHash(true);
}
returntrue;
}
/**
* remove the given string from the table.
* @param toDelete value to delete.
* @param newIndex the index of toDelete in the table.
*/
protectedabstractvoidremove(StringtoDelete, intnewIndex);
/**
* @return The number of elements currently in the set
*/
@Override
publicintsize() {
returnsize;
}
/**
* rehash the set.
* @param toSmaller true if need smaller table, false otherwise.
*/
protectedabstractvoidreHash(booleantoSmaller);
/**
* add to the set all the strings in the given array.
* @param data array of strings to add.
*/
protectedvoidaddAll(String[] data){
for (Stringstring: data){
add(string);
}
}
}