@Override public boolean add(E e) { if (e == null) throw new NullPointerException(); boolean added = LinearProbing.add(data, e); if (added && ++size == LinearProbing.getUpperSize(data.length)) enlarge(); return added; }
@Override public boolean containsKey(Object key) { if (key == null) throw new NullPointerException(); return LinearProbing.contains(keys, key); }
static <E> int getPosition(E[] d, Object o) { int i = getIndex(o, d.length); for (;;) { Object probe = d[i]; if (probe == null || o.equals(probe)) return i; if (++i == d.length) i = 0; } }
/** * Removes the element from the set represented by given data array, if it * occurs there. * * @param d * the elements of the set * @param e * the element to be removed from the array * @return {@code true} if the set has changed (the element is added), * {@code false} otherwise */ static <E> boolean remove(E[] d, Object o) { int pos = getPosition(d, o); if (d[pos] == null) return false; // else remove(d, pos); return true; } }
@Override public boolean remove(Object o) { if (o == null) throw new NullPointerException(); boolean removed = LinearProbing.remove(data, o); if (removed && --size == LinearProbing.getLowerSize(data.length)) shrink(); return removed; }
/** * Tests if the set represented by given data array contains a given object. * * @param d * the elements representing the set * @param o * the object to be tested on inclusion in the set * @return {@code true} if the object occurs in the array and {@code false} * otherwise */ static <E> boolean contains(E[] d, Object o) { int pos = getPosition(d, o); if (d[pos] == null) return false; // else return true; }
@SuppressWarnings("unchecked") public ArrayHashSet(int initialCapacity) { int capacity = LinearProbing.getInitialCapacity(initialCapacity); this.data = (E[]) new Object[capacity]; this.size = 0; }
/** * Removes the element at the given position of the table shifting, if * necessary, other elements so that all elements can be found by linear * probing. * * @param d * the array of the elements * @param pos * the position of data at which to delete the element */ static <E> void remove(E[] d, int pos) { for (;;) { int next = getMovedPosition(d, pos); E moved = d[pos] = d[next]; if (moved == null) return; // else pos = next; } }
@Override public V put(K key, V value) { if (key == null) throw new NullPointerException(); V result = putKeyValue(keys, values, key, value); if (result == null && ++size == LinearProbing.getUpperSize(keys.length)) enlarge(); return result; }
@Override public V remove(Object key) { if (key == null) throw new NullPointerException(); V result = removeEntry(keys, values, key); if (result != null && --size == LinearProbing.getLowerSize(keys.length)) shrink(); return result; }
/** * Decreasing the capacity of the table */ private void shrink() { int oldCapacity = data.length; if (oldCapacity == 1) return; E[] oldData = data; int newCapacity = oldCapacity >> 1; @SuppressWarnings("unchecked") E[] newData = (E[]) new Object[newCapacity]; for (int i = 0; i < oldCapacity; i++) { E e = oldData[i]; if (e != null) LinearProbing.add(newData, e); } this.data = newData; }
/** * Removes the element from the set represented by given data array, if it * occurs there. * * @param d * the elements of the set * @param e * the element to be removed from the array * @return {@code true} if the set has changed (the element is added), * {@code false} otherwise */ static <E> boolean remove(E[] d, Object o) { int pos = getPosition(d, o); if (d[pos] == null) return false; // else remove(d, pos); return true; } }
/** * Tests if the set represented by given data array contains a given object. * * @param d * the elements representing the set * @param o * the object to be tested on inclusion in the set * @return {@code true} if the object occurs in the array and {@code false} * otherwise */ static <E> boolean contains(E[] d, Object o) { int pos = getPosition(d, o); if (d[pos] == null) return false; // else return true; }
@Override public boolean remove(Object o) { if (o == null) throw new NullPointerException(); boolean removed = LinearProbing.remove(data, o); if (removed && --size == LinearProbing.getLowerSize(data.length)) shrink(); return removed; }
@SuppressWarnings("unchecked") public ArrayHashSet(int initialCapacity) { int capacity = LinearProbing.getInitialCapacity(initialCapacity); this.data = (E[]) new Object[capacity]; this.size = 0; }
/** * Removes the element at the given position of the table shifting, if * necessary, other elements so that all elements can be found by linear * probing. * * @param d * the array of the elements * @param pos * the position of data at which to delete the element */ static <E> void remove(E[] d, int pos) { for (;;) { int next = getMovedPosition(d, pos); E moved = d[pos] = d[next]; if (moved == null) return; // else pos = next; } }
@Override public V put(K key, V value) { if (key == null) throw new NullPointerException(); V result = putKeyValue(keys, values, key, value); if (result == null && ++size == LinearProbing.getUpperSize(keys.length)) enlarge(); return result; }
@Override public V remove(Object key) { if (key == null) throw new NullPointerException(); V result = removeEntry(keys, values, key); if (result != null && --size == LinearProbing.getLowerSize(keys.length)) shrink(); return result; }
/** * Decreasing the capacity of the table */ private void shrink() { int oldCapacity = data.length; if (oldCapacity == 1) return; E[] oldData = data; int newCapacity = oldCapacity >> 1; @SuppressWarnings("unchecked") E[] newData = (E[]) new Object[newCapacity]; for (int i = 0; i < oldCapacity; i++) { E e = oldData[i]; if (e != null) LinearProbing.add(newData, e); } this.data = newData; }
/** * Removes the element from the set represented by given data array, if it * occurs there. * * @param d * the elements of the set * @param e * the element to be removed from the array * @return {@code true} if the set has changed (the element is added), * {@code false} otherwise */ static <E> boolean remove(E[] d, Object o) { int pos = getPosition(d, o); if (d[pos] == null) return false; // else remove(d, pos); return true; } }