Commit | Line | Data |
---|---|---|
f8d5d8fd JB |
1 | /** |
2 | * Default buffer size. | |
3 | */ | |
4 | export const defaultQueueSize = 2048 | |
5 | ||
6 | /** | |
65c178f1 | 7 | * Fixed priority queue node. |
f8d5d8fd JB |
8 | * |
9 | * @typeParam T - Type of priority queue node data. | |
10 | * @internal | |
11 | */ | |
65c178f1 | 12 | export interface FixedPriorityQueueNode<T> { |
f8d5d8fd JB |
13 | data: T |
14 | priority: number | |
15 | } | |
16 | ||
eadb37e2 JB |
17 | /** |
18 | * Fixed priority queue. | |
19 | * | |
20 | * @typeParam T - Type of fixed priority queue data. | |
21 | * @internal | |
22 | */ | |
f8d5d8fd JB |
23 | export class FixedPriorityQueue<T> { |
24 | private start!: number | |
65c178f1 | 25 | private readonly nodeArray: Array<FixedPriorityQueueNode<T>> |
10b1b252 | 26 | /** The fixed priority queue capacity. */ |
9df282a0 | 27 | public readonly capacity: number |
10b1b252 | 28 | /** The fixed priority queue size */ |
f8d5d8fd | 29 | public size!: number |
f8d5d8fd | 30 | |
9df282a0 JB |
31 | /** |
32 | * Constructs a fixed priority queue. | |
33 | * | |
34 | * @param size - Fixed priority queue size. @defaultValue defaultQueueSize | |
35 | * @returns FixedPriorityQueue. | |
36 | */ | |
f8d5d8fd JB |
37 | constructor (size: number = defaultQueueSize) { |
38 | this.checkSize(size) | |
9df282a0 | 39 | this.capacity = size |
65c178f1 | 40 | this.nodeArray = new Array<FixedPriorityQueueNode<T>>(this.capacity) |
f8d5d8fd JB |
41 | this.clear() |
42 | } | |
43 | ||
e75373e6 JB |
44 | /** |
45 | * Checks if the fixed priority queue is empty. | |
46 | * | |
47 | * @returns `true` if the fixed priority queue is empty, `false` otherwise. | |
48 | */ | |
f8d5d8fd JB |
49 | public empty (): boolean { |
50 | return this.size === 0 | |
51 | } | |
52 | ||
e75373e6 JB |
53 | /** |
54 | * Checks if the fixed priority queue is full. | |
55 | * | |
56 | * @returns `true` if the fixed priority queue is full, `false` otherwise. | |
57 | */ | |
f8d5d8fd | 58 | public full (): boolean { |
9df282a0 | 59 | return this.size === this.capacity |
f8d5d8fd JB |
60 | } |
61 | ||
e75373e6 JB |
62 | /** |
63 | * Enqueue data into the fixed priority queue. | |
64 | * | |
65 | * @param data - Data to enqueue. | |
66 | * @param priority - Priority of the data. Lower values have higher priority. | |
67 | * @returns The new size of the priority queue. | |
68 | */ | |
f8d5d8fd JB |
69 | public enqueue (data: T, priority?: number): number { |
70 | if (this.full()) { | |
71 | throw new Error('Priority queue is full') | |
72 | } | |
73 | priority = priority ?? 0 | |
3451940f | 74 | let index = this.start |
f8d5d8fd | 75 | let inserted = false |
3451940f | 76 | for (let i = 0; i < this.size; i++) { |
f8d5d8fd JB |
77 | if (this.nodeArray[index]?.priority > priority) { |
78 | this.nodeArray.splice(index, 0, { data, priority }) | |
79 | inserted = true | |
80 | break | |
81 | } | |
02c769d0 | 82 | ++index |
9df282a0 | 83 | if (index === this.capacity) { |
3451940f JB |
84 | index = 0 |
85 | } | |
f8d5d8fd | 86 | } |
9df282a0 JB |
87 | this.nodeArray.length !== this.capacity && |
88 | (this.nodeArray.length = this.capacity) | |
f8d5d8fd JB |
89 | if (!inserted) { |
90 | let index = this.start + this.size | |
9df282a0 JB |
91 | if (index >= this.capacity) { |
92 | index -= this.capacity | |
f8d5d8fd JB |
93 | } |
94 | this.nodeArray[index] = { data, priority } | |
95 | } | |
10b1b252 | 96 | return ++this.size |
f8d5d8fd JB |
97 | } |
98 | ||
1d98bad2 JB |
99 | /** |
100 | * Gets data from the fixed priority queue. | |
101 | * | |
102 | * @param index - The index of the data to get. | |
103 | * @returns The data at the index or `undefined` if the fixed priority queue is empty or the index is out of bounds. | |
104 | */ | |
105 | public get (index: number): T | undefined { | |
106 | if (this.empty() || index >= this.size) { | |
107 | return undefined | |
108 | } | |
109 | index += this.start | |
110 | if (index >= this.capacity) { | |
111 | index -= this.capacity | |
112 | } | |
113 | return this.nodeArray[index].data | |
114 | } | |
115 | ||
e75373e6 JB |
116 | /** |
117 | * Dequeue data from the fixed priority queue. | |
118 | * | |
119 | * @returns The dequeued data or `undefined` if the priority queue is empty. | |
120 | */ | |
f8d5d8fd JB |
121 | public dequeue (): T | undefined { |
122 | if (this.empty()) { | |
123 | return undefined | |
124 | } | |
125 | const index = this.start | |
126 | --this.size | |
127 | ++this.start | |
9df282a0 | 128 | if (this.start === this.capacity) { |
f8d5d8fd JB |
129 | this.start = 0 |
130 | } | |
131 | return this.nodeArray[index].data | |
132 | } | |
133 | ||
134 | /** | |
135 | * Clears the fixed priority queue. | |
136 | */ | |
137 | public clear (): void { | |
138 | this.start = 0 | |
139 | this.size = 0 | |
f8d5d8fd JB |
140 | } |
141 | ||
142 | /** | |
143 | * Returns an iterator for the fixed priority queue. | |
144 | * | |
145 | * @returns An iterator for the fixed priority queue. | |
146 | * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols | |
147 | */ | |
9df282a0 | 148 | public [Symbol.iterator] (): Iterator<T> { |
3451940f JB |
149 | let index = this.start |
150 | let i = 0 | |
f8d5d8fd JB |
151 | return { |
152 | next: () => { | |
153 | if (i >= this.size) { | |
154 | return { | |
155 | value: undefined, | |
156 | done: true | |
157 | } | |
158 | } | |
3451940f | 159 | const value = this.nodeArray[index].data |
02c769d0 JB |
160 | ++index |
161 | ++i | |
9df282a0 | 162 | if (index === this.capacity) { |
3451940f JB |
163 | index = 0 |
164 | } | |
f8d5d8fd JB |
165 | return { |
166 | value, | |
167 | done: false | |
168 | } | |
169 | } | |
170 | } | |
171 | } | |
172 | ||
e75373e6 JB |
173 | /** |
174 | * Checks the size. | |
175 | * | |
176 | * @param size - The size to check. | |
177 | */ | |
f8d5d8fd JB |
178 | private checkSize (size: number): void { |
179 | if (!Number.isSafeInteger(size)) { | |
180 | throw new TypeError( | |
f45a8925 | 181 | `Invalid fixed priority queue size: '${size}' is not an integer` |
f8d5d8fd JB |
182 | ) |
183 | } | |
184 | if (size < 0) { | |
185 | throw new RangeError(`Invalid fixed priority queue size: ${size} < 0`) | |
186 | } | |
187 | } | |
188 | } |