Good day. I just realized that it's better to implement arrays in C++ such as shown below:
index = [(i*height*depth) + (j*depth) + k]
However, I am still new to this implementation, and I wanted to ask your advice how will I implement this?
For example, if I have a set of values:
1 2 3
4 5 6
7 8 9
Wherein:
1 is at [0][0] index
2 is at [0][1] index
3 is at [0][2] index
4 is at [1][0] index
5 is at [1][1] index
6 is at [1][2] index
7 is at [2][0] index
8 is at [2][1] index
9 is at [2][2] index
How can I effectively utilize the [(i*height*depth) + (j*depth) + k] to completely access the values above?
I highly recommend a thin class that has the dimensions and wraps this in an inline function that does the math for you so you can just have
thing.set(row,col, value);
I don't know if there is a way to overload an operator to make this cleaner. Probably.
note that for arrays, it does not matter:
x[10][20] IS a solid block of bytes, you can collapse it to 1d with pointer magic (type * oned = & x[0][0]).
the issue is dynamic arrays and vectors that get a block for each row.
type ** ugly;
ugly = new type*[size]; //one block of memory.
for(i=... size)
ugly[i] = new type[size2];//multiple blocks of memory in random places in ram.
this is fragmented and causes page faults and problems and can't be directly iterated to touch all items and is a performance hit for large data.
thing.set(row,col, value);
I don't know if there is a way to overload an operator to make this cleaner. Probably.
You can do it with a helper class. think.operator[] returns an instance of the helper. Helper.operator[] returns the data that you really want. This lets you say thing[row][col]. Here's a simplified example.