~ps-jenkins/ubuntu-push/ubuntu-vivid-proposed

« back to all changes in this revision

Viewing changes to docs/example-server/node_modules/mongodb/node_modules/bson/lib/bson/long.js

  • Committer: Roberto Alsina
  • Date: 2014-09-05 14:57:17 UTC
  • mto: (91.179.25 automatic)
  • mto: This revision was merged to the branch mainline in revision 129.
  • Revision ID: roberto.alsina@canonical.com-20140905145717-0ufcsv27w25i1nnu
added example app server

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Licensed under the Apache License, Version 2.0 (the "License");
 
2
// you may not use this file except in compliance with the License.
 
3
// You may obtain a copy of the License at
 
4
//
 
5
//     http://www.apache.org/licenses/LICENSE-2.0
 
6
//
 
7
// Unless required by applicable law or agreed to in writing, software
 
8
// distributed under the License is distributed on an "AS IS" BASIS,
 
9
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
10
// See the License for the specific language governing permissions and
 
11
// limitations under the License.
 
12
//
 
13
// Copyright 2009 Google Inc. All Rights Reserved
 
14
 
 
15
/**
 
16
 * Defines a Long class for representing a 64-bit two's-complement
 
17
 * integer value, which faithfully simulates the behavior of a Java "Long". This
 
18
 * implementation is derived from LongLib in GWT.
 
19
 *
 
20
 * Constructs a 64-bit two's-complement integer, given its low and high 32-bit
 
21
 * values as *signed* integers.  See the from* functions below for more
 
22
 * convenient ways of constructing Longs.
 
23
 *
 
24
 * The internal representation of a Long is the two given signed, 32-bit values.
 
25
 * We use 32-bit pieces because these are the size of integers on which
 
26
 * Javascript performs bit-operations.  For operations like addition and
 
27
 * multiplication, we split each number into 16-bit pieces, which can easily be
 
28
 * multiplied within Javascript's floating-point representation without overflow
 
29
 * or change in sign.
 
30
 *
 
31
 * In the algorithms below, we frequently reduce the negative case to the
 
32
 * positive case by negating the input(s) and then post-processing the result.
 
33
 * Note that we must ALWAYS check specially whether those values are MIN_VALUE
 
34
 * (-2^63) because -MIN_VALUE == MIN_VALUE (since 2^63 cannot be represented as
 
35
 * a positive number, it overflows back into a negative).  Not handling this
 
36
 * case would often result in infinite recursion.
 
37
 *
 
38
 * @class Represents the BSON Long type.
 
39
 * @param {Number} low  the low (signed) 32 bits of the Long.
 
40
 * @param {Number} high the high (signed) 32 bits of the Long.
 
41
 */
 
42
function Long(low, high) {
 
43
  if(!(this instanceof Long)) return new Long(low, high);
 
44
  
 
45
  this._bsontype = 'Long';
 
46
  /**
 
47
   * @type {number}
 
48
   * @api private
 
49
   */
 
50
  this.low_ = low | 0;  // force into 32 signed bits.
 
51
 
 
52
  /**
 
53
   * @type {number}
 
54
   * @api private
 
55
   */
 
56
  this.high_ = high | 0;  // force into 32 signed bits.
 
57
};
 
58
 
 
59
/**
 
60
 * Return the int value.
 
61
 *
 
62
 * @return {Number} the value, assuming it is a 32-bit integer.
 
63
 * @api public
 
64
 */
 
65
Long.prototype.toInt = function() {
 
66
  return this.low_;
 
67
};
 
68
 
 
69
/**
 
70
 * Return the Number value.
 
71
 *
 
72
 * @return {Number} the closest floating-point representation to this value.
 
73
 * @api public
 
74
 */
 
75
Long.prototype.toNumber = function() {
 
76
  return this.high_ * Long.TWO_PWR_32_DBL_ +
 
77
         this.getLowBitsUnsigned();
 
78
};
 
79
 
 
80
/**
 
81
 * Return the JSON value.
 
82
 *
 
83
 * @return {String} the JSON representation.
 
84
 * @api public
 
85
 */
 
86
Long.prototype.toJSON = function() {
 
87
  return this.toString();
 
88
}
 
89
 
 
90
/**
 
91
 * Return the String value.
 
92
 *
 
93
 * @param {Number} [opt_radix] the radix in which the text should be written.
 
94
 * @return {String} the textual representation of this value.
 
95
 * @api public
 
96
 */
 
97
Long.prototype.toString = function(opt_radix) {
 
98
  var radix = opt_radix || 10;
 
99
  if (radix < 2 || 36 < radix) {
 
100
    throw Error('radix out of range: ' + radix);
 
101
  }
 
102
 
 
103
  if (this.isZero()) {
 
104
    return '0';
 
105
  }
 
106
 
 
107
  if (this.isNegative()) {
 
108
    if (this.equals(Long.MIN_VALUE)) {
 
109
      // We need to change the Long value before it can be negated, so we remove
 
110
      // the bottom-most digit in this base and then recurse to do the rest.
 
111
      var radixLong = Long.fromNumber(radix);
 
112
      var div = this.div(radixLong);
 
113
      var rem = div.multiply(radixLong).subtract(this);
 
114
      return div.toString(radix) + rem.toInt().toString(radix);
 
115
    } else {
 
116
      return '-' + this.negate().toString(radix);
 
117
    }
 
118
  }
 
119
 
 
120
  // Do several (6) digits each time through the loop, so as to
 
121
  // minimize the calls to the very expensive emulated div.
 
122
  var radixToPower = Long.fromNumber(Math.pow(radix, 6));
 
123
 
 
124
  var rem = this;
 
125
  var result = '';
 
126
  while (true) {
 
127
    var remDiv = rem.div(radixToPower);
 
128
    var intval = rem.subtract(remDiv.multiply(radixToPower)).toInt();
 
129
    var digits = intval.toString(radix);
 
130
 
 
131
    rem = remDiv;
 
132
    if (rem.isZero()) {
 
133
      return digits + result;
 
134
    } else {
 
135
      while (digits.length < 6) {
 
136
        digits = '0' + digits;
 
137
      }
 
138
      result = '' + digits + result;
 
139
    }
 
140
  }
 
141
};
 
142
 
 
143
/**
 
144
 * Return the high 32-bits value.
 
145
 *
 
146
 * @return {Number} the high 32-bits as a signed value.
 
147
 * @api public
 
148
 */
 
149
Long.prototype.getHighBits = function() {
 
150
  return this.high_;
 
151
};
 
152
 
 
153
/**
 
154
 * Return the low 32-bits value.
 
155
 *
 
156
 * @return {Number} the low 32-bits as a signed value.
 
157
 * @api public
 
158
 */
 
159
Long.prototype.getLowBits = function() {
 
160
  return this.low_;
 
161
};
 
162
 
 
163
/**
 
164
 * Return the low unsigned 32-bits value.
 
165
 *
 
166
 * @return {Number} the low 32-bits as an unsigned value.
 
167
 * @api public
 
168
 */
 
169
Long.prototype.getLowBitsUnsigned = function() {
 
170
  return (this.low_ >= 0) ?
 
171
      this.low_ : Long.TWO_PWR_32_DBL_ + this.low_;
 
172
};
 
173
 
 
174
/**
 
175
 * Returns the number of bits needed to represent the absolute value of this Long.
 
176
 *
 
177
 * @return {Number} Returns the number of bits needed to represent the absolute value of this Long.
 
178
 * @api public
 
179
 */
 
180
Long.prototype.getNumBitsAbs = function() {
 
181
  if (this.isNegative()) {
 
182
    if (this.equals(Long.MIN_VALUE)) {
 
183
      return 64;
 
184
    } else {
 
185
      return this.negate().getNumBitsAbs();
 
186
    }
 
187
  } else {
 
188
    var val = this.high_ != 0 ? this.high_ : this.low_;
 
189
    for (var bit = 31; bit > 0; bit--) {
 
190
      if ((val & (1 << bit)) != 0) {
 
191
        break;
 
192
      }
 
193
    }
 
194
    return this.high_ != 0 ? bit + 33 : bit + 1;
 
195
  }
 
196
};
 
197
 
 
198
/**
 
199
 * Return whether this value is zero.
 
200
 *
 
201
 * @return {Boolean} whether this value is zero.
 
202
 * @api public
 
203
 */
 
204
Long.prototype.isZero = function() {
 
205
  return this.high_ == 0 && this.low_ == 0;
 
206
};
 
207
 
 
208
/**
 
209
 * Return whether this value is negative.
 
210
 *
 
211
 * @return {Boolean} whether this value is negative.
 
212
 * @api public
 
213
 */
 
214
Long.prototype.isNegative = function() {
 
215
  return this.high_ < 0;
 
216
};
 
217
 
 
218
/**
 
219
 * Return whether this value is odd.
 
220
 *
 
221
 * @return {Boolean} whether this value is odd.
 
222
 * @api public
 
223
 */
 
224
Long.prototype.isOdd = function() {
 
225
  return (this.low_ & 1) == 1;
 
226
};
 
227
 
 
228
/**
 
229
 * Return whether this Long equals the other
 
230
 *
 
231
 * @param {Long} other Long to compare against.
 
232
 * @return {Boolean} whether this Long equals the other
 
233
 * @api public
 
234
 */
 
235
Long.prototype.equals = function(other) {
 
236
  return (this.high_ == other.high_) && (this.low_ == other.low_);
 
237
};
 
238
 
 
239
/**
 
240
 * Return whether this Long does not equal the other.
 
241
 *
 
242
 * @param {Long} other Long to compare against.
 
243
 * @return {Boolean} whether this Long does not equal the other.
 
244
 * @api public
 
245
 */
 
246
Long.prototype.notEquals = function(other) {
 
247
  return (this.high_ != other.high_) || (this.low_ != other.low_);
 
248
};
 
249
 
 
250
/**
 
251
 * Return whether this Long is less than the other.
 
252
 *
 
253
 * @param {Long} other Long to compare against.
 
254
 * @return {Boolean} whether this Long is less than the other.
 
255
 * @api public
 
256
 */
 
257
Long.prototype.lessThan = function(other) {
 
258
  return this.compare(other) < 0;
 
259
};
 
260
 
 
261
/**
 
262
 * Return whether this Long is less than or equal to the other.
 
263
 *
 
264
 * @param {Long} other Long to compare against.
 
265
 * @return {Boolean} whether this Long is less than or equal to the other.
 
266
 * @api public
 
267
 */
 
268
Long.prototype.lessThanOrEqual = function(other) {
 
269
  return this.compare(other) <= 0;
 
270
};
 
271
 
 
272
/**
 
273
 * Return whether this Long is greater than the other.
 
274
 *
 
275
 * @param {Long} other Long to compare against.
 
276
 * @return {Boolean} whether this Long is greater than the other.
 
277
 * @api public
 
278
 */
 
279
Long.prototype.greaterThan = function(other) {
 
280
  return this.compare(other) > 0;
 
281
};
 
282
 
 
283
/**
 
284
 * Return whether this Long is greater than or equal to the other.
 
285
 *
 
286
 * @param {Long} other Long to compare against.
 
287
 * @return {Boolean} whether this Long is greater than or equal to the other.
 
288
 * @api public
 
289
 */
 
290
Long.prototype.greaterThanOrEqual = function(other) {
 
291
  return this.compare(other) >= 0;
 
292
};
 
293
 
 
294
/**
 
295
 * Compares this Long with the given one.
 
296
 *
 
297
 * @param {Long} other Long to compare against.
 
298
 * @return {Boolean} 0 if they are the same, 1 if the this is greater, and -1 if the given one is greater.
 
299
 * @api public
 
300
 */
 
301
Long.prototype.compare = function(other) {
 
302
  if (this.equals(other)) {
 
303
    return 0;
 
304
  }
 
305
 
 
306
  var thisNeg = this.isNegative();
 
307
  var otherNeg = other.isNegative();
 
308
  if (thisNeg && !otherNeg) {
 
309
    return -1;
 
310
  }
 
311
  if (!thisNeg && otherNeg) {
 
312
    return 1;
 
313
  }
 
314
 
 
315
  // at this point, the signs are the same, so subtraction will not overflow
 
316
  if (this.subtract(other).isNegative()) {
 
317
    return -1;
 
318
  } else {
 
319
    return 1;
 
320
  }
 
321
};
 
322
 
 
323
/**
 
324
 * The negation of this value.
 
325
 *
 
326
 * @return {Long} the negation of this value.
 
327
 * @api public
 
328
 */
 
329
Long.prototype.negate = function() {
 
330
  if (this.equals(Long.MIN_VALUE)) {
 
331
    return Long.MIN_VALUE;
 
332
  } else {
 
333
    return this.not().add(Long.ONE);
 
334
  }
 
335
};
 
336
 
 
337
/**
 
338
 * Returns the sum of this and the given Long.
 
339
 *
 
340
 * @param {Long} other Long to add to this one.
 
341
 * @return {Long} the sum of this and the given Long.
 
342
 * @api public
 
343
 */
 
344
Long.prototype.add = function(other) {
 
345
  // Divide each number into 4 chunks of 16 bits, and then sum the chunks.
 
346
 
 
347
  var a48 = this.high_ >>> 16;
 
348
  var a32 = this.high_ & 0xFFFF;
 
349
  var a16 = this.low_ >>> 16;
 
350
  var a00 = this.low_ & 0xFFFF;
 
351
 
 
352
  var b48 = other.high_ >>> 16;
 
353
  var b32 = other.high_ & 0xFFFF;
 
354
  var b16 = other.low_ >>> 16;
 
355
  var b00 = other.low_ & 0xFFFF;
 
356
 
 
357
  var c48 = 0, c32 = 0, c16 = 0, c00 = 0;
 
358
  c00 += a00 + b00;
 
359
  c16 += c00 >>> 16;
 
360
  c00 &= 0xFFFF;
 
361
  c16 += a16 + b16;
 
362
  c32 += c16 >>> 16;
 
363
  c16 &= 0xFFFF;
 
364
  c32 += a32 + b32;
 
365
  c48 += c32 >>> 16;
 
366
  c32 &= 0xFFFF;
 
367
  c48 += a48 + b48;
 
368
  c48 &= 0xFFFF;
 
369
  return Long.fromBits((c16 << 16) | c00, (c48 << 16) | c32);
 
370
};
 
371
 
 
372
/**
 
373
 * Returns the difference of this and the given Long.
 
374
 *
 
375
 * @param {Long} other Long to subtract from this.
 
376
 * @return {Long} the difference of this and the given Long.
 
377
 * @api public
 
378
 */
 
379
Long.prototype.subtract = function(other) {
 
380
  return this.add(other.negate());
 
381
};
 
382
 
 
383
/**
 
384
 * Returns the product of this and the given Long.
 
385
 *
 
386
 * @param {Long} other Long to multiply with this.
 
387
 * @return {Long} the product of this and the other.
 
388
 * @api public
 
389
 */
 
390
Long.prototype.multiply = function(other) {
 
391
  if (this.isZero()) {
 
392
    return Long.ZERO;
 
393
  } else if (other.isZero()) {
 
394
    return Long.ZERO;
 
395
  }
 
396
 
 
397
  if (this.equals(Long.MIN_VALUE)) {
 
398
    return other.isOdd() ? Long.MIN_VALUE : Long.ZERO;
 
399
  } else if (other.equals(Long.MIN_VALUE)) {
 
400
    return this.isOdd() ? Long.MIN_VALUE : Long.ZERO;
 
401
  }
 
402
 
 
403
  if (this.isNegative()) {
 
404
    if (other.isNegative()) {
 
405
      return this.negate().multiply(other.negate());
 
406
    } else {
 
407
      return this.negate().multiply(other).negate();
 
408
    }
 
409
  } else if (other.isNegative()) {
 
410
    return this.multiply(other.negate()).negate();
 
411
  }
 
412
 
 
413
  // If both Longs are small, use float multiplication
 
414
  if (this.lessThan(Long.TWO_PWR_24_) &&
 
415
      other.lessThan(Long.TWO_PWR_24_)) {
 
416
    return Long.fromNumber(this.toNumber() * other.toNumber());
 
417
  }
 
418
 
 
419
  // Divide each Long into 4 chunks of 16 bits, and then add up 4x4 products.
 
420
  // We can skip products that would overflow.
 
421
 
 
422
  var a48 = this.high_ >>> 16;
 
423
  var a32 = this.high_ & 0xFFFF;
 
424
  var a16 = this.low_ >>> 16;
 
425
  var a00 = this.low_ & 0xFFFF;
 
426
 
 
427
  var b48 = other.high_ >>> 16;
 
428
  var b32 = other.high_ & 0xFFFF;
 
429
  var b16 = other.low_ >>> 16;
 
430
  var b00 = other.low_ & 0xFFFF;
 
431
 
 
432
  var c48 = 0, c32 = 0, c16 = 0, c00 = 0;
 
433
  c00 += a00 * b00;
 
434
  c16 += c00 >>> 16;
 
435
  c00 &= 0xFFFF;
 
436
  c16 += a16 * b00;
 
437
  c32 += c16 >>> 16;
 
438
  c16 &= 0xFFFF;
 
439
  c16 += a00 * b16;
 
440
  c32 += c16 >>> 16;
 
441
  c16 &= 0xFFFF;
 
442
  c32 += a32 * b00;
 
443
  c48 += c32 >>> 16;
 
444
  c32 &= 0xFFFF;
 
445
  c32 += a16 * b16;
 
446
  c48 += c32 >>> 16;
 
447
  c32 &= 0xFFFF;
 
448
  c32 += a00 * b32;
 
449
  c48 += c32 >>> 16;
 
450
  c32 &= 0xFFFF;
 
451
  c48 += a48 * b00 + a32 * b16 + a16 * b32 + a00 * b48;
 
452
  c48 &= 0xFFFF;
 
453
  return Long.fromBits((c16 << 16) | c00, (c48 << 16) | c32);
 
454
};
 
455
 
 
456
/**
 
457
 * Returns this Long divided by the given one.
 
458
 *
 
459
 * @param {Long} other Long by which to divide.
 
460
 * @return {Long} this Long divided by the given one.
 
461
 * @api public
 
462
 */
 
463
Long.prototype.div = function(other) {
 
464
  if (other.isZero()) {
 
465
    throw Error('division by zero');
 
466
  } else if (this.isZero()) {
 
467
    return Long.ZERO;
 
468
  }
 
469
 
 
470
  if (this.equals(Long.MIN_VALUE)) {
 
471
    if (other.equals(Long.ONE) ||
 
472
        other.equals(Long.NEG_ONE)) {
 
473
      return Long.MIN_VALUE;  // recall that -MIN_VALUE == MIN_VALUE
 
474
    } else if (other.equals(Long.MIN_VALUE)) {
 
475
      return Long.ONE;
 
476
    } else {
 
477
      // At this point, we have |other| >= 2, so |this/other| < |MIN_VALUE|.
 
478
      var halfThis = this.shiftRight(1);
 
479
      var approx = halfThis.div(other).shiftLeft(1);
 
480
      if (approx.equals(Long.ZERO)) {
 
481
        return other.isNegative() ? Long.ONE : Long.NEG_ONE;
 
482
      } else {
 
483
        var rem = this.subtract(other.multiply(approx));
 
484
        var result = approx.add(rem.div(other));
 
485
        return result;
 
486
      }
 
487
    }
 
488
  } else if (other.equals(Long.MIN_VALUE)) {
 
489
    return Long.ZERO;
 
490
  }
 
491
 
 
492
  if (this.isNegative()) {
 
493
    if (other.isNegative()) {
 
494
      return this.negate().div(other.negate());
 
495
    } else {
 
496
      return this.negate().div(other).negate();
 
497
    }
 
498
  } else if (other.isNegative()) {
 
499
    return this.div(other.negate()).negate();
 
500
  }
 
501
 
 
502
  // Repeat the following until the remainder is less than other:  find a
 
503
  // floating-point that approximates remainder / other *from below*, add this
 
504
  // into the result, and subtract it from the remainder.  It is critical that
 
505
  // the approximate value is less than or equal to the real value so that the
 
506
  // remainder never becomes negative.
 
507
  var res = Long.ZERO;
 
508
  var rem = this;
 
509
  while (rem.greaterThanOrEqual(other)) {
 
510
    // Approximate the result of division. This may be a little greater or
 
511
    // smaller than the actual value.
 
512
    var approx = Math.max(1, Math.floor(rem.toNumber() / other.toNumber()));
 
513
 
 
514
    // We will tweak the approximate result by changing it in the 48-th digit or
 
515
    // the smallest non-fractional digit, whichever is larger.
 
516
    var log2 = Math.ceil(Math.log(approx) / Math.LN2);
 
517
    var delta = (log2 <= 48) ? 1 : Math.pow(2, log2 - 48);
 
518
 
 
519
    // Decrease the approximation until it is smaller than the remainder.  Note
 
520
    // that if it is too large, the product overflows and is negative.
 
521
    var approxRes = Long.fromNumber(approx);
 
522
    var approxRem = approxRes.multiply(other);
 
523
    while (approxRem.isNegative() || approxRem.greaterThan(rem)) {
 
524
      approx -= delta;
 
525
      approxRes = Long.fromNumber(approx);
 
526
      approxRem = approxRes.multiply(other);
 
527
    }
 
528
 
 
529
    // We know the answer can't be zero... and actually, zero would cause
 
530
    // infinite recursion since we would make no progress.
 
531
    if (approxRes.isZero()) {
 
532
      approxRes = Long.ONE;
 
533
    }
 
534
 
 
535
    res = res.add(approxRes);
 
536
    rem = rem.subtract(approxRem);
 
537
  }
 
538
  return res;
 
539
};
 
540
 
 
541
/**
 
542
 * Returns this Long modulo the given one.
 
543
 *
 
544
 * @param {Long} other Long by which to mod.
 
545
 * @return {Long} this Long modulo the given one.
 
546
 * @api public
 
547
 */
 
548
Long.prototype.modulo = function(other) {
 
549
  return this.subtract(this.div(other).multiply(other));
 
550
};
 
551
 
 
552
/**
 
553
 * The bitwise-NOT of this value.
 
554
 *
 
555
 * @return {Long} the bitwise-NOT of this value.
 
556
 * @api public
 
557
 */
 
558
Long.prototype.not = function() {
 
559
  return Long.fromBits(~this.low_, ~this.high_);
 
560
};
 
561
 
 
562
/**
 
563
 * Returns the bitwise-AND of this Long and the given one.
 
564
 *
 
565
 * @param {Long} other the Long with which to AND.
 
566
 * @return {Long} the bitwise-AND of this and the other.
 
567
 * @api public
 
568
 */
 
569
Long.prototype.and = function(other) {
 
570
  return Long.fromBits(this.low_ & other.low_, this.high_ & other.high_);
 
571
};
 
572
 
 
573
/**
 
574
 * Returns the bitwise-OR of this Long and the given one.
 
575
 *
 
576
 * @param {Long} other the Long with which to OR.
 
577
 * @return {Long} the bitwise-OR of this and the other.
 
578
 * @api public
 
579
 */
 
580
Long.prototype.or = function(other) {
 
581
  return Long.fromBits(this.low_ | other.low_, this.high_ | other.high_);
 
582
};
 
583
 
 
584
/**
 
585
 * Returns the bitwise-XOR of this Long and the given one.
 
586
 *
 
587
 * @param {Long} other the Long with which to XOR.
 
588
 * @return {Long} the bitwise-XOR of this and the other.
 
589
 * @api public
 
590
 */
 
591
Long.prototype.xor = function(other) {
 
592
  return Long.fromBits(this.low_ ^ other.low_, this.high_ ^ other.high_);
 
593
};
 
594
 
 
595
/**
 
596
 * Returns this Long with bits shifted to the left by the given amount.
 
597
 *
 
598
 * @param {Number} numBits the number of bits by which to shift.
 
599
 * @return {Long} this shifted to the left by the given amount.
 
600
 * @api public
 
601
 */
 
602
Long.prototype.shiftLeft = function(numBits) {
 
603
  numBits &= 63;
 
604
  if (numBits == 0) {
 
605
    return this;
 
606
  } else {
 
607
    var low = this.low_;
 
608
    if (numBits < 32) {
 
609
      var high = this.high_;
 
610
      return Long.fromBits(
 
611
                 low << numBits,
 
612
                 (high << numBits) | (low >>> (32 - numBits)));
 
613
    } else {
 
614
      return Long.fromBits(0, low << (numBits - 32));
 
615
    }
 
616
  }
 
617
};
 
618
 
 
619
/**
 
620
 * Returns this Long with bits shifted to the right by the given amount.
 
621
 *
 
622
 * @param {Number} numBits the number of bits by which to shift.
 
623
 * @return {Long} this shifted to the right by the given amount.
 
624
 * @api public
 
625
 */
 
626
Long.prototype.shiftRight = function(numBits) {
 
627
  numBits &= 63;
 
628
  if (numBits == 0) {
 
629
    return this;
 
630
  } else {
 
631
    var high = this.high_;
 
632
    if (numBits < 32) {
 
633
      var low = this.low_;
 
634
      return Long.fromBits(
 
635
                 (low >>> numBits) | (high << (32 - numBits)),
 
636
                 high >> numBits);
 
637
    } else {
 
638
      return Long.fromBits(
 
639
                 high >> (numBits - 32),
 
640
                 high >= 0 ? 0 : -1);
 
641
    }
 
642
  }
 
643
};
 
644
 
 
645
/**
 
646
 * Returns this Long with bits shifted to the right by the given amount, with the new top bits matching the current sign bit.
 
647
 *
 
648
 * @param {Number} numBits the number of bits by which to shift.
 
649
 * @return {Long} this shifted to the right by the given amount, with zeros placed into the new leading bits.
 
650
 * @api public
 
651
 */
 
652
Long.prototype.shiftRightUnsigned = function(numBits) {
 
653
  numBits &= 63;
 
654
  if (numBits == 0) {
 
655
    return this;
 
656
  } else {
 
657
    var high = this.high_;
 
658
    if (numBits < 32) {
 
659
      var low = this.low_;
 
660
      return Long.fromBits(
 
661
                 (low >>> numBits) | (high << (32 - numBits)),
 
662
                 high >>> numBits);
 
663
    } else if (numBits == 32) {
 
664
      return Long.fromBits(high, 0);
 
665
    } else {
 
666
      return Long.fromBits(high >>> (numBits - 32), 0);
 
667
    }
 
668
  }
 
669
};
 
670
 
 
671
/**
 
672
 * Returns a Long representing the given (32-bit) integer value.
 
673
 *
 
674
 * @param {Number} value the 32-bit integer in question.
 
675
 * @return {Long} the corresponding Long value.
 
676
 * @api public
 
677
 */
 
678
Long.fromInt = function(value) {
 
679
  if (-128 <= value && value < 128) {
 
680
    var cachedObj = Long.INT_CACHE_[value];
 
681
    if (cachedObj) {
 
682
      return cachedObj;
 
683
    }
 
684
  }
 
685
 
 
686
  var obj = new Long(value | 0, value < 0 ? -1 : 0);
 
687
  if (-128 <= value && value < 128) {
 
688
    Long.INT_CACHE_[value] = obj;
 
689
  }
 
690
  return obj;
 
691
};
 
692
 
 
693
/**
 
694
 * Returns a Long representing the given value, provided that it is a finite number. Otherwise, zero is returned.
 
695
 *
 
696
 * @param {Number} value the number in question.
 
697
 * @return {Long} the corresponding Long value.
 
698
 * @api public
 
699
 */
 
700
Long.fromNumber = function(value) {
 
701
  if (isNaN(value) || !isFinite(value)) {
 
702
    return Long.ZERO;
 
703
  } else if (value <= -Long.TWO_PWR_63_DBL_) {
 
704
    return Long.MIN_VALUE;
 
705
  } else if (value + 1 >= Long.TWO_PWR_63_DBL_) {
 
706
    return Long.MAX_VALUE;
 
707
  } else if (value < 0) {
 
708
    return Long.fromNumber(-value).negate();
 
709
  } else {
 
710
    return new Long(
 
711
               (value % Long.TWO_PWR_32_DBL_) | 0,
 
712
               (value / Long.TWO_PWR_32_DBL_) | 0);
 
713
  }
 
714
};
 
715
 
 
716
/**
 
717
 * Returns a Long representing the 64-bit integer that comes by concatenating the given high and low bits. Each is assumed to use 32 bits.
 
718
 *
 
719
 * @param {Number} lowBits the low 32-bits.
 
720
 * @param {Number} highBits the high 32-bits.
 
721
 * @return {Long} the corresponding Long value.
 
722
 * @api public
 
723
 */
 
724
Long.fromBits = function(lowBits, highBits) {
 
725
  return new Long(lowBits, highBits);
 
726
};
 
727
 
 
728
/**
 
729
 * Returns a Long representation of the given string, written using the given radix.
 
730
 *
 
731
 * @param {String} str the textual representation of the Long.
 
732
 * @param {Number} opt_radix the radix in which the text is written.
 
733
 * @return {Long} the corresponding Long value.
 
734
 * @api public
 
735
 */
 
736
Long.fromString = function(str, opt_radix) {
 
737
  if (str.length == 0) {
 
738
    throw Error('number format error: empty string');
 
739
  }
 
740
 
 
741
  var radix = opt_radix || 10;
 
742
  if (radix < 2 || 36 < radix) {
 
743
    throw Error('radix out of range: ' + radix);
 
744
  }
 
745
 
 
746
  if (str.charAt(0) == '-') {
 
747
    return Long.fromString(str.substring(1), radix).negate();
 
748
  } else if (str.indexOf('-') >= 0) {
 
749
    throw Error('number format error: interior "-" character: ' + str);
 
750
  }
 
751
 
 
752
  // Do several (8) digits each time through the loop, so as to
 
753
  // minimize the calls to the very expensive emulated div.
 
754
  var radixToPower = Long.fromNumber(Math.pow(radix, 8));
 
755
 
 
756
  var result = Long.ZERO;
 
757
  for (var i = 0; i < str.length; i += 8) {
 
758
    var size = Math.min(8, str.length - i);
 
759
    var value = parseInt(str.substring(i, i + size), radix);
 
760
    if (size < 8) {
 
761
      var power = Long.fromNumber(Math.pow(radix, size));
 
762
      result = result.multiply(power).add(Long.fromNumber(value));
 
763
    } else {
 
764
      result = result.multiply(radixToPower);
 
765
      result = result.add(Long.fromNumber(value));
 
766
    }
 
767
  }
 
768
  return result;
 
769
};
 
770
 
 
771
// NOTE: Common constant values ZERO, ONE, NEG_ONE, etc. are defined below the
 
772
// from* methods on which they depend.
 
773
 
 
774
 
 
775
/**
 
776
 * A cache of the Long representations of small integer values.
 
777
 * @type {Object}
 
778
 * @api private
 
779
 */
 
780
Long.INT_CACHE_ = {};
 
781
 
 
782
// NOTE: the compiler should inline these constant values below and then remove
 
783
// these variables, so there should be no runtime penalty for these.
 
784
 
 
785
/**
 
786
 * Number used repeated below in calculations.  This must appear before the
 
787
 * first call to any from* function below.
 
788
 * @type {number}
 
789
 * @api private
 
790
 */
 
791
Long.TWO_PWR_16_DBL_ = 1 << 16;
 
792
 
 
793
/**
 
794
 * @type {number}
 
795
 * @api private
 
796
 */
 
797
Long.TWO_PWR_24_DBL_ = 1 << 24;
 
798
 
 
799
/**
 
800
 * @type {number}
 
801
 * @api private
 
802
 */
 
803
Long.TWO_PWR_32_DBL_ = Long.TWO_PWR_16_DBL_ * Long.TWO_PWR_16_DBL_;
 
804
 
 
805
/**
 
806
 * @type {number}
 
807
 * @api private
 
808
 */
 
809
Long.TWO_PWR_31_DBL_ = Long.TWO_PWR_32_DBL_ / 2;
 
810
 
 
811
/**
 
812
 * @type {number}
 
813
 * @api private
 
814
 */
 
815
Long.TWO_PWR_48_DBL_ = Long.TWO_PWR_32_DBL_ * Long.TWO_PWR_16_DBL_;
 
816
 
 
817
/**
 
818
 * @type {number}
 
819
 * @api private
 
820
 */
 
821
Long.TWO_PWR_64_DBL_ = Long.TWO_PWR_32_DBL_ * Long.TWO_PWR_32_DBL_;
 
822
 
 
823
/**
 
824
 * @type {number}
 
825
 * @api private
 
826
 */
 
827
Long.TWO_PWR_63_DBL_ = Long.TWO_PWR_64_DBL_ / 2;
 
828
 
 
829
/** @type {Long} */
 
830
Long.ZERO = Long.fromInt(0);
 
831
 
 
832
/** @type {Long} */
 
833
Long.ONE = Long.fromInt(1);
 
834
 
 
835
/** @type {Long} */
 
836
Long.NEG_ONE = Long.fromInt(-1);
 
837
 
 
838
/** @type {Long} */
 
839
Long.MAX_VALUE =
 
840
    Long.fromBits(0xFFFFFFFF | 0, 0x7FFFFFFF | 0);
 
841
 
 
842
/** @type {Long} */
 
843
Long.MIN_VALUE = Long.fromBits(0, 0x80000000 | 0);
 
844
 
 
845
/**
 
846
 * @type {Long}
 
847
 * @api private
 
848
 */
 
849
Long.TWO_PWR_24_ = Long.fromInt(1 << 24);
 
850
 
 
851
/**
 
852
 * Expose.
 
853
 */
 
854
exports.Long = Long;
 
 
b'\\ No newline at end of file'