@llblab/uniqueue 1.2.1 → 1.3.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 CHANGED
@@ -75,7 +75,7 @@ Creates a new priority queue instance.
75
75
  | `data` | `T[]` | `[]` | Initial data array. |
76
76
  | `maxSize` | `number` | `Infinity` | Maximum number of items. If exceeded, lowest priority item is evicted. |
77
77
  | `compare` | `(a, b) => number` | `(a, b) => (a < b ? -1 : a > b ? 1 : 0)` | Comparison function for heap ordering. |
78
- | `extractKey` | `(item) => string` | `(item) => item` | Function to extract unique key string from item. |
78
+ | `extractKey` | `(item) => K` | `(item) => item` | Function to extract unique key any type from item. |
79
79
 
80
80
  ### Instance Methods
81
81
 
@@ -94,15 +94,15 @@ Removes and returns the highest priority item (the root of the heap).
94
94
 
95
95
  Returns the highest priority item without removing it.
96
96
 
97
- #### `remove(key: string): boolean`
97
+ #### `remove(key: K): boolean`
98
98
 
99
99
  Removes the item with the given key. Returns `true` if removed, `false` otherwise.
100
100
 
101
- #### `has(key: string): boolean`
101
+ #### `has(key: K): boolean`
102
102
 
103
103
  Checks if an item with the given key exists.
104
104
 
105
- #### `get(key: string): T | undefined`
105
+ #### `get(key: K): T | undefined`
106
106
 
107
107
  Returns the item with the given key without removing it.
108
108
 
package/dist/index.d.ts CHANGED
@@ -1,20 +1,20 @@
1
- export interface UniqueueOptions<T> {
1
+ export interface UniqueueOptions<T, K = string> {
2
2
  data?: T[];
3
3
  maxSize?: number;
4
4
  compare?: (a: T, b: T) => number;
5
- extractKey?: (item: T) => string;
5
+ extractKey?: (item: T) => K;
6
6
  }
7
- export declare class Uniqueue<T> {
7
+ export declare class Uniqueue<T, K = string> {
8
8
  #private;
9
9
  data: T[];
10
- indexes: Map<string, number>;
11
- constructor({ data, maxSize, compare, extractKey, }?: UniqueueOptions<T>);
10
+ indexes: Map<K, number>;
11
+ constructor({ data, maxSize, compare, extractKey, }?: UniqueueOptions<T, K>);
12
12
  push(item: T): T | undefined;
13
13
  pop(): T | undefined;
14
14
  peek(): T | undefined;
15
- remove(key: string): boolean;
16
- has(key: string): boolean;
17
- get(key: string): T | undefined;
15
+ remove(key: K): boolean;
16
+ has(key: K): boolean;
17
+ get(key: K): T | undefined;
18
18
  clear(): void;
19
19
  get size(): number;
20
20
  [Symbol.iterator](): IterableIterator<T>;
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,eAAe,CAAC,CAAC;IAChC,IAAI,CAAC,EAAE,CAAC,EAAE,CAAC;IACX,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,OAAO,CAAC,EAAE,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,KAAK,MAAM,CAAC;IACjC,UAAU,CAAC,EAAE,CAAC,IAAI,EAAE,CAAC,KAAK,MAAM,CAAC;CAClC;AAED,qBAAa,QAAQ,CAAC,CAAC;;IACrB,IAAI,EAAE,CAAC,EAAE,CAAC;IACV,OAAO,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;gBAMjB,EACV,IAAS,EACT,OAAkB,EAClB,OAAgD,EAChD,UAAgD,GACjD,GAAE,eAAe,CAAC,CAAC,CAAM;IAkE1B,IAAI,CAAC,IAAI,EAAE,CAAC,GAAG,CAAC,GAAG,SAAS;IAsB5B,GAAG,IAAI,CAAC,GAAG,SAAS;IAgBpB,IAAI,IAAI,CAAC,GAAG,SAAS;IAIrB,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO;IA0B5B,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO;IAIzB,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,CAAC,GAAG,SAAS;IAK/B,KAAK,IAAI,IAAI;IAKb,IAAI,IAAI,IAAI,MAAM,CAEjB;IAEA,CAAC,MAAM,CAAC,QAAQ,CAAC,IAAI,gBAAgB,CAAC,CAAC,CAAC;CAG1C"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,eAAe,CAAC,CAAC,EAAE,CAAC,GAAG,MAAM;IAC5C,IAAI,CAAC,EAAE,CAAC,EAAE,CAAC;IACX,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,OAAO,CAAC,EAAE,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,KAAK,MAAM,CAAC;IACjC,UAAU,CAAC,EAAE,CAAC,IAAI,EAAE,CAAC,KAAK,CAAC,CAAC;CAC7B;AAED,qBAAa,QAAQ,CAAC,CAAC,EAAE,CAAC,GAAG,MAAM;;IACjC,IAAI,EAAE,CAAC,EAAE,CAAC;IACV,OAAO,EAAE,GAAG,CAAC,CAAC,EAAE,MAAM,CAAC,CAAC;gBAMZ,EACV,IAAS,EACT,OAAkB,EAClB,OAAgD,EAChD,UAA2C,GAC5C,GAAE,eAAe,CAAC,CAAC,EAAE,CAAC,CAAM;IAyE7B,IAAI,CAAC,IAAI,EAAE,CAAC,GAAG,CAAC,GAAG,SAAS;IAsB5B,GAAG,IAAI,CAAC,GAAG,SAAS;IAgBpB,IAAI,IAAI,CAAC,GAAG,SAAS;IAIrB,MAAM,CAAC,GAAG,EAAE,CAAC,GAAG,OAAO;IA0BvB,GAAG,CAAC,GAAG,EAAE,CAAC,GAAG,OAAO;IAIpB,GAAG,CAAC,GAAG,EAAE,CAAC,GAAG,CAAC,GAAG,SAAS;IAK1B,KAAK,IAAI,IAAI;IAKb,IAAI,IAAI,IAAI,MAAM,CAEjB;IAEA,CAAC,MAAM,CAAC,QAAQ,CAAC,IAAI,gBAAgB,CAAC,CAAC,CAAC;CAG1C"}
package/dist/index.js CHANGED
@@ -5,13 +5,20 @@ export class Uniqueue {
5
5
  #compare;
6
6
  #extractKey;
7
7
  constructor({ data = [], maxSize = Infinity, compare = (a, b) => (a < b ? -1 : a > b ? 1 : 0), extractKey = (item) => item, } = {}) {
8
- this.data = data;
9
- this.indexes = new Map(data.map((item, index) => [extractKey(item), index]));
10
8
  this.#maxSize = maxSize;
11
9
  this.#compare = compare;
12
10
  this.#extractKey = extractKey;
13
- if (data.length > 0) {
14
- for (let i = (data.length >>> 1) - 1; i >= 0; i--) {
11
+ this.data = [];
12
+ this.indexes = new Map();
13
+ for (const item of data) {
14
+ const key = extractKey(item);
15
+ if (!this.indexes.has(key)) {
16
+ this.indexes.set(key, this.data.length);
17
+ this.data.push(item);
18
+ }
19
+ }
20
+ if (this.data.length > 0) {
21
+ for (let i = (this.data.length >>> 1) - 1; i >= 0; i--) {
15
22
  this.#siftDown(i);
16
23
  }
17
24
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@llblab/uniqueue",
3
- "version": "1.2.1",
3
+ "version": "1.3.0",
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",
@@ -28,7 +28,6 @@
28
28
  "deduplication",
29
29
  "leaderboard",
30
30
  "cache",
31
- "lru",
32
31
  "performance"
33
32
  ],
34
33
  "author": "LLB <shlavik@gmail.com>",