add intersection for unsorted lists

Changed the way the intersection is computed. An intersection does
not return duplicate values. So the result is like a set.
This commit is contained in:
2017-12-09 15:38:25 +01:00
parent b56f1e6688
commit 14181822c9
2 changed files with 143 additions and 27 deletions

View File

@@ -1094,21 +1094,56 @@ public class IntListTest {
Assert.assertEquals(IntList.of(2, 4), actual);
}
{
final IntList a = IntList.of(0, 2, 4, 6);
final IntList b = IntList.of(3, 5);
final IntList actual = IntList.intersection(a, b);
Assert.assertEquals(IntList.of(), actual);
}
/*
* cardinality of elements that occur multiple time is equal to the minimum
* cardinality in either list
* duplicate elements are removed in the result
*/
{
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);
Assert.assertEquals(IntList.of(3), actual);
}
{
final IntList a = IntList.of(4);
final IntList a = IntList.of(4, 4);
final IntList b = IntList.of(4, 4, 4);
final IntList actual = IntList.intersection(a, b);
Assert.assertEquals(IntList.of(4), actual);
}
}
@Test
public void testIntersectionUnsortedLists() {
{
final IntList a = IntList.of(0, 1, 2, 3, 4);
final IntList b = IntList.of(2, 4, 5);
a.shuffle();
b.shuffle();
final IntList actual = IntList.intersection(a, b);
actual.sort();
Assert.assertEquals(IntList.of(2, 4), actual);
}
/*
* duplicate elements are removed in the result
*/
{
final IntList a = IntList.of(3, 5, 3, 3, 1);
final IntList b = IntList.of(2, 3, 3);
final IntList actual = IntList.intersection(a, b);
Assert.assertEquals(IntList.of(3), actual);
}
{
final IntList a = IntList.of(1, 4);
final IntList b = IntList.of(4, 3, 4, 4, 2);
final IntList actual = IntList.intersection(a, b);
Assert.assertEquals(IntList.of(4), actual);
}
}
}