-
Notifications
You must be signed in to change notification settings - Fork 0
/
HashTable_OA.java
173 lines (156 loc) · 4.43 KB
/
HashTable_OA.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
package shake_n_bacon;
import providedCode.*;
/**
* @author Scott Kinder
* @UWNetID kinders
* @studentID 1235149
* @email [email protected]
*
* This is a Hash table that uses the idea of quadratic probing. It is
* meant to be used with text files, to make hash values for words, and
* store them in a index, which would contain both the name of the word,
* and how many times the word was used in the file.
*/
public class HashTable_OA extends DataCounter {
private static final int[] PRIMES = {7, 17, 37, 67, 137, 281, 571, 1151, 2309, 4621, 9257, 18517, 37039,
74093, 148193, 296437};
private int arraySize;
private Hasher hasher;
private Comparator<String> comparitor;
private HashEntry[] array;
private int totalSize;
/*
* Param c is a comparator used to compare strings
* Param h is a hash value creator to make hash values
* Constructor for a quadratic probing hash table. Initializes needed
* fields.
*/
public HashTable_OA(Comparator<String> c, Hasher h) {
arraySize = 0;
totalSize = -1;
hasher = h;
comparitor = c;
array = new HashEntry[PRIMES[arraySize]];
}
/*
* Param data is the word being passed in
* This is used to either make a new hash table entry with the given word,
* or if the word is already in the hash table, it will increase the count
* of the word.
*/
public void incCount(String data) {
int currentPos = findPos(data);
if (isActive(currentPos)) {
array[currentPos].count++;
} else {
array[currentPos] = new HashEntry(data, 1);
if (++totalSize >= PRIMES[arraySize] / 2) {
rehash();
}
}
}
/*
* This is used to get the total number of unique words in the hash table
*/
public int getSize() {
return totalSize;
}
/*
* Param data is the word being checked
* This is used to get the count of how many times the word was used.
*/
public int getCount(String data) {
int currentPos = findPos(data);
if (isActive(currentPos)) {
return array[currentPos].count;
} else {
return 0;
}
}
/*
* Creates a simple iterator which can be used to get all of the data
* from the hash table.
*/
public SimpleIterator getIterator() {
SimpleIterator itr = new SimpleIterator() {
private int currentIndex = 0;
private int totalCount = 0;
private HashEntry current = array[currentIndex];
/*
* Gets the next element in the hash table.
*/
public DataCount next() {
if (!hasNext()) {
throw new IllegalArgumentException();
}
while (current == null) {
current = array[++currentIndex];
}
totalCount++;
DataCount out = new HashEntry(current.data, current.count);
current = array[++currentIndex];
return out;
}
/*
* Checks to see if there is another element in hash table.
*/
public boolean hasNext() {
return totalCount < totalSize;
}
};
return itr;
}
/*
* This is a hash table entry which is used to store the data at the given
* index.
*/
private static class HashEntry extends DataCount {
/*
* Param data is the given word being passed in
* Param count is the number of times given word was used
* Constructor call for a hash table entry, initializes needed
* fields
*/
public HashEntry(String data, int count) {
super(data, count);
}
}
/*
* Param currentPos is the position in the hash table to check
* This is used to see if the given index has a entry that is valid
* for being examined.
*/
private boolean isActive(int currentPos) {
return array[currentPos] != null;
}
/*
* Param data is the given word being checked
* This is used to find where in the table the given word is.
*/
public int findPos(String data) {
int offset = 1;
int currentPos = hasher.hash(data) % PRIMES[arraySize];
while (array[currentPos] != null && comparitor.compare(array[currentPos].data, data) != 0) {
currentPos += offset;
offset += 2;
if (currentPos >= PRIMES[arraySize]) {
currentPos -= PRIMES[arraySize];
}
}
return currentPos;
}
/*
* This is used to rehash words to their new respective index, making a new hash
* table with the old entries, and approximately double the size.
*/
private void rehash() {
HashEntry[] oldLists = array;
array = new HashEntry[PRIMES[++arraySize]];
for (int i = 0; i < PRIMES[arraySize - 1]; i++) {
if (oldLists[i] != null) {
int currentPos = findPos(oldLists[i].data);
array[currentPos] = new HashEntry(oldLists[i].data, oldLists[i].count);
}
}
}
}