-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChainHashMap.java
153 lines (139 loc) · 4.67 KB
/
ChainHashMap.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
/*
* Copyright 2014, Michael T. Goodrich, Roberto Tamassia, Michael H. Goldwasser
*
* Developed for use with the book:
*
* Data Structures and Algorithms in Java, Sixth Edition
* Michael T. Goodrich, Roberto Tamassia, and Michael H. Goldwasser
* John Wiley & Sons, 2014
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import java.util.*;
/*
* Map implementation using hash table with separate chaining.
*
* @author Michael T. Goodrich
* @author Roberto Tamassia
* @author Michael H. Goldwasser
*/
public class ChainHashMap<K, V> extends AbstractHashMap<K, V> {
// a fixed capacity array of UnsortedTableMap that serve as buckets
private UnsortedTableMap<K, V>[] table; // initialized within createTable
// provide same constructors as base class
/** Creates a hash table with default capacity 17 and prime factor 109345121. */
public ChainHashMap() {
super();
}
/** Creates a hash table with given capacity and prime factor 109345121. */
public ChainHashMap(int cap) {
super(cap);
}
/** Creates a hash table with the given capacity and prime factor. */
public ChainHashMap(int cap, int p) {
super(cap, p);
}
/** Creates an empty table having length equal to current capacity. */
@Override
@SuppressWarnings({ "unchecked" })
protected void createTable() {
table = (UnsortedTableMap<K, V>[]) new UnsortedTableMap[capacity];
}
/**
* Returns value associated with key k in bucket with hash value h.
* If no such entry exists, returns null.
*
* @param h the hash value of the relevant bucket
* @param k the key of interest
* @return associate value (or null, if no such entry)
*/
@Override
protected V bucketGet(int h, K k) {
UnsortedTableMap<K, V> bucket = table[h];
if (bucket == null)
return null;
return bucket.get(k);
}
/**
* Associates key k with value v in bucket with hash value h, returning
* the previously associated value, if any.
*
* @param h the hash value of the relevant bucket
* @param k the key of interest
* @param v the value to be associated
* @return previous value associated with k (or null, if no such entry)
*/
@Override
protected V bucketPut(int h, K k, V v) {
UnsortedTableMap<K, V> bucket = table[h];
if (bucket == null)
bucket = table[h] = new UnsortedTableMap<>();
int oldSize = bucket.size();
V answer = bucket.put(k, v);
n += (bucket.size() - oldSize); // size may have increased
return answer;
}
/**
* Removes entry having key k from bucket with hash value h, returning
* the previously associated value, if found.
*
* @param h the hash value of the relevant bucket
* @param k the key of interest
* @return previous value associated with k (or null, if no such entry)
*/
@Override
protected V bucketRemove(int h, K k) {
UnsortedTableMap<K, V> bucket = table[h];
if (bucket == null)
return null;
int oldSize = bucket.size();
V answer = bucket.remove(k);
n -= (oldSize - bucket.size()); // size may have decreased
return answer;
}
/**
* Returns an iterable collection of all key-value entries of the map.
*
* @return iterable collection of the map's entries
*/
@Override
public Iterable<Entry<K,V>> entrySet() {
ArrayList<Entry<K,V>> buffer = new ArrayList<>();
for (int h=0; h < capacity; h++)
if (table[h] != null)
for (Entry<K,V> entry : table[h].entrySet())
buffer.add(entry);
return buffer;
}
public HashMap<Integer, ArrayList<K>> buckets() {
HashMap<Integer, ArrayList<K>> result = new HashMap<Integer, ArrayList<K>>();
ArrayList<K> buffer = new ArrayList<>();
for (int h = 0; h < capacity; h++) {
if (table[h] != null) {
buffer = new ArrayList<>();
for (Entry<K, V> entry : table[h].entrySet()) {
buffer.add(entry.getKey());
}
result.put(h, buffer);
}
}
return result;
}
protected Map<K, V> getObjectsInSameBucket(K key) {
UnsortedTableMap<K, V> bucket = table[hashValue(key)];
if (bucket == null)
return null;
return bucket;
}
}