1 | ;
|
2 |
|
3 | var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
4 |
|
5 | Object.defineProperty(exports, "__esModule", {
|
6 | value: true
|
7 | });
|
8 | exports["default"] = exports.toString = exports.toNumber = exports.inSafeRange = exports.isInt = exports["int"] = void 0;
|
9 |
|
10 | var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
|
11 |
|
12 | var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
|
13 |
|
14 | var _error = require("./error");
|
15 |
|
16 | /**
|
17 | * Copyright (c) 2002-2019 "Neo4j,"
|
18 | * Neo4j Sweden AB [http://neo4j.com]
|
19 | *
|
20 | * This file is part of Neo4j.
|
21 | *
|
22 | * Licensed under the Apache License, Version 2.0 (the "License");
|
23 | * you may not use this file except in compliance with the License.
|
24 | * You may obtain a copy of the License at
|
25 | *
|
26 | * http://www.apache.org/licenses/LICENSE-2.0
|
27 | *
|
28 | * Unless required by applicable law or agreed to in writing, software
|
29 | * distributed under the License is distributed on an "AS IS" BASIS,
|
30 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
31 | * See the License for the specific language governing permissions and
|
32 | * limitations under the License.
|
33 | */
|
34 | // 64-bit Integer library, originally from Long.js by dcodeIO
|
35 | // https://github.com/dcodeIO/Long.js
|
36 | // License Apache 2
|
37 |
|
38 | /**
|
39 | * Constructs a 64 bit two's-complement integer, given its low and high 32 bit values as *signed* integers.
|
40 | * See exported functions for more convenient ways of operating integers.
|
41 | * Use `int()` function to create new integers, `isInt()` to check if given object is integer,
|
42 | * `inSafeRange()` to check if it is safe to convert given value to native number,
|
43 | * `toNumber()` and `toString()` to convert given integer to number or string respectively.
|
44 | * @access public
|
45 | * @exports Integer
|
46 | * @class A Integer class for representing a 64 bit two's-complement integer value.
|
47 | * @param {number} low The low (signed) 32 bits of the long
|
48 | * @param {number} high The high (signed) 32 bits of the long
|
49 | * @constructor
|
50 | */
|
51 | var Integer =
|
52 | /*#__PURE__*/
|
53 | function () {
|
54 | function Integer(low, high) {
|
55 | (0, _classCallCheck2["default"])(this, Integer);
|
56 |
|
57 | /**
|
58 | * The low 32 bits as a signed value.
|
59 | * @type {number}
|
60 | * @expose
|
61 | */
|
62 | this.low = low | 0;
|
63 | /**
|
64 | * The high 32 bits as a signed value.
|
65 | * @type {number}
|
66 | * @expose
|
67 | */
|
68 |
|
69 | this.high = high | 0;
|
70 | } // The internal representation of an Integer is the two given signed, 32-bit values.
|
71 | // We use 32-bit pieces because these are the size of integers on which
|
72 | // JavaScript performs bit-operations. For operations like addition and
|
73 | // multiplication, we split each number into 16 bit pieces, which can easily be
|
74 | // multiplied within JavaScript's floating-point representation without overflow
|
75 | // or change in sign.
|
76 | //
|
77 | // In the algorithms below, we frequently reduce the negative case to the
|
78 | // positive case by negating the input(s) and then post-processing the result.
|
79 | // Note that we must ALWAYS check specially whether those values are MIN_VALUE
|
80 | // (-2^63) because -MIN_VALUE == MIN_VALUE (since 2^63 cannot be represented as
|
81 | // a positive number, it overflows back into a negative). Not handling this
|
82 | // case would often result in infinite recursion.
|
83 | //
|
84 | // Common constant values ZERO, ONE, NEG_ONE, etc. are defined below the from*
|
85 | // methods on which they depend.
|
86 |
|
87 |
|
88 | (0, _createClass2["default"])(Integer, [{
|
89 | key: "inSafeRange",
|
90 | value: function inSafeRange() {
|
91 | return this.greaterThanOrEqual(Integer.MIN_SAFE_VALUE) && this.lessThanOrEqual(Integer.MAX_SAFE_VALUE);
|
92 | }
|
93 | /**
|
94 | * Converts the Integer to an exact javascript Number, assuming it is a 32 bit integer.
|
95 | * @returns {number}
|
96 | * @expose
|
97 | */
|
98 |
|
99 | }, {
|
100 | key: "toInt",
|
101 | value: function toInt() {
|
102 | return this.low;
|
103 | }
|
104 | /**
|
105 | * Converts the Integer to a the nearest floating-point representation of this value (double, 53 bit mantissa).
|
106 | * @returns {number}
|
107 | * @expose
|
108 | */
|
109 |
|
110 | }, {
|
111 | key: "toNumber",
|
112 | value: function toNumber() {
|
113 | return this.high * TWO_PWR_32_DBL + (this.low >>> 0);
|
114 | }
|
115 | /**
|
116 | * Converts the Integer to native number or -Infinity/+Infinity when it does not fit.
|
117 | * @return {number}
|
118 | * @package
|
119 | */
|
120 |
|
121 | }, {
|
122 | key: "toNumberOrInfinity",
|
123 | value: function toNumberOrInfinity() {
|
124 | if (this.lessThan(Integer.MIN_SAFE_VALUE)) {
|
125 | return Number.NEGATIVE_INFINITY;
|
126 | } else if (this.greaterThan(Integer.MAX_SAFE_VALUE)) {
|
127 | return Number.POSITIVE_INFINITY;
|
128 | } else {
|
129 | return this.toNumber();
|
130 | }
|
131 | }
|
132 | /**
|
133 | * Converts the Integer to a string written in the specified radix.
|
134 | * @param {number=} radix Radix (2-36), defaults to 10
|
135 | * @returns {string}
|
136 | * @override
|
137 | * @throws {RangeError} If `radix` is out of range
|
138 | * @expose
|
139 | */
|
140 |
|
141 | }, {
|
142 | key: "toString",
|
143 | value: function toString(radix) {
|
144 | radix = radix || 10;
|
145 |
|
146 | if (radix < 2 || radix > 36) {
|
147 | throw RangeError('radix out of range: ' + radix);
|
148 | }
|
149 |
|
150 | if (this.isZero()) {
|
151 | return '0';
|
152 | }
|
153 |
|
154 | var rem;
|
155 |
|
156 | if (this.isNegative()) {
|
157 | if (this.equals(Integer.MIN_VALUE)) {
|
158 | // We need to change the Integer value before it can be negated, so we remove
|
159 | // the bottom-most digit in this base and then recurse to do the rest.
|
160 | var radixInteger = Integer.fromNumber(radix);
|
161 | var div = this.div(radixInteger);
|
162 | rem = div.multiply(radixInteger).subtract(this);
|
163 | return div.toString(radix) + rem.toInt().toString(radix);
|
164 | } else {
|
165 | return '-' + this.negate().toString(radix);
|
166 | }
|
167 | } // Do several (6) digits each time through the loop, so as to
|
168 | // minimize the calls to the very expensive emulated div.
|
169 |
|
170 |
|
171 | var radixToPower = Integer.fromNumber(Math.pow(radix, 6));
|
172 | rem = this;
|
173 | var result = '';
|
174 |
|
175 | while (true) {
|
176 | var remDiv = rem.div(radixToPower);
|
177 | var intval = rem.subtract(remDiv.multiply(radixToPower)).toInt() >>> 0;
|
178 | var digits = intval.toString(radix);
|
179 | rem = remDiv;
|
180 |
|
181 | if (rem.isZero()) {
|
182 | return digits + result;
|
183 | } else {
|
184 | while (digits.length < 6) {
|
185 | digits = '0' + digits;
|
186 | }
|
187 |
|
188 | result = '' + digits + result;
|
189 | }
|
190 | }
|
191 | }
|
192 | /**
|
193 | * Gets the high 32 bits as a signed integer.
|
194 | * @returns {number} Signed high bits
|
195 | * @expose
|
196 | */
|
197 |
|
198 | }, {
|
199 | key: "getHighBits",
|
200 | value: function getHighBits() {
|
201 | return this.high;
|
202 | }
|
203 | /**
|
204 | * Gets the low 32 bits as a signed integer.
|
205 | * @returns {number} Signed low bits
|
206 | * @expose
|
207 | */
|
208 |
|
209 | }, {
|
210 | key: "getLowBits",
|
211 | value: function getLowBits() {
|
212 | return this.low;
|
213 | }
|
214 | /**
|
215 | * Gets the number of bits needed to represent the absolute value of this Integer.
|
216 | * @returns {number}
|
217 | * @expose
|
218 | */
|
219 |
|
220 | }, {
|
221 | key: "getNumBitsAbs",
|
222 | value: function getNumBitsAbs() {
|
223 | if (this.isNegative()) {
|
224 | return this.equals(Integer.MIN_VALUE) ? 64 : this.negate().getNumBitsAbs();
|
225 | }
|
226 |
|
227 | var val = this.high !== 0 ? this.high : this.low;
|
228 |
|
229 | for (var bit = 31; bit > 0; bit--) {
|
230 | if ((val & 1 << bit) !== 0) {
|
231 | break;
|
232 | }
|
233 | }
|
234 |
|
235 | return this.high !== 0 ? bit + 33 : bit + 1;
|
236 | }
|
237 | /**
|
238 | * Tests if this Integer's value equals zero.
|
239 | * @returns {boolean}
|
240 | * @expose
|
241 | */
|
242 |
|
243 | }, {
|
244 | key: "isZero",
|
245 | value: function isZero() {
|
246 | return this.high === 0 && this.low === 0;
|
247 | }
|
248 | /**
|
249 | * Tests if this Integer's value is negative.
|
250 | * @returns {boolean}
|
251 | * @expose
|
252 | */
|
253 |
|
254 | }, {
|
255 | key: "isNegative",
|
256 | value: function isNegative() {
|
257 | return this.high < 0;
|
258 | }
|
259 | /**
|
260 | * Tests if this Integer's value is positive.
|
261 | * @returns {boolean}
|
262 | * @expose
|
263 | */
|
264 |
|
265 | }, {
|
266 | key: "isPositive",
|
267 | value: function isPositive() {
|
268 | return this.high >= 0;
|
269 | }
|
270 | /**
|
271 | * Tests if this Integer's value is odd.
|
272 | * @returns {boolean}
|
273 | * @expose
|
274 | */
|
275 |
|
276 | }, {
|
277 | key: "isOdd",
|
278 | value: function isOdd() {
|
279 | return (this.low & 1) === 1;
|
280 | }
|
281 | /**
|
282 | * Tests if this Integer's value is even.
|
283 | * @returns {boolean}
|
284 | * @expose
|
285 | */
|
286 |
|
287 | }, {
|
288 | key: "isEven",
|
289 | value: function isEven() {
|
290 | return (this.low & 1) === 0;
|
291 | }
|
292 | /**
|
293 | * Tests if this Integer's value equals the specified's.
|
294 | * @param {!Integer|number|string} other Other value
|
295 | * @returns {boolean}
|
296 | * @expose
|
297 | */
|
298 |
|
299 | }, {
|
300 | key: "equals",
|
301 | value: function equals(other) {
|
302 | if (!Integer.isInteger(other)) {
|
303 | other = Integer.fromValue(other);
|
304 | }
|
305 |
|
306 | return this.high === other.high && this.low === other.low;
|
307 | }
|
308 | /**
|
309 | * Tests if this Integer's value differs from the specified's.
|
310 | * @param {!Integer|number|string} other Other value
|
311 | * @returns {boolean}
|
312 | * @expose
|
313 | */
|
314 |
|
315 | }, {
|
316 | key: "notEquals",
|
317 | value: function notEquals(other) {
|
318 | return !this.equals(
|
319 | /* validates */
|
320 | other);
|
321 | }
|
322 | /**
|
323 | * Tests if this Integer's value is less than the specified's.
|
324 | * @param {!Integer|number|string} other Other value
|
325 | * @returns {boolean}
|
326 | * @expose
|
327 | */
|
328 |
|
329 | }, {
|
330 | key: "lessThan",
|
331 | value: function lessThan(other) {
|
332 | return this.compare(
|
333 | /* validates */
|
334 | other) < 0;
|
335 | }
|
336 | /**
|
337 | * Tests if this Integer's value is less than or equal the specified's.
|
338 | * @param {!Integer|number|string} other Other value
|
339 | * @returns {boolean}
|
340 | * @expose
|
341 | */
|
342 |
|
343 | }, {
|
344 | key: "lessThanOrEqual",
|
345 | value: function lessThanOrEqual(other) {
|
346 | return this.compare(
|
347 | /* validates */
|
348 | other) <= 0;
|
349 | }
|
350 | /**
|
351 | * Tests if this Integer's value is greater than the specified's.
|
352 | * @param {!Integer|number|string} other Other value
|
353 | * @returns {boolean}
|
354 | * @expose
|
355 | */
|
356 |
|
357 | }, {
|
358 | key: "greaterThan",
|
359 | value: function greaterThan(other) {
|
360 | return this.compare(
|
361 | /* validates */
|
362 | other) > 0;
|
363 | }
|
364 | /**
|
365 | * Tests if this Integer's value is greater than or equal the specified's.
|
366 | * @param {!Integer|number|string} other Other value
|
367 | * @returns {boolean}
|
368 | * @expose
|
369 | */
|
370 |
|
371 | }, {
|
372 | key: "greaterThanOrEqual",
|
373 | value: function greaterThanOrEqual(other) {
|
374 | return this.compare(
|
375 | /* validates */
|
376 | other) >= 0;
|
377 | }
|
378 | /**
|
379 | * Compares this Integer's value with the specified's.
|
380 | * @param {!Integer|number|string} other Other value
|
381 | * @returns {number} 0 if they are the same, 1 if the this is greater and -1
|
382 | * if the given one is greater
|
383 | * @expose
|
384 | */
|
385 |
|
386 | }, {
|
387 | key: "compare",
|
388 | value: function compare(other) {
|
389 | if (!Integer.isInteger(other)) {
|
390 | other = Integer.fromValue(other);
|
391 | }
|
392 |
|
393 | if (this.equals(other)) {
|
394 | return 0;
|
395 | }
|
396 |
|
397 | var thisNeg = this.isNegative();
|
398 | var otherNeg = other.isNegative();
|
399 |
|
400 | if (thisNeg && !otherNeg) {
|
401 | return -1;
|
402 | }
|
403 |
|
404 | if (!thisNeg && otherNeg) {
|
405 | return 1;
|
406 | } // At this point the sign bits are the same
|
407 |
|
408 |
|
409 | return this.subtract(other).isNegative() ? -1 : 1;
|
410 | }
|
411 | /**
|
412 | * Negates this Integer's value.
|
413 | * @returns {!Integer} Negated Integer
|
414 | * @expose
|
415 | */
|
416 |
|
417 | }, {
|
418 | key: "negate",
|
419 | value: function negate() {
|
420 | if (this.equals(Integer.MIN_VALUE)) {
|
421 | return Integer.MIN_VALUE;
|
422 | }
|
423 |
|
424 | return this.not().add(Integer.ONE);
|
425 | }
|
426 | /**
|
427 | * Returns the sum of this and the specified Integer.
|
428 | * @param {!Integer|number|string} addend Addend
|
429 | * @returns {!Integer} Sum
|
430 | * @expose
|
431 | */
|
432 |
|
433 | }, {
|
434 | key: "add",
|
435 | value: function add(addend) {
|
436 | if (!Integer.isInteger(addend)) {
|
437 | addend = Integer.fromValue(addend);
|
438 | } // Divide each number into 4 chunks of 16 bits, and then sum the chunks.
|
439 |
|
440 |
|
441 | var a48 = this.high >>> 16;
|
442 | var a32 = this.high & 0xffff;
|
443 | var a16 = this.low >>> 16;
|
444 | var a00 = this.low & 0xffff;
|
445 | var b48 = addend.high >>> 16;
|
446 | var b32 = addend.high & 0xffff;
|
447 | var b16 = addend.low >>> 16;
|
448 | var b00 = addend.low & 0xffff;
|
449 | var c48 = 0;
|
450 | var c32 = 0;
|
451 | var c16 = 0;
|
452 | var c00 = 0;
|
453 | c00 += a00 + b00;
|
454 | c16 += c00 >>> 16;
|
455 | c00 &= 0xffff;
|
456 | c16 += a16 + b16;
|
457 | c32 += c16 >>> 16;
|
458 | c16 &= 0xffff;
|
459 | c32 += a32 + b32;
|
460 | c48 += c32 >>> 16;
|
461 | c32 &= 0xffff;
|
462 | c48 += a48 + b48;
|
463 | c48 &= 0xffff;
|
464 | return Integer.fromBits(c16 << 16 | c00, c48 << 16 | c32);
|
465 | }
|
466 | /**
|
467 | * Returns the difference of this and the specified Integer.
|
468 | * @param {!Integer|number|string} subtrahend Subtrahend
|
469 | * @returns {!Integer} Difference
|
470 | * @expose
|
471 | */
|
472 |
|
473 | }, {
|
474 | key: "subtract",
|
475 | value: function subtract(subtrahend) {
|
476 | if (!Integer.isInteger(subtrahend)) {
|
477 | subtrahend = Integer.fromValue(subtrahend);
|
478 | }
|
479 |
|
480 | return this.add(subtrahend.negate());
|
481 | }
|
482 | /**
|
483 | * Returns the product of this and the specified Integer.
|
484 | * @param {!Integer|number|string} multiplier Multiplier
|
485 | * @returns {!Integer} Product
|
486 | * @expose
|
487 | */
|
488 |
|
489 | }, {
|
490 | key: "multiply",
|
491 | value: function multiply(multiplier) {
|
492 | if (this.isZero()) {
|
493 | return Integer.ZERO;
|
494 | }
|
495 |
|
496 | if (!Integer.isInteger(multiplier)) {
|
497 | multiplier = Integer.fromValue(multiplier);
|
498 | }
|
499 |
|
500 | if (multiplier.isZero()) {
|
501 | return Integer.ZERO;
|
502 | }
|
503 |
|
504 | if (this.equals(Integer.MIN_VALUE)) {
|
505 | return multiplier.isOdd() ? Integer.MIN_VALUE : Integer.ZERO;
|
506 | }
|
507 |
|
508 | if (multiplier.equals(Integer.MIN_VALUE)) {
|
509 | return this.isOdd() ? Integer.MIN_VALUE : Integer.ZERO;
|
510 | }
|
511 |
|
512 | if (this.isNegative()) {
|
513 | if (multiplier.isNegative()) {
|
514 | return this.negate().multiply(multiplier.negate());
|
515 | } else {
|
516 | return this.negate().multiply(multiplier).negate();
|
517 | }
|
518 | } else if (multiplier.isNegative()) {
|
519 | return this.multiply(multiplier.negate()).negate();
|
520 | } // If both longs are small, use float multiplication
|
521 |
|
522 |
|
523 | if (this.lessThan(TWO_PWR_24) && multiplier.lessThan(TWO_PWR_24)) {
|
524 | return Integer.fromNumber(this.toNumber() * multiplier.toNumber());
|
525 | } // Divide each long into 4 chunks of 16 bits, and then add up 4x4 products.
|
526 | // We can skip products that would overflow.
|
527 |
|
528 |
|
529 | var a48 = this.high >>> 16;
|
530 | var a32 = this.high & 0xffff;
|
531 | var a16 = this.low >>> 16;
|
532 | var a00 = this.low & 0xffff;
|
533 | var b48 = multiplier.high >>> 16;
|
534 | var b32 = multiplier.high & 0xffff;
|
535 | var b16 = multiplier.low >>> 16;
|
536 | var b00 = multiplier.low & 0xffff;
|
537 | var c48 = 0;
|
538 | var c32 = 0;
|
539 | var c16 = 0;
|
540 | var c00 = 0;
|
541 | c00 += a00 * b00;
|
542 | c16 += c00 >>> 16;
|
543 | c00 &= 0xffff;
|
544 | c16 += a16 * b00;
|
545 | c32 += c16 >>> 16;
|
546 | c16 &= 0xffff;
|
547 | c16 += a00 * b16;
|
548 | c32 += c16 >>> 16;
|
549 | c16 &= 0xffff;
|
550 | c32 += a32 * b00;
|
551 | c48 += c32 >>> 16;
|
552 | c32 &= 0xffff;
|
553 | c32 += a16 * b16;
|
554 | c48 += c32 >>> 16;
|
555 | c32 &= 0xffff;
|
556 | c32 += a00 * b32;
|
557 | c48 += c32 >>> 16;
|
558 | c32 &= 0xffff;
|
559 | c48 += a48 * b00 + a32 * b16 + a16 * b32 + a00 * b48;
|
560 | c48 &= 0xffff;
|
561 | return Integer.fromBits(c16 << 16 | c00, c48 << 16 | c32);
|
562 | }
|
563 | /**
|
564 | * Returns this Integer divided by the specified.
|
565 | * @param {!Integer|number|string} divisor Divisor
|
566 | * @returns {!Integer} Quotient
|
567 | * @expose
|
568 | */
|
569 |
|
570 | }, {
|
571 | key: "div",
|
572 | value: function div(divisor) {
|
573 | if (!Integer.isInteger(divisor)) {
|
574 | divisor = Integer.fromValue(divisor);
|
575 | }
|
576 |
|
577 | if (divisor.isZero()) {
|
578 | throw (0, _error.newError)('division by zero');
|
579 | }
|
580 |
|
581 | if (this.isZero()) {
|
582 | return Integer.ZERO;
|
583 | }
|
584 |
|
585 | var approx, rem, res;
|
586 |
|
587 | if (this.equals(Integer.MIN_VALUE)) {
|
588 | if (divisor.equals(Integer.ONE) || divisor.equals(Integer.NEG_ONE)) {
|
589 | return Integer.MIN_VALUE;
|
590 | }
|
591 |
|
592 | if (divisor.equals(Integer.MIN_VALUE)) {
|
593 | return Integer.ONE;
|
594 | } else {
|
595 | // At this point, we have |other| >= 2, so |this/other| < |MIN_VALUE|.
|
596 | var halfThis = this.shiftRight(1);
|
597 | approx = halfThis.div(divisor).shiftLeft(1);
|
598 |
|
599 | if (approx.equals(Integer.ZERO)) {
|
600 | return divisor.isNegative() ? Integer.ONE : Integer.NEG_ONE;
|
601 | } else {
|
602 | rem = this.subtract(divisor.multiply(approx));
|
603 | res = approx.add(rem.div(divisor));
|
604 | return res;
|
605 | }
|
606 | }
|
607 | } else if (divisor.equals(Integer.MIN_VALUE)) {
|
608 | return Integer.ZERO;
|
609 | }
|
610 |
|
611 | if (this.isNegative()) {
|
612 | if (divisor.isNegative()) {
|
613 | return this.negate().div(divisor.negate());
|
614 | }
|
615 |
|
616 | return this.negate().div(divisor).negate();
|
617 | } else if (divisor.isNegative()) {
|
618 | return this.div(divisor.negate()).negate();
|
619 | } // Repeat the following until the remainder is less than other: find a
|
620 | // floating-point that approximates remainder / other *from below*, add this
|
621 | // into the result, and subtract it from the remainder. It is critical that
|
622 | // the approximate value is less than or equal to the real value so that the
|
623 | // remainder never becomes negative.
|
624 |
|
625 |
|
626 | res = Integer.ZERO;
|
627 | rem = this;
|
628 |
|
629 | while (rem.greaterThanOrEqual(divisor)) {
|
630 | // Approximate the result of division. This may be a little greater or
|
631 | // smaller than the actual value.
|
632 | approx = Math.max(1, Math.floor(rem.toNumber() / divisor.toNumber())); // We will tweak the approximate result by changing it in the 48-th digit or
|
633 | // the smallest non-fractional digit, whichever is larger.
|
634 |
|
635 | var log2 = Math.ceil(Math.log(approx) / Math.LN2);
|
636 | var delta = log2 <= 48 ? 1 : Math.pow(2, log2 - 48); // Decrease the approximation until it is smaller than the remainder. Note
|
637 | // that if it is too large, the product overflows and is negative.
|
638 |
|
639 | var approxRes = Integer.fromNumber(approx);
|
640 | var approxRem = approxRes.multiply(divisor);
|
641 |
|
642 | while (approxRem.isNegative() || approxRem.greaterThan(rem)) {
|
643 | approx -= delta;
|
644 | approxRes = Integer.fromNumber(approx);
|
645 | approxRem = approxRes.multiply(divisor);
|
646 | } // We know the answer can't be zero... and actually, zero would cause
|
647 | // infinite recursion since we would make no progress.
|
648 |
|
649 |
|
650 | if (approxRes.isZero()) {
|
651 | approxRes = Integer.ONE;
|
652 | }
|
653 |
|
654 | res = res.add(approxRes);
|
655 | rem = rem.subtract(approxRem);
|
656 | }
|
657 |
|
658 | return res;
|
659 | }
|
660 | /**
|
661 | * Returns this Integer modulo the specified.
|
662 | * @param {!Integer|number|string} divisor Divisor
|
663 | * @returns {!Integer} Remainder
|
664 | * @expose
|
665 | */
|
666 |
|
667 | }, {
|
668 | key: "modulo",
|
669 | value: function modulo(divisor) {
|
670 | if (!Integer.isInteger(divisor)) {
|
671 | divisor = Integer.fromValue(divisor);
|
672 | }
|
673 |
|
674 | return this.subtract(this.div(divisor).multiply(divisor));
|
675 | }
|
676 | /**
|
677 | * Returns the bitwise NOT of this Integer.
|
678 | * @returns {!Integer}
|
679 | * @expose
|
680 | */
|
681 |
|
682 | }, {
|
683 | key: "not",
|
684 | value: function not() {
|
685 | return Integer.fromBits(~this.low, ~this.high);
|
686 | }
|
687 | /**
|
688 | * Returns the bitwise AND of this Integer and the specified.
|
689 | * @param {!Integer|number|string} other Other Integer
|
690 | * @returns {!Integer}
|
691 | * @expose
|
692 | */
|
693 |
|
694 | }, {
|
695 | key: "and",
|
696 | value: function and(other) {
|
697 | if (!Integer.isInteger(other)) {
|
698 | other = Integer.fromValue(other);
|
699 | }
|
700 |
|
701 | return Integer.fromBits(this.low & other.low, this.high & other.high);
|
702 | }
|
703 | /**
|
704 | * Returns the bitwise OR of this Integer and the specified.
|
705 | * @param {!Integer|number|string} other Other Integer
|
706 | * @returns {!Integer}
|
707 | * @expose
|
708 | */
|
709 |
|
710 | }, {
|
711 | key: "or",
|
712 | value: function or(other) {
|
713 | if (!Integer.isInteger(other)) {
|
714 | other = Integer.fromValue(other);
|
715 | }
|
716 |
|
717 | return Integer.fromBits(this.low | other.low, this.high | other.high);
|
718 | }
|
719 | /**
|
720 | * Returns the bitwise XOR of this Integer and the given one.
|
721 | * @param {!Integer|number|string} other Other Integer
|
722 | * @returns {!Integer}
|
723 | * @expose
|
724 | */
|
725 |
|
726 | }, {
|
727 | key: "xor",
|
728 | value: function xor(other) {
|
729 | if (!Integer.isInteger(other)) {
|
730 | other = Integer.fromValue(other);
|
731 | }
|
732 |
|
733 | return Integer.fromBits(this.low ^ other.low, this.high ^ other.high);
|
734 | }
|
735 | /**
|
736 | * Returns this Integer with bits shifted to the left by the given amount.
|
737 | * @param {number|!Integer} numBits Number of bits
|
738 | * @returns {!Integer} Shifted Integer
|
739 | * @expose
|
740 | */
|
741 |
|
742 | }, {
|
743 | key: "shiftLeft",
|
744 | value: function shiftLeft(numBits) {
|
745 | if (Integer.isInteger(numBits)) {
|
746 | numBits = numBits.toInt();
|
747 | }
|
748 |
|
749 | if ((numBits &= 63) === 0) {
|
750 | return this;
|
751 | } else if (numBits < 32) {
|
752 | return Integer.fromBits(this.low << numBits, this.high << numBits | this.low >>> 32 - numBits);
|
753 | } else {
|
754 | return Integer.fromBits(0, this.low << numBits - 32);
|
755 | }
|
756 | }
|
757 | /**
|
758 | * Returns this Integer with bits arithmetically shifted to the right by the given amount.
|
759 | * @param {number|!Integer} numBits Number of bits
|
760 | * @returns {!Integer} Shifted Integer
|
761 | * @expose
|
762 | */
|
763 |
|
764 | }, {
|
765 | key: "shiftRight",
|
766 | value: function shiftRight(numBits) {
|
767 | if (Integer.isInteger(numBits)) {
|
768 | numBits = numBits.toInt();
|
769 | }
|
770 |
|
771 | if ((numBits &= 63) === 0) {
|
772 | return this;
|
773 | } else if (numBits < 32) {
|
774 | return Integer.fromBits(this.low >>> numBits | this.high << 32 - numBits, this.high >> numBits);
|
775 | } else {
|
776 | return Integer.fromBits(this.high >> numBits - 32, this.high >= 0 ? 0 : -1);
|
777 | }
|
778 | }
|
779 | }]);
|
780 | return Integer;
|
781 | }();
|
782 | /**
|
783 | * An indicator used to reliably determine if an object is a Integer or not.
|
784 | * @type {boolean}
|
785 | * @const
|
786 | * @expose
|
787 | * @private
|
788 | */
|
789 |
|
790 |
|
791 | Integer.__isInteger__ = true;
|
792 | Object.defineProperty(Integer.prototype, '__isInteger__', {
|
793 | value: true,
|
794 | enumerable: false,
|
795 | configurable: false
|
796 | });
|
797 | /**
|
798 | * Tests if the specified object is a Integer.
|
799 | * @access private
|
800 | * @param {*} obj Object
|
801 | * @returns {boolean}
|
802 | * @expose
|
803 | */
|
804 |
|
805 | Integer.isInteger = function (obj) {
|
806 | return (obj && obj['__isInteger__']) === true;
|
807 | };
|
808 | /**
|
809 | * A cache of the Integer representations of small integer values.
|
810 | * @type {!Object}
|
811 | * @inner
|
812 | * @private
|
813 | */
|
814 |
|
815 |
|
816 | var INT_CACHE = {};
|
817 | /**
|
818 | * Returns a Integer representing the given 32 bit integer value.
|
819 | * @access private
|
820 | * @param {number} value The 32 bit integer in question
|
821 | * @returns {!Integer} The corresponding Integer value
|
822 | * @expose
|
823 | */
|
824 |
|
825 | Integer.fromInt = function (value) {
|
826 | var obj, cachedObj;
|
827 | value = value | 0;
|
828 |
|
829 | if (value >= -128 && value < 128) {
|
830 | cachedObj = INT_CACHE[value];
|
831 |
|
832 | if (cachedObj) {
|
833 | return cachedObj;
|
834 | }
|
835 | }
|
836 |
|
837 | obj = new Integer(value, value < 0 ? -1 : 0, false);
|
838 |
|
839 | if (value >= -128 && value < 128) {
|
840 | INT_CACHE[value] = obj;
|
841 | }
|
842 |
|
843 | return obj;
|
844 | };
|
845 | /**
|
846 | * Returns a Integer representing the given value, provided that it is a finite number. Otherwise, zero is returned.
|
847 | * @access private
|
848 | * @param {number} value The number in question
|
849 | * @returns {!Integer} The corresponding Integer value
|
850 | * @expose
|
851 | */
|
852 |
|
853 |
|
854 | Integer.fromNumber = function (value) {
|
855 | if (isNaN(value) || !isFinite(value)) {
|
856 | return Integer.ZERO;
|
857 | }
|
858 |
|
859 | if (value <= -TWO_PWR_63_DBL) {
|
860 | return Integer.MIN_VALUE;
|
861 | }
|
862 |
|
863 | if (value + 1 >= TWO_PWR_63_DBL) {
|
864 | return Integer.MAX_VALUE;
|
865 | }
|
866 |
|
867 | if (value < 0) {
|
868 | return Integer.fromNumber(-value).negate();
|
869 | }
|
870 |
|
871 | return new Integer(value % TWO_PWR_32_DBL | 0, value / TWO_PWR_32_DBL | 0);
|
872 | };
|
873 | /**
|
874 | * Returns a Integer representing the 64 bit integer that comes by concatenating the given low and high bits. Each is
|
875 | * assumed to use 32 bits.
|
876 | * @access private
|
877 | * @param {number} lowBits The low 32 bits
|
878 | * @param {number} highBits The high 32 bits
|
879 | * @returns {!Integer} The corresponding Integer value
|
880 | * @expose
|
881 | */
|
882 |
|
883 |
|
884 | Integer.fromBits = function (lowBits, highBits) {
|
885 | return new Integer(lowBits, highBits);
|
886 | };
|
887 | /**
|
888 | * Returns a Integer representation of the given string, written using the specified radix.
|
889 | * @access private
|
890 | * @param {string} str The textual representation of the Integer
|
891 | * @param {number=} radix The radix in which the text is written (2-36), defaults to 10
|
892 | * @returns {!Integer} The corresponding Integer value
|
893 | * @expose
|
894 | */
|
895 |
|
896 |
|
897 | Integer.fromString = function (str, radix) {
|
898 | if (str.length === 0) {
|
899 | throw (0, _error.newError)('number format error: empty string');
|
900 | }
|
901 |
|
902 | if (str === 'NaN' || str === 'Infinity' || str === '+Infinity' || str === '-Infinity') {
|
903 | return Integer.ZERO;
|
904 | }
|
905 |
|
906 | radix = radix || 10;
|
907 |
|
908 | if (radix < 2 || radix > 36) {
|
909 | throw (0, _error.newError)('radix out of range: ' + radix);
|
910 | }
|
911 |
|
912 | var p;
|
913 |
|
914 | if ((p = str.indexOf('-')) > 0) {
|
915 | throw (0, _error.newError)('number format error: interior "-" character: ' + str);
|
916 | } else if (p === 0) {
|
917 | return Integer.fromString(str.substring(1), radix).negate();
|
918 | } // Do several (8) digits each time through the loop, so as to
|
919 | // minimize the calls to the very expensive emulated div.
|
920 |
|
921 |
|
922 | var radixToPower = Integer.fromNumber(Math.pow(radix, 8));
|
923 | var result = Integer.ZERO;
|
924 |
|
925 | for (var i = 0; i < str.length; i += 8) {
|
926 | var size = Math.min(8, str.length - i);
|
927 | var value = parseInt(str.substring(i, i + size), radix);
|
928 |
|
929 | if (size < 8) {
|
930 | var power = Integer.fromNumber(Math.pow(radix, size));
|
931 | result = result.multiply(power).add(Integer.fromNumber(value));
|
932 | } else {
|
933 | result = result.multiply(radixToPower);
|
934 | result = result.add(Integer.fromNumber(value));
|
935 | }
|
936 | }
|
937 |
|
938 | return result;
|
939 | };
|
940 | /**
|
941 | * Converts the specified value to a Integer.
|
942 | * @access private
|
943 | * @param {!Integer|number|string|!{low: number, high: number}} val Value
|
944 | * @returns {!Integer}
|
945 | * @expose
|
946 | */
|
947 |
|
948 |
|
949 | Integer.fromValue = function (val) {
|
950 | if (val
|
951 | /* is compatible */
|
952 | instanceof Integer) {
|
953 | return val;
|
954 | }
|
955 |
|
956 | if (typeof val === 'number') {
|
957 | return Integer.fromNumber(val);
|
958 | }
|
959 |
|
960 | if (typeof val === 'string') {
|
961 | return Integer.fromString(val);
|
962 | } // Throws for non-objects, converts non-instanceof Integer:
|
963 |
|
964 |
|
965 | return new Integer(val.low, val.high);
|
966 | };
|
967 | /**
|
968 | * Converts the specified value to a number.
|
969 | * @access private
|
970 | * @param {!Integer|number|string|!{low: number, high: number}} val Value
|
971 | * @returns {number}
|
972 | * @expose
|
973 | */
|
974 |
|
975 |
|
976 | Integer.toNumber = function (val) {
|
977 | return Integer.fromValue(val).toNumber();
|
978 | };
|
979 | /**
|
980 | * Converts the specified value to a string.
|
981 | * @access private
|
982 | * @param {!Integer|number|string|!{low: number, high: number}} val Value
|
983 | * @param {number} radix optional radix for string conversion, defaults to 10
|
984 | * @returns {string}
|
985 | * @expose
|
986 | */
|
987 |
|
988 |
|
989 | Integer.toString = function (val, radix) {
|
990 | return Integer.fromValue(val).toString(radix);
|
991 | };
|
992 | /**
|
993 | * Checks if the given value is in the safe range in order to be converted to a native number
|
994 | * @access private
|
995 | * @param {!Integer|number|string|!{low: number, high: number}} val Value
|
996 | * @param {number} radix optional radix for string conversion, defaults to 10
|
997 | * @returns {boolean}
|
998 | * @expose
|
999 | */
|
1000 |
|
1001 |
|
1002 | Integer.inSafeRange = function (val) {
|
1003 | return Integer.fromValue(val).inSafeRange();
|
1004 | };
|
1005 | /**
|
1006 | * @type {number}
|
1007 | * @const
|
1008 | * @inner
|
1009 | * @private
|
1010 | */
|
1011 |
|
1012 |
|
1013 | var TWO_PWR_16_DBL = 1 << 16;
|
1014 | /**
|
1015 | * @type {number}
|
1016 | * @const
|
1017 | * @inner
|
1018 | * @private
|
1019 | */
|
1020 |
|
1021 | var TWO_PWR_24_DBL = 1 << 24;
|
1022 | /**
|
1023 | * @type {number}
|
1024 | * @const
|
1025 | * @inner
|
1026 | * @private
|
1027 | */
|
1028 |
|
1029 | var TWO_PWR_32_DBL = TWO_PWR_16_DBL * TWO_PWR_16_DBL;
|
1030 | /**
|
1031 | * @type {number}
|
1032 | * @const
|
1033 | * @inner
|
1034 | * @private
|
1035 | */
|
1036 |
|
1037 | var TWO_PWR_64_DBL = TWO_PWR_32_DBL * TWO_PWR_32_DBL;
|
1038 | /**
|
1039 | * @type {number}
|
1040 | * @const
|
1041 | * @inner
|
1042 | * @private
|
1043 | */
|
1044 |
|
1045 | var TWO_PWR_63_DBL = TWO_PWR_64_DBL / 2;
|
1046 | /**
|
1047 | * @type {!Integer}
|
1048 | * @const
|
1049 | * @inner
|
1050 | * @private
|
1051 | */
|
1052 |
|
1053 | var TWO_PWR_24 = Integer.fromInt(TWO_PWR_24_DBL);
|
1054 | /**
|
1055 | * Signed zero.
|
1056 | * @type {!Integer}
|
1057 | * @expose
|
1058 | */
|
1059 |
|
1060 | Integer.ZERO = Integer.fromInt(0);
|
1061 | /**
|
1062 | * Signed one.
|
1063 | * @type {!Integer}
|
1064 | * @expose
|
1065 | */
|
1066 |
|
1067 | Integer.ONE = Integer.fromInt(1);
|
1068 | /**
|
1069 | * Signed negative one.
|
1070 | * @type {!Integer}
|
1071 | * @expose
|
1072 | */
|
1073 |
|
1074 | Integer.NEG_ONE = Integer.fromInt(-1);
|
1075 | /**
|
1076 | * Maximum signed value.
|
1077 | * @type {!Integer}
|
1078 | * @expose
|
1079 | */
|
1080 |
|
1081 | Integer.MAX_VALUE = Integer.fromBits(0xffffffff | 0, 0x7fffffff | 0, false);
|
1082 | /**
|
1083 | * Minimum signed value.
|
1084 | * @type {!Integer}
|
1085 | * @expose
|
1086 | */
|
1087 |
|
1088 | Integer.MIN_VALUE = Integer.fromBits(0, 0x80000000 | 0, false);
|
1089 | /**
|
1090 | * Minimum safe value.
|
1091 | * @type {!Integer}
|
1092 | * @expose
|
1093 | */
|
1094 |
|
1095 | Integer.MIN_SAFE_VALUE = Integer.fromBits(0x1 | 0, 0xffffffffffe00000 | 0);
|
1096 | /**
|
1097 | * Maximum safe value.
|
1098 | * @type {!Integer}
|
1099 | * @expose
|
1100 | */
|
1101 |
|
1102 | Integer.MAX_SAFE_VALUE = Integer.fromBits(0xffffffff | 0, 0x1fffff | 0);
|
1103 | /**
|
1104 | * Cast value to Integer type.
|
1105 | * @access public
|
1106 | * @param {Mixed} value - The value to use.
|
1107 | * @return {Integer} - An object of type Integer.
|
1108 | */
|
1109 |
|
1110 | var _int = Integer.fromValue;
|
1111 | /**
|
1112 | * Check if a variable is of Integer type.
|
1113 | * @access public
|
1114 | * @param {Mixed} value - The variable to check.
|
1115 | * @return {Boolean} - Is it of the Integer type?
|
1116 | */
|
1117 |
|
1118 | exports["int"] = _int;
|
1119 | var isInt = Integer.isInteger;
|
1120 | /**
|
1121 | * Check if a variable can be safely converted to a number
|
1122 | * @access public
|
1123 | * @param {Mixed} value - The variable to check
|
1124 | * @return {Boolean} - true if it is safe to call toNumber on variable otherwise false
|
1125 | */
|
1126 |
|
1127 | exports.isInt = isInt;
|
1128 | var inSafeRange = Integer.inSafeRange;
|
1129 | /**
|
1130 | * Converts a variable to a number
|
1131 | * @access public
|
1132 | * @param {Mixed} value - The variable to convert
|
1133 | * @return {number} - the variable as a number
|
1134 | */
|
1135 |
|
1136 | exports.inSafeRange = inSafeRange;
|
1137 | var toNumber = Integer.toNumber;
|
1138 | /**
|
1139 | * Converts the integer to a string representation
|
1140 | * @access public
|
1141 | * @param {Mixed} value - The variable to convert
|
1142 | * @param {number} radix - radix to use in string conversion, defaults to 10
|
1143 | * @return {string} - returns a string representation of the integer
|
1144 | */
|
1145 |
|
1146 | exports.toNumber = toNumber;
|
1147 | var toString = Integer.toString;
|
1148 | exports.toString = toString;
|
1149 | var _default = Integer;
|
1150 | exports["default"] = _default; |
\ | No newline at end of file |