| 13 | import avian.Data; |
| 14 | |
| 15 | public class HashMap<K, V> implements Map<K, V> { |
| 16 | private static final int MinimumCapacity = 16; |
| 17 | |
| 18 | private int size; |
| 19 | private Cell[] array; |
| 20 | private final Helper helper; |
| 21 | |
| 22 | public HashMap(int capacity, Helper<K, V> helper) { |
| 23 | if (capacity > 0) { |
| 24 | array = new Cell[Data.nextPowerOfTwo(capacity)]; |
| 25 | } |
| 26 | this.helper = helper; |
| 27 | } |
| 28 | |
| 29 | public HashMap(int capacity) { |
| 30 | this(capacity, new MyHelper()); |
| 31 | } |
| 32 | |
| 33 | public HashMap() { |
| 34 | this(0); |
| 35 | } |
| 36 | |
| 37 | public HashMap(Map<K, V> map) { |
| 38 | this(map.size()); |
| 39 | for (Map.Entry<K, V> entry : map.entrySet()) { |
| 40 | put(entry.getKey(), entry.getValue()); |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | public String toString() { |
| 45 | return avian.Data.toString(this); |
| 46 | } |
| 47 | |
| 48 | public boolean isEmpty() { |
| 49 | return size() == 0; |
| 50 | } |
| 51 | |
| 52 | public int size() { |
| 53 | return size; |
| 54 | } |
| 55 | |
| 56 | private void grow() { |
| 57 | if (array == null || size >= array.length * 2) { |
| 58 | resize(array == null ? MinimumCapacity : array.length * 2); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | private void shrink() { |
| 63 | if (array.length / 2 >= MinimumCapacity && size <= array.length / 3) { |
| 64 | resize(array.length / 2); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | private void resize(int capacity) { |
| 69 | Cell<K, V>[] newArray = null; |
| 70 | if (capacity != 0) { |
| 71 | capacity = Data.nextPowerOfTwo(capacity); |
| 72 | if (array != null && array.length == capacity) { |
nothing calls this directly
no outgoing calls
no test coverage detected