Re: circular shift array
- From: Richard Heathfield <rjh@xxxxxxxxxxxxxxx>
- Date: Tue, 31 Jul 2007 09:33:36 +0000
Chris Torek said:
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.
Er, kind of. What I really meant was:
proc(arr[idx]);
if(++idx == n)
{
idx = 0;
}
(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. :-)
Well, it does avoid a division. Had I got it right, I think it would
have been a reasonable thing to do. :-)
<snip>
--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
.
- References:
- circular shift array
- From: Bint
- Re: circular shift array
- From: Richard Heathfield
- Re: circular shift array
- From: Chris Torek
- circular shift array
- Prev by Date: Re: Staic array de allocation
- Next by Date: Re: Staic array de allocation
- Previous by thread: Re: circular shift array
- Next by thread: circular shift array
- Index(es):
Relevant Pages
|