-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashTableChained.java
More file actions
214 lines (163 loc) · 3.97 KB
/
HashTableChained.java
File metadata and controls
214 lines (163 loc) · 3.97 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
import java.util.Random;
import java.io.Serializable;
public class HashTableChained implements Serializable{
protected int buckets;
protected int entries;
protected int alpha;
protected int beta;
protected PList[] dict;
protected final static int PRIME = 9999991;
private static final long serialVersionUID = 3L;
public HashTableChained(int sizeEstimate) {
Random random = new Random();
buckets = nextPrime( (int) (sizeEstimate*1.5) );
alpha = random.nextInt(PRIME-2)+2;
beta = random.nextInt(PRIME);
entries = 0;
dict = new PList[buckets];
}
public HashTableChained() {
this(101);
}
int compFunction(int code) {
return ( ( Math.abs( (alpha*code) + beta ) % PRIME ) % buckets);
}
public int size() {
return entries;
}
public boolean isEmpty() {
return entries==0;
}
public PList insert(Object key, Object value) {
//value is a path to the directory
//key is the name of the directory
int index = compFunction(key.hashCode());
PList chainedList=null;
PList subList=null;
boolean makeNewSublist = true;
if(dict[index]==null){
//add new PList
dict[index] = new PList();
}
//check to see if sublist with the same key exists.
//if it exists, just add the value to that typelist
chainedList=dict[index];
PListNode reference = chainedList.front();
while(reference.isValidNode()){
subList = (PList) reference.item();
reference = reference.next();
if(key.equals(subList.queryType())){
subList.insert(value);
makeNewSublist = false;
break;
}
}
//if the typelist does not exist,
// make a new sublist and add it to the chainedList
if(makeNewSublist){
subList = new PList(key);
subList.insert(value);
chainedList.insert(subList);
entries++;
}
return subList;
}
public PList find(Object key) {
int index = compFunction(key.hashCode());
PList entry = dict[index];
if (entry==null){
return null;
}
else{
try{
PListNode cur = entry.front();
while (cur.isValidNode()){
PList subList = (PList) cur.item();
if (subList.queryType().equals(key) ){
return subList;
}
cur = cur.next();
}
}
catch (Exception e){
System.err.println(e);
}
return null;
}
}
public PList remove(Object key) {
int index = compFunction(key.hashCode());
PList entry = dict[index];
if (entry==null){
return null;
}
else{
try{
PListNode cur = entry.front();
while (cur.isValidNode()){
PList subList = (PList) cur.item();
if ( subList.queryType().equals(key) ){
entry.remove(cur);
entries--;
return subList;
}
cur = cur.next();
}
}
catch (Exception e){
System.out.println("happens in remove");
System.err.println(e);
}
return null;
}
}
public void makeEmpty() {
dict = new PList[buckets];
entries=0;
}
//nextPrime() returns the first prime number greater than or equal to n
protected int nextPrime(int n){
for (int i = n;;i++){
if ( isPrime(i) ){
return i;
}
}
}
//isPrime() returns true if n is prime, false if otherwise
protected boolean isPrime(int n){
if ( n < 2){
return false;
}
else if (n < 4){
return true;
}
else{
for (int i = 2; i < Math.pow(n,.5)+1; i++){
if ( n % i ==0 ){
return false;
}
}
return true;
}
}
public String collisions(){
String s = "Collision Map \n==============\n";
int col=0;
int len;
for (int i=0; i < dict.length; i++){
if ( ! (dict[i]==null)){
len = dict[i].length();
s+= "Bucket " + i + " contains " + len + " entries \n";
if (len >1){
col += len-1;
}
}
}
s+= "Total collisions: " + Integer.toString(col);
s+= "\nExpected collisions: " + Double.toString(expectedCollisions(entries));
return s;
}
public double expectedCollisions(int n){
return n - buckets + buckets*Math.pow((1- 1.0/buckets),n);
}
}