POV-Ray : Newsgroups : povray.general : priority queue : Re: priority queue Server Time
30 Jul 2024 06:19:46 EDT (-0400)
  Re: priority queue  
From: clipka
Date: 16 Jul 2009 06:30:00
Message: <web.4a5f00b2d5f9581db7c8d1200@news.povray.org>
"Anthony D. Baye" <Sha### [at] spamnomorehotmailcom> 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

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.