data-structure-typed 2.4.4 → 2.5.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/CHANGELOG.md +22 -1
- package/README.md +34 -1
- package/dist/cjs/index.cjs +10639 -2151
- package/dist/cjs-legacy/index.cjs +10694 -2195
- package/dist/esm/index.mjs +10639 -2150
- package/dist/esm-legacy/index.mjs +10694 -2194
- package/dist/types/common/error.d.ts +23 -0
- package/dist/types/common/index.d.ts +1 -0
- package/dist/types/data-structures/base/iterable-element-base.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +128 -51
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +210 -164
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +439 -78
- package/dist/types/data-structures/binary-tree/bst.d.ts +311 -28
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +217 -31
- package/dist/types/data-structures/binary-tree/segment-tree.d.ts +218 -152
- package/dist/types/data-structures/binary-tree/tree-map.d.ts +1281 -5
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +1087 -201
- package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +858 -65
- package/dist/types/data-structures/binary-tree/tree-set.d.ts +1133 -5
- package/dist/types/data-structures/graph/abstract-graph.d.ts +44 -0
- package/dist/types/data-structures/graph/directed-graph.d.ts +220 -47
- package/dist/types/data-structures/graph/map-graph.d.ts +59 -1
- package/dist/types/data-structures/graph/undirected-graph.d.ts +218 -59
- package/dist/types/data-structures/hash/hash-map.d.ts +230 -77
- package/dist/types/data-structures/heap/heap.d.ts +287 -99
- package/dist/types/data-structures/heap/max-heap.d.ts +46 -0
- package/dist/types/data-structures/heap/min-heap.d.ts +59 -0
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +286 -44
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +278 -65
- package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +415 -12
- package/dist/types/data-structures/matrix/matrix.d.ts +331 -0
- package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +57 -0
- package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +60 -0
- package/dist/types/data-structures/priority-queue/priority-queue.d.ts +60 -0
- package/dist/types/data-structures/queue/deque.d.ts +313 -66
- package/dist/types/data-structures/queue/queue.d.ts +211 -42
- package/dist/types/data-structures/stack/stack.d.ts +174 -32
- package/dist/types/data-structures/trie/trie.d.ts +213 -43
- package/dist/types/types/data-structures/binary-tree/segment-tree.d.ts +1 -1
- package/dist/types/types/data-structures/linked-list/skip-linked-list.d.ts +1 -4
- package/dist/types/types/data-structures/queue/deque.d.ts +6 -0
- package/dist/umd/data-structure-typed.js +10725 -2221
- package/dist/umd/data-structure-typed.min.js +4 -2
- package/package.json +5 -4
- package/src/common/error.ts +60 -0
- package/src/common/index.ts +2 -0
- package/src/data-structures/base/iterable-element-base.ts +2 -2
- package/src/data-structures/binary-tree/avl-tree.ts +146 -51
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +317 -247
- package/src/data-structures/binary-tree/binary-tree.ts +567 -121
- package/src/data-structures/binary-tree/bst.ts +370 -37
- package/src/data-structures/binary-tree/red-black-tree.ts +328 -96
- package/src/data-structures/binary-tree/segment-tree.ts +378 -248
- package/src/data-structures/binary-tree/tree-map.ts +1411 -13
- package/src/data-structures/binary-tree/tree-multi-map.ts +1218 -215
- package/src/data-structures/binary-tree/tree-multi-set.ts +959 -69
- package/src/data-structures/binary-tree/tree-set.ts +1257 -15
- package/src/data-structures/graph/abstract-graph.ts +106 -1
- package/src/data-structures/graph/directed-graph.ts +233 -47
- package/src/data-structures/graph/map-graph.ts +59 -1
- package/src/data-structures/graph/undirected-graph.ts +308 -59
- package/src/data-structures/hash/hash-map.ts +254 -79
- package/src/data-structures/heap/heap.ts +305 -102
- package/src/data-structures/heap/max-heap.ts +48 -3
- package/src/data-structures/heap/min-heap.ts +59 -0
- package/src/data-structures/linked-list/doubly-linked-list.ts +303 -44
- package/src/data-structures/linked-list/singly-linked-list.ts +293 -65
- package/src/data-structures/linked-list/skip-linked-list.ts +707 -90
- package/src/data-structures/matrix/matrix.ts +433 -22
- package/src/data-structures/priority-queue/max-priority-queue.ts +59 -3
- package/src/data-structures/priority-queue/min-priority-queue.ts +60 -0
- package/src/data-structures/priority-queue/priority-queue.ts +60 -0
- package/src/data-structures/queue/deque.ts +358 -68
- package/src/data-structures/queue/queue.ts +223 -42
- package/src/data-structures/stack/stack.ts +184 -32
- package/src/data-structures/trie/trie.ts +227 -44
- package/src/types/data-structures/binary-tree/segment-tree.ts +1 -1
- package/src/types/data-structures/linked-list/skip-linked-list.ts +2 -1
- package/src/types/data-structures/queue/deque.ts +7 -0
- package/src/utils/utils.ts +4 -2
|
@@ -9,6 +9,89 @@ import type { MatrixOptions } from '../../types';
|
|
|
9
9
|
/**
|
|
10
10
|
*
|
|
11
11
|
*/
|
|
12
|
+
/**
|
|
13
|
+
* Matrix — a numeric matrix with standard linear algebra operations.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* // Basic matrix arithmetic
|
|
17
|
+
* const a = new Matrix([
|
|
18
|
+
* [1, 2],
|
|
19
|
+
* [3, 4]
|
|
20
|
+
* ]);
|
|
21
|
+
* const b = new Matrix([
|
|
22
|
+
* [5, 6],
|
|
23
|
+
* [7, 8]
|
|
24
|
+
* ]);
|
|
25
|
+
*
|
|
26
|
+
* const sum = a.add(b);
|
|
27
|
+
* console.log(sum?.data); // [
|
|
28
|
+
* // [6, 8],
|
|
29
|
+
* // [10, 12]
|
|
30
|
+
* // ];
|
|
31
|
+
*
|
|
32
|
+
* const diff = b.subtract(a);
|
|
33
|
+
* console.log(diff?.data); // [
|
|
34
|
+
* // [4, 4],
|
|
35
|
+
* // [4, 4]
|
|
36
|
+
* // ];
|
|
37
|
+
* @example
|
|
38
|
+
* // Matrix multiplication for transformations
|
|
39
|
+
* // 2x3 matrix * 3x2 matrix = 2x2 matrix
|
|
40
|
+
* const a = new Matrix([
|
|
41
|
+
* [1, 2, 3],
|
|
42
|
+
* [4, 5, 6]
|
|
43
|
+
* ]);
|
|
44
|
+
* const b = new Matrix([
|
|
45
|
+
* [7, 8],
|
|
46
|
+
* [9, 10],
|
|
47
|
+
* [11, 12]
|
|
48
|
+
* ]);
|
|
49
|
+
*
|
|
50
|
+
* const product = a.multiply(b);
|
|
51
|
+
* console.log(product?.rows); // 2;
|
|
52
|
+
* console.log(product?.cols); // 2;
|
|
53
|
+
* // Row 0: 1*7+2*9+3*11=58, 1*8+2*10+3*12=64
|
|
54
|
+
* // Row 1: 4*7+5*9+6*11=139, 4*8+5*10+6*12=154
|
|
55
|
+
* console.log(product?.data); // [
|
|
56
|
+
* // [58, 64],
|
|
57
|
+
* // [139, 154]
|
|
58
|
+
* // ];
|
|
59
|
+
* @example
|
|
60
|
+
* // Matrix transpose (square matrix)
|
|
61
|
+
* const m = new Matrix([
|
|
62
|
+
* [1, 2, 3],
|
|
63
|
+
* [4, 5, 6],
|
|
64
|
+
* [7, 8, 9]
|
|
65
|
+
* ]);
|
|
66
|
+
*
|
|
67
|
+
* const transposed = m.transpose();
|
|
68
|
+
* console.log(transposed.rows); // 3;
|
|
69
|
+
* console.log(transposed.cols); // 3;
|
|
70
|
+
* console.log(transposed.data); // [
|
|
71
|
+
* // [1, 4, 7],
|
|
72
|
+
* // [2, 5, 8],
|
|
73
|
+
* // [3, 6, 9]
|
|
74
|
+
* // ];
|
|
75
|
+
*
|
|
76
|
+
* // Transpose of transpose = original
|
|
77
|
+
* console.log(transposed.transpose().data); // m.data;
|
|
78
|
+
* @example
|
|
79
|
+
* // Get and set individual cells
|
|
80
|
+
* const m = new Matrix([
|
|
81
|
+
* [0, 0, 0],
|
|
82
|
+
* [0, 0, 0]
|
|
83
|
+
* ]);
|
|
84
|
+
*
|
|
85
|
+
* m.set(0, 1, 42);
|
|
86
|
+
* m.set(1, 2, 99);
|
|
87
|
+
*
|
|
88
|
+
* console.log(m.get(0, 1)); // 42;
|
|
89
|
+
* console.log(m.get(1, 2)); // 99;
|
|
90
|
+
* console.log(m.get(0, 0)); // 0;
|
|
91
|
+
*
|
|
92
|
+
* // Out of bounds returns undefined
|
|
93
|
+
* console.log(m.get(5, 5)); // undefined;
|
|
94
|
+
*/
|
|
12
95
|
export declare class Matrix {
|
|
13
96
|
/**
|
|
14
97
|
* The constructor function initializes a matrix object with the provided data and options, or with
|
|
@@ -59,6 +142,33 @@ export declare class Matrix {
|
|
|
59
142
|
* retrieve from the data array.
|
|
60
143
|
* @returns The `get` function returns a number if the provided row and column indices are valid.
|
|
61
144
|
* Otherwise, it returns `undefined`.
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
* @example
|
|
157
|
+
* // Get and set individual cells
|
|
158
|
+
* const m = new Matrix([
|
|
159
|
+
* [0, 0, 0],
|
|
160
|
+
* [0, 0, 0]
|
|
161
|
+
* ]);
|
|
162
|
+
*
|
|
163
|
+
* m.set(0, 1, 42);
|
|
164
|
+
* m.set(1, 2, 99);
|
|
165
|
+
*
|
|
166
|
+
* console.log(m.get(0, 1)); // 42;
|
|
167
|
+
* console.log(m.get(1, 2)); // 99;
|
|
168
|
+
* console.log(m.get(0, 0)); // 0;
|
|
169
|
+
*
|
|
170
|
+
* // Out of bounds returns undefined
|
|
171
|
+
* console.log(m.get(5, 5)); // undefined;
|
|
62
172
|
*/
|
|
63
173
|
get(row: number, col: number): number | undefined;
|
|
64
174
|
/**
|
|
@@ -72,6 +182,24 @@ export declare class Matrix {
|
|
|
72
182
|
* @returns a boolean value. It returns true if the index (row, col) is valid and the value is
|
|
73
183
|
* successfully set in the data array. It returns false if the index is invalid and the value is not
|
|
74
184
|
* set.
|
|
185
|
+
|
|
186
|
+
|
|
187
|
+
|
|
188
|
+
|
|
189
|
+
|
|
190
|
+
|
|
191
|
+
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
|
|
195
|
+
|
|
196
|
+
* @example
|
|
197
|
+
* // Modify individual cells
|
|
198
|
+
* const m = Matrix.zeros(2, 2);
|
|
199
|
+
* console.log(m.set(0, 0, 5)); // true;
|
|
200
|
+
* console.log(m.set(1, 1, 10)); // true;
|
|
201
|
+
* console.log(m.get(0, 0)); // 5;
|
|
202
|
+
* console.log(m.get(1, 1)); // 10;
|
|
75
203
|
*/
|
|
76
204
|
set(row: number, col: number, value: number): boolean;
|
|
77
205
|
/**
|
|
@@ -86,6 +214,39 @@ export declare class Matrix {
|
|
|
86
214
|
* @param {Matrix} matrix - The `matrix` parameter is an instance of the `Matrix` class.
|
|
87
215
|
* @returns The `add` method returns a new `Matrix` object that represents the result of adding the
|
|
88
216
|
* current matrix with the provided `matrix` parameter.
|
|
217
|
+
|
|
218
|
+
|
|
219
|
+
|
|
220
|
+
|
|
221
|
+
|
|
222
|
+
|
|
223
|
+
|
|
224
|
+
|
|
225
|
+
|
|
226
|
+
|
|
227
|
+
|
|
228
|
+
* @example
|
|
229
|
+
* // Basic matrix arithmetic
|
|
230
|
+
* const a = new Matrix([
|
|
231
|
+
* [1, 2],
|
|
232
|
+
* [3, 4]
|
|
233
|
+
* ]);
|
|
234
|
+
* const b = new Matrix([
|
|
235
|
+
* [5, 6],
|
|
236
|
+
* [7, 8]
|
|
237
|
+
* ]);
|
|
238
|
+
*
|
|
239
|
+
* const sum = a.add(b);
|
|
240
|
+
* console.log(sum?.data); // [
|
|
241
|
+
* // [6, 8],
|
|
242
|
+
* // [10, 12]
|
|
243
|
+
* // ];
|
|
244
|
+
*
|
|
245
|
+
* const diff = b.subtract(a);
|
|
246
|
+
* console.log(diff?.data); // [
|
|
247
|
+
* // [4, 4],
|
|
248
|
+
* // [4, 4]
|
|
249
|
+
* // ];
|
|
89
250
|
*/
|
|
90
251
|
add(matrix: Matrix): Matrix | undefined;
|
|
91
252
|
/**
|
|
@@ -94,6 +255,23 @@ export declare class Matrix {
|
|
|
94
255
|
* @param {Matrix} matrix - The `matrix` parameter is an instance of the `Matrix` class. It
|
|
95
256
|
* represents the matrix that you want to subtract from the current matrix.
|
|
96
257
|
* @returns a new Matrix object with the result of the subtraction operation.
|
|
258
|
+
|
|
259
|
+
|
|
260
|
+
|
|
261
|
+
|
|
262
|
+
|
|
263
|
+
|
|
264
|
+
|
|
265
|
+
|
|
266
|
+
|
|
267
|
+
|
|
268
|
+
|
|
269
|
+
* @example
|
|
270
|
+
* // Element-wise subtraction
|
|
271
|
+
* const a = Matrix.from([[5, 6], [7, 8]]);
|
|
272
|
+
* const b = Matrix.from([[1, 2], [3, 4]]);
|
|
273
|
+
* const result = a.subtract(b);
|
|
274
|
+
* console.log(result?.toArray()); // [[4, 4], [4, 4]];
|
|
97
275
|
*/
|
|
98
276
|
subtract(matrix: Matrix): Matrix | undefined;
|
|
99
277
|
/**
|
|
@@ -101,23 +279,125 @@ export declare class Matrix {
|
|
|
101
279
|
* as a new matrix.
|
|
102
280
|
* @param {Matrix} matrix - The `matrix` parameter is an instance of the `Matrix` class.
|
|
103
281
|
* @returns a new Matrix object.
|
|
282
|
+
|
|
283
|
+
|
|
284
|
+
|
|
285
|
+
|
|
286
|
+
|
|
287
|
+
|
|
288
|
+
|
|
289
|
+
|
|
290
|
+
|
|
291
|
+
|
|
292
|
+
|
|
293
|
+
* @example
|
|
294
|
+
* // Matrix multiplication for transformations
|
|
295
|
+
* // 2x3 matrix * 3x2 matrix = 2x2 matrix
|
|
296
|
+
* const a = new Matrix([
|
|
297
|
+
* [1, 2, 3],
|
|
298
|
+
* [4, 5, 6]
|
|
299
|
+
* ]);
|
|
300
|
+
* const b = new Matrix([
|
|
301
|
+
* [7, 8],
|
|
302
|
+
* [9, 10],
|
|
303
|
+
* [11, 12]
|
|
304
|
+
* ]);
|
|
305
|
+
*
|
|
306
|
+
* const product = a.multiply(b);
|
|
307
|
+
* console.log(product?.rows); // 2;
|
|
308
|
+
* console.log(product?.cols); // 2;
|
|
309
|
+
* // Row 0: 1*7+2*9+3*11=58, 1*8+2*10+3*12=64
|
|
310
|
+
* // Row 1: 4*7+5*9+6*11=139, 4*8+5*10+6*12=154
|
|
311
|
+
* console.log(product?.data); // [
|
|
312
|
+
* // [58, 64],
|
|
313
|
+
* // [139, 154]
|
|
314
|
+
* // ];
|
|
104
315
|
*/
|
|
105
316
|
multiply(matrix: Matrix): Matrix | undefined;
|
|
106
317
|
/**
|
|
107
318
|
* The transpose function takes a matrix and returns a new matrix that is the transpose of the
|
|
108
319
|
* original matrix.
|
|
109
320
|
* @returns The transpose() function returns a new Matrix object with the transposed data.
|
|
321
|
+
|
|
322
|
+
|
|
323
|
+
|
|
324
|
+
|
|
325
|
+
|
|
326
|
+
|
|
327
|
+
|
|
328
|
+
|
|
329
|
+
|
|
330
|
+
|
|
331
|
+
|
|
332
|
+
* @example
|
|
333
|
+
* // Matrix transpose (square matrix)
|
|
334
|
+
* const m = new Matrix([
|
|
335
|
+
* [1, 2, 3],
|
|
336
|
+
* [4, 5, 6],
|
|
337
|
+
* [7, 8, 9]
|
|
338
|
+
* ]);
|
|
339
|
+
*
|
|
340
|
+
* const transposed = m.transpose();
|
|
341
|
+
* console.log(transposed.rows); // 3;
|
|
342
|
+
* console.log(transposed.cols); // 3;
|
|
343
|
+
* console.log(transposed.data); // [
|
|
344
|
+
* // [1, 4, 7],
|
|
345
|
+
* // [2, 5, 8],
|
|
346
|
+
* // [3, 6, 9]
|
|
347
|
+
* // ];
|
|
348
|
+
*
|
|
349
|
+
* // Transpose of transpose = original
|
|
350
|
+
* console.log(transposed.transpose().data); // m.data;
|
|
110
351
|
*/
|
|
111
352
|
transpose(): Matrix;
|
|
112
353
|
/**
|
|
113
354
|
* The `inverse` function calculates the inverse of a square matrix using Gaussian elimination.
|
|
114
355
|
* @returns a Matrix object, which represents the inverse of the original matrix.
|
|
356
|
+
|
|
357
|
+
|
|
358
|
+
|
|
359
|
+
|
|
360
|
+
|
|
361
|
+
|
|
362
|
+
|
|
363
|
+
|
|
364
|
+
|
|
365
|
+
|
|
366
|
+
|
|
367
|
+
* @example
|
|
368
|
+
* // Compute the inverse of a 2x2 matrix
|
|
369
|
+
* const m = Matrix.from([[4, 7], [2, 6]]);
|
|
370
|
+
* const inv = m.inverse();
|
|
371
|
+
* console.log(inv); // defined;
|
|
372
|
+
* // A * A^-1 should ≈ Identity
|
|
373
|
+
* const product = m.multiply(inv!);
|
|
374
|
+
* console.log(product?.get(0, 0)); // toBeCloseTo;
|
|
375
|
+
* console.log(product?.get(0, 1)); // toBeCloseTo;
|
|
376
|
+
* console.log(product?.get(1, 0)); // toBeCloseTo;
|
|
377
|
+
* console.log(product?.get(1, 1)); // toBeCloseTo;
|
|
115
378
|
*/
|
|
116
379
|
inverse(): Matrix | undefined;
|
|
117
380
|
/**
|
|
118
381
|
* The dot function calculates the dot product of two matrices and returns a new matrix.
|
|
119
382
|
* @param {Matrix} matrix - The `matrix` parameter is an instance of the `Matrix` class.
|
|
120
383
|
* @returns a new Matrix object.
|
|
384
|
+
|
|
385
|
+
|
|
386
|
+
|
|
387
|
+
|
|
388
|
+
|
|
389
|
+
|
|
390
|
+
|
|
391
|
+
|
|
392
|
+
|
|
393
|
+
|
|
394
|
+
|
|
395
|
+
* @example
|
|
396
|
+
* // Dot product of two matrices
|
|
397
|
+
* const a = Matrix.from([[1, 2], [3, 4]]);
|
|
398
|
+
* const b = Matrix.from([[5, 6], [7, 8]]);
|
|
399
|
+
* const result = a.dot(b);
|
|
400
|
+
* console.log(result?.toArray()); // [[19, 22], [43, 50]];
|
|
121
401
|
*/
|
|
122
402
|
dot(matrix: Matrix): Matrix | undefined;
|
|
123
403
|
/**
|
|
@@ -136,6 +416,57 @@ export declare class Matrix {
|
|
|
136
416
|
* and properties as the current instance.
|
|
137
417
|
*/
|
|
138
418
|
clone(): Matrix;
|
|
419
|
+
/**
|
|
420
|
+
* Returns [rows, cols] dimensions tuple.
|
|
421
|
+
*/
|
|
422
|
+
get size(): [number, number];
|
|
423
|
+
isEmpty(): boolean;
|
|
424
|
+
/**
|
|
425
|
+
* Returns a deep copy of the data as a plain 2D array.
|
|
426
|
+
*/
|
|
427
|
+
toArray(): number[][];
|
|
428
|
+
/**
|
|
429
|
+
* Returns a flat row-major array.
|
|
430
|
+
*/
|
|
431
|
+
flatten(): number[];
|
|
432
|
+
/**
|
|
433
|
+
* Iterates over rows.
|
|
434
|
+
*/
|
|
435
|
+
[Symbol.iterator](): IterableIterator<number[]>;
|
|
436
|
+
/**
|
|
437
|
+
* Visits each element with its row and column index.
|
|
438
|
+
*/
|
|
439
|
+
forEach(callback: (value: number, row: number, col: number) => void): void;
|
|
440
|
+
/**
|
|
441
|
+
* Maps each element (number → number) and returns a new Matrix.
|
|
442
|
+
*/
|
|
443
|
+
map(callback: (value: number, row: number, col: number) => number): Matrix;
|
|
444
|
+
print(): void;
|
|
445
|
+
/**
|
|
446
|
+
* Creates a rows×cols zero matrix.
|
|
447
|
+
* @example
|
|
448
|
+
* ```ts
|
|
449
|
+
* const z = Matrix.zeros(2, 3); // [[0,0,0],[0,0,0]]
|
|
450
|
+
* ```
|
|
451
|
+
*/
|
|
452
|
+
static zeros(rows: number, cols: number): Matrix;
|
|
453
|
+
/**
|
|
454
|
+
* Creates an n×n identity matrix.
|
|
455
|
+
* @example
|
|
456
|
+
* ```ts
|
|
457
|
+
* const I = Matrix.identity(3); // [[1,0,0],[0,1,0],[0,0,1]]
|
|
458
|
+
* ```
|
|
459
|
+
*/
|
|
460
|
+
static identity(n: number): Matrix;
|
|
461
|
+
/**
|
|
462
|
+
* Creates a Matrix from a plain 2D array (deep copy).
|
|
463
|
+
* @example
|
|
464
|
+
* ```ts
|
|
465
|
+
* const m = Matrix.from([[1, 2], [3, 4]]);
|
|
466
|
+
* m.get(0, 1); // 2
|
|
467
|
+
* ```
|
|
468
|
+
*/
|
|
469
|
+
static from(data: number[][]): Matrix;
|
|
139
470
|
protected _addFn(a: number | undefined, b: number): number | undefined;
|
|
140
471
|
protected _subtractFn(a: number, b: number): number;
|
|
141
472
|
protected _multiplyFn(a: number, b: number): number;
|
|
@@ -14,6 +14,63 @@ import { PriorityQueue } from './priority-queue';
|
|
|
14
14
|
* @template E Element type stored in the queue.
|
|
15
15
|
* @template R Extra record/metadata associated with each element.
|
|
16
16
|
* @example
|
|
17
|
+
* // Job scheduling by priority
|
|
18
|
+
* const jobs = new MaxPriorityQueue<number>();
|
|
19
|
+
*
|
|
20
|
+
* jobs.add(3); // low priority
|
|
21
|
+
* jobs.add(7); // high priority
|
|
22
|
+
* jobs.add(5); // medium priority
|
|
23
|
+
* jobs.add(10); // critical
|
|
24
|
+
*
|
|
25
|
+
* // Highest priority job first
|
|
26
|
+
* console.log(jobs.poll()); // 10;
|
|
27
|
+
* console.log(jobs.poll()); // 7;
|
|
28
|
+
* console.log(jobs.poll()); // 5;
|
|
29
|
+
* console.log(jobs.poll()); // 3;
|
|
30
|
+
* @example
|
|
31
|
+
* // Auction system with highest bid tracking
|
|
32
|
+
* interface Bid {
|
|
33
|
+
* bidder: string;
|
|
34
|
+
* amount: number;
|
|
35
|
+
* }
|
|
36
|
+
*
|
|
37
|
+
* const auction = new MaxPriorityQueue<Bid>([], {
|
|
38
|
+
* comparator: (a, b) => b.amount - a.amount
|
|
39
|
+
* });
|
|
40
|
+
*
|
|
41
|
+
* auction.add({ bidder: 'Alice', amount: 100 });
|
|
42
|
+
* auction.add({ bidder: 'Bob', amount: 250 });
|
|
43
|
+
* auction.add({ bidder: 'Charlie', amount: 175 });
|
|
44
|
+
*
|
|
45
|
+
* // Current highest bid
|
|
46
|
+
* console.log(auction.peek()?.bidder); // 'Bob';
|
|
47
|
+
* console.log(auction.peek()?.amount); // 250;
|
|
48
|
+
*
|
|
49
|
+
* // Process winning bid
|
|
50
|
+
* const winner = auction.poll()!;
|
|
51
|
+
* console.log(winner.bidder); // 'Bob';
|
|
52
|
+
* console.log(auction.peek()?.bidder); // 'Charlie';
|
|
53
|
+
* @example
|
|
54
|
+
* // CPU process scheduling
|
|
55
|
+
* const cpuQueue = new MaxPriorityQueue<[number, string]>([], {
|
|
56
|
+
* comparator: (a, b) => b[0] - a[0]
|
|
57
|
+
* });
|
|
58
|
+
*
|
|
59
|
+
* cpuQueue.add([5, 'System process']);
|
|
60
|
+
* cpuQueue.add([1, 'Background task']);
|
|
61
|
+
* cpuQueue.add([8, 'User interaction']);
|
|
62
|
+
* cpuQueue.add([3, 'Network sync']);
|
|
63
|
+
*
|
|
64
|
+
* const order = [];
|
|
65
|
+
* while (cpuQueue.size > 0) {
|
|
66
|
+
* order.push(cpuQueue.poll()![1]);
|
|
67
|
+
* }
|
|
68
|
+
* console.log(order); // [
|
|
69
|
+
* // 'User interaction',
|
|
70
|
+
* // 'System process',
|
|
71
|
+
* // 'Network sync',
|
|
72
|
+
* // 'Background task'
|
|
73
|
+
* // ];
|
|
17
74
|
*/
|
|
18
75
|
export declare class MaxPriorityQueue<E = any, R = any> extends PriorityQueue<E, R> {
|
|
19
76
|
/**
|
|
@@ -14,6 +14,66 @@ import { PriorityQueue } from './priority-queue';
|
|
|
14
14
|
* @template E Element type stored in the queue.
|
|
15
15
|
* @template R Extra record/metadata associated with each element.
|
|
16
16
|
* @example
|
|
17
|
+
* // Shortest job first scheduling
|
|
18
|
+
* const jobs = new MinPriorityQueue<number>();
|
|
19
|
+
*
|
|
20
|
+
* jobs.add(8); // 8 seconds
|
|
21
|
+
* jobs.add(2); // 2 seconds
|
|
22
|
+
* jobs.add(5); // 5 seconds
|
|
23
|
+
* jobs.add(1); // 1 second
|
|
24
|
+
*
|
|
25
|
+
* // Shortest job first
|
|
26
|
+
* console.log(jobs.poll()); // 1;
|
|
27
|
+
* console.log(jobs.poll()); // 2;
|
|
28
|
+
* console.log(jobs.poll()); // 5;
|
|
29
|
+
* console.log(jobs.poll()); // 8;
|
|
30
|
+
* @example
|
|
31
|
+
* // Event-driven simulation with timestamps
|
|
32
|
+
* interface Event {
|
|
33
|
+
* time: number;
|
|
34
|
+
* action: string;
|
|
35
|
+
* }
|
|
36
|
+
*
|
|
37
|
+
* const timeline = new MinPriorityQueue<Event>([], {
|
|
38
|
+
* comparator: (a, b) => a.time - b.time
|
|
39
|
+
* });
|
|
40
|
+
*
|
|
41
|
+
* timeline.add({ time: 300, action: 'Timeout' });
|
|
42
|
+
* timeline.add({ time: 100, action: 'Request received' });
|
|
43
|
+
* timeline.add({ time: 200, action: 'Processing done' });
|
|
44
|
+
* timeline.add({ time: 150, action: 'Cache hit' });
|
|
45
|
+
*
|
|
46
|
+
* const order = [];
|
|
47
|
+
* while (timeline.size > 0) {
|
|
48
|
+
* order.push(timeline.poll()!.action);
|
|
49
|
+
* }
|
|
50
|
+
* console.log(order); // [
|
|
51
|
+
* // 'Request received',
|
|
52
|
+
* // 'Cache hit',
|
|
53
|
+
* // 'Processing done',
|
|
54
|
+
* // 'Timeout'
|
|
55
|
+
* // ];
|
|
56
|
+
* @example
|
|
57
|
+
* // Huffman coding frequency selection
|
|
58
|
+
* // Character frequencies for Huffman tree building
|
|
59
|
+
* const freq = new MinPriorityQueue<[number, string]>([], {
|
|
60
|
+
* comparator: (a, b) => a[0] - b[0]
|
|
61
|
+
* });
|
|
62
|
+
*
|
|
63
|
+
* freq.add([5, 'a']);
|
|
64
|
+
* freq.add([9, 'b']);
|
|
65
|
+
* freq.add([12, 'c']);
|
|
66
|
+
* freq.add([2, 'd']);
|
|
67
|
+
*
|
|
68
|
+
* // Always pick two lowest frequencies
|
|
69
|
+
* const first = freq.poll()!;
|
|
70
|
+
* const second = freq.poll()!;
|
|
71
|
+
* console.log(first[1]); // 'd'; // freq 2
|
|
72
|
+
* console.log(second[1]); // 'a'; // freq 5
|
|
73
|
+
*
|
|
74
|
+
* // Combined node goes back
|
|
75
|
+
* freq.add([first[0] + second[0], first[1] + second[1]]);
|
|
76
|
+
* console.log(freq.peek()![0]); // 7;
|
|
17
77
|
*/
|
|
18
78
|
export declare class MinPriorityQueue<E = any, R = any> extends PriorityQueue<E, R> {
|
|
19
79
|
/**
|
|
@@ -9,6 +9,66 @@ import type { PriorityQueueOptions } from '../../types';
|
|
|
9
9
|
import { Heap } from '../heap';
|
|
10
10
|
/**
|
|
11
11
|
* @example
|
|
12
|
+
* // Hospital emergency room triage
|
|
13
|
+
* interface Patient {
|
|
14
|
+
* name: string;
|
|
15
|
+
* severity: number; // 1 = critical, 5 = minor
|
|
16
|
+
* }
|
|
17
|
+
*
|
|
18
|
+
* const er = new PriorityQueue<Patient>([], {
|
|
19
|
+
* comparator: (a, b) => a.severity - b.severity
|
|
20
|
+
* });
|
|
21
|
+
*
|
|
22
|
+
* er.add({ name: 'Flu symptoms', severity: 4 });
|
|
23
|
+
* er.add({ name: 'Heart attack', severity: 1 });
|
|
24
|
+
* er.add({ name: 'Broken arm', severity: 3 });
|
|
25
|
+
* er.add({ name: 'Stroke', severity: 1 });
|
|
26
|
+
*
|
|
27
|
+
* // Most critical patients first
|
|
28
|
+
* console.log(er.poll()?.severity); // 1;
|
|
29
|
+
* console.log(er.poll()?.severity); // 1;
|
|
30
|
+
* console.log(er.poll()?.severity); // 3;
|
|
31
|
+
* console.log(er.poll()?.severity); // 4;
|
|
32
|
+
* @example
|
|
33
|
+
* // Task scheduler with deadlines
|
|
34
|
+
* interface Task {
|
|
35
|
+
* name: string;
|
|
36
|
+
* deadline: number; // hours until due
|
|
37
|
+
* }
|
|
38
|
+
*
|
|
39
|
+
* const scheduler = new PriorityQueue<Task>([], {
|
|
40
|
+
* comparator: (a, b) => a.deadline - b.deadline
|
|
41
|
+
* });
|
|
42
|
+
*
|
|
43
|
+
* scheduler.add({ name: 'Report', deadline: 24 });
|
|
44
|
+
* scheduler.add({ name: 'Email', deadline: 2 });
|
|
45
|
+
* scheduler.add({ name: 'Meeting prep', deadline: 4 });
|
|
46
|
+
* scheduler.add({ name: 'Code review', deadline: 8 });
|
|
47
|
+
*
|
|
48
|
+
* // Process most urgent first
|
|
49
|
+
* console.log(scheduler.peek()?.name); // 'Email';
|
|
50
|
+
* console.log(scheduler.size); // 4;
|
|
51
|
+
*
|
|
52
|
+
* const order = [];
|
|
53
|
+
* while (scheduler.size > 0) {
|
|
54
|
+
* order.push(scheduler.poll()!.name);
|
|
55
|
+
* }
|
|
56
|
+
* console.log(order); // ['Email', 'Meeting prep', 'Code review', 'Report'];
|
|
57
|
+
* @example
|
|
58
|
+
* // Bandwidth allocation with priorities
|
|
59
|
+
* const bandwidth = new PriorityQueue<[number, string]>([], {
|
|
60
|
+
* comparator: (a, b) => a[0] - b[0]
|
|
61
|
+
* });
|
|
62
|
+
*
|
|
63
|
+
* bandwidth.add([1, 'Video call']); // highest priority
|
|
64
|
+
* bandwidth.add([3, 'File download']);
|
|
65
|
+
* bandwidth.add([2, 'Web browsing']);
|
|
66
|
+
* bandwidth.add([1, 'Voice call']);
|
|
67
|
+
*
|
|
68
|
+
* // Allocate bandwidth to highest priority first
|
|
69
|
+
* console.log(bandwidth.poll()?.[1]); // 'Video call';
|
|
70
|
+
* console.log(bandwidth.poll()?.[1]); // 'Voice call';
|
|
71
|
+
* console.log(bandwidth.size); // 2;
|
|
12
72
|
*/
|
|
13
73
|
export declare class PriorityQueue<E = any, R = any> extends Heap<E, R> {
|
|
14
74
|
constructor(elements?: Iterable<E> | Iterable<R>, options?: PriorityQueueOptions<E, R>);
|