Skip to content

Commit

Permalink
[CALCITE-6698] Wrong (swapped) results in PartiallyOrderedSet#getNonC…
Browse files Browse the repository at this point in the history
…hildren and getNonParents

1. Align the behavior of getNonChildren/getNonParents with:
i) the class documentation;
ii) the behavior of getParents/getChildren methods.
2. Add explicit Javadoc to clarify the results of its method.
3. Update and add test cases with expected results.
  • Loading branch information
zabetak committed Nov 18, 2024
1 parent e20a9a5 commit e62a8d5
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ public class PartiallyOrderedSet<E> extends AbstractSet<E> {
* in the set.
*/
private final Node<E> topNode;
/**
* Synthetic node to hold all nodes that have no children. It does not appear
* in the set.
*/
private final Node<E> bottomNode;

/**
Expand Down Expand Up @@ -552,7 +556,7 @@ public void out(StringBuilder buf) {
// breadth-first search, to iterate over every element once, printing
// those nearest the top element first
final Set<E> seen = new HashSet<>();
final Deque<E> unseen = new ArrayDeque<>(getNonChildren());
final Deque<E> unseen = new ArrayDeque<>(getNonParents());
while (!unseen.isEmpty()) {
E e = unseen.pop();
buf.append(" ");
Expand Down Expand Up @@ -681,12 +685,24 @@ private void closure(Function<E, Iterable<E>> generator, E e, ImmutableList.Buil
}
}

/**
* Returns all elements in the set that have no children. These elements are the leaf/minimal
* elements of the poset.
*
* @return a list of elements that have no children.
*/
public List<E> getNonChildren() {
return strip(topNode.childList);
return strip(bottomNode.parentList);
}

/**
* Returns all elements in the set that have no parents. These elements are the roots/maximal
* elements of the poset.
*
* @return a list of elements that have no parents.
*/
public List<E> getNonParents() {
return strip(bottomNode.parentList);
return strip(topNode.childList);
}

@Override public void clear() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import java.util.AbstractList;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.List;
Expand Down Expand Up @@ -110,8 +111,8 @@ private static boolean isBitSuperset(Integer e1, Integer e2) {
poset.add(abcd);
printValidate(poset);
assertThat(poset, hasSize(2));
assertThat(poset.getNonChildren(), hasToString("['abcd']"));
assertThat(poset.getNonParents(), hasToString("['']"));
assertThat(poset.getNonChildren(), hasToString("['']"));
assertThat(poset.getNonParents(), hasToString("['abcd']"));

final String ab = "'ab'";
poset.add(ab);
Expand Down Expand Up @@ -158,8 +159,8 @@ private static boolean isBitSuperset(Integer e1, Integer e2) {

poset.add(b);
printValidate(poset);
assertThat(poset.getNonChildren(), hasToString("['abcd']"));
assertThat(poset.getNonParents(), hasToString("['']"));
assertThat(poset.getNonChildren(), hasToString("['']"));
assertThat(poset.getNonParents(), hasToString("['abcd']"));
assertThat(poset.getChildren(b), hasToString("['']"));
assertEqualsList("['ab', 'bcd']", poset.getParents(b));
assertThat(poset.getChildren(b), hasToString("['']"));
Expand All @@ -180,6 +181,32 @@ private static boolean isBitSuperset(Integer e1, Integer e2) {
assertEqualsList("['ab', 'abcd']", poset.getAncestors("'a'"));
}

@Test void testGetNonParentsOnLteIntPosetReturnsMaxValue() {
PartiallyOrderedSet<Integer> poset =
new PartiallyOrderedSet<>((i, j) -> i <= j, Arrays.asList(20, 30, 40));
assertThat(poset.getNonParents(), hasToString("[40]"));
}

@Test void testGetNonChildrenOnLteIntPosetReturnsMinValue() {
PartiallyOrderedSet<Integer> poset =
new PartiallyOrderedSet<>((i, j) -> i <= j, Arrays.asList(20, 30, 40));
assertThat(poset.getNonChildren(), hasToString("[20]"));
}

@Test void testGetNonParentsOnGteIntPosetReturnsMinValue() {
PartiallyOrderedSet<Integer> poset =
new PartiallyOrderedSet<>((i, j) -> i >= j, Arrays.asList(20, 30, 40));
assertThat(poset.getNonParents(), hasToString("[20]"));
}

@Test void testGetNonChildrenOnGteIntPosetReturnsMaxValue() {
PartiallyOrderedSet<Integer> poset =
new PartiallyOrderedSet<>((i, j) -> i >= j, Arrays.asList(20, 30, 40));
printValidate(poset);
System.out.println(poset.getChildren(20));
assertThat(poset.getNonChildren(), hasToString("[40]"));
}

@Test void testPosetTricky() {
final PartiallyOrderedSet<String> poset =
new PartiallyOrderedSet<>(STRING_SUBSET_ORDERING);
Expand All @@ -195,6 +222,8 @@ private static boolean isBitSuperset(Integer e1, Integer e2) {
printValidate(poset);
poset.add("'ab'");
printValidate(poset);
assertThat(poset.getNonChildren(), hasToString("['a', 'b']"));
assertThat(poset.getNonParents(), hasToString("['ac', 'ab']"));
}

@Test void testPosetBits() {
Expand Down

0 comments on commit e62a8d5

Please sign in to comment.