Very Fast Dyanmic Two Dimensional Array Class for Critical Review

P

Peter Olcott

//
// Array2D.h 2005-11-27 5:50 AM
//
#define UINT unsigned int
//
//
//
class ArrayType2D {
private:
int Width;
int Height;
int Size;
UINT* Array;
bool Allocated;
void Copy(ArrayType2D& Array2);
public:
ArrayType2D();
ArrayType2D(ArrayType2D& Array2) { Copy(Array2); };
~ArrayType2D() { if (Allocated) delete [] Array; };
UINT& operator()(const UINT ROW, const UINT COL) { return Array[ROW * Width + COL]; }
ArrayType2D& operator=(ArrayType2D& Array2) { Copy(Array2); return *this; };
UINT* operator&() { return Array; };
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 [Size];
for (int N = 0; N < Size; N++)
Array[N] = Array2.Array[N];
this->Allocated = true;
}



ArrayType2D::ArrayType2D() {
Width = 0;
Height = 0;
Size = 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];
this->Allocated = true;
}
 
P

Peter Olcott

//
// Array2D.h 2005-11-27 5:50 AM
//
#define UINT unsigned int
//
//
//
class ArrayType2D {
private:
int Width;
int Height;
int Size;
UINT* Array;
bool Allocated;
void Copy(ArrayType2D& Array2);
public:
ArrayType2D();
ArrayType2D(ArrayType2D& Array2) { Copy(Array2); };
~ArrayType2D() { if (Allocated) delete [] Array; };
UINT& operator()(const UINT ROW, const UINT COL) { return Array[ROW * Width + COL]; }
ArrayType2D& operator=(ArrayType2D& Array2) { Copy(Array2); return *this; };
UINT* operator&() { return Array; };
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 [Size];
for (int N = 0; N < Size; N++)
Array[N] = Array2.Array[N];
this->Allocated = true;
}



ArrayType2D::ArrayType2D() {
Width = 0;
Height = 0;
Size = 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];
this->Allocated = true;
}
 
P

Peter Olcott

//
// Array2D.h 2005-11-27 5:50 AM
//
#define UINT unsigned int

class ArrayType2D {
private:
int Width;
int Height;
int Size;
UINT* Array;
bool Allocated;
void Copy(ArrayType2D& Array2);
public:
ArrayType2D();
ArrayType2D(ArrayType2D& Array2) { Copy(Array2); };
~ArrayType2D() { if (Allocated) delete [] Array; };
UINT& operator()(const UINT ROW, const UINT COL) { return Array[ROW * Width + COL]; }
ArrayType2D& operator=(ArrayType2D& Array2) { Copy(Array2); return *this; };
UINT* operator&() { return Array; };
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 [Size];
for (int N = 0; N < Size; N++)
Array[N] = Array2.Array[N];
this->Allocated = true;
}

ArrayType2D::ArrayType2D() {
Width = 0;
Height = 0;
Size = 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];
this->Allocated = true;
}
 
N

Neelesh Bodas

Peter said:
//
// Array2D.h 2005-11-27 5:50 AM
//

I actually couldn't get what is "fast" and what is "dynamic" with this
Array class. Its not even polymorphic.
You could get much more with, say vectors.

Or am I missing something out here?
 
P

Peter Olcott

Neelesh Bodas said:
I actually couldn't get what is "fast" and what is "dynamic" with this
Array class. Its not even polymorphic.
You could get much more with, say vectors.

Or am I missing something out here?
I have not yet transformed it into the syntax of templates, so it only
does unsigned integers at the moment. I will make this conversion
some time soon. The memory allocated to this array is dynamically
allocated at run-time as opposed to statically allocated at compile-time.
Every other dynamically allocated array is either restricted to single
dimensional access, or allocates each row separately, making it much
slower. When I benchmarked this latter design it was fifty-fold slower
than my design.
 
J

John Harrison

Peter said:
I have not yet transformed it into the syntax of templates, so it only
does unsigned integers at the moment. I will make this conversion
some time soon. The memory allocated to this array is dynamically
allocated at run-time as opposed to statically allocated at compile-time.
Every other dynamically allocated array is either restricted to single
dimensional access, or allocates each row separately, making it much
slower. When I benchmarked this latter design it was fifty-fold slower
than my design.

Allocated is unnecessary.

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

works correctly even if Array is NULL.

john
 
P

Peter Olcott

John Harrison said:
Peter said:
I have not yet transformed it into the syntax of templates, so it only
does unsigned integers at the moment. I will make this conversion
some time soon. The memory allocated to this array is dynamically
allocated at run-time as opposed to statically allocated at compile-time.
Every other dynamically allocated array is either restricted to single
dimensional access, or allocates each row separately, making it much
slower. When I benchmarked this latter design it was fifty-fold slower
than my design.

Allocated is unnecessary.

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

works correctly even if Array is NULL.

john

It turns out that my whole class in unnecessary. Gianni Mariani's
original posting on the other thread is already perfect in every way.
news:[email protected]...
 
P

Peter Olcott

John Harrison said:
Allocated is unnecessary.

It turns out that the whole class is unnecessary, Gianni Mariani's
earlier posting is already perfect in every way.


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

works correctly even if Array is NULL.

john
 
K

Karl Heinz Buchegger

As usual:
You created something that you say is very fast.
But it has problems in functionality (aka: bugs).

You might analyze the problems of Copy. Especially if it is called
from the copy constructor. (Start with asking: What does Allocated
tell me, what the pointer 'Array' doesn't tell me, if used and initialized
correctly)
 
A

Axter

Peter said:
//
// Array2D.h 2005-11-27 5:50 AM
//
#define UINT unsigned int
//
//
//
class ArrayType2D {
private:
int Width;
int Height;
int Size;
UINT* Array;
bool Allocated;
void Copy(ArrayType2D& Array2);
public:
ArrayType2D();
ArrayType2D(ArrayType2D& Array2) { Copy(Array2); };
~ArrayType2D() { if (Allocated) delete [] Array; };
UINT& operator()(const UINT ROW, const UINT COL) { return Array[ROW * Width + COL]; }
ArrayType2D& operator=(ArrayType2D& Array2) { Copy(Array2); return *this; };
UINT* operator&() { return Array; };
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 [Size];
for (int N = 0; N < Size; N++)
Array[N] = Array2.Array[N];
this->Allocated = true;
}



ArrayType2D::ArrayType2D() {
Width = 0;
Height = 0;
Size = 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];
this->Allocated = true;
}

Try comparing the above class to using std::vector<std::vector<T> >
You'll be surprise at the results. On some implementations the vector
version is faster then above class.
Also check out the following class:
http://code.axter.com/dynamic_2d_array.h

Also, I recommend using [][] access method over operator().
I don't recommend following the FAQ recommendation on this matter.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top