Skip to content

Commit

Permalink
Indexes 100%
Browse files Browse the repository at this point in the history
  • Loading branch information
Baunsgaard committed Aug 8, 2023
1 parent eedc261 commit 13c14a7
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,25 @@ public static enum ColIndexType {
*/
public int findIndex(int i);

/**
* Slice the range given.
*
* The slice result is an object containing the indexes in the original array to slice out and a new index for the
* sliced columns offset by l.
*
* Example:
*
* ArrayIndex(1,3,5).slice(2,6)
*
* returns
*
* SliceResult(1,3,ArrayIndex(1,3))
*
*
* @param l inclusive lower bound
* @param u exclusive upper bound
* @return A slice result
*/
public SliceResult slice(int l, int u);

@Override
Expand Down Expand Up @@ -202,6 +221,12 @@ public static class SliceResult {
/** The already modified column index to return on slices */
public final IColIndex ret;

/**
* The slice result
* @param idStart The starting index
* @param idEnd The ending index (not inclusive)
* @param ret The resulting IColIndex
*/
protected SliceResult(int idStart, int idEnd, IColIndex ret) {
this.idStart = idStart;
this.idEnd = idEnd;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.io.IOException;
import java.util.Arrays;

import org.apache.commons.lang.NotImplementedException;
import org.apache.sysds.runtime.compress.DMLCompressionException;
import org.apache.sysds.runtime.compress.utils.IntArrayList;

Expand Down Expand Up @@ -122,17 +123,24 @@ else if(i < u)

@Override
public SliceResult slice(int l, int u) {

if(u <= this.l)
return new SliceResult(0, 0, null);
else if(l >= this.u)
return new SliceResult(0, 0, null);
else if(l <= this.l && u >= this.u)
return new SliceResult(0, size(), new RangeIndex(this.l - l, this.u - l));
// else if (l == this.l){
// int minU = Math.min(u, this.u);
// int offR = minU - this.l;
// return new SliceResult(0, offR + 1, new RangeIndex(0, minU - l));

// }
else {
int offL = Math.max(l, this.l) - this.l;
int offR = Math.min(u, this.u) - this.l;
return new SliceResult(offL, offR, new RangeIndex(Math.max(l, this.l) - l, Math.min(u, this.u) - l));
int maxL = Math.max(l, this.l);
int minU = Math.min(u, this.u);
int offL = maxL - this.l;
int offR = minU - this.l;
return new SliceResult(offL, offR, new RangeIndex(maxL - l, minU - l ));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -975,4 +975,56 @@ public void combineRanges4() {
IColIndex e = ColIndexFactory.createI(1, 2, 3, 4, 6, 7, 8, 9);
assertEquals(e, a.combine(b));
}

@Test
public void containsTest() {
// to get coverage
IColIndex a = new TwoRangesIndex(new RangeIndex(1, 10), new RangeIndex(5, 10));
assertTrue(a.contains(7));
assertTrue(a.contains(2));
assertTrue(a.contains(9));
assertFalse(a.contains(-1));
assertFalse(a.contains(11));
assertFalse(a.contains(10));
}

@Test
public void containsTest2() {
// to get coverage
IColIndex a = new TwoRangesIndex(new RangeIndex(1, 4), new RangeIndex(11, 20));
assertFalse(a.contains(7));
assertTrue(a.contains(2));
assertTrue(a.contains(11));
assertFalse(a.contains(-1));
assertFalse(a.contains(20));
assertFalse(a.contains(10));
}

@Test
public void containsAnyArray1() {
IColIndex a = new TwoRangesIndex(new RangeIndex(1, 4), new RangeIndex(11, 20));
IColIndex b = new RangeIndex(7, 15);
assertTrue(a.containsAny(b));
}

@Test
public void containsAnyArrayF1() {
IColIndex a = new TwoRangesIndex(new RangeIndex(1, 4), new RangeIndex(11, 20));
IColIndex b = new RangeIndex(20, 25);
assertFalse(a.containsAny(b));
}

@Test
public void containsAnyArrayF2() {
IColIndex a = new TwoRangesIndex(new RangeIndex(1, 4), new RangeIndex(11, 20));
IColIndex b = new RangeIndex(4, 11);
assertFalse(a.containsAny(b));
}

@Test
public void containsAnyArray2() {
IColIndex a = new TwoRangesIndex(new RangeIndex(1, 4), new RangeIndex(11, 20));
IColIndex b = new RangeIndex(3, 11);
assertTrue(a.containsAny(b));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,16 @@ public static Collection<Object[]> data() {
new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18}, //
ColIndexFactory.create(1, 19)});

tests.add(new Object[] {//
new int[] {1, 2, 3, 4, 5, 6}, //
ColIndexFactory.create(1, 7)});
tests.add(new Object[] {//
new int[] {2, 3, 4, 5, 6}, //
ColIndexFactory.create(2, 7)});
tests.add(new Object[] {//
new int[] {3, 4, 5, 6}, //
ColIndexFactory.create(3, 7)});

tests.add(createWithArray(1, 323));
tests.add(createWithArray(2, 1414));
tests.add(createWithArray(144, 32));
Expand Down Expand Up @@ -294,10 +304,8 @@ public void slice_5_SubRange() {
if(expected.length > 5) {

SliceResult sr = actual.slice(4, expected[5] + 1);
int i = 0;
while(expected[i] < sr.idStart)
i++;
assertEquals(actual.toString(), i, sr.idStart);

assertEquals(actual.toString(), expected[5], sr.ret.get(sr.ret.size() - 1) + 4);
}
}

Expand Down Expand Up @@ -563,6 +571,18 @@ public void findIndexAfter() {
assertEquals(er, -el - 1, actual.findIndex(expected[el - 1] + 100));
}

@Test
public void testHash() {
// flawed test in the case hashes can collide, but it should be unlikely.
IColIndex a = ColIndexFactory.createI(1, 2, 3, 1342);
if(a.equals(actual)) {
assertEquals(a.hashCode(), actual.hashCode());
}
else {
assertNotEquals(a.hashCode(), actual.hashCode());
}
}

private void shift(int i) {
compare(expected, actual.shift(i), i);
}
Expand Down

0 comments on commit 13c14a7

Please sign in to comment.