@Override public boolean add(final Integer integer) { return add((int) integer); }
/** * Adds a range of integer values to the collection. * * This method avoid the need to explicity indicate all values in that range. Notice that the range is fully inclusive. * You can indicate a decrease range (fromValue > toValue). * * @param fromValue the first value to add in the set (inclusive). * @param toValue the last value to add to the set (inclusive). * @return <code>true</code> if the set changed as a result of this invocation, <code>false</code> otherwise. */ public boolean addRange(final int fromValue, final int toValue) { final int lowEnd; final int highEnd; if (fromValue <= toValue) { lowEnd = fromValue; highEnd = toValue; } else { highEnd = fromValue; lowEnd = toValue; } //TODO to be optimized to add missing sub-ranges in one go: boolean result = false; for (int i = lowEnd; i <= highEnd; i++) result = add(i) | 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; }
@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 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; }
@Override public boolean retainAll(final Collection<?> c) { if (size == 0) return false; @SuppressWarnings("all") final CountSet retainIndices = new CountSet(c.size() + 2); retainIndices.add(-1); retainIndices.add(size); for (final Object o : c) { if (!(o instanceof Integer)) continue; final int pos = Arrays.binarySearch(elements,0,size,(int) o); if (pos < 0) continue; retainIndices.add(pos); } if (retainIndices.size == 2) { size = 0; return true; } else if (retainIndices.size == size + 2) { return false; } else { for (int idx = retainIndices.size - 1; idx > 0; idx--) { final int toIdx = retainIndices.elements[idx]; final int fromIdx = retainIndices.elements[idx - 1] + 1; removeIndices(toIdx,fromIdx); } return true; } }
/** * Add a assembly-result object. * * @param ar the assembly result to add. * * @throws NullPointerException if {@code ar} is {@code null}. * @throws IllegalStateException if there is an assembly result with the same kmerSize. * @return {@code true} iff this addition changed the assembly result set. */ public boolean add(final AssemblyResult ar) { if (ar == null) throw new NullPointerException(); final int kmerSize = ar.getKmerSize(); if (assemblyResultByKmerSize.containsKey(kmerSize)) { if (!assemblyResultByKmerSize.get(kmerSize).equals(ar)) throw new IllegalStateException("a different assembly result with the same kmerSize was already added"); return false; } else { assemblyResultByKmerSize.put(kmerSize, ar); kmerSizes.add(kmerSize); return true; } }
@Test public void testSingleValueAdd() { final int CAPACITY = 10; final CountSet subject = new CountSet(CAPACITY); final HashSet<Integer> reasuranceSet = new HashSet<>(CAPACITY); final int REPEATS = 1000; final Random rnd = new Random(13); for (int i = 0; i < REPEATS; i++) { int newInt = rnd.nextInt(500); boolean expectedResult = reasuranceSet.add(newInt); boolean result = subject.add(newInt); Assert.assertEquals(result,expectedResult); Assert.assertEquals(subject.size(),reasuranceSet.size()); } for (final int j : reasuranceSet) Assert.assertTrue(subject.contains(j)); for (int j = 0; j < 501; j++) Assert.assertEquals(subject.contains(j),reasuranceSet.contains(j)); }
@Test(dataProvider="capacities") public void testSize(final int capacity) { final CountSet empty = new CountSet(capacity); Assert.assertEquals(empty.size(), 0); CountSet nonEmpty = new CountSet(capacity); for (int i = 0; i < capacity * 3; i++) { nonEmpty.add(i); Assert.assertEquals(nonEmpty.size(),i + 1); } }