|
![](/i/fill.gif) |
"Anthony D. Baye" <Sha### [at] spamnomore hotmail com> wrote:
> I was, perhaps, unclear as to what I am looking for. All I need is a
> First-in-First-out structure. A plain old queue which, preferably, grows and
> shrinks with need.
If you can identify an upper limit for the size, a circular buffer would
probably be the easiest to implement, e.g.:
#declare MAX_FIFO_SIZE 1000;
#declare MyFifo = array[MAX_FIFO_SIZE];
#declare MyFifoInPos = 0;
#declare MyFifoOutPos = 0;
#declare MyFifoSize = 0;
// push
#if( MyFifoSize >= MAX_FIFO_SIZE )
// overrun
#else
#declare MyFifo[MyFifoInPos] = Whatever;
#declare MyFifoInPos = mod( MyFifoInPos + 1, MAX_FIFO_SIZE );
#declare MyFifoSize = MyFifoSize + 1;
#end
// pull
#if( MyFifoSize <= 0 )
// underrun
#else
#declare Whatever = MyFifo[MyFifoOutPos];
#declare MyFifoOutPos = mod( MyFifoOutPos + 1, MAX_FIFO_SIZE );
#declare MyFifoSize = MyFifoSize - 1;
#end
Note that you can handle overrun by resizing the array (by allocating a new one
and copying the data); in this case, being able to specify an absolute maximum
size beforehand is not required.
(You can get away without MyFifoSize, computing the size from MyFifoInPos and
MyFifoOutPos, but then the buffer can effectively hold at most MAX_FIFO_SIZE-1
elements.)
Post a reply to this message
|
![](/i/fill.gif) |