Hi all,
Plz clarify me, on the implementation of two or three dimensional
array using overloaded [ ] operator.
Thanks, in advance
Bangalore
Do you mean an actual array? Or an array-like Class?
Re. multidimensional built-in arrays, I made copious notes on
those a few years ago. For reference, I've included those notes
at the end of this message.
As for an array-like class, I think the best way to impliement
that might be using an actual multidimensional built-in array,
with added safety features (esp. bounds checking on indexes).
This would actually be easier and cleaner than using vectors,
I think.
I don't know if I'd recommend using an overloaded [] operator,
though. Trying to get MyArray[7][37][6] to work right would be
challenging to impliment. (Would almost force you to write
your array class recursively, so that a 3d MyArray is a 1d MyArray
of 2d MyArrays, etc.) Sounds messy.
The application operator() might work better for you. That way
you'd use just ONE operator to access an element of your array,
regardless of the number of dimensions:
dValue = MyArray(13,7,5,3); // (example for 4d Array)
Also consider templatizing you class for different types, since
it's basically just a container. You might also pass the
number of dimensions as a template parameter, and pass the
dimensions as arguments to a constructor. Something like this:
MyArray<double, 3> Fred (13, 7, 7); // 3d array of doubles, 13x7x7
Postscript: What follows are some very lengthy detailed
notes on mulitdimensionally arrays I made some years ago,
when I was first learning C. Anyone who would be bored
by this please stop reading here. You've been warned.
Wednesday July 4, 2001:
I've been thinking-about and experimenting-with arrays today. I've
learned that the nomenclature:
int array[5][7][11];
means: "an array of 5 elements, each of which is an array of 7 elements,
each of which is an array of 11 elements, each of which is an integer".
That is pretty much backwards from how I had thought that arrays work!
In memory, a [3][4] array is stored in a contiguous block, in this
order:
[0][0] [0][1] [0][2] [0][3]
[1][0] [1][1] [1][2] [1][3]
[2][0] [2][1] [2][2] [2][3]
Another way to look at multi-dimensional arrays is that they are
multi-level n-ary trees. A [l][m][n] array is a tree with one trunk,
l limbs, m branches per limb, and n twigs per branch. So, element
array[3][5][7] is (from left to right): limb 3, branch 5, twig 7;
or (from right to left): twig 7 of branch 5 of limb 3 of the array.
Behind the scenes, I believe element address for an array such as:
int Array[Size1][Size2][Size3];
is implimented by pointers, like so:
Array[x][y][z] =
*(&(Array[0][0][0]) + x*Size2*Size3 + y*Size3 + z);
All sizes except for the first are needed to calculate the RAM address of each element.
(
And still deeper behind the scenes, at the assembly level, we need to know the size of
the actual elements (this is, sizeof(int)), so that we can translate this C statement:
IntPtr += 4; /* step forward over 4 "int" elements */
Into the equivalent pseudo-machine-language statement:
BytePtr += 16 /* step forward over 16 bytes */
In other words,
MachLangArray[x][y][z] =
*
(
sizeof(int)
*
(
&(Array[0][0][0])
+ x*Size2*Size3
+ y*Size3
+ z
)
);
)
This is why, when passing n-dimensional arrays to functions, all but the first size
number must be passed:
int MyArray[3][16][6][82];
/* ... */
QuidBlitz = GurzBlunkt(Fidgit, MyArray[][16][6][82]);
The first number is not needed, because the RAM address of any one element is independent
of the number of items in the top-level grouping. (This is because we don't need to pass
over the end boundary of the top-level grouping, but we DO pass over end-boundaries
for all the other grouping levels, so we need to know how long they all are in order to
calculate the RAM addresses in bytes.)
Cheers,
Robbie Hatley
Tustin, CA, USA
email: lonewolfintj at pacbell dot net
web: home dot pacbell dot net slant earnur slant