Package fj.data

Class Set<A>

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

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

    • ord

      private final Ord<A> ord
  • Constructor Details

    • Set

      private Set(Ord<A> ord)
  • Method Details

    • isEmpty

      public final boolean isEmpty()
    • color

      abstract Set.Color color()
    • 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<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<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(Object other)
      Overrides:
      equals in class Object
    • hashCode

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

      public final String toString()
      Overrides:
      toString in class 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,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)
    • balance

      private static <A> Set<A> balance(Ord<A> ord, Set.Color c, Set<A> l, A h, Set<A> r)
    • isTR

      private boolean isTR()
    • iterator

      public final Iterator<A> iterator()
      Returns an iterator over this set.
      Specified by:
      iterator in interface 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 Set<A> toJavaSet()
      Returns a java.util.Set representation of this set.
      Returns:
      a java.util.Set representation of this set.
    • toJavaHashSet

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

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

      public final 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:
    • filter

      public final Set<A> filter(F<A,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:
    • 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:
    • 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:
      o - An order for the elements of the new set.
      s - A set of sets.
      Returns:
      A new set which is the join of the given set of sets.
    • iterableSet

      public static <A> Set<A> iterableSet(Ord<A> o, 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, 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.