// Helene Martin, CSE 143 // Hash table implementation of a set, an unordered collection without duplicates. // The hash table stores values at indexes determined by a hashing function. // This very basic implementation has no collision resolution so it will only work // if all inserted values hash to unique indexes. public class HashSetIncomplete implements Set { private int size; private E[] elementData; // Builds a new HashSet of the specified capacity public HashSetIncomplete(int capacity) { elementData = (E[]) new Object[capacity]; // don't worry about this } // Adds the given value to the set // Pre: There is nothing at indexOf(value) @Override public void add(E value) { if (!contains(value)) { elementData[indexOf(value)] = value; size++; } } // Remove the given value from the set @Override public void remove(E value) { if (contains(value)) { elementData[indexOf(value)] = null; size--; } } // Checks whether the given value is in the set @Override public boolean contains(E value) { return elementData[indexOf(value)] != null; } // Returns the size of the set @Override public int size() { return size; } // Uses the hashing function to find the index of a value. private int indexOf(E value) { return Math.abs(value.hashCode() % elementData.length); } }