From: Larry Evans Date: 2011-01-08T13:00:23+09:00 Subject: Re: Nooby question : multidimensional arrays. On 01/07/11 17:56, Kedar Mhaswade wrote: > Is this an okay job of implementing a multidimensional array? > > https://github.com/kedarmhaswade/datacube/blob/master/data_cube.rb > > Thank you for any feedback. > > -Kedar > There's a book: http://web.engr.oregonstate.edu/~budd/Books/aplc/ which describes an expansion vector which is a scan of the array sizes. IOW, for: arr = Array.new(s0,s1,...sn) the expansion vector for this arr is: arr.ev = [1, s0, s0*s1, s0*s1*s3,..., s0*s1*...*sn] The i-th element of arr.ev is the distance between successive elements in the i-th dimension. IOW: the locatoin of: arr[i0,i1,..., ij, ... in] is located arr.ev[j] elements from: arr[i0,i1,..., ij+1, ... in] IOW: arr[i0,i1,..., ij, ... in] is located: i0*arr.ev[0]+i1*arr.ev[1]+...+in*arr.ev[n] elements from the 1st element: arr[0,0,...,0] Thus, given the sizes, you can create a member variable which is the expansion vector, then use that to access the elements by calculating the offset from the initial element using the dot product: i0*arr.ev[0]+i1*arr.ev[1]+...+in*arr.ev[n] HTH. Good luck.