org.apache.commons.math.util

## Class MultidimensionalCounter

- java.lang.Object
- - org.apache.commons.math.util.MultidimensionalCounter

All Implemented Interfaces:
  java.lang.Iterable<java.lang.Integer>

    public class MultidimensionalCounter
    extends java.lang.Object
    implements java.lang.Iterable<java.lang.Integer>

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

Since:
  2.2
Version:
  $Revision$ $Date$

### Nested Class Summary

Nested Classes
`class` `MultidimensionalCounter.Iterator` Perform iteration over the multidimensional counter.

### Field Summary

Fields
`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
Constructor and Description
`MultidimensionalCounter(int... size)` Create a counter.

### Method Summary

Methods
`private int[]` `copyOf(int[] source, int newLen)` Java 1.5 does not support Arrays.copyOf()
`int` `getCount(int... c)` Convert to unidimensional counter.
`int[]` `getCounts(int index)` Convert to multidimensional counter.
`int` `getDimension()` Get the number of dimensions of the multidimensional counter.
`int` `getSize()` Get the total number of elements.
`int[]` `getSizes()` Get the number of multidimensional counter slots in each dimension.
`MultidimensionalCounter.Iterator` `iterator()` Create an iterator over this counter.
`java.lang.String` `toString()`

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`

### 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.

### Constructor Detail

#### MultidimensionalCounter

    public MultidimensionalCounter(int... size)

Create a counter.

Parameters:
  `size` - Counter sizes (number of slots in each dimension).
Throws:
  `NotStrictlyPositiveException` - if one of the sizes is negative or zero.

### Method Detail

#### iterator

    public MultidimensionalCounter.Iterator iterator()

Create an iterator over this counter.

Specified by:
  `iterator` in interface `java.lang.Iterable<java.lang.Integer>`
Returns:
  the iterator.

#### getDimension

    public int getDimension()

Get the number of dimensions of the multidimensional counter.

Returns:
  the number of dimensions.

#### getCounts

    public int[] getCounts(int index)

Convert to multidimensional counter.

Parameters:
  `index` - Index in unidimensional counter.
Returns:
  the multidimensional counts.
Throws:
  `OutOfRangeException` - if `index` is not between `0` and the value returned by `getSize()` (excluded).

#### getCount

    public int getCount(int... c)
                 throws OutOfRangeException

Convert to unidimensional counter.

Parameters:
  `c` - Indices in multidimensional counter.
Returns:
  the index within the unidimensionl counter.
Throws:
  `DimensionMismatchException` - if the size of `c` does not match the size of the array given in the constructor.
  `OutOfRangeException` - if a value of `c` is not in the range of the corresponding dimension, as defined in the `constructor`.

#### getSize

    public int getSize()

Get the total number of elements.

Returns:
  the total size of the unidimensional counter.

#### getSizes

    public int[] getSizes()

Get the number of multidimensional counter slots in each dimension.

Returns:
  the sizes of the multidimensional counter in each dimension.

#### toString

    public java.lang.String toString()

Overrides:
  `toString` in class `java.lang.Object`

#### copyOf

    private int[] copyOf(int[] source,
               int newLen)

Java 1.5 does not support Arrays.copyOf()

Parameters:
  `source` - the array to be copied
  `newLen` - the length of the copy to be returned
Returns:
  the copied array, truncated or padded as necessary.