-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashTable.java
More file actions
196 lines (159 loc) · 4.93 KB
/
HashTable.java
File metadata and controls
196 lines (159 loc) · 4.93 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
/**
* Simple HashTable class.
*
* @author Maricel Vicente
*
*/
public class HashTable<K, V> {
/**
* Item class is a simple wrapper for key/value pairs.
*/
class Item<K, V> { // leave this non-private for testing.
private K key;
private V value;
private boolean tombstone;
/**
* Create an Item object.
*/
public Item(K key, V value) {
this.key = key;
this.value = value;
this.tombstone = false;
}
}
public static final int INITIAL_CAPACITY = 16; // must be a power of 2.
public static final double MAX_LOAD = .5;
Item<K, V>[] table; // (Leave this non-private for testing.)
private int size;
/**
* HashTable constructor.
*/
@SuppressWarnings("unchecked")
public HashTable() {
table = (Item[]) new Item[INITIAL_CAPACITY];
size = 0;
}
/**
* Store the provided key/value pair.
*/
public void put(K key, V value) {
// rehash if load factor is under the maxload
if ((1 + size) / MAX_LOAD < (double) table.length) {
rehash();
}
int counter = findKey(key);
table[counter] = new Item<K,V>(key, value);
size++;
}
/**
* Return the value associated with the provided key, or null if no such value
* exists.
*/
public V get(K key) {
// UNFINISHED!
int counter = findKey(key);
// return null if invalid
if (counter == -1) {
return null;
}
else {
return table[counter].value;
}
}
/**
* Remove the provided key from the hash table and return the associated
* value. Returns null if the key is not stored in the table.
*/
public V remove(K key) {
// UNFINISHED!
int counter = findKey(key);
// if counter is invalid return null
if (counter == -1) {
return null;
}
// tombstone is true and decrement size
else {
table[counter].tombstone = true;
size--;
return table[counter].value;
}
}
/**
* Return the number of items stored in the table.
*/
public int size() {
return size;
}
// PRIVATE HELPER METHODS BELOW THIS POINT----------
/**
* Double the size of the hash table and rehash all existing items.
*/
private void rehash() {
// UNFINISHED!
}
/**
* Find the index of a key or return -1 if it can't be found. If removal is
* implemented, this will skip over tombstone positions during the search.
*/
private int findKey(K key) {
// UNFINISHED -- THIS METHOD SHOULD BE HELPFUL FOR BOTH GET AND REMOVE.
// counter is the index for hash code h
int counter = indexFor(key.hashCode(), table.length);
// increment counter if there is contents in table
while ((table[counter].tombstone)) {
counter++;
}
// if there is nothing in table, return null
if (table[counter] == null) {
return -1;
}
// return count
else {
return counter;
}
}
/**
* Returns index for hash code h.
*/
private int indexFor(int h, int length) {
return hash(h) & (length - 1);
}
/**
* Applies a supplemental hash function to a given hashCode, which defends
* against poor quality hash functions. This is critical because HashMap uses
* power-of-two length hash tables, that otherwise encounter collisions for
* hashCodes that do not differ in lower bits.
*/
private int hash(int h) {
// This function ensures that hashCodes that differ only by
// constant multiples at each bit position have a bounded
// number of collisions (approximately 8 at default load factor).
h ^= (h >>> 20) ^ (h >>> 12);
return h ^ (h >>> 7) ^ (h >>> 4);
}
}
// The hash and indexFor methods are taken directly from the Java HashMap
// implementation with some modifications. That code is licensed as follows:
/*
* Copyright 1997-2007 Sun Microsystems, Inc. All Rights Reserved. DO NOT ALTER
* OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License version 2 only, as published by
* the Free Software Foundation. Sun designates this particular file as subject
* to the "Classpath" exception as provided by Sun in the LICENSE file that
* accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
* A PARTICULAR PURPOSE. See the GNU General Public License version 2 for more
* details (a copy is included in the LICENSE file that accompanied this code).
*
* You should have received a copy of the GNU General Public License version 2
* along with this work; if not, write to the Free Software Foundation, Inc., 51
* Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, CA
* 95054 USA or visit www.sun.com if you need additional information or have any
* questions.
*/