@obinexusmk2/hypernum 0.1.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/LICENSE +21 -0
- package/README.md +256 -0
- package/dist/index.cjs +3425 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +1449 -0
- package/dist/index.js +3284 -0
- package/dist/index.js.map +1 -0
- package/dist/index.umd.js +8 -0
- package/dist/index.umd.js.map +1 -0
- package/dist/types/config/config-loader.d.ts +56 -0
- package/dist/types/config/config-loader.d.ts.map +1 -0
- package/dist/types/config/config-parser.d.ts +28 -0
- package/dist/types/config/config-parser.d.ts.map +1 -0
- package/dist/types/config/config-resolver.d.ts +21 -0
- package/dist/types/config/config-resolver.d.ts.map +1 -0
- package/dist/types/config/config-source.d.ts +27 -0
- package/dist/types/config/config-source.d.ts.map +1 -0
- package/dist/types/config/index.d.ts +68 -0
- package/dist/types/config/index.d.ts.map +1 -0
- package/dist/types/core/common.d.ts +169 -0
- package/dist/types/core/common.d.ts.map +1 -0
- package/dist/types/core/config.d.ts +197 -0
- package/dist/types/core/config.d.ts.map +1 -0
- package/dist/types/core/constants.d.ts +88 -0
- package/dist/types/core/constants.d.ts.map +1 -0
- package/dist/types/core/errors.d.ts +97 -0
- package/dist/types/core/errors.d.ts.map +1 -0
- package/dist/types/core/hypernum.d.ts +60 -0
- package/dist/types/core/hypernum.d.ts.map +1 -0
- package/dist/types/core/index.d.ts +6 -0
- package/dist/types/core/index.d.ts.map +1 -0
- package/dist/types/index.d.ts +33 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/operations/arithmetic.d.ts +72 -0
- package/dist/types/operations/arithmetic.d.ts.map +1 -0
- package/dist/types/operations/bitwise.d.ts +98 -0
- package/dist/types/operations/bitwise.d.ts.map +1 -0
- package/dist/types/operations/comparison.d.ts +94 -0
- package/dist/types/operations/comparison.d.ts.map +1 -0
- package/dist/types/operations/conversion.d.ts +79 -0
- package/dist/types/operations/conversion.d.ts.map +1 -0
- package/dist/types/operations/factorial.d.ts +58 -0
- package/dist/types/operations/factorial.d.ts.map +1 -0
- package/dist/types/operations/index.d.ts +6 -0
- package/dist/types/operations/index.d.ts.map +1 -0
- package/dist/types/operations/power.d.ts +49 -0
- package/dist/types/operations/power.d.ts.map +1 -0
- package/dist/types/storage/Heap.d.ts +95 -0
- package/dist/types/storage/Heap.d.ts.map +1 -0
- package/dist/types/storage/index.d.ts +2 -0
- package/dist/types/storage/index.d.ts.map +1 -0
- package/dist/types/structures/ackermann.d.ts +74 -0
- package/dist/types/structures/ackermann.d.ts.map +1 -0
- package/dist/types/structures/big-array.d.ts +102 -0
- package/dist/types/structures/big-array.d.ts.map +1 -0
- package/dist/types/structures/index.d.ts +5 -0
- package/dist/types/structures/index.d.ts.map +1 -0
- package/dist/types/structures/number-tree.d.ts +114 -0
- package/dist/types/structures/number-tree.d.ts.map +1 -0
- package/dist/types/structures/power-tower.d.ts +74 -0
- package/dist/types/structures/power-tower.d.ts.map +1 -0
- package/dist/types/utils/formatting.d.ts +45 -0
- package/dist/types/utils/formatting.d.ts.map +1 -0
- package/dist/types/utils/index.d.ts +5 -0
- package/dist/types/utils/index.d.ts.map +1 -0
- package/dist/types/utils/parser.d.ts +39 -0
- package/dist/types/utils/parser.d.ts.map +1 -0
- package/dist/types/utils/precision.d.ts +57 -0
- package/dist/types/utils/precision.d.ts.map +1 -0
- package/dist/types/utils/validation.d.ts +28 -0
- package/dist/types/utils/validation.d.ts.map +1 -0
- package/package.json +164 -0
- package/rollup.config.js +162 -0
- package/src/config/config-loader.ts +226 -0
- package/src/config/config-parser.ts +161 -0
- package/src/config/config-resolver.ts +52 -0
- package/src/config/config-source.ts +32 -0
- package/src/config/index.ts +159 -0
- package/src/core/common.ts +185 -0
- package/src/core/config.ts +393 -0
- package/src/core/constants.ts +102 -0
- package/src/core/errors.ts +203 -0
- package/src/core/hypernum.ts +241 -0
- package/src/core/index.ts +5 -0
- package/src/index.ts +183 -0
- package/src/operations/arithmetic.ts +333 -0
- package/src/operations/bitwise.ts +367 -0
- package/src/operations/comparison.ts +272 -0
- package/src/operations/conversion.ts +400 -0
- package/src/operations/factorial.ts +279 -0
- package/src/operations/index.ts +5 -0
- package/src/operations/power.ts +316 -0
- package/src/storage/Heap.ts +238 -0
- package/src/storage/index.ts +1 -0
- package/src/structures/ackermann.ts +233 -0
- package/src/structures/big-array.ts +306 -0
- package/src/structures/index.ts +4 -0
- package/src/structures/number-tree.ts +404 -0
- package/src/structures/power-tower.ts +278 -0
- package/src/types/common.d.ts +357 -0
- package/src/types/core.d.ts +161 -0
- package/src/types/index.d.ts +2 -0
- package/src/utils/formatting.ts +246 -0
- package/src/utils/index.ts +4 -0
- package/src/utils/parser.ts +245 -0
- package/src/utils/precision.ts +217 -0
- package/src/utils/validation.ts +183 -0
- package/tsconfig.json +84 -0
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Custom error types for Hypernum library
|
|
3
|
+
* Provides specific error classes for different types of errors that can occur
|
|
4
|
+
* during mathematical operations and data structure manipulations
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { ERROR_MESSAGES } from './constants';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Base error class for Hypernum library
|
|
11
|
+
* All other error classes inherit from this
|
|
12
|
+
*/
|
|
13
|
+
export class HypernumError extends Error {
|
|
14
|
+
constructor(message: string) {
|
|
15
|
+
super(message);
|
|
16
|
+
this.name = 'HypernumError';
|
|
17
|
+
Object.setPrototypeOf(this, HypernumError.prototype);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Error for validation failures
|
|
23
|
+
*/
|
|
24
|
+
export class ValidationError extends HypernumError {
|
|
25
|
+
constructor(message: string) {
|
|
26
|
+
super(message);
|
|
27
|
+
this.name = 'ValidationError';
|
|
28
|
+
Object.setPrototypeOf(this, ValidationError.prototype);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Error for arithmetic overflow conditions
|
|
34
|
+
*/
|
|
35
|
+
export class OverflowError extends HypernumError {
|
|
36
|
+
constructor(message: string = ERROR_MESSAGES.OVERFLOW) {
|
|
37
|
+
super(message);
|
|
38
|
+
this.name = 'OverflowError';
|
|
39
|
+
Object.setPrototypeOf(this, OverflowError.prototype);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Error for arithmetic underflow conditions
|
|
45
|
+
*/
|
|
46
|
+
export class UnderflowError extends HypernumError {
|
|
47
|
+
constructor(message: string = ERROR_MESSAGES.UNDERFLOW) {
|
|
48
|
+
super(message);
|
|
49
|
+
this.name = 'UnderflowError';
|
|
50
|
+
Object.setPrototypeOf(this, UnderflowError.prototype);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Error for division by zero
|
|
56
|
+
*/
|
|
57
|
+
export class DivisionByZeroError extends HypernumError {
|
|
58
|
+
constructor(message: string = ERROR_MESSAGES.DIVISION_BY_ZERO) {
|
|
59
|
+
super(message);
|
|
60
|
+
this.name = 'DivisionByZeroError';
|
|
61
|
+
Object.setPrototypeOf(this, DivisionByZeroError.prototype);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Error for precision-related issues
|
|
67
|
+
*/
|
|
68
|
+
export class PrecisionError extends HypernumError {
|
|
69
|
+
constructor(message: string = ERROR_MESSAGES.INVALID_PRECISION) {
|
|
70
|
+
super(message);
|
|
71
|
+
this.name = 'PrecisionError';
|
|
72
|
+
Object.setPrototypeOf(this, PrecisionError.prototype);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Error for computation limits exceeded
|
|
78
|
+
*/
|
|
79
|
+
export class ComputationLimitError extends HypernumError {
|
|
80
|
+
constructor(message: string = ERROR_MESSAGES.COMPUTATION_LIMIT) {
|
|
81
|
+
super(message);
|
|
82
|
+
this.name = 'ComputationLimitError';
|
|
83
|
+
Object.setPrototypeOf(this, ComputationLimitError.prototype);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Error for invalid operations on data structures
|
|
89
|
+
*/
|
|
90
|
+
export class DataStructureError extends HypernumError {
|
|
91
|
+
constructor(message: string) {
|
|
92
|
+
super(message);
|
|
93
|
+
this.name = 'DataStructureError';
|
|
94
|
+
Object.setPrototypeOf(this, DataStructureError.prototype);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Error for heap property violations
|
|
100
|
+
*/
|
|
101
|
+
export class HeapPropertyError extends DataStructureError {
|
|
102
|
+
constructor(message: string = ERROR_MESSAGES.INVALID_HEAP_PROPERTY) {
|
|
103
|
+
super(message);
|
|
104
|
+
this.name = 'HeapPropertyError';
|
|
105
|
+
Object.setPrototypeOf(this, HeapPropertyError.prototype);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Error for tree-related issues
|
|
111
|
+
*/
|
|
112
|
+
export class TreeError extends DataStructureError {
|
|
113
|
+
constructor(message: string = ERROR_MESSAGES.TREE_DEPTH_EXCEEDED) {
|
|
114
|
+
super(message);
|
|
115
|
+
this.name = 'TreeError';
|
|
116
|
+
Object.setPrototypeOf(this, TreeError.prototype);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Error for array index out of bounds
|
|
122
|
+
*/
|
|
123
|
+
export class IndexError extends DataStructureError {
|
|
124
|
+
constructor(message: string = ERROR_MESSAGES.NEGATIVE_INDEX) {
|
|
125
|
+
super(message);
|
|
126
|
+
this.name = 'IndexError';
|
|
127
|
+
Object.setPrototypeOf(this, IndexError.prototype);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Error for invalid number format or conversion
|
|
133
|
+
*/
|
|
134
|
+
export class FormatError extends HypernumError {
|
|
135
|
+
constructor(message: string) {
|
|
136
|
+
super(message);
|
|
137
|
+
this.name = 'FormatError';
|
|
138
|
+
Object.setPrototypeOf(this, FormatError.prototype);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Error for invalid Roman numeral operations
|
|
144
|
+
*/
|
|
145
|
+
export class RomanNumeralError extends FormatError {
|
|
146
|
+
constructor(message: string = ERROR_MESSAGES.INVALID_ROMAN) {
|
|
147
|
+
super(message);
|
|
148
|
+
this.name = 'RomanNumeralError';
|
|
149
|
+
Object.setPrototypeOf(this, RomanNumeralError.prototype);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Type guard to check if an error is a Hypernum error
|
|
155
|
+
*/
|
|
156
|
+
export function isHypernumError(error: unknown): error is HypernumError {
|
|
157
|
+
return error instanceof HypernumError;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* Helper function to wrap unknown errors into HypernumError
|
|
162
|
+
*/
|
|
163
|
+
export function wrapError(error: unknown): HypernumError {
|
|
164
|
+
if (isHypernumError(error)) {
|
|
165
|
+
return error;
|
|
166
|
+
}
|
|
167
|
+
if (error instanceof Error) {
|
|
168
|
+
return new HypernumError(error.message);
|
|
169
|
+
}
|
|
170
|
+
return new HypernumError('An unknown error occurred');
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* Helper function to create an appropriate error from a message and optional type
|
|
175
|
+
*/
|
|
176
|
+
export function createError(message: string, type?: string): HypernumError {
|
|
177
|
+
switch (type) {
|
|
178
|
+
case 'validation':
|
|
179
|
+
return new ValidationError(message);
|
|
180
|
+
case 'overflow':
|
|
181
|
+
return new OverflowError(message);
|
|
182
|
+
case 'underflow':
|
|
183
|
+
return new UnderflowError(message);
|
|
184
|
+
case 'division':
|
|
185
|
+
return new DivisionByZeroError(message);
|
|
186
|
+
case 'precision':
|
|
187
|
+
return new PrecisionError(message);
|
|
188
|
+
case 'computation':
|
|
189
|
+
return new ComputationLimitError(message);
|
|
190
|
+
case 'heap':
|
|
191
|
+
return new HeapPropertyError(message);
|
|
192
|
+
case 'tree':
|
|
193
|
+
return new TreeError(message);
|
|
194
|
+
case 'index':
|
|
195
|
+
return new IndexError(message);
|
|
196
|
+
case 'format':
|
|
197
|
+
return new FormatError(message);
|
|
198
|
+
case 'roman':
|
|
199
|
+
return new RomanNumeralError(message);
|
|
200
|
+
default:
|
|
201
|
+
return new HypernumError(message);
|
|
202
|
+
}
|
|
203
|
+
}
|
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Main Hypernum class that provides a high-level interface to all library functionality
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
import {
|
|
7
|
+
DEFAULT_OPTIONS,
|
|
8
|
+
FEATURES,
|
|
9
|
+
MAX_PRECISION,
|
|
10
|
+
MAX_COMPUTATION_STEPS
|
|
11
|
+
} from './constants';
|
|
12
|
+
import {
|
|
13
|
+
HypernumError,
|
|
14
|
+
ValidationError,
|
|
15
|
+
OverflowError
|
|
16
|
+
} from './errors';
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
// Import all operations and structures
|
|
20
|
+
import * as arithmetic from '../operations/arithmetic';
|
|
21
|
+
import * as bitwise from '../operations/bitwise';
|
|
22
|
+
import * as power from '../operations/power';
|
|
23
|
+
import { BigArray, NumberTree, AckermannStructure } from '../structures';
|
|
24
|
+
import * as formatting from '../utils/formatting';
|
|
25
|
+
import * as validation from '../utils/validation';
|
|
26
|
+
import * as precision from '../utils/precision';
|
|
27
|
+
import { MinHeap, MaxHeap } from '@/storage';
|
|
28
|
+
/**
|
|
29
|
+
* Configuration options for Hypernum instance
|
|
30
|
+
*/
|
|
31
|
+
export interface HypernumConfig {
|
|
32
|
+
precision?: number;
|
|
33
|
+
roundingMode?: precision.RoundingMode;
|
|
34
|
+
checkOverflow?: boolean;
|
|
35
|
+
maxSteps?: number;
|
|
36
|
+
debug?: boolean;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export class Hypernum {
|
|
40
|
+
private readonly config: Required<HypernumConfig>;
|
|
41
|
+
private readonly structures: {
|
|
42
|
+
arrays: Map<string, BigArray<bigint>>;
|
|
43
|
+
trees: Map<string, NumberTree>;
|
|
44
|
+
heaps: Map<string, MinHeap<bigint> | MaxHeap<bigint>>;
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
constructor(config: HypernumConfig = {}) {
|
|
48
|
+
this.config = {
|
|
49
|
+
precision: config.precision ?? DEFAULT_OPTIONS.precision,
|
|
50
|
+
roundingMode: config.roundingMode ?? DEFAULT_OPTIONS.roundingMode as precision.RoundingMode,
|
|
51
|
+
checkOverflow: config.checkOverflow ?? DEFAULT_OPTIONS.checkOverflow,
|
|
52
|
+
maxSteps: config.maxSteps ?? DEFAULT_OPTIONS.maxSteps,
|
|
53
|
+
debug: config.debug ?? FEATURES.DEBUG_MODE
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
// Validate configuration
|
|
57
|
+
if (this.config.precision < 0 || this.config.precision > MAX_PRECISION) {
|
|
58
|
+
throw new ValidationError(`Precision must be between 0 and ${MAX_PRECISION}`);
|
|
59
|
+
}
|
|
60
|
+
if (this.config.maxSteps < 1 || this.config.maxSteps > MAX_COMPUTATION_STEPS) {
|
|
61
|
+
throw new ValidationError(`Max steps must be between 1 and ${MAX_COMPUTATION_STEPS}`);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// Initialize data structure storage
|
|
65
|
+
this.structures = {
|
|
66
|
+
arrays: new Map(),
|
|
67
|
+
trees: new Map(),
|
|
68
|
+
heaps: new Map()
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// Arithmetic Operations
|
|
73
|
+
public add(a: bigint | string | number, b: bigint | string | number): bigint {
|
|
74
|
+
return arithmetic.add(a, b, this.config);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
public subtract(a: bigint | string | number, b: bigint | string | number): bigint {
|
|
78
|
+
return arithmetic.subtract(a, b, this.config);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
public multiply(a: bigint | string | number, b: bigint | string | number): bigint {
|
|
82
|
+
return arithmetic.multiply(a, b, this.config);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
public divide(a: bigint | string | number, b: bigint | string | number): bigint {
|
|
86
|
+
return arithmetic.divide(a, b, this.config);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
public mod(a: bigint | string | number, b: bigint | string | number): bigint {
|
|
90
|
+
return arithmetic.remainder(a, b, this.config);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// Power Operations
|
|
94
|
+
public power(base: bigint | string | number, exponent: bigint | string | number): bigint {
|
|
95
|
+
return power.power(base, exponent, this.config);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
public sqrt(value: bigint | string | number): bigint {
|
|
99
|
+
return power.sqrt(value, this.config);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
public nthRoot(value: bigint | string | number, n: bigint | string | number): bigint {
|
|
103
|
+
return power.nthRoot(value, n, this.config);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
// Bitwise Operations
|
|
107
|
+
public and(a: bigint | string | number, b: bigint | string | number): bigint {
|
|
108
|
+
return bitwise.and(a, b);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
public or(a: bigint | string | number, b: bigint | string | number): bigint {
|
|
112
|
+
return bitwise.or(a, b);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
public xor(a: bigint | string | number, b: bigint | string | number): bigint {
|
|
116
|
+
return bitwise.xor(a, b);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
public not(value: bigint | string | number): bigint {
|
|
120
|
+
return bitwise.not(value);
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Calculates the greatest common divisor of two numbers
|
|
124
|
+
*/
|
|
125
|
+
public gcd(a: bigint | string | number, b: bigint | string | number): bigint {
|
|
126
|
+
return arithmetic.gcd(a, b);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Calculates the least common multiple of two numbers
|
|
131
|
+
*/
|
|
132
|
+
public lcm(a: bigint | string | number, b: bigint | string | number): bigint {
|
|
133
|
+
return arithmetic.lcm(a, b);
|
|
134
|
+
}
|
|
135
|
+
// Data Structure Management
|
|
136
|
+
public createArray(id: string): BigArray<bigint> {
|
|
137
|
+
if (this.structures.arrays.has(id)) {
|
|
138
|
+
throw new ValidationError(`Array with id '${id}' already exists`);
|
|
139
|
+
}
|
|
140
|
+
const array = new BigArray<bigint>();
|
|
141
|
+
this.structures.arrays.set(id, array);
|
|
142
|
+
return array;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
public getArray(id: string): BigArray<bigint> {
|
|
146
|
+
const array = this.structures.arrays.get(id);
|
|
147
|
+
if (!array) {
|
|
148
|
+
throw new ValidationError(`Array with id '${id}' not found`);
|
|
149
|
+
}
|
|
150
|
+
return array;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
public createTree(id: string): NumberTree {
|
|
154
|
+
if (this.structures.trees.has(id)) {
|
|
155
|
+
throw new ValidationError(`Tree with id '${id}' already exists`);
|
|
156
|
+
}
|
|
157
|
+
const tree = new NumberTree();
|
|
158
|
+
this.structures.trees.set(id, tree);
|
|
159
|
+
return tree;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
public getTree(id: string): NumberTree {
|
|
163
|
+
const tree = this.structures.trees.get(id);
|
|
164
|
+
if (!tree) {
|
|
165
|
+
throw new ValidationError(`Tree with id '${id}' not found`);
|
|
166
|
+
}
|
|
167
|
+
return tree;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
public createHeap(id: string, isMinHeap: boolean = true): MinHeap<bigint> | MaxHeap<bigint> {
|
|
171
|
+
if (this.structures.heaps.has(id)) {
|
|
172
|
+
throw new ValidationError(`Heap with id '${id}' already exists`);
|
|
173
|
+
}
|
|
174
|
+
const heap = isMinHeap ? new MinHeap<bigint>(this.compareValues) : new MaxHeap<bigint>(this.compareValues);
|
|
175
|
+
this.structures.heaps.set(id, heap);
|
|
176
|
+
return heap;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
public getHeap(id: string): MinHeap<bigint> | MaxHeap<bigint> {
|
|
180
|
+
const heap = this.structures.heaps.get(id);
|
|
181
|
+
if (!heap) {
|
|
182
|
+
throw new ValidationError(`Heap with id '${id}' not found`);
|
|
183
|
+
}
|
|
184
|
+
return heap;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
// Special Functions
|
|
188
|
+
public createAckermannStructure(): AckermannStructure {
|
|
189
|
+
return new AckermannStructure();
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
// Formatting and Validation
|
|
193
|
+
public format(value: bigint | string | number, options?: formatting.FormatOptions): string {
|
|
194
|
+
const bigValue = validation.toBigInt(value);
|
|
195
|
+
return formatting.formatBigInt(bigValue, options);
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
public validate(value: unknown): boolean {
|
|
199
|
+
try {
|
|
200
|
+
validation.toBigInt(value);
|
|
201
|
+
return true;
|
|
202
|
+
} catch {
|
|
203
|
+
return false;
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
// Configuration Management
|
|
208
|
+
public updateConfig(newConfig: Partial<HypernumConfig>): void {
|
|
209
|
+
Object.assign(this.config, newConfig);
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
public getConfig(): Readonly<Required<HypernumConfig>> {
|
|
213
|
+
return { ...this.config };
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
// Utility Functions
|
|
217
|
+
private compareValues(a: bigint, b: bigint): -1 | 0 | 1 {
|
|
218
|
+
if (a < b) return -1;
|
|
219
|
+
if (a > b) return 1;
|
|
220
|
+
return 0;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
// Cleanup
|
|
224
|
+
public dispose(): void {
|
|
225
|
+
this.structures.arrays.clear();
|
|
226
|
+
this.structures.trees.clear();
|
|
227
|
+
this.structures.heaps.clear();
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
// Export additional types and utilities
|
|
232
|
+
export {
|
|
233
|
+
HypernumError,
|
|
234
|
+
ValidationError,
|
|
235
|
+
OverflowError,
|
|
236
|
+
precision,
|
|
237
|
+
formatting,
|
|
238
|
+
validation
|
|
239
|
+
};
|
|
240
|
+
|
|
241
|
+
export default Hypernum;
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Hypernum - A TypeScript/JavaScript library for large number operations
|
|
3
|
+
*/
|
|
4
|
+
import { HypernumConfig, mergeConfig, validateConfig } from './core';
|
|
5
|
+
import { Hypernum } from './core/hypernum';
|
|
6
|
+
import { RoundingMode } from './utils/precision';
|
|
7
|
+
|
|
8
|
+
// Version
|
|
9
|
+
import path from 'path';
|
|
10
|
+
import { fileURLToPath } from 'url';
|
|
11
|
+
import fs from 'fs';
|
|
12
|
+
const packagePath = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../package.json');
|
|
13
|
+
const { version: VERSION } = JSON.parse(fs.readFileSync(packagePath, 'utf-8'));
|
|
14
|
+
|
|
15
|
+
// Core exports
|
|
16
|
+
export { Hypernum } from './core/hypernum';
|
|
17
|
+
export * from './core/constants';
|
|
18
|
+
export * from './core/common';
|
|
19
|
+
export * from './core/config';
|
|
20
|
+
|
|
21
|
+
// Re-export errors with explicit names to avoid conflicts
|
|
22
|
+
export {
|
|
23
|
+
HypernumError,
|
|
24
|
+
ComputationLimitError,
|
|
25
|
+
DataStructureError,
|
|
26
|
+
DivisionByZeroError,
|
|
27
|
+
FormatError,
|
|
28
|
+
HeapPropertyError,
|
|
29
|
+
IndexError,
|
|
30
|
+
PrecisionError,
|
|
31
|
+
RomanNumeralError,
|
|
32
|
+
TreeError,
|
|
33
|
+
UnderflowError
|
|
34
|
+
} from './core/errors';
|
|
35
|
+
|
|
36
|
+
// Data structures with explicit imports and exports
|
|
37
|
+
export { AckermannStructure } from './structures/ackermann';
|
|
38
|
+
export { BigArray, type BigArrayOptions } from './structures/big-array';
|
|
39
|
+
export { NumberTree } from './structures/number-tree';
|
|
40
|
+
export { PowerTower } from './structures/power-tower';
|
|
41
|
+
export { MinHeap, MaxHeap, type Comparator } from './storage/';
|
|
42
|
+
|
|
43
|
+
// Operations with explicit exports
|
|
44
|
+
export {
|
|
45
|
+
add,
|
|
46
|
+
subtract,
|
|
47
|
+
multiply,
|
|
48
|
+
divide,
|
|
49
|
+
remainder,
|
|
50
|
+
abs,
|
|
51
|
+
sign,
|
|
52
|
+
gcd,
|
|
53
|
+
lcm
|
|
54
|
+
} from './operations/arithmetic';
|
|
55
|
+
|
|
56
|
+
export {
|
|
57
|
+
and,
|
|
58
|
+
or,
|
|
59
|
+
xor,
|
|
60
|
+
not,
|
|
61
|
+
leftShift,
|
|
62
|
+
rightShift,
|
|
63
|
+
unsignedRightShift,
|
|
64
|
+
rotateLeft,
|
|
65
|
+
rotateRight,
|
|
66
|
+
popCount,
|
|
67
|
+
trailingZeros,
|
|
68
|
+
leadingZeros,
|
|
69
|
+
getBit,
|
|
70
|
+
setBit,
|
|
71
|
+
clearBit,
|
|
72
|
+
toggleBit
|
|
73
|
+
} from './operations/bitwise';
|
|
74
|
+
|
|
75
|
+
export {
|
|
76
|
+
compare,
|
|
77
|
+
equals,
|
|
78
|
+
lessThan,
|
|
79
|
+
lessThanOrEqual,
|
|
80
|
+
greaterThan,
|
|
81
|
+
greaterThanOrEqual,
|
|
82
|
+
between,
|
|
83
|
+
max,
|
|
84
|
+
min,
|
|
85
|
+
clamp,
|
|
86
|
+
allEqual,
|
|
87
|
+
isAscending,
|
|
88
|
+
isDescending,
|
|
89
|
+
createComparator
|
|
90
|
+
} from './operations/comparison';
|
|
91
|
+
|
|
92
|
+
export {
|
|
93
|
+
toBinary,
|
|
94
|
+
toOctal,
|
|
95
|
+
toHexadecimal,
|
|
96
|
+
toBase,
|
|
97
|
+
fromBase,
|
|
98
|
+
toFraction,
|
|
99
|
+
fromFraction,
|
|
100
|
+
fromScientific,
|
|
101
|
+
toScientific,
|
|
102
|
+
fromRoman,
|
|
103
|
+
toRoman
|
|
104
|
+
} from './operations/conversion';
|
|
105
|
+
|
|
106
|
+
export {
|
|
107
|
+
factorial,
|
|
108
|
+
binomial,
|
|
109
|
+
subfactorial,
|
|
110
|
+
risingFactorial,
|
|
111
|
+
fallingFactorial,
|
|
112
|
+
multiFactorial,
|
|
113
|
+
primorial
|
|
114
|
+
} from './operations/factorial';
|
|
115
|
+
|
|
116
|
+
export {
|
|
117
|
+
power,
|
|
118
|
+
sqrt,
|
|
119
|
+
nthRoot,
|
|
120
|
+
tetration,
|
|
121
|
+
superRoot
|
|
122
|
+
} from './operations/power';
|
|
123
|
+
|
|
124
|
+
// Utils with explicit exports
|
|
125
|
+
export {
|
|
126
|
+
toBigInt,
|
|
127
|
+
validateNonNegative,
|
|
128
|
+
validatePositive,
|
|
129
|
+
checkAdditionOverflow,
|
|
130
|
+
checkMultiplicationOverflow,
|
|
131
|
+
checkPowerOverflow
|
|
132
|
+
} from './utils/validation';
|
|
133
|
+
|
|
134
|
+
export {
|
|
135
|
+
formatBigInt,
|
|
136
|
+
parseBigIntString,
|
|
137
|
+
normalizeNumberString
|
|
138
|
+
} from './utils/formatting';
|
|
139
|
+
|
|
140
|
+
export {
|
|
141
|
+
RoundingMode,
|
|
142
|
+
round,
|
|
143
|
+
scaleByPowerOfTen,
|
|
144
|
+
scaledDivision,
|
|
145
|
+
normalizePrecision
|
|
146
|
+
} from './utils/precision';
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Creates a new Hypernum instance with custom configuration
|
|
150
|
+
*/
|
|
151
|
+
export function createHypernum(config?: Partial<HypernumConfig>): Hypernum {
|
|
152
|
+
const mergedConfig = mergeConfig(config || {});
|
|
153
|
+
validateConfig(mergedConfig);
|
|
154
|
+
|
|
155
|
+
const instanceConfig = {
|
|
156
|
+
precision: 'arithmetic' in mergedConfig
|
|
157
|
+
? mergedConfig.arithmetic.defaultPrecision
|
|
158
|
+
: mergedConfig.precision ?? 0,
|
|
159
|
+
roundingMode: 'arithmetic' in mergedConfig
|
|
160
|
+
? mergedConfig.arithmetic.defaultRoundingMode
|
|
161
|
+
: (mergedConfig.roundingMode as RoundingMode) ?? RoundingMode.HALF_EVEN,
|
|
162
|
+
checkOverflow: 'arithmetic' in mergedConfig
|
|
163
|
+
? mergedConfig.arithmetic.checkOverflow
|
|
164
|
+
: mergedConfig.checkOverflow ?? true,
|
|
165
|
+
maxSteps: 'arithmetic' in mergedConfig
|
|
166
|
+
? mergedConfig.arithmetic.maxComputationSteps
|
|
167
|
+
: mergedConfig.maxSteps ?? 1000,
|
|
168
|
+
debug: 'debug' in mergedConfig && typeof mergedConfig.debug === 'object'
|
|
169
|
+
? mergedConfig.debug.verbose
|
|
170
|
+
: false
|
|
171
|
+
};
|
|
172
|
+
|
|
173
|
+
return new Hypernum(instanceConfig);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
// Default instance
|
|
177
|
+
export const defaultHypernum = createHypernum();
|
|
178
|
+
|
|
179
|
+
// Export version
|
|
180
|
+
export { VERSION };
|
|
181
|
+
|
|
182
|
+
// Default export
|
|
183
|
+
export default Hypernum;
|