Class List<V>

java.lang.Object
io.lacuna.bifurcan.IList.Mixin<V>
io.lacuna.bifurcan.List<V>
All Implemented Interfaces:
ICollection<IList<V>,V>, IList<V>, Cloneable, Iterable<V>

public class List<V> extends IList.Mixin<V> implements Cloneable
An implementation of an immutable list which allows for elements to be added and removed from both ends of the collection, as well as random-access reads and writes. Due to its relaxed radix structure, slice(), concat(), and split() are near-constant time.
  • Field Details

    • EMPTY

      public static final List EMPTY
    • prefix

      public Object[] prefix
    • suffix

      public Object[] suffix
  • Constructor Details

    • List

      public List()
    • List

      protected List(boolean linear, io.lacuna.bifurcan.nodes.ListNodes.Node root, int prefixLen, Object[] prefix, int suffixLen, Object[] suffix)
  • Method Details

    • of

      public static <V> List<V> of(V... elements)
    • from

      public static <V> List<V> from(IList<V> list)
    • from

      public static <V> List<V> from(Iterable<V> iterable)
    • from

      public static <V> List<V> from(Iterator<V> iterator)
    • empty

      public static <V> List<V> empty()
    • nth

      public V nth(long idx)
      Specified by:
      nth in interface ICollection<IList<V>,V>
      Returns:
      the element at idx
    • size

      public long size()
      Specified by:
      size in interface ICollection<IList<V>,V>
      Returns:
      the number of elements in the collection
    • isLinear

      public boolean isLinear()
      Specified by:
      isLinear in interface ICollection<IList<V>,V>
      Specified by:
      isLinear in interface IList<V>
      Returns:
      true, if the list is linear
    • addLast

      public List<V> addLast(V value)
      Specified by:
      addLast in interface IList<V>
      Returns:
      a new list, with value appended
    • addFirst

      public List<V> addFirst(V value)
      Specified by:
      addFirst in interface IList<V>
      Returns:
      a new list, with value prepended
    • removeLast

      public List<V> removeLast()
      Specified by:
      removeLast in interface IList<V>
      Returns:
      a new list with the last value removed, or the same list if already empty
    • removeFirst

      public List<V> removeFirst()
      Specified by:
      removeFirst in interface IList<V>
      Returns:
      a new list with the first value removed, or the same value if already empty
    • set

      public List<V> set(long idx, V value)
      Specified by:
      set in interface IList<V>
      Returns:
      a new list, with the element at idx overwritten with value. If idx is equal to ICollection.size(), the value is appended.
    • iterator

      public Iterator<V> iterator()
      Specified by:
      iterator in interface ICollection<IList<V>,V>
      Specified by:
      iterator in interface Iterable<V>
    • slice

      public List<V> slice(long start, long end)
      Specified by:
      slice in interface IList<V>
      Parameters:
      start - the inclusive start of the range
      end - the exclusive end of the range
      Returns:
      a sub-range of the list within [start, end), which is linear if this is linear
    • concat

      public IList<V> concat(IList<V> l)
      Specified by:
      concat in interface IList<V>
      Parameters:
      l - another list
      Returns:
      a new collection representing the concatenation of the two lists, which is linear if this is linear
    • forked

      public List<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.

      Specified by:
      forked in interface ICollection<IList<V>,V>
      Specified by:
      forked in interface IList<V>
      Returns:
      a forked form of the data structure
    • linear

      public List<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.

      Specified by:
      linear in interface ICollection<IList<V>,V>
      Specified by:
      linear in interface IList<V>
      Returns:
      a linear form of this data structure
    • clone

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