import {
EmptyMutableQueue,
MutableQueueSym
} from "@tsplus/stdlib/collections/mutable/MutableQueue/definition"
export class Unbounded implements MutableQueue {
readonly [MutableQueueSym]: MutableQueueSym = MutableQueueSym
private queue = new DoublyLinkedList();
[Hash.sym]() {
return Hash.randomCached(this)
}
[Equals.sym](that: unknown) {
return this === that
}
get size(): number {
return this.queue.length
}
get isEmpty(): boolean {
return this.size === 0
}
get isFull(): boolean {
return false
}
get capacity(): number {
return Number.MAX_SAFE_INTEGER
}
offer(a: A): boolean {
this.queue.add(a)
return true
}
offerAll(as: Collection): Chunk {
for (const a of as) {
this.offer(a)
}
return Chunk.empty()
}
poll(a: D): A | D {
if (this.isEmpty) {
return a
}
return this.queue.shift()!
}
pollUpTo(n: number): Chunk {
let result = Chunk.empty()
let count = 0
while (count < n) {
const elem = this.poll(EmptyMutableQueue)
if (elem === EmptyMutableQueue) {
break
}
result = result.append(elem)
count += 1
}
return result
}
[Symbol.iterator](): Iterator {
return this.queue[Symbol.iterator]()
}
}