473,809 Members | 2,506 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Very Fast Dyanmic Two Dimensional Array Class for Critical Review

//
// 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(ArrayType2 D& Array2);
public:
ArrayType2D();
ArrayType2D(Arr ayType2D& Array2) { Copy(Array2); };
~ArrayType2D() { if (Allocated) delete [] Array; };
UINT& operator()(cons t UINT ROW, const UINT COL) { return Array[ROW * Width + COL]; }
ArrayType2D& operator=(Array Type2D& 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::Co py(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::Ar rayType2D() {
Width = 0;
Height = 0;
Size = 0;
Array = NULL;
Allocated = false;
}

inline void ArrayType2D::Al locate(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;
}



Nov 27 '05 #1
9 2073
//
// 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(ArrayType2 D& Array2);
public:
ArrayType2D();
ArrayType2D(Arr ayType2D& Array2) { Copy(Array2); };
~ArrayType2D() { if (Allocated) delete [] Array; };
UINT& operator()(cons t UINT ROW, const UINT COL) { return Array[ROW * Width + COL]; }
ArrayType2D& operator=(Array Type2D& 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::Co py(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::Ar rayType2D() {
Width = 0;
Height = 0;
Size = 0;
Array = NULL;
Allocated = false;
}

inline void ArrayType2D::Al locate(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;
}



Nov 27 '05 #2
//
// 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(ArrayType2 D& Array2);
public:
ArrayType2D();
ArrayType2D(Arr ayType2D& Array2) { Copy(Array2); };
~ArrayType2D() { if (Allocated) delete [] Array; };
UINT& operator()(cons t UINT ROW, const UINT COL) { return Array[ROW * Width + COL]; }
ArrayType2D& operator=(Array Type2D& 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::Co py(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::Ar rayType2D() {
Width = 0;
Height = 0;
Size = 0;
Array = NULL;
Allocated = false;
}

inline void ArrayType2D::Al locate(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;
}
Nov 27 '05 #3

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?

Nov 27 '05 #4

"Neelesh Bodas" <ne***********@ gmail.com> wrote in message news:11******** *************@g 47g2000cwa.goog legroups.com...

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.
Nov 27 '05 #5
Peter Olcott wrote:
"Neelesh Bodas" <ne***********@ gmail.com> wrote in message news:11******** *************@g 47g2000cwa.goog legroups.com...
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
Nov 27 '05 #6

"John Harrison" <jo************ *@hotmail.com> wrote in message news:L2******** *********@newsf e6-win.ntli.net...
Peter Olcott wrote:
"Neelesh Bodas" <ne***********@ gmail.com> wrote in message news:11******** *************@g 47g2000cwa.goog legroups.com...
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:q4******** *************** *******@speakea sy.net...
Nov 27 '05 #7

"John Harrison" <jo************ *@hotmail.com> wrote in message news:L2******** *********@newsf e6-win.ntli.net...
Peter Olcott wrote:
"Neelesh Bodas" <ne***********@ gmail.com> wrote in message news:11******** *************@g 47g2000cwa.goog legroups.com...
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" <gi*******@mari ani.ws> wrote in message
news:q4******** *************** *******@speakea sy.net...


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

works correctly even if Array is NULL.

john

Nov 27 '05 #8
> 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
kb******@gascad .at
Nov 28 '05 #9

Peter Olcott wrote:
//
// 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(ArrayType2 D& Array2);
public:
ArrayType2D();
ArrayType2D(Arr ayType2D& Array2) { Copy(Array2); };
~ArrayType2D() { if (Allocated) delete [] Array; };
UINT& operator()(cons t UINT ROW, const UINT COL) { return Array[ROW * Width + COL]; }
ArrayType2D& operator=(Array Type2D& 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::Co py(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::Ar rayType2D() {
Width = 0;
Height = 0;
Size = 0;
Array = NULL;
Allocated = false;
}

inline void ArrayType2D::Al locate(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.

Nov 28 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
7674
by: ip4ram | last post by:
I used to work with C and have a set of libraries which allocate multi-dimensional arrays(2 and 3) with single malloc call. data_type **myarray = (data_type**)malloc(widht*height*sizeof(data_type)+ height* sizeof(data_type*)); //allocate individual addresses for row pointers. Now that I am moving to C++,am looking for something by which I can
5
13171
by: cql60 | last post by:
Hi all Pro, I have the class call "Person", and I do declare two dimensional array type of string inside this class which will keep "First_Name" and "Last_Name" as the record. When I try to serialize it by using XMLSerializer, I got error like this: An unhandled exception of type 'System.InvalidOperationException' occurred in system.xml.dll Additional information: There was an error reflecting type
2
4397
by: frankh | last post by:
With Marshal you can copy data from a one-dimensional array to BitmapData.Scan0. What do I do if my array is two-dimensional? Or, alternatively, can I create somehow two variables byte arr1; // size 480*640 byte arr2; // size which are references to the same memory block and allow me to access it at will with one or two indices, e.g. arr1 or arr2?
38
3020
by: djhulme | last post by:
Hi, I'm using GCC. Please could you tell me, what is the maximum number of array elements that I can create in C, i.e. char* anArray = (char*) calloc( ??MAX?? , sizeof(char) ) ; I've managed to create arrays using DOUBLE data types, but when I try to access the array, the compiler complains that the number is not an INT, i.e.
8
11831
by: per9000 | last post by:
Hi all, I have a two-dimensional array of data, f.x int's. We can imagine that the array is "really large". Now I want the data in it and store this in a one-dimensional array. The obvious way to do this is a nested for-loop - but we all know O(n^2) is bad. So I am looking for something like ArrayList.ToArray(), or Matlabs A(:). C#
272
14214
by: Peter Olcott | last post by:
http://groups.google.com/group/comp.lang.c++/msg/a9092f0f6c9bf13a I think that the operator() member function does not work correctly, does anyone else know how to make a template for making two dimensional arrays from std::vectors ??? I want to use normal Array Syntax.
4
559
by: Gernot Frisch | last post by:
Hi, I need a class, that has a 4 dimensional array (can be 3 dimensional, too) with such an operator: T operator()(int x1, int x2=0, int x3=0, int x4=0); that can be used as:
5
3889
by: nelly0 | last post by:
developing a program that will manipulate noise levels (measured in decibels) that is collected by car manufacturers. These noise levels are produced at seven different speeds by a maximum of six different models of cars that are produced by the car manufacturer. Task 1 Step 1: Create a directory called Assignment02 and create the files of steps 2, 3 and 4 in this directory as well as your project file. Step 2: Create a file called...
7
3660
by: Sanny | last post by:
I have an app in Java. It works fine. Some people say Java works as fast as C. Is that true? C can use assembly language programs. How much faster are they inplace of calling general routines. Can C++ directly acess the Registers. Will the Computation 5-10 times faster than Java?
0
9603
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10378
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10391
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10121
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9200
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7664
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5690
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3862
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3015
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.