Re: circular shift array
- From: Chris Torek <nospam@xxxxxxxxx>
- Date: 31 Jul 2007 08:58:34 GMT
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.
.
- Follow-Ups:
- Re: circular shift array
- From: Richard Heathfield
- Re: circular shift array
- References:
- circular shift array
- From: Bint
- Re: circular shift array
- From: Richard Heathfield
- circular shift array
- Prev by Date: Re: Staic array de allocation
- Next by Date: Re: Redefine fprintf for debugging purposes
- Previous by thread: Re: circular shift array
- Next by thread: Re: circular shift array
- Index(es):
Relevant Pages
|