@Test(groups = "1s", timeOut = 60000) public void testRemoveBetween3() { 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 = 26; Assert.assertTrue(is.removeBetween(26, 38)); Assert.assertEquals(is.CARDINALITY, 16); Assert.assertEquals(is.SIZE, 10); Assert.assertEquals(Arrays.copyOf(is.ELEMENTS, is.SIZE), new int[]{1, 5, 10, 15, 20, 20, 25, 25, 39, 40}); }
@Override public boolean removeBetween(int f, int t) { if (lock) { throw new IllegalStateException("This set is immutable"); } boolean rem = false; if (f > t) { throw new IllegalArgumentException("Cannot remove from empty range [" + f + "," + t + "]"); } int rf = rangeOf(f); if (rf < 0) { rf *= -1; if (rf > SIZE >> 1) { return false; } 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) { return false; } t = ELEMENTS[((rt - 1) << 1) + 1]; } assert rt > 0; int i = (rf - 1) << 1; int j = (rt - 1) << 1; if (rf <= rt) { int dcard = -(f - ELEMENTS[i] + ELEMENTS[j + 1] - t); dcard += ELEMENTS[i + 1] - ELEMENTS[i] + 1; if (rf < rt) { for (int k = i + 2; k <= j + 1; k += 2) { dcard += ELEMENTS[k + 1] - ELEMENTS[k] + 1; } System.arraycopy(ELEMENTS, j + 1, ELEMENTS, i + 1, SIZE - (j + 1)); SIZE -= (rt - rf) << 1; } CARDINALITY -= dcard; int c = ELEMENTS[i] == f ? 1 : 0; c += ELEMENTS[i + 1] == t ? 2 : 0; switch (c) { case 0: grow(SIZE + 2); System.arraycopy(ELEMENTS, i, ELEMENTS, i + 2, SIZE - i); ELEMENTS[i + 1] = f - 1; ELEMENTS[i + 2] = t + 1; SIZE += 2; break; case 1: ELEMENTS[i] = t + 1; break; case 2: ELEMENTS[i + 1] = f - 1; break; case 3: if (i < SIZE - 2) { System.arraycopy(ELEMENTS, i + 2, ELEMENTS, i, SIZE - (i + 2)); } SIZE -= 2; break; } rem = true; } return rem; }
IntIterableRangeSet implements IntIterableSet { @Override public boolean removeBetween(int f, int t) { if (lock) { throw new IllegalStateException("This set is immutable"); } boolean rem = false; if (f > t) { throw new IllegalArgumentException("Cannot remove from empty range [" + f + "," + t + "]"); } int rf = rangeOf(f); if (rf < 0) { rf *= -1; if (rf > SIZE >> 1) { return false; } 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) { return false; } t = ELEMENTS[((rt - 1) << 1) + 1]; } assert rt > 0; int i = (rf - 1) << 1; int j = (rt - 1) << 1; if (rf <= rt) { int dcard = -(f - ELEMENTS[i] + ELEMENTS[j + 1] - t); dcard += ELEMENTS[i + 1] - ELEMENTS[i] + 1; if (rf < rt) { for (int k = i + 2; k <= j + 1; k += 2) { dcard += ELEMENTS[k + 1] - ELEMENTS[k] + 1; } System.arraycopy(ELEMENTS, j + 1, ELEMENTS, i + 1, SIZE - (j + 1)); SIZE -= (rt - rf) << 1; } CARDINALITY -= dcard; int c = ELEMENTS[i] == f ? 1 : 0; c += ELEMENTS[i + 1] == t ? 2 : 0; switch (c) { case 0: grow(SIZE + 2); System.arraycopy(ELEMENTS, i, ELEMENTS, i + 2, SIZE - i); ELEMENTS[i + 1] = f - 1; ELEMENTS[i + 2] = t + 1; SIZE += 2; break; case 1: ELEMENTS[i] = t + 1; break; case 2: ELEMENTS[i + 1] = f - 1; break; case 3: if (i < SIZE - 2) { System.arraycopy(ELEMENTS, i + 2, ELEMENTS, i, SIZE - (i + 2)); } SIZE -= 2; break; } rem = true; } return rem; } }
IntIterableRangeSet implements IntIterableSet { @Override public boolean removeBetween(int f, int t) { if (lock) { throw new IllegalStateException("This set is immutable"); } boolean rem = false; if (f > t) { throw new IllegalArgumentException("Cannot remove from empty range [" + f + "," + t + "]"); } int rf = rangeOf(f); if (rf < 0) { rf *= -1; if (rf > SIZE >> 1) { return false; } 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) { return false; } t = ELEMENTS[((rt - 1) << 1) + 1]; } assert rt > 0; int i = (rf - 1) << 1; int j = (rt - 1) << 1; if (rf <= rt) { int dcard = -(f - ELEMENTS[i] + ELEMENTS[j + 1] - t); dcard += ELEMENTS[i + 1] - ELEMENTS[i] + 1; if (rf < rt) { for (int k = i + 2; k <= j + 1; k += 2) { dcard += ELEMENTS[k + 1] - ELEMENTS[k] + 1; } System.arraycopy(ELEMENTS, j + 1, ELEMENTS, i + 1, SIZE - (j + 1)); SIZE -= (rt - rf) << 1; } CARDINALITY -= dcard; int c = ELEMENTS[i] == f ? 1 : 0; c += ELEMENTS[i + 1] == t ? 2 : 0; switch (c) { case 0: grow(SIZE + 2); System.arraycopy(ELEMENTS, i, ELEMENTS, i + 2, SIZE - i); ELEMENTS[i + 1] = f - 1; ELEMENTS[i + 2] = t + 1; SIZE += 2; break; case 1: ELEMENTS[i] = t + 1; break; case 2: ELEMENTS[i + 1] = f - 1; break; case 3: if (i < SIZE - 2) { System.arraycopy(ELEMENTS, i + 2, ELEMENTS, i, SIZE - (i + 2)); } SIZE -= 2; break; } rem = true; } return rem; } IntIterableRangeSet();  IntIterableRangeSet(int a, int b);  IntIterableRangeSet(int e);  IntIterableRangeSet(int[] values);  IntIterableRangeSet(IntVar var); }
IntIterableRangeSet implements IntIterableSet { @Override public boolean removeBetween(int f, int t) { if (lock) { throw new IllegalStateException("This set is immutable"); } boolean rem = false; if (f > t) { throw new IllegalArgumentException("Cannot remove from empty range [" + f + "," + t + "]"); } int rf = rangeOf(f); if (rf < 0) { rf *= -1; if (rf > SIZE >> 1) { return false; } 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) { return false; } t = ELEMENTS[((rt - 1) << 1) + 1]; } assert rt > 0; int i = (rf - 1) << 1; int j = (rt - 1) << 1; if (rf <= rt) { int dcard = -(f - ELEMENTS[i] + ELEMENTS[j + 1] - t); dcard += ELEMENTS[i + 1] - ELEMENTS[i] + 1; if (rf < rt) { for (int k = i + 2; k <= j + 1; k += 2) { dcard += ELEMENTS[k + 1] - ELEMENTS[k] + 1; } System.arraycopy(ELEMENTS, j + 1, ELEMENTS, i + 1, SIZE - (j + 1)); SIZE -= (rt - rf) << 1; } CARDINALITY -= dcard; int c = ELEMENTS[i] == f ? 1 : 0; c += ELEMENTS[i + 1] == t ? 2 : 0; switch (c) { case 0: grow(SIZE + 2); System.arraycopy(ELEMENTS, i, ELEMENTS, i + 2, SIZE - i); ELEMENTS[i + 1] = f - 1; ELEMENTS[i + 2] = t + 1; SIZE += 2; break; case 1: ELEMENTS[i] = t + 1; break; case 2: ELEMENTS[i + 1] = f - 1; break; case 3: if (i < SIZE - 2) { System.arraycopy(ELEMENTS, i + 2, ELEMENTS, i, SIZE - (i + 2)); } SIZE -= 2; break; } rem = true; } return rem; } 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 { @Override public boolean removeBetween(int f, int t) { if (lock) { throw new IllegalStateException("This set is immutable"); } boolean rem = false; if (f > t) { throw new IllegalArgumentException("Cannot remove from empty range [" + f + "," + t + "]"); } int rf = rangeOf(f); if (rf < 0) { rf *= -1; if (rf > SIZE >> 1) { return false; } 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) { return false; } t = ELEMENTS[((rt - 1) << 1) + 1]; } assert rt > 0; int i = (rf - 1) << 1; int j = (rt - 1) << 1; if (rf <= rt) { int dcard = -(f - ELEMENTS[i] + ELEMENTS[j + 1] - t); dcard += ELEMENTS[i + 1] - ELEMENTS[i] + 1; if (rf < rt) { for (int k = i + 2; k <= j + 1; k += 2) { dcard += ELEMENTS[k + 1] - ELEMENTS[k] + 1; } System.arraycopy(ELEMENTS, j + 1, ELEMENTS, i + 1, SIZE - (j + 1)); SIZE -= (rt - rf) << 1; } CARDINALITY -= dcard; int c = ELEMENTS[i] == f ? 1 : 0; c += ELEMENTS[i + 1] == t ? 2 : 0; switch (c) { case 0: grow(SIZE + 2); System.arraycopy(ELEMENTS, i, ELEMENTS, i + 2, SIZE - i); ELEMENTS[i + 1] = f - 1; ELEMENTS[i + 2] = t + 1; SIZE += 2; break; case 1: ELEMENTS[i] = t + 1; break; case 2: ELEMENTS[i + 1] = f - 1; break; case 3: if (i < SIZE - 2) { System.arraycopy(ELEMENTS, i + 2, ELEMENTS, i, SIZE - (i + 2)); } SIZE -= 2; break; } rem = true; } return rem; } 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; }