@hardlydifficult/queue 1.1.2 → 1.1.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/README.md +61 -148
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @hardlydifficult/queue
|
|
2
2
|
|
|
3
|
-
A high-performance priority queue
|
|
3
|
+
A high-performance priority queue with O(1) enqueue/dequeue and FIFO ordering within priority levels.
|
|
4
4
|
|
|
5
5
|
## Installation
|
|
6
6
|
|
|
@@ -15,195 +15,108 @@ import { createPriorityQueue } from "@hardlydifficult/queue";
|
|
|
15
15
|
|
|
16
16
|
const queue = createPriorityQueue<string>();
|
|
17
17
|
|
|
18
|
-
queue.enqueue("low-priority", "low");
|
|
19
|
-
queue.enqueue("
|
|
20
|
-
queue.enqueue("
|
|
18
|
+
queue.enqueue("low-priority-task", "low");
|
|
19
|
+
queue.enqueue("critical-task", "high");
|
|
20
|
+
queue.enqueue("default-task"); // defaults to 'medium' priority
|
|
21
21
|
|
|
22
|
-
//
|
|
23
|
-
console.log(queue.dequeue()?.data); // "
|
|
24
|
-
console.log(queue.dequeue()?.data); // "
|
|
25
|
-
console.log(queue.dequeue()?.data); // "low-priority"
|
|
22
|
+
console.log(queue.dequeue()?.data); // "critical-task"
|
|
23
|
+
console.log(queue.dequeue()?.data); // "default-task"
|
|
24
|
+
console.log(queue.dequeue()?.data); // "low-priority-task"
|
|
26
25
|
```
|
|
27
26
|
|
|
28
27
|
## Core API
|
|
29
28
|
|
|
30
|
-
### createPriorityQueue
|
|
29
|
+
### createPriorityQueue<T>()
|
|
31
30
|
|
|
32
31
|
Creates a new priority queue instance.
|
|
33
32
|
|
|
34
33
|
```typescript
|
|
35
34
|
import { createPriorityQueue } from "@hardlydifficult/queue";
|
|
36
35
|
|
|
37
|
-
const queue = createPriorityQueue<
|
|
38
|
-
```
|
|
39
|
-
|
|
40
|
-
### PriorityQueue Interface
|
|
41
|
-
|
|
42
|
-
All queue operations are provided via the `PriorityQueue<T>` interface returned by `createPriorityQueue()`.
|
|
43
|
-
|
|
44
|
-
#### enqueue(data, priority?)
|
|
45
|
-
|
|
46
|
-
Adds an item to the queue.
|
|
47
|
-
|
|
48
|
-
| Parameter | Type | Default | Description |
|
|
49
|
-
|-----------|-----------|-----------|------------------------------|
|
|
50
|
-
| data | `T` | — | The item data to enqueue |
|
|
51
|
-
| priority | `Priority`| `"medium"`| Priority level: `"high"`, `"medium"`, or `"low"` |
|
|
52
|
-
|
|
53
|
-
Returns a `QueueItem<T>` with metadata including unique ID and enqueue timestamp.
|
|
54
|
-
|
|
55
|
-
```typescript
|
|
56
|
-
const item = queue.enqueue("task", "high");
|
|
57
|
-
console.log(item.id); // "q_1"
|
|
58
|
-
console.log(item.priority); // "high"
|
|
36
|
+
const queue = createPriorityQueue<string>();
|
|
59
37
|
```
|
|
60
38
|
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
Removes and returns the highest-priority item (FIFO within same priority).
|
|
64
|
-
|
|
65
|
-
```typescript
|
|
66
|
-
const item = queue.dequeue();
|
|
67
|
-
if (item) {
|
|
68
|
-
console.log(item.data); // "urgent"
|
|
69
|
-
}
|
|
70
|
-
```
|
|
39
|
+
### PriorityQueue<T> Methods
|
|
71
40
|
|
|
72
|
-
|
|
41
|
+
| Method | Description |
|
|
42
|
+
|--------|-------------|
|
|
43
|
+
| `enqueue(data, priority?)` | Add an item to the queue (default priority: `"medium"`) |
|
|
44
|
+
| `dequeue()` | Remove and return the highest-priority item (FIFO within same priority) |
|
|
45
|
+
| `peek()` | View the next item without removing it |
|
|
46
|
+
| `remove(id)` | Remove a specific item by its ID |
|
|
47
|
+
| `size` | Number of items in the queue |
|
|
48
|
+
| `isEmpty` | Whether the queue is empty |
|
|
49
|
+
| `onEnqueue(callback)` | Register a callback for enqueue events |
|
|
50
|
+
| `toArray()` | Get all items in dequeue order as an array |
|
|
51
|
+
| `clear()` | Remove all items from the queue |
|
|
52
|
+
| `updatePriority(id, newPriority)` | Change an item's priority |
|
|
53
|
+
| `moveBefore(itemId, beforeItemId)` | Move an item before another in its priority bucket |
|
|
54
|
+
| `moveToEnd(itemId)` | Move an item to the end of its priority bucket |
|
|
73
55
|
|
|
74
|
-
|
|
56
|
+
### QueueItem<T>
|
|
75
57
|
|
|
76
|
-
|
|
58
|
+
Represents an item in the queue with metadata:
|
|
77
59
|
|
|
78
60
|
```typescript
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
61
|
+
interface QueueItem<T> {
|
|
62
|
+
data: T;
|
|
63
|
+
priority: "high" | "medium" | "low";
|
|
64
|
+
enqueuedAt: number;
|
|
65
|
+
id: string;
|
|
82
66
|
}
|
|
83
67
|
```
|
|
84
68
|
|
|
85
|
-
|
|
69
|
+
### Priority Levels
|
|
86
70
|
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
```typescript
|
|
90
|
-
const item = queue.enqueue("remove-me");
|
|
91
|
-
const removed = queue.remove(item.id); // true
|
|
92
|
-
const absent = queue.remove("unknown"); // false
|
|
93
|
-
```
|
|
71
|
+
Items are dequeued in priority order: `high` → `medium` → `low`. Within the same priority, items follow FIFO (first-in-first-out) order.
|
|
94
72
|
|
|
95
|
-
|
|
73
|
+
## Observer Pattern
|
|
96
74
|
|
|
97
|
-
|
|
75
|
+
Register callbacks to be notified when items are enqueued:
|
|
98
76
|
|
|
99
77
|
```typescript
|
|
100
|
-
|
|
101
|
-
```
|
|
102
|
-
|
|
103
|
-
#### isEmpty
|
|
104
|
-
|
|
105
|
-
Readonly property indicating whether the queue is empty.
|
|
106
|
-
|
|
107
|
-
```typescript
|
|
108
|
-
console.log(queue.isEmpty); // false
|
|
109
|
-
```
|
|
110
|
-
|
|
111
|
-
#### onEnqueue(callback)
|
|
112
|
-
|
|
113
|
-
Registers a callback invoked whenever an item is enqueued.
|
|
114
|
-
|
|
115
|
-
Returns an unsubscribe function.
|
|
78
|
+
const queue = createPriorityQueue<string>();
|
|
116
79
|
|
|
117
|
-
```typescript
|
|
118
80
|
const unsubscribe = queue.onEnqueue((item) => {
|
|
119
|
-
console.log(
|
|
81
|
+
console.log(`Enqueued item: ${item.data}`);
|
|
120
82
|
});
|
|
121
83
|
|
|
122
|
-
queue.enqueue("
|
|
123
|
-
unsubscribe();
|
|
124
|
-
queue.enqueue("ignored"); // callback no longer invoked
|
|
125
|
-
```
|
|
126
|
-
|
|
127
|
-
#### toArray()
|
|
128
|
-
|
|
129
|
-
Returns a snapshot of all items in dequeue order (by priority, then FIFO). Does not modify the queue.
|
|
130
|
-
|
|
131
|
-
```typescript
|
|
132
|
-
const items = queue.toArray();
|
|
133
|
-
// Items sorted by priority: high → medium → low, then by insertion order
|
|
134
|
-
```
|
|
135
|
-
|
|
136
|
-
#### clear()
|
|
84
|
+
queue.enqueue("new task");
|
|
137
85
|
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
```typescript
|
|
141
|
-
queue.enqueue("a");
|
|
142
|
-
queue.enqueue("b");
|
|
143
|
-
queue.clear();
|
|
144
|
-
console.log(queue.size); // 0
|
|
86
|
+
unsubscribe(); // stop notifications
|
|
145
87
|
```
|
|
146
88
|
|
|
147
|
-
|
|
89
|
+
## Priority Manipulation
|
|
148
90
|
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
Changes the priority of an existing item.
|
|
91
|
+
Update priority and reorder items after enqueue:
|
|
152
92
|
|
|
153
93
|
```typescript
|
|
154
|
-
const
|
|
155
|
-
queue.updatePriority(item.id, "high"); // true
|
|
156
|
-
```
|
|
157
|
-
|
|
158
|
-
Returns `true` if the item was found and updated, `false` otherwise.
|
|
159
|
-
|
|
160
|
-
#### moveBefore(itemId, beforeItemId)
|
|
161
|
-
|
|
162
|
-
Moves an item before another item within the same priority bucket.
|
|
163
|
-
|
|
164
|
-
```typescript
|
|
165
|
-
const a = queue.enqueue("a", "medium");
|
|
166
|
-
const b = queue.enqueue("b", "medium");
|
|
167
|
-
const c = queue.enqueue("c", "medium");
|
|
168
|
-
|
|
169
|
-
queue.moveBefore(c.id, a.id); // true
|
|
170
|
-
console.log(queue.toArray().map(i => i.data)); // ["c", "a", "b"]
|
|
171
|
-
```
|
|
172
|
-
|
|
173
|
-
Both items must exist and share the same priority. Returns `true` on success.
|
|
94
|
+
const queue = createPriorityQueue<string>();
|
|
174
95
|
|
|
175
|
-
|
|
96
|
+
const a = queue.enqueue("task-a", "low");
|
|
97
|
+
queue.enqueue("task-b", "low");
|
|
98
|
+
queue.enqueue("task-c", "low");
|
|
176
99
|
|
|
177
|
-
|
|
100
|
+
// Move 'task-a' to high priority
|
|
101
|
+
queue.updatePriority(a.id, "high");
|
|
178
102
|
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
const b = queue.enqueue("b", "medium");
|
|
182
|
-
queue.moveToEnd(a.id); // true
|
|
183
|
-
console.log(queue.toArray().map(i => i.data)); // ["b", "a"]
|
|
103
|
+
// Move 'task-c' before 'task-b' in the low bucket
|
|
104
|
+
queue.moveBefore(queue.toArray()[2].id, queue.toArray()[1].id);
|
|
184
105
|
```
|
|
185
106
|
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
## QueueItem Type
|
|
189
|
-
|
|
190
|
-
Represents an item in the queue with metadata:
|
|
191
|
-
|
|
192
|
-
```typescript
|
|
193
|
-
interface QueueItem<T> {
|
|
194
|
-
data: T;
|
|
195
|
-
priority: Priority;
|
|
196
|
-
enqueuedAt: number; // epoch milliseconds
|
|
197
|
-
id: string; // unique identifier
|
|
198
|
-
}
|
|
199
|
-
```
|
|
107
|
+
## Reference
|
|
200
108
|
|
|
201
|
-
|
|
109
|
+
### Priority Order
|
|
202
110
|
|
|
203
|
-
Priority
|
|
111
|
+
| Priority | Dequeue Order |
|
|
112
|
+
|----------|---------------|
|
|
113
|
+
| `"high"` | First |
|
|
114
|
+
| `"medium"` | Second |
|
|
115
|
+
| `"low"` | Last |
|
|
204
116
|
|
|
205
|
-
|
|
206
|
-
2. `"medium"` (default)
|
|
207
|
-
3. `"low"`
|
|
117
|
+
### Time Complexity
|
|
208
118
|
|
|
209
|
-
|
|
119
|
+
- `enqueue`: O(1)
|
|
120
|
+
- `dequeue`: O(1)
|
|
121
|
+
- `remove`, `updatePriority`, `moveBefore`, `moveToEnd`: O(n)
|
|
122
|
+
- `toArray`, `peek`: O(1)
|