473,396 Members | 1,895 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Two-Dimensional Dynamic Arrays

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.

Thanks,
Peter Olcott
Nov 25 '05
60 10079
ro**********@gmail.com wrote:

Why not just return &&array[x]

Notice how the () syntax keeps encapsulation without the need of an
adapter even with 2d and with 3d still doesn't need one.


plonk
Nov 30 '05 #51

Axter wrote:
I forgot to mention that your wrapper class also fails to have a GetCol
and GetRow function.
You are of course right and that is an obvious problem except for one
thing. The presented class is an adapter to provide () syntax when
using a C array. Since someone that needs that adapter is already
using a C array outside of a class and so is tracking the dimensions
already the GetCol and GetRow functions would be redundant. However,
since the addition of these functions is relatively straight forward
and could come of use, possibly, their ommision is probably a mistake.

As you also state, I could have also improved it to also adapt vectors.
Also check out another CodeGuru STL FAQ:
http://www.codeguru.com/forum/showth...hreadid=297838

The above link has two classes. One which I'm the author of, and the
second class which allows you to use both [][] and operator() method.
I don't think it's a good idea to have both methods in a class, but
some developers may find this method more acceptable.


Decent:
http://www.codeguru.com/forum/showth...hreadid=231046

Still has a dependency that could be removed. It could be more
abstract. No matter how you slice it the interface still spells out a
particular alignment. This is forgivable but with op() that problem
doesn't exist so that is still +1 for op().

The reason this version is better is that because it returns a vector
reference instead of pointer to a memory chunk; therefor the class is
protected from clients trying to overwrite into a second row on
accident. The vector interface is sufficient to offer protection in
this area and it still retains a great deal of efficiency. Basically
the vector is acting a lot like the previously supplied "proxy".

On the other hand, this class doesn't seem that necessary. Why not
just use the vector<vector<> > in the open? The only really nice thing
this class offers is the sizing of the internal vectors. Makes me
wonder if the constructor shouldn't just call resize.

The other link you have above... The first class is no good; it is the
same trash you already posted in fact - why are you wasting everyone's
time like that? Do you think that by looking at it somewhere else it
will change the problems inherent in its design??

Second implementation also has problems but is several degrees better
than yours. I would remove the op[]'s. The iterator idea was great
but could have been expanded to include columns. getRow is dangerous
and has the same problems as op[] - I think it would be better to stay
with the iterator idea and toss getRow or just make it return an
iterator.

Nov 30 '05 #52

Gianni Mariani wrote:
ro**********@gmail.com wrote:

Why not just return &&array[x]

Notice how the () syntax keeps encapsulation without the need of an
adapter even with 2d and with 3d still doesn't need one.


plonk


Coward.

Nov 30 '05 #53

ro**********@gmail.com wrote:
Decent:
http://www.codeguru.com/forum/showth...hreadid=231046


Come to think of it, I like it less now that I think on it....not for a
real 2d matrix anyway - or anything where rows should be the same
length. Reason is simple:

T t();

mtx[x].push_back(t);

This is ok, since the class doesn't stop you from doing it (and can't)
but it's rather ugly. The purpose of this class seems pretty straight
forward in that the above call shouldn't be allowable even though it
is. This is a pretty tricky problem though and I'm not surprised it
slipped past the developer (maybe it didn't and they decided to live
with it). The answer is of course that some sort of policy is created
that is self enforced by developers...someone usually breaks that kind
of policy though. Any client that used the iterator interface or based
its operation on .size() could break down with the above, buggered mtx.

It's a point against it anyway. You can do the same with
vector<vector<> > but at least that isn't pretending to be rectangular.
The single chunk of memory solution also seems to have the possibility
at least of being more efficient....less redirections.

Of course this is also caused by an encapsulation issue. I tricked
myself into thinking it would be ok in this case. Just goes to show I
guess.

You could fix this by returning a class that *contains* the vector (or
non-public inheritance) that did not provide the functions that would
pose the problem. This works because it fixes the encapsulation issue.
If you thought ahead enough this returned class could be based on
something abstract that could be implemented in any number of
manners...meaning the vectors wouldn't even have to be there at all.

In the end it is similar to the previous example and I still think the
() interface still ends up providing the same benefits with less work.

Nov 30 '05 #54
jrp
I can see both sides of the argument so far, but would like to explore
a bit further real world usage and performance of such classes,
including with"legacy" code.

The first thing that most of the above examples would need is const
versions of many of the operations; the second is that it needs to be
possible to use an aligned malloc to allocate storage to get the
benfits of vectorization. Finally, it needs to be possible to allocate
the memory so that there is no aliasing (or do so via a restrict
decoration).

To be more specifif, suppose that I want to build a system of 3d
numeric objects (cubes) that can be sliced into 2 and 1d objects
(planes, rows), and those 2d objects can also be sliced into 1d rows.
And I want to be able to present those rows to legacy C routines that
expect C arrays and lengths. I also want to be able to do things like

acube = 0;
acube[nplane] = 0;

aplane[nrow] =0;

acube[nplane] = aplane;

and present an acube[nplane] to a routine as a const Plane& parameter.

(using () if necessary).

As I understand it, the original intention was for valarray + slice to
do this job but that seems to be harder to achieve efficiently and
usable.

There are those that say use Blitz++, or boost::multi_array, or
stlorg::fixed_array. These are heavy packages, yet none of them seems
to provide a drop-in replacement for the ugly but functional
vector<vector<vector>>> and [][][].
It would be helpful to discuss this on the basis of some concrete
candidate classes such as those earlier in the thread so that we can
test performance / applicability. For example, are address
dereferences slower than int multiplications?

Nov 30 '05 #55

ro**********@gmail.com wrote:
ro**********@gmail.com wrote:
Decent:
http://www.codeguru.com/forum/showth...hreadid=231046


Come to think of it, I like it less now that I think on it....not for a
real 2d matrix anyway - or anything where rows should be the same
length. Reason is simple:

T t();

mtx[x].push_back(t);

This is ok, since the class doesn't stop you from doing it (and can't)
but it's rather ugly. The purpose of this class seems pretty straight
forward in that the above call shouldn't be allowable even though it
is. This is a pretty tricky problem though and I'm not surprised it
slipped past the developer (maybe it didn't and they decided to live
with it). The answer is of course that some sort of policy is created
that is self enforced by developers...someone usually breaks that kind
of policy though. Any client that used the iterator interface or based
its operation on .size() could break down with the above, buggered mtx.

It's a point against it anyway. You can do the same with
vector<vector<> > but at least that isn't pretending to be rectangular.
The single chunk of memory solution also seems to have the possibility
at least of being more efficient....less redirections.

Of course this is also caused by an encapsulation issue. I tricked
myself into thinking it would be ok in this case. Just goes to show I
guess.

You could fix this by returning a class that *contains* the vector (or
non-public inheritance) that did not provide the functions that would
pose the problem. This works because it fixes the encapsulation issue.
If you thought ahead enough this returned class could be based on
something abstract that could be implemented in any number of
manners...meaning the vectors wouldn't even have to be there at all.

In the end it is similar to the previous example and I still think the
() interface still ends up providing the same benefits with less work.


I know you among others, don't seem to see a syntax problem with using
operator(), but here's a small example of how using operator() instead
of [][] can make your code ambiguous.
CMatrix arr(12, 34);
..... lost of other code ....
..... losts more code ....
int x = arr(1, 3);// Is arr a function, or a class????

If arr is far removed from it's declaration, a normal assumption would
be to think arr is a function, and the above line of code is calling a
global arr function.

int x = arr[1][3]; // This is much less ambiguous, and easy to identify
arr as an array

Using operator() for arrays makes the code more confusing, and harder
to read and maintain, where as using [][] is straight forward, and very
easy to pick what's going on in above line of code.
Why use ambiguous syntax when you don't have to?????

Nov 30 '05 #56
Axter wrote:
[snip] I know you among others, don't seem to see a syntax problem with using
operator(), but here's a small example of how using operator() instead
of [][] can make your code ambiguous.
CMatrix arr(12, 34);
.... lost of other code ....
.... losts more code ....
int x = arr(1, 3);// Is arr a function, or a class????

If arr is far removed from it's declaration, a normal assumption would
be to think arr is a function, and the above line of code is calling a
global arr function.

int x = arr[1][3]; // This is much less ambiguous, and easy to identify
arr as an array

Using operator() for arrays makes the code more confusing, and harder
to read and maintain, where as using [][] is straight forward, and very
easy to pick what's going on in above line of code.
Why use ambiguous syntax when you don't have to?????


To me, it seems that the problem in this code does not arise from
overloading operator(). It arises from choosing a poor variable name. C++
has the feature of allowing for function objects. Therefore, any expression
like

identifier( arg_1, arg_2, arg_3 );

could be a function call, creation of a temporary, invoking operator(), or
maybe even more. The way to cope with this "ambiguity" is not to ban
overloading operator() but to choose identifiers wisely so that an object
of type CMatrix can be recognized as such.
Best

Kai-Uwe Bux
Nov 30 '05 #57

Kai-Uwe Bux wrote:
Axter wrote:

[snip]
I know you among others, don't seem to see a syntax problem with using
operator(), but here's a small example of how using operator() instead
of [][] can make your code ambiguous.
CMatrix arr(12, 34);
.... lost of other code ....
.... losts more code ....
int x = arr(1, 3);// Is arr a function, or a class????

If arr is far removed from it's declaration, a normal assumption would
be to think arr is a function, and the above line of code is calling a
global arr function.

int x = arr[1][3]; // This is much less ambiguous, and easy to identify
arr as an array

Using operator() for arrays makes the code more confusing, and harder
to read and maintain, where as using [][] is straight forward, and very
easy to pick what's going on in above line of code.
Why use ambiguous syntax when you don't have to?????


To me, it seems that the problem in this code does not arise from
overloading operator(). It arises from choosing a poor variable name. C++
has the feature of allowing for function objects. Therefore, any expression
like


I agree, but you find too many developers that will use short
initialized names, that may be perfectly clear to them, but complete
ambiguous to the next developer that has to maintain his/her code.
I'm not advocating a ban on using operator(). I just don't think using
it in place of [][] is appropriate, and leads to ambiguous code.

Nov 30 '05 #58

jrp wrote:
To be more specifif, suppose that I want to build a system of 3d
numeric objects (cubes) that can be sliced into 2 and 1d objects
(planes, rows), and those 2d objects can also be sliced into 1d rows.
And I want to be able to present those rows to legacy C routines that
expect C arrays and lengths. I also want to be able to do things like

acube = 0;
acube[nplane] = 0;
I take it that the above is filling with 0's? I see the need for such
an operation but I am not sure I like the syntax. You could also be
meaning (especially in the top instance) assign to this cube with a
cube with no dimensions.
aplane[nrow] =0;

acube[nplane] = aplane;
I can also see the need for this operation but I think the syntax is
flawed here as well. Do you mean X, Y, or Z axis plane? Functional
syntax would be better here I think. Could be a single function
getPlane(x, axis) or three seperate for each axis.
and present an acube[nplane] to a routine as a const Plane& parameter.
Or even a non const. Seems you would also want to be able to pass
planes, rows, or individual items to manipulators.
It would be helpful to discuss this on the basis of some concrete
candidate classes such as those earlier in the thread so that we can
test performance / applicability. For example, are address
dereferences slower than int multiplications?


I would add a requirement. You must be able to create a new class that
stores its data in a different manner, possibly col mjr form but could
be anything, that will work as a parameter to any function or algorithm
that expects the designed class.

I'll play but it will take a while as time is an issue.

Nov 30 '05 #59
jrp
Thanks. I won't quibble about the syntax (although there is a logic to
it). What interests me is ((empirical) relative performance, including
vectorizability) and completeness: so missing const versions of
operators are an issue, and may help to tip the balance for those
interested in clutter / complexity.
I'll play but it will take a while as time is an issue.


Thanks. Likewise. Happy to contribute, if necessary.

Nov 30 '05 #60
jrp
Oh, and some other example (based on projections on a base array)

http://www.math.ualberta.ca/~bowman/Array

http://www.bluesailsoftware.com/software/aptl/

http://www.netwood.net/~edwin/svmtl/

http://www.research.att.com/~bs/matrix.c

as well as heavyweights such as POOMA

Nov 30 '05 #61

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

Similar topics

0
by: SimonC | last post by:
I'm looking to do something similar to a feature found on Ticketmaster.com, where you select your seats at a venue, and then you have two minutes in which to take or leave them. QUESTION 1a....
8
by: John Grenier | last post by:
Hi, I have to determine the "standing" (WIN - TIE - LOSS) from confrontations between two teams on a contest. The table matchResults has fields cont_id, team_id and contest_result (int). ...
6
by: Willem | last post by:
Hi, I have a newbie question: is it possible to make a search form in asp that searches in two different databases (access)? Willem
10
by: Hank1234 | last post by:
Can I use one Data Adapter and one Command Builder to update amny tables? Currently in my data adapter I query two tables and fill them into two tables in a data set. When I make a change to a...
6
by: Matt K. | last post by:
Hi there, I have a form in an Access project that contains a subform which displays the results of a query of the style "select * from where = #a certain date#". In the main part of the form...
7
by: Prabhudhas Peter | last post by:
I have two object instances of a same class... and i assigned values in both object instances (or the values can be taken from databse and assigned to the members of the objects)... Now i want to...
0
by: clintonG | last post by:
I applied aspnet_regsql to SQL2K which was working fine throughout Beta 2 development. After installing Visual Studio and SQL Express RTM my application has blown up. Logging in to the application...
9
by: Steven | last post by:
Hello, I have a question about strcmp(). I have four words, who need to be compared if it were two strings. I tried adding the comparison values like '(strcmp(w1, w2) + strcmp(w3, w4))', where...
9
by: dhable | last post by:
I just started working with Python and ran into an annoyance. Is there a way to avoid having to use the "from xxx import yyy" syntax from files in the same directory? I'm sure it's been asked a...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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...
0
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...
0
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,...

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.