/** * Set the set contents to a single integer value. * @param value the integer value to set the set to. */ public void setTo(int value) { ensureCapacity(1); size = 1; elements[0] = value; }
/** * Set the content of this set to a collection of integers. * @param values the new values to be included in the set. * @throws NullPointerException if <code>value</code> is <code>null</code>. */ public void setTo(int ... values) { ensureCapacity(values.length); size = values.length; System.arraycopy(values, 0, elements, 0, size); Arrays.sort(elements,0,size); }
/** * Add an integer value to the set. * @param value to add to the set. * @return <code>true</code> if the set changed as a result of this invocation, <code>false</code> otherwise. */ public boolean add(final int value) { int pos = Arrays.binarySearch(elements,0,size,value); if (pos >= 0) return false; int insertPos = - pos - 1; ensureCapacity(size + 1); System.arraycopy(elements, insertPos, elements, insertPos + 1, size - insertPos); elements[insertPos] = value; size++; return true; }
@Override public boolean addAll(final Collection<? extends Integer> numbers) { ensureCapacity(size + numbers.size()); boolean result = false; for (final Number n : numbers) result = add(n.intValue()) | result; return result; }
/** * Add a arbitrary number of integers to the set. * * @param values integer to add to the set. * @return <code>true</code> if the set changed as a result of this invocation, <code>false</code> otherwise. */ public boolean addAll(final int ... values) { ensureCapacity(size + values.length); boolean result = false; for (final int v : values) result = add(v) | result; return result; }
/** * Add all values within a range in an integer array. * * @param source array where the values to add are found. * @param fromIndex first position from <code>source</code> to add (inclusive). * @param toIndex index after the last position in <code>source</code> to add (thus exclusive). * @throws NullPointerException if <code>source</code> is <code>null</code>. * @throws NegativeArraySizeException if <code>fromIndex</code> or <code>toIndex</code> are negative. * @throws ArrayIndexOutOfBoundsException if <code>fromIndex</code> or <code>toIndex</code> are beyond bounds * allowed <code>[0 .. source.length]</code>. * @return <code>true</code> if the set changed as a result of this invocation, <code>false</code> otherwise. */ public boolean addAll(final int[] source, final int fromIndex, final int toIndex) { ensureCapacity(size + source.length); boolean result = false; for (int i = fromIndex; i < toIndex; i++) result = add(source[i]) | result; return result; }