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