Bret Comnes 80459935e1
0.0.1
2020-02-12 16:58:30 -07:00

33 lines
669 B
JavaScript

const FixedFIFO = require('./fixed-size')
module.exports = class FastFIFO {
constructor (hwm) {
this.hwm = hwm || 16
this.head = new FixedFIFO(this.hwm)
this.tail = this.head
}
push (val) {
if (!this.head.push(val)) {
const prev = this.head
this.head = prev.next = new FixedFIFO(2 * this.head.buffer.length)
this.head.push(val)
}
}
shift () {
const val = this.tail.shift()
if (val === undefined && this.tail.next) {
const next = this.tail.next
this.tail.next = null
this.tail = next
return this.tail.shift()
}
return val
}
isEmpty () {
return this.head.isEmpty()
}
}