@llblab/uniqueue 1.1.0 → 1.1.1

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.
Files changed (3) hide show
  1. package/README.md +1 -1
  2. package/dist/index.js +66 -75
  3. 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**: ~1KB minified.
14
+ - **Zero Dependencies**: ~1.7KB minified, ~0.7KB gzipped.
15
15
 
16
16
  ## Installation
17
17
 
package/dist/index.js CHANGED
@@ -1,62 +1,94 @@
1
- var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
2
- if (kind === "m") throw new TypeError("Private method is not writable");
3
- if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
4
- if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
5
- return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
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
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
- __classPrivateFieldSet(this, _UniQueue_maxSize, maxSize, "f");
22
- __classPrivateFieldSet(this, _UniQueue_compare, compare, "f");
23
- __classPrivateFieldSet(this, _UniQueue_extractKey, extractKey, "f");
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
- __classPrivateFieldGet(this, _UniQueue_instances, "m", _UniQueue_siftDown).call(this, i);
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 = __classPrivateFieldGet(this, _UniQueue_extractKey, "f").call(this, item);
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
- __classPrivateFieldGet(this, _UniQueue_instances, "m", _UniQueue_siftUp).call(this, this.data.length - 1);
36
- if (this.data.length <= __classPrivateFieldGet(this, _UniQueue_maxSize, "f"))
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 = __classPrivateFieldGet(this, _UniQueue_compare, "f").call(this, oldItem, item);
74
+ const cmp = this.#compare(oldItem, item);
43
75
  if (cmp < 0) {
44
- __classPrivateFieldGet(this, _UniQueue_instances, "m", _UniQueue_siftDown).call(this, index);
76
+ this.#siftDown(index);
45
77
  }
46
78
  else if (cmp > 0) {
47
- __classPrivateFieldGet(this, _UniQueue_instances, "m", _UniQueue_siftUp).call(this, index);
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(__classPrivateFieldGet(this, _UniQueue_extractKey, "f").call(this, top));
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(__classPrivateFieldGet(this, _UniQueue_extractKey, "f").call(this, bottom), 0);
89
+ this.indexes.set(this.#extractKey(bottom), 0);
58
90
  this.data[0] = bottom;
59
- __classPrivateFieldGet(this, _UniQueue_instances, "m", _UniQueue_siftDown).call(this, 0);
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(__classPrivateFieldGet(this, _UniQueue_extractKey, "f").call(this, item), index);
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 && __classPrivateFieldGet(this, _UniQueue_compare, "f").call(this, item, this.data[parentIndex]) < 0) {
82
- __classPrivateFieldGet(this, _UniQueue_instances, "m", _UniQueue_siftUp).call(this, index);
113
+ if (index > 0 && this.#compare(item, this.data[parentIndex]) < 0) {
114
+ this.#siftUp(index);
83
115
  }
84
116
  else {
85
- __classPrivateFieldGet(this, _UniQueue_instances, "m", _UniQueue_siftDown).call(this, index);
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
- *[(_UniQueue_maxSize = new WeakMap(), _UniQueue_compare = new WeakMap(), _UniQueue_extractKey = new WeakMap(), _UniQueue_instances = new WeakSet(), _UniQueue_siftUp = function _UniQueue_siftUp(pos) {
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
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@llblab/uniqueue",
3
- "version": "1.1.0",
3
+ "version": "1.1.1",
4
4
  "description": "High-performance priority queue with unique key constraint (O(1) lookup, O(log n) update)",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",