Class PartiallyOrderedSet<E>
- Type Parameters:
E
- Element type
- All Implemented Interfaces:
Iterable<E>
,Collection<E>
,Set<E>
When you create a partially-ordered set ('poset' for short) you must
provide an PartiallyOrderedSet.Ordering
that determines the order relation. The
ordering must be:
- reflexive: e.lte(e) returns true;
- anti-symmetric: if e.lte(f) returns true, then f.lte(e) returns false only if e = f;
- transitive: if e.lte(f) returns true and f.lte(g) returns true, then e.lte(g) must return true.
Note that not all pairs of elements are related. It is OK if e.lte(f) returns false and f.lte(e) returns false also.
In addition to the usual set methods, there are methods to determine the immediate parents and children of an element in the set, and method to find all elements which have no parents or no children (i.e. "root" and "leaf" elements).
A lattice is a special kind of poset where there is a unique top and bottom element. You can use a PartiallyOrderedSet for a lattice also. It may be helpful to add the top and bottom elements to the poset on construction.
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic interface
Ordering relation. -
Field Summary
Modifier and TypeFieldDescriptionstatic final PartiallyOrderedSet.Ordering<ImmutableBitSet>
Ordering that orders bit sets by inclusion. -
Constructor Summary
ConstructorDescriptionPartiallyOrderedSet
(PartiallyOrderedSet.Ordering<E> ordering) Creates a partially-ordered set.PartiallyOrderedSet
(PartiallyOrderedSet.Ordering<E> ordering, com.google.common.base.Function<E, Iterable<E>> childFunction, com.google.common.base.Function<E, Iterable<E>> parentFunction) Deprecated.PartiallyOrderedSet
(PartiallyOrderedSet.Ordering<E> ordering, Collection<E> collection) Creates a partially-ordered set, and populates it with a given collection.PartiallyOrderedSet
(PartiallyOrderedSet.Ordering<E> ordering, Function<E, Iterable<E>> childFunction, Function<E, Iterable<E>> parentFunction) Creates a partially-ordered set with a parent-generating function. -
Method Summary
Modifier and TypeMethodDescriptionboolean
Adds an element to this lattice.void
clear()
boolean
getAncestors
(E e) Returns a list of values in the set that are less-than a given value.getChildren
(E e) Returns the values in this partially-ordered set that are less-than a given value and there are no intervening values.getChildren
(E e, boolean hypothetical) Returns the values in this partially-ordered set that are less-than a given value and there are no intervening values.getDescendants
(E e) Returns a list of values in the set that are less-than a given value.getParents
(E e) Returns the values in this partially-ordered set that are greater-than a given value and there are no intervening values.getParents
(E e, boolean hypothetical) Returns the values in this partially-ordered set that are greater-than a given value and there are no intervening values.boolean
isValid
(boolean fail) Checks internal consistency of this lattice.iterator()
void
out
(StringBuilder buf) boolean
int
size()
static <E> List<E>
Returns a list, backed by a list ofPartiallyOrderedSet.Node
s, that strips away the node and returns the element inside.Methods inherited from class java.util.AbstractSet
equals, hashCode, removeAll
Methods inherited from class java.util.AbstractCollection
addAll, containsAll, isEmpty, retainAll, toArray, toArray, toString
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.util.Collection
parallelStream, removeIf, stream, toArray
Methods inherited from interface java.util.Set
addAll, containsAll, isEmpty, retainAll, spliterator, toArray, toArray
-
Field Details
-
BIT_SET_INCLUSION_ORDERING
Ordering that orders bit sets by inclusion.For example, the children of 14 (1110) are 12 (1100), 10 (1010) and 6 (0110).
-
-
Constructor Details
-
PartiallyOrderedSet
Creates a partially-ordered set.- Parameters:
ordering
- Ordering relation
-
PartiallyOrderedSet
public PartiallyOrderedSet(PartiallyOrderedSet.Ordering<E> ordering, Function<E, Iterable<E>> childFunction, Function<E, Iterable<E>> parentFunction) Creates a partially-ordered set with a parent-generating function.- Parameters:
ordering
- Ordering relationparentFunction
- Function to compute parents of a node; may be null
-
PartiallyOrderedSet
@Deprecated public PartiallyOrderedSet(PartiallyOrderedSet.Ordering<E> ordering, com.google.common.base.Function<E, Iterable<E>> childFunction, com.google.common.base.Function<E, Iterable<E>> parentFunction) Deprecated. -
PartiallyOrderedSet
Creates a partially-ordered set, and populates it with a given collection.- Parameters:
ordering
- Ordering relationcollection
- Initial contents of partially-ordered set
-
-
Method Details
-
iterator
-
size
public int size()- Specified by:
size
in interfaceCollection<E>
- Specified by:
size
in interfaceSet<E>
- Specified by:
size
in classAbstractCollection<E>
-
contains
- Specified by:
contains
in interfaceCollection<E>
- Specified by:
contains
in interfaceSet<E>
- Overrides:
contains
in classAbstractCollection<E>
-
remove
- Specified by:
remove
in interfaceCollection<E>
- Specified by:
remove
in interfaceSet<E>
- Overrides:
remove
in classAbstractCollection<E>
-
add
Adds an element to this lattice.- Specified by:
add
in interfaceCollection<E>
- Specified by:
add
in interfaceSet<E>
- Overrides:
add
in classAbstractCollection<E>
-
isValid
public boolean isValid(boolean fail) Checks internal consistency of this lattice.- Parameters:
fail
- Whether to throw an assertion error- Returns:
- Whether valid
-
out
-
getChildren
Returns the values in this partially-ordered set that are less-than a given value and there are no intervening values.If the value is not in this set, returns null.
- Parameters:
e
- Value- Returns:
- List of values in this set that are directly less than the given value
- See Also:
-
getChildren
Returns the values in this partially-ordered set that are less-than a given value and there are no intervening values.If the value is not in this set, returns null if
hypothetical
is false.- Parameters:
e
- Valuehypothetical
- Whether to generate a list if value is not in the set- Returns:
- List of values in this set that are directly less than the given value
- See Also:
-
getParents
Returns the values in this partially-ordered set that are greater-than a given value and there are no intervening values.If the value is not in this set, returns null.
- Parameters:
e
- Value- Returns:
- List of values in this set that are directly greater than the given value
- See Also:
-
getParents
Returns the values in this partially-ordered set that are greater-than a given value and there are no intervening values.If the value is not in this set, returns
null
ifhypothetical
is false.- Parameters:
e
- Valuehypothetical
- Whether to generate a list if value is not in the set- Returns:
- List of values in this set that are directly greater than the given value
- See Also:
-
getNonChildren
-
getNonParents
-
clear
public void clear()- Specified by:
clear
in interfaceCollection<E>
- Specified by:
clear
in interfaceSet<E>
- Overrides:
clear
in classAbstractCollection<E>
-
getDescendants
Returns a list of values in the set that are less-than a given value. The list is in topological order but order is otherwise non-deterministic.- Parameters:
e
- Value- Returns:
- Values less than given value
-
strip
Returns a list, backed by a list ofPartiallyOrderedSet.Node
s, that strips away the node and returns the element inside.- Type Parameters:
E
- Element type
-
getAncestors
Returns a list of values in the set that are less-than a given value. The list is in topological order but order is otherwise non-deterministic.- Parameters:
e
- Value- Returns:
- Values less than given value
-