-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCircularBuffer.mon
77 lines (73 loc) · 1.84 KB
/
CircularBuffer.mon
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package com.apamax.containers;
/**
* A fixed-sized circular buffer object.
*
* Once the buffer is full, adding another value removes the oldest one in the buffer.
*/
event FixedSizeCircularBuffer
{
/** Create an empty buffer of a particular size.
* @param size The maximum capacity of the buffer.
*/
static action create(integer size) returns FixedSizeCircularBuffer
{
FixedSizeCircularBuffer buf := new FixedSizeCircularBuffer;
buf.data.setCapacity(size);
buf._capacity := size;
return buf;
}
/** Push a value onto the buffer.
* If the buffer is full, removes the oldest value.
* @param val The value to add to the buffer.
*/
action push(any val)
{
data.append(val);
if data.size() > _capacity {
data.remove(0);
}
}
/** Return the item currently at the given position in the buffer.
* @param offs The offset into the buffer, with 0 being the oldest and capacity-1 being the most recent.
*/
action get(integer offs) returns any
{
return data[offs];
}
/** Return the underlying sequence containing the circular buffer in order.
* This is returned by reference. You should not modify the return value, only clone it or iterate over it.
*/
action getData() returns sequence<any>
{
return data;
}
/** Return the capacity of this circular buffer. */
action capacity() returns integer
{
return _capacity;
}
/** Return the number of values in the buffer. */
action size() returns integer
{
return data.size();
}
/** Returns true if the buffer is empty. */
action empty() returns boolean
{
return data.size()=0;
}
/** Returns true if the buffer is full (size() == capacity()). */
action full() returns boolean
{
return data.size()=_capacity;
}
/** Remove all the values in the buffer. */
action clear()
{
data.clear();
}
/** @private */
sequence<any> data;
/** @private */
integer _capacity;
}