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,3 @@
1
+ export * from './priority-queue';
2
+ export * from './min-priority-queue';
3
+ export * from './max-priority-queue';
package/dist/index.js ADDED
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./priority-queue"), exports);
18
+ __exportStar(require("./min-priority-queue"), exports);
19
+ __exportStar(require("./max-priority-queue"), exports);
@@ -0,0 +1,15 @@
1
+ /**
2
+ * data-structure-typed
3
+ *
4
+ * @author Tyler Zeng
5
+ * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
6
+ * @license MIT License
7
+ */
8
+ import { PriorityQueue } from 'data-structure-typed';
9
+ import type { PriorityQueueOptions } from 'data-structure-typed';
10
+ export declare class MaxPriorityQueue<T = number> extends PriorityQueue<T> {
11
+ constructor(options?: Omit<PriorityQueueOptions<number>, 'comparator'>);
12
+ constructor(options: PriorityQueueOptions<T>);
13
+ static heapify<T extends number>(options?: Omit<PriorityQueueOptions<T>, 'comparator'>): MaxPriorityQueue<T>;
14
+ static heapify<T>(options: PriorityQueueOptions<T>): MaxPriorityQueue<T>;
15
+ }
@@ -0,0 +1,67 @@
1
+ "use strict";
2
+ var __extends = (this && this.__extends) || (function () {
3
+ var extendStatics = function (d, b) {
4
+ extendStatics = Object.setPrototypeOf ||
5
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
+ function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7
+ return extendStatics(d, b);
8
+ };
9
+ return function (d, b) {
10
+ if (typeof b !== "function" && b !== null)
11
+ throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
12
+ extendStatics(d, b);
13
+ function __() { this.constructor = d; }
14
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15
+ };
16
+ })();
17
+ var __assign = (this && this.__assign) || function () {
18
+ __assign = Object.assign || function(t) {
19
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
20
+ s = arguments[i];
21
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
22
+ t[p] = s[p];
23
+ }
24
+ return t;
25
+ };
26
+ return __assign.apply(this, arguments);
27
+ };
28
+ Object.defineProperty(exports, "__esModule", { value: true });
29
+ exports.MaxPriorityQueue = void 0;
30
+ /**
31
+ * data-structure-typed
32
+ *
33
+ * @author Tyler Zeng
34
+ * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
35
+ * @license MIT License
36
+ */
37
+ var data_structure_typed_1 = require("data-structure-typed");
38
+ var MaxPriorityQueue = /** @class */ (function (_super) {
39
+ __extends(MaxPriorityQueue, _super);
40
+ /**
41
+ * The constructor initializes a priority queue with an optional comparator function.
42
+ * @param [options] - The `options` parameter is an optional object that can contain various properties to configure
43
+ * the priority queue.
44
+ */
45
+ function MaxPriorityQueue(options) {
46
+ return _super.call(this, __assign(__assign({}, options), { comparator: (options === null || options === void 0 ? void 0 : options.comparator) ? options.comparator : function (a, b) {
47
+ var aKey = a, bKey = b;
48
+ return bKey - aKey;
49
+ } })) || this;
50
+ }
51
+ /**
52
+ * The function `heapify` creates a max priority queue from the given options and returns it.
53
+ * @param options - The `options` parameter is an object that contains configuration options for creating a priority
54
+ * queue. It can have the following properties:
55
+ * @returns a MaxPriorityQueue object.
56
+ */
57
+ MaxPriorityQueue.heapify = function (options) {
58
+ var maxPQ = new MaxPriorityQueue(__assign(__assign({}, options), { comparator: (options === null || options === void 0 ? void 0 : options.comparator) ? options.comparator : function (a, b) {
59
+ var aKey = a, bKey = b;
60
+ return bKey - aKey;
61
+ } }));
62
+ maxPQ._fix();
63
+ return maxPQ;
64
+ };
65
+ return MaxPriorityQueue;
66
+ }(data_structure_typed_1.PriorityQueue));
67
+ exports.MaxPriorityQueue = MaxPriorityQueue;
@@ -0,0 +1,15 @@
1
+ /**
2
+ * data-structure-typed
3
+ *
4
+ * @author Tyler Zeng
5
+ * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
6
+ * @license MIT License
7
+ */
8
+ import { PriorityQueue } from 'data-structure-typed';
9
+ import type { PriorityQueueOptions } from 'data-structure-typed';
10
+ export declare class MinPriorityQueue<T = number> extends PriorityQueue<T> {
11
+ constructor(options?: Omit<PriorityQueueOptions<number>, 'comparator'>);
12
+ constructor(options: PriorityQueueOptions<T>);
13
+ static heapify<T extends number>(options?: Omit<PriorityQueueOptions<T>, 'comparator'>): MinPriorityQueue<T>;
14
+ static heapify<T>(options: PriorityQueueOptions<T>): MinPriorityQueue<T>;
15
+ }
@@ -0,0 +1,68 @@
1
+ "use strict";
2
+ var __extends = (this && this.__extends) || (function () {
3
+ var extendStatics = function (d, b) {
4
+ extendStatics = Object.setPrototypeOf ||
5
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
+ function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7
+ return extendStatics(d, b);
8
+ };
9
+ return function (d, b) {
10
+ if (typeof b !== "function" && b !== null)
11
+ throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
12
+ extendStatics(d, b);
13
+ function __() { this.constructor = d; }
14
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15
+ };
16
+ })();
17
+ var __assign = (this && this.__assign) || function () {
18
+ __assign = Object.assign || function(t) {
19
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
20
+ s = arguments[i];
21
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
22
+ t[p] = s[p];
23
+ }
24
+ return t;
25
+ };
26
+ return __assign.apply(this, arguments);
27
+ };
28
+ Object.defineProperty(exports, "__esModule", { value: true });
29
+ exports.MinPriorityQueue = void 0;
30
+ /**
31
+ * data-structure-typed
32
+ *
33
+ * @author Tyler Zeng
34
+ * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
35
+ * @license MIT License
36
+ */
37
+ var data_structure_typed_1 = require("data-structure-typed");
38
+ var MinPriorityQueue = /** @class */ (function (_super) {
39
+ __extends(MinPriorityQueue, _super);
40
+ /**
41
+ * The constructor initializes a priority queue with an optional comparator function.
42
+ * @param [options] - The `options` parameter is an optional object that can contain various configuration options for
43
+ * the `PriorityQueue` constructor.
44
+ */
45
+ function MinPriorityQueue(options) {
46
+ return _super.call(this, __assign(__assign({}, options), { comparator: (options === null || options === void 0 ? void 0 : options.comparator) ? options.comparator : function (a, b) {
47
+ var aKey = a, bKey = b;
48
+ return aKey - bKey;
49
+ } })) || this;
50
+ }
51
+ /**
52
+ * The function `heapify` creates a new MinPriorityQueue instance and sets the comparator function based on the options
53
+ * provided, and then fixes the heap structure of the queue.
54
+ * @param options - The `options` parameter is an object that contains configuration options for creating a priority
55
+ * queue. It can have the following properties:
56
+ * @returns a MinPriorityQueue object.
57
+ */
58
+ MinPriorityQueue.heapify = function (options) {
59
+ var minPQ = new MinPriorityQueue(__assign(__assign({}, options), { comparator: (options === null || options === void 0 ? void 0 : options.comparator) ? options.comparator : function (a, b) {
60
+ var aKey = a, bKey = b;
61
+ return aKey - bKey;
62
+ } }));
63
+ minPQ._fix();
64
+ return minPQ;
65
+ };
66
+ return MinPriorityQueue;
67
+ }(data_structure_typed_1.PriorityQueue));
68
+ exports.MinPriorityQueue = MinPriorityQueue;
@@ -0,0 +1,180 @@
1
+ /**
2
+ * data-structure-typed
3
+ *
4
+ * @author Tyler Zeng
5
+ * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
6
+ * @license MIT License
7
+ */
8
+ import type { PriorityQueueComparator, PriorityQueueDFSOrderPattern, PriorityQueueOptions } from 'data-structure-typed';
9
+ export declare class PriorityQueue<T = number> {
10
+ /**
11
+ * The constructor initializes a priority queue with the given options, including an array of nodes and a comparator
12
+ * function.
13
+ * @param options - The `options` parameter is an object that contains the following properties:
14
+ */
15
+ constructor(options: PriorityQueueOptions<T>);
16
+ protected _nodes: T[];
17
+ get nodes(): T[];
18
+ get size(): number;
19
+ /**
20
+ * The `heapify` function creates a new PriorityQueue instance and fixes the heap property.
21
+ * @param options - The "options" parameter is an object that contains the configuration options for the PriorityQueue.
22
+ * It can include properties such as "comparator" which specifies the comparison function used to order the elements in
23
+ * the priority queue, and "initialValues" which is an array of initial values to be added to the priority
24
+ * @returns a new instance of the PriorityQueue class after performing the heapify operation on it.
25
+ */
26
+ static heapify<T>(options: PriorityQueueOptions<T>): PriorityQueue<T>;
27
+ /**
28
+ * The function checks if a priority queue is valid by creating a new priority queue with a fix option and then calling
29
+ * the isValid method.
30
+ * @param options - An object containing options for creating a priority queue. The options object should have the
31
+ * following properties:
32
+ * @returns the result of calling the `isValid()` method on a new instance of the `PriorityQueue` class.
33
+ */
34
+ static isPriorityQueueified<T>(options: Omit<PriorityQueueOptions<T>, 'isFix'>): boolean;
35
+ /**
36
+ * 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.
37
+ */
38
+ getNodes(): T[];
39
+ /**
40
+ * The "add" function adds a node to the heap and ensures that the heap property is maintained.
41
+ * @param {T} node - The parameter "node" is of type T, which means it can be any data type. It represents the node
42
+ * that needs to be added to the heap.
43
+ */
44
+ add(node: T): void;
45
+ /**
46
+ * The "has" function checks if a given node is present in the list of nodes.
47
+ * @param {T} node - The parameter `node` is of type `T`, which means it can be any type. It represents the node that
48
+ * we want to check if it exists in the `nodes` array.
49
+ * @returns a boolean value indicating whether the given node is included in the array of nodes.
50
+ */
51
+ has(node: T): boolean;
52
+ /**
53
+ * The `peek` function returns the first element of the `nodes` array if it exists, otherwise it returns `null`.
54
+ * @returns The `peek()` function is returning the first element (`T`) of the `nodes` array if the `size` is not zero.
55
+ * Otherwise, it returns `null`.
56
+ */
57
+ peek(): T | null;
58
+ /**
59
+ * The `poll` function removes and returns the top element from a heap data structure.
60
+ * @returns The `poll()` method returns a value of type `T` or `null`.
61
+ */
62
+ poll(): T | null;
63
+ /**
64
+ * The `leaf` function returns the last element in the `nodes` array or `null` if the array is empty.
65
+ * @returns The method `leaf()` is returning the last element (`T`) in the `nodes` array if it exists. If the array is
66
+ * empty or the last element is `null`, then it returns `null`.
67
+ */
68
+ leaf(): T | null;
69
+ /**
70
+ * The function checks if the size of an object is equal to zero and returns a boolean value indicating whether the
71
+ * object is empty or not.
72
+ * @returns The method `isEmpty()` is returning a boolean value indicating whether the size of the object is equal to
73
+ * 0.
74
+ */
75
+ isEmpty(): boolean;
76
+ /**
77
+ * The clear function clears the nodes array.
78
+ */
79
+ clear(): void;
80
+ /**
81
+ * The toArray function returns an array containing all the elements in the nodes property.
82
+ * @returns An array of type T, which is the elements of the nodes property.
83
+ */
84
+ toArray(): T[];
85
+ /**
86
+ * The `clone` function returns a new instance of the `PriorityQueue` class with the same nodes and comparator as the
87
+ * original instance.
88
+ * @returns The `clone()` method is returning a new instance of the `PriorityQueue` class with the same `nodes` and
89
+ * `comparator` properties as the original instance.
90
+ */
91
+ clone(): PriorityQueue<T>;
92
+ /**
93
+ * The `isValid` function recursively checks if a binary tree satisfies a certain condition.
94
+ * @returns The function `isValid()` returns a boolean value.
95
+ */
96
+ isValid(): boolean;
97
+ /**
98
+ * Plan to support sorting of duplicate elements.
99
+ */
100
+ /**
101
+ * The function sorts the elements in a data structure and returns them in an array.
102
+ * Plan to support sorting of duplicate elements.
103
+ * @returns The `sort()` method is returning an array of type `T[]`.
104
+ */
105
+ sort(): T[];
106
+ /**
107
+ * The DFS function performs a depth-first search traversal on a binary tree and returns an array of visited nodes
108
+ * based on the specified traversal order.
109
+ * @param {PriorityQueueDFSOrderPattern} dfsMode - The dfsMode parameter is a string that specifies the order in which
110
+ * the nodes should be visited during the Depth-First Search (DFS) traversal. It can have one of the following values:
111
+ * @returns an array of type `(T | null)[]`.
112
+ */
113
+ DFS(dfsMode: PriorityQueueDFSOrderPattern): (T | null)[];
114
+ protected _setNodes(value: T[]): void;
115
+ protected readonly _comparator: PriorityQueueComparator<T>;
116
+ /**
117
+ * The function compares two numbers using a custom comparator function.
118
+ * @param {number} a - The parameter "a" is a number that represents the index of a node in an array.
119
+ * @param {number} b - The parameter "b" is a number.
120
+ * @returns the result of the comparison between the elements at indices `a` and `b` in the `nodes` array. The
121
+ * comparison is done using the `_comparator` function, and if the result is greater than 0, `true` is returned,
122
+ * indicating that the element at index `a` is greater than the element at index `b`.
123
+ */
124
+ protected _compare(a: number, b: number): boolean;
125
+ /**
126
+ * The function swaps two elements in an array.
127
+ * @param {number} a - The parameter "a" is a number that represents the index of an element in an array.
128
+ * @param {number} b - The parameter "b" is a number.
129
+ */
130
+ protected _swap(a: number, b: number): void;
131
+ /**
132
+ * The function checks if a given index is valid within an array.
133
+ * @param {number} index - The parameter "index" is of type number and represents the index value that needs to be
134
+ * checked for validity.
135
+ * @returns A boolean value indicating whether the given index is valid or not.
136
+ */
137
+ protected _isValidIndex(index: number): boolean;
138
+ /**
139
+ * The function returns the index of the parent node given the index of a child node in a binary tree.
140
+ * @param {number} child - The "child" parameter is a number representing the index of a child node in a binary tree.
141
+ * @returns the parent of the given child node.
142
+ */
143
+ protected _getParent(child: number): number;
144
+ /**
145
+ * The function returns the index of the left child node in a binary tree given the index of its parent node.
146
+ * @param {number} parent - The parameter "parent" is a number that represents the index of a node in a binary tree.
147
+ * @returns the left child of a given parent node in a binary tree.
148
+ */
149
+ protected _getLeft(parent: number): number;
150
+ /**
151
+ * The function returns the index of the right child node in a binary tree given the index of its parent node.
152
+ * @param {number} parent - The parameter "parent" is a number that represents the index of a node in a binary tree.
153
+ * @returns the right child of a given parent node in a binary tree.
154
+ */
155
+ protected _getRight(parent: number): number;
156
+ /**
157
+ * The function returns the index of the smallest child node of a given parent node.
158
+ * @param {number} parent - The parent parameter is a number that represents the index of the parent node in a binary
159
+ * tree.
160
+ * @returns the minimum value between the parent node and its left and right child nodes.
161
+ */
162
+ protected _getComparedChild(parent: number): number;
163
+ /**
164
+ * The function `_heapifyUp` is used to maintain the heap property by moving an element up the heap until it is in the
165
+ * correct position.
166
+ * @param {number} start - The start parameter is the index of the element that needs to be moved up in the heap.
167
+ */
168
+ protected _heapifyUp(start: number): void;
169
+ /**
170
+ * The function performs a heapify operation by comparing and swapping elements in a binary heap.
171
+ * @param {number} start - The start parameter is the index of the element in the heap from where the heapifyDown
172
+ * operation should start.
173
+ */
174
+ protected _heapifyDown(start: number): void;
175
+ /**
176
+ * The _fix function performs a heapify operation on the elements of the heap starting from the middle and moving
177
+ * towards the root.
178
+ */
179
+ protected _fix(): void;
180
+ }