|
3 | 3 | public class LinearProbingHashST<Key, Value> { |
4 | 4 |
|
5 | 5 | private int N; // number of key-value pairs in the table |
6 | | - private int M = 16; // size of linear-probing table |
| 6 | + private int M = 16; // SIZE -- of linear-probing table |
7 | 7 | private Key[] keys; // the keys |
8 | 8 | private Value[] vals; // the values |
9 | 9 |
|
10 | 10 | public LinearProbingHashST() { |
11 | | - this.M = 16; |
12 | 11 | keys = (Key[]) new Object[M]; |
13 | 12 | vals = (Value[]) new Object[M]; |
14 | | - } |
| 13 | + } |
15 | 14 |
|
| 15 | + private LinearProbingHashST(int cap) { |
| 16 | + this.M = cap; // I made a constructor that sets the value of M (the initial size) |
| 17 | + keys = (Key[]) new Object[M]; // to the incomming value = cap, cap comes from the rezise method (M * 2) |
| 18 | + vals = (Value[]) new Object[M]; |
| 19 | + } |
| 20 | + |
16 | 21 | // Hash function - Compound keys. If the key type has multiple integer fields, we can typically mix them |
17 | 22 | // together in the way just described for String values. |
18 | 23 | private int hash(Key key) { |
19 | 24 | return (key.hashCode() & 0x7fffffff) % M; |
20 | 25 | } |
21 | 26 |
|
22 | | - private void resize(int M) { |
| 27 | + private void resize(int cap) { |
23 | 28 | LinearProbingHashST<Key, Value> t; |
24 | | - t = new LinearProbingHashST<Key, Value>(M); |
| 29 | + t = new LinearProbingHashST<Key, Value>(cap); |
25 | 30 | for (int i = 0; i < M; i++) { |
26 | 31 | if (keys[i] != null) { |
27 | 32 | t.put(keys[i], vals[i]); |
|
0 commit comments