Package arc.struct

Class Seq<T>

java.lang.Object
arc.struct.Seq<T>
All Implemented Interfaces:
Eachable<T>, Iterable<T>
Direct Known Subclasses:
DelayedRemovalSeq, Jval.JsonArray, SnapshotSeq

public class Seq<T> extends Object implements Iterable<T>, Eachable<T>
A resizable, ordered or unordered array of objects. If unordered, this class avoids a memory copy when removing elements (the last element is moved to the removed element's position).
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static class 
     
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    T[]
    Provides direct access to the underlying array.
    static int
    Debugging variable to count total number of iterators allocated.
    boolean
     
    int
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    Seq()
    Creates an ordered array with a capacity of 16.
    Seq(boolean ordered)
    Creates an ordered/unordered array with the specified capacity.
    Seq(boolean ordered, int capacity)
     
    Seq(boolean ordered, int capacity, Class<?> arrayType)
    Creates a new array with items of the specified type.
    Seq(boolean ordered, T[] array, int start, int count)
    Creates a new array containing the elements in the specified array.
    Seq(int capacity)
    Creates an ordered array with the specified capacity.
    Seq(Seq<? extends T> array)
    Creates a new array containing the elements in the specified array.
    Seq(Class<?> arrayType)
    Creates an ordered array with items of the specified type and a capacity of 16.
    Seq(T[] array)
    Creates a new ordered array containing the elements in the specified array.
  • Method Summary

    Modifier and Type
    Method
    Description
    add(Seq<? extends T> array)
     
    add(T value)
     
    add(T[] array)
     
    add(T value1, T value2)
     
    add(T value1, T value2, T value3)
     
    add(T value1, T value2, T value3, T value4)
     
    addAll(Seq<? extends T> array)
     
    addAll(Seq<? extends T> array, int start, int count)
     
    addAll(Iterable<? extends T> items)
     
    addAll(T... array)
     
    addAll(T[] array, int start, int count)
     
    boolean
    addUnique(T value)
    Adds a value if it was not already in this sequence.
    boolean
    allMatch(Boolf<T> predicate)
     
    boolean
    any()
     
    <R> Seq<R>
    as()
     
    <K> ObjectMap<K,T>
    asMap(Func<T,K> keygen)
     
    <K, V> ObjectMap<K,V>
    asMap(Func<T,K> keygen, Func<T,V> valgen)
     
     
     
    boolean
    contains(Boolf<T> predicate)
     
    boolean
    contains(T value)
     
    boolean
    contains(T value, boolean identity)
    Returns if this array contains value.
    boolean
     
    boolean
    containsAll(Seq<T> seq, boolean identity)
     
     
    int
    count(Boolf<T> predicate)
     
    Note that this allocates a new set.
    <E extends T>
    void
    each(Boolf<? super T> pred, Cons<E> consumer)
     
    void
    each(Cons<? super T> consumer)
     
    T[]
    ensureCapacity(int additionalCapacity)
    Increases the size of the backing array to accommodate the specified number of additional items.
    boolean
    equals(Object object)
     
    filter(Boolf<T> predicate)
    Deprecated.
    This name is misleading, as it modifies the collection! If you want a newly allocated Seq, use select.
    find(Boolf<T> predicate)
     
    Returns the first item.
    Returns the first item, or null if this Seq is empty.
    <R> Seq<R>
    flatMap(Func<T,Iterable<R>> mapper)
    Returns a new array with the mapped values.
    <R> Seq<R>
    Flattens this array of arrays into one array.
    get(int index)
     
    getFrac(float index)
     
    int
     
    int
    indexOf(Boolf<T> value)
     
    int
    indexOf(T value)
     
    int
    indexOf(T value, boolean identity)
    Returns the index of first occurrence of value in the array, or -1 if no such value exists.
    void
    insert(int index, T value)
     
    boolean
    Returns true if the array is empty.
    Returns an iterator for the items in the array.
    int
    lastIndexOf(T value, boolean identity)
    Returns an index of last occurrence of value in array or -1 if no such value exists.
     
    <R> Seq<R>
    map(Func<T,R> mapper)
    Returns a new array with the mapped values.
    mapFloat(Floatf<T> mapper)
     
    mapInt(Intf<T> mapper)
     
    max(Floatf<T> func)
     
    max(Comparator<T> func)
     
    min(Boolf<T> filter, Floatf<T> func)
     
    min(Floatf<T> func)
     
    min(Comparator<T> func)
     
    Returns the last item.
    pop()
    Removes and returns the last item.
    pop(Prov<T> constructor)
    If this array is empty, returns an object specified by the constructor.
    Returns a random item from the array, or null if the array is empty.
    random(Rand rand)
     
    random(T exclude)
    Returns a random item from the array, excluding the specified element.
    <R> R
    reduce(R initial, Func2<T,R,R> reducer)
     
    remove(int index)
    Removes and returns the item at the specified index.
    boolean
    remove(Boolf<T> value)
    Removes a single value by predicate.
    boolean
    remove(T value)
    Removes a value, without using identity.
    boolean
    remove(T value, boolean identity)
    Removes the first instance of the specified value in the array.
    removeAll(Boolf<T> pred)
     
    boolean
    removeAll(Seq<? extends T> array)
     
    boolean
    removeAll(Seq<? extends T> array, boolean identity)
    Removes from this array all of elements contained in the specified array.
    void
    removeRange(int start, int end)
    Removes the items between the specified indices, inclusive.
    void
    replace(Func<T,T> mapper)
    Replaces values without creating a new array.
    boolean
    replace(T from, T to)
    Replaces the first occurrence of 'from' with 'to'.
    protected T[]
    resize(int newSize)
    Creates a new backing array with the specified size containing the current items.
    retainAll(Boolf<T> predicate)
    Removes everything that does not match this predicate.
     
    select(Boolf<T> predicate)
    Allocates a new array with all elements that match the predicate.
    static <T> Seq<T>
    select(T[] array, Boolf<T> test)
     
    selectFrom(Seq<T> base, Boolf<T> predicate)
     
    selectRanked(Comparator<T> comparator, int kthLowest)
    Selects the nth-lowest element from the Seq according to Comparator ranking.
    int
    selectRankedIndex(Comparator<T> comparator, int kthLowest)
     
    void
    set(int index, T value)
     
    void
    set(Seq<? extends T> array)
    Sets this array's contents to the specified array.
    void
    set(T[] array)
    Sets this array's contents to the specified array.
    T[]
    setSize(int newSize)
    Sets the array size, leaving any values beyond the current size null.
    T[]
    Reduces the size of the backing array to the size of the actual items.
     
    Sorts this array.
    sort(Floatf<? super T> comparator)
     
    sort(Comparator<? super T> comparator)
    Sorts the array.
    <U extends Comparable<? super U>>
    Seq<T>
    sortComparing(Func<? super T,? extends U> keyExtractor)
     
    int
    sum(Intf<T> summer)
     
    float
    sumf(Floatf<T> summer)
     
    void
    swap(int first, int second)
     
    T[]
    Returns the items as an array.
    <V> V[]
    toArray(Class type)
     
     
    toString(String separator)
     
    toString(String separator, Func<T,String> stringifier)
     
    void
    truncate(int newSize)
    Reduces the size of the array to the specified size.
    with(Cons<Seq<T>> cons)
     
    static <T> Seq<T>
    with(Iterable<T> array)
     
    static <T> Seq<T>
    with(T... array)
     
    static <T> Seq<T>
    withArrays(Object... arrays)
     

    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 Details

    • iteratorsAllocated

      public static int iteratorsAllocated
      Debugging variable to count total number of iterators allocated.
    • items

      public T[] items
      Provides direct access to the underlying array. If the Array's generic type is not Object, this field may only be accessed if the Seq(boolean, int, Class) constructor was used.
    • size

      public int size
    • ordered

      public boolean ordered
  • Constructor Details

    • Seq

      public Seq()
      Creates an ordered array with a capacity of 16.
    • Seq

      public Seq(int capacity)
      Creates an ordered array with the specified capacity.
    • Seq

      public Seq(boolean ordered)
      Creates an ordered/unordered array with the specified capacity.
    • Seq

      public Seq(boolean ordered, int capacity)
      Parameters:
      ordered - If false, methods that remove elements may change the order of other elements in the array, which avoids a memory copy.
      capacity - Any elements added beyond this will cause the backing array to be grown.
    • Seq

      public Seq(boolean ordered, int capacity, Class<?> arrayType)
      Creates a new array with items of the specified type.
      Parameters:
      ordered - If false, methods that remove elements may change the order of other elements in the array, which avoids a memory copy.
      capacity - Any elements added beyond this will cause the backing array to be grown.
    • Seq

      public Seq(Class<?> arrayType)
      Creates an ordered array with items of the specified type and a capacity of 16.
    • Seq

      public Seq(Seq<? extends T> array)
      Creates a new array containing the elements in the specified array. The new array will have the same type of backing array and will be ordered if the specified array is ordered. The capacity is set to the number of elements, so any subsequent elements added will cause the backing array to be grown.
    • Seq

      public Seq(T[] array)
      Creates a new ordered array containing the elements in the specified array. The new array will have the same type of backing array. The capacity is set to the number of elements, so any subsequent elements added will cause the backing array to be grown.
    • Seq

      public Seq(boolean ordered, T[] array, int start, int count)
      Creates a new array containing the elements in the specified array. The new array will have the same type of backing array. The capacity is set to the number of elements, so any subsequent elements added will cause the backing array to be grown.
      Parameters:
      ordered - If false, methods that remove elements may change the order of other elements in the array, which avoids a memory copy.
  • Method Details

    • withArrays

      public static <T> Seq<T> withArrays(Object... arrays)
    • with

      public static <T> Seq<T> with(T... array)
      See Also:
    • with

      public static <T> Seq<T> with(Iterable<T> array)
    • select

      public static <T> Seq<T> select(T[] array, Boolf<T> test)
      See Also:
    • asMap

      public <K, V> ObjectMap<K,V> asMap(Func<T,K> keygen, Func<T,V> valgen)
    • asMap

      public <K> ObjectMap<K,T> asMap(Func<T,K> keygen)
    • asSet

      public ObjectSet<T> asSet()
    • copy

      public Seq<T> copy()
    • list

      public ArrayList<T> list()
    • sumf

      public float sumf(Floatf<T> summer)
    • sum

      public int sum(Intf<T> summer)
    • each

      public <E extends T> void each(Boolf<? super T> pred, Cons<E> consumer)
    • each

      public void each(Cons<? super T> consumer)
      Specified by:
      each in interface Eachable<T>
    • replace

      public void replace(Func<T,T> mapper)
      Replaces values without creating a new array.
    • flatten

      public <R> Seq<R> flatten()
      Flattens this array of arrays into one array. Allocates a new instance.
    • flatMap

      public <R> Seq<R> flatMap(Func<T,Iterable<R>> mapper)
      Returns a new array with the mapped values.
    • map

      public <R> Seq<R> map(Func<T,R> mapper)
      Returns a new array with the mapped values.
    • mapInt

      public IntSeq mapInt(Intf<T> mapper)
      Returns:
      a new int array with the mapped values.
    • mapFloat

      public FloatSeq mapFloat(Floatf<T> mapper)
      Returns:
      a new float array with the mapped values.
    • reduce

      public <R> R reduce(R initial, Func2<T,R,R> reducer)
    • allMatch

      public boolean allMatch(Boolf<T> predicate)
    • contains

      public boolean contains(Boolf<T> predicate)
    • min

      public T min(Comparator<T> func)
    • max

      public T max(Comparator<T> func)
    • min

      public T min(Boolf<T> filter, Floatf<T> func)
    • min

      public T min(Floatf<T> func)
    • max

      public T max(Floatf<T> func)
    • find

      @Nullable public T find(Boolf<T> predicate)
    • with

      public Seq<T> with(Cons<Seq<T>> cons)
    • addUnique

      public boolean addUnique(T value)
      Adds a value if it was not already in this sequence.
      Returns:
      whether this value was added successfully.
    • add

      public Seq<T> add(T value)
    • add

      public Seq<T> add(T value1, T value2)
    • add

      public Seq<T> add(T value1, T value2, T value3)
    • add

      public Seq<T> add(T value1, T value2, T value3, T value4)
    • add

      public Seq<T> add(Seq<? extends T> array)
    • add

      public Seq<T> add(T[] array)
    • addAll

      public Seq<T> addAll(Seq<? extends T> array)
    • addAll

      public Seq<T> addAll(Seq<? extends T> array, int start, int count)
    • addAll

      public Seq<T> addAll(T... array)
    • addAll

      public Seq<T> addAll(T[] array, int start, int count)
    • addAll

      public Seq<T> addAll(Iterable<? extends T> items)
    • set

      public void set(Seq<? extends T> array)
      Sets this array's contents to the specified array.
    • set

      public void set(T[] array)
      Sets this array's contents to the specified array.
    • getFrac

      @Nullable public T getFrac(float index)
    • get

      public T get(int index)
    • set

      public void set(int index, T value)
    • insert

      public void insert(int index, T value)
    • swap

      public void swap(int first, int second)
    • replace

      public boolean replace(T from, T to)
      Replaces the first occurrence of 'from' with 'to'.
      Returns:
      whether anything was replaced.
    • containsAll

      public boolean containsAll(Seq<T> seq)
      Returns:
      whether this sequence contains every other element in the other sequence.
    • containsAll

      public boolean containsAll(Seq<T> seq, boolean identity)
      Returns:
      whether this sequence contains every other element in the other sequence.
    • contains

      public boolean contains(T value)
    • contains

      public boolean contains(T value, boolean identity)
      Returns if this array contains value.
      Parameters:
      value - May be null.
      identity - If true, == comparison will be used. If false, .equals() comparison will be used.
      Returns:
      true if array contains value, false if it doesn't
    • indexOf

      public int indexOf(T value)
    • indexOf

      public int indexOf(T value, boolean identity)
      Returns the index of first occurrence of value in the array, or -1 if no such value exists.
      Parameters:
      value - May be null.
      identity - If true, == comparison will be used. If false, .equals() comparison will be used.
      Returns:
      An index of first occurrence of value in array or -1 if no such value exists
    • indexOf

      public int indexOf(Boolf<T> value)
    • lastIndexOf

      public int lastIndexOf(T value, boolean identity)
      Returns an index of last occurrence of value in array or -1 if no such value exists. Search is started from the end of an array.
      Parameters:
      value - May be null.
      identity - If true, == comparison will be used. If false, .equals() comparison will be used.
      Returns:
      An index of last occurrence of value in array or -1 if no such value exists
    • remove

      public boolean remove(T value)
      Removes a value, without using identity.
    • remove

      public boolean remove(Boolf<T> value)
      Removes a single value by predicate.
      Returns:
      whether the item was found and removed.
    • remove

      public boolean remove(T value, boolean identity)
      Removes the first instance of the specified value in the array.
      Parameters:
      value - May be null.
      identity - If true, == comparison will be used. If false, .equals() comparison will be used.
      Returns:
      true if value was found and removed, false otherwise
    • remove

      public T remove(int index)
      Removes and returns the item at the specified index.
    • removeRange

      public void removeRange(int start, int end)
      Removes the items between the specified indices, inclusive.
    • removeAll

      public Seq<T> removeAll(Boolf<T> pred)
      Returns:
      this object
    • removeAll

      public boolean removeAll(Seq<? extends T> array)
    • removeAll

      public boolean removeAll(Seq<? extends T> array, boolean identity)
      Removes from this array all of elements contained in the specified array.
      Parameters:
      identity - True to use ==, false to use .equals().
      Returns:
      true if this array was modified.
    • pop

      public T pop(Prov<T> constructor)
      If this array is empty, returns an object specified by the constructor. Otherwise, acts like pop().
    • pop

      public T pop()
      Removes and returns the last item.
    • peek

      public T peek()
      Returns the last item.
    • first

      public T first()
      Returns the first item.
    • firstOpt

      @Nullable public T firstOpt()
      Returns the first item, or null if this Seq is empty.
    • isEmpty

      public boolean isEmpty()
      Returns true if the array is empty.
    • any

      public boolean any()
    • clear

      public Seq<T> clear()
    • shrink

      public T[] shrink()
      Reduces the size of the backing array to the size of the actual items. This is useful to release memory when many items have been removed, or if it is known that more items will not be added.
      Returns:
      items
    • ensureCapacity

      public T[] ensureCapacity(int additionalCapacity)
      Increases the size of the backing array to accommodate the specified number of additional items. Useful before adding many items to avoid multiple backing array resizes.
      Returns:
      items
    • setSize

      public T[] setSize(int newSize)
      Sets the array size, leaving any values beyond the current size null.
      Returns:
      items
    • resize

      protected T[] resize(int newSize)
      Creates a new backing array with the specified size containing the current items.
    • sort

      public Seq<T> sort()
      Sorts this array. The array elements must implement Comparable. This method is not thread safe (uses Sort.instance()).
    • sort

      public Seq<T> sort(Comparator<? super T> comparator)
      Sorts the array. This method is not thread safe (uses Sort.instance()).
    • sort

      public Seq<T> sort(Floatf<? super T> comparator)
    • sortComparing

      public <U extends Comparable<? super U>> Seq<T> sortComparing(Func<? super T,? extends U> keyExtractor)
    • selectFrom

      public Seq<T> selectFrom(Seq<T> base, Boolf<T> predicate)
    • distinct

      public Seq<T> distinct()
      Note that this allocates a new set. Mutates.
    • as

      public <R> Seq<R> as()
    • select

      public Seq<T> select(Boolf<T> predicate)
      Allocates a new array with all elements that match the predicate.
    • retainAll

      public Seq<T> retainAll(Boolf<T> predicate)
      Removes everything that does not match this predicate.
    • filter

      @Deprecated public Seq<T> filter(Boolf<T> predicate)
      Deprecated.
      This name is misleading, as it modifies the collection! If you want a newly allocated Seq, use select. For a direct replacement, use retainAll.
      Removes everything that does not match this predicate.
    • count

      public int count(Boolf<T> predicate)
    • selectRanked

      public T selectRanked(Comparator<T> comparator, int kthLowest)
      Selects the nth-lowest element from the Seq according to Comparator ranking. This might partially sort the Array. The array must have a size greater than 0, or a ArcRuntimeException will be thrown.
      Parameters:
      comparator - used for comparison
      kthLowest - rank of desired object according to comparison, n is based on ordinal numbers, not array indices. for min value use 1, for max value use size of array, using 0 results in runtime exception.
      Returns:
      the value of the Nth lowest ranked object.
      See Also:
    • selectRankedIndex

      public int selectRankedIndex(Comparator<T> comparator, int kthLowest)
      Parameters:
      comparator - used for comparison
      kthLowest - rank of desired object according to comparison, n is based on ordinal numbers, not array indices. for min value use 1, for max value use size of array, using 0 results in runtime exception.
      Returns:
      the index of the Nth lowest ranked object.
      See Also:
    • reverse

      public Seq<T> reverse()
    • shuffle

      public Seq<T> shuffle()
    • truncate

      public void truncate(int newSize)
      Reduces the size of the array to the specified size. If the array is already smaller than the specified size, no action is taken.
    • random

      public T random(Rand rand)
    • random

      public T random()
      Returns a random item from the array, or null if the array is empty.
    • random

      public T random(T exclude)
      Returns a random item from the array, excluding the specified element. If the array is empty, returns null. If this array only has one element, returns that element.
    • toArray

      public T[] toArray()
      Returns the items as an array. Note the array is typed, so the Seq(Class) constructor must have been used. Otherwise use toArray(Class) to specify the array type.
    • toArray

      public <V> V[] toArray(Class type)
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • equals

      public boolean equals(Object object)
      Overrides:
      equals in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • toString

      public String toString(String separator, Func<T,String> stringifier)
    • toString

      public String toString(String separator)
    • iterator

      public Iterator<T> iterator()
      Returns an iterator for the items in the array. Remove is supported. Note that the same iterator instance is returned each time this method is called, unless you are using nested loops. Never, ever access this iterator's method manually, e.g. hasNext()/next(). Note that calling 'break' while iterating will permanently clog this iterator, falling back to an implementation that allocates new ones.
      Specified by:
      iterator in interface Iterable<T>