|
Data Structures and Algorithms
with Object-Oriented Design Patterns in Ruby |
In this section we illustrate the implementation of a
multi-dimensional array using a one-dimensional array.
We do this by defining a class called MultiDimensionalArray
that is very similar to the Array class
defined in Section
.
Altogether three instance attributes are used
to implement the MultiDimensionalArray class.
The first, @dimensions is an array of length n,
where n is number of dimensions
and
is the size of the
dimension (
).
The second instance attribute, @factors,
is also an array of length n.
The
element of the @factors array
corresponds to the factor
given by Equation
.
The third instance attribute, @data, is a one-dimensional array used to hold the elements of the multi-dimensional array in row-major order.