Class FastArray<T>

  • All Implemented Interfaces:
    java.lang.Iterable<T>, java.util.Collection<T>, java.util.List<T>

    public class FastArray<T>
    extends java.lang.Object
    implements java.lang.Iterable<T>, java.util.List<T>
    Simple
    unsynchronized
    array. The array can only grow, but never shrinks (no arraycopy()). Elements are removed by nulling them. A size variable is maintained for quick size() / isEmpty().
    Since:
    5.2
    Author:
    Bela Ban
    • Constructor Summary

      Constructors 
      Constructor Description
      FastArray()  
      FastArray​(int capacity)  
      FastArray​(java.util.Collection<? extends T> c)  
      FastArray​(T[] elements, int index)  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void add​(int idx, T el)  
      boolean add​(T el)  
      boolean add​(T el, boolean resize)  
      boolean addAll​(int idx, java.util.Collection<? extends T> c)  
      boolean addAll​(java.util.Collection<? extends T> list)  
      boolean addAll​(FastArray<T> fa)  
      boolean addAll​(FastArray<T> fa, boolean resize)  
      boolean addAll​(T... els)  
      boolean addAll​(T[] els, int length)
      Adds elements from an array els to this array
      boolean anyMatch​(java.util.function.Predicate<T> pred)  
      int capacity()  
      protected int checkIndex​(int idx)  
      void clear()  
      FastArray<T> clear​(boolean null_elements)  
      boolean contains​(java.lang.Object o)  
      boolean containsAll​(java.util.Collection<?> c)  
      int count()
      Returns the number of non-null elements, should have the same result as size().
      boolean equals​(java.lang.Object obj)  
      protected boolean equalsArrayList​(FastArray<?> other)  
      protected boolean equalsRange​(java.util.List<?> other, int from, int to)  
      T get​(int idx)  
      int increment()  
      FastArray<T> increment​(int i)  
      int index()  
      int indexOf​(java.lang.Object o)  
      boolean isEmpty()  
      FastArray.FastIterator iterator()
      Iterator which iterates only over non-null elements, skipping null elements
      FastArray.FastIterator iterator​(java.util.function.Predicate<T> filter)
      Iterates over all non-null elements which match filter
      int lastIndexOf​(java.lang.Object o)  
      java.util.ListIterator<T> listIterator()  
      java.util.ListIterator<T> listIterator​(int index)  
      java.lang.String print()  
      protected java.lang.String print​(int limit)  
      int printLimit()  
      FastArray<T> printLimit​(int l)  
      T remove​(int idx)  
      boolean remove​(java.lang.Object o)  
      boolean removeAll​(java.util.Collection<?> c)  
      T removeFirst()
      Removes the first non-null element starting at index 0
      FastArray<T> removeIf​(java.util.function.Predicate<T> filter, boolean replace_all)  
      T removeLast()  
      FastArray<T> replaceIf​(java.util.function.Predicate<T> filter, T new_el, boolean replace_all)
      Replaces any or all elements matching filter with a new element
      FastArray<T> resize​(int new_capacity)  
      boolean retainAll​(java.util.Collection<?> c)  
      T set​(int idx, T el)  
      FastArray<T> set​(T[] elements)  
      int size()  
      java.util.stream.Stream<T> stream()  
      java.util.List<T> subList​(int fromIndex, int toIndex)  
      java.lang.Object[] toArray()  
      <T1> T1[] toArray​(T1[] a)  
      java.lang.String toString()  
      int transferFrom​(FastArray<T> other, boolean clear)
      Copies the messages from the other array into this one,
      FastArray<T> trimTo​(int new_capacity)
      Attempts to reduce the current capacity to new_capacity
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
      • Methods inherited from interface java.util.Collection

        parallelStream, removeIf, toArray
      • Methods inherited from interface java.lang.Iterable

        forEach
      • Methods inherited from interface java.util.List

        hashCode, replaceAll, sort, spliterator
    • Field Detail

      • elements

        protected T[] elements
      • index

        protected int index
      • size

        protected int size
      • increment

        protected int increment
      • print_limit

        protected int print_limit
    • Constructor Detail

      • FastArray

        public FastArray()
      • FastArray

        public FastArray​(int capacity)
      • FastArray

        public FastArray​(T[] elements,
                         int index)
      • FastArray

        public FastArray​(java.util.Collection<? extends T> c)
    • Method Detail

      • capacity

        public int capacity()
      • index

        public int index()
      • size

        public int size()
        Specified by:
        size in interface java.util.Collection<T>
        Specified by:
        size in interface java.util.List<T>
      • isEmpty

        public boolean isEmpty()
        Specified by:
        isEmpty in interface java.util.Collection<T>
        Specified by:
        isEmpty in interface java.util.List<T>
      • increment

        public int increment()
      • increment

        public FastArray<T> increment​(int i)
      • printLimit

        public int printLimit()
      • printLimit

        public FastArray<T> printLimit​(int l)
      • add

        public boolean add​(T el)
        Specified by:
        add in interface java.util.Collection<T>
        Specified by:
        add in interface java.util.List<T>
      • add

        public boolean add​(T el,
                           boolean resize)
      • add

        public void add​(int idx,
                        T el)
        Specified by:
        add in interface java.util.List<T>
      • addAll

        public boolean addAll​(T[] els,
                              int length)
        Adds elements from an array els to this array
        Parameters:
        els - The other array, can have null elements
        length - The number of elements to add. must be <= els.length
        Returns:
        The number of elements added
      • addAll

        @SafeVarargs
        public final boolean addAll​(T... els)
      • addAll

        public boolean addAll​(java.util.Collection<? extends T> list)
        Specified by:
        addAll in interface java.util.Collection<T>
        Specified by:
        addAll in interface java.util.List<T>
      • addAll

        public boolean addAll​(FastArray<T> fa)
      • addAll

        public boolean addAll​(FastArray<T> fa,
                              boolean resize)
      • addAll

        public boolean addAll​(int idx,
                              java.util.Collection<? extends T> c)
        Specified by:
        addAll in interface java.util.List<T>
      • transferFrom

        public int transferFrom​(FastArray<T> other,
                                boolean clear)
        Copies the messages from the other array into this one,
        including
        null elements (using System.arraycopy(Object, int, Object, int, int). This is the same as calling clear(boolean) followed by addAll(FastArray, boolean), but faster.
        Parameters:
        other - The other array
        clear - Clears the other array after the transfer when true
        Returns:
        The number of non-null elements transferred from other
      • contains

        public boolean contains​(java.lang.Object o)
        Specified by:
        contains in interface java.util.Collection<T>
        Specified by:
        contains in interface java.util.List<T>
      • containsAll

        public boolean containsAll​(java.util.Collection<?> c)
        Specified by:
        containsAll in interface java.util.Collection<T>
        Specified by:
        containsAll in interface java.util.List<T>
      • equals

        public boolean equals​(java.lang.Object obj)
        Specified by:
        equals in interface java.util.Collection<T>
        Specified by:
        equals in interface java.util.List<T>
        Overrides:
        equals in class java.lang.Object
      • indexOf

        public int indexOf​(java.lang.Object o)
        Specified by:
        indexOf in interface java.util.List<T>
      • lastIndexOf

        public int lastIndexOf​(java.lang.Object o)
        Specified by:
        lastIndexOf in interface java.util.List<T>
      • anyMatch

        public boolean anyMatch​(java.util.function.Predicate<T> pred)
      • get

        public T get​(int idx)
        Specified by:
        get in interface java.util.List<T>
      • set

        public T set​(int idx,
                     T el)
        Specified by:
        set in interface java.util.List<T>
      • remove

        public T remove​(int idx)
        Specified by:
        remove in interface java.util.List<T>
      • remove

        public boolean remove​(java.lang.Object o)
        Specified by:
        remove in interface java.util.Collection<T>
        Specified by:
        remove in interface java.util.List<T>
      • removeFirst

        public T removeFirst()
        Removes the first non-null element starting at index 0
      • removeLast

        public T removeLast()
      • removeAll

        public boolean removeAll​(java.util.Collection<?> c)
        Specified by:
        removeAll in interface java.util.Collection<T>
        Specified by:
        removeAll in interface java.util.List<T>
      • removeIf

        public FastArray<T> removeIf​(java.util.function.Predicate<T> filter,
                                     boolean replace_all)
      • replaceIf

        public FastArray<T> replaceIf​(java.util.function.Predicate<T> filter,
                                      T new_el,
                                      boolean replace_all)
        Replaces any or all elements matching filter with a new element
        Parameters:
        filter - The filter, must ne non-null or no replacements will take place
        new_el - The new element, can be null
        replace_all - When false, the method returns after the first match (if any). Otherwise, all matching elements are replaced
      • retainAll

        public boolean retainAll​(java.util.Collection<?> c)
        Specified by:
        retainAll in interface java.util.Collection<T>
        Specified by:
        retainAll in interface java.util.List<T>
      • clear

        public void clear()
        Specified by:
        clear in interface java.util.Collection<T>
        Specified by:
        clear in interface java.util.List<T>
      • clear

        public FastArray<T> clear​(boolean null_elements)
      • trimTo

        public FastArray<T> trimTo​(int new_capacity)
        Attempts to reduce the current capacity to new_capacity
        Parameters:
        new_capacity - The new capacity. If greater than the current capacity, this will be a no-op. If smaller than the current size, the current size will be taken instead as new capacity.
        Returns:
      • iterator

        public FastArray.FastIterator iterator()
        Iterator which iterates only over non-null elements, skipping null elements
        Specified by:
        iterator in interface java.util.Collection<T>
        Specified by:
        iterator in interface java.lang.Iterable<T>
        Specified by:
        iterator in interface java.util.List<T>
      • iterator

        public FastArray.FastIterator iterator​(java.util.function.Predicate<T> filter)
        Iterates over all non-null elements which match filter
      • stream

        public java.util.stream.Stream<T> stream()
        Specified by:
        stream in interface java.util.Collection<T>
      • listIterator

        public java.util.ListIterator<T> listIterator()
        Specified by:
        listIterator in interface java.util.List<T>
      • listIterator

        public java.util.ListIterator<T> listIterator​(int index)
        Specified by:
        listIterator in interface java.util.List<T>
      • subList

        public java.util.List<T> subList​(int fromIndex,
                                         int toIndex)
        Specified by:
        subList in interface java.util.List<T>
      • toArray

        public java.lang.Object[] toArray()
        Specified by:
        toArray in interface java.util.Collection<T>
        Specified by:
        toArray in interface java.util.List<T>
      • toArray

        public <T1> T1[] toArray​(T1[] a)
        Specified by:
        toArray in interface java.util.Collection<T>
        Specified by:
        toArray in interface java.util.List<T>
      • count

        public int count()
        Returns the number of non-null elements, should have the same result as size(). Only used for testing!
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • print

        public java.lang.String print()
      • resize

        public FastArray<T> resize​(int new_capacity)
      • equalsArrayList

        protected boolean equalsArrayList​(FastArray<?> other)
      • equalsRange

        protected boolean equalsRange​(java.util.List<?> other,
                                      int from,
                                      int to)
      • print

        protected java.lang.String print​(int limit)
      • checkIndex

        protected int checkIndex​(int idx)