Class MultidimensionalCounter
java.lang.Object
org.apache.commons.numbers.arrays.MultidimensionalCounter
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
-
Method Summary
Modifier and TypeMethodDescriptionintGets the number of dimensions of the multidimensional counter.intgetSize()Gets the total number of elements.int[]getSizes()Gets the number of multidimensional counter slots in each dimension.static MultidimensionalCounterof(int... size) Creates a counter.int[]toMulti(int index) Converts to a multidimensional counter.toString()inttoUni(int... c) Converts to a unidimensional counter.
-
Method Details
-
of
Creates a counter.- Parameters:
size- Counter sizes (number of slots in each dimension).- Returns:
- a new instance.
- Throws:
IllegalArgumentException- if one of the sizes is negative or zero.
-
getDimension
Gets the number of dimensions of the multidimensional counter.- Returns:
- the number of dimensions.
-
toMulti
Converts to a multidimensional counter.- Parameters:
index- Index in unidimensional counter.- Returns:
- the multidimensional counts.
- Throws:
IndexOutOfBoundsException- ifindexis not between0and the value returned bygetSize()(excluded).
-
toUni
Converts to a unidimensional counter.- Parameters:
c- Indices in multidimensional counter.- Returns:
- the index within the unidimensionl counter.
- Throws:
IllegalArgumentException- if the size ofcdoes not match the size of the array given in the constructor.IndexOutOfBoundsException- if a value ofcis not in the range of the corresponding dimension, as defined in theconstructor.
-
getSize
Gets the total number of elements.- Returns:
- the total size of the unidimensional counter.
-
getSizes
Gets the number of multidimensional counter slots in each dimension.- Returns:
- the number of slots in each dimension.
-
toString
-