provide counting-only intersection test for VectorMath

This commit is contained in:
Jonathan Shook
2023-08-30 01:10:20 -05:00
parent fc1e207453
commit cf6ecfe42b
2 changed files with 49 additions and 0 deletions

View File

@@ -20,6 +20,25 @@ import java.util.Arrays;
public class Intersections {
public static long count(long[] reference, long[] sample) {
int a_index = 0, b_index = 0, matches = 0;
long a_element, b_element;
while (a_index < reference.length && b_index < sample.length) {
a_element = reference[a_index];
b_element = sample[b_index];
if (a_element == b_element) {
++matches;
a_index++;
b_index++;
} else if (b_element < a_element) {
b_index++;
} else {
a_index++;
}
}
return matches;
}
public static long[] find(long[] reference, long[] sample) {
long[] result = new long[reference.length];
int a_index = 0, b_index = 0, acc_index = -1;
@@ -62,6 +81,25 @@ public class Intersections {
return Arrays.copyOfRange(result,0,acc_index+1);
}
public static int count(int[] reference, int[] sample) {
int a_index = 0, b_index = 0, matches = 0;
int a_element, b_element;
while (a_index < reference.length && b_index < sample.length) {
a_element = reference[a_index];
b_element = sample[b_index];
if (a_element == b_element) {
++matches;
a_index++;
b_index++;
} else if (b_element < a_element) {
b_index++;
} else {
a_index++;
}
}
return matches;
}
public static int[] resize(int[] arr) {
int len = arr.length;
int[] copy = new int[len + 1];

View File

@@ -33,4 +33,15 @@ class IntersectionsTest {
assertThat(result).isEqualTo(new long[]{4,5});
}
@Test
public void testCountIntIntersection() {
long result = Intersections.count(new int[]{1,3,5,7,9}, new int[]{1,2,3,9,10});
assertThat(result).isEqualTo(3L);
}
@Test
public void testCountLongIntersection() {
long result = Intersections.count(new long[]{1,3,5,7,9}, new long[]{1,2,3,9,10});
assertThat(result).isEqualTo(3);
}
}