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>
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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <S> MultiDimensionalArray<S>
build
(int width, java.util.function.IntFunction<MultiDimensionalArray<S>> populator) Create a new MultiDimensionalArray.static <S> MultiDimensionalArray<S>
Create a new multi-dimensional array with the given shape and with each entry being generated by the populator function.static <S> MultiDimensionalArray<S>
Create a new one-dimensional array with the given entries.void
forEachWithIndices
(java.util.function.BiConsumer<T, List<Integer>> consumer) Perform an operation on all elementsget
(int[] index) Get the element in this array with the given index vector.Get the element in this array with the given index vector.abstract T
Get the element in this array with the given index vector.abstract int
Get the dimension of this array.getShape()
Get the shape of this array.<S> MultiDimensionalArray<S>
Return a newMultiDimensionalArray
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 arrayProject this array into an array of dimension d-1 using the given projection function.void
Set a new value for the given index vector.abstract void
Set a new value for the given index vector.static <S> MultiDimensionalArray<S>
Methods inherited from class java.util.AbstractCollection
add, addAll, clear, contains, containsAll, isEmpty, iterator, remove, removeAll, retainAll, size, toArray, toArray, toString
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.util.Collection
equals, hashCode, parallelStream, removeIf, spliterator, stream, toArray
-
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
-
build
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
Get the element in this array with the given index vector.- Throws:
IndexOutOfBoundsException
-
get
Get the element in this array with the given index vector.- Throws:
IndexOutOfBoundsException
-
get
Get the element in this array with the given index vector.- Throws:
IndexOutOfBoundsException
-
set
Set a new value for the given index vector.- Throws:
IndexOutOfBoundsException
-
set
Set a new value for the given index vector.- Throws:
IndexOutOfBoundsException
-
getShape
Get the shape of this array. -
getDimension
public abstract int getDimension()Get the dimension of this array. -
project
Project this array into an array of dimension d-1 using the given projection function. -
map
Return a newMultiDimensionalArray
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
Perform an operation on all elements
-