Package fj.data

Class Set<A>

  • All Implemented Interfaces:
    java.lang.Iterable<A>
    Direct Known Subclasses:
    Set.Empty, Set.Tree

    public abstract class Set<A>
    extends java.lang.Object
    implements java.lang.Iterable<A>
    Provides an in-memory, immutable set, implemented as a red/black tree.
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      private static class  Set.Color  
      private static class  Set.Empty<A>  
      private static class  Set.Tree<A>  
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private Ord<A> ord  
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private Set​(Ord<A> ord)  
    • Method Summary

      All Methods Static Methods Instance Methods Abstract Methods Concrete Methods 
      Modifier and Type Method Description
      static <A> Set<A> arraySet​(Ord<A> o, A... as)
      Return the elements of the given iterator as a set.
      private static <A> Set<A> balance​(Ord<A> ord, Set.Color c, Set<A> l, A h, Set<A> r)  
      <B> Set<B> bind​(Ord<B> o, F<A,​Set<B>> f)
      Binds the given function across this set.
      (package private) abstract Set.Color color()  
      F<A,​F<Set<A>,​Set<A>>> delete()
      First-class deletion function.
      Set<A> delete​(A a)
      Deletes the given element from this set.
      static <A> Set<A> empty​(Ord<A> ord)
      The empty set.
      boolean equals​(java.lang.Object other)  
      Set<A> filter​(F<A,​java.lang.Boolean> f)
      Filters elements from this set by returning only elements which produce true when the given function is applied to them.
      <B> B foldMap​(F<A,​B> f, Monoid<B> m)
      Folds this Set using the given monoid.
      <B> B foldMapRight​(F<A,​B> f, Monoid<B> m)
      Folds this Set from the right using the given monoid.
      int hashCode()  
      (package private) abstract A head()  
      private Set<A> ins​(A x)  
      static <A> F<A,​F<Set<A>,​Set<A>>> insert()
      First-class insertion function.
      Set<A> insert​(A x)
      Inserts the given element into this set.
      static <A> F<Set<A>,​F<Set<A>,​Set<A>>> intersect()
      A first class function for intersect(Set).
      Set<A> intersect​(Set<A> s)
      Remove all elements from this set that do not occur in the given set.
      boolean isEmpty()  
      private boolean isTR()  
      static <A> Set<A> iterableSet​(Ord<A> o, java.lang.Iterable<A> as)
      Return the elements of the given iterable as a set.
      java.util.Iterator<A> iterator()
      Returns an iterator over this set.
      static <A> Set<A> iteratorSet​(Ord<A> o, java.util.Iterator<A> as)
      Return the elements of the given iterator as a set.
      static <A> Set<A> join​(Ord<A> o, Set<Set<A>> s)
      Join a set of sets into a single set.
      (package private) abstract Set<A> l()  
      Option<A> lookup​(A a)
      Find element equal to the given one.
      Option<A> lookupGE​(A a)
      Find smallest element greater or equal to the given one.
      Option<A> lookupGT​(A a)
      Find smallest element greater than the given one.
      Option<A> lookupLE​(A a)
      Find largest element smaller or equal to the given one.
      Option<A> lookupLT​(A a)
      Find largest element smaller than the given one.
      private Set<A> makeBlack()  
      <B> Set<B> map​(Ord<B> o, F<A,​B> f)
      Maps the given function across this set.
      Option<A> max()  
      static <A> F<Set<A>,​F<A,​java.lang.Boolean>> member()
      First-class membership check.
      boolean member​(A x)
      Checks if the given element is a member of this set.
      Option<A> min()  
      static <A> F<Set<A>,​F<Set<A>,​Set<A>>> minus()
      A first class function for minus(Set).
      Set<A> minus​(Set<A> s)
      Remove all elements from this set that occur in the given set.
      Ord<A> ord()
      Returns the order of this Set.
      (package private) abstract Set<A> r()  
      static <A> Set<A> set​(Ord<A> o, A... as)
      Constructs a set from the given elements.
      static <A> Set<A> single​(Ord<A> o, A a)
      Returns a set with a single element.
      int size()
      Returns the size of this set.
      P3<Set<A>,​Option<A>,​Set<A>> split​(A a)
      Splits this set at the given element.
      boolean subsetOf​(Set<A> s)
      Returns true if this set is a subset of the given set.
      java.util.HashSet<A> toJavaHashSet()
      Returns a java.util.HashSet representation of this set.
      java.util.List<A> toJavaList()
      Returns a java.util.List representation of this set.
      java.util.Set<A> toJavaSet()
      Returns a java.util.Set representation of this set.
      java.util.TreeSet<A> toJavaTreeSet()
      Returns a java.util.TreeSet representation of this set.
      List<A> toList()
      Returns a list representation of this set.
      List<A> toListReverse()
      Returns a list representation of this set in reverse order.
      Stream<A> toStream()
      Returns a stream representation of this set.
      Stream<A> toStreamReverse()
      Returns a stream representation of this set in reverse order.
      java.lang.String toString()  
      private static <A> Set.Tree<A> tr​(Ord<A> o, Set<A> a, A x, Set<A> b, A y, Set<A> c, A z, Set<A> d)  
      private Either<A,​P2<java.lang.Boolean,​Set<A>>> tryUpdate​(A a, F<A,​A> f)  
      static <A> F<Set<A>,​F<Set<A>,​Set<A>>> union()
      A first class function for union(Set).
      Set<A> union​(Set<A> s)
      Add all the elements of the given set to this set.
      P2<java.lang.Boolean,​Set<A>> update​(A a, F<A,​A> f)
      Updates, with the given function, the first element in the set that is equal to the given element, according to the order.
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
      • Methods inherited from interface java.lang.Iterable

        forEach, spliterator
    • Field Detail

      • ord

        private final Ord<A> ord
    • Constructor Detail

      • Set

        private Set​(Ord<A> ord)
    • Method Detail

      • isEmpty

        public final boolean isEmpty()
      • l

        abstract Set<A> l()
      • head

        abstract A head()
      • r

        abstract Set<A> r()
      • ord

        public final Ord<A> ord()
        Returns the order of this Set.
        Returns:
        the order of this Set.
      • update

        public final P2<java.lang.Boolean,​Set<A>> update​(A a,
                                                               F<A,​A> f)
        Updates, with the given function, the first element in the set that is equal to the given element, according to the order.
        Parameters:
        a - An element to replace.
        f - A function to transforms the found element.
        Returns:
        A pair of: (1) True if an element was found that matches the given element, otherwise false. (2) A new set with the given function applied to the first set element that was equal to the given element.
      • tryUpdate

        private Either<A,​P2<java.lang.Boolean,​Set<A>>> tryUpdate​(A a,
                                                                             F<A,​A> f)
      • empty

        public static <A> Set<A> empty​(Ord<A> ord)
        The empty set.
        Parameters:
        ord - An order for the type of elements.
        Returns:
        the empty set.
      • equals

        public final boolean equals​(java.lang.Object other)
        Overrides:
        equals in class java.lang.Object
      • hashCode

        public final int hashCode()
        Overrides:
        hashCode in class java.lang.Object
      • toString

        public final java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • member

        public final boolean member​(A x)
        Checks if the given element is a member of this set.
        Parameters:
        x - An element to check for membership in this set.
        Returns:
        true if the given element is a member of this set.
      • member

        public static <A> F<Set<A>,​F<A,​java.lang.Boolean>> member()
        First-class membership check.
        Returns:
        A function that returns true if the given element if a member of the given set.
      • insert

        public final Set<A> insert​(A x)
        Inserts the given element into this set.
        Parameters:
        x - An element to insert into this set.
        Returns:
        A new set with the given element inserted.
      • insert

        public static <A> F<A,​F<Set<A>,​Set<A>>> insert()
        First-class insertion function.
        Returns:
        A function that inserts a given element into a given set.
      • ins

        private Set<A> ins​(A x)
      • makeBlack

        private Set<A> makeBlack()
      • tr

        private static <A> Set.Tree<A> tr​(Ord<A> o,
                                          Set<A> a,
                                          A x,
                                          Set<A> b,
                                          A y,
                                          Set<A> c,
                                          A z,
                                          Set<A> d)
      • isTR

        private boolean isTR()
      • iterator

        public final java.util.Iterator<A> iterator()
        Returns an iterator over this set.
        Specified by:
        iterator in interface java.lang.Iterable<A>
        Returns:
        an iterator over this set.
      • single

        public static <A> Set<A> single​(Ord<A> o,
                                        A a)
        Returns a set with a single element.
        Parameters:
        o - An order for the type of element.
        a - An element to put in a set.
        Returns:
        A new set with the given element in it.
      • map

        public final <B> Set<B> map​(Ord<B> o,
                                    F<A,​B> f)
        Maps the given function across this set.
        Parameters:
        o - An order for the elements of the new set.
        f - A function to map across this set.
        Returns:
        The set of the results of applying the given function to the elements of this set.
      • foldMap

        public final <B> B foldMap​(F<A,​B> f,
                                   Monoid<B> m)
        Folds this Set using the given monoid.
        Parameters:
        f - A transformation from this Set's elements, to the monoid.
        m - The monoid to fold this Set with.
        Returns:
        The result of folding the Set with the given monoid.
      • foldMapRight

        public final <B> B foldMapRight​(F<A,​B> f,
                                        Monoid<B> m)
        Folds this Set from the right using the given monoid.
        Parameters:
        f - A transformation from this Set's elements, to the monoid.
        m - The monoid to fold this Set with.
        Returns:
        The result of folding the Set from the right with the given monoid.
      • toList

        public final List<A> toList()
        Returns a list representation of this set.
        Returns:
        a list representation of this set.
      • toJavaSet

        public final java.util.Set<A> toJavaSet()
        Returns a java.util.Set representation of this set.
        Returns:
        a java.util.Set representation of this set.
      • toJavaHashSet

        public final java.util.HashSet<A> toJavaHashSet()
        Returns a java.util.HashSet representation of this set.
        Returns:
        a java.util.HashSet representation of this set.
      • toJavaTreeSet

        public final java.util.TreeSet<A> toJavaTreeSet()
        Returns a java.util.TreeSet representation of this set.
        Returns:
        a java.util.TreeSet representation of this set.
      • toJavaList

        public final java.util.List<A> toJavaList()
        Returns a java.util.List representation of this set.
        Returns:
        a java.util.List representation of this set.
      • toListReverse

        public final List<A> toListReverse()
        Returns a list representation of this set in reverse order.
        Returns:
        a list representation of this set in reverse order.
      • toStream

        public final Stream<A> toStream()
        Returns a stream representation of this set.
        Returns:
        a stream representation of this set.
      • toStreamReverse

        public final Stream<A> toStreamReverse()
        Returns a stream representation of this set in reverse order.
        Returns:
        a stream representation of this set in reverse order.
      • bind

        public final <B> Set<B> bind​(Ord<B> o,
                                     F<A,​Set<B>> f)
        Binds the given function across this set.
        Parameters:
        o - An order for the elements of the target set.
        f - A function to bind across this set.
        Returns:
        A new set after applying the given function and joining the resulting sets.
      • union

        public final Set<A> union​(Set<A> s)
        Add all the elements of the given set to this set.
        Parameters:
        s - A set to add to this set.
        Returns:
        A new set containing all elements of both sets.
      • union

        public static <A> F<Set<A>,​F<Set<A>,​Set<A>>> union()
        A first class function for union(Set).
        Returns:
        A function that adds all the elements of one set to another set.
        See Also:
        union(Set)
      • filter

        public final Set<A> filter​(F<A,​java.lang.Boolean> f)
        Filters elements from this set by returning only elements which produce true when the given function is applied to them.
        Parameters:
        f - The predicate function to filter on.
        Returns:
        A new set whose elements all match the given predicate.
      • delete

        public final Set<A> delete​(A a)
        Deletes the given element from this set.
        Parameters:
        a - an element to remove.
        Returns:
        A new set containing all the elements of this set, except the given element.
      • delete

        public final F<A,​F<Set<A>,​Set<A>>> delete()
        First-class deletion function.
        Returns:
        A function that deletes a given element from a given set.
      • intersect

        public final Set<A> intersect​(Set<A> s)
        Remove all elements from this set that do not occur in the given set.
        Parameters:
        s - A set of elements to retain.
        Returns:
        A new set which is the intersection of this set and the given set.
      • intersect

        public static <A> F<Set<A>,​F<Set<A>,​Set<A>>> intersect()
        A first class function for intersect(Set).
        Returns:
        A function that intersects two given sets.
        See Also:
        intersect(Set)
      • minus

        public final Set<A> minus​(Set<A> s)
        Remove all elements from this set that occur in the given set.
        Parameters:
        s - A set of elements to delete.
        Returns:
        A new set which contains only the elements of this set that do not occur in the given set.
      • minus

        public static <A> F<Set<A>,​F<Set<A>,​Set<A>>> minus()
        A first class function for minus(Set).
        Returns:
        A function that removes all elements of one set from another set.
        See Also:
        minus(Set)
      • min

        public final Option<A> min()
      • max

        public final Option<A> max()
      • size

        public final int size()
        Returns the size of this set.
        Returns:
        The number of elements in this set.
      • split

        public final P3<Set<A>,​Option<A>,​Set<A>> split​(A a)
        Splits this set at the given element. Returns a product-3 of:
        • A set containing all the elements of this set which are less than the given value.
        • An option of a value equal to the given value, if one was found in this set, otherwise None.
        • A set containing all the elements of this set which are greater than the given value.
        Parameters:
        a - A value at which to split this set.
        Returns:
        Two sets and an optional value, where all elements in the first set are less than the given value and all the elements in the second set are greater than the given value, and the optional value is the given value if found, otherwise None.
      • lookup

        public final Option<A> lookup​(A a)
        Find element equal to the given one.
        Parameters:
        a - An element to compare with.
        Returns:
        Some element in this set equal to the given one, or None.
      • lookupLT

        public final Option<A> lookupLT​(A a)
        Find largest element smaller than the given one.
        Parameters:
        a - An element to compare with.
        Returns:
        Some largest element in this set smaller than the given one, or None.
      • lookupGT

        public final Option<A> lookupGT​(A a)
        Find smallest element greater than the given one.
        Parameters:
        a - An element to compare with.
        Returns:
        Some smallest element in this set greater than the given one, or None.
      • lookupLE

        public final Option<A> lookupLE​(A a)
        Find largest element smaller or equal to the given one.
        Parameters:
        a - An element to compare with.
        Returns:
        Some largest element in this set smaller or equal to the given one, or None.
      • lookupGE

        public final Option<A> lookupGE​(A a)
        Find smallest element greater or equal to the given one.
        Parameters:
        a - An element to compare with.
        Returns:
        Some smallest element in this set greater or equal to the given one, or None.
      • subsetOf

        public final boolean subsetOf​(Set<A> s)
        Returns true if this set is a subset of the given set.
        Parameters:
        s - A set which is a superset of this set if this method returns true.
        Returns:
        true if this set is a subset of the given set.
      • join

        public static <A> Set<A> join​(Ord<A> o,
                                      Set<Set<A>> s)
        Join a set of sets into a single set.
        Parameters:
        s - A set of sets.
        o - An order for the elements of the new set.
        Returns:
        A new set which is the join of the given set of sets.
      • iterableSet

        public static <A> Set<A> iterableSet​(Ord<A> o,
                                             java.lang.Iterable<A> as)
        Return the elements of the given iterable as a set.
        Parameters:
        o - An order for the elements of the new set.
        as - An iterable of elements to add to a set.
        Returns:
        A new set containing the elements of the given iterable.
      • iteratorSet

        public static <A> Set<A> iteratorSet​(Ord<A> o,
                                             java.util.Iterator<A> as)
        Return the elements of the given iterator as a set.
        Parameters:
        o - An order for the elements of the new set.
        as - An iterator of elements to add to a set.
        Returns:
        A new set containing the elements of the given iterator.
      • arraySet

        @SafeVarargs
        public static <A> Set<A> arraySet​(Ord<A> o,
                                          A... as)
        Return the elements of the given iterator as a set.
        Parameters:
        o - An order for the elements of the new set.
        as - An iterator of elements to add to a set.
        Returns:
        A new set containing the elements of the given iterator.
      • set

        @SafeVarargs
        public static <A> Set<A> set​(Ord<A> o,
                                     A... as)
        Constructs a set from the given elements.
        Parameters:
        o - An order for the elements of the new set.
        as - The elements to add to a set.
        Returns:
        A new set containing the elements of the given iterable.