5 |
irasnyd |
1 |
//import chap09.list03.Map;
|
|
|
2 |
|
|
|
3 |
import java.io.*;
|
|
|
4 |
import java.util.*;
|
|
|
5 |
|
|
|
6 |
public class HashTable implements Map { // open addressing
|
7 |
irasnyd |
7 |
private Entry[] entries;
|
|
|
8 |
|
|
|
9 |
//size is the total number of elements, including NIL
|
|
|
10 |
//used is the total number of elements, NOT INCLUDING NIL
|
5 |
irasnyd |
11 |
private int size, used;
|
|
|
12 |
private float loadFactor;
|
|
|
13 |
private final Entry NIL = new Entry(null,null); // dummy
|
7 |
irasnyd |
14 |
private int collisions; //initialized to 0 by default
|
5 |
irasnyd |
15 |
|
7 |
irasnyd |
16 |
//create a HashTable with a certain capacity (maximum items)
|
|
|
17 |
//and a maximum load factor (if we exceed this we will rehash)
|
5 |
irasnyd |
18 |
public HashTable(int capacity, float loadFactor) {
|
|
|
19 |
entries = new Entry[capacity];
|
|
|
20 |
this.loadFactor = loadFactor; }
|
|
|
21 |
|
7 |
irasnyd |
22 |
//create a HashTable with a certain capacity (maximum items)
|
|
|
23 |
//and a default load factor of 0.75
|
5 |
irasnyd |
24 |
public HashTable(int capacity) {
|
7 |
irasnyd |
25 |
this(capacity,0.75F);} //calls the HashTable( int, float ) constructor
|
5 |
irasnyd |
26 |
|
7 |
irasnyd |
27 |
//create a default HashTable with default capacity (101 items) and
|
|
|
28 |
//default load factor of 0.75
|
5 |
irasnyd |
29 |
public HashTable() {
|
7 |
irasnyd |
30 |
this(101); } //calls the HashTable( int ) constructor
|
5 |
irasnyd |
31 |
|
7 |
irasnyd |
32 |
//Method to find a certain key in the HashTable
|
|
|
33 |
//Precondition: key != null
|
|
|
34 |
//Postcondition: return the entry's value if the key is found
|
|
|
35 |
// otherwise return null for not found
|
5 |
irasnyd |
36 |
public Object get(Object key) {
|
|
|
37 |
int h=hash(key);
|
|
|
38 |
for (int i=0; i<entries.length; i++) {
|
|
|
39 |
int j = nextProbe(h,i);
|
|
|
40 |
Entry entry=entries[j];
|
|
|
41 |
if (entry==null) break;
|
|
|
42 |
if (entry==NIL) continue;
|
|
|
43 |
if (entry.key.equals(key)) return entry.value; // success
|
|
|
44 |
}
|
|
|
45 |
return null; // failure: key not found
|
|
|
46 |
}
|
6 |
irasnyd |
47 |
|
7 |
irasnyd |
48 |
//Method to add a key/value pair to the HashTable
|
|
|
49 |
//Key - the value that gets hash() called on it. Should be unique (hopefully)
|
|
|
50 |
//Value - the actual data that is to be stored.
|
|
|
51 |
//Precondition: key != null
|
|
|
52 |
//Postcondition: return null if the object was inserted sucessfully
|
|
|
53 |
// return the value that was there if we updated a value
|
|
|
54 |
// return null if the table overflows (should never happen,
|
|
|
55 |
// but it is there just in case)
|
5 |
irasnyd |
56 |
public Object put(Object key, Object value) {
|
|
|
57 |
if (used>loadFactor*entries.length) rehash();
|
7 |
irasnyd |
58 |
int h=hash(key); //hash the key
|
|
|
59 |
for (int i=0; i<entries.length; i++) { //run once for each place in the Table
|
|
|
60 |
int j = nextProbe(h,i); //get the [first, next] place to try putting the value
|
|
|
61 |
Entry entry=entries[j]; //get the Entry at j (where we want to put value)
|
|
|
62 |
|
|
|
63 |
//if entry[j] == null then we have found a good place to put value!
|
|
|
64 |
if (entry==null) {
|
5 |
irasnyd |
65 |
entries[j] = new Entry(key,value);
|
|
|
66 |
++size;
|
|
|
67 |
++used;
|
|
|
68 |
return null; // insertion success
|
|
|
69 |
}
|
7 |
irasnyd |
70 |
|
|
|
71 |
//if entry[j] == NIL then we must continue to probe for a new place,
|
|
|
72 |
//otherwise we will confuse the remove() and get() methods
|
5 |
irasnyd |
73 |
if (entry==NIL) continue;
|
7 |
irasnyd |
74 |
|
|
|
75 |
//if entry[j].key == key then we need to update the record
|
5 |
irasnyd |
76 |
if (entry.key.equals(key)) {
|
|
|
77 |
Object oldValue=entry.value;
|
|
|
78 |
entries[j].value = value;
|
|
|
79 |
return oldValue; // update success
|
|
|
80 |
}
|
|
|
81 |
}
|
|
|
82 |
return null; // failure: table overflow
|
|
|
83 |
}
|
|
|
84 |
|
7 |
irasnyd |
85 |
//Method to remove a key/value pair from the table. This will leave a NIL behind
|
|
|
86 |
//Precondition: key != null
|
|
|
87 |
//Postcondition: return the old value if the key was found, then put a NIL in it's place
|
|
|
88 |
// return null if the key was not found in the table.
|
5 |
irasnyd |
89 |
public Object remove(Object key) {
|
7 |
irasnyd |
90 |
int h=hash(key); //hash the key
|
|
|
91 |
for (int i=0; i<entries.length; i++) { //run once for every place in the table
|
|
|
92 |
int j = nextProbe(h,i); //find the [first, next] place to put the value
|
5 |
irasnyd |
93 |
Entry entry=entries[j];
|
7 |
irasnyd |
94 |
|
|
|
95 |
if (entry==null) break; //break out of the loop, since the key was not found
|
|
|
96 |
if (entry==NIL) continue; //keep going, since we found a NIL
|
|
|
97 |
if (entry.key.equals(key)) { //we found the key we are looking for...
|
|
|
98 |
Object oldValue=entry.value; //save value to return
|
|
|
99 |
entries[j] = NIL; //replace the value with a NIL
|
|
|
100 |
--size; //decrement size
|
5 |
irasnyd |
101 |
return oldValue; // success
|
|
|
102 |
}
|
|
|
103 |
}
|
|
|
104 |
return null; // failure: key not found
|
|
|
105 |
}
|
|
|
106 |
|
7 |
irasnyd |
107 |
//Method to access private variable size
|
|
|
108 |
//Precondition: none
|
|
|
109 |
//Postcondition: return size
|
5 |
irasnyd |
110 |
public int size() {
|
|
|
111 |
return size;
|
|
|
112 |
}
|
|
|
113 |
|
7 |
irasnyd |
114 |
//A private class which will hold a key/value pair
|
5 |
irasnyd |
115 |
private class Entry {
|
|
|
116 |
Object key, value;
|
|
|
117 |
Entry(Object k, Object v) { key=k; value=v; }
|
|
|
118 |
}
|
|
|
119 |
|
7 |
irasnyd |
120 |
//our implementation of the hash() function. Takes the Object.hashCode() method
|
|
|
121 |
//and chops off the sign bit
|
|
|
122 |
//Precondition: None (we check for key == null)
|
|
|
123 |
//Postcondition: throw an IllegalArgumentException() if the key == null
|
|
|
124 |
// return an integer hash for the given key
|
5 |
irasnyd |
125 |
private int hash(Object key) {
|
|
|
126 |
if (key==null) throw new IllegalArgumentException();
|
|
|
127 |
return (key.hashCode() & 0x7FFFFFFF) % entries.length;
|
|
|
128 |
}
|
7 |
irasnyd |
129 |
|
|
|
130 |
//Method to implement linear probing
|
|
|
131 |
//Precondition: h != null; i != null
|
|
|
132 |
//Postcondition: return the next place to try and put the value
|
5 |
irasnyd |
133 |
private int nextProbe(int h, int i) {
|
|
|
134 |
return (h + i)%entries.length; // Linear Probing
|
8 |
irasnyd |
135 |
|
|
|
136 |
//return (h + (i * 3 ))%entries.length; //Prime Probing ( p=3 )
|
|
|
137 |
//return (h + (i * 5 ))%entries.length; //Prime Probing ( p=5 )
|
|
|
138 |
//return (h + (i * 7 ))%entries.length; //Prime Probing ( p=7 )
|
|
|
139 |
//return (h + (i * 11))%entries.length; //Prime Probing ( p=11 )
|
|
|
140 |
|
|
|
141 |
//return (h + (i * i ))%entries.length; //Quadratic Probing
|
|
|
142 |
|
|
|
143 |
// STILL NEED TO ADD DOUBLE HASHING IMPLEMENTATION HERE
|
|
|
144 |
|
5 |
irasnyd |
145 |
}
|
|
|
146 |
|
7 |
irasnyd |
147 |
//Method to rehash the entire table. This will get rid of NIL's
|
|
|
148 |
//Precondition: none
|
|
|
149 |
//Postcondition: entries will be twice the size plus one (ensures odd size)
|
|
|
150 |
// all NIL will be gone
|
|
|
151 |
// all values will be re-hash()ed
|
5 |
irasnyd |
152 |
private void rehash() {
|
|
|
153 |
Entry[] oldEntries = entries;
|
|
|
154 |
entries = new Entry[2*oldEntries.length+1];
|
|
|
155 |
for (int k=0; k<oldEntries.length; k++) {
|
|
|
156 |
Entry entry=oldEntries[k];
|
|
|
157 |
if (entry==null || entry==NIL) continue;
|
|
|
158 |
int h=hash(entry.key);
|
|
|
159 |
for (int i=0; i<entries.length; i++) {
|
|
|
160 |
int j = nextProbe(h,i);
|
|
|
161 |
if (entries[j]==null) {
|
|
|
162 |
entries[j] = entry;
|
|
|
163 |
break;
|
|
|
164 |
}
|
|
|
165 |
}
|
|
|
166 |
}
|
|
|
167 |
used = size;
|
|
|
168 |
}
|
|
|
169 |
}
|