Class MultiDimensionalArray<T>

java.lang.Object
java.util.AbstractCollection<T>
dk.jonaslindstrom.ruffini.common.util.MultiDimensionalArray<T>
Type Parameters:
T - The type of objects stored in the array.
All Implemented Interfaces:
Iterable<T>, Collection<T>

public abstract class MultiDimensionalArray<T> extends AbstractCollection<T>
A multi-dimensional array is a data collection where entries are indexed by a fixed length vector (the length equals the dimension of the array). The elements are stored recursively using nested instances ArrayList's, so the complexity of get and set operations are linear in the depth of the array.
  • Constructor Details

    • MultiDimensionalArray

      public MultiDimensionalArray()
  • Method Details

    • build

      public static <S> MultiDimensionalArray<S> build(int width, java.util.function.IntFunction<MultiDimensionalArray<S>> populator)
      Create a new MultiDimensionalArray.
      Type Parameters:
      S - The type of objects stored in the array.
      Parameters:
      width - The width of this array in the first dimension.
      populator - A function supplying new MultiDimensionalArrays of dimension one less than the resulting array. The function should return arrays for input 0,1,...,width-1.
      Returns:
      A new MultiDimensionalArray generated using the given populator.
    • sparse

      public static <S> MultiDimensionalArray<S> sparse(List<Integer> shape)
    • build

      public static <S> MultiDimensionalArray<S> build(List<S> entries)
      Create a new one-dimensional array with the given entries.
    • build

      public static <S> MultiDimensionalArray<S> build(List<Integer> shape, java.util.function.Function<List<Integer>,S> populator)
      Create a new multi-dimensional array with the given shape and with each entry being generated by the populator function.
    • get

      public abstract T get(List<Integer> index) throws IndexOutOfBoundsException
      Get the element in this array with the given index vector.
      Throws:
      IndexOutOfBoundsException
    • get

      public T get(Integer... index) throws IndexOutOfBoundsException
      Get the element in this array with the given index vector.
      Throws:
      IndexOutOfBoundsException
    • get

      public T get(int[] index) throws IndexOutOfBoundsException
      Get the element in this array with the given index vector.
      Throws:
      IndexOutOfBoundsException
    • set

      public abstract void set(List<Integer> index, T value) throws IndexOutOfBoundsException
      Set a new value for the given index vector.
      Throws:
      IndexOutOfBoundsException
    • set

      public void set(int[] index, T value) throws IndexOutOfBoundsException
      Set a new value for the given index vector.
      Throws:
      IndexOutOfBoundsException
    • getShape

      public abstract List<Integer> getShape()
      Get the shape of this array.
    • getDimension

      public abstract int getDimension()
      Get the dimension of this array.
    • project

      public MultiDimensionalArray<T> project(java.util.function.Function<List<T>,T> projection)
      Project this array into an array of dimension d-1 using the given projection function.
    • map

      public <S> MultiDimensionalArray<S> map(java.util.function.Function<T,S> function)
      Return a new MultiDimensionalArray of the same size as this, using the given function to map from the elements of this array to the corresponding entry in the new array
    • forEachWithIndices

      public void forEachWithIndices(java.util.function.BiConsumer<T,List<Integer>> consumer)
      Perform an operation on all elements