Can I abbreviate array in C++

W

wakun

Hi there,
I am working with a data structure like

typedef struct
{
int number;
double left, right;
bool is_closed
} DT_INFO;

DT_INFO topology_grids[512][512];

In my program, topology_girds.left and topology_girds.right will be
accessed frequently, e.g.

if (topology_grids[r][c].is_closed)
tmp = (double)topology_grids[r][c].number*
(topology_grids[r][c].right-topology_grids[r][c].left);
else tmp=0.0;

ans = 5.0*(topology_grids[r][c].left + topology_grids[r+1][c].left) -
9.5*(topology_grids[r][c-1].right +
topology_grids[r][c].right) + tmp;

And there are more long and tedious expressions in my program. How can
I abbrev. the expressions in C++ like

if (topology_grids[r][c].is_closed)
tmp = (double)number[r][c]*(right[r][c]-left[r][c]);
else tmp=0.0;

ans = 5.0*(left[r][c]+ left[r+1][c]) - 9.5*(right[r][c-1] +
right[r][c]) + tmp;


Thanks in advance.
 
M

Mike Wahler

Hi there,
I am working with a data structure like

typedef struct
{
int number;
double left, right;
bool is_closed
} DT_INFO;

DT_INFO topology_grids[512][512];

Prefer vectors over arrays.
In my program, topology_girds.left and topology_girds.right will be
accessed frequently, e.g.

if (topology_grids[r][c].is_closed)
tmp = (double)topology_grids[r][c].number*
(topology_grids[r][c].right-topology_grids[r][c].left);
else tmp=0.0;

ans = 5.0*(topology_grids[r][c].left + topology_grids[r+1][c].left) -
9.5*(topology_grids[r][c-1].right +
topology_grids[r][c].right) + tmp;

And there are more long and tedious expressions

Well, vectors won't help with that, but they *are* safer
and easier to use.

DT_INFO& dt = topology_grids[r][c];
DT_INFO& dtr1 = topology_grids[r+1][c];
DT_INFO& dtc1 = topology_grids[r][c-1];

if(dt.is_closed)
tmp = (double)r.number * (dt.right - dt.left);
else tmp = 0.0;

ans = 5.0 * (dt.left + dtr1.left) - 9.5 * (dtc1.right + dt.right) + tmp;
in my program. How can
I abbrev. the expressions in C++

As above (you can directly refer to your objects with references
as shown, or use actual 'temporary' DTINFO objects)

DT_INFO dt = topology_grids[r][c];

The reference method *might* be faster for a large number of repetitions,
but does pose a potential hazard: even if 'r' and/or 'c' are changed,
the references will still point to the objects indexed by their
previous values (i.e. a reference doesn't act like a macro). One
way to contain this is to enclose the reference initializations and
the code that uses them in a separate scope:

{
DT_INFO& dt = topology_grids[r][c];
DT_INFO& dtr1 = topology_grids[r+1][c];
DT_INFO& dtc1 = topology_grids[r][c-1];

if(dt.is_closed)
/* etc */
}

Here, the references don't exist, so no danger of
using them when they might refer to the wrong objects.

-Mike
 
W

wakun

Well, vectors won't help with that, but they *are* safer
and easier to use.
> DT_INFO& dt = topology_grids[r][c];
> DT_INFO& dtr1 = topology_grids[r+1][c];
> DT_INFO& dtc1 = topology_grids[r][c-1];

> if(dt.is_closed)
> tmp = (double)r.number * (dt.right - dt.left);
> else tmp = 0.0;
> ans = 5.0 * (dt.left + dtr1.left) - 9.5 * (dtc1.right +
dt.right) + tmp;

Thanks for your reply. It may help but do not meet my demand
completely. It is because in my program some expressions are much more
complicated than that shown above. For instance, one expression refer
to [r][c], [r+1][c], [r+1][c-1], [r][c-1], [r+2][c-1], [r-2][c] and
etc. so it's impossible to define dummy variables for all case.
 
N

n2xssvv g02gfr12930

Hi there,
I am working with a data structure like

typedef struct
{
int number;
double left, right;
bool is_closed
} DT_INFO;

DT_INFO topology_grids[512][512];

In my program, topology_girds.left and topology_girds.right will be
accessed frequently, e.g.

if (topology_grids[r][c].is_closed)
tmp = (double)topology_grids[r][c].number*
(topology_grids[r][c].right-topology_grids[r][c].left);
else tmp=0.0;

ans = 5.0*(topology_grids[r][c].left + topology_grids[r+1][c].left) -
9.5*(topology_grids[r][c-1].right +
topology_grids[r][c].right) + tmp;

And there are more long and tedious expressions in my program. How can
I abbrev. the expressions in C++ like

if (topology_grids[r][c].is_closed)
tmp = (double)number[r][c]*(right[r][c]-left[r][c]);
else tmp=0.0;

ans = 5.0*(left[r][c]+ left[r+1][c]) - 9.5*(right[r][c-1] +
right[r][c]) + tmp;


Thanks in advance.
Use a reference to frequently accessed array items as follows:

DT_INFO &tg_r_c = topology_grids[r][c];
if (tg_r_c.is_closed)
tmp = (double)tg_r_c.number*
(tg_r_c.right-tg_r_c.left);
else tmp=0.0;

ans = 5.0*(tg_r_c.left + topology_grids[r+1][c].left) -
9.5*(topology_grids[r][c-1].right +
tg_r_c.right) + tmp;

This also is more code efficient as code required dereferencing
arrays is slower than for a reference.

JB
 
W

wakun

Use a reference to frequently accessed array items as follows:
Yes, I know this is a good way for some case. However, as I mentioned
in previous post, many expressions will invoke more then 20 terms, in
which no term is duplicated. It is no wise to set reference for each
terms. I am wondering if I can have a reference point to the structure
like

DT_INFO &ref = topology_grids[0][0];

so I can access any grid via ref? I try, but fail :(
 
N

n2xssvv g02gfr12930

Yes, I know this is a good way for some case. However, as I mentioned
in previous post, many expressions will invoke more then 20 terms, in
which no term is duplicated. It is no wise to set reference for each
terms. I am wondering if I can have a reference point to the structure
like

DT_INFO &ref = topology_grids[0][0];

so I can access any grid via ref? I try, but fail :(
If your using r and c as indices that iterate, why not use pointers to
iterate through the 2D array, like below:-


// Using const items means changes in size
// handled properly in loop
const int nc = 512,nr = 512;

typedef DT_INFO DT_INFO_ROW[nc]

DT_INFO_ROW topology_grids[nr];

DT_INFO_ROW *r1,
DT_INFO *c1;

for (r1 = topology_grids;r1 != topology_grids + nr;r1++)
{
// r[0] is current row
// r[1] is next row
// r[-1] is previous row
for (c1 = *r1; c1 != *r1 + nc;c1++)
{
// c[0] is current entry
// c[1] is next entry
// c[-1] is previous entry
}
}

Hope this helps.

JB
 
M

Mike Wahler

Yes, I know this is a good way for some case. However, as I mentioned
in previous post, many expressions will invoke more then 20 terms, in
which no term is duplicated. It is no wise to set reference for each
terms. I am wondering if I can have a reference point to the structure
like

DT_INFO &ref = topology_grids[0][0];

so I can access any grid via ref? I try, but fail :(

cout << topology_grids[0][0];

and

cout << ref;

should give same output.

If you think this is indeed what you're doing,
post an example of some compilable code that
doesn't work as you expect.

-Mike
 
N

niklasb

Yes, I know this is a good way for some case. However, as I mentioned
in previous post, many expressions will invoke more then 20 terms, in
which no term is duplicated. It is no wise to set reference for each
terms. I am wondering if I can have a reference point to the structure
like

DT_INFO &ref = topology_grids[0][0];

so I can access any grid via ref? I try, but fail :(

That's because ref above is a reference to a particular element of the
array, not a reference to the array itself. You can create a reference
to the entire array as follows:

DT_INFO (&ref)[512][512] = topology_grids;

and then use ref as a synonym for topology_grids. However, you
should consider some of the other options suggested, such as
using pointers to iterate over the array.
 
M

Michiel.Salters

Hi there,
I am working with a data structure like

typedef struct
{
int number;
double left, right;
bool is_closed
} DT_INFO;

DT_INFO topology_grids[512][512];

In my program, topology_girds.left and topology_girds.right will be
accessed frequently, e.g.

if (topology_grids[r][c].is_closed)
tmp = (double)topology_grids[r][c].number*
(topology_grids[r][c].right-topology_grids[r][c].left);
else tmp=0.0;

ans = 5.0*(topology_grids[r][c].left + topology_grids[r+1][c].left) -
9.5*(topology_grids[r][c-1].right +
topology_grids[r][c].right) + tmp;

And there are more long and tedious expressions in my program. How can
I abbrev. the expressions in C++ like

if (topology_grids[r][c].is_closed)
tmp = (double)number[r][c]*(right[r][c]-left[r][c]);
else tmp=0.0;

ans = 5.0*(left[r][c]+ left[r+1][c]) - 9.5*(right[r][c-1] +
right[r][c]) + tmp;

C++ is object-oriented, so make it an object:

struct coordinate {
int r,c;
coordinate(int r,int c) : r(r),c(c) {}
};
coordinate North(coordinate rc); // obvious implementation
coordinate South(coordinate rc); // obvious implementation
coordinate East(coordinate rc); // obvious implementation
coordinate West(coordinate rc); // obvious implementation

struct topology_grid {
DT_INFO data[512][512];
int right(coordinate rc) { return data[rc.r][rc.c].right; }
int left(coordinate rc) { return data[rc.r][rc.c].left; }
int diff(coordinate rc) { return right(rc)-left(rc); }
// etcetera
};

You could then write
coordinate rc = South(coordinate(0,0)); // just an example
if (topology_grids.is_closed(rc)
tmp = (double)topology_grids.number(rc)*topology_grids.diff(rc);
else tmp=0.0;

HTH,
Michiel Salters

PS. There's no need to typedef nameless struct's in C++.
 
T

Thomas J. Gritzan

Hi there,
I am working with a data structure like

typedef struct
{
int number;
double left, right;
bool is_closed
} DT_INFO;

DT_INFO topology_grids[512][512]; [...]
How can
I abbrev. the expressions in C++ like

if (topology_grids[r][c].is_closed)
tmp = (double)number[r][c]*(right[r][c]-left[r][c]);
else tmp=0.0;

You could write a functor and use it like this:

right_member right(topology_grids);
left_member left(topology_grids);

.... = right(r, c) - left(r, c);

The compiler should optimize this away.

Thomas
 

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

No members online now.

Forum statistics

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

Latest Threads

Top