C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Two-Dimensional Dynamic Arrays
Goto page 1, 2, 3, 4, 5  Next
 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Peter Olcott
Guest





PostPosted: Fri Nov 25, 2005 2:19 am    Post subject: Two-Dimensional Dynamic Arrays Reply with quote



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:

Quote:
http://groups.google.com/group/comp.lang.c++/msg/db577c43260a5310?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.

Thanks,


Peter Olcott



Back to top
Gianni Mariani
Guest





PostPosted: Fri Nov 25, 2005 3:21 am    Post subject: Re: Two-Dimensional Dynamic Arrays Reply with quote



Peter Olcott wrote:
Quote:
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.lang.c++/msg/db577c43260a5310?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.lang.c/msg/909f01d9b4f1d9bd?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";

}

Back to top
Peter Olcott
Guest





PostPosted: Fri Nov 25, 2005 4:31 am    Post subject: Re: Two-Dimensional Dynamic Arrays Reply with quote



That looks like an excellent solution. I need absolutely top performance.
You mentioned :

some extensive matrix libraries you could use
and ones that are very efficient if the dimensions
are known.

Could you provide me a link or other reference to these?
Thanks again for your top notch assistance.

"Gianni Mariani" <gi2nospam (AT) mariani (DOT) ws> wrote

Quote:
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.lang.c++/msg/db577c43260a5310?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.lang.c/msg/909f01d9b4f1d9bd?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 class matrix
{
public:
typedef int t_Size;

t_Size m_columns;
t_Size m_rows;

std::vector
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
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 matrix<float> mat2;
matrix<float> mat3( array );

mat2 = mat3;

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

}



Back to top
Peter Olcott
Guest





PostPosted: Fri Nov 25, 2005 4:42 am    Post subject: Re: Two-Dimensional Dynamic Arrays Reply with quote

Oh yeah, I only need very fast addressing of the elements
of the Matrix. I will likely be moving up or down matrix rows
about five times as often as moving across matrix columns.

"Gianni Mariani" <gi2nospam (AT) mariani (DOT) ws> wrote

Quote:
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.lang.c++/msg/db577c43260a5310?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.lang.c/msg/909f01d9b4f1d9bd?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 class matrix
{
public:
typedef int t_Size;

t_Size m_columns;
t_Size m_rows;

std::vector
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
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 matrix<float> mat2;
matrix<float> mat3( array );

mat2 = mat3;

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

}



Back to top
Gianni Mariani
Guest





PostPosted: Fri Nov 25, 2005 8:15 pm    Post subject: Re: Two-Dimensional Dynamic Arrays Reply with quote

Peter Olcott wrote:
Quote:
That looks like an excellent solution. I need absolutely top performance.
You mentioned :

some extensive matrix libraries you could use
and ones that are very efficient if the dimensions
are known.

Could you provide me a link or other reference to these?

Google "C++ matrix" gives a plethora of answers. Quite a while ago I
looked at some of these but I didn't get involved with the project so I
can't give you any real feedback.

What I mean by "dimensions are known" is "known at compile time". If
they are known at compile time, you don't need to perform dynamic memory
allocation and you can enable some usual compiler optimizations (like
loop unrolling).

For somthing like a 3D graphics library, this would be ideal since it
predominantly uses a 4x4 matrices (Homogeneous Coordinates) and 1x4
vectors and so much of the code can be unrolled and the compiler has a
much better chance of optimizing it.

See below, the matrix example now with the row and column sizes known.

template <typename w_elem_type, unsigned w_rows, unsigned w_columns>
class matrix
{
public:

typedef w_elem_type value_type;
static const unsigned m_columns = w_columns;
static const unsigned m_rows = w_rows;

value_type m_data[ m_rows ][ m_columns ];

typedef w_elem_type row_value_type[ m_columns ];

matrix()
: m_data()
{
}

row_value_type & operator[]( unsigned i_index )
{
return m_data[ i_index ];
}

template <typename w_elem_intype>
matrix(
const w_elem_intype (&i_array)[m_rows][m_columns]
)
{
copy_matrix( i_array );
}

template <typename w_elem_intype>
matrix(
const matrix<w_elem_intype, m_rows, m_columns > & i_array
)
{
copy_matrix( i_array.m_data );
}

template <typename w_elem_intype>
matrix & operator=(
const w_elem_intype (&i_array)[m_rows][m_columns]
)
{
copy_matrix( i_array );
return * this;
}

template <typename w_elem_intype>
matrix & operator=(
const matrix<w_elem_intype, m_rows, m_columns > & i_array
)
{
copy_matrix( i_array.m_data );
return * this;
}

private:

template <typename w_elem_intype>
void copy_matrix( w_elem_intype (&i_array)[m_rows][m_columns] )
{
w_elem_type * l_elem = & m_data[ 0 ][ 0 ];
w_elem_intype * l_from = & i_array[ 0 ][ 0 ];

for ( unsigned l_i = 0; l_i < m_columns * m_rows; ++ l_i )
{
l_elem[ l_i ] = l_from[ l_i ];
}
}

};



#include
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, 3, 4> mat1;
matrix<float, 3, 4> mat2;
matrix<float, 3, 4> mat3( array );

mat2 = mat3;

matrix<double, 3, 4> mat2d = mat2;
mat2d = mat3;

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

Back to top
Peter Olcott
Guest





PostPosted: Fri Nov 25, 2005 11:31 pm    Post subject: Re: Two-Dimensional Dynamic Arrays Reply with quote

I tested the performance of the code below and it took 150 ms,
as opposed to 4.5 ms for a conventional two dimensional array.
I am going to work on making a faster version tonight. I am
guessing that it might have to have a kludge interface:
void SetData(int ROW, int COL, UINT Data);
UINT GetData(int ROW, int COL);

"Gianni Mariani" <gi2nospam (AT) mariani (DOT) ws> wrote

Quote:
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.lang.c++/msg/db577c43260a5310?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.lang.c/msg/909f01d9b4f1d9bd?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 class matrix
{
public:
typedef int t_Size;

t_Size m_columns;
t_Size m_rows;

std::vector
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
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 matrix<float> mat2;
matrix<float> mat3( array );

mat2 = mat3;

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

}



Back to top
Peter Olcott
Guest





PostPosted: Sat Nov 26, 2005 1:29 pm    Post subject: Re: Two-Dimensional Dynamic Arrays Reply with quote

#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"
Quote:
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.lang.c++/msg/db577c43260a5310?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.lang.c/msg/909f01d9b4f1d9bd?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 class matrix
{
public:
typedef int t_Size;

t_Size m_columns;
t_Size m_rows;

std::vector
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
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 matrix<float> mat2;
matrix<float> mat3( array );

mat2 = mat3;

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

}



Back to top
Gianni Mariani
Guest





PostPosted: Sat Nov 26, 2005 8:56 pm    Post subject: Re: Two-Dimensional Dynamic Arrays Reply with quote

Peter Olcott wrote:
Quote:
#define UINT unsigned int
Use a typedef here - avoid macros where possible

typedef unsigned int UINT;
Quote:
const int Width = 3000;
const int Height = 2000;
these could be template parameters.


Quote:
UINT Array01[Width][Height];
ArrayType2D Array02;
UINT Array03[Width][Height];


class ArrayType2D {
private:
UINT* Array;
int last_row;
public:

// oops - using the default copy constructor - will cause
// delete [] Array to be called multiple times (and leak)
// if the array is ever copied. (same goes for assignment
// operator.)

Quote:
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.


Did you check out the last example I posted ? Since you know the size
of your array at compile time, you can use somthing like that. On a
good optimizing compiler, this one would be just about as fast at copies
as you can get.

Is the Test04 function the only test you want ?

SetRow buys you very little in terms of performance (as implemented).


Back to top
roberts.noah@gmail.com
Guest





PostPosted: Sat Nov 26, 2005 11:03 pm    Post subject: Re: Two-Dimensional Dynamic Arrays Reply with quote


Gianni Mariani wrote:

Quote:
w_elem_type * operator[]( t_Size i_index )
{
return & ( m_data[ i_index * m_rows ] );
}

This FAQ entry might be of interest:
http://www.parashift.com/c++-faq-lite/operator-overloading.html#faq-13.11

See also 13.10 right above it.


Back to top
Peter Olcott
Guest





PostPosted: Sat Nov 26, 2005 11:35 pm    Post subject: Re: Two-Dimensional Dynamic Arrays Reply with quote


"Gianni Mariani" <gi2nospam (AT) mariani (DOT) ws> wrote

Quote:
Peter Olcott wrote:
#define UINT unsigned int
Use a typedef here - avoid macros where possible
typedef unsigned int UINT;
const int Width = 3000;
const int Height = 2000;
these could be template parameters.

UINT Array01[Width][Height];
ArrayType2D Array02;
UINT Array03[Width][Height];


class ArrayType2D {
private:
UINT* Array;
int last_row;
public:

// oops - using the default copy constructor - will cause
// delete [] Array to be called multiple times (and leak)
// if the array is ever copied. (same goes for assignment
// operator.)

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.


Did you check out the last example I posted ? Since you know the size of your array at compile time, you can use somthing like
that. On a

I know it at run time, not compile time. I will take your other
suggestion (above) and repost an updated copy. The current
version seems to work just as fast as the build-in matrix, and
only lacks conventional double operator[] syntax~~~~>Data[row][col]
the next poster posted material that I will study on this. Thanks for all
your help.


Quote:
good optimizing compiler, this one would be just about as fast at copies as you can get.

Is the Test04 function the only test you want ?

SetRow buys you very little in terms of performance (as implemented).




Back to top
Peter Olcott
Guest





PostPosted: Sat Nov 26, 2005 11:50 pm    Post subject: Re: Two-Dimensional Dynamic Arrays Reply with quote


"Gianni Mariani" <gi2nospam (AT) mariani (DOT) ws> wrote

Quote:
// oops - using the default copy constructor - will cause
// delete [] Array to be called multiple times (and leak)
// if the array is ever copied. (same goes for assignment
// operator.)

//
// Array2D.h 2005-11-26 9:32 AM
//
#define UINT unsigned int
//
class ArrayType2D {
private:
int Size;
int Width;
int Height;
UINT* Array;
bool Allocated;
void Copy(ArrayType2D& Array2);
public:
ArrayType2D();
ArrayType2D(ArrayType2D& Array2) { Copy(Array2); };
~ArrayType2D() { if (Allocated) delete [] Array; };
UINT operator[](int N) { return Array[N]; };
ArrayType2D& operator=(ArrayType2D& Array2) { Copy(Array2); return *this; };
UINT* operator&() { return Array; };
UINT Get(int ROW, int COL) { return Array[ROW * Width + COL]; };
void Set(int ROW, int COL, UINT DATA) { Array[ROW * Width + COL] = DATA; }
const int size() { return Size; };
const int width() { return Width; };
const int height() { return Height; };
void Allocate(int Height, int Width);
};

void ArrayType2D::Copy(ArrayType2D& Array2) {
if (this->Allocated)
delete [] Array;
this->Width = Array2.Width;
this->Height = Array2.Height;
this->Size = Array2.Size;;
this->Array = new UINT [this->Size];
}

ArrayType2D::ArrayType2D() {
Size = 0;
Width = 0;
Height = 0;
Array = NULL;
Allocated = false;
}

inline void ArrayType2D::Allocate(int Height, int Width) {
if (Allocated)
delete [] Array;
this->Width = Width;
this->Height = Height;
this->Size = Width * Height;
this->Array = new UINT [Size];
}








Back to top
Peter Olcott
Guest





PostPosted: Sun Nov 27, 2005 5:25 am    Post subject: Re: Two-Dimensional Dynamic Arrays Reply with quote

This was very helpful. Not only did it provide an excellent solution,
it also analyzed all of my alternatives. If you want very high speed
and ease of use, on creating a multi-dimensional array from dynamic
memory, operator() is the way to go. I will post my latest update
shortly.

<roberts.noah (AT) gmail (DOT) com> wrote

Quote:

Gianni Mariani wrote:

w_elem_type * operator[]( t_Size i_index )
{
return & ( m_data[ i_index * m_rows ] );
}

This FAQ entry might be of interest:
http://www.parashift.com/c++-faq-lite/operator-overloading.html#faq-13.11

See also 13.10 right above it.




Back to top
Peter Olcott
Guest





PostPosted: Sun Nov 27, 2005 4:41 pm    Post subject: Re: Two-Dimensional Dynamic Arrays Reply with quote

I goofed your code works perfectly.
I could not get the last function to compile on MSVC++ 6.0,
but after I stripped this function out and properly benchmarked
the speed it was superb. It exactly matched a statically defined
matrix. Also I got to use the double operator[], that I wanted.
Because of these reasons, I must say that your code is the best
possible code according to any possible criterion measure of
a method to create a dynamically allocated two-dimensional array.

matrix.cpp
matrix.cpp(27) : error C2265: '<Unknown>' : reference to a zero-sized array is illegal
matrix.cpp(34) : see reference to class template instantiation 'matrix<w_elem_type>' being compiled
matrix.cpp(27) : error C2265: '<Unknown>' : reference to a zero-sized array is illegal
matrix.cpp(47) : see reference to class template instantiation 'matrix<float>' being compiled
matrix.cpp(27) : error C2087: '<Unknown>' : missing subscript
matrix.cpp(47) : see reference to class template instantiation 'matrix<float>' being compiled
matrix.cpp(49) : error C2664: '__thiscall matrix<float>::matrix<float>(int,int)' : cannot convert parameter 1 from 'double [3][4]'
to 'int'
This conversion requires a reinterpret_cast, a C-style cast or function-style cast
matrix.cpp(55) : warning C4508: 'main' : function should return a value; 'void' return type assumed


#include <vector> // Line (1) of matrix.cpp identical copy of your code

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";

}


Back to top
Peter Olcott
Guest





PostPosted: Sun Nov 27, 2005 6:28 pm    Post subject: Re: Two-Dimensional Dynamic Arrays Reply with quote


"Gianni Mariani" <gi2nospam (AT) mariani (DOT) ws> wrote

Quote:
Peter Olcott wrote:
I goofed your code works perfectly.
I could not get the last function to compile on MSVC++ 6.0,

MSCV6 is ancient. Do you "have to" use it ?

Currently yes. The performance of your code is superb and
allows me to accomplish my execution well within my real-time
constraints. I am posting my adaptations to your code below.
All that I did is add a little more functionality.

Quote:

but after I stripped this function out and properly benchmarked

It seems like it's picking the wrong vector constructor. I think there is another constructor vector( pointer, size ) that you
could use.

the speed it was superb. It exactly matched a statically defined
matrix. Also I got to use the double operator[], that I wanted.
Because of these reasons, I must say that your code is the best
possible code according to any possible criterion measure of
a method to create a dynamically allocated two-dimensional array.

Good luck !

//
// Matrix.h 2005-11-27 11:45 AM Dynamic Two-Dimensional Array Template
//
// comp.lang.c++ message from Gianni Mariani Nov 24, 2005 at 9:21 pm
// http://groups.google.com/group/comp.lang.c++/msg/a9092f0f6c9bf13a
// Adapted by Peter Olcott resize() and operator&() added.
//
#include <vector>

template <typename w_elem_type>
class matrix
{
public:
typedef int t_Size;

t_Size m_rows;
t_Size m_columns;

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&(){ return &m_data[0]; };

void resize( t_Size i_columns, t_Size i_rows );

w_elem_type * operator[]( t_Size i_index ) {
return & ( m_data[ i_index * m_rows ] );
}

};

template <typename w_elem_type>
void matrix<w_elem_type>::resize( t_Size i_columns, t_Size i_rows ) {
m_rows = i_rows;
m_columns = i_columns;
m_data.resize(i_columns * i_rows );
}




Back to top
Gianni Mariani
Guest





PostPosted: Sun Nov 27, 2005 6:54 pm    Post subject: Re: Two-Dimensional Dynamic Arrays Reply with quote

[email]roberts.noah (AT) gmail (DOT) com[/email] wrote:
Quote:
Gianni Mariani wrote:


w_elem_type * operator[]( t_Size i_index )
{
return & ( m_data[ i_index * m_rows ] );
}


This FAQ entry might be of interest:
http://www.parashift.com/c++-faq-lite/operator-overloading.html#faq-13.11

See also 13.10 right above it.


That FAQ is a classic case of premature optimization.

It's quite easy to turn [A][B] into (A,B) with no loss of performance
(on a good optimizing compiler). Also, the compiler probably has built
in optimizations that know how to deal with stride and may get confused
when dealing with (A,B) like functor calls.

Give me an example where [A][B] is slower than (A,B) (using an decent
optimizer) and we'll see if we can fix it. Otherwise I declare the FAQ
hogwash.

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Goto page 1, 2, 3, 4, 5  Next
Page 1 of 5

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.