@Test(groups = "1s", timeOut = 60000) public void testRetainBetween() { IntIterableRangeSet is = new IntIterableRangeSet(); is.ELEMENTS = new int[]{1, 5, 10, 15, 20, 20, 25, 30, 32, 32, 35, 40}; is.SIZE = 12; is.CARDINALITY = 25; Assert.assertFalse(is.retainBetween(0, 41)); Assert.assertEquals(is.CARDINALITY, 25); Assert.assertEquals(is.SIZE, 12); Assert.assertEquals(Arrays.copyOf(is.ELEMENTS, is.SIZE), new int[]{1, 5, 10, 15, 20, 20, 25, 30, 32, 32, 35, 40}); Assert.assertFalse(is.retainBetween(1, 40)); Assert.assertEquals(is.CARDINALITY, 25); Assert.assertEquals(is.SIZE, 12); Assert.assertEquals(Arrays.copyOf(is.ELEMENTS, is.SIZE), new int[]{1, 5, 10, 15, 20, 20, 25, 30, 32, 32, 35, 40}); Assert.assertTrue(is.retainBetween(2, 38)); Assert.assertEquals(is.CARDINALITY, 22); Assert.assertEquals(is.SIZE, 12); Assert.assertEquals(Arrays.copyOf(is.ELEMENTS, is.SIZE), new int[]{2, 5, 10, 15, 20, 20, 25, 30, 32, 32, 35, 38}); Assert.assertTrue(is.retainBetween(18, 33)); Assert.assertEquals(is.CARDINALITY, 8); Assert.assertEquals(is.SIZE, 6); Assert.assertEquals(Arrays.copyOf(is.ELEMENTS, is.SIZE), new int[]{20, 20, 25, 30, 32, 32}); Assert.assertTrue(is.retainBetween(21, 24)); Assert.assertEquals(is.CARDINALITY, 0); Assert.assertEquals(is.SIZE, 0); Assert.assertEquals(Arrays.copyOf(is.ELEMENTS, is.SIZE), new int[]{}); is.ELEMENTS = new int[]{1, 5, 10, 15, 20, 20, 25, 30, 32, 32, 35, 40}; is.SIZE = 12; is.CARDINALITY = 25; Assert.assertTrue(is.retainBetween(-3, -1)); Assert.assertEquals(is.CARDINALITY, 0); Assert.assertEquals(is.SIZE, 0); Assert.assertEquals(Arrays.copyOf(is.ELEMENTS, is.SIZE), new int[]{}); is.ELEMENTS = new int[]{1, 5, 10, 15, 20, 20, 25, 30, 32, 32, 35, 40}; is.SIZE = 12; is.CARDINALITY = 25; Assert.assertTrue(is.retainBetween(41, 43)); Assert.assertEquals(is.CARDINALITY, 0); Assert.assertEquals(is.SIZE, 0); Assert.assertEquals(Arrays.copyOf(is.ELEMENTS, is.SIZE), new int[]{}); }
public boolean retainBetween(int f, int t) { if (lock) { throw new IllegalStateException("This set is immutable"); } if (f > t) { throw new IllegalArgumentException("Cannot retain from empty range [" + f + "," + t + "]"); } if (isEmpty() || f <= this.min() && t >= this.max()) { return false; } int rf = rangeOf(f); if (rf < 0) { rf *= -1; if (rf << 1 > SIZE) { this.clear(); return true; } f = ELEMENTS[(rf - 1) << 1]; } assert rf > 0; int rt = rangeOf(t, (rf - 1) << 1, SIZE); if (rt < 0) { rt = -rt - 1; if (rt < 1) { this.clear(); return true; } t = ELEMENTS[((rt - 1) << 1) + 1]; } assert rt > 0; int i = (rf - 1) << 1; int j = (rt - 1) << 1; if (rf <= rt) { ELEMENTS[i] = f; ELEMENTS[j + 1] = t; System.arraycopy(ELEMENTS, i, ELEMENTS, 0, j - i + 2); SIZE = (rt - rf + 1) << 1; CARDINALITY = 0; for (int k = 0; k < SIZE; k += 2) { CARDINALITY += ELEMENTS[k + 1] - ELEMENTS[k] + 1; } } else { this.clear(); } return true; }
IntIterableRangeSet implements IntIterableSet { public boolean retainBetween(int f, int t) { if (lock) { throw new IllegalStateException("This set is immutable"); } if (f > t) { throw new IllegalArgumentException("Cannot retain from empty range [" + f + "," + t + "]"); } if (isEmpty() || f <= this.min() && t >= this.max()) { return false; } int rf = rangeOf(f); if (rf < 0) { rf *= -1; if (rf << 1 > SIZE) { this.clear(); return true; } f = ELEMENTS[(rf - 1) << 1]; } assert rf > 0; int rt = rangeOf(t, (rf - 1) << 1, SIZE); if (rt < 0) { rt = -rt - 1; if (rt < 1) { this.clear(); return true; } t = ELEMENTS[((rt - 1) << 1) + 1]; } assert rt > 0; int i = (rf - 1) << 1; int j = (rt - 1) << 1; if (rf <= rt) { ELEMENTS[i] = f; ELEMENTS[j + 1] = t; System.arraycopy(ELEMENTS, i, ELEMENTS, 0, j - i + 2); SIZE = (rt - rf + 1) << 1; CARDINALITY = 0; for (int k = 0; k < SIZE; k += 2) { CARDINALITY += ELEMENTS[k + 1] - ELEMENTS[k] + 1; } } else { this.clear(); } return true; } }
IntIterableRangeSet implements IntIterableSet { public boolean retainBetween(int f, int t) { if (lock) { throw new IllegalStateException("This set is immutable"); } if (f > t) { throw new IllegalArgumentException("Cannot retain from empty range [" + f + "," + t + "]"); } if (isEmpty() || f <= this.min() && t >= this.max()) { return false; } int rf = rangeOf(f); if (rf < 0) { rf *= -1; if (rf << 1 > SIZE) { this.clear(); return true; } f = ELEMENTS[(rf - 1) << 1]; } assert rf > 0; int rt = rangeOf(t, (rf - 1) << 1, SIZE); if (rt < 0) { rt = -rt - 1; if (rt < 1) { this.clear(); return true; } t = ELEMENTS[((rt - 1) << 1) + 1]; } assert rt > 0; int i = (rf - 1) << 1; int j = (rt - 1) << 1; if (rf <= rt) { ELEMENTS[i] = f; ELEMENTS[j + 1] = t; System.arraycopy(ELEMENTS, i, ELEMENTS, 0, j - i + 2); SIZE = (rt - rf + 1) << 1; CARDINALITY = 0; for (int k = 0; k < SIZE; k += 2) { CARDINALITY += ELEMENTS[k + 1] - ELEMENTS[k] + 1; } } else { this.clear(); } return true; } IntIterableRangeSet();  IntIterableRangeSet(int a, int b);  IntIterableRangeSet(int e);  IntIterableRangeSet(int[] values);  IntIterableRangeSet(IntVar var); }
IntIterableRangeSet implements IntIterableSet { public boolean retainBetween(int f, int t) { if (lock) { throw new IllegalStateException("This set is immutable"); } if (f > t) { throw new IllegalArgumentException("Cannot retain from empty range [" + f + "," + t + "]"); } if (isEmpty() || f <= this.min() && t >= this.max()) { return false; } int rf = rangeOf(f); if (rf < 0) { rf *= -1; if (rf << 1 > SIZE) { this.clear(); return true; } f = ELEMENTS[(rf - 1) << 1]; } assert rf > 0; int rt = rangeOf(t, (rf - 1) << 1, SIZE); if (rt < 0) { rt = -rt - 1; if (rt < 1) { this.clear(); return true; } t = ELEMENTS[((rt - 1) << 1) + 1]; } assert rt > 0; int i = (rf - 1) << 1; int j = (rt - 1) << 1; if (rf <= rt) { ELEMENTS[i] = f; ELEMENTS[j + 1] = t; System.arraycopy(ELEMENTS, i, ELEMENTS, 0, j - i + 2); SIZE = (rt - rf + 1) << 1; CARDINALITY = 0; for (int k = 0; k < SIZE; k += 2) { CARDINALITY += ELEMENTS[k + 1] - ELEMENTS[k] + 1; } } else { this.clear(); } return true; } IntIterableRangeSet();  IntIterableRangeSet(int a, int b);  IntIterableRangeSet(int e);  IntIterableRangeSet(int[] values);  IntIterableRangeSet(IntVar var); @Override String toString(); String toSmartString(); void lock(); void unlock(); int getNbRanges(); int cardinality(); int minOfRange(int r); int maxOfRange(int r); @Override int min(); @Override int max(); @Override boolean add(int e); @Override boolean addAll(int... values); @Override boolean addAll(IntIterableSet set); boolean addAll(IntVar var); boolean addAll(IntIterableRangeSet set); @Override boolean retainAll(IntIterableSet set); boolean retainAll(IntIterableRangeSet set); @Override boolean remove(int e); @Override boolean removeAll(IntIterableSet set); boolean removeAll(IntIterableRangeSet set); @Override void clear(); @Override SetType getSetType(); boolean addBetween(int a, int b); @Override boolean removeBetween(int f, int t); boolean retainBetween(int f, int t); @Override int nextValue(int e); @Override int nextValueOut(int e); @Override int previousValue(int e); @Override int previousValueOut(int e); @Override boolean contains(int o); @Override IntIterableRangeSet duplicate(); IntIterableRangeSet copyFrom(IntIterableRangeSet me); void copyFrom(IntVar var); @Override int size(); @Override ISetIterator newIterator(); void plus(int x); @Override ISetIterator iterator(); void minus(int x); void times(int x); int rangeOf(int x); void compact(); IntIterableRangeSet flip(); IntIterableRangeSet flip(int lb, int ub); @SuppressWarnings("Duplicates") boolean intersect(int lb, int ub); @SuppressWarnings("Duplicates") boolean intersectDichot(int lb, int ub); @SuppressWarnings("Duplicates") boolean intersect(IntIterableRangeSet set); @SuppressWarnings("Duplicates") boolean intersect(IntVar var); void forEachValueIn(IntConsumer c); void forEachValueOut(IntConsumer c); int[] toArray(); }
IntIterableRangeSet implements IntIterableSet { public boolean retainBetween(int f, int t) { if (lock) { throw new IllegalStateException("This set is immutable"); } if (f > t) { throw new IllegalArgumentException("Cannot retain from empty range [" + f + "," + t + "]"); } if (isEmpty() || f <= this.min() && t >= this.max()) { return false; } int rf = rangeOf(f); if (rf < 0) { rf *= -1; if (rf << 1 > SIZE) { this.clear(); return true; } f = ELEMENTS[(rf - 1) << 1]; } assert rf > 0; int rt = rangeOf(t, (rf - 1) << 1, SIZE); if (rt < 0) { rt = -rt - 1; if (rt < 1) { this.clear(); return true; } t = ELEMENTS[((rt - 1) << 1) + 1]; } assert rt > 0; int i = (rf - 1) << 1; int j = (rt - 1) << 1; if (rf <= rt) { ELEMENTS[i] = f; ELEMENTS[j + 1] = t; System.arraycopy(ELEMENTS, i, ELEMENTS, 0, j - i + 2); SIZE = (rt - rf + 1) << 1; CARDINALITY = 0; for (int k = 0; k < SIZE; k += 2) { CARDINALITY += ELEMENTS[k + 1] - ELEMENTS[k] + 1; } } else { this.clear(); } return true; } IntIterableRangeSet();  IntIterableRangeSet(int a, int b);  IntIterableRangeSet(int e);  IntIterableRangeSet(int[] values);  IntIterableRangeSet(IntVar var); @Override String toString(); String toSmartString(); void lock(); void unlock(); int getNbRanges(); int cardinality(); int minOfRange(int r); int maxOfRange(int r); @Override int min(); @Override int max(); @Override boolean add(int e); @Override boolean addAll(int... values); @Override boolean addAll(IntIterableSet set); boolean addAll(IntVar var); boolean addAll(IntIterableRangeSet set); @Override boolean retainAll(IntIterableSet set); boolean retainAll(IntIterableRangeSet set); @Override boolean remove(int e); @Override boolean removeAll(IntIterableSet set); boolean removeAll(IntIterableRangeSet set); @Override void clear(); @Override SetType getSetType(); boolean addBetween(int a, int b); @Override boolean removeBetween(int f, int t); boolean retainBetween(int f, int t); @Override int nextValue(int e); @Override int nextValueOut(int e); @Override int previousValue(int e); @Override int previousValueOut(int e); @Override boolean contains(int o); @Override IntIterableRangeSet duplicate(); IntIterableRangeSet copyFrom(IntIterableRangeSet me); void copyFrom(IntVar var); @Override int size(); @Override ISetIterator newIterator(); void plus(int x); @Override ISetIterator iterator(); void minus(int x); void times(int x); int rangeOf(int x); void compact(); IntIterableRangeSet flip(); IntIterableRangeSet flip(int lb, int ub); @SuppressWarnings("Duplicates") boolean intersect(int lb, int ub); @SuppressWarnings("Duplicates") boolean intersectDichot(int lb, int ub); @SuppressWarnings("Duplicates") boolean intersect(IntIterableRangeSet set); @SuppressWarnings("Duplicates") boolean intersect(IntVar var); void forEachValueIn(IntConsumer c); void forEachValueOut(IntConsumer c); int[] toArray(); static final int MIN; static final int MAX; }