-
Notifications
You must be signed in to change notification settings - Fork 0
/
Long.ts
482 lines (411 loc) · 15 KB
/
Long.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
/*
* Copyright (c) Mike Lischke. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*/
import { MurmurHash } from "../../MurmurHash.js";
import { JavaString } from "./String.js";
import { Class, JavaObject } from "./Object.js";
import { Serializable } from "../io/Serializable.js";
import { Comparable } from "./Comparable.js";
import { NumberFormatException } from "./NumberFormatException.js";
import { Throwable } from "./Throwable.js";
import { System } from "./System.js";
import { Integer } from "./Integer.js";
export class Long extends JavaObject implements Serializable, Comparable<Long> {
public static readonly MAX_VALUE = 0x7FFFFFFFFFFFFFFFn;
public static readonly MIN_VALUE = -0x8000000000000000n;
public static readonly SIZE = 64;
// @ts-expect-error, as the constructors are incompatible. Need to investigate.
public static readonly TYPE = Class.fromConstructor(Long);
private value: bigint;
/**
* Constructs a newly allocated Long object that represents the specified int or string value.
*
* @param value A primitive type to wrap in this instance.
*/
public constructor(value: bigint | number | string | JavaString) {
super();
if (value instanceof JavaString) {
this.value = BigInt(`${value}`);
} else {
this.value = BigInt(value);
}
}
/**
* @returns The number of one-bits in the two's complement binary representation of the specified bigint value.
*
* @param i The value to examine.
*/
public static bitCount(i: bigint): number {
i = i - ((i >> 1n) & 0x5555555555555555n);
i = (i & 0x3333333333333333n) + ((i >> 2n) & 0x3333333333333333n);
i = (i + (i >> 4n)) & 0x0f0f0f0f0f0f0f0fn;
i = i + (i >> 8n);
i = i + (i >> 16n);
i = i + (i >> 32n);
return Number(i & 0x7Fn);
}
/**
* Compares two int values numerically.
*
* @param x The first value to compare.
* @param y The second value to compare.
*
* @returns A value < 0 if x is less than y, > 0 if x is larger than y, otherwise 0.
*/
public static compare(x: bigint, y: bigint): number {
return Number(x - y);
}
/**
* Decodes a String into a Long.
*
* @param nm The number as string.
*
* @returns A new Long with the converted value.
*/
public static decode(nm: JavaString): Long {
const n = nm.valueOf().trim().toLowerCase();
if (n.length === 0) {
throw new NumberFormatException();
}
try {
// The function parseInt does not support octal numbers, so we have to handle that case manually.
let sign = "";
let start = 0;
if (n[0] === "+" || n[0] === "-") {
sign = n[0];
++start;
}
let radix = 10;
if (n.length > start + 1) {
if (n[0] === "#") {
++start;
radix = 16;
} else if (n[start] === "0") {
++start;
if (n[start] === "x") {
radix = 16;
++start;
} else {
radix = 8;
}
}
}
return new Long(parseInt(sign + n.substring(start), radix));
} catch (reason) {
throw new NumberFormatException(Throwable.fromError(reason));
}
}
/**
* Determines the long value of the system property with the specified name.
*
* @param nm The name of the system property to read.
* @param val A value to be used if the property wasn't found.
*
* @returns The system property as Integer or the default value as Long.
*/
public static getLong(nm?: JavaString, val?: number): Long | null {
const p = nm && nm.length() > 0 ? System.getProperty(nm) : undefined;
if (!p) {
if (val === undefined) {
return null;
}
return new Long(val);
}
try {
return new Long(p);
} catch (reason) {
return null;
}
}
/**
* @returns a bigint value with at most a single one-bit, in the position of the highest-order ("leftmost") one-bit
* in the specified bigint value.
*
* @param i The value for which the result must be determined.
*/
public static highestOneBit(i: bigint): bigint {
return 1n << BigInt(63 - this.numberOfLeadingZeros(i));
}
/**
* @returns a bigint value with at most a single one-bit, in the position of the lowest-order ("rightmost") one-bit
* in the specified int value.
*
* @param i The value for which the result must be determined.
*/
public static lowestOneBit(i: bigint): bigint {
return i & -i;
}
/**
* @returns the number of zero bits preceding the highest-order ("leftmost") one-bit in the two's complement binary
* representation of the specified int value.
*
* @param i The value for which the result must be determined.
*/
public static numberOfLeadingZeros(i: bigint): number {
const x = Number((i >> 32n) & 0xFFFFFFFFn);
return x === 0 ? 32 + Integer.numberOfLeadingZeros(Number(i))
: Integer.numberOfLeadingZeros(x);
}
/**
* @returns the number of zero bits following the lowest-order ("rightmost") one-bit in the two's complement binary
* representation of the specified int value.
*
* @param i The value for which the result must be determined.
*/
public static numberOfTrailingZeros(i: bigint): number {
i = ~i & (i - 1n);
if (i <= 0) {
return Number(i & 64n);
}
let n = 1n;
if (i > (1n << 32n)) {
n += 16n;
i >>= 16n;
}
if (i > 1 << 16) {
n += 16n;
i >>= 16n;
}
if (i > 1 << 8) {
n += 8n;
i >>= 8n;
}
if (i > 1 << 4) {
n += 4n;
i >>= 4n;
}
if (i > 1 << 2) {
n += 2n;
i >>= 2n;
}
return Number(n + (i >> 1n));
}
/**
* @returns the value obtained by reversing the order of the bits in the two's complement binary representation of
* the specified int value.
*
* @param i The value to reverse.
*/
public static reverse(i: bigint): bigint {
i = ((i & 0x5555555555555555n) << 1n) | ((i >> 1n) & 0x5555555555555555n);
i = ((i & 0x3333333333333333n) << 2n) | ((i >> 2n) & 0x3333333333333333n);
i = ((i & 0x0F0F0F0F0F0F0F0Fn) << 4n) | ((i >> 4n) & 0x0F0F0F0F0F0F0F0Fn);
return this.reverseBytes(i);
}
/**
* @returns the value obtained by reversing the order of the bytes in the two's complement representation of the
* specified int value.
*
* @param i The number to reverse.
*/
public static reverseBytes(i: bigint): bigint {
// Note: bigint does not support an unsigned right shift, so we have first to shift and then mask out
// any sign bits on the left.
i = ((i >> 32n) & 0xFFFFFFFFn) | ((i & 0xFFFFFFFFn) << 32n);
i = ((i >> 16n) & 0x0000FFFF0000FFFFn) | ((i & 0x0000FFFF0000FFFFn) << 16n);
i = ((i >> 8n) & 0x00FF00FF00FF00FFn) | ((i & 0x00FF00FF00FF00FFn) << 8n);
return i;
}
/**
* @returns the value obtained by rotating the two's complement binary representation of the specified bigint value
* left by the specified number of bits.
*
* @param i The number with the bits to rotate.
* @param distance Determines how far to rotate.
*/
public static rotateLeft(i: bigint, distance: number): bigint {
if (distance < 0) {
return this.rotateRight(i, -distance);
}
const high = (i >> 32n) & 0xFFFFFFFFn;
let low = i & 0xFFFFFFFFn;
let bits = BigInt(distance & 0x3F); // At most 63 bits to shift.
if (bits === 32n) {
return (low << 32n) | high;
}
let newHigh;
if (bits > 32) {
bits -= 32n;
newHigh = ((low << bits) & 0xFFFFFFFFn) | ((high >> (32n - bits)) & 0xFFFFFFFFn);
low = (high << bits) | ((low >> (32n - bits)) & 0xFFFFFFFFn);
} else {
newHigh = ((high << bits) & 0xFFFFFFFFn) | ((low >> (32n - bits)) & 0xFFFFFFFFn);
low = (low << bits) | ((high >> (32n - bits)) & 0xFFFFFFFFn);
}
return BigInt((newHigh << 32n) | low);
}
/**
* @returns the value obtained by rotating the two's complement binary representation of the specified bigint value
* right by the specified number of bits.
*
* @param i The number with the bits to rotate.
* @param distance Determines how far to rotate.
*/
public static rotateRight(i: bigint, distance: number): bigint {
if (distance < 0) {
return this.rotateLeft(i, -distance);
}
const high = (i >> 32n) & 0xFFFFFFFFn;
let low = i & 0xFFFFFFFFn;
let bits = BigInt(distance & 0x3F); // At most 63 bits to shift.
if (bits === 32n) {
return (low << 32n) | high;
}
let newHigh;
if (bits > 32) {
bits -= 32n;
newHigh = ((high << (32n - bits)) & 0xFFFFFFFFn) | ((low >> bits) & 0xFFFFFFFFn);
low = (low << (32n - bits)) | ((high >> bits) & 0xFFFFFFFFn);
} else {
newHigh = ((low << (32n - bits)) & 0xFFFFFFFFn) | ((high >> bits) & 0xFFFFFFFFn);
low = (high << (32n - bits)) | ((low >> bits) & 0xFFFFFFFFn);
}
return BigInt((newHigh << 32n) | low);
}
/**
* @returns the signum function of the specified int value.
*
* @param i The value from which to get the signum.
*/
public static signum(i: bigint): number {
return i < 0 ? -1 : (i > 0) ? 1 : 0;
}
/**
* @returns a string representation of the integer argument as an unsigned integer in base 2.
*
* @param i The number to convert.
*/
public static toBinaryString(i: bigint): JavaString {
return new JavaString(i.toString(2));
}
/**
* @returns a string representation of the integer argument as an unsigned integer in base 16.
*
* @param i The number to convert.
*/
public static toHexString(i: bigint): JavaString {
return new JavaString(i.toString(16));
}
/**
* @returns a string representation of the integer argument as an unsigned integer in base 8.
*
* @param i The number to convert.
*/
public static toOctalString(i: bigint): JavaString {
return new JavaString(i.toString(8));
}
/**
* @returns a string representation of the first argument in the radix specified by the second argument.
*
* @param i The number to convert.
* @param radix The radix of the result string.
*/
public static override toString(i: bigint, radix?: number): JavaString {
return new JavaString(i.toString(radix));
}
/**
* Returns an Integer object holding the value given or extracted from the specified String when parsed with the
* radix given by the second argument.
*/
public static override valueOf(i: bigint): Long;
public static override valueOf(s: string | JavaString, radix?: number): Long;
public static override valueOf(value: bigint | string | JavaString, radix?: number): Long {
if (typeof value === "bigint") {
return new Long(value);
}
return this.parseLong(value, radix);
}
/**
* Parses the string argument as a signed decimal long.
* Note: under the hood this method parses the string as number, because BigInt (as used for the `long` Java type)
* does not support radixes other than 10. This limits the possible values here.
*
* @param s The string to parse.
* @param radix A radix for the string.
*
* @returns a new Long with the number value from the string.
*/
public static parseLong(s: string | JavaString, radix = 10): Long {
const value = parseInt(`${s}`, radix);
if (isNaN(value) || value > Long.MAX_VALUE || value < Long.MIN_VALUE) {
throw new NumberFormatException();
}
return new Long(parseInt(`${s}`, radix));
}
/** @returns the value of this Integer as a byte. */
public byteValue(): number {
return Number(BigInt.asIntN(8, this.value));
}
/**
* Compares two Integer objects numerically.
*
* @param anotherLong The value to compare this instance to.
*
* @returns A value < 0 if this instance is smaller than the other one, > 0 if larger, and 0 if they are equal.
*/
public compareTo(anotherLong: Long): number {
return Number(this.value - anotherLong.value);
}
/** @returns the value of this Integer as a double. */
public doubleValue(): number {
return Number(this.value);
}
/**
* Compares this object to the specified object.
*
* @param obj The object to compare this instance to.
*
* @returns True if obj is an instance of Integer and both represent the same numerical value,
* otherwise false.
*/
public override equals(obj?: unknown): boolean {
if (obj instanceof Long) {
return this.value === obj.value;
}
if (typeof obj === "bigint") {
return this.value === obj;
}
if (typeof obj === "number") {
return this.value === BigInt(obj);
}
return false;
}
/** @returns the value of this Integer as a float. */
public floatValue(): number {
return Number(this.value);
}
/** @returns a hash code for this Long. */
public override hashCode(): number {
let hash = MurmurHash.initialize(11);
hash = MurmurHash.update(hash, Number((this.value & 0xFFFFFFFF00000000n) >> 32n));
hash = MurmurHash.update(hash, Number(this.value & 0xFFFFFFFFn));
hash = MurmurHash.finish(hash, 2);
return hash;
}
/** @returns the value of this Integer as an int. */
public intValue(): number {
return Number(BigInt.asIntN(32, this.value));
}
/** @returns the value of this Integer as a long. */
public longValue(): bigint {
return this.value;
}
/** @returns the value of this Integer as a short. */
public shortValue(): number {
return Number(BigInt.asIntN(16, this.value));
}
// Returns a String object representing this Integer's value.
public override toString(): string {
return `${this.value}`;
}
public override valueOf(): bigint {
return this.value;
}
protected [Symbol.toPrimitive](hint: string): bigint | string {
if (hint === "string") {
return this.value.toString();
}
return this.value;
}
}