1 | var CircularBuffer = module.exports = function(size) {
|
2 | this.pos = 0;
|
3 | this._buf = [];
|
4 | this.size = size;
|
5 | }
|
6 |
|
7 | CircularBuffer.prototype.get = function(i) {
|
8 | if (i == undefined) i = 0;
|
9 | if (i >= this.size) return undefined;
|
10 | if (i >= this._buf.length) return undefined;
|
11 | return this._buf[(this.pos - i - 1) % this.size];
|
12 | }
|
13 |
|
14 | CircularBuffer.prototype.push = function(o) {
|
15 | this._buf[this.pos % this.size] = o;
|
16 | return this.pos++;
|
17 | }
|