Class LinearList<V>

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

public class LinearList<V> extends IList.Mixin<V> implements Cloneable
A simple implementation of a mutable list combining the best characteristics of ArrayList and ArrayDeque, allowing elements to be added and removed from both ends of the collection and allowing random-access reads and updates. Unlike List, it can only hold Integer.MAX_VALUE elements.

Calls to IList.concat(IList), IList.slice(long, long), and IList.split(int) create virtual collections which retain a reference to the whole underlying collection, and are somewhat less efficient than LinearList.

  • Constructor Details

    • LinearList

      public LinearList()
    • LinearList

      public LinearList(int capacity)
      Parameters:
      capacity - the initial capacity of the list
  • Method Details

    • of

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

      public static <V> LinearList<V> from(Collection<V> collection)
      Returns:
      a list containing the entries of collection
    • from

      public static <V> LinearList<V> from(Iterable<V> iterable)
      Returns:
      a list containing the elements of iterable
    • from

      public static <V> LinearList<V> from(Iterator<V> iterator)
      Returns:
      a list containing all remaining elements of iterator
    • from

      public static <V> LinearList<V> from(IList<V> list)
      Returns:
      a list containing the elements of list
    • 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 LinearList<V> addLast(V value)
      Specified by:
      addLast in interface IList<V>
      Returns:
      a new list, with value appended
    • addFirst

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

      public LinearList<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
    • removeLast

      public LinearList<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
    • clear

      public LinearList<V> clear()
    • set

      public LinearList<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.
    • nth

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

      public V popFirst()
      Removes, and returns, the first element of the list.
      Returns:
      the first element of the list
      Throws:
      IndexOutOfBoundsException - if the list is empty
    • popLast

      public V popLast()
      Removes, and returns, the last element of the list.
      Returns:
      the last element of the list
      Throws:
      IndexOutOfBoundsException - if the list is empty
    • iterator

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

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

      public IList<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 IList<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 LinearList<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