}
++currentBucket
tail = tail.next
- tailChanged = true
}
+ tailChanged = tail !== this.tail
}
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const data = tail!.dequeue()
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
- if (tail!.empty() && tail!.next != null) {
- if (!tailChanged) {
+ if (tail!.empty()) {
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
+ if (!tailChanged && tail!.next != null) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
this.tail = tail!.next
- } else {
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
+ delete tail!.next
+ } else if (tailChanged) {
let node: PriorityQueueNode<T> | undefined = this.tail
while (node != null) {
- if (node.next === tail) {
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
+ if (node.next === tail && tail!.next != null) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
node.next = tail!.next
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
+ delete tail!.next
+ break
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
+ } else if (node.next === tail && tail!.next == null) {
+ delete node.next
+ this.head = node
break
}
node = node.next
}
}
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
- delete tail!.next
}
return data
}
{ data: 2, priority: 0 },
])
expect(priorityQueue.tail.next).toStrictEqual(priorityQueue.head)
+ expect(priorityQueue.tail).not.toStrictEqual(priorityQueue.head)
rtSize = priorityQueue.enqueue(3, -1)
expect(priorityQueue.buckets).toBe(2)
expect(priorityQueue.size).toBe(4)
{ data: 2, priority: 0 },
])
expect(priorityQueue.tail.next).toStrictEqual(priorityQueue.head)
+ expect(priorityQueue.tail).not.toStrictEqual(priorityQueue.head)
rtSize = priorityQueue.enqueue(1, 1)
expect(priorityQueue.buckets).toBe(2)
expect(priorityQueue.size).toBe(5)
{ data: 1, priority: 0 },
{ data: 2, priority: 0 },
])
- expect(priorityQueue.tail.next).not.toStrictEqual(priorityQueue.head)
expect(priorityQueue.tail.next.nodeArray).toMatchObject([
{ data: 3, priority: -1 },
{ data: 3, priority: 0 },
])
+ expect(priorityQueue.tail.next.next).toStrictEqual(priorityQueue.head)
+ expect(priorityQueue.tail.next).not.toStrictEqual(priorityQueue.head)
+ expect(priorityQueue.tail).not.toStrictEqual(priorityQueue.head)
rtSize = priorityQueue.enqueue(3, -2)
expect(priorityQueue.buckets).toBe(3)
expect(priorityQueue.size).toBe(6)
{ data: 1, priority: 0 },
{ data: 2, priority: 0 },
])
- expect(priorityQueue.tail.next).not.toStrictEqual(priorityQueue.head)
expect(priorityQueue.tail.next.nodeArray).toMatchObject([
{ data: 3, priority: -1 },
{ data: 3, priority: 0 },
])
+ expect(priorityQueue.tail.next.next).toStrictEqual(priorityQueue.head)
+ expect(priorityQueue.tail.next).not.toStrictEqual(priorityQueue.head)
+ expect(priorityQueue.tail).not.toStrictEqual(priorityQueue.head)
})
it('Verify default bucket size dequeue() behavior', () => {
expect(priorityQueue.maxSize).toBe(3)
expect(priorityQueue.tail.empty()).toBe(false)
expect(priorityQueue.tail.next).toBe(undefined)
+ expect(priorityQueue.tail).toStrictEqual(priorityQueue.head)
let rtItem = priorityQueue.dequeue()
expect(priorityQueue.buckets).toBe(0)
expect(priorityQueue.size).toBe(2)
expect(rtItem).toBe(2)
expect(priorityQueue.tail.empty()).toBe(false)
expect(priorityQueue.tail.next).toBe(undefined)
+ expect(priorityQueue.tail).toStrictEqual(priorityQueue.head)
rtItem = priorityQueue.dequeue()
expect(priorityQueue.buckets).toBe(0)
expect(priorityQueue.size).toBe(1)
expect(rtItem).toBe(1)
expect(priorityQueue.tail.empty()).toBe(false)
expect(priorityQueue.tail.next).toBe(undefined)
+ expect(priorityQueue.tail).toStrictEqual(priorityQueue.head)
rtItem = priorityQueue.dequeue()
expect(priorityQueue.buckets).toBe(0)
expect(priorityQueue.size).toBe(0)
expect(rtItem).toBe(3)
expect(priorityQueue.tail.empty()).toBe(true)
expect(priorityQueue.tail.next).toBe(undefined)
+ expect(priorityQueue.tail).toStrictEqual(priorityQueue.head)
})
it('Verify bucketSize=2 dequeue() behavior', () => {
expect(priorityQueue.maxSize).toBe(6)
expect(rtItem).toBe(1)
expect(priorityQueue.tail.empty()).toBe(false)
- expect(priorityQueue.tail.next).toBeInstanceOf(FixedPriorityQueue)
+ expect(priorityQueue.tail.next).toBe(undefined)
rtItem = priorityQueue.dequeue()
expect(priorityQueue.buckets).toBe(0)
expect(priorityQueue.size).toBe(0)