Class LinearSet<V>

java.lang.Object
io.lacuna.bifurcan.ISet.Mixin<V>
io.lacuna.bifurcan.LinearSet<V>
All Implemented Interfaces:
ICollection<ISet<V>,V>, ISet<V>, Iterable<V>, Predicate<V>

public class LinearSet<V> extends ISet.Mixin<V>
A set which builds atop LinearMap, and shares the same performance characteristics.
  • Constructor Details

    • LinearSet

      public LinearSet()
    • LinearSet

      public LinearSet(int initialCapacity)
      Parameters:
      initialCapacity - the initial capacity of the set
    • LinearSet

      public LinearSet(int initialCapacity, ToLongFunction<V> hashFn, BiPredicate<V,V> equalsFn)
      Parameters:
      initialCapacity - the initial capacity of the set
      hashFn - the hash function used by the set
      equalsFn - the equality semantics used by the set
    • LinearSet

      public LinearSet(ToLongFunction<V> hashFn, BiPredicate<V,V> equalsFn)
      Parameters:
      hashFn - the hash function used by the set
      equalsFn - the equality semantics used by the set
  • Method Details

    • from

      public static <V> LinearSet<V> from(IList<V> list)
      Returns:
      a set containing the elements in list
    • from

      public static <V> LinearSet<V> from(Collection<V> collection)
      Returns:
      a set containing the elements in collection
    • from

      public static <V> LinearSet<V> from(Iterator<V> iterator)
      Returns:
      a set containing the remaining elements in iterator
    • from

      public static <V> LinearSet<V> from(Iterable<V> iterable)
      Returns:
      a set containing the elements in iterable
    • from

      public static <V> LinearSet<V> from(ISet<V> set)
      Returns:
      a set containing the same elements as set, with the same equality semantics
    • of

      public static <V> LinearSet<V> of(V... elements)
      Returns:
      a set containing elements
    • isLinear

      public boolean isLinear()
      Returns:
      true, if the set is linear
    • valueHash

      public ToLongFunction<V> valueHash()
      Returns:
      the hash function used by the set
    • valueEquality

      public BiPredicate<V,V> valueEquality()
      Returns:
      the equality semantics used by the set
    • add

      public LinearSet<V> add(V value)
      Returns:
      the set, containing value
    • remove

      public LinearSet<V> remove(V value)
      Returns:
      the set, without value
    • clear

      public LinearSet<V> clear()
    • contains

      public boolean contains(V value)
      Returns:
      true, if the set contains value
    • size

      public long size()
      Returns:
      the number of elements in the collection
    • indexOf

      public OptionalLong indexOf(V element)
      Returns:
      the position of element in the collection, if it's present
    • nth

      public V nth(long idx)
      Returns:
      the element at idx
    • iterator

      public Iterator<V> iterator()
    • zip

      public <U> LinearMap<V,U> zip(Function<V,U> f)
      Returns:
      a map which has a corresponding value, computed by f, for each element in the set
    • union

      public LinearSet<V> union(ISet<V> s)
      Returns:
      a new set, representing the union with set
    • difference

      public LinearSet<V> difference(ISet<V> s)
      Returns:
      a new set, representing the difference with set
    • intersection

      public LinearSet<V> intersection(ISet<V> s)
      Returns:
      a new set, representing the intersection with set
    • forked

      public ISet<V> forked()
      Description copied from interface: ICollection
      This returns a data structure which is forked, which is equivalent to Clojure's persistent data structures, also sometimes called functional or immutable. This is called "forked" because it means that multiple functions can make divergent changes to the data structure without affecting each other.

      If only a single function or scope uses the data structure, it can be left as a linear data structure, which can have significant performance benefits.

      If the data structure is already forked, it will simply return itself.

      Returns:
      a forked form of the data structure
    • linear

      public LinearSet<V> linear()
      Description copied from interface: ICollection
      This returns a data structure which is linear, or temporarily mutable. The term "linear", as used here, does not completely align with the formal definition of linear types as used in type theory. It is meant to describe the linear dataflow of the method calls, and as a converse to "forked" data structures.

      If ICollection.forked() is called on a linear collection, all references to that linear collection should be discarded.

      If the data structure is already linear, it will simply return itself.

      Returns:
      a linear form of this data structure
    • split

      public List<LinearSet<V>> split(int parts)
      Description copied from interface: ICollection
      Splits the collection into roughly even pieces, for parallel processing. Depending on the size and contents of the collection, this function may not return exactly parts subsets.
      Parameters:
      parts - the target number of pieces
      Returns:
      a list containing subsets of the collection.
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class ISet.Mixin<V>
    • clone

      public LinearSet<V> clone()
      Specified by:
      clone in interface ICollection<ISet<V>,V>
      Overrides:
      clone in class ISet.Mixin<V>
      Returns:
      a cloned copy of the collection