Class MultidimensionalCounter
- java.lang.Object
-
- org.apache.commons.numbers.arrays.MultidimensionalCounter
-
public final class MultidimensionalCounter extends java.lang.Object
Converter between unidimensional storage structure and multidimensional conceptual structure. This utility will convert from indices in a multidimensional structure to the corresponding index in a one-dimensional array. For example, assuming that the ranges (in 3 dimensions) of indices are 2, 4 and 3, the following correspondences, between 3-tuples indices and unidimensional indices, will hold:- (0, 0, 0) corresponds to 0
- (0, 0, 1) corresponds to 1
- (0, 0, 2) corresponds to 2
- (0, 1, 0) corresponds to 3
- ...
- (1, 0, 0) corresponds to 12
- ...
- (1, 3, 2) corresponds to 23
-
-
Field Summary
Fields Modifier and Type Field Description private int
dimension
Number of dimensions.private int
last
Index of last dimension.private int[]
size
Counter sizes.private int
totalSize
Total number of (one-dimensional) slots.private int[]
uniCounterOffset
Offset for each dimension.
-
Constructor Summary
Constructors Modifier Constructor Description private
MultidimensionalCounter(int... size)
Creates a counter.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private static void
checkStrictlyPositive(java.lang.String name, int size)
Check the size is strictly positive:size > 0
.private static java.lang.String
createIndexOutOfBoundsMessage(int size, int index)
Creates the message for the index out of bounds exception.int
getDimension()
Gets the number of dimensions of the multidimensional counter.int
getSize()
Gets the total number of elements.int[]
getSizes()
Gets the number of multidimensional counter slots in each dimension.static MultidimensionalCounter
of(int... size)
Creates a counter.int[]
toMulti(int index)
Converts to a multidimensional counter.java.lang.String
toString()
int
toUni(int... c)
Converts to a unidimensional counter.
-
-
-
Field Detail
-
dimension
private final int dimension
Number of dimensions.
-
uniCounterOffset
private final int[] uniCounterOffset
Offset for each dimension.
-
size
private final int[] size
Counter sizes.
-
totalSize
private final int totalSize
Total number of (one-dimensional) slots.
-
last
private final int last
Index of last dimension.
-
-
Method Detail
-
of
public static MultidimensionalCounter of(int... size)
Creates a counter.- Parameters:
size
- Counter sizes (number of slots in each dimension).- Returns:
- a new instance.
- Throws:
java.lang.IllegalArgumentException
- if one of the sizes is negative or zero.
-
getDimension
public int getDimension()
Gets the number of dimensions of the multidimensional counter.- Returns:
- the number of dimensions.
-
toMulti
public int[] toMulti(int index)
Converts to a multidimensional counter.- Parameters:
index
- Index in unidimensional counter.- Returns:
- the multidimensional counts.
- Throws:
java.lang.IndexOutOfBoundsException
- ifindex
is not between0
and the value returned bygetSize()
(excluded).
-
toUni
public int toUni(int... c)
Converts to a unidimensional counter.- Parameters:
c
- Indices in multidimensional counter.- Returns:
- the index within the unidimensionl counter.
- Throws:
java.lang.IllegalArgumentException
- if the size ofc
does not match the size of the array given in the constructor.java.lang.IndexOutOfBoundsException
- if a value ofc
is not in the range of the corresponding dimension, as defined in theconstructor
.
-
getSize
public int getSize()
Gets the total number of elements.- Returns:
- the total size of the unidimensional counter.
-
getSizes
public int[] getSizes()
Gets the number of multidimensional counter slots in each dimension.- Returns:
- the number of slots in each dimension.
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
checkStrictlyPositive
private static void checkStrictlyPositive(java.lang.String name, int size)
Check the size is strictly positive:size > 0
.- Parameters:
name
- the name of the sizesize
- the size
-
createIndexOutOfBoundsMessage
private static java.lang.String createIndexOutOfBoundsMessage(int size, int index)
Creates the message for the index out of bounds exception.- Parameters:
size
- the sizeindex
- the index- Returns:
- the message
-
-