| View previous topic :: View next topic |
| Author |
Message |
Peter Olcott Guest
|
Posted: Sun Nov 27, 2005 11:51 am Post subject: Very Fast Dyanmic Two Dimensional Array Class for Critical R |
|
|
//
// 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;
}
|
|
| Back to top |
|
 |
Peter Olcott Guest
|
Posted: Sun Nov 27, 2005 12:13 pm Post subject: Re: Very Fast Dyanmic Two Dimensional Array Class for Critic |
|
|
//
// 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;
}
|
|
| Back to top |
|
 |
Peter Olcott Guest
|
Posted: Sun Nov 27, 2005 12:15 pm Post subject: Re: Very Fast Dyanmic Two Dimensional Array Class for Critic |
|
|
//
// 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;
}
|
|
| Back to top |
|
 |
Neelesh Bodas Guest
|
Posted: Sun Nov 27, 2005 12:17 pm Post subject: Re: Very Fast Dyanmic Two Dimensional Array Class for Critic |
|
|
Peter Olcott wrote:
| Quote: | //
// 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?
|
|
| Back to top |
|
 |
Peter Olcott Guest
|
Posted: Sun Nov 27, 2005 4:04 pm Post subject: Re: Very Fast Dyanmic Two Dimensional Array Class for Critic |
|
|
"Neelesh Bodas" <neelesh.bodas (AT) gmail (DOT) com> wrote
| Quote: |
Peter Olcott wrote:
//
// 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?
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.
|
|
| Back to top |
|
 |
John Harrison Guest
|
Posted: Sun Nov 27, 2005 4:22 pm Post subject: Re: Very Fast Dyanmic Two Dimensional Array Class for Critic |
|
|
Peter Olcott wrote:
| Quote: | "Neelesh Bodas" <neelesh.bodas (AT) gmail (DOT) com> wrote
Peter Olcott wrote:
//
// 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?
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
|
|
| Back to top |
|
 |
Peter Olcott Guest
|
Posted: Sun Nov 27, 2005 5:14 pm Post subject: Re: Very Fast Dyanmic Two Dimensional Array Class for Critic |
|
|
"John Harrison" <john_andronicus (AT) hotmail (DOT) com> wrote
| Quote: | Peter Olcott wrote:
"Neelesh Bodas" <neelesh.bodas (AT) gmail (DOT) com> wrote
Peter Olcott wrote:
//
// 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?
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:q4WdnRxIUN3ZHhvenZ2dnUVZ_sednZ2d (AT) speakeasy (DOT) net...
|
|
| Back to top |
|
 |
Peter Olcott Guest
|
Posted: Sun Nov 27, 2005 5:17 pm Post subject: Re: Very Fast Dyanmic Two Dimensional Array Class for Critic |
|
|
"John Harrison" <john_andronicus (AT) hotmail (DOT) com> wrote
| Quote: | Peter Olcott wrote:
"Neelesh Bodas" <neelesh.bodas (AT) gmail (DOT) com> wrote
Peter Olcott wrote:
//
// 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?
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.
|
It turns out that the whole class is unnecessary, Gianni Mariani's
earlier posting is already perfect in every way.
"Gianni Mariani" <gi2nospam (AT) mariani (DOT) ws> wrote
| Quote: |
~ArrayType2D() { delete [] Array; };
works correctly even if Array is NULL.
john
|
|
|
| Back to top |
|
 |
Karl Heinz Buchegger Guest
|
Posted: Mon Nov 28, 2005 10:41 am Post subject: Re: Very Fast Dyanmic Two Dimensional Array Class for Critic |
|
|
| Quote: | Peter Olcott wrote:
|
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)
--
Karl Heinz Buchegger
[email]kbuchegg (AT) gascad (DOT) at[/email]
|
|
| Back to top |
|
 |
Axter Guest
|
Posted: Mon Nov 28, 2005 11:49 am Post subject: Re: Very Fast Dyanmic Two Dimensional Array Class for Critic |
|
|
Peter Olcott wrote:
| Quote: | //
// 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
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.
|
|
| Back to top |
|
 |
|