-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChainHashMap.java
More file actions
95 lines (86 loc) · 3.18 KB
/
ChainHashMap.java
File metadata and controls
95 lines (86 loc) · 3.18 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
import java.util.ArrayList;
/*
* 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 capacity 11 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;
}
}