mthb-circular-buffer
v0.0.45
Published
Circular buffer / ring buffer with overflow support
Downloads
17
Maintainers
Readme
CircularBuffer
import { CircularBuffer } from 'mthb-circular-buffer';
Allow OverFlow = true
var circularBuffer = new CircularBuffer(2 /*length*/, true /*allowOverFlow*/);
circularBuffer.push(1);
circularBuffer.push(2);
console.log(circularBuffer.size()); // 2
circularBuffer.push(3);
console.log(circularBuffer.size()); // 2
console.log(circularBuffer.pop()); // 2
console.log(circularBuffer.pop()); // 3
console.log(circularBuffer.size()); // 0
Allow OverFlow = false
var circularBuffer = new CircularBuffer(2 /*length*/, false /*allowOverFlow*/);
circularBuffer.push(1);
circularBuffer.push(2);
console.log(circularBuffer.pop()); // 1
console.log(circularBuffer.pop()); // 2
circularBuffer.push(3);
circularBuffer.push(4);
circularBuffer.push(5); // throw "overflow"
Description and Big O
Function | Big O | Description ---------|-------|------------ push | O(1)| push item to circular buffer pop | O(1) | pop item of circular buffer size | O(1) | size of circular buffer export | O(n) | pop all items of circular buffer clear | O(n) | clear all items of circular buffer