Squeak
  links to this page:    
View this PageEdit this PageUploads to this PageHistory of this PageTop of the SwikiRecent ChangesSearch the SwikiHelp Guide
RingBuffer
Last updated at 5:07 pm UTC on 16 January 2006
RingBuffer implemets a very simple circular queue of fixed size based on an array with two pointer to the first and last value.

Very similar to OrderedCollection in structure, the main difference is the fixed size.

In the case of need to add an element and the RingBuffer is full, the first (older) element is overwritten.

When created, the RingBuffer is filled with nil's. you can use the #addAll: aValue to put aValue in all the positions of the RingBuffer, or #array: anArray to initialize the posiitons with a given array.

Usage:
rb _ RingBuffer new: 10 "For a ten elements queue"
rb add: 5 "add the value 5 in the next position"
rb at: 1 "access to a element in the first position. May be useful, may be not"
value _ rb rem "gets the first value in the queue"


Goodie:
Declare in SequenceableCollection a method to convert any collection in a RingBuffer. I use:
"asRingBuffer
"converts a collection into a RingBuffer of the size of the collection, and assumed as full"
| ringBuf |
ringBuf _ RingBuffer new: (self size).
ringBuf array: self asArray.
^ringBuf
"

As all Squeak code, criticism and enhancement are both welcomed.
Fredy Hernandez
fredyh@netgate.com.uy
RingBuffer.st 20/04/2002 17:07 (GMT -3)