46 lines
586 B
TypeScript
46 lines
586 B
TypeScript
import * as Events from 'events'
|
|
|
|
|
|
export class Queue<T> extends Events.EventEmitter {
|
|
private q : T[] = []
|
|
|
|
constructor() {
|
|
super()
|
|
}
|
|
|
|
isEmpty() : boolean {
|
|
return this.q.length == 0
|
|
}
|
|
|
|
knock() {
|
|
this.emit('data')
|
|
}
|
|
|
|
enq(x : T) {
|
|
this.q.push(x)
|
|
this.emit('data')
|
|
}
|
|
|
|
reenq(x : T) {
|
|
this.q.unshift(x)
|
|
this.emit('data')
|
|
}
|
|
|
|
deq() : T {
|
|
var x : T = this.peek()
|
|
this.q.shift()
|
|
|
|
return x
|
|
}
|
|
|
|
peek() : T {
|
|
if (this.isEmpty()) {
|
|
throw new Error("queue is empty")
|
|
}
|
|
|
|
var x : T = this.q[0]
|
|
|
|
return x
|
|
}
|
|
}
|