Skip to content

Latest commit

 

History

History
43 lines (37 loc) · 1.35 KB

README.md

File metadata and controls

43 lines (37 loc) · 1.35 KB

CircularBuffer

GitHub issues github languages

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