Re: circular shift array



In article <N_adnSlUjZ1NczPb4p2dnAA@xxxxxx>,
Richard Heathfield <rjh@xxxxxxxxxxxxxxx> wrote:
An easier way [to deal with circular queues], which doesn't involve
any shifting at all, is to maintain an object whose purpose is to
store the index of the first item. Then you just process the array
as follows:

idx = head;
for(i = 0; i < n; i++)
{
if(idx++ == n) /* idx = (head + i) % n is shorter, but costlier */

I believe you mean "if (++idx == n)" here. (The post-increment
version can be made to work by changing other values, but it is
then not equivalent to the "idx = (head + i) % n" method.)

Or, one could say:

idx = (head + i) % n; /* if (idx++ == n) is faster, but less correct */

which suggests perhaps you are doing premature optimization. :-)

{
idx = 0;
}
proc(arr[idx]);
}

Also, usually one wants to process first, then test ++idx against n.
(Again, "increment before processing" can be made to work, but is
more complicated.)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
.



Relevant Pages

  • Re: circular shift array
    ... is to maintain an object whose purpose is to ... store the index of the first item. ... idx = head; ... Richard Heathfield ...
    (comp.lang.c)
  • Re: bitshift operator problem
    ... That shouldn't be so: idx should ... did you discover that idx was zero (or have I not ... Keep in mind that when you assign a value to idx, the compiler is ... not obliged to store that value in idx' memory cell immediately. ...
    (comp.lang.c)
  • Re: Cells and saving as ASCII
    ... %store as ASCII-format ... idx = find ... The result file must be in ASCII-format. ... able to store it as an xls file: ...
    (comp.soft-sys.matlab)