diff options
author | Brian Picciano <mediocregopher@gmail.com> | 2022-05-20 13:37:43 -0600 |
---|---|---|
committer | Brian Picciano <mediocregopher@gmail.com> | 2022-05-20 13:37:43 -0600 |
commit | 16cfbd19157df76e7296dddb287412f1099feb33 (patch) | |
tree | e4bbf892066cceeaeeaee4c25e5365152412a1c3 /srv/src/http/static/viz/1/goog/math | |
parent | 3cdee89c961ae9c836234f5aec87174a04a800a8 (diff) |
Move static assets to within srv
Diffstat (limited to 'srv/src/http/static/viz/1/goog/math')
-rw-r--r-- | srv/src/http/static/viz/1/goog/math/coordinate.js | 268 | ||||
-rw-r--r-- | srv/src/http/static/viz/1/goog/math/integer.js | 807 | ||||
-rw-r--r-- | srv/src/http/static/viz/1/goog/math/long.js | 843 | ||||
-rw-r--r-- | srv/src/http/static/viz/1/goog/math/math.js | 447 | ||||
-rw-r--r-- | srv/src/http/static/viz/1/goog/math/size.js | 227 |
5 files changed, 2592 insertions, 0 deletions
diff --git a/srv/src/http/static/viz/1/goog/math/coordinate.js b/srv/src/http/static/viz/1/goog/math/coordinate.js new file mode 100644 index 0000000..a08b9cb --- /dev/null +++ b/srv/src/http/static/viz/1/goog/math/coordinate.js @@ -0,0 +1,268 @@ +// Copyright 2006 The Closure Library Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS-IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +/** + * @fileoverview A utility class for representing two-dimensional positions. + */ + + +goog.provide('goog.math.Coordinate'); + +goog.require('goog.math'); + + + +/** + * Class for representing coordinates and positions. + * @param {number=} opt_x Left, defaults to 0. + * @param {number=} opt_y Top, defaults to 0. + * @struct + * @constructor + */ +goog.math.Coordinate = function(opt_x, opt_y) { + /** + * X-value + * @type {number} + */ + this.x = goog.isDef(opt_x) ? opt_x : 0; + + /** + * Y-value + * @type {number} + */ + this.y = goog.isDef(opt_y) ? opt_y : 0; +}; + + +/** + * Returns a new copy of the coordinate. + * @return {!goog.math.Coordinate} A clone of this coordinate. + */ +goog.math.Coordinate.prototype.clone = function() { + return new goog.math.Coordinate(this.x, this.y); +}; + + +if (goog.DEBUG) { + /** + * Returns a nice string representing the coordinate. + * @return {string} In the form (50, 73). + * @override + */ + goog.math.Coordinate.prototype.toString = function() { + return '(' + this.x + ', ' + this.y + ')'; + }; +} + + +/** + * Compares coordinates for equality. + * @param {goog.math.Coordinate} a A Coordinate. + * @param {goog.math.Coordinate} b A Coordinate. + * @return {boolean} True iff the coordinates are equal, or if both are null. + */ +goog.math.Coordinate.equals = function(a, b) { + if (a == b) { + return true; + } + if (!a || !b) { + return false; + } + return a.x == b.x && a.y == b.y; +}; + + +/** + * Returns the distance between two coordinates. + * @param {!goog.math.Coordinate} a A Coordinate. + * @param {!goog.math.Coordinate} b A Coordinate. + * @return {number} The distance between {@code a} and {@code b}. + */ +goog.math.Coordinate.distance = function(a, b) { + var dx = a.x - b.x; + var dy = a.y - b.y; + return Math.sqrt(dx * dx + dy * dy); +}; + + +/** + * Returns the magnitude of a coordinate. + * @param {!goog.math.Coordinate} a A Coordinate. + * @return {number} The distance between the origin and {@code a}. + */ +goog.math.Coordinate.magnitude = function(a) { + return Math.sqrt(a.x * a.x + a.y * a.y); +}; + + +/** + * Returns the angle from the origin to a coordinate. + * @param {!goog.math.Coordinate} a A Coordinate. + * @return {number} The angle, in degrees, clockwise from the positive X + * axis to {@code a}. + */ +goog.math.Coordinate.azimuth = function(a) { + return goog.math.angle(0, 0, a.x, a.y); +}; + + +/** + * Returns the squared distance between two coordinates. Squared distances can + * be used for comparisons when the actual value is not required. + * + * Performance note: eliminating the square root is an optimization often used + * in lower-level languages, but the speed difference is not nearly as + * pronounced in JavaScript (only a few percent.) + * + * @param {!goog.math.Coordinate} a A Coordinate. + * @param {!goog.math.Coordinate} b A Coordinate. + * @return {number} The squared distance between {@code a} and {@code b}. + */ +goog.math.Coordinate.squaredDistance = function(a, b) { + var dx = a.x - b.x; + var dy = a.y - b.y; + return dx * dx + dy * dy; +}; + + +/** + * Returns the difference between two coordinates as a new + * goog.math.Coordinate. + * @param {!goog.math.Coordinate} a A Coordinate. + * @param {!goog.math.Coordinate} b A Coordinate. + * @return {!goog.math.Coordinate} A Coordinate representing the difference + * between {@code a} and {@code b}. + */ +goog.math.Coordinate.difference = function(a, b) { + return new goog.math.Coordinate(a.x - b.x, a.y - b.y); +}; + + +/** + * Returns the sum of two coordinates as a new goog.math.Coordinate. + * @param {!goog.math.Coordinate} a A Coordinate. + * @param {!goog.math.Coordinate} b A Coordinate. + * @return {!goog.math.Coordinate} A Coordinate representing the sum of the two + * coordinates. + */ +goog.math.Coordinate.sum = function(a, b) { + return new goog.math.Coordinate(a.x + b.x, a.y + b.y); +}; + + +/** + * Rounds the x and y fields to the next larger integer values. + * @return {!goog.math.Coordinate} This coordinate with ceil'd fields. + */ +goog.math.Coordinate.prototype.ceil = function() { + this.x = Math.ceil(this.x); + this.y = Math.ceil(this.y); + return this; +}; + + +/** + * Rounds the x and y fields to the next smaller integer values. + * @return {!goog.math.Coordinate} This coordinate with floored fields. + */ +goog.math.Coordinate.prototype.floor = function() { + this.x = Math.floor(this.x); + this.y = Math.floor(this.y); + return this; +}; + + +/** + * Rounds the x and y fields to the nearest integer values. + * @return {!goog.math.Coordinate} This coordinate with rounded fields. + */ +goog.math.Coordinate.prototype.round = function() { + this.x = Math.round(this.x); + this.y = Math.round(this.y); + return this; +}; + + +/** + * Translates this box by the given offsets. If a {@code goog.math.Coordinate} + * is given, then the x and y values are translated by the coordinate's x and y. + * Otherwise, x and y are translated by {@code tx} and {@code opt_ty} + * respectively. + * @param {number|goog.math.Coordinate} tx The value to translate x by or the + * the coordinate to translate this coordinate by. + * @param {number=} opt_ty The value to translate y by. + * @return {!goog.math.Coordinate} This coordinate after translating. + */ +goog.math.Coordinate.prototype.translate = function(tx, opt_ty) { + if (tx instanceof goog.math.Coordinate) { + this.x += tx.x; + this.y += tx.y; + } else { + this.x += Number(tx); + if (goog.isNumber(opt_ty)) { + this.y += opt_ty; + } + } + return this; +}; + + +/** + * Scales this coordinate by the given scale factors. The x and y values are + * scaled by {@code sx} and {@code opt_sy} respectively. If {@code opt_sy} + * is not given, then {@code sx} is used for both x and y. + * @param {number} sx The scale factor to use for the x dimension. + * @param {number=} opt_sy The scale factor to use for the y dimension. + * @return {!goog.math.Coordinate} This coordinate after scaling. + */ +goog.math.Coordinate.prototype.scale = function(sx, opt_sy) { + var sy = goog.isNumber(opt_sy) ? opt_sy : sx; + this.x *= sx; + this.y *= sy; + return this; +}; + + +/** + * Rotates this coordinate clockwise about the origin (or, optionally, the given + * center) by the given angle, in radians. + * @param {number} radians The angle by which to rotate this coordinate + * clockwise about the given center, in radians. + * @param {!goog.math.Coordinate=} opt_center The center of rotation. Defaults + * to (0, 0) if not given. + */ +goog.math.Coordinate.prototype.rotateRadians = function(radians, opt_center) { + var center = opt_center || new goog.math.Coordinate(0, 0); + + var x = this.x; + var y = this.y; + var cos = Math.cos(radians); + var sin = Math.sin(radians); + + this.x = (x - center.x) * cos - (y - center.y) * sin + center.x; + this.y = (x - center.x) * sin + (y - center.y) * cos + center.y; +}; + + +/** + * Rotates this coordinate clockwise about the origin (or, optionally, the given + * center) by the given angle, in degrees. + * @param {number} degrees The angle by which to rotate this coordinate + * clockwise about the given center, in degrees. + * @param {!goog.math.Coordinate=} opt_center The center of rotation. Defaults + * to (0, 0) if not given. + */ +goog.math.Coordinate.prototype.rotateDegrees = function(degrees, opt_center) { + this.rotateRadians(goog.math.toRadians(degrees), opt_center); +}; diff --git a/srv/src/http/static/viz/1/goog/math/integer.js b/srv/src/http/static/viz/1/goog/math/integer.js new file mode 100644 index 0000000..11b6a95 --- /dev/null +++ b/srv/src/http/static/viz/1/goog/math/integer.js @@ -0,0 +1,807 @@ +// Copyright 2009 The Closure Library Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS-IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +/** + * @fileoverview Defines an Integer class for representing (potentially) + * infinite length two's-complement integer values. + * + * For the specific case of 64-bit integers, use goog.math.Long, which is more + * efficient. + * + */ + +goog.provide('goog.math.Integer'); + + + +/** + * Constructs a two's-complement integer an array containing bits of the + * integer in 32-bit (signed) pieces, given in little-endian order (i.e., + * lowest-order bits in the first piece), and the sign of -1 or 0. + * + * See the from* functions below for other convenient ways of constructing + * Integers. + * + * The internal representation of an integer is an array of 32-bit signed + * pieces, along with a sign (0 or -1) that indicates the contents of all the + * other 32-bit pieces out to infinity. We use 32-bit pieces because these are + * the size of integers on which Javascript performs bit-operations. For + * operations like addition and multiplication, we split each number into 16-bit + * pieces, which can easily be multiplied within Javascript's floating-point + * representation without overflow or change in sign. + * + * @struct + * @constructor + * @param {Array<number>} bits Array containing the bits of the number. + * @param {number} sign The sign of the number: -1 for negative and 0 positive. + * @final + */ +goog.math.Integer = function(bits, sign) { + /** + * @type {!Array<number>} + * @private + */ + this.bits_ = []; + + /** + * @type {number} + * @private + */ + this.sign_ = sign; + + // Copy the 32-bit signed integer values passed in. We prune out those at the + // top that equal the sign since they are redundant. + var top = true; + for (var i = bits.length - 1; i >= 0; i--) { + var val = bits[i] | 0; + if (!top || val != sign) { + this.bits_[i] = val; + top = false; + } + } +}; + + +// NOTE: Common constant values ZERO, ONE, NEG_ONE, etc. are defined below the +// from* methods on which they depend. + + +/** + * A cache of the Integer representations of small integer values. + * @type {!Object} + * @private + */ +goog.math.Integer.IntCache_ = {}; + + +/** + * Returns an Integer representing the given (32-bit) integer value. + * @param {number} value A 32-bit integer value. + * @return {!goog.math.Integer} The corresponding Integer value. + */ +goog.math.Integer.fromInt = function(value) { + if (-128 <= value && value < 128) { + var cachedObj = goog.math.Integer.IntCache_[value]; + if (cachedObj) { + return cachedObj; + } + } + + var obj = new goog.math.Integer([value | 0], value < 0 ? -1 : 0); + if (-128 <= value && value < 128) { + goog.math.Integer.IntCache_[value] = obj; + } + return obj; +}; + + +/** + * Returns an Integer representing the given value, provided that it is a finite + * number. Otherwise, zero is returned. + * @param {number} value The value in question. + * @return {!goog.math.Integer} The corresponding Integer value. + */ +goog.math.Integer.fromNumber = function(value) { + if (isNaN(value) || !isFinite(value)) { + return goog.math.Integer.ZERO; + } else if (value < 0) { + return goog.math.Integer.fromNumber(-value).negate(); + } else { + var bits = []; + var pow = 1; + for (var i = 0; value >= pow; i++) { + bits[i] = (value / pow) | 0; + pow *= goog.math.Integer.TWO_PWR_32_DBL_; + } + return new goog.math.Integer(bits, 0); + } +}; + + +/** + * Returns a Integer representing the value that comes by concatenating the + * given entries, each is assumed to be 32 signed bits, given in little-endian + * order (lowest order bits in the lowest index), and sign-extending the highest + * order 32-bit value. + * @param {Array<number>} bits The bits of the number, in 32-bit signed pieces, + * in little-endian order. + * @return {!goog.math.Integer} The corresponding Integer value. + */ +goog.math.Integer.fromBits = function(bits) { + var high = bits[bits.length - 1]; + return new goog.math.Integer(bits, high & (1 << 31) ? -1 : 0); +}; + + +/** + * Returns an Integer representation of the given string, written using the + * given radix. + * @param {string} str The textual representation of the Integer. + * @param {number=} opt_radix The radix in which the text is written. + * @return {!goog.math.Integer} The corresponding Integer value. + */ +goog.math.Integer.fromString = function(str, opt_radix) { + if (str.length == 0) { + throw Error('number format error: empty string'); + } + + var radix = opt_radix || 10; + if (radix < 2 || 36 < radix) { + throw Error('radix out of range: ' + radix); + } + + if (str.charAt(0) == '-') { + return goog.math.Integer.fromString(str.substring(1), radix).negate(); + } else if (str.indexOf('-') >= 0) { + throw Error('number format error: interior "-" character'); + } + + // Do several (8) digits each time through the loop, so as to + // minimize the calls to the very expensive emulated div. + var radixToPower = goog.math.Integer.fromNumber(Math.pow(radix, 8)); + + var result = goog.math.Integer.ZERO; + for (var i = 0; i < str.length; i += 8) { + var size = Math.min(8, str.length - i); + var value = parseInt(str.substring(i, i + size), radix); + if (size < 8) { + var power = goog.math.Integer.fromNumber(Math.pow(radix, size)); + result = result.multiply(power).add(goog.math.Integer.fromNumber(value)); + } else { + result = result.multiply(radixToPower); + result = result.add(goog.math.Integer.fromNumber(value)); + } + } + return result; +}; + + +/** + * A number used repeatedly in calculations. This must appear before the first + * call to the from* functions below. + * @type {number} + * @private + */ +goog.math.Integer.TWO_PWR_32_DBL_ = (1 << 16) * (1 << 16); + + +/** @type {!goog.math.Integer} */ +goog.math.Integer.ZERO = goog.math.Integer.fromInt(0); + + +/** @type {!goog.math.Integer} */ +goog.math.Integer.ONE = goog.math.Integer.fromInt(1); + + +/** + * @type {!goog.math.Integer} + * @private + */ +goog.math.Integer.TWO_PWR_24_ = goog.math.Integer.fromInt(1 << 24); + + +/** + * Returns the value, assuming it is a 32-bit integer. + * @return {number} The corresponding int value. + */ +goog.math.Integer.prototype.toInt = function() { + return this.bits_.length > 0 ? this.bits_[0] : this.sign_; +}; + + +/** @return {number} The closest floating-point representation to this value. */ +goog.math.Integer.prototype.toNumber = function() { + if (this.isNegative()) { + return -this.negate().toNumber(); + } else { + var val = 0; + var pow = 1; + for (var i = 0; i < this.bits_.length; i++) { + val += this.getBitsUnsigned(i) * pow; + pow *= goog.math.Integer.TWO_PWR_32_DBL_; + } + return val; + } +}; + + +/** + * @param {number=} opt_radix The radix in which the text should be written. + * @return {string} The textual representation of this value. + * @override + */ +goog.math.Integer.prototype.toString = function(opt_radix) { + var radix = opt_radix || 10; + if (radix < 2 || 36 < radix) { + throw Error('radix out of range: ' + radix); + } + + if (this.isZero()) { + return '0'; + } else if (this.isNegative()) { + return '-' + this.negate().toString(radix); + } + + // Do several (6) digits each time through the loop, so as to + // minimize the calls to the very expensive emulated div. + var radixToPower = goog.math.Integer.fromNumber(Math.pow(radix, 6)); + + var rem = this; + var result = ''; + while (true) { + var remDiv = rem.divide(radixToPower); + // The right shifting fixes negative values in the case when + // intval >= 2^31; for more details see + // https://github.com/google/closure-library/pull/498 + var intval = rem.subtract(remDiv.multiply(radixToPower)).toInt() >>> 0; + var digits = intval.toString(radix); + + rem = remDiv; + if (rem.isZero()) { + return digits + result; + } else { + while (digits.length < 6) { + digits = '0' + digits; + } + result = '' + digits + result; + } + } +}; + + +/** + * Returns the index-th 32-bit (signed) piece of the Integer according to + * little-endian order (i.e., index 0 contains the smallest bits). + * @param {number} index The index in question. + * @return {number} The requested 32-bits as a signed number. + */ +goog.math.Integer.prototype.getBits = function(index) { + if (index < 0) { + return 0; // Allowing this simplifies bit shifting operations below... + } else if (index < this.bits_.length) { + return this.bits_[index]; + } else { + return this.sign_; + } +}; + + +/** + * Returns the index-th 32-bit piece as an unsigned number. + * @param {number} index The index in question. + * @return {number} The requested 32-bits as an unsigned number. + */ +goog.math.Integer.prototype.getBitsUnsigned = function(index) { + var val = this.getBits(index); + return val >= 0 ? val : goog.math.Integer.TWO_PWR_32_DBL_ + val; +}; + + +/** @return {number} The sign bit of this number, -1 or 0. */ +goog.math.Integer.prototype.getSign = function() { + return this.sign_; +}; + + +/** @return {boolean} Whether this value is zero. */ +goog.math.Integer.prototype.isZero = function() { + if (this.sign_ != 0) { + return false; + } + for (var i = 0; i < this.bits_.length; i++) { + if (this.bits_[i] != 0) { + return false; + } + } + return true; +}; + + +/** @return {boolean} Whether this value is negative. */ +goog.math.Integer.prototype.isNegative = function() { + return this.sign_ == -1; +}; + + +/** @return {boolean} Whether this value is odd. */ +goog.math.Integer.prototype.isOdd = function() { + return (this.bits_.length == 0) && (this.sign_ == -1) || + (this.bits_.length > 0) && ((this.bits_[0] & 1) != 0); +}; + + +/** + * @param {goog.math.Integer} other Integer to compare against. + * @return {boolean} Whether this Integer equals the other. + */ +goog.math.Integer.prototype.equals = function(other) { + if (this.sign_ != other.sign_) { + return false; + } + var len = Math.max(this.bits_.length, other.bits_.length); + for (var i = 0; i < len; i++) { + if (this.getBits(i) != other.getBits(i)) { + return false; + } + } + return true; +}; + + +/** + * @param {goog.math.Integer} other Integer to compare against. + * @return {boolean} Whether this Integer does not equal the other. + */ +goog.math.Integer.prototype.notEquals = function(other) { + return !this.equals(other); +}; + + +/** + * @param {goog.math.Integer} other Integer to compare against. + * @return {boolean} Whether this Integer is greater than the other. + */ +goog.math.Integer.prototype.greaterThan = function(other) { + return this.compare(other) > 0; +}; + + +/** + * @param {goog.math.Integer} other Integer to compare against. + * @return {boolean} Whether this Integer is greater than or equal to the other. + */ +goog.math.Integer.prototype.greaterThanOrEqual = function(other) { + return this.compare(other) >= 0; +}; + + +/** + * @param {goog.math.Integer} other Integer to compare against. + * @return {boolean} Whether this Integer is less than the other. + */ +goog.math.Integer.prototype.lessThan = function(other) { + return this.compare(other) < 0; +}; + + +/** + * @param {goog.math.Integer} other Integer to compare against. + * @return {boolean} Whether this Integer is less than or equal to the other. + */ +goog.math.Integer.prototype.lessThanOrEqual = function(other) { + return this.compare(other) <= 0; +}; + + +/** + * Compares this Integer with the given one. + * @param {goog.math.Integer} other Integer to compare against. + * @return {number} 0 if they are the same, 1 if the this is greater, and -1 + * if the given one is greater. + */ +goog.math.Integer.prototype.compare = function(other) { + var diff = this.subtract(other); + if (diff.isNegative()) { + return -1; + } else if (diff.isZero()) { + return 0; + } else { + return +1; + } +}; + + +/** + * Returns an integer with only the first numBits bits of this value, sign + * extended from the final bit. + * @param {number} numBits The number of bits by which to shift. + * @return {!goog.math.Integer} The shorted integer value. + */ +goog.math.Integer.prototype.shorten = function(numBits) { + var arr_index = (numBits - 1) >> 5; + var bit_index = (numBits - 1) % 32; + var bits = []; + for (var i = 0; i < arr_index; i++) { + bits[i] = this.getBits(i); + } + var sigBits = bit_index == 31 ? 0xFFFFFFFF : (1 << (bit_index + 1)) - 1; + var val = this.getBits(arr_index) & sigBits; + if (val & (1 << bit_index)) { + val |= 0xFFFFFFFF - sigBits; + bits[arr_index] = val; + return new goog.math.Integer(bits, -1); + } else { + bits[arr_index] = val; + return new goog.math.Integer(bits, 0); + } +}; + + +/** @return {!goog.math.Integer} The negation of this value. */ +goog.math.Integer.prototype.negate = function() { + return this.not().add(goog.math.Integer.ONE); +}; + + +/** + * Returns the sum of this and the given Integer. + * @param {goog.math.Integer} other The Integer to add to this. + * @return {!goog.math.Integer} The Integer result. + */ +goog.math.Integer.prototype.add = function(other) { + var len = Math.max(this.bits_.length, other.bits_.length); + var arr = []; + var carry = 0; + + for (var i = 0; i <= len; i++) { + var a1 = this.getBits(i) >>> 16; + var a0 = this.getBits(i) & 0xFFFF; + + var b1 = other.getBits(i) >>> 16; + var b0 = other.getBits(i) & 0xFFFF; + + var c0 = carry + a0 + b0; + var c1 = (c0 >>> 16) + a1 + b1; + carry = c1 >>> 16; + c0 &= 0xFFFF; + c1 &= 0xFFFF; + arr[i] = (c1 << 16) | c0; + } + return goog.math.Integer.fromBits(arr); +}; + + +/** + * Returns the difference of this and the given Integer. + * @param {goog.math.Integer} other The Integer to subtract from this. + * @return {!goog.math.Integer} The Integer result. + */ +goog.math.Integer.prototype.subtract = function(other) { + return this.add(other.negate()); +}; + + +/** + * Returns the product of this and the given Integer. + * @param {goog.math.Integer} other The Integer to multiply against this. + * @return {!goog.math.Integer} The product of this and the other. + */ +goog.math.Integer.prototype.multiply = function(other) { + if (this.isZero()) { + return goog.math.Integer.ZERO; + } else if (other.isZero()) { + return goog.math.Integer.ZERO; + } + + if (this.isNegative()) { + if (other.isNegative()) { + return this.negate().multiply(other.negate()); + } else { + return this.negate().multiply(other).negate(); + } + } else if (other.isNegative()) { + return this.multiply(other.negate()).negate(); + } + + // If both numbers are small, use float multiplication + if (this.lessThan(goog.math.Integer.TWO_PWR_24_) && + other.lessThan(goog.math.Integer.TWO_PWR_24_)) { + return goog.math.Integer.fromNumber(this.toNumber() * other.toNumber()); + } + + // Fill in an array of 16-bit products. + var len = this.bits_.length + other.bits_.length; + var arr = []; + for (var i = 0; i < 2 * len; i++) { + arr[i] = 0; + } + for (var i = 0; i < this.bits_.length; i++) { + for (var j = 0; j < other.bits_.length; j++) { + var a1 = this.getBits(i) >>> 16; + var a0 = this.getBits(i) & 0xFFFF; + + var b1 = other.getBits(j) >>> 16; + var b0 = other.getBits(j) & 0xFFFF; + + arr[2 * i + 2 * j] += a0 * b0; + goog.math.Integer.carry16_(arr, 2 * i + 2 * j); + arr[2 * i + 2 * j + 1] += a1 * b0; + goog.math.Integer.carry16_(arr, 2 * i + 2 * j + 1); + arr[2 * i + 2 * j + 1] += a0 * b1; + goog.math.Integer.carry16_(arr, 2 * i + 2 * j + 1); + arr[2 * i + 2 * j + 2] += a1 * b1; + goog.math.Integer.carry16_(arr, 2 * i + 2 * j + 2); + } + } + + // Combine the 16-bit values into 32-bit values. + for (var i = 0; i < len; i++) { + arr[i] = (arr[2 * i + 1] << 16) | arr[2 * i]; + } + for (var i = len; i < 2 * len; i++) { + arr[i] = 0; + } + return new goog.math.Integer(arr, 0); +}; + + +/** + * Carries any overflow from the given index into later entries. + * @param {Array<number>} bits Array of 16-bit values in little-endian order. + * @param {number} index The index in question. + * @private + */ +goog.math.Integer.carry16_ = function(bits, index) { + while ((bits[index] & 0xFFFF) != bits[index]) { + bits[index + 1] += bits[index] >>> 16; + bits[index] &= 0xFFFF; + } +}; + + +/** + * Returns "this" Integer divided by the given one. Both "this" and the given + * Integer MUST be positive. + * + * This method is only needed for very large numbers (>10^308), + * for which the original division algorithm gets into an infinite + * loop (see https://github.com/google/closure-library/issues/500). + * + * The algorithm has some possible performance enhancements (or + * could be rewritten entirely), it's just an initial solution for + * the issue linked above. + * + * @param {!goog.math.Integer} other The Integer to divide "this" by. + * @return {!goog.math.Integer} "this" value divided by the given one. + * @private + */ +goog.math.Integer.prototype.slowDivide_ = function(other) { + if (this.isNegative() || other.isNegative()) { + throw Error('slowDivide_ only works with positive integers.'); + } + + var twoPower = goog.math.Integer.ONE; + var multiple = other; + + // First we have to figure out what the highest bit of the result + // is, so we increase "twoPower" and "multiple" until "multiple" + // exceeds "this". + while (multiple.lessThanOrEqual(this)) { + twoPower = twoPower.shiftLeft(1); + multiple = multiple.shiftLeft(1); + } + + // Rewind by one power of two, giving us the highest bit of the + // result. + var res = twoPower.shiftRight(1); + var total = multiple.shiftRight(1); + + // Now we starting decreasing "multiple" and "twoPower" to find the + // rest of the bits of the result. + var total2; + multiple = multiple.shiftRight(2); + twoPower = twoPower.shiftRight(2); + while (!multiple.isZero()) { + // whenever we can add "multiple" to the total and not exceed + // "this", that means we've found a 1 bit. Else we've found a 0 + // and don't need to add to the result. + total2 = total.add(multiple); + if (total2.lessThanOrEqual(this)) { + res = res.add(twoPower); + total = total2; + } + multiple = multiple.shiftRight(1); + twoPower = twoPower.shiftRight(1); + } + return res; +}; + + +/** + * Returns this Integer divided by the given one. + * @param {!goog.math.Integer} other The Integer to divide this by. + * @return {!goog.math.Integer} This value divided by the given one. + */ +goog.math.Integer.prototype.divide = function(other) { + if (other.isZero()) { + throw Error('division by zero'); + } else if (this.isZero()) { + return goog.math.Integer.ZERO; + } + + if (this.isNegative()) { + if (other.isNegative()) { + return this.negate().divide(other.negate()); + } else { + return this.negate().divide(other).negate(); + } + } else if (other.isNegative()) { + return this.divide(other.negate()).negate(); + } + + // Have to degrade to slowDivide for Very Large Numbers, because + // they're out of range for the floating-point approximation + // technique used below. + if (this.bits_.length > 30) { + return this.slowDivide_(other); + } + + // Repeat the following until the remainder is less than other: find a + // floating-point that approximates remainder / other *from below*, add this + // into the result, and subtract it from the remainder. It is critical that + // the approximate value is less than or equal to the real value so that the + // remainder never becomes negative. + var res = goog.math.Integer.ZERO; + var rem = this; + while (rem.greaterThanOrEqual(other)) { + // Approximate the result of division. This may be a little greater or + // smaller than the actual value. + var approx = Math.max(1, Math.floor(rem.toNumber() / other.toNumber())); + + // We will tweak the approximate result by changing it in the 48-th digit or + // the smallest non-fractional digit, whichever is larger. + var log2 = Math.ceil(Math.log(approx) / Math.LN2); + var delta = (log2 <= 48) ? 1 : Math.pow(2, log2 - 48); + + // Decrease the approximation until it is smaller than the remainder. Note + // that if it is too large, the product overflows and is negative. + var approxRes = goog.math.Integer.fromNumber(approx); + var approxRem = approxRes.multiply(other); + while (approxRem.isNegative() || approxRem.greaterThan(rem)) { + approx -= delta; + approxRes = goog.math.Integer.fromNumber(approx); + approxRem = approxRes.multiply(other); + } + + // We know the answer can't be zero... and actually, zero would cause + // infinite recursion since we would make no progress. + if (approxRes.isZero()) { + approxRes = goog.math.Integer.ONE; + } + + res = res.add(approxRes); + rem = rem.subtract(approxRem); + } + return res; +}; + + +/** + * Returns this Integer modulo the given one. + * @param {!goog.math.Integer} other The Integer by which to mod. + * @return {!goog.math.Integer} This value modulo the given one. + */ +goog.math.Integer.prototype.modulo = function(other) { + return this.subtract(this.divide(other).multiply(other)); +}; + + +/** @return {!goog.math.Integer} The bitwise-NOT of this value. */ +goog.math.Integer.prototype.not = function() { + var len = this.bits_.length; + var arr = []; + for (var i = 0; i < len; i++) { + arr[i] = ~this.bits_[i]; + } + return new goog.math.Integer(arr, ~this.sign_); +}; + + +/** + * Returns the bitwise-AND of this Integer and the given one. + * @param {goog.math.Integer} other The Integer to AND with this. + * @return {!goog.math.Integer} The bitwise-AND of this and the other. + */ +goog.math.Integer.prototype.and = function(other) { + var len = Math.max(this.bits_.length, other.bits_.length); + var arr = []; + for (var i = 0; i < len; i++) { + arr[i] = this.getBits(i) & other.getBits(i); + } + return new goog.math.Integer(arr, this.sign_ & other.sign_); +}; + + +/** + * Returns the bitwise-OR of this Integer and the given one. + * @param {goog.math.Integer} other The Integer to OR with this. + * @return {!goog.math.Integer} The bitwise-OR of this and the other. + */ +goog.math.Integer.prototype.or = function(other) { + var len = Math.max(this.bits_.length, other.bits_.length); + var arr = []; + for (var i = 0; i < len; i++) { + arr[i] = this.getBits(i) | other.getBits(i); + } + return new goog.math.Integer(arr, this.sign_ | other.sign_); +}; + + +/** + * Returns the bitwise-XOR of this Integer and the given one. + * @param {goog.math.Integer} other The Integer to XOR with this. + * @return {!goog.math.Integer} The bitwise-XOR of this and the other. + */ +goog.math.Integer.prototype.xor = function(other) { + var len = Math.max(this.bits_.length, other.bits_.length); + var arr = []; + for (var i = 0; i < len; i++) { + arr[i] = this.getBits(i) ^ other.getBits(i); + } + return new goog.math.Integer(arr, this.sign_ ^ other.sign_); +}; + + +/** + * Returns this value with bits shifted to the left by the given amount. + * @param {number} numBits The number of bits by which to shift. + * @return {!goog.math.Integer} This shifted to the left by the given amount. + */ +goog.math.Integer.prototype.shiftLeft = function(numBits) { + var arr_delta = numBits >> 5; + var bit_delta = numBits % 32; + var len = this.bits_.length + arr_delta + (bit_delta > 0 ? 1 : 0); + var arr = []; + for (var i = 0; i < len; i++) { + if (bit_delta > 0) { + arr[i] = (this.getBits(i - arr_delta) << bit_delta) | + (this.getBits(i - arr_delta - 1) >>> (32 - bit_delta)); + } else { + arr[i] = this.getBits(i - arr_delta); + } + } + return new goog.math.Integer(arr, this.sign_); +}; + + +/** + * Returns this value with bits shifted to the right by the given amount. + * @param {number} numBits The number of bits by which to shift. + * @return {!goog.math.Integer} This shifted to the right by the given amount. + */ +goog.math.Integer.prototype.shiftRight = function(numBits) { + var arr_delta = numBits >> 5; + var bit_delta = numBits % 32; + var len = this.bits_.length - arr_delta; + var arr = []; + for (var i = 0; i < len; i++) { + if (bit_delta > 0) { + arr[i] = (this.getBits(i + arr_delta) >>> bit_delta) | + (this.getBits(i + arr_delta + 1) << (32 - bit_delta)); + } else { + arr[i] = this.getBits(i + arr_delta); + } + } + return new goog.math.Integer(arr, this.sign_); +}; diff --git a/srv/src/http/static/viz/1/goog/math/long.js b/srv/src/http/static/viz/1/goog/math/long.js new file mode 100644 index 0000000..a43ea3f --- /dev/null +++ b/srv/src/http/static/viz/1/goog/math/long.js @@ -0,0 +1,843 @@ +// Copyright 2009 The Closure Library Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS-IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +/** + * @fileoverview Defines a Long class for representing a 64-bit two's-complement + * integer value, which faithfully simulates the behavior of a Java "long". This + * implementation is derived from LongLib in GWT. + * + */ + +goog.provide('goog.math.Long'); + +goog.require('goog.reflect'); + + + +/** + * Constructs a 64-bit two's-complement integer, given its low and high 32-bit + * values as *signed* integers. See the from* functions below for more + * convenient ways of constructing Longs. + * + * The internal representation of a long is the two given signed, 32-bit values. + * We use 32-bit pieces because these are the size of integers on which + * Javascript performs bit-operations. For operations like addition and + * multiplication, we split each number into 16-bit pieces, which can easily be + * multiplied within Javascript's floating-point representation without overflow + * or change in sign. + * + * In the algorithms below, we frequently reduce the negative case to the + * positive case by negating the input(s) and then post-processing the result. + * Note that we must ALWAYS check specially whether those values are MIN_VALUE + * (-2^63) because -MIN_VALUE == MIN_VALUE (since 2^63 cannot be represented as + * a positive number, it overflows back into a negative). Not handling this + * case would often result in infinite recursion. + * + * @param {number} low The low (signed) 32 bits of the long. + * @param {number} high The high (signed) 32 bits of the long. + * @struct + * @constructor + * @final + */ +goog.math.Long = function(low, high) { + /** + * @type {number} + * @private + */ + this.low_ = low | 0; // force into 32 signed bits. + + /** + * @type {number} + * @private + */ + this.high_ = high | 0; // force into 32 signed bits. +}; + + +// NOTE: Common constant values ZERO, ONE, NEG_ONE, etc. are defined below the +// from* methods on which they depend. + + +/** + * A cache of the Long representations of small integer values. + * @type {!Object<number, !goog.math.Long>} + * @private + */ +goog.math.Long.IntCache_ = {}; + + +/** + * A cache of the Long representations of common values. + * @type {!Object<goog.math.Long.ValueCacheId_, !goog.math.Long>} + * @private + */ +goog.math.Long.valueCache_ = {}; + + +/** + * Returns a Long representing the given (32-bit) integer value. + * @param {number} value The 32-bit integer in question. + * @return {!goog.math.Long} The corresponding Long value. + */ +goog.math.Long.fromInt = function(value) { + if (-128 <= value && value < 128) { + return goog.reflect.cache(goog.math.Long.IntCache_, value, function(val) { + return new goog.math.Long(val | 0, val < 0 ? -1 : 0); + }); + } else { + return new goog.math.Long(value | 0, value < 0 ? -1 : 0); + } +}; + + +/** + * Returns a Long representing the given value. + * NaN will be returned as zero. Infinity is converted to max value and + * -Infinity to min value. + * @param {number} value The number in question. + * @return {!goog.math.Long} The corresponding Long value. + */ +goog.math.Long.fromNumber = function(value) { + if (isNaN(value)) { + return goog.math.Long.getZero(); + } else if (value <= -goog.math.Long.TWO_PWR_63_DBL_) { + return goog.math.Long.getMinValue(); + } else if (value + 1 >= goog.math.Long.TWO_PWR_63_DBL_) { + return goog.math.Long.getMaxValue(); + } else if (value < 0) { + return goog.math.Long.fromNumber(-value).negate(); + } else { + return new goog.math.Long( + (value % goog.math.Long.TWO_PWR_32_DBL_) | 0, + (value / goog.math.Long.TWO_PWR_32_DBL_) | 0); + } +}; + + +/** + * 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. + * @param {number} lowBits The low 32-bits. + * @param {number} highBits The high 32-bits. + * @return {!goog.math.Long} The corresponding Long value. + */ +goog.math.Long.fromBits = function(lowBits, highBits) { + return new goog.math.Long(lowBits, highBits); +}; + + +/** + * Returns a Long representation of the given string, written using the given + * radix. + * @param {string} str The textual representation of the Long. + * @param {number=} opt_radix The radix in which the text is written. + * @return {!goog.math.Long} The corresponding Long value. + */ +goog.math.Long.fromString = function(str, opt_radix) { + if (str.length == 0) { + throw Error('number format error: empty string'); + } + + var radix = opt_radix || 10; + if (radix < 2 || 36 < radix) { + throw Error('radix out of range: ' + radix); + } + + if (str.charAt(0) == '-') { + return goog.math.Long.fromString(str.substring(1), radix).negate(); + } else if (str.indexOf('-') >= 0) { + throw Error('number format error: interior "-" character: ' + str); + } + + // Do several (8) digits each time through the loop, so as to + // minimize the calls to the very expensive emulated div. + var radixToPower = goog.math.Long.fromNumber(Math.pow(radix, 8)); + + var result = goog.math.Long.getZero(); + for (var i = 0; i < str.length; i += 8) { + var size = Math.min(8, str.length - i); + var value = parseInt(str.substring(i, i + size), radix); + if (size < 8) { + var power = goog.math.Long.fromNumber(Math.pow(radix, size)); + result = result.multiply(power).add(goog.math.Long.fromNumber(value)); + } else { + result = result.multiply(radixToPower); + result = result.add(goog.math.Long.fromNumber(value)); + } + } + return result; +}; + + +// NOTE: the compiler should inline these constant values below and then remove +// these variables, so there should be no runtime penalty for these. + + +/** + * Number used repeated below in calculations. This must appear before the + * first call to any from* function below. + * @type {number} + * @private + */ +goog.math.Long.TWO_PWR_16_DBL_ = 1 << 16; + + +/** + * @type {number} + * @private + */ +goog.math.Long.TWO_PWR_32_DBL_ = + goog.math.Long.TWO_PWR_16_DBL_ * goog.math.Long.TWO_PWR_16_DBL_; + + +/** + * @type {number} + * @private + */ +goog.math.Long.TWO_PWR_64_DBL_ = + goog.math.Long.TWO_PWR_32_DBL_ * goog.math.Long.TWO_PWR_32_DBL_; + + +/** + * @type {number} + * @private + */ +goog.math.Long.TWO_PWR_63_DBL_ = goog.math.Long.TWO_PWR_64_DBL_ / 2; + + +/** + * @return {!goog.math.Long} + * @public + */ +goog.math.Long.getZero = function() { + return goog.reflect.cache( + goog.math.Long.valueCache_, goog.math.Long.ValueCacheId_.ZERO, + function() { return goog.math.Long.fromInt(0); }); +}; + + +/** + * @return {!goog.math.Long} + * @public + */ +goog.math.Long.getOne = function() { + return goog.reflect.cache( + goog.math.Long.valueCache_, goog.math.Long.ValueCacheId_.ONE, + function() { return goog.math.Long.fromInt(1); }); +}; + + +/** + * @return {!goog.math.Long} + * @public + */ +goog.math.Long.getNegOne = function() { + return goog.reflect.cache( + goog.math.Long.valueCache_, goog.math.Long.ValueCacheId_.NEG_ONE, + function() { return goog.math.Long.fromInt(-1); }); +}; + + +/** + * @return {!goog.math.Long} + * @public + */ +goog.math.Long.getMaxValue = function() { + return goog.reflect.cache( + goog.math.Long.valueCache_, goog.math.Long.ValueCacheId_.MAX_VALUE, + function() { + return goog.math.Long.fromBits(0xFFFFFFFF | 0, 0x7FFFFFFF | 0); + }); +}; + + +/** + * @return {!goog.math.Long} + * @public + */ +goog.math.Long.getMinValue = function() { + return goog.reflect.cache( + goog.math.Long.valueCache_, goog.math.Long.ValueCacheId_.MIN_VALUE, + function() { return goog.math.Long.fromBits(0, 0x80000000 | 0); }); +}; + + +/** + * @return {!goog.math.Long} + * @public + */ +goog.math.Long.getTwoPwr24 = function() { + return goog.reflect.cache( + goog.math.Long.valueCache_, goog.math.Long.ValueCacheId_.TWO_PWR_24, + function() { return goog.math.Long.fromInt(1 << 24); }); +}; + + +/** @return {number} The value, assuming it is a 32-bit integer. */ +goog.math.Long.prototype.toInt = function() { + return this.low_; +}; + + +/** @return {number} The closest floating-point representation to this value. */ +goog.math.Long.prototype.toNumber = function() { + return this.high_ * goog.math.Long.TWO_PWR_32_DBL_ + + this.getLowBitsUnsigned(); +}; + + +/** + * @param {number=} opt_radix The radix in which the text should be written. + * @return {string} The textual representation of this value. + * @override + */ +goog.math.Long.prototype.toString = function(opt_radix) { + var radix = opt_radix || 10; + if (radix < 2 || 36 < radix) { + throw Error('radix out of range: ' + radix); + } + + if (this.isZero()) { + return '0'; + } + + if (this.isNegative()) { + if (this.equals(goog.math.Long.getMinValue())) { + // We need to change the Long value before it can be negated, so we remove + // the bottom-most digit in this base and then recurse to do the rest. + var radixLong = goog.math.Long.fromNumber(radix); + var div = this.div(radixLong); + var rem = div.multiply(radixLong).subtract(this); + return div.toString(radix) + rem.toInt().toString(radix); + } else { + return '-' + this.negate().toString(radix); + } + } + + // Do several (6) digits each time through the loop, so as to + // minimize the calls to the very expensive emulated div. + var radixToPower = goog.math.Long.fromNumber(Math.pow(radix, 6)); + + var rem = this; + var result = ''; + while (true) { + var remDiv = rem.div(radixToPower); + // The right shifting fixes negative values in the case when + // intval >= 2^31; for more details see + // https://github.com/google/closure-library/pull/498 + var intval = rem.subtract(remDiv.multiply(radixToPower)).toInt() >>> 0; + var digits = intval.toString(radix); + + rem = remDiv; + if (rem.isZero()) { + return digits + result; + } else { + while (digits.length < 6) { + digits = '0' + digits; + } + result = '' + digits + result; + } + } +}; + + +/** @return {number} The high 32-bits as a signed value. */ +goog.math.Long.prototype.getHighBits = function() { + return this.high_; +}; + + +/** @return {number} The low 32-bits as a signed value. */ +goog.math.Long.prototype.getLowBits = function() { + return this.low_; +}; + + +/** @return {number} The low 32-bits as an unsigned value. */ +goog.math.Long.prototype.getLowBitsUnsigned = function() { + return (this.low_ >= 0) ? this.low_ : + goog.math.Long.TWO_PWR_32_DBL_ + this.low_; +}; + + +/** + * @return {number} Returns the number of bits needed to represent the absolute + * value of this Long. + */ +goog.math.Long.prototype.getNumBitsAbs = function() { + if (this.isNegative()) { + if (this.equals(goog.math.Long.getMinValue())) { + return 64; + } else { + return this.negate().getNumBitsAbs(); + } + } else { + var val = this.high_ != 0 ? this.high_ : this.low_; + for (var bit = 31; bit > 0; bit--) { + if ((val & (1 << bit)) != 0) { + break; + } + } + return this.high_ != 0 ? bit + 33 : bit + 1; + } +}; + + +/** @return {boolean} Whether this value is zero. */ +goog.math.Long.prototype.isZero = function() { + return this.high_ == 0 && this.low_ == 0; +}; + + +/** @return {boolean} Whether this value is negative. */ +goog.math.Long.prototype.isNegative = function() { + return this.high_ < 0; +}; + + +/** @return {boolean} Whether this value is odd. */ +goog.math.Long.prototype.isOdd = function() { + return (this.low_ & 1) == 1; +}; + + +/** + * @param {goog.math.Long} other Long to compare against. + * @return {boolean} Whether this Long equals the other. + */ +goog.math.Long.prototype.equals = function(other) { + return (this.high_ == other.high_) && (this.low_ == other.low_); +}; + + +/** + * @param {goog.math.Long} other Long to compare against. + * @return {boolean} Whether this Long does not equal the other. + */ +goog.math.Long.prototype.notEquals = function(other) { + return (this.high_ != other.high_) || (this.low_ != other.low_); +}; + + +/** + * @param {goog.math.Long} other Long to compare against. + * @return {boolean} Whether this Long is less than the other. + */ +goog.math.Long.prototype.lessThan = function(other) { + return this.compare(other) < 0; +}; + + +/** + * @param {goog.math.Long} other Long to compare against. + * @return {boolean} Whether this Long is less than or equal to the other. + */ +goog.math.Long.prototype.lessThanOrEqual = function(other) { + return this.compare(other) <= 0; +}; + + +/** + * @param {goog.math.Long} other Long to compare against. + * @return {boolean} Whether this Long is greater than the other. + */ +goog.math.Long.prototype.greaterThan = function(other) { + return this.compare(other) > 0; +}; + + +/** + * @param {goog.math.Long} other Long to compare against. + * @return {boolean} Whether this Long is greater than or equal to the other. + */ +goog.math.Long.prototype.greaterThanOrEqual = function(other) { + return this.compare(other) >= 0; +}; + + +/** + * Compares this Long with the given one. + * @param {goog.math.Long} other Long to compare against. + * @return {number} 0 if they are the same, 1 if the this is greater, and -1 + * if the given one is greater. + */ +goog.math.Long.prototype.compare = function(other) { + if (this.equals(other)) { + return 0; + } + + var thisNeg = this.isNegative(); + var otherNeg = other.isNegative(); + if (thisNeg && !otherNeg) { + return -1; + } + if (!thisNeg && otherNeg) { + return 1; + } + + // at this point, the signs are the same, so subtraction will not overflow + if (this.subtract(other).isNegative()) { + return -1; + } else { + return 1; + } +}; + + +/** @return {!goog.math.Long} The negation of this value. */ +goog.math.Long.prototype.negate = function() { + if (this.equals(goog.math.Long.getMinValue())) { + return goog.math.Long.getMinValue(); + } else { + return this.not().add(goog.math.Long.getOne()); + } +}; + + +/** + * Returns the sum of this and the given Long. + * @param {goog.math.Long} other Long to add to this one. + * @return {!goog.math.Long} The sum of this and the given Long. + */ +goog.math.Long.prototype.add = function(other) { + // Divide each number into 4 chunks of 16 bits, and then sum the chunks. + + var a48 = this.high_ >>> 16; + var a32 = this.high_ & 0xFFFF; + var a16 = this.low_ >>> 16; + var a00 = this.low_ & 0xFFFF; + + var b48 = other.high_ >>> 16; + var b32 = other.high_ & 0xFFFF; + var b16 = other.low_ >>> 16; + var b00 = other.low_ & 0xFFFF; + + var c48 = 0, c32 = 0, c16 = 0, c00 = 0; + c00 += a00 + b00; + c16 += c00 >>> 16; + c00 &= 0xFFFF; + c16 += a16 + b16; + c32 += c16 >>> 16; + c16 &= 0xFFFF; + c32 += a32 + b32; + c48 += c32 >>> 16; + c32 &= 0xFFFF; + c48 += a48 + b48; + c48 &= 0xFFFF; + return goog.math.Long.fromBits((c16 << 16) | c00, (c48 << 16) | c32); +}; + + +/** + * Returns the difference of this and the given Long. + * @param {goog.math.Long} other Long to subtract from this. + * @return {!goog.math.Long} The difference of this and the given Long. + */ +goog.math.Long.prototype.subtract = function(other) { + return this.add(other.negate()); +}; + + +/** + * Returns the product of this and the given long. + * @param {goog.math.Long} other Long to multiply with this. + * @return {!goog.math.Long} The product of this and the other. + */ +goog.math.Long.prototype.multiply = function(other) { + if (this.isZero()) { + return goog.math.Long.getZero(); + } else if (other.isZero()) { + return goog.math.Long.getZero(); + } + + if (this.equals(goog.math.Long.getMinValue())) { + return other.isOdd() ? goog.math.Long.getMinValue() : + goog.math.Long.getZero(); + } else if (other.equals(goog.math.Long.getMinValue())) { + return this.isOdd() ? goog.math.Long.getMinValue() : + goog.math.Long.getZero(); + } + + if (this.isNegative()) { + if (other.isNegative()) { + return this.negate().multiply(other.negate()); + } else { + return this.negate().multiply(other).negate(); + } + } else if (other.isNegative()) { + return this.multiply(other.negate()).negate(); + } + + // If both longs are small, use float multiplication + if (this.lessThan(goog.math.Long.getTwoPwr24()) && + other.lessThan(goog.math.Long.getTwoPwr24())) { + return goog.math.Long.fromNumber(this.toNumber() * other.toNumber()); + } + + // Divide each long into 4 chunks of 16 bits, and then add up 4x4 products. + // We can skip products that would overflow. + + var a48 = this.high_ >>> 16; + var a32 = this.high_ & 0xFFFF; + var a16 = this.low_ >>> 16; + var a00 = this.low_ & 0xFFFF; + + var b48 = other.high_ >>> 16; + var b32 = other.high_ & 0xFFFF; + var b16 = other.low_ >>> 16; + var b00 = other.low_ & 0xFFFF; + + var c48 = 0, c32 = 0, c16 = 0, c00 = 0; + c00 += a00 * b00; + c16 += c00 >>> 16; + c00 &= 0xFFFF; + c16 += a16 * b00; + c32 += c16 >>> 16; + c16 &= 0xFFFF; + c16 += a00 * b16; + c32 += c16 >>> 16; + c16 &= 0xFFFF; + c32 += a32 * b00; + c48 += c32 >>> 16; + c32 &= 0xFFFF; + c32 += a16 * b16; + c48 += c32 >>> 16; + c32 &= 0xFFFF; + c32 += a00 * b32; + c48 += c32 >>> 16; + c32 &= 0xFFFF; + c48 += a48 * b00 + a32 * b16 + a16 * b32 + a00 * b48; + c48 &= 0xFFFF; + return goog.math.Long.fromBits((c16 << 16) | c00, (c48 << 16) | c32); +}; + + +/** + * Returns this Long divided by the given one. + * @param {goog.math.Long} other Long by which to divide. + * @return {!goog.math.Long} This Long divided by the given one. + */ +goog.math.Long.prototype.div = function(other) { + if (other.isZero()) { + throw Error('division by zero'); + } else if (this.isZero()) { + return goog.math.Long.getZero(); + } + + if (this.equals(goog.math.Long.getMinValue())) { + if (other.equals(goog.math.Long.getOne()) || + other.equals(goog.math.Long.getNegOne())) { + return goog.math.Long.getMinValue(); // recall -MIN_VALUE == MIN_VALUE + } else if (other.equals(goog.math.Long.getMinValue())) { + return goog.math.Long.getOne(); + } else { + // At this point, we have |other| >= 2, so |this/other| < |MIN_VALUE|. + var halfThis = this.shiftRight(1); + var approx = halfThis.div(other).shiftLeft(1); + if (approx.equals(goog.math.Long.getZero())) { + return other.isNegative() ? goog.math.Long.getOne() : + goog.math.Long.getNegOne(); + } else { + var rem = this.subtract(other.multiply(approx)); + var result = approx.add(rem.div(other)); + return result; + } + } + } else if (other.equals(goog.math.Long.getMinValue())) { + return goog.math.Long.getZero(); + } + + if (this.isNegative()) { + if (other.isNegative()) { + return this.negate().div(other.negate()); + } else { + return this.negate().div(other).negate(); + } + } else if (other.isNegative()) { + return this.div(other.negate()).negate(); + } + + // Repeat the following until the remainder is less than other: find a + // floating-point that approximates remainder / other *from below*, add this + // into the result, and subtract it from the remainder. It is critical that + // the approximate value is less than or equal to the real value so that the + // remainder never becomes negative. + var res = goog.math.Long.getZero(); + var rem = this; + while (rem.greaterThanOrEqual(other)) { + // Approximate the result of division. This may be a little greater or + // smaller than the actual value. + var approx = Math.max(1, Math.floor(rem.toNumber() / other.toNumber())); + + // We will tweak the approximate result by changing it in the 48-th digit or + // the smallest non-fractional digit, whichever is larger. + var log2 = Math.ceil(Math.log(approx) / Math.LN2); + var delta = (log2 <= 48) ? 1 : Math.pow(2, log2 - 48); + + // Decrease the approximation until it is smaller than the remainder. Note + // that if it is too large, the product overflows and is negative. + var approxRes = goog.math.Long.fromNumber(approx); + var approxRem = approxRes.multiply(other); + while (approxRem.isNegative() || approxRem.greaterThan(rem)) { + approx -= delta; + approxRes = goog.math.Long.fromNumber(approx); + approxRem = approxRes.multiply(other); + } + + // We know the answer can't be zero... and actually, zero would cause + // infinite recursion since we would make no progress. + if (approxRes.isZero()) { + approxRes = goog.math.Long.getOne(); + } + + res = res.add(approxRes); + rem = rem.subtract(approxRem); + } + return res; +}; + + +/** + * Returns this Long modulo the given one. + * @param {goog.math.Long} other Long by which to mod. + * @return {!goog.math.Long} This Long modulo the given one. + */ +goog.math.Long.prototype.modulo = function(other) { + return this.subtract(this.div(other).multiply(other)); +}; + + +/** @return {!goog.math.Long} The bitwise-NOT of this value. */ +goog.math.Long.prototype.not = function() { + return goog.math.Long.fromBits(~this.low_, ~this.high_); +}; + + +/** + * Returns the bitwise-AND of this Long and the given one. + * @param {goog.math.Long} other The Long with which to AND. + * @return {!goog.math.Long} The bitwise-AND of this and the other. + */ +goog.math.Long.prototype.and = function(other) { + return goog.math.Long.fromBits( + this.low_ & other.low_, this.high_ & other.high_); +}; + + +/** + * Returns the bitwise-OR of this Long and the given one. + * @param {goog.math.Long} other The Long with which to OR. + * @return {!goog.math.Long} The bitwise-OR of this and the other. + */ +goog.math.Long.prototype.or = function(other) { + return goog.math.Long.fromBits( + this.low_ | other.low_, this.high_ | other.high_); +}; + + +/** + * Returns the bitwise-XOR of this Long and the given one. + * @param {goog.math.Long} other The Long with which to XOR. + * @return {!goog.math.Long} The bitwise-XOR of this and the other. + */ +goog.math.Long.prototype.xor = function(other) { + return goog.math.Long.fromBits( + this.low_ ^ other.low_, this.high_ ^ other.high_); +}; + + +/** + * Returns this Long with bits shifted to the left by the given amount. + * @param {number} numBits The number of bits by which to shift. + * @return {!goog.math.Long} This shifted to the left by the given amount. + */ +goog.math.Long.prototype.shiftLeft = function(numBits) { + numBits &= 63; + if (numBits == 0) { + return this; + } else { + var low = this.low_; + if (numBits < 32) { + var high = this.high_; + return goog.math.Long.fromBits( + low << numBits, (high << numBits) | (low >>> (32 - numBits))); + } else { + return goog.math.Long.fromBits(0, low << (numBits - 32)); + } + } +}; + + +/** + * Returns this Long with bits shifted to the right by the given amount. + * The new leading bits match the current sign bit. + * @param {number} numBits The number of bits by which to shift. + * @return {!goog.math.Long} This shifted to the right by the given amount. + */ +goog.math.Long.prototype.shiftRight = function(numBits) { + numBits &= 63; + if (numBits == 0) { + return this; + } else { + var high = this.high_; + if (numBits < 32) { + var low = this.low_; + return goog.math.Long.fromBits( + (low >>> numBits) | (high << (32 - numBits)), high >> numBits); + } else { + return goog.math.Long.fromBits( + high >> (numBits - 32), high >= 0 ? 0 : -1); + } + } +}; + + +/** + * Returns this Long with bits shifted to the right by the given amount, with + * zeros placed into the new leading bits. + * @param {number} numBits The number of bits by which to shift. + * @return {!goog.math.Long} This shifted to the right by the given amount, with + * zeros placed into the new leading bits. + */ +goog.math.Long.prototype.shiftRightUnsigned = function(numBits) { + numBits &= 63; + if (numBits == 0) { + return this; + } else { + var high = this.high_; + if (numBits < 32) { + var low = this.low_; + return goog.math.Long.fromBits( + (low >>> numBits) | (high << (32 - numBits)), high >>> numBits); + } else if (numBits == 32) { + return goog.math.Long.fromBits(high, 0); + } else { + return goog.math.Long.fromBits(high >>> (numBits - 32), 0); + } + } +}; + + +/** + * @enum {number} Ids of commonly requested Long instances. + * @private + */ +goog.math.Long.ValueCacheId_ = { + MAX_VALUE: 1, + MIN_VALUE: 2, + ZERO: 3, + ONE: 4, + NEG_ONE: 5, + TWO_PWR_24: 6 +}; diff --git a/srv/src/http/static/viz/1/goog/math/math.js b/srv/src/http/static/viz/1/goog/math/math.js new file mode 100644 index 0000000..95e5fb5 --- /dev/null +++ b/srv/src/http/static/viz/1/goog/math/math.js @@ -0,0 +1,447 @@ +// Copyright 2006 The Closure Library Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS-IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +/** + * @fileoverview Additional mathematical functions. + */ + +goog.provide('goog.math'); + +goog.require('goog.array'); +goog.require('goog.asserts'); + + +/** + * Returns a random integer greater than or equal to 0 and less than {@code a}. + * @param {number} a The upper bound for the random integer (exclusive). + * @return {number} A random integer N such that 0 <= N < a. + */ +goog.math.randomInt = function(a) { + return Math.floor(Math.random() * a); +}; + + +/** + * Returns a random number greater than or equal to {@code a} and less than + * {@code b}. + * @param {number} a The lower bound for the random number (inclusive). + * @param {number} b The upper bound for the random number (exclusive). + * @return {number} A random number N such that a <= N < b. + */ +goog.math.uniformRandom = function(a, b) { + return a + Math.random() * (b - a); +}; + + +/** + * Takes a number and clamps it to within the provided bounds. + * @param {number} value The input number. + * @param {number} min The minimum value to return. + * @param {number} max The maximum value to return. + * @return {number} The input number if it is within bounds, or the nearest + * number within the bounds. + */ +goog.math.clamp = function(value, min, max) { + return Math.min(Math.max(value, min), max); +}; + + +/** + * The % operator in JavaScript returns the remainder of a / b, but differs from + * some other languages in that the result will have the same sign as the + * dividend. For example, -1 % 8 == -1, whereas in some other languages + * (such as Python) the result would be 7. This function emulates the more + * correct modulo behavior, which is useful for certain applications such as + * calculating an offset index in a circular list. + * + * @param {number} a The dividend. + * @param {number} b The divisor. + * @return {number} a % b where the result is between 0 and b (either 0 <= x < b + * or b < x <= 0, depending on the sign of b). + */ +goog.math.modulo = function(a, b) { + var r = a % b; + // If r and b differ in sign, add b to wrap the result to the correct sign. + return (r * b < 0) ? r + b : r; +}; + + +/** + * Performs linear interpolation between values a and b. Returns the value + * between a and b proportional to x (when x is between 0 and 1. When x is + * outside this range, the return value is a linear extrapolation). + * @param {number} a A number. + * @param {number} b A number. + * @param {number} x The proportion between a and b. + * @return {number} The interpolated value between a and b. + */ +goog.math.lerp = function(a, b, x) { + return a + x * (b - a); +}; + + +/** + * Tests whether the two values are equal to each other, within a certain + * tolerance to adjust for floating point errors. + * @param {number} a A number. + * @param {number} b A number. + * @param {number=} opt_tolerance Optional tolerance range. Defaults + * to 0.000001. If specified, should be greater than 0. + * @return {boolean} Whether {@code a} and {@code b} are nearly equal. + */ +goog.math.nearlyEquals = function(a, b, opt_tolerance) { + return Math.abs(a - b) <= (opt_tolerance || 0.000001); +}; + + +// TODO(user): Rename to normalizeAngle, retaining old name as deprecated +// alias. +/** + * Normalizes an angle to be in range [0-360). Angles outside this range will + * be normalized to be the equivalent angle with that range. + * @param {number} angle Angle in degrees. + * @return {number} Standardized angle. + */ +goog.math.standardAngle = function(angle) { + return goog.math.modulo(angle, 360); +}; + + +/** + * Normalizes an angle to be in range [0-2*PI). Angles outside this range will + * be normalized to be the equivalent angle with that range. + * @param {number} angle Angle in radians. + * @return {number} Standardized angle. + */ +goog.math.standardAngleInRadians = function(angle) { + return goog.math.modulo(angle, 2 * Math.PI); +}; + + +/** + * Converts degrees to radians. + * @param {number} angleDegrees Angle in degrees. + * @return {number} Angle in radians. + */ +goog.math.toRadians = function(angleDegrees) { + return angleDegrees * Math.PI / 180; +}; + + +/** + * Converts radians to degrees. + * @param {number} angleRadians Angle in radians. + * @return {number} Angle in degrees. + */ +goog.math.toDegrees = function(angleRadians) { + return angleRadians * 180 / Math.PI; +}; + + +/** + * For a given angle and radius, finds the X portion of the offset. + * @param {number} degrees Angle in degrees (zero points in +X direction). + * @param {number} radius Radius. + * @return {number} The x-distance for the angle and radius. + */ +goog.math.angleDx = function(degrees, radius) { + return radius * Math.cos(goog.math.toRadians(degrees)); +}; + + +/** + * For a given angle and radius, finds the Y portion of the offset. + * @param {number} degrees Angle in degrees (zero points in +X direction). + * @param {number} radius Radius. + * @return {number} The y-distance for the angle and radius. + */ +goog.math.angleDy = function(degrees, radius) { + return radius * Math.sin(goog.math.toRadians(degrees)); +}; + + +/** + * Computes the angle between two points (x1,y1) and (x2,y2). + * Angle zero points in the +X direction, 90 degrees points in the +Y + * direction (down) and from there we grow clockwise towards 360 degrees. + * @param {number} x1 x of first point. + * @param {number} y1 y of first point. + * @param {number} x2 x of second point. + * @param {number} y2 y of second point. + * @return {number} Standardized angle in degrees of the vector from + * x1,y1 to x2,y2. + */ +goog.math.angle = function(x1, y1, x2, y2) { + return goog.math.standardAngle( + goog.math.toDegrees(Math.atan2(y2 - y1, x2 - x1))); +}; + + +/** + * Computes the difference between startAngle and endAngle (angles in degrees). + * @param {number} startAngle Start angle in degrees. + * @param {number} endAngle End angle in degrees. + * @return {number} The number of degrees that when added to + * startAngle will result in endAngle. Positive numbers mean that the + * direction is clockwise. Negative numbers indicate a counter-clockwise + * direction. + * The shortest route (clockwise vs counter-clockwise) between the angles + * is used. + * When the difference is 180 degrees, the function returns 180 (not -180) + * angleDifference(30, 40) is 10, and angleDifference(40, 30) is -10. + * angleDifference(350, 10) is 20, and angleDifference(10, 350) is -20. + */ +goog.math.angleDifference = function(startAngle, endAngle) { + var d = + goog.math.standardAngle(endAngle) - goog.math.standardAngle(startAngle); + if (d > 180) { + d = d - 360; + } else if (d <= -180) { + d = 360 + d; + } + return d; +}; + + +/** + * Returns the sign of a number as per the "sign" or "signum" function. + * @param {number} x The number to take the sign of. + * @return {number} -1 when negative, 1 when positive, 0 when 0. Preserves + * signed zeros and NaN. + */ +goog.math.sign = Math.sign || function(x) { + if (x > 0) { + return 1; + } + if (x < 0) { + return -1; + } + return x; // Preserves signed zeros and NaN. +}; + + +/** + * JavaScript implementation of Longest Common Subsequence problem. + * http://en.wikipedia.org/wiki/Longest_common_subsequence + * + * Returns the longest possible array that is subarray of both of given arrays. + * + * @param {IArrayLike<S>} array1 First array of objects. + * @param {IArrayLike<T>} array2 Second array of objects. + * @param {Function=} opt_compareFn Function that acts as a custom comparator + * for the array ojects. Function should return true if objects are equal, + * otherwise false. + * @param {Function=} opt_collectorFn Function used to decide what to return + * as a result subsequence. It accepts 2 arguments: index of common element + * in the first array and index in the second. The default function returns + * element from the first array. + * @return {!Array<S|T>} A list of objects that are common to both arrays + * such that there is no common subsequence with size greater than the + * length of the list. + * @template S,T + */ +goog.math.longestCommonSubsequence = function( + array1, array2, opt_compareFn, opt_collectorFn) { + + var compare = opt_compareFn || function(a, b) { return a == b; }; + + var collect = opt_collectorFn || function(i1, i2) { return array1[i1]; }; + + var length1 = array1.length; + var length2 = array2.length; + + var arr = []; + for (var i = 0; i < length1 + 1; i++) { + arr[i] = []; + arr[i][0] = 0; + } + + for (var j = 0; j < length2 + 1; j++) { + arr[0][j] = 0; + } + + for (i = 1; i <= length1; i++) { + for (j = 1; j <= length2; j++) { + if (compare(array1[i - 1], array2[j - 1])) { + arr[i][j] = arr[i - 1][j - 1] + 1; + } else { + arr[i][j] = Math.max(arr[i - 1][j], arr[i][j - 1]); + } + } + } + + // Backtracking + var result = []; + var i = length1, j = length2; + while (i > 0 && j > 0) { + if (compare(array1[i - 1], array2[j - 1])) { + result.unshift(collect(i - 1, j - 1)); + i--; + j--; + } else { + if (arr[i - 1][j] > arr[i][j - 1]) { + i--; + } else { + j--; + } + } + } + + return result; +}; + + +/** + * Returns the sum of the arguments. + * @param {...number} var_args Numbers to add. + * @return {number} The sum of the arguments (0 if no arguments were provided, + * {@code NaN} if any of the arguments is not a valid number). + */ +goog.math.sum = function(var_args) { + return /** @type {number} */ ( + goog.array.reduce( + arguments, function(sum, value) { return sum + value; }, 0)); +}; + + +/** + * Returns the arithmetic mean of the arguments. + * @param {...number} var_args Numbers to average. + * @return {number} The average of the arguments ({@code NaN} if no arguments + * were provided or any of the arguments is not a valid number). + */ +goog.math.average = function(var_args) { + return goog.math.sum.apply(null, arguments) / arguments.length; +}; + + +/** + * Returns the unbiased sample variance of the arguments. For a definition, + * see e.g. http://en.wikipedia.org/wiki/Variance + * @param {...number} var_args Number samples to analyze. + * @return {number} The unbiased sample variance of the arguments (0 if fewer + * than two samples were provided, or {@code NaN} if any of the samples is + * not a valid number). + */ +goog.math.sampleVariance = function(var_args) { + var sampleSize = arguments.length; + if (sampleSize < 2) { + return 0; + } + + var mean = goog.math.average.apply(null, arguments); + var variance = + goog.math.sum.apply(null, goog.array.map(arguments, function(val) { + return Math.pow(val - mean, 2); + })) / (sampleSize - 1); + + return variance; +}; + + +/** + * Returns the sample standard deviation of the arguments. For a definition of + * sample standard deviation, see e.g. + * http://en.wikipedia.org/wiki/Standard_deviation + * @param {...number} var_args Number samples to analyze. + * @return {number} The sample standard deviation of the arguments (0 if fewer + * than two samples were provided, or {@code NaN} if any of the samples is + * not a valid number). + */ +goog.math.standardDeviation = function(var_args) { + return Math.sqrt(goog.math.sampleVariance.apply(null, arguments)); +}; + + +/** + * Returns whether the supplied number represents an integer, i.e. that is has + * no fractional component. No range-checking is performed on the number. + * @param {number} num The number to test. + * @return {boolean} Whether {@code num} is an integer. + */ +goog.math.isInt = function(num) { + return isFinite(num) && num % 1 == 0; +}; + + +/** + * Returns whether the supplied number is finite and not NaN. + * @param {number} num The number to test. + * @return {boolean} Whether {@code num} is a finite number. + */ +goog.math.isFiniteNumber = function(num) { + return isFinite(num) && !isNaN(num); +}; + + +/** + * @param {number} num The number to test. + * @return {boolean} Whether it is negative zero. + */ +goog.math.isNegativeZero = function(num) { + return num == 0 && 1 / num < 0; +}; + + +/** + * Returns the precise value of floor(log10(num)). + * Simpler implementations didn't work because of floating point rounding + * errors. For example + * <ul> + * <li>Math.floor(Math.log(num) / Math.LN10) is off by one for num == 1e+3. + * <li>Math.floor(Math.log(num) * Math.LOG10E) is off by one for num == 1e+15. + * <li>Math.floor(Math.log10(num)) is off by one for num == 1e+15 - 1. + * </ul> + * @param {number} num A floating point number. + * @return {number} Its logarithm to base 10 rounded down to the nearest + * integer if num > 0. -Infinity if num == 0. NaN if num < 0. + */ +goog.math.log10Floor = function(num) { + if (num > 0) { + var x = Math.round(Math.log(num) * Math.LOG10E); + return x - (parseFloat('1e' + x) > num ? 1 : 0); + } + return num == 0 ? -Infinity : NaN; +}; + + +/** + * A tweaked variant of {@code Math.floor} which tolerates if the passed number + * is infinitesimally smaller than the closest integer. It often happens with + * the results of floating point calculations because of the finite precision + * of the intermediate results. For example {@code Math.floor(Math.log(1000) / + * Math.LN10) == 2}, not 3 as one would expect. + * @param {number} num A number. + * @param {number=} opt_epsilon An infinitesimally small positive number, the + * rounding error to tolerate. + * @return {number} The largest integer less than or equal to {@code num}. + */ +goog.math.safeFloor = function(num, opt_epsilon) { + goog.asserts.assert(!goog.isDef(opt_epsilon) || opt_epsilon > 0); + return Math.floor(num + (opt_epsilon || 2e-15)); +}; + + +/** + * A tweaked variant of {@code Math.ceil}. See {@code goog.math.safeFloor} for + * details. + * @param {number} num A number. + * @param {number=} opt_epsilon An infinitesimally small positive number, the + * rounding error to tolerate. + * @return {number} The smallest integer greater than or equal to {@code num}. + */ +goog.math.safeCeil = function(num, opt_epsilon) { + goog.asserts.assert(!goog.isDef(opt_epsilon) || opt_epsilon > 0); + return Math.ceil(num - (opt_epsilon || 2e-15)); +}; diff --git a/srv/src/http/static/viz/1/goog/math/size.js b/srv/src/http/static/viz/1/goog/math/size.js new file mode 100644 index 0000000..f5c379b --- /dev/null +++ b/srv/src/http/static/viz/1/goog/math/size.js @@ -0,0 +1,227 @@ +// Copyright 2007 The Closure Library Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS-IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +/** + * @fileoverview A utility class for representing two-dimensional sizes. + * @author brenneman@google.com (Shawn Brenneman) + */ + + +goog.provide('goog.math.Size'); + + + +/** + * Class for representing sizes consisting of a width and height. Undefined + * width and height support is deprecated and results in compiler warning. + * @param {number} width Width. + * @param {number} height Height. + * @struct + * @constructor + */ +goog.math.Size = function(width, height) { + /** + * Width + * @type {number} + */ + this.width = width; + + /** + * Height + * @type {number} + */ + this.height = height; +}; + + +/** + * Compares sizes for equality. + * @param {goog.math.Size} a A Size. + * @param {goog.math.Size} b A Size. + * @return {boolean} True iff the sizes have equal widths and equal + * heights, or if both are null. + */ +goog.math.Size.equals = function(a, b) { + if (a == b) { + return true; + } + if (!a || !b) { + return false; + } + return a.width == b.width && a.height == b.height; +}; + + +/** + * @return {!goog.math.Size} A new copy of the Size. + */ +goog.math.Size.prototype.clone = function() { + return new goog.math.Size(this.width, this.height); +}; + + +if (goog.DEBUG) { + /** + * Returns a nice string representing size. + * @return {string} In the form (50 x 73). + * @override + */ + goog.math.Size.prototype.toString = function() { + return '(' + this.width + ' x ' + this.height + ')'; + }; +} + + +/** + * @return {number} The longer of the two dimensions in the size. + */ +goog.math.Size.prototype.getLongest = function() { + return Math.max(this.width, this.height); +}; + + +/** + * @return {number} The shorter of the two dimensions in the size. + */ +goog.math.Size.prototype.getShortest = function() { + return Math.min(this.width, this.height); +}; + + +/** + * @return {number} The area of the size (width * height). + */ +goog.math.Size.prototype.area = function() { + return this.width * this.height; +}; + + +/** + * @return {number} The perimeter of the size (width + height) * 2. + */ +goog.math.Size.prototype.perimeter = function() { + return (this.width + this.height) * 2; +}; + + +/** + * @return {number} The ratio of the size's width to its height. + */ +goog.math.Size.prototype.aspectRatio = function() { + return this.width / this.height; +}; + + +/** + * @return {boolean} True if the size has zero area, false if both dimensions + * are non-zero numbers. + */ +goog.math.Size.prototype.isEmpty = function() { + return !this.area(); +}; + + +/** + * Clamps the width and height parameters upward to integer values. + * @return {!goog.math.Size} This size with ceil'd components. + */ +goog.math.Size.prototype.ceil = function() { + this.width = Math.ceil(this.width); + this.height = Math.ceil(this.height); + return this; +}; + + +/** + * @param {!goog.math.Size} target The target size. + * @return {boolean} True if this Size is the same size or smaller than the + * target size in both dimensions. + */ +goog.math.Size.prototype.fitsInside = function(target) { + return this.width <= target.width && this.height <= target.height; +}; + + +/** + * Clamps the width and height parameters downward to integer values. + * @return {!goog.math.Size} This size with floored components. + */ +goog.math.Size.prototype.floor = function() { + this.width = Math.floor(this.width); + this.height = Math.floor(this.height); + return this; +}; + + +/** + * Rounds the width and height parameters to integer values. + * @return {!goog.math.Size} This size with rounded components. + */ +goog.math.Size.prototype.round = function() { + this.width = Math.round(this.width); + this.height = Math.round(this.height); + return this; +}; + + +/** + * Scales this size by the given scale factors. The width and height are scaled + * by {@code sx} and {@code opt_sy} respectively. If {@code opt_sy} is not + * given, then {@code sx} is used for both the width and height. + * @param {number} sx The scale factor to use for the width. + * @param {number=} opt_sy The scale factor to use for the height. + * @return {!goog.math.Size} This Size object after scaling. + */ +goog.math.Size.prototype.scale = function(sx, opt_sy) { + var sy = goog.isNumber(opt_sy) ? opt_sy : sx; + this.width *= sx; + this.height *= sy; + return this; +}; + + +/** + * Uniformly scales the size to perfectly cover the dimensions of a given size. + * If the size is already larger than the target, it will be scaled down to the + * minimum size at which it still covers the entire target. The original aspect + * ratio will be preserved. + * + * This function assumes that both Sizes contain strictly positive dimensions. + * @param {!goog.math.Size} target The target size. + * @return {!goog.math.Size} This Size object, after optional scaling. + */ +goog.math.Size.prototype.scaleToCover = function(target) { + var s = this.aspectRatio() <= target.aspectRatio() ? + target.width / this.width : + target.height / this.height; + + return this.scale(s); +}; + + +/** + * Uniformly scales the size to fit inside the dimensions of a given size. The + * original aspect ratio will be preserved. + * + * This function assumes that both Sizes contain strictly positive dimensions. + * @param {!goog.math.Size} target The target size. + * @return {!goog.math.Size} This Size object, after optional scaling. + */ +goog.math.Size.prototype.scaleToFit = function(target) { + var s = this.aspectRatio() > target.aspectRatio() ? + target.width / this.width : + target.height / this.height; + + return this.scale(s); +}; |