Skip to content

Commit

Permalink
Implement intersect operation on primitive sets. Partially addresses …
Browse files Browse the repository at this point in the history
…issue #310.

Signed-off-by: Sirisha Pratha <sirisha.pratha@bnymellon.com>
  • Loading branch information
prathasirisha committed Oct 23, 2020
1 parent 4c55367 commit c45c67e
Show file tree
Hide file tree
Showing 10 changed files with 273 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,25 @@ public interface Immutable<name>Set extends Immutable<name>Collection, <name>Set
}
}

/**
* Returns the set of all objects that are members of both {@code this} and {@code set}. The intersection of
* [1, 2, 3] and [2, 3, 4] is the set [2, 3].
*
* @since 11.0.
*/
@Override
default Immutable<name>Set intersect(<name>Set set)
{
if (this.size() \< set.size())
{
return this.select(set::contains);
}
else
{
return set.select(this::contains).toImmutable();
}
}

<immutableAPI(fileName(primitive), type, name)>
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,25 @@ public interface Mutable<name>Set extends Mutable<name>Collection, <name>Set
return set.toSet().withAll(this);
}
}

/**
* Returns the set of all objects that are members of both {@code this} and {@code set}. The intersection of
* [1, 2, 3] and [2, 3, 4] is the set [2, 3].
*
* @since 11.0.
*/
@Override
default Mutable<name>Set intersect(<name>Set set)
{
if (this.size() \< set.size())
{
return this.select(set::contains);
}
else
{
return set.select(this::contains, this.newEmpty());
}
}
}

>>
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ public interface <name>Set extends <name>Iterable
*/
<name>Set union(<name>Set set);

/**
* Returns the set of all objects that are members of both {@code this} and {@code set}. The intersection of
* [1, 2, 3] and [2, 3, 4] is the set [2, 3].
*
* @since 11.0.
*/
<name>Set intersect(<name>Set set);

/**
* Follows the same general contract as {@link Set#equals(Object)}.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,46 @@ public abstract class AbstractImmutable<name>HashSetTestCase extends AbstractImm
Immutable<name>Set actual = set1.union(set2);
Assert.assertEquals(expected, actual);
}

@Test
public void intersect()
{
this.assertIntersect(
this.newWith(<["1", "2", "3"]:(literal.(type))(); separator=", ">),
this.newWith(<["3", "4", "5"]:(literal.(type))(); separator=", ">),
this.newWith(<["3"]:(literal.(type))(); separator=", ">));

this.assertIntersect(
this.newWith(<["1", "2", "3", "6"]:(literal.(type))(); separator=", ">),
this.newWith(<["3", "4", "5"]:(literal.(type))(); separator=", ">),
this.newWith(<["3"]:(literal.(type))(); separator=", ">));

this.assertIntersect(
this.newWith(<["1", "2", "3"]:(literal.(type))(); separator=", ">),
this.newWith(<["3", "4", "5" , "6"]:(literal.(type))(); separator=", ">),
this.newWith(<["3"]:(literal.(type))(); separator=", ">));

this.assertIntersect(
this.newWith(),
this.newWith(),
this.newWith());

this.assertIntersect(
this.newWith(),
this.newWith(<["3", "4", "5"]:(literal.(type))(); separator=", ">),
this.newWith());

this.assertIntersect(
this.newWith(<["1", "2", "3"]:(literal.(type))(); separator=", ">),
this.newWith(),
this.newWith());
}

private void assertIntersect(Immutable<name>Set set1, Immutable<name>Set set2, Immutable<name>Set expected)
{
Immutable<name>Set actual = set1.intersect(set2);
Assert.assertEquals(expected, actual);
}
}

>>
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,46 @@ public abstract class Abstract<name>SetTestCase extends AbstractMutable<name>Col
Mutable<name>Set actual = set1.union(set2);
Assert.assertEquals(expected, actual);
}

@Test
public void intersect()
{
this.assertIntersect(
this.newWith(<["1", "2", "3"]:(literal.(type))(); separator=", ">),
this.newWith(<["3", "4", "5"]:(literal.(type))(); separator=", ">),
this.newWith(<["3"]:(literal.(type))(); separator=", ">));

this.assertIntersect(
this.newWith(<["1", "2", "3", "6"]:(literal.(type))(); separator=", ">),
this.newWith(<["3", "4", "5"]:(literal.(type))(); separator=", ">),
this.newWith(<["3"]:(literal.(type))(); separator=", ">));

this.assertIntersect(
this.newWith(<["1", "2", "3"]:(literal.(type))(); separator=", ">),
this.newWith(<["3", "4", "5" , "6"]:(literal.(type))(); separator=", ">),
this.newWith(<["3"]:(literal.(type))(); separator=", ">));

this.assertIntersect(
this.newWith(),
this.newWith(),
this.newWith());

this.assertIntersect(
this.newWith(),
this.newWith(<["3", "4", "5"]:(literal.(type))(); separator=", ">),
this.newWith());

this.assertIntersect(
this.newWith(<["1", "2", "3"]:(literal.(type))(); separator=", ">),
this.newWith(),
this.newWith());
}

private void assertIntersect(Mutable<name>Set set1, Mutable<name>Set set2, Mutable<name>Set expected)
{
Mutable<name>Set actual = set1.intersect(set2);
Assert.assertEquals(expected, actual);
}
}

>>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,15 @@ public static BooleanHashSet newSet(BooleanIterable source)
return BooleanHashSet.newSetWith(source.toArray());
}

/**
* @since 11.0.
*/
@Override
public BooleanHashSet newEmpty()
{
return new BooleanHashSet();
}

@Override
public boolean add(boolean element)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -864,4 +864,33 @@ public void union()
MutableBooleanSet actual5 = set15.union(set25);
Assert.assertEquals(this.emptySet, actual5);
}

@Test
public void intersect()
{
MutableBooleanSet set11 = this.newWith(true);
MutableBooleanSet set21 = this.newWith(false);
MutableBooleanSet actual = set11.intersect(set21);
Assert.assertEquals(this.emptySet, actual);

MutableBooleanSet set12 = this.newWith(false);
MutableBooleanSet set22 = this.newWith(false);
MutableBooleanSet actual2 = set12.intersect(set22);
Assert.assertEquals(this.setWithFalse, actual2);

MutableBooleanSet set13 = this.newWith(true);
MutableBooleanSet set23 = this.newWith(true);
MutableBooleanSet actual3 = set13.intersect(set23);
Assert.assertEquals(this.setWithTrue, actual3);

MutableBooleanSet set14 = this.setWithTrueFalse;
MutableBooleanSet set24 = this.newWith();
MutableBooleanSet actual4 = set14.intersect(set24);
Assert.assertEquals(this.emptySet, actual4);

MutableBooleanSet set15 = this.newWith();
MutableBooleanSet set25 = this.newWith();
MutableBooleanSet actual5 = set15.intersect(set25);
Assert.assertEquals(this.emptySet, actual5);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -428,4 +428,44 @@ private void assertUnion(MutableByteSet set1, MutableByteSet set2, MutableByteSe
MutableByteSet actual = set1.union(set2);
Assert.assertEquals(expected, actual);
}

@Test
public void intersect()
{
this.assertIntersect(
this.newWith((byte) 1, (byte) 2, (byte) 3),
this.newWith((byte) 3, (byte) 4, (byte) 5),
this.newWith((byte) 3));

this.assertIntersect(
this.newWith((byte) 1, (byte) 2, (byte) 3, (byte) 6),
this.newWith((byte) 3, (byte) 4, (byte) 5),
this.newWith((byte) 3));

this.assertIntersect(
this.newWith((byte) 1, (byte) 2, (byte) 3),
this.newWith((byte) 3, (byte) 4, (byte) 5, (byte) 6),
this.newWith((byte) 3));

this.assertIntersect(
this.newWith(),
this.newWith(),
this.newWith());

this.assertIntersect(
this.newWith(),
this.newWith((byte) 3, (byte) 4, (byte) 5),
this.newWith());

this.assertIntersect(
this.newWith((byte) 1, (byte) 2, (byte) 3),
this.newWith(),
this.newWith());
}

private void assertIntersect(MutableByteSet set1, MutableByteSet set2, MutableByteSet expected)
{
MutableByteSet actual = set1.intersect(set2);
Assert.assertEquals(expected, actual);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -268,4 +268,44 @@ private void assertUnion(ImmutableByteSet set1, ImmutableByteSet set2, Immutable
ImmutableByteSet actual = set1.union(set2);
Assert.assertEquals(expected, actual);
}

@Test
public void intersect()
{
this.assertIntersect(
this.newWith((byte) 1, (byte) 2, (byte) 3),
this.newWith((byte) 3, (byte) 4, (byte) 5),
this.newWith((byte) 3));

this.assertIntersect(
this.newWith((byte) 1, (byte) 2, (byte) 3, (byte) 6),
this.newWith((byte) 3, (byte) 4, (byte) 5),
this.newWith((byte) 3));

this.assertIntersect(
this.newWith((byte) 1, (byte) 2, (byte) 3),
this.newWith((byte) 3, (byte) 4, (byte) 5, (byte) 6),
this.newWith((byte) 3));

this.assertIntersect(
this.newWith(),
this.newWith(),
this.newWith());

this.assertIntersect(
this.newWith(),
this.newWith((byte) 3, (byte) 4, (byte) 5),
this.newWith());

this.assertIntersect(
this.newWith((byte) 1, (byte) 2, (byte) 3),
this.newWith(),
this.newWith());
}

private void assertIntersect(ImmutableByteSet set1, ImmutableByteSet set2, ImmutableByteSet expected)
{
ImmutableByteSet actual = set1.intersect(set2);
Assert.assertEquals(expected, actual);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -596,4 +596,33 @@ public void union()
ImmutableBooleanSet actual5 = set15.union(set25);
Assert.assertEquals(this.emptySet, actual5);
}

@Test
public void intersect()
{
ImmutableBooleanSet set11 = this.newWith(true);
ImmutableBooleanSet set21 = this.newWith(false);
ImmutableBooleanSet actual = set11.intersect(set21);
Assert.assertEquals(this.emptySet, actual);

ImmutableBooleanSet set12 = this.newWith(false);
ImmutableBooleanSet set22 = this.newWith(false);
ImmutableBooleanSet actual2 = set12.intersect(set22);
Assert.assertEquals(this.falseSet, actual2);

ImmutableBooleanSet set13 = this.newWith(true);
ImmutableBooleanSet set23 = this.newWith(true);
ImmutableBooleanSet actual3 = set13.intersect(set23);
Assert.assertEquals(this.trueSet, actual3);

ImmutableBooleanSet set14 = this.trueFalseSet;
ImmutableBooleanSet set24 = this.newWith();
ImmutableBooleanSet actual4 = set14.intersect(set24);
Assert.assertEquals(this.emptySet, actual4);

ImmutableBooleanSet set15 = this.newWith();
ImmutableBooleanSet set25 = this.newWith();
ImmutableBooleanSet actual5 = set15.intersect(set25);
Assert.assertEquals(this.emptySet, actual5);
}
}

0 comments on commit c45c67e

Please sign in to comment.