-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhashedDictionary.java
More file actions
257 lines (207 loc) · 7.95 KB
/
hashedDictionary.java
File metadata and controls
257 lines (207 loc) · 7.95 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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
public class hashedDictionary {
static int index = 255; // Can set to larger values (but don't go too large - limit ~ 214748360)
static String[] dictKeys = new String[index];
static int[][] dictValues = new int[index][];
/**
* Hash calculation function, based on ascii sum MOD index
* @param key - string of chars
* @return - integer hash value
*/
public static int hash(String key){
int pos = 0;
for (char chr: key.toCharArray()){
pos += chr;
}
pos = pos % index;
return pos;
}
/**
* Adds key, value pair
* @param key - string key for dict. key
* @param value - int value for dict. data
*/
public static String insert(String key, int value){
int pos = hash(key);
int[] temp_value = {value};
if (dictKeys[pos] == null){
dictKeys[pos] = key;
dictValues[pos] = temp_value;
return "Success";
}
else if (dictKeys[pos] == key){
return "Duplicate Key";
}
else{
while (dictKeys[pos] != null){
pos ++;
pos = pos % index;
}
dictKeys[pos] = key;
dictValues[pos] = temp_value;
return ("Collision Error, Handled Hash Location: " + pos);
}
}
/**
* Adds key, value pair
* @param key - string key for dict. key
* @param value - array of int values for dict. data
*/
public static String insert(String key, int[] value){
int pos = hash(key);
if (dictKeys[pos] == null){
dictKeys[pos] = key;
dictValues[pos] = value;
return "Success";
}
else if (dictKeys[pos] == key){
return "Duplicate Key";
}
else{
while (dictKeys[pos] != null){
pos ++;
pos = pos % index;
}
dictKeys[pos] = key;
dictValues[pos] = value;
return ("Collision Error, Handled Hash Location: " + pos);
}
}
/**
* Deletes a dict. entry based of it's key
* @param key
*/
public static String delete(String key){
int pos = hash(key);
int temp = pos;
if ((dictValues[pos] != null) && (dictKeys[pos] == key)){
dictKeys[pos] = null;
dictValues[pos] = null;
return "Delete - Success";
}
else if (dictKeys[pos] != key){
while ((dictKeys[pos] != key)) {
pos ++;
pos = pos % index;
if (temp == pos){
break;
}
}
dictKeys[pos] = null;
dictValues[pos] = null;
return ("Delete - Collision Success, Handled Hash Location: " + pos);
}
else{
return "Delete - Failed";
}
}
/**
* Returns string representation of dictionary
* data corresponding to a passed key value
* @param key - string key for dict. key
* @return - string representation of data
*/
public static String search(String key){
int pos = hash(key);
int temp = pos;
if ((dictValues[pos] != null) && (dictKeys[pos] == key)){
return java.util.Arrays.toString(dictValues[pos]);
}
else if (dictKeys[pos] != key){
while ((dictKeys[pos] != key)) {
pos ++;
pos = pos % index;
if (temp == pos){
return null;
}
}
return java.util.Arrays.toString(dictValues[pos]);
}
else{
return null;
}
}
/**
* Test data for hashing function
* @param args
*/
public static void main(String[] args) {
int[] data = {1,2,3};
int[] data_2 = {5, 6, 7};
int[] data_3 = {5, 6, 7, 8};
// Collision Test Data Insertion
System.out.println("Edge Collision Test Data Insertion:");
System.out.println(insert("zAC", data));
System.out.println(insert("zACzAD", data_2));
System.out.println(insert("zACzADzAD", data_3));
System.out.println("\nCollision Test Data Insertion:");
System.out.println(insert("one", data));
System.out.println(insert("onezAD", data_2));
System.out.println(insert("onezADzAD", data_3));
// Standard Test Data Insertion
System.out.println("\nStandard Data Insertion:");
System.out.println(insert("two", data_2));
System.out.println(insert("four", data_3));
System.out.println(insert("five", 5));
System.out.println(insert("three", 3));
//----------------------------------------------//
// Test Data Deletion
System.out.println("\n//----------------------------------------------//");
System.out.println("\nData Deletion:");
//Standard
System.out.println(delete("three"));
//Collision
System.out.println(delete("one"));
System.out.println(delete("onezAD"));
//Edge
System.out.println(delete("zAC"));
System.out.println(delete("zACzAD"));
//----------------------------------------------//
// Deleted Viewer
System.out.println("\n//----------------------------------------------//");
System.out.println("\nDeleted Viewer:");
System.out.println(search("zAC")); // Expected null as deleted
System.out.println(search("zACzAD")); // Expected null as deleted
System.out.println(search("zACzADzAD")); // Expected value as present
System.out.println();
System.out.println(search("one")); // Expected null as deleted
System.out.println(search("onezAD")); // Expected null as deleted
System.out.println(search("onezADzAD")); // Expected value as present
System.out.println();
System.out.println(search("three")); // Expected null as deleted
System.out.println(search("four")); // Expected value as present
//----------------------------------------------//
// Re-insert deleted data
System.out.println("\n//----------------------------------------------//");
System.out.println("\nRe-insert Deleted Data:");
System.out.println(insert("one", data));
System.out.println(insert("onezAD", data_2));
System.out.println(insert("zAC", data));
System.out.println(insert("zACzAD", data_2));
System.out.println(insert("three", 3));
//----------------------------------------------//
// Collision Tests
System.out.println("\n//----------------------------------------------//");
System.out.println("\nEdge Collision Tests:");
System.out.println(search("zAC"));
System.out.println(search("zACzAD"));
System.out.println(search("zACzADzAD"));
System.out.println("\nCollision Tests:");
System.out.println(search("one"));
System.out.println(search("onezAD"));
System.out.println(search("onezADzAD"));
// Standard Tests
System.out.println("\nStandard Tests:");
System.out.println(search("four"));
System.out.println(search("three"));
//----------------------------------------------//
//All data viewer
//System.out.println("\n//----------------------------------------------//");
// int c = 0;
// for (int[] i : dictValues) {
// System.out.print(dictKeys[c] + ' ');
// System.out.print(Integer.toString(c) + ' ');
// System.out.println(java.util.Arrays.toString(i));
// c++;
// }
}
}