#define UINT unsigned int

const int Width = 3000;

const int Height = 2000;

UINT Array01[Width][Height];

ArrayType2D Array02;

UINT Array03[Width][Height];

class ArrayType2D {

private:

UINT* Array;

int last_row;

public:

ArrayType2D(){ Array = new UINT [Width * Height]; };

~ArrayType2D(){ delete [] Array; };

UINT operator[](int N){ return Array[N]; };

void SetRow(int ROW) { last_row = ROW * Width; };

UINT GetPixel(int ROW, int COL){ return Array[(ROW * Width) + COL]; }

UINT GetPixel(int COL){ return Array[last_row + COL]; }

void SetPixel(int COL, UINT DATA){ Array[last_row + COL] = DATA; }

void SetPixel(int ROW, int COL, UINT DATA){ Array[(ROW * Width) + COL] = DATA; }

};

void Test04(int HEIGHT, int WIDTH) {

for (int ROW = 0; ROW < HEIGHT; ROW++) {

Array02.SetRow(ROW);

for (int COL = 0; COL < WIDTH; COL++)

Array03[ROW][COL] = Array02.GetPixel(COL);

}

}

The above code accesses a dynamic two-dimensional array

about fifteen percent faster than a conventional two-dimensional

array is accessed, if the data is to be accessed in the order

specified, moving through all the columns in a row, and then

moving to the next row. If the data is to accessed in a different

order such as moving through all the rows, and then moving to

the next column, the above code would need to be adapted.

Also one must make sure that the data is stored in the single

dimension array in the order corresponding to this different

access order.

Now if I could only have the following function

UINT GetPixel(int ROW, int COL){ return Array[(ROW * Width) + COL]; }

work with conventional Array02[ROW][COL] syntax, and find a way

to make it at least as fast as conventional array access (right now it is only

57% as fast), then I would be happier.

"Gianni Mariani" <(E-Mail Removed)> wrote in message news:(E-Mail Removed) t...

> Peter Olcott wrote:

>> I need to know how to get the solution mentioned below to work. The

>> solution is from gbayles Jan 29 2001, 12:50 pm, link is provided below:

>>

>>

>>>http://groups.google.com/group/comp....c43260a5310?hl

>>>

>>>Another way is to create a one dimensional array and handle the

>>>indexing yourself (index = row * row_size + col). This is readily

>>>implemented in template classes that can create dynamically allocated

>>>multi-dimensional arrays of any element type and number of

>>>dimensions.

>>

>>

>> What would be the syntax for created a template that allowed a

>> one dimensional dynamic array, to be addressed using the conventional

>> syntax for accessing a two dimensional array? I am thinking that this

>> must be some sort of operator[] overloading.

>

> http://groups.google.com/group/comp....4f1d9bd?hl=en&

>

> I knew I answered this once before - I think there is an FAQ as well..

>

> Here is the example code from that posting.

>

> #include <vector>

>

> template <typename w_elem_type>

> class matrix

> {

> public:

> typedef int t_Size;

>

> t_Size m_columns;

> t_Size m_rows;

>

> std::vector<w_elem_type> m_data;

>

> matrix( t_Size i_columns = 0, t_Size i_rows = 0 )

> : m_columns( i_columns ),

> m_rows( i_rows ),

> m_data( i_columns * i_rows )

> {

> }

>

> w_elem_type * operator[]( t_Size i_index )

> {

> return & ( m_data[ i_index * m_rows ] );

> }

>

> template <typename w_Type, int w_columns, int w_rows>

> matrix( const w_Type (&i_array)[w_columns][w_rows] )

> : m_columns( w_columns ),

> m_rows( w_rows ),

> m_data( & (i_array[0][0]), & (i_array[w_columns-1][w_rows]) )

> {

> }

>

> };

>

> #include <iostream>

>

> double array[3][4] = {

> { 1.0, 2.0, 3.3, 4.4 },

> { 1.0, 2.0, 3.3, 4.4 },

> { 1.0, 2.0, 3.3, 4.5 },

>

> };

>

> int main()

> {

> matrix<float> mat1( 3, 4 );

> matrix<float> mat2;

> matrix<float> mat3( array );

>

> mat2 = mat3;

>

> std::cout << mat2[2][3] << "\n";

>

> }