A queue is a syntax generated class, specific for a certain element type, that can be used very much like any built-in collection in CM.
Characteristics
- Preserves order (and behaves like a FIFO queue)
- Fixed size (see below)
- Small memory footprint
Performance
- Efficient enque and deque operations (as opposed to sequence)
- Small memory footprint (elementSize*count + small overhead), equal to sequence
Supported Element Types
All types are supported.
Fixed-size
The queue has a fixed size. This means that when it is full and we enqueue another element, the oldest element is dropped (overwritten). This makes the queue ideal to store for instance the last 100 mouse moves, without you having to worry about the size growing beyond control.
Good performance is achieved by storing the elements in an underlying fix array, and keeping a start and an end index (so the element stored at index 0 of the underlying array is not necessarily the first element in the queue).
NOTE! but what is the size and how to set it?? Calling constructor does not limit! Is this a bug? The size limit feature seems unused, but be wary:
use cm.util; public queue<int> MyQueue; { MyQueue queue(100); for (x in 0..1000) { queue.put(x); } for (z in queue, index=i) pln(#i; z); }
Example
use cm.util; public queue<str> MyStringQueue; { MyStringQueue queue(16); queue.put("aa"); // enque queue.put("bb"); // enque queue.put("cc"); // enque pln(#queue); str v1 = queue.get(); // deque pln(#v1); pln(#queue); str v2 = queue.peek(); pln(#v2); pln(#queue); }
Iteration
Built-in for iteration is fully supported.
for (z in queue) pln(z); for (z in queue, index=i) pln(#i; z); for (z in queue, reverse) pln(z);
Sorting
Sorting is not supported.
Comments
0 comments
Please sign in to leave a comment.