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. 9 1572
<wa***@wakun.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com... 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
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. wa***@wakun.com wrote: 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
>> 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 :( wa***@wakun.com wrote: 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 :(
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
<wa***@wakun.com> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com... 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 :(
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 wa***@wakun.com wrote: 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 :(
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. wa***@wakun.com wrote: 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.d iff(rc);
else tmp=0.0;
HTH,
Michiel Salters
PS. There's no need to typedef nameless struct's in C++. wa***@wakun.com schrieb: 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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Brian |
last post by:
I'm diddlying with a script, and found some behavior I don't understand.
Take this snippet:
for ($i = 0; $i <= count($m); $i++) {
array_shift($m);
reset($m);
}
|
by: Stormkid |
last post by:
Hi Group I'm trying to figure out a way that I can take two (two
dimensional) arrays and avShed and shed, and subtract the matching elements
in shed from avShed I've pasted the arrays blow from a...
|
by: lawrence |
last post by:
I wanted to test xml_parse_into_struct() so I took the example off of
www.php.net and put this code up on a site:
<?php
$simple = <<<END
<item>
|
by: vcardillo |
last post by:
Hello all,
Okay, I am having some troubles. What I am doing here is dealing with
an employee hierarchy that is stored in an array. It looks like this:
$employees = array( "user_id" => array(...
|
by: Sam Collett |
last post by:
How do I remove an item with a specified value from an array?
i.e. array values 1,2,2,5,7,12,15,21
remove 2 from array would return
1,5,7,12,15,21
(12 and 21 are NOT removed, duplicates are...
| |
by: Mike S. Nowostawsky |
last post by:
I tried using the "toUpperCase()" property to change the value of an array
entity to uppercase BUT it tells me that the property is invalid. It seems
that an array is not considered an object when...
|
by: maurir |
last post by:
Hello
I have an array of char, like these examples:
John Smith
Juan de la Rosa
Darren Russel Miller
Ana Maria da Silva Santos
The output of the compacted string's image:
|
by: voidmaingetch |
last post by:
hey i was asking, y'know, how do i abbreviate a string.
eg--if i give>> world health organization it'll give me<< w.h.o.
|
by: Sunny |
last post by:
Hi,
Is there a way in Javascript to abbreviate currency.
Like if it is $1000 then it convert it into 1K.
|
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,...
|
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...
| |
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...
|
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: 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...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
| |
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...
| |