@llblab/uniqueue 1.1.0 → 1.2.0
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 +4 -4
- package/dist/index.d.ts +3 -3
- package/dist/index.js +67 -76
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -11,7 +11,7 @@ Combines a binary heap with a key-to-index `Map`, enabling O(log n) inserts/upda
|
|
|
11
11
|
- **O(1) Lookup**: Tracks item positions internally, avoiding O(n) scans for updates.
|
|
12
12
|
- **Max Size Eviction**: Automatically removes the lowest-priority item when the limit is reached.
|
|
13
13
|
- **Iterable**: Supports `for...of` iteration over items.
|
|
14
|
-
- **Zero Dependencies**: ~
|
|
14
|
+
- **Zero Dependencies**: ~1.7KB minified, ~0.7KB gzipped.
|
|
15
15
|
|
|
16
16
|
## Installation
|
|
17
17
|
|
|
@@ -24,10 +24,10 @@ npm install @llblab/uniqueue
|
|
|
24
24
|
### Basic Example (Max Heap)
|
|
25
25
|
|
|
26
26
|
```javascript
|
|
27
|
-
import {
|
|
27
|
+
import { Uniqueue } from "@llblab/uniqueue";
|
|
28
28
|
|
|
29
29
|
// Create a max-heap for leaderboard scores
|
|
30
|
-
const leaderboard = new
|
|
30
|
+
const leaderboard = new Uniqueue({
|
|
31
31
|
compare: (a, b) => b.score - a.score, // Sort by score descending
|
|
32
32
|
extractKey: (item) => item.playerId, // Unique by playerId
|
|
33
33
|
maxSize: 3, // Keep only top 3 scores
|
|
@@ -60,7 +60,7 @@ for (const player of leaderboard) {
|
|
|
60
60
|
|
|
61
61
|
## API
|
|
62
62
|
|
|
63
|
-
### `new
|
|
63
|
+
### `new Uniqueue(options)`
|
|
64
64
|
|
|
65
65
|
Creates a new priority queue instance.
|
|
66
66
|
|
package/dist/index.d.ts
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
|
-
export interface
|
|
1
|
+
export interface UniqueueOptions<T> {
|
|
2
2
|
data?: T[];
|
|
3
3
|
maxSize?: number;
|
|
4
4
|
compare?: (a: T, b: T) => number;
|
|
5
5
|
extractKey?: (item: T) => string;
|
|
6
6
|
}
|
|
7
|
-
export declare class
|
|
7
|
+
export declare class Uniqueue<T> {
|
|
8
8
|
#private;
|
|
9
9
|
data: T[];
|
|
10
10
|
indexes: Map<string, number>;
|
|
11
|
-
constructor({ data, maxSize, compare, extractKey, }?:
|
|
11
|
+
constructor({ data, maxSize, compare, extractKey, }?: UniqueueOptions<T>);
|
|
12
12
|
push(item: T): T | undefined;
|
|
13
13
|
pop(): T | undefined;
|
|
14
14
|
peek(): T | undefined;
|
package/dist/index.js
CHANGED
|
@@ -1,62 +1,94 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
8
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
9
|
-
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
10
|
-
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
11
|
-
};
|
|
12
|
-
var _UniQueue_instances, _UniQueue_maxSize, _UniQueue_compare, _UniQueue_extractKey, _UniQueue_siftUp, _UniQueue_siftDown;
|
|
13
|
-
export class UniQueue {
|
|
1
|
+
export class Uniqueue {
|
|
2
|
+
data;
|
|
3
|
+
indexes;
|
|
4
|
+
#maxSize;
|
|
5
|
+
#compare;
|
|
6
|
+
#extractKey;
|
|
14
7
|
constructor({ data = [], maxSize = Infinity, compare = (a, b) => (a < b ? -1 : a > b ? 1 : 0), extractKey = (item) => item, } = {}) {
|
|
15
|
-
_UniQueue_instances.add(this);
|
|
16
|
-
_UniQueue_maxSize.set(this, void 0);
|
|
17
|
-
_UniQueue_compare.set(this, void 0);
|
|
18
|
-
_UniQueue_extractKey.set(this, void 0);
|
|
19
8
|
this.data = data;
|
|
20
9
|
this.indexes = new Map(data.map((item, index) => [extractKey(item), index]));
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
10
|
+
this.#maxSize = maxSize;
|
|
11
|
+
this.#compare = compare;
|
|
12
|
+
this.#extractKey = extractKey;
|
|
24
13
|
if (data.length > 0) {
|
|
25
14
|
for (let i = (data.length >>> 1) - 1; i >= 0; i--) {
|
|
26
|
-
|
|
15
|
+
this.#siftDown(i);
|
|
27
16
|
}
|
|
28
17
|
}
|
|
29
18
|
}
|
|
19
|
+
#siftUp(pos) {
|
|
20
|
+
const { data, indexes } = this;
|
|
21
|
+
const compare = this.#compare;
|
|
22
|
+
const extractKey = this.#extractKey;
|
|
23
|
+
const item = data[pos];
|
|
24
|
+
while (pos > 0) {
|
|
25
|
+
const parentIndex = (pos - 1) >>> 1;
|
|
26
|
+
const parent = data[parentIndex];
|
|
27
|
+
if (compare(item, parent) >= 0)
|
|
28
|
+
break;
|
|
29
|
+
indexes.set(extractKey(parent), pos);
|
|
30
|
+
data[pos] = parent;
|
|
31
|
+
pos = parentIndex;
|
|
32
|
+
}
|
|
33
|
+
indexes.set(extractKey(item), pos);
|
|
34
|
+
data[pos] = item;
|
|
35
|
+
}
|
|
36
|
+
#siftDown(pos) {
|
|
37
|
+
const { data, indexes } = this;
|
|
38
|
+
const compare = this.#compare;
|
|
39
|
+
const extractKey = this.#extractKey;
|
|
40
|
+
const item = data[pos];
|
|
41
|
+
const halfLength = data.length >>> 1;
|
|
42
|
+
while (pos < halfLength) {
|
|
43
|
+
let leftIndex = (pos << 1) + 1;
|
|
44
|
+
let best = data[leftIndex];
|
|
45
|
+
const rightIndex = leftIndex + 1;
|
|
46
|
+
if (rightIndex < data.length) {
|
|
47
|
+
const right = data[rightIndex];
|
|
48
|
+
if (compare(right, best) < 0) {
|
|
49
|
+
leftIndex = rightIndex;
|
|
50
|
+
best = right;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
if (compare(best, item) >= 0)
|
|
54
|
+
break;
|
|
55
|
+
indexes.set(extractKey(best), pos);
|
|
56
|
+
data[pos] = best;
|
|
57
|
+
pos = leftIndex;
|
|
58
|
+
}
|
|
59
|
+
indexes.set(extractKey(item), pos);
|
|
60
|
+
data[pos] = item;
|
|
61
|
+
}
|
|
30
62
|
push(item) {
|
|
31
|
-
const key =
|
|
63
|
+
const key = this.#extractKey(item);
|
|
32
64
|
const index = this.indexes.get(key);
|
|
33
65
|
if (index === undefined) {
|
|
34
66
|
this.data.push(item);
|
|
35
|
-
|
|
36
|
-
if (this.data.length <=
|
|
67
|
+
this.#siftUp(this.data.length - 1);
|
|
68
|
+
if (this.data.length <= this.#maxSize)
|
|
37
69
|
return;
|
|
38
70
|
return this.pop();
|
|
39
71
|
}
|
|
40
72
|
const oldItem = this.data[index];
|
|
41
73
|
this.data[index] = item;
|
|
42
|
-
const cmp =
|
|
74
|
+
const cmp = this.#compare(oldItem, item);
|
|
43
75
|
if (cmp < 0) {
|
|
44
|
-
|
|
76
|
+
this.#siftDown(index);
|
|
45
77
|
}
|
|
46
78
|
else if (cmp > 0) {
|
|
47
|
-
|
|
79
|
+
this.#siftUp(index);
|
|
48
80
|
}
|
|
49
81
|
}
|
|
50
82
|
pop() {
|
|
51
83
|
if (this.data.length === 0)
|
|
52
84
|
return;
|
|
53
85
|
const top = this.data[0];
|
|
54
|
-
this.indexes.delete(
|
|
86
|
+
this.indexes.delete(this.#extractKey(top));
|
|
55
87
|
const bottom = this.data.pop();
|
|
56
88
|
if (this.data.length > 0 && bottom !== undefined) {
|
|
57
|
-
this.indexes.set(
|
|
89
|
+
this.indexes.set(this.#extractKey(bottom), 0);
|
|
58
90
|
this.data[0] = bottom;
|
|
59
|
-
|
|
91
|
+
this.#siftDown(0);
|
|
60
92
|
}
|
|
61
93
|
return top;
|
|
62
94
|
}
|
|
@@ -75,14 +107,14 @@ export class UniQueue {
|
|
|
75
107
|
}
|
|
76
108
|
const item = this.data.pop();
|
|
77
109
|
this.indexes.delete(key);
|
|
78
|
-
this.indexes.set(
|
|
110
|
+
this.indexes.set(this.#extractKey(item), index);
|
|
79
111
|
this.data[index] = item;
|
|
80
112
|
const parentIndex = (index - 1) >>> 1;
|
|
81
|
-
if (index > 0 &&
|
|
82
|
-
|
|
113
|
+
if (index > 0 && this.#compare(item, this.data[parentIndex]) < 0) {
|
|
114
|
+
this.#siftUp(index);
|
|
83
115
|
}
|
|
84
116
|
else {
|
|
85
|
-
|
|
117
|
+
this.#siftDown(index);
|
|
86
118
|
}
|
|
87
119
|
return true;
|
|
88
120
|
}
|
|
@@ -100,48 +132,7 @@ export class UniQueue {
|
|
|
100
132
|
get size() {
|
|
101
133
|
return this.data.length;
|
|
102
134
|
}
|
|
103
|
-
*[(
|
|
104
|
-
const { data, indexes } = this;
|
|
105
|
-
const compare = __classPrivateFieldGet(this, _UniQueue_compare, "f");
|
|
106
|
-
const extractKey = __classPrivateFieldGet(this, _UniQueue_extractKey, "f");
|
|
107
|
-
const item = data[pos];
|
|
108
|
-
while (pos > 0) {
|
|
109
|
-
const parentIndex = (pos - 1) >>> 1;
|
|
110
|
-
const parent = data[parentIndex];
|
|
111
|
-
if (compare(item, parent) >= 0)
|
|
112
|
-
break;
|
|
113
|
-
indexes.set(extractKey(parent), pos);
|
|
114
|
-
data[pos] = parent;
|
|
115
|
-
pos = parentIndex;
|
|
116
|
-
}
|
|
117
|
-
indexes.set(extractKey(item), pos);
|
|
118
|
-
data[pos] = item;
|
|
119
|
-
}, _UniQueue_siftDown = function _UniQueue_siftDown(pos) {
|
|
120
|
-
const { data, indexes } = this;
|
|
121
|
-
const compare = __classPrivateFieldGet(this, _UniQueue_compare, "f");
|
|
122
|
-
const extractKey = __classPrivateFieldGet(this, _UniQueue_extractKey, "f");
|
|
123
|
-
const item = data[pos];
|
|
124
|
-
const halfLength = data.length >>> 1;
|
|
125
|
-
while (pos < halfLength) {
|
|
126
|
-
let leftIndex = (pos << 1) + 1;
|
|
127
|
-
let best = data[leftIndex];
|
|
128
|
-
const rightIndex = leftIndex + 1;
|
|
129
|
-
if (rightIndex < data.length) {
|
|
130
|
-
const right = data[rightIndex];
|
|
131
|
-
if (compare(right, best) < 0) {
|
|
132
|
-
leftIndex = rightIndex;
|
|
133
|
-
best = right;
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
if (compare(best, item) >= 0)
|
|
137
|
-
break;
|
|
138
|
-
indexes.set(extractKey(best), pos);
|
|
139
|
-
data[pos] = best;
|
|
140
|
-
pos = leftIndex;
|
|
141
|
-
}
|
|
142
|
-
indexes.set(extractKey(item), pos);
|
|
143
|
-
data[pos] = item;
|
|
144
|
-
}, Symbol.iterator)]() {
|
|
135
|
+
*[Symbol.iterator]() {
|
|
145
136
|
yield* this.data;
|
|
146
137
|
}
|
|
147
138
|
}
|