/** * This class exists because the structure5 Vector assumes * that the vector should only expand when the array is * at capacity. Instead, we want a vector that expands * when an element is inserted in an index beyond the end * of the underlying array storage. This allows us to * use SparseVector to store implicit binary trees. */ public class SparseVector { protected Object[] data; protected int capacity = 10; public SparseVector() { data = new Object[capacity]; } public void add(int index, T value) { ensureCapacity(index + 1); data[index] = value; } @SuppressWarnings("unchecked") public T get(int index) { if (index >= capacity()) { return null; } return (T)data[index]; } public int capacity() { return capacity; } protected void ensureCapacity(int cap) { if (cap > capacity) { while (capacity < cap) { capacity = capacity * 2; } Object[] newData = new Object[capacity]; for (int i = 0; i < data.length; i++) { newData[i] = data[i]; } data = newData; } } }