@obinexusmk2/hypernum 0.1.0 → 0.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.
- package/LICENSE +21 -21
- package/README.md +355 -256
- package/dist/index.cjs +4 -8
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +4 -7
- package/dist/index.js.map +1 -1
- package/dist/index.umd.js +2 -2
- package/dist/index.umd.js.map +1 -1
- package/dist/types/{config → src/config}/config-loader.d.ts +0 -0
- package/dist/types/src/config/config-loader.d.ts.map +1 -0
- package/dist/types/{config → src/config}/config-parser.d.ts +0 -0
- package/dist/types/src/config/config-parser.d.ts.map +1 -0
- package/dist/types/{config → src/config}/config-resolver.d.ts +0 -0
- package/dist/types/src/config/config-resolver.d.ts.map +1 -0
- package/dist/types/{config → src/config}/config-source.d.ts +0 -0
- package/dist/types/src/config/config-source.d.ts.map +1 -0
- package/dist/types/{config → src/config}/index.d.ts +0 -0
- package/dist/types/src/config/index.d.ts.map +1 -0
- package/dist/types/{core → src/core}/common.d.ts +0 -0
- package/dist/types/src/core/common.d.ts.map +1 -0
- package/dist/types/{core → src/core}/config.d.ts +0 -0
- package/dist/types/src/core/config.d.ts.map +1 -0
- package/dist/types/{core → src/core}/constants.d.ts +0 -0
- package/dist/types/src/core/constants.d.ts.map +1 -0
- package/dist/types/{core → src/core}/errors.d.ts +0 -0
- package/dist/types/src/core/errors.d.ts.map +1 -0
- package/dist/types/{core → src/core}/hypernum.d.ts +0 -0
- package/dist/types/src/core/hypernum.d.ts.map +1 -0
- package/dist/types/{core → src/core}/index.d.ts +0 -0
- package/dist/types/src/core/index.d.ts.map +1 -0
- package/dist/types/{index.d.ts → src/index.d.ts} +1 -1
- package/dist/types/src/index.d.ts.map +1 -0
- package/dist/types/{operations → src/operations}/arithmetic.d.ts +0 -0
- package/dist/types/src/operations/arithmetic.d.ts.map +1 -0
- package/dist/types/{operations → src/operations}/bitwise.d.ts +0 -0
- package/dist/types/src/operations/bitwise.d.ts.map +1 -0
- package/dist/types/{operations → src/operations}/comparison.d.ts +0 -0
- package/dist/types/src/operations/comparison.d.ts.map +1 -0
- package/dist/types/{operations → src/operations}/conversion.d.ts +0 -0
- package/dist/types/src/operations/conversion.d.ts.map +1 -0
- package/dist/types/{operations → src/operations}/factorial.d.ts +0 -0
- package/dist/types/src/operations/factorial.d.ts.map +1 -0
- package/dist/types/{operations → src/operations}/index.d.ts +0 -0
- package/dist/types/src/operations/index.d.ts.map +1 -0
- package/dist/types/{operations → src/operations}/power.d.ts +0 -0
- package/dist/types/src/operations/power.d.ts.map +1 -0
- package/dist/types/{storage → src/storage}/Heap.d.ts +0 -0
- package/dist/types/src/storage/Heap.d.ts.map +1 -0
- package/dist/types/{storage → src/storage}/index.d.ts +0 -0
- package/dist/types/src/storage/index.d.ts.map +1 -0
- package/dist/types/{structures → src/structures}/ackermann.d.ts +0 -0
- package/dist/types/src/structures/ackermann.d.ts.map +1 -0
- package/dist/types/{structures → src/structures}/big-array.d.ts +0 -0
- package/dist/types/src/structures/big-array.d.ts.map +1 -0
- package/dist/types/{structures → src/structures}/index.d.ts +0 -0
- package/dist/types/src/structures/index.d.ts.map +1 -0
- package/dist/types/{structures → src/structures}/number-tree.d.ts +0 -0
- package/dist/types/src/structures/number-tree.d.ts.map +1 -0
- package/dist/types/{structures → src/structures}/power-tower.d.ts +0 -0
- package/dist/types/src/structures/power-tower.d.ts.map +1 -0
- package/dist/types/{utils → src/utils}/formatting.d.ts +0 -0
- package/dist/types/src/utils/formatting.d.ts.map +1 -0
- package/dist/types/{utils → src/utils}/index.d.ts +0 -0
- package/dist/types/src/utils/index.d.ts.map +1 -0
- package/dist/types/{utils → src/utils}/parser.d.ts +0 -0
- package/dist/types/src/utils/parser.d.ts.map +1 -0
- package/dist/types/{utils → src/utils}/precision.d.ts +0 -0
- package/dist/types/src/utils/precision.d.ts.map +1 -0
- package/dist/types/{utils → src/utils}/validation.d.ts +0 -0
- package/dist/types/src/utils/validation.d.ts.map +1 -0
- package/package.json +169 -164
- package/rollup.config.js +163 -161
- package/src/cli/hypernum.js +272 -0
- package/src/config/config-loader.ts +0 -0
- package/src/config/config-parser.ts +160 -160
- package/src/config/config-resolver.ts +0 -0
- package/src/config/config-source.ts +0 -0
- package/src/config/index.ts +0 -0
- package/src/core/common.ts +184 -184
- package/src/core/config.ts +392 -392
- package/src/core/constants.ts +101 -101
- package/src/core/errors.ts +202 -202
- package/src/core/hypernum.ts +240 -240
- package/src/core/index.ts +4 -4
- package/src/index.ts +179 -182
- package/src/operations/arithmetic.ts +332 -332
- package/src/operations/bitwise.ts +366 -366
- package/src/operations/comparison.ts +271 -271
- package/src/operations/conversion.ts +399 -399
- package/src/operations/factorial.ts +278 -278
- package/src/operations/index.ts +4 -4
- package/src/operations/power.ts +315 -315
- package/src/storage/Heap.ts +237 -237
- package/src/storage/index.ts +0 -0
- package/src/structures/ackermann.ts +232 -232
- package/src/structures/big-array.ts +305 -305
- package/src/structures/index.ts +3 -3
- package/src/structures/number-tree.ts +403 -403
- package/src/structures/power-tower.ts +277 -277
- package/src/types/common.d.ts +356 -356
- package/src/types/core.d.ts +160 -160
- package/src/types/index.d.ts +1 -1
- package/src/utils/formatting.ts +245 -245
- package/src/utils/index.ts +4 -4
- package/src/utils/parser.ts +244 -244
- package/src/utils/precision.ts +216 -216
- package/src/utils/validation.ts +182 -182
- package/tsconfig.json +83 -83
- package/dist/types/config/config-loader.d.ts.map +0 -1
- package/dist/types/config/config-parser.d.ts.map +0 -1
- package/dist/types/config/config-resolver.d.ts.map +0 -1
- package/dist/types/config/config-source.d.ts.map +0 -1
- package/dist/types/config/index.d.ts.map +0 -1
- package/dist/types/core/common.d.ts.map +0 -1
- package/dist/types/core/config.d.ts.map +0 -1
- package/dist/types/core/constants.d.ts.map +0 -1
- package/dist/types/core/errors.d.ts.map +0 -1
- package/dist/types/core/hypernum.d.ts.map +0 -1
- package/dist/types/core/index.d.ts.map +0 -1
- package/dist/types/index.d.ts.map +0 -1
- package/dist/types/operations/arithmetic.d.ts.map +0 -1
- package/dist/types/operations/bitwise.d.ts.map +0 -1
- package/dist/types/operations/comparison.d.ts.map +0 -1
- package/dist/types/operations/conversion.d.ts.map +0 -1
- package/dist/types/operations/factorial.d.ts.map +0 -1
- package/dist/types/operations/index.d.ts.map +0 -1
- package/dist/types/operations/power.d.ts.map +0 -1
- package/dist/types/storage/Heap.d.ts.map +0 -1
- package/dist/types/storage/index.d.ts.map +0 -1
- package/dist/types/structures/ackermann.d.ts.map +0 -1
- package/dist/types/structures/big-array.d.ts.map +0 -1
- package/dist/types/structures/index.d.ts.map +0 -1
- package/dist/types/structures/number-tree.d.ts.map +0 -1
- package/dist/types/structures/power-tower.d.ts.map +0 -1
- package/dist/types/utils/formatting.d.ts.map +0 -1
- package/dist/types/utils/index.d.ts.map +0 -1
- package/dist/types/utils/parser.d.ts.map +0 -1
- package/dist/types/utils/precision.d.ts.map +0 -1
- package/dist/types/utils/validation.d.ts.map +0 -1
package/src/storage/Heap.ts
CHANGED
|
@@ -1,238 +1,238 @@
|
|
|
1
|
-
|
|
2
|
-
/**
|
|
3
|
-
* Represents the result of a comparison operation
|
|
4
|
-
* -1: first value is less than second value
|
|
5
|
-
* 0: values are equal
|
|
6
|
-
* 1: first value is greater than second value
|
|
7
|
-
*/
|
|
8
|
-
export type ComparisonResult = -1 | 0 | 1;
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* Generic comparator function type for heap elements
|
|
12
|
-
*/
|
|
13
|
-
export type Comparator<T> = (a: T, b: T) => ComparisonResult;
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* Abstract base heap class implementing common heap operations
|
|
17
|
-
*/
|
|
18
|
-
abstract class Heap<T> {
|
|
19
|
-
protected heap: T[];
|
|
20
|
-
protected readonly compare: Comparator<T>;
|
|
21
|
-
|
|
22
|
-
constructor(comparator: Comparator<T>) {
|
|
23
|
-
this.heap = [];
|
|
24
|
-
this.compare = comparator;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* Gets the size of the heap
|
|
29
|
-
*/
|
|
30
|
-
public size(): number {
|
|
31
|
-
return this.heap.length;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* Checks if the heap is empty
|
|
36
|
-
*/
|
|
37
|
-
public isEmpty(): boolean {
|
|
38
|
-
return this.heap.length === 0;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* Peeks at the root element without removing it
|
|
43
|
-
*/
|
|
44
|
-
public peek(): T | undefined {
|
|
45
|
-
return this.heap[0];
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* Inserts a new element into the heap
|
|
50
|
-
*/
|
|
51
|
-
public push(value: T): void {
|
|
52
|
-
this.heap.push(value);
|
|
53
|
-
this.siftUp(this.heap.length - 1);
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* Removes and returns the root element
|
|
58
|
-
*/
|
|
59
|
-
public pop(): T | undefined {
|
|
60
|
-
if (this.isEmpty()) {
|
|
61
|
-
return undefined;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
const root = this.heap[0];
|
|
65
|
-
const last = this.heap.pop()!;
|
|
66
|
-
|
|
67
|
-
if (!this.isEmpty()) {
|
|
68
|
-
this.heap[0] = last;
|
|
69
|
-
this.siftDown(0);
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
return root;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
/**
|
|
76
|
-
* Removes all elements from the heap
|
|
77
|
-
*/
|
|
78
|
-
public clear(): void {
|
|
79
|
-
this.heap = [];
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
/**
|
|
83
|
-
* Creates a heap from an array of elements
|
|
84
|
-
*/
|
|
85
|
-
public static heapify<T extends {}>(array: T[], comparator: Comparator<T>): Heap<T> {
|
|
86
|
-
const heap = this instanceof MinHeap ? new MinHeap(comparator) : new MaxHeap(comparator);
|
|
87
|
-
array.forEach(item => heap.push(item));
|
|
88
|
-
return heap;
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
/**
|
|
92
|
-
* Gets the parent index of a node
|
|
93
|
-
*/
|
|
94
|
-
protected getParentIndex(index: number): number {
|
|
95
|
-
return Math.floor((index - 1) / 2);
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
/**
|
|
99
|
-
* Gets the left child index of a node
|
|
100
|
-
*/
|
|
101
|
-
protected getLeftChildIndex(index: number): number {
|
|
102
|
-
return 2 * index + 1;
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
/**
|
|
106
|
-
* Gets the right child index of a node
|
|
107
|
-
*/
|
|
108
|
-
protected getRightChildIndex(index: number): number {
|
|
109
|
-
return 2 * index + 2;
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
/**
|
|
113
|
-
* Swaps two elements in the heap
|
|
114
|
-
*/
|
|
115
|
-
protected swap(i: number, j: number): void {
|
|
116
|
-
const temp = this.heap[i]!;
|
|
117
|
-
this.heap[i] = this.heap[j]!;
|
|
118
|
-
this.heap[j] = temp;
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
/**
|
|
122
|
-
* Moves an element up the heap until heap property is satisfied
|
|
123
|
-
*/
|
|
124
|
-
protected abstract siftUp(index: number): void;
|
|
125
|
-
|
|
126
|
-
/**
|
|
127
|
-
* Moves an element down the heap until heap property is satisfied
|
|
128
|
-
*/
|
|
129
|
-
protected abstract siftDown(index: number): void;
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
/**
|
|
133
|
-
* MinHeap implementation where the root is the smallest element
|
|
134
|
-
*/
|
|
135
|
-
export class MinHeap<T> extends Heap<T> {
|
|
136
|
-
constructor(comparator: Comparator<T>) {
|
|
137
|
-
super(comparator);
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
protected siftUp(index: number): void {
|
|
141
|
-
while (index > 0) {
|
|
142
|
-
const parentIndex = this.getParentIndex(index);
|
|
143
|
-
if (this.compare(this.heap[index]!, this.heap[parentIndex]!) >= 0) {
|
|
144
|
-
break;
|
|
145
|
-
}
|
|
146
|
-
this.swap(index, parentIndex);
|
|
147
|
-
index = parentIndex;
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
protected siftDown(index: number): void {
|
|
152
|
-
const size = this.heap.length;
|
|
153
|
-
|
|
154
|
-
while (true) {
|
|
155
|
-
let smallest = index;
|
|
156
|
-
const left = this.getLeftChildIndex(index);
|
|
157
|
-
const right = this.getRightChildIndex(index);
|
|
158
|
-
|
|
159
|
-
if (left < size && this.compare(this.heap[left]!, this.heap[smallest]!) < 0) {
|
|
160
|
-
smallest = left;
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
if (right < size && this.heap[right] !== undefined && this.compare(this.heap[right] as T, this.heap[smallest] as T) < 0) {
|
|
164
|
-
smallest = right;
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
if (smallest === index) {
|
|
168
|
-
break;
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
this.swap(index, smallest);
|
|
172
|
-
index = smallest;
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
/**
|
|
178
|
-
* MaxHeap implementation where the root is the largest element
|
|
179
|
-
*/
|
|
180
|
-
export class MaxHeap<T> extends Heap<T> {
|
|
181
|
-
constructor(comparator: Comparator<T>) {
|
|
182
|
-
super(comparator);
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
protected siftUp(index: number): void {
|
|
186
|
-
while (index > 0) {
|
|
187
|
-
const parentIndex = this.getParentIndex(index);
|
|
188
|
-
if (this.compare(this.heap[index]!, this.heap[parentIndex]!) <= 0) {
|
|
189
|
-
break;
|
|
190
|
-
}
|
|
191
|
-
this.swap(index, parentIndex);
|
|
192
|
-
index = parentIndex;
|
|
193
|
-
}
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
protected siftDown(index: number): void {
|
|
197
|
-
const size = this.heap.length;
|
|
198
|
-
|
|
199
|
-
while (true) {
|
|
200
|
-
let largest = index;
|
|
201
|
-
const left = this.getLeftChildIndex(index);
|
|
202
|
-
const right = this.getRightChildIndex(index);
|
|
203
|
-
|
|
204
|
-
if (left < size && this.heap[left] !== undefined && this.compare(this.heap[left]!, this.heap[largest]!) > 0) {
|
|
205
|
-
largest = left;
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
if (right < size && this.heap[right] !== undefined && this.compare(this.heap[right]!, this.heap[largest]!) > 0) {
|
|
209
|
-
largest = right;
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
if (largest === index) {
|
|
213
|
-
break;
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
this.swap(index, largest);
|
|
217
|
-
index = largest;
|
|
218
|
-
}
|
|
219
|
-
}
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
// Type Guards
|
|
223
|
-
export const isMinHeap = <T>(heap: Heap<T>): heap is MinHeap<T> => {
|
|
224
|
-
return heap instanceof MinHeap;
|
|
225
|
-
};
|
|
226
|
-
|
|
227
|
-
export const isMaxHeap = <T>(heap: Heap<T>): heap is MaxHeap<T> => {
|
|
228
|
-
return heap instanceof MaxHeap;
|
|
229
|
-
};
|
|
230
|
-
|
|
231
|
-
/**
|
|
232
|
-
* Custom comparator for large numbers
|
|
233
|
-
*/
|
|
234
|
-
export function createLargeNumberComparator(): (a: bigint, b: bigint) => number {
|
|
235
|
-
return (a, b) => {
|
|
236
|
-
return a > b ? 1 : a < b ? -1 : 0;
|
|
237
|
-
};
|
|
1
|
+
|
|
2
|
+
/**
|
|
3
|
+
* Represents the result of a comparison operation
|
|
4
|
+
* -1: first value is less than second value
|
|
5
|
+
* 0: values are equal
|
|
6
|
+
* 1: first value is greater than second value
|
|
7
|
+
*/
|
|
8
|
+
export type ComparisonResult = -1 | 0 | 1;
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Generic comparator function type for heap elements
|
|
12
|
+
*/
|
|
13
|
+
export type Comparator<T> = (a: T, b: T) => ComparisonResult;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Abstract base heap class implementing common heap operations
|
|
17
|
+
*/
|
|
18
|
+
abstract class Heap<T> {
|
|
19
|
+
protected heap: T[];
|
|
20
|
+
protected readonly compare: Comparator<T>;
|
|
21
|
+
|
|
22
|
+
constructor(comparator: Comparator<T>) {
|
|
23
|
+
this.heap = [];
|
|
24
|
+
this.compare = comparator;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Gets the size of the heap
|
|
29
|
+
*/
|
|
30
|
+
public size(): number {
|
|
31
|
+
return this.heap.length;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Checks if the heap is empty
|
|
36
|
+
*/
|
|
37
|
+
public isEmpty(): boolean {
|
|
38
|
+
return this.heap.length === 0;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Peeks at the root element without removing it
|
|
43
|
+
*/
|
|
44
|
+
public peek(): T | undefined {
|
|
45
|
+
return this.heap[0];
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Inserts a new element into the heap
|
|
50
|
+
*/
|
|
51
|
+
public push(value: T): void {
|
|
52
|
+
this.heap.push(value);
|
|
53
|
+
this.siftUp(this.heap.length - 1);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Removes and returns the root element
|
|
58
|
+
*/
|
|
59
|
+
public pop(): T | undefined {
|
|
60
|
+
if (this.isEmpty()) {
|
|
61
|
+
return undefined;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const root = this.heap[0];
|
|
65
|
+
const last = this.heap.pop()!;
|
|
66
|
+
|
|
67
|
+
if (!this.isEmpty()) {
|
|
68
|
+
this.heap[0] = last;
|
|
69
|
+
this.siftDown(0);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
return root;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Removes all elements from the heap
|
|
77
|
+
*/
|
|
78
|
+
public clear(): void {
|
|
79
|
+
this.heap = [];
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Creates a heap from an array of elements
|
|
84
|
+
*/
|
|
85
|
+
public static heapify<T extends {}>(array: T[], comparator: Comparator<T>): Heap<T> {
|
|
86
|
+
const heap = this instanceof MinHeap ? new MinHeap(comparator) : new MaxHeap(comparator);
|
|
87
|
+
array.forEach(item => heap.push(item));
|
|
88
|
+
return heap;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Gets the parent index of a node
|
|
93
|
+
*/
|
|
94
|
+
protected getParentIndex(index: number): number {
|
|
95
|
+
return Math.floor((index - 1) / 2);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Gets the left child index of a node
|
|
100
|
+
*/
|
|
101
|
+
protected getLeftChildIndex(index: number): number {
|
|
102
|
+
return 2 * index + 1;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Gets the right child index of a node
|
|
107
|
+
*/
|
|
108
|
+
protected getRightChildIndex(index: number): number {
|
|
109
|
+
return 2 * index + 2;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Swaps two elements in the heap
|
|
114
|
+
*/
|
|
115
|
+
protected swap(i: number, j: number): void {
|
|
116
|
+
const temp = this.heap[i]!;
|
|
117
|
+
this.heap[i] = this.heap[j]!;
|
|
118
|
+
this.heap[j] = temp;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Moves an element up the heap until heap property is satisfied
|
|
123
|
+
*/
|
|
124
|
+
protected abstract siftUp(index: number): void;
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Moves an element down the heap until heap property is satisfied
|
|
128
|
+
*/
|
|
129
|
+
protected abstract siftDown(index: number): void;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* MinHeap implementation where the root is the smallest element
|
|
134
|
+
*/
|
|
135
|
+
export class MinHeap<T> extends Heap<T> {
|
|
136
|
+
constructor(comparator: Comparator<T>) {
|
|
137
|
+
super(comparator);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
protected siftUp(index: number): void {
|
|
141
|
+
while (index > 0) {
|
|
142
|
+
const parentIndex = this.getParentIndex(index);
|
|
143
|
+
if (this.compare(this.heap[index]!, this.heap[parentIndex]!) >= 0) {
|
|
144
|
+
break;
|
|
145
|
+
}
|
|
146
|
+
this.swap(index, parentIndex);
|
|
147
|
+
index = parentIndex;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
protected siftDown(index: number): void {
|
|
152
|
+
const size = this.heap.length;
|
|
153
|
+
|
|
154
|
+
while (true) {
|
|
155
|
+
let smallest = index;
|
|
156
|
+
const left = this.getLeftChildIndex(index);
|
|
157
|
+
const right = this.getRightChildIndex(index);
|
|
158
|
+
|
|
159
|
+
if (left < size && this.compare(this.heap[left]!, this.heap[smallest]!) < 0) {
|
|
160
|
+
smallest = left;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
if (right < size && this.heap[right] !== undefined && this.compare(this.heap[right] as T, this.heap[smallest] as T) < 0) {
|
|
164
|
+
smallest = right;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
if (smallest === index) {
|
|
168
|
+
break;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
this.swap(index, smallest);
|
|
172
|
+
index = smallest;
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* MaxHeap implementation where the root is the largest element
|
|
179
|
+
*/
|
|
180
|
+
export class MaxHeap<T> extends Heap<T> {
|
|
181
|
+
constructor(comparator: Comparator<T>) {
|
|
182
|
+
super(comparator);
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
protected siftUp(index: number): void {
|
|
186
|
+
while (index > 0) {
|
|
187
|
+
const parentIndex = this.getParentIndex(index);
|
|
188
|
+
if (this.compare(this.heap[index]!, this.heap[parentIndex]!) <= 0) {
|
|
189
|
+
break;
|
|
190
|
+
}
|
|
191
|
+
this.swap(index, parentIndex);
|
|
192
|
+
index = parentIndex;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
protected siftDown(index: number): void {
|
|
197
|
+
const size = this.heap.length;
|
|
198
|
+
|
|
199
|
+
while (true) {
|
|
200
|
+
let largest = index;
|
|
201
|
+
const left = this.getLeftChildIndex(index);
|
|
202
|
+
const right = this.getRightChildIndex(index);
|
|
203
|
+
|
|
204
|
+
if (left < size && this.heap[left] !== undefined && this.compare(this.heap[left]!, this.heap[largest]!) > 0) {
|
|
205
|
+
largest = left;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
if (right < size && this.heap[right] !== undefined && this.compare(this.heap[right]!, this.heap[largest]!) > 0) {
|
|
209
|
+
largest = right;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
if (largest === index) {
|
|
213
|
+
break;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
this.swap(index, largest);
|
|
217
|
+
index = largest;
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
// Type Guards
|
|
223
|
+
export const isMinHeap = <T>(heap: Heap<T>): heap is MinHeap<T> => {
|
|
224
|
+
return heap instanceof MinHeap;
|
|
225
|
+
};
|
|
226
|
+
|
|
227
|
+
export const isMaxHeap = <T>(heap: Heap<T>): heap is MaxHeap<T> => {
|
|
228
|
+
return heap instanceof MaxHeap;
|
|
229
|
+
};
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
* Custom comparator for large numbers
|
|
233
|
+
*/
|
|
234
|
+
export function createLargeNumberComparator(): (a: bigint, b: bigint) => number {
|
|
235
|
+
return (a, b) => {
|
|
236
|
+
return a > b ? 1 : a < b ? -1 : 0;
|
|
237
|
+
};
|
|
238
238
|
}
|
package/src/storage/index.ts
CHANGED
|
File without changes
|