- Inherits from:
- Object
- Conforms to:
- DQueuable, DStackable
- Declared in:
- DCircle.h
Object
|
+---DCircle
Class Description
The circular buffer collection stores objects in an circular buffer that can grow.
Objects can be read by an index, which can be negative for accessing elements from
the end. This collection can be used for implementing a queue (FIFO) or a stack (LIFO).
- Example:
#include <stdio.h>
#include "ofc/DCircle.h"
#include "ofc/DInt.h"
int main(int argc, char *argv[])
{
DCircle *fifo = [DCircle alloc];
DCircle *lifo = [DCircle new ];
DInt *val;
[fifo init :20]; // Init with size 20
val = [DInt new]; [val set :1];
[fifo enqueue :val]; // Enqueue value 1 in lifo
val = [DInt new]; [val set :2]; // Enqueue value 2 in lifo
[fifo enqueue :val];
val = [fifo dequeue];
printf("First value in fifo:%d\n", (val != nil ? [val get] : -1));
[lifo size :20]; // Insure size in lifo
val = [DInt new]; [val set :3];
[lifo push :val]; // Push value 3 in lifo
val = [DInt new]; [val set :4]; // Push value 4 in lifo
[lifo push :val];
printf("Lifo length:%ld\n", [lifo length]);
val = [lifo tos];
printf("TOS in lifo:%d\n", (val != nil ? [val get] : -1));
[fifo free]; // Cleanup incl. stored objects
[lifo free];
return 0;
}
- Last modified:
- 19-Jul-2008 (DCircle.h)
Instance Variables
- private id *_objects
- the stored objects
- private long _size
- the size of the buffer
- private long _first
- the first element in the buffer (or -1 for empty)
- private long _next
- the next element in the buffer
- Constructors
- - (DCircle *) init
- Initialise to an empty buffer
- Returns:
- the object
- - (DCircle *) init :(long) size
- Initialise to a circular buffer with an initial size
- Parameters:
- size - the size of the buffer
- Returns:
- the object
- Copy related methods
- - deepen
- Deepen a copy of the buffer
- Returns:
- the object
- - shallowCopy
- Do a shallow copy of the object
- Returns:
- the object
- Destructor
- - free
- (Deep) free the buffer (incl. the stored objects)
- Returns:
- the object
- - shallowFree
- Free the buffer, not the stored objects
- Returns:
- the object
- Member methods
- - (BOOL) isEmpty
- Check if the buffer is empty
- Returns:
- is it ?
- - (BOOL) isFull
- Check if the buffer is full
- Returns:
- is it ?
- - (long) length
- Return the number of stored objects
- Returns:
- the length
- - (long) size
- Return the size of the buffer
- Returns:
- the size
- - (DCircle *) size :(long) size
- Set the size for the buffer
- Parameters:
- size - the new, larger size
- Returns:
- the object
- Index methods
- - (id) get :(long) index
- Return a reference to the indexth object
- Parameters:
- index - the index
- Returns:
- the object (or nil if invalid index)
- - (BOOL) isValid :(long) index
- Check if an index is valid
- Parameters:
- index - the index
- Returns:
- is it valid ?
- Stack/LIFO methods
- - (id) pop
- Pop an object from the stack (LIFO)
- Returns:
- the object (or nil if empty)
- - (BOOL) push :(id) obj
- Push an object on the stack (LIFO)
- Parameters:
- obj - the object to be pushed
- Returns:
- success (or buffer full)
- - (id) tos
- Return the object on top of the stack (LIFO)
(without changing the top of the stack)
- Returns:
- the object (or nil if empty)
- Queue/FIFO Methods
- - (id) dequeue
- Get an object from the queue (FIFO)
- Parameters:
- obj - the object
- Returns:
- the object (or nil for buffer empty)
- - (BOOL) enqueue :(id) obj
- Put an object in the queue (FIFO)
- Parameters:
- obj - the object
- Returns:
- success (or buffer full)
- Selector methods
- - (DCircle *) each :(SEL) sel
- Perform a selector on each object in the circular buffer,
starting from the first item in the buffer
- Parameters:
- sel - the selector
- Returns:
- the object
- - (DCircle *) reach :(SEL) sel
- Perform a selector on each object in the circular buffer,
starting from the last item in the buffer
- Parameters:
- sel - the selector
- Returns:
- the object
generated 06-Sep-2008 by ObjcDoc 3.0.0