1 // Fixed size ringbuffer
2 RingBuffer : SequenceableCollection
4 var <array, <readPos, <writePos;
6 *new { | size, collectionClass(Array) |
7 ^this.newCopyArgs(collectionClass.newClear(size), 0, 0)
10 // return maximum capacity.
14 // return number of readable items.
19 // return number of readble items.
21 ^if (readPos <= writePos)
22 { writePos - readPos }
23 { array.size - readPos + writePos }
25 // return number of writable items.
27 ^array.size - this.readable - 1
30 // add value and increase writePos.
31 // do nothing if no items can be written.
33 if (this.writable > 0) {
34 array.put(writePos, value);
35 writePos = (writePos + 1) % array.size;
38 // return next readable item and increase readPos.
39 // return nil if no items can be read.
42 if (this.readable > 0) {
43 result = array.at(readPos);
44 readPos = (readPos + 1) % array.size;
48 // add value and increase writePos by overwriting oldest readable
52 if (this.writable == 0) {
59 // iterate over the currently readable items.
61 var n = this.readable, i = 0;
63 function.value(array.wrapAt(readPos + i), i);