Can I abbreviate array in C++

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • wakun@wakun.com

    #1

    Can I abbreviate array in C++

    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)topolog y_grids[r][c].number*
    (topology_grids[r][c].right-topology_grids[r][c].left);
    else tmp=0.0;

    ans = 5.0*(topology_g rids[r][c].left + topology_grids[r+1][c].left) -
    9.5*(topology_g rids[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.

  • Mike Wahler

    #2
    Re: Can I abbreviate array in C++


    <wakun@wakun.co m> wrote in message
    news:1132742826 .365501.290820@ g49g2000cwa.goo glegroups.com.. .[color=blue]
    > 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];[/color]

    Prefer vectors over arrays.
    [color=blue]
    >
    > 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)topolog y_grids[r][c].number*
    > (topology_grids[r][c].right-topology_grids[r][c].left);
    > else tmp=0.0;
    >
    > ans = 5.0*(topology_g rids[r][c].left + topology_grids[r+1][c].left) -
    > 9.5*(topology_g rids[r][c-1].right +
    > topology_grids[r][c].right) + tmp;
    >
    > And there are more long and tedious expressions[/color]

    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.numbe r * (dt.right - dt.left);
    else tmp = 0.0;

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

    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


    Comment

    • wakun@wakun.com

      #3
      Re: Can I abbreviate array in C++

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

      [color=blue]
      > if(dt.is_closed )
      > tmp = (double)r.numbe r * (dt.right - dt.left);
      > else tmp = 0.0;[/color]
      [color=blue]
      > ans = 5.0 * (dt.left + dtr1.left) - 9.5 * (dtc1.right +[/color]
      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.

      Comment

      • n2xssvv g02gfr12930

        #4
        Re: Can I abbreviate array in C++

        wakun@wakun.com wrote:[color=blue]
        > 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)topolog y_grids[r][c].number*
        > (topology_grids[r][c].right-topology_grids[r][c].left);
        > else tmp=0.0;
        >
        > ans = 5.0*(topology_g rids[r][c].left + topology_grids[r+1][c].left) -
        > 9.5*(topology_g rids[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.
        >[/color]
        Use a reference to frequently accessed array items as follows:

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

        ans = 5.0*(tg_r_c.lef t + topology_grids[r+1][c].left) -
        9.5*(topology_g rids[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

        Comment

        • wakun@wakun.com

          #5
          Re: Can I abbreviate array in C++

          >> 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 :(

          Comment

          • n2xssvv g02gfr12930

            #6
            Re: Can I abbreviate array in C++

            wakun@wakun.com wrote:[color=blue][color=green][color=darkred]
            >>>Use a reference to frequently accessed array items as follows:[/color][/color]
            >
            > 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 :(
            >[/color]
            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

            Comment

            • Mike Wahler

              #7
              Re: Can I abbreviate array in C++


              <wakun@wakun.co m> wrote in message
              news:1132771856 .019918.88700@z 14g2000cwz.goog legroups.com...[color=blue][color=green][color=darkred]
              >>> Use a reference to frequently accessed array items as follows:[/color][/color]
              > 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 :([/color]

              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


              Comment

              • niklasb@microsoft.com

                #8
                Re: Can I abbreviate array in C++


                wakun@wakun.com wrote:[color=blue][color=green][color=darkred]
                > >> Use a reference to frequently accessed array items as follows:[/color][/color]
                > 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 :([/color]

                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.

                Comment

                • Michiel.Salters@tomtom.com

                  #9
                  Re: Can I abbreviate array in C++


                  wakun@wakun.com wrote:[color=blue]
                  > 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)topolog y_grids[r][c].number*
                  > (topology_grids[r][c].right-topology_grids[r][c].left);
                  > else tmp=0.0;
                  >
                  > ans = 5.0*(topology_g rids[r][c].left + topology_grids[r+1][c].left) -
                  > 9.5*(topology_g rids[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;[/color]

                  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(coordinat e rc); // obvious implementation
                  coordinate South(coordinat e 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(coordinat e 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(coordinat e(0,0)); // just an example
                  if (topology_grids .is_closed(rc)
                  tmp = (double)topolog y_grids.number( rc)*topology_gr ids.diff(rc);
                  else tmp=0.0;

                  HTH,
                  Michiel Salters

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

                  Comment

                  • Thomas J. Gritzan

                    #10
                    Re: Can I abbreviate array in C++

                    wakun@wakun.com schrieb:[color=blue]
                    > 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];[/color]
                    [...][color=blue]
                    > 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;[/color]

                    You could write a functor and use it like this:

                    right_member right(topology_ grids);
                    left_member left(topology_g rids);

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

                    The compiler should optimize this away.

                    Thomas

                    Comment

                    Working...