add static intersection method for sorted lists

This commit is contained in:
2017-12-03 09:59:58 +01:00
parent ba51b62a53
commit c41e52f8b3
2 changed files with 72 additions and 1 deletions

View File

@@ -1032,4 +1032,31 @@ public class IntListTest {
list.replaceAll(v -> 2); // replace all with 2 -> [2,2,2,2]
Assert.assertFalse("unsorted list stays unsorted", list.isSorted());
}
@Test
public void testIntersectionSortedLists() {
{
final IntList a = IntList.of(0, 1, 2, 3, 4);
final IntList b = IntList.of(2, 4, 5);
final IntList actual = IntList.intersection(a, b);
Assert.assertEquals(IntList.of(2, 4), actual);
}
/*
* cardinality of elements that occur multiple time is equal to the minimum
* cardinality in either list
*/
{
final IntList a = IntList.of(3, 3, 3);
final IntList b = IntList.of(3, 3);
final IntList actual = IntList.intersection(a, b);
Assert.assertEquals(IntList.of(3, 3), actual);
}
{
final IntList a = IntList.of(4);
final IntList b = IntList.of(4, 4, 4);
final IntList actual = IntList.intersection(a, b);
Assert.assertEquals(IntList.of(4), actual);
}
}
}