strong-trace
Version:
StrongTrace Node.js Tracer
42 lines (35 loc) • 978 B
JavaScript
;
module.exports = UniqueQueue
function UniqueQueue(initial) {
if (!(this instanceof UniqueQueue)) return new UniqueQueue(initial)
this.seen = []
this.todo = []
if (initial !== null) {
this.merge(initial)
}
}
// Does not accept null/undefined values
UniqueQueue.prototype.add = function add(element) {
if (element != null && this.seen.indexOf(element) < 0 && this.todo.indexOf(element) < 0) {
this.todo.push(element)
}
}
// Does not accept null/undefined values
UniqueQueue.prototype.merge = function merge(toAdd) {
for (var i = 0; i < toAdd.length; i++) {
if (toAdd[i] != null && this.seen.indexOf(toAdd[i]) < 0 && this.todo.indexOf(toAdd[i]) < 0) {
this.todo.push(toAdd[i])
}
}
}
UniqueQueue.prototype.hasNext = function hasNext() {
return this.todo.length > 0
}
UniqueQueue.prototype.next = function next() {
if (this.todo.length === 0) {
return
}
var n = this.todo.pop()
this.seen.push(n)
return n
}