X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;ds=sidebyside;f=src%2Ffixed-priority-queue.ts;h=1204cf8153c7a7e8b9b9f4e774f4528ba5e9bdab;hb=3451940fcbcf9750ad6e702ca296be7f8560408e;hp=fadaee750515b3de72bcc6c5a9f6cd70de0b68cd;hpb=f45a8925025d4b76849ea359542bb6bafd39695d;p=poolifier.git diff --git a/src/fixed-priority-queue.ts b/src/fixed-priority-queue.ts index fadaee75..1204cf81 100644 --- a/src/fixed-priority-queue.ts +++ b/src/fixed-priority-queue.ts @@ -46,13 +46,18 @@ export class FixedPriorityQueue { } priority = priority ?? 0 const nodeArrayLength = this.nodeArray.length + let index = this.start let inserted = false - for (let index = this.start; index < nodeArrayLength; index++) { + for (let i = 0; i < this.size; i++) { if (this.nodeArray[index]?.priority > priority) { this.nodeArray.splice(index, 0, { data, priority }) inserted = true break } + index++ + if (index === nodeArrayLength) { + index = 0 + } } this.nodeArray.length !== nodeArrayLength && (this.nodeArray.length = nodeArrayLength) @@ -95,7 +100,8 @@ export class FixedPriorityQueue { * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols */ [Symbol.iterator] (): Iterator { - let i = this.start + let index = this.start + let i = 0 return { next: () => { if (i >= this.size) { @@ -104,8 +110,12 @@ export class FixedPriorityQueue { done: true } } - const value = this.nodeArray[i].data + const value = this.nodeArray[index].data + index++ i++ + if (index === this.nodeArray.length) { + index = 0 + } return { value, done: false