-
Notifications
You must be signed in to change notification settings - Fork 0
/
EventHashTable.java
119 lines (107 loc) · 3.77 KB
/
EventHashTable.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
import java.util.*;
public class EventHashTable {
public static int size = 0;
public static int maxSize, hashAlgorithm, index, min, max;
private int random = 0;
private static Event[] eventHashTable;
private static Event e1, e2, value;
private static Random rand = new Random();
private static boolean flag = true;
public EventHashTable(int maxSize) {
this.size = size;
this.maxSize = maxSize;
eventHashTable = new Event[maxSize];
}
public static void put(int key, Event value) {
hashAlgorithm = key % maxSize;
switch (hashAlgorithm) {
case 3:
index = 0;
while (eventHashTable[index] != null && index <= (maxSize/3) - 1) {
index = index + 1;
}
if (eventHashTable[index] == null) {
eventHashTable[index] = value;
break;
} else {
throw new ArrayStoreException("There is already something in this index!");
}
case 4:
index = (maxSize / 3);
while (eventHashTable[index] != null && index <= ((maxSize * 2) / 3) - 1) {
index = index + 1;
}
if (eventHashTable[index] == null) {
eventHashTable[index] = value;
break;
} else {
throw new ArrayStoreException("There is already something in this index!");
}
case 5:
index = ((maxSize * 2) / 3);
while (eventHashTable[index] != null) {
index = index + 1;
}
if (eventHashTable[index] == null) {
eventHashTable[index] = value;
break;
} else {
throw new ArrayStoreException("There is already something in this index!");
}
}
}
public boolean isEmpty() {
for (int i = 0; i < maxSize; i++) {
if (eventHashTable[i] != null) {
flag = false;
}
}
return flag;
}
public Event get(int key) {
switch (key) {
case 3:
min = 0;
max = (maxSize / 3) - 1;
random = rand.nextInt((max + 1 - min) + min);
if (eventHashTable[random] != null) {
value = eventHashTable[random];
} else {
get(key);
}
break;
case 4:
min = (maxSize / 3); //4
max = ((maxSize * 2) / 3) - 1; //7
random = rand.nextInt(max + 1 - min) + min;
if (eventHashTable[random] != null) {
value = eventHashTable[random];
} else {
get(key);
}
break;
case 5:
min = ((maxSize * 2) / 3);
max = (maxSize - 1);
random = rand.nextInt(max + 1 - min) + min;
if (eventHashTable[random] != null) {
value = eventHashTable[random];
} else {
get(key);
}
break;
}
return value;
}
public void printHashTable() {
for (int i = 0; i < maxSize; i++) {
if (eventHashTable[i] == null) {
System.out.println("null");
}
else {
System.out.print(eventHashTable[i].toString() + "\n");
}
}
System.out.println();
}
}