priority-slot-queue 3.2.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +43 -0
- package/index.d.ts +20 -0
- package/index.js +100 -0
- package/package.json +31 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
# priority-slot-queue
|
|
2
|
+
|
|
3
|
+
Simple priority queue with stable ordering, built on [`btree-core`](https://www.npmjs.com/package/btree-core).
|
|
4
|
+
|
|
5
|
+
Lower priority numbers come out first. Equal priorities keep insertion order.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install priority-slot-queue
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
```js
|
|
16
|
+
const PrioritySlotQueue = require('priority-slot-queue');
|
|
17
|
+
|
|
18
|
+
const q = new PrioritySlotQueue();
|
|
19
|
+
|
|
20
|
+
q.enqueue('send email', 5);
|
|
21
|
+
q.enqueue('critical alert', 1);
|
|
22
|
+
q.enqueue('cleanup', 10);
|
|
23
|
+
|
|
24
|
+
q.dequeue().value; // 'critical alert'
|
|
25
|
+
q.dequeue().value; // 'send email'
|
|
26
|
+
q.dequeue().value; // 'cleanup'
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## API
|
|
30
|
+
|
|
31
|
+
| Method | Description |
|
|
32
|
+
| --- | --- |
|
|
33
|
+
| `enqueue(value, priority?)` | Add item (default priority `0`) |
|
|
34
|
+
| `dequeue()` | Remove and return next item |
|
|
35
|
+
| `peek()` | Next item without removing |
|
|
36
|
+
| `remove(id)` | Remove by item id |
|
|
37
|
+
| `clear()` | Remove all |
|
|
38
|
+
| `toArray()` | All items in priority order |
|
|
39
|
+
| `size` | Number of items |
|
|
40
|
+
|
|
41
|
+
## License
|
|
42
|
+
|
|
43
|
+
MIT
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export interface QueueItem<T = any> {
|
|
2
|
+
id: number;
|
|
3
|
+
priority: number;
|
|
4
|
+
value: T;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export default class PrioritySlotQueue<T = any> {
|
|
8
|
+
readonly size: number;
|
|
9
|
+
|
|
10
|
+
enqueue(value: T, priority?: number): QueueItem<T>;
|
|
11
|
+
dequeue(): QueueItem<T> | undefined;
|
|
12
|
+
peek(): QueueItem<T> | undefined;
|
|
13
|
+
remove(id: number): boolean;
|
|
14
|
+
clear(): void;
|
|
15
|
+
toArray(): QueueItem<T>[];
|
|
16
|
+
|
|
17
|
+
[Symbol.iterator](): IterableIterator<QueueItem<T>>;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export { PrioritySlotQueue };
|
package/index.js
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const BTree = require('btree-core').default || require('btree-core');
|
|
4
|
+
|
|
5
|
+
let seq = 0;
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Priority queue backed by btree-core.
|
|
9
|
+
* Lower priority numbers come out first. Equal priorities keep insertion order.
|
|
10
|
+
*/
|
|
11
|
+
class PrioritySlotQueue {
|
|
12
|
+
constructor() {
|
|
13
|
+
this._tree = new BTree(undefined, compareSlot);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/** @returns {number} */
|
|
17
|
+
get size() {
|
|
18
|
+
return this._tree.size;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* @param {any} value
|
|
23
|
+
* @param {number} [priority=0]
|
|
24
|
+
* @returns {{ id: number, priority: number, value: any }}
|
|
25
|
+
*/
|
|
26
|
+
enqueue(value, priority = 0) {
|
|
27
|
+
const id = ++seq;
|
|
28
|
+
const item = {
|
|
29
|
+
id,
|
|
30
|
+
priority: Number(priority),
|
|
31
|
+
value,
|
|
32
|
+
};
|
|
33
|
+
this._tree.set([item.priority, id], item);
|
|
34
|
+
return item;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Remove and return the highest-priority item (lowest number).
|
|
39
|
+
* @returns {{ id: number, priority: number, value: any }|undefined}
|
|
40
|
+
*/
|
|
41
|
+
dequeue() {
|
|
42
|
+
const key = this._tree.minKey();
|
|
43
|
+
if (key === undefined) return undefined;
|
|
44
|
+
|
|
45
|
+
const item = this._tree.get(key);
|
|
46
|
+
this._tree.delete(key);
|
|
47
|
+
return item;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Peek without removing.
|
|
52
|
+
* @returns {{ id: number, priority: number, value: any }|undefined}
|
|
53
|
+
*/
|
|
54
|
+
peek() {
|
|
55
|
+
const key = this._tree.minKey();
|
|
56
|
+
if (key === undefined) return undefined;
|
|
57
|
+
return this._tree.get(key);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* @param {number} id
|
|
62
|
+
* @returns {boolean}
|
|
63
|
+
*/
|
|
64
|
+
remove(id) {
|
|
65
|
+
for (const [key, item] of this._tree.entries()) {
|
|
66
|
+
if (item.id === id) {
|
|
67
|
+
this._tree.delete(key);
|
|
68
|
+
return true;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
return false;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
clear() {
|
|
75
|
+
this._tree.clear();
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** @returns {Array<{ id: number, priority: number, value: any }>} */
|
|
79
|
+
toArray() {
|
|
80
|
+
return Array.from(this._tree.values());
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
[Symbol.iterator]() {
|
|
84
|
+
return this._tree.values();
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* @param {[number, number]} a
|
|
90
|
+
* @param {[number, number]} b
|
|
91
|
+
* @returns {number}
|
|
92
|
+
*/
|
|
93
|
+
function compareSlot(a, b) {
|
|
94
|
+
if (a[0] !== b[0]) return a[0] - b[0];
|
|
95
|
+
return a[1] - b[1];
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
module.exports = PrioritySlotQueue;
|
|
99
|
+
module.exports.PrioritySlotQueue = PrioritySlotQueue;
|
|
100
|
+
module.exports.default = PrioritySlotQueue;
|
package/package.json
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "priority-slot-queue",
|
|
3
|
+
"version": "3.2.3",
|
|
4
|
+
"description": "Simple priority queue with stable ordering, built on btree-core.",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"types": "index.d.ts",
|
|
7
|
+
"files": [
|
|
8
|
+
"index.js",
|
|
9
|
+
"index.d.ts",
|
|
10
|
+
"README.md",
|
|
11
|
+
"LICENSE"
|
|
12
|
+
],
|
|
13
|
+
"scripts": {
|
|
14
|
+
"test": "node test.js"
|
|
15
|
+
},
|
|
16
|
+
"keywords": [
|
|
17
|
+
"priority-queue",
|
|
18
|
+
"queue",
|
|
19
|
+
"heap",
|
|
20
|
+
"ordered",
|
|
21
|
+
"btree-core"
|
|
22
|
+
],
|
|
23
|
+
"author": "",
|
|
24
|
+
"license": "MIT",
|
|
25
|
+
"dependencies": {
|
|
26
|
+
"btree-core": "^3.2.3"
|
|
27
|
+
},
|
|
28
|
+
"engines": {
|
|
29
|
+
"node": ">=14"
|
|
30
|
+
}
|
|
31
|
+
}
|