priority-queue-typed 1.19.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.
@@ -0,0 +1,365 @@
1
+ "use strict";
2
+ var __assign = (this && this.__assign) || function () {
3
+ __assign = Object.assign || function(t) {
4
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
5
+ s = arguments[i];
6
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
13
+ var __read = (this && this.__read) || function (o, n) {
14
+ var m = typeof Symbol === "function" && o[Symbol.iterator];
15
+ if (!m) return o;
16
+ var i = m.call(o), r, ar = [], e;
17
+ try {
18
+ while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
19
+ }
20
+ catch (error) { e = { error: error }; }
21
+ finally {
22
+ try {
23
+ if (r && !r.done && (m = i["return"])) m.call(i);
24
+ }
25
+ finally { if (e) throw e.error; }
26
+ }
27
+ return ar;
28
+ };
29
+ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
30
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
31
+ if (ar || !(i in from)) {
32
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
33
+ ar[i] = from[i];
34
+ }
35
+ }
36
+ return to.concat(ar || Array.prototype.slice.call(from));
37
+ };
38
+ Object.defineProperty(exports, "__esModule", { value: true });
39
+ exports.PriorityQueue = void 0;
40
+ var PriorityQueue = /** @class */ (function () {
41
+ /**
42
+ * The constructor initializes a priority queue with the given options, including an array of nodes and a comparator
43
+ * function.
44
+ * @param options - The `options` parameter is an object that contains the following properties:
45
+ */
46
+ function PriorityQueue(options) {
47
+ this._nodes = [];
48
+ this._comparator = function (a, b) {
49
+ var aKey = a, bKey = b;
50
+ return aKey - bKey;
51
+ };
52
+ var nodes = options.nodes, comparator = options.comparator, _a = options.isFix, isFix = _a === void 0 ? true : _a;
53
+ this._comparator = comparator;
54
+ if (nodes && Array.isArray(nodes) && nodes.length > 0) {
55
+ // TODO support distinct
56
+ this._nodes = __spreadArray([], __read(nodes), false);
57
+ isFix && this._fix();
58
+ }
59
+ }
60
+ Object.defineProperty(PriorityQueue.prototype, "nodes", {
61
+ get: function () {
62
+ return this._nodes;
63
+ },
64
+ enumerable: false,
65
+ configurable: true
66
+ });
67
+ Object.defineProperty(PriorityQueue.prototype, "size", {
68
+ get: function () {
69
+ return this.nodes.length;
70
+ },
71
+ enumerable: false,
72
+ configurable: true
73
+ });
74
+ /**
75
+ * The `heapify` function creates a new PriorityQueue instance and fixes the heap property.
76
+ * @param options - The "options" parameter is an object that contains the configuration options for the PriorityQueue.
77
+ * It can include properties such as "comparator" which specifies the comparison function used to order the elements in
78
+ * the priority queue, and "initialValues" which is an array of initial values to be added to the priority
79
+ * @returns a new instance of the PriorityQueue class after performing the heapify operation on it.
80
+ */
81
+ PriorityQueue.heapify = function (options) {
82
+ var heap = new PriorityQueue(options);
83
+ heap._fix();
84
+ return heap;
85
+ };
86
+ /**
87
+ * The function checks if a priority queue is valid by creating a new priority queue with a fix option and then calling
88
+ * the isValid method.
89
+ * @param options - An object containing options for creating a priority queue. The options object should have the
90
+ * following properties:
91
+ * @returns the result of calling the `isValid()` method on a new instance of the `PriorityQueue` class.
92
+ */
93
+ PriorityQueue.isPriorityQueueified = function (options) {
94
+ return new PriorityQueue(__assign(__assign({}, options), { isFix: false })).isValid();
95
+ };
96
+ /**
97
+ * Starting from TypeScript version 5.0 and onwards, the use of distinct access modifiers for Getters and Setters is not permitted. As an alternative, to ensure compatibility, it is necessary to adopt a Java-style approach for Setters (using the same name as the property) while utilizing separate method names for Getters.
98
+ */
99
+ PriorityQueue.prototype.getNodes = function () {
100
+ return this._nodes;
101
+ };
102
+ /**
103
+ * The "add" function adds a node to the heap and ensures that the heap property is maintained.
104
+ * @param {T} node - The parameter "node" is of type T, which means it can be any data type. It represents the node
105
+ * that needs to be added to the heap.
106
+ */
107
+ PriorityQueue.prototype.add = function (node) {
108
+ this.nodes.push(node);
109
+ this._heapifyUp(this.size - 1);
110
+ };
111
+ /**
112
+ * The "has" function checks if a given node is present in the list of nodes.
113
+ * @param {T} node - The parameter `node` is of type `T`, which means it can be any type. It represents the node that
114
+ * we want to check if it exists in the `nodes` array.
115
+ * @returns a boolean value indicating whether the given node is included in the array of nodes.
116
+ */
117
+ PriorityQueue.prototype.has = function (node) {
118
+ return this.nodes.includes(node);
119
+ };
120
+ /**
121
+ * The `peek` function returns the first element of the `nodes` array if it exists, otherwise it returns `null`.
122
+ * @returns The `peek()` function is returning the first element (`T`) of the `nodes` array if the `size` is not zero.
123
+ * Otherwise, it returns `null`.
124
+ */
125
+ PriorityQueue.prototype.peek = function () {
126
+ return this.size ? this.nodes[0] : null;
127
+ };
128
+ /**
129
+ * The `poll` function removes and returns the top element from a heap data structure.
130
+ * @returns The `poll()` method returns a value of type `T` or `null`.
131
+ */
132
+ PriorityQueue.prototype.poll = function () {
133
+ var _a, _b;
134
+ var res = null;
135
+ if (this.size > 1) {
136
+ this._swap(0, this.nodes.length - 1);
137
+ res = (_a = this.nodes.pop()) !== null && _a !== void 0 ? _a : null;
138
+ this._heapifyDown(0);
139
+ }
140
+ else if (this.size === 1) {
141
+ res = (_b = this.nodes.pop()) !== null && _b !== void 0 ? _b : null;
142
+ }
143
+ return res;
144
+ };
145
+ /**
146
+ * The `leaf` function returns the last element in the `nodes` array or `null` if the array is empty.
147
+ * @returns The method `leaf()` is returning the last element (`T`) in the `nodes` array if it exists. If the array is
148
+ * empty or the last element is `null`, then it returns `null`.
149
+ */
150
+ PriorityQueue.prototype.leaf = function () {
151
+ var _a;
152
+ return (_a = this.nodes[this.size - 1]) !== null && _a !== void 0 ? _a : null;
153
+ };
154
+ /**
155
+ * The function checks if the size of an object is equal to zero and returns a boolean value indicating whether the
156
+ * object is empty or not.
157
+ * @returns The method `isEmpty()` is returning a boolean value indicating whether the size of the object is equal to
158
+ * 0.
159
+ */
160
+ PriorityQueue.prototype.isEmpty = function () {
161
+ return this.size === 0;
162
+ };
163
+ /**
164
+ * The clear function clears the nodes array.
165
+ */
166
+ PriorityQueue.prototype.clear = function () {
167
+ this._setNodes([]);
168
+ };
169
+ /**
170
+ * The toArray function returns an array containing all the elements in the nodes property.
171
+ * @returns An array of type T, which is the elements of the nodes property.
172
+ */
173
+ PriorityQueue.prototype.toArray = function () {
174
+ return __spreadArray([], __read(this.nodes), false);
175
+ };
176
+ /**
177
+ * The `clone` function returns a new instance of the `PriorityQueue` class with the same nodes and comparator as the
178
+ * original instance.
179
+ * @returns The `clone()` method is returning a new instance of the `PriorityQueue` class with the same `nodes` and
180
+ * `comparator` properties as the original instance.
181
+ */
182
+ PriorityQueue.prototype.clone = function () {
183
+ return new PriorityQueue({ nodes: this.nodes, comparator: this._comparator });
184
+ };
185
+ // --- start additional methods ---
186
+ /**
187
+ * The `isValid` function recursively checks if a binary tree satisfies a certain condition.
188
+ * @returns The function `isValid()` returns a boolean value.
189
+ */
190
+ PriorityQueue.prototype.isValid = function () {
191
+ for (var i = 0; i < this.nodes.length; i++) {
192
+ var leftChildIndex = this._getLeft(i);
193
+ var rightChildIndex = this._getRight(i);
194
+ if (this._isValidIndex(leftChildIndex) && !this._compare(leftChildIndex, i)) {
195
+ return false;
196
+ }
197
+ if (this._isValidIndex(rightChildIndex) && !this._compare(rightChildIndex, i)) {
198
+ return false;
199
+ }
200
+ }
201
+ return true;
202
+ };
203
+ /**
204
+ * Plan to support sorting of duplicate elements.
205
+ */
206
+ /**
207
+ * The function sorts the elements in a data structure and returns them in an array.
208
+ * Plan to support sorting of duplicate elements.
209
+ * @returns The `sort()` method is returning an array of type `T[]`.
210
+ */
211
+ PriorityQueue.prototype.sort = function () {
212
+ // TODO Plan to support sorting of duplicate elements.
213
+ var visitedNode = [];
214
+ while (this.size !== 0) {
215
+ var top = this.poll();
216
+ if (top)
217
+ visitedNode.push(top);
218
+ }
219
+ return visitedNode;
220
+ };
221
+ /**
222
+ * The DFS function performs a depth-first search traversal on a binary tree and returns an array of visited nodes
223
+ * based on the specified traversal order.
224
+ * @param {PriorityQueueDFSOrderPattern} dfsMode - The dfsMode parameter is a string that specifies the order in which
225
+ * the nodes should be visited during the Depth-First Search (DFS) traversal. It can have one of the following values:
226
+ * @returns an array of type `(T | null)[]`.
227
+ */
228
+ PriorityQueue.prototype.DFS = function (dfsMode) {
229
+ var _this = this;
230
+ var visitedNode = [];
231
+ var traverse = function (cur) {
232
+ var _a, _b, _c;
233
+ var leftChildIndex = _this._getLeft(cur);
234
+ var rightChildIndex = _this._getRight(cur);
235
+ switch (dfsMode) {
236
+ case 'in':
237
+ _this._isValidIndex(leftChildIndex) && traverse(leftChildIndex);
238
+ visitedNode.push((_a = _this.nodes[cur]) !== null && _a !== void 0 ? _a : null);
239
+ _this._isValidIndex(rightChildIndex) && traverse(rightChildIndex);
240
+ break;
241
+ case 'pre':
242
+ visitedNode.push((_b = _this.nodes[cur]) !== null && _b !== void 0 ? _b : null);
243
+ _this._isValidIndex(leftChildIndex) && traverse(leftChildIndex);
244
+ _this._isValidIndex(rightChildIndex) && traverse(rightChildIndex);
245
+ break;
246
+ case 'post':
247
+ _this._isValidIndex(leftChildIndex) && traverse(leftChildIndex);
248
+ _this._isValidIndex(rightChildIndex) && traverse(rightChildIndex);
249
+ visitedNode.push((_c = _this.nodes[cur]) !== null && _c !== void 0 ? _c : null);
250
+ break;
251
+ }
252
+ };
253
+ this._isValidIndex(0) && traverse(0);
254
+ return visitedNode;
255
+ };
256
+ PriorityQueue.prototype._setNodes = function (value) {
257
+ this._nodes = value;
258
+ };
259
+ /**
260
+ * The function compares two numbers using a custom comparator function.
261
+ * @param {number} a - The parameter "a" is a number that represents the index of a node in an array.
262
+ * @param {number} b - The parameter "b" is a number.
263
+ * @returns the result of the comparison between the elements at indices `a` and `b` in the `nodes` array. The
264
+ * comparison is done using the `_comparator` function, and if the result is greater than 0, `true` is returned,
265
+ * indicating that the element at index `a` is greater than the element at index `b`.
266
+ */
267
+ PriorityQueue.prototype._compare = function (a, b) {
268
+ return this._comparator(this.nodes[a], this.nodes[b]) > 0;
269
+ };
270
+ /**
271
+ * The function swaps two elements in an array.
272
+ * @param {number} a - The parameter "a" is a number that represents the index of an element in an array.
273
+ * @param {number} b - The parameter "b" is a number.
274
+ */
275
+ PriorityQueue.prototype._swap = function (a, b) {
276
+ var temp = this.nodes[a];
277
+ this.nodes[a] = this.nodes[b];
278
+ this.nodes[b] = temp;
279
+ };
280
+ /**
281
+ * The function checks if a given index is valid within an array.
282
+ * @param {number} index - The parameter "index" is of type number and represents the index value that needs to be
283
+ * checked for validity.
284
+ * @returns A boolean value indicating whether the given index is valid or not.
285
+ */
286
+ PriorityQueue.prototype._isValidIndex = function (index) {
287
+ return index > -1 && index < this.nodes.length;
288
+ };
289
+ /**
290
+ * The function returns the index of the parent node given the index of a child node in a binary tree.
291
+ * @param {number} child - The "child" parameter is a number representing the index of a child node in a binary tree.
292
+ * @returns the parent of the given child node.
293
+ */
294
+ PriorityQueue.prototype._getParent = function (child) {
295
+ return Math.floor((child - 1) / 2);
296
+ };
297
+ /**
298
+ * The function returns the index of the left child node in a binary tree given the index of its parent node.
299
+ * @param {number} parent - The parameter "parent" is a number that represents the index of a node in a binary tree.
300
+ * @returns the left child of a given parent node in a binary tree.
301
+ */
302
+ PriorityQueue.prototype._getLeft = function (parent) {
303
+ return (2 * parent) + 1;
304
+ };
305
+ /**
306
+ * The function returns the index of the right child node in a binary tree given the index of its parent node.
307
+ * @param {number} parent - The parameter "parent" is a number that represents the index of a node in a binary tree.
308
+ * @returns the right child of a given parent node in a binary tree.
309
+ */
310
+ PriorityQueue.prototype._getRight = function (parent) {
311
+ return (2 * parent) + 2;
312
+ };
313
+ /**
314
+ * The function returns the index of the smallest child node of a given parent node.
315
+ * @param {number} parent - The parent parameter is a number that represents the index of the parent node in a binary
316
+ * tree.
317
+ * @returns the minimum value between the parent node and its left and right child nodes.
318
+ */
319
+ PriorityQueue.prototype._getComparedChild = function (parent) {
320
+ var min = parent;
321
+ var left = this._getLeft(parent), right = this._getRight(parent);
322
+ if (left < this.size && this._compare(min, left)) {
323
+ min = left;
324
+ }
325
+ if (right < this.size && this._compare(min, right)) {
326
+ min = right;
327
+ }
328
+ return min;
329
+ };
330
+ /**
331
+ * The function `_heapifyUp` is used to maintain the heap property by moving an element up the heap until it is in the
332
+ * correct position.
333
+ * @param {number} start - The start parameter is the index of the element that needs to be moved up in the heap.
334
+ */
335
+ PriorityQueue.prototype._heapifyUp = function (start) {
336
+ while (start > 0 && this._compare(this._getParent(start), start)) {
337
+ var parent = this._getParent(start);
338
+ this._swap(start, parent);
339
+ start = parent;
340
+ }
341
+ };
342
+ /**
343
+ * The function performs a heapify operation by comparing and swapping elements in a binary heap.
344
+ * @param {number} start - The start parameter is the index of the element in the heap from where the heapifyDown
345
+ * operation should start.
346
+ */
347
+ PriorityQueue.prototype._heapifyDown = function (start) {
348
+ var min = this._getComparedChild(start);
349
+ while (this._compare(start, min)) {
350
+ this._swap(min, start);
351
+ start = min;
352
+ min = this._getComparedChild(start);
353
+ }
354
+ };
355
+ /**
356
+ * The _fix function performs a heapify operation on the elements of the heap starting from the middle and moving
357
+ * towards the root.
358
+ */
359
+ PriorityQueue.prototype._fix = function () {
360
+ for (var i = Math.floor(this.size / 2); i > -1; i--)
361
+ this._heapifyDown(i);
362
+ };
363
+ return PriorityQueue;
364
+ }());
365
+ exports.PriorityQueue = PriorityQueue;
package/jest.config.js ADDED
@@ -0,0 +1,5 @@
1
+ module.exports = {
2
+ preset: 'ts-jest',
3
+ testEnvironment: 'node',
4
+ testMatch: ['<rootDir>/tests/**/*.test.ts'],
5
+ };
package/package.json ADDED
@@ -0,0 +1,61 @@
1
+ {
2
+ "name": "priority-queue-typed",
3
+ "version": "1.19.3",
4
+ "description": "Priority Queue, Min Priority Queue, Max Priority Queue. Javascript & Typescript Data Structure.",
5
+ "main": "dist/index.js",
6
+ "scripts": {
7
+ "build": "rm -rf dist && npx tsc",
8
+ "test": "jest",
9
+ "build:docs": "typedoc --out docs ./src",
10
+ "deps:check": "dependency-cruiser src",
11
+ "build:publish": "npm run build && npm run build:docs && npm publish"
12
+ },
13
+ "repository": {
14
+ "type": "git",
15
+ "url": "git+https://github.com/zrwusa/data-structure-typed"
16
+ },
17
+ "keywords": [
18
+ "Priority queue",
19
+ "Heap data structure",
20
+ "Binary heap",
21
+ "Priority management",
22
+ "Insertion",
23
+ "Deletion",
24
+ "Heap property",
25
+ "Complete binary tree",
26
+ "Heapify",
27
+ "Extract min/max",
28
+ "Heap operations",
29
+ "Data structure",
30
+ "Efficient priority handling",
31
+ "Ordering property",
32
+ "Heap operations complexity",
33
+ "Binary tree",
34
+ "Data management",
35
+ "Dynamic resizing",
36
+ "Priority-based processing",
37
+ "Data sorting",
38
+ "Element priority",
39
+ "Queue with priorities"
40
+ ],
41
+ "author": "Tyler Zeng zrwusa@gmail.com",
42
+ "license": "MIT",
43
+ "bugs": {
44
+ "url": "https://github.com/zrwusa/data-structure-typed/issues"
45
+ },
46
+ "homepage": "https://github.com/zrwusa/data-structure-typed#readme",
47
+ "types": "dist/index.d.ts",
48
+ "devDependencies": {
49
+ "@types/jest": "^29.5.3",
50
+ "@types/node": "^20.4.9",
51
+ "dependency-cruiser": "^13.1.2",
52
+ "jest": "^29.6.2",
53
+ "ts-jest": "^29.1.1",
54
+ "typedoc": "^0.24.8",
55
+ "typescript": "^4.9.5"
56
+ },
57
+ "dependencies": {
58
+ "data-structure-typed": "^1.19.3",
59
+ "zod": "^3.22.2"
60
+ }
61
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,37 @@
1
+ {
2
+ "compilerOptions": {
3
+ "declaration": true,
4
+ "outDir": "./dist",
5
+ "module": "commonjs",
6
+ "target": "es5",
7
+ "lib": [
8
+ "esnext",
9
+ ],
10
+ "strict": true,
11
+ "esModuleInterop": true,
12
+ "moduleResolution": "node",
13
+ "declarationDir": "./dist",
14
+ "skipLibCheck": true,
15
+ "downlevelIteration": true,
16
+ "experimentalDecorators": true,
17
+ // "allowJs": true,
18
+ // "allowSyntheticDefaultImports": true,
19
+ // "forceConsistentCasingInFileNames": true,
20
+ // "noFallthroughCasesInSwitch": true,
21
+ // "resolveJsonModule": true,
22
+ // "isolatedModules": true,
23
+ // "noEmit": true,
24
+ "typeRoots": [
25
+ "node_modules/@types"
26
+ ]
27
+ },
28
+
29
+ "include": [
30
+ "src",
31
+ ],
32
+ "exclude": [
33
+ // "node_modules/data-structure-typed",
34
+ "node_modules",
35
+ "dist"
36
+ ]
37
+ }