Data Structures and Algorithms with Object-Oriented Design Patterns in Ruby
next up previous index

An Implementation

 

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

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 tex2html_wrap_inline59677 is the size of the tex2html_wrap_inline57049 dimension ( tex2html_wrap_inline59681).

The second instance attribute, @factors, is also an array of length n. The tex2html_wrap_inline59685 element of the @factors array corresponds to the factor tex2html_wrap_inline59665 given by Equation gif.

The third instance attribute, @data, is a one-dimensional array used to hold the elements of the multi-dimensional array in row-major order.


next up previous index

Bruno Copyright © 2004 by Bruno R. Preiss, P.Eng. All rights reserved.