// Copyright 2006 Google Inc. // All Rights Reserved. // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions // are met: // // * Redistributions of source code must retain the above copyright // notice, this list of conditions and the following disclaimer. // * Redistributions in binary form must reproduce the above copyright // notice, this list of conditions and the following disclaimer in // the documentation and/or other materials provided with the // distribution. // // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE // COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN // ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE // POSSIBILITY OF SUCH DAMAGE. /** * @fileoverview Utilities for manipulating arrays. * */ goog.provide('goog.array'); /** * @type {Array|NodeList|Arguments|{length: number}} */ goog.array.ArrayLike = goog.typedef; /** * Returns the last element in an array without removing it. * @param {goog.array.ArrayLike} array The array. * @return {*} Last item in array. */ goog.array.peek = function(array) { return array[array.length - 1]; }; /** * Returns the index of the first element of an array with a specified * value, or -1 if the element is not present in the array. * * See {@link http://tinyurl.com/nga8b} * * @param {goog.array.ArrayLike} arr The array to be searched. * @param {*} obj The object for which we are searching. * @param {number} opt_fromIndex The index at which to start the search. If * omitted the search starts at index 0. * @return {number} The index of the first matching array element. */ goog.array.indexOf = function(arr, obj, opt_fromIndex) { if (arr.indexOf) { return arr.indexOf(obj, opt_fromIndex); } if (Array.indexOf) { return Array.indexOf(arr, obj, opt_fromIndex); } var fromIndex = opt_fromIndex == null ? 0 : (opt_fromIndex < 0 ? Math.max(0, arr.length + opt_fromIndex) : opt_fromIndex); for (var i = fromIndex; i < arr.length; i++) { if (i in arr && arr[i] === obj) return i; } return -1; }; /** * Returns the index of the last element of an array with a specified value, or * -1 if the element is not present in the array. * * See {@link http://tinyurl.com/ru6lg} * * @param {goog.array.ArrayLike} arr The array to be searched. * @param {*} obj The object for which we are searching. * @param {number?} opt_fromIndex The index at which to start the search. If * omitted the search starts at the end of the array. * @return {number} The index of the last matching array element. */ goog.array.lastIndexOf = function(arr, obj, opt_fromIndex) { // if undefined or null are passed then that is treated as 0 which will // always return -1; var fromIndex = opt_fromIndex == null ? arr.length - 1 : opt_fromIndex; if (arr.lastIndexOf) { return arr.lastIndexOf(obj, fromIndex); } if (Array.lastIndexOf) { return Array.lastIndexOf(arr, obj, fromIndex); } if (fromIndex < 0) { fromIndex = Math.max(0, arr.length + fromIndex); } for (var i = fromIndex; i >= 0; i--) { if (i in arr && arr[i] === obj) return i; } return -1; }; /** * Calls a function for each element in an array. * * See {@link http://tinyurl.com/jrvcb} * * @param {goog.array.ArrayLike} arr Array or array like object over * which to iterate. * @param {Function} f The function to call for every element. This function * takes 3 arguments (the element, the index and the array). The return * value is ignored. The function is called only for indexes of the array * which have assigned values; it is not called for indexes which have * been deleted or which have never been assigned values. See {@link * https://developer.mozilla.org/En/Core_JavaScript_1.5_Reference:Objects: * Array:forEach}. * * @param {Object} opt_obj The object to be used as the value of 'this' * within f. */ goog.array.forEach = function(arr, f, opt_obj) { if (arr.forEach) { arr.forEach(f, opt_obj); } else if (Array.forEach) { Array.forEach(/** @type {Array} */ (arr), f, opt_obj); } else { var l = arr.length; // must be fixed during loop... see docs var arr2 = goog.isString(arr) ? arr.split('') : arr; for (var i = 0; i < l; i++) { if (i in arr2) { f.call(opt_obj, arr2[i], i, arr); } } } }; /** * Calls a function for each element in an array, starting from the last * element rather than the first. * * @param {goog.Array.ArrayLike} arr The array over which to iterate. * @param {Function} f The function to call for every element. This function * takes 3 arguments (the element, the index and the array). The return * value is ignored. * @param {Object} opt_obj The object to be used as the value of 'this' * within f. */ goog.array.forEachRight = function(arr, f, opt_obj) { var l = arr.length; // must be fixed during loop... see docs var arr2 = goog.isString(arr) ? arr.split('') : arr; for (var i = l - 1; i >= 0; --i) { if (i in arr2) { f.call(opt_obj, arr2[i], i, arr); } } }; /** * Calls a function for each element in an array, and if the function returns * true adds the element to a new array. * * See {@link http://tinyurl.com/rmtuo} * * @param {Array} arr The array over which to iterate. * @param {Function} f The function to call for every element. This function * takes 3 arguments (the element, the index and the array) and must * return a Boolean. If the return value is true the element is added to the * result array. If it is false the element is not included. * @param {Object} opt_obj The object to be used as the value of 'this' * within f. * @return {Array} a new array in which only elements that passed the test are * present. */ goog.array.filter = function(arr, f, opt_obj) { if (arr.filter) { return arr.filter(f, opt_obj); } if (Array.filter) { return Array.filter(arr, f, opt_obj); } var l = arr.length; // must be fixed during loop... see docs var res = []; var resLength = 0; var arr2 = goog.isString(arr) ? arr.split('') : arr; for (var i = 0; i < l; i++) { if (i in arr2) { var val = arr2[i]; // in case f mutates arr2 if (f.call(opt_obj, val, i, arr)) { res[resLength++] = val; } } } return res; }; /** * Calls a function for each element in an array and inserts the result into a * new array. * * See {@link http://tinyurl.com/hlx5p} * * @param {Array} arr The array over which to iterate. * @param {Function} f The function to call for every element. This function * takes 3 arguments (the element, the index and the array) and should * return something. The result will be inserted into a new array. * @param {Object} opt_obj The object to be used as the value of 'this' * within f. * @return {Array} a new array with the results from f. */ goog.array.map = function(arr, f, opt_obj) { if (arr.map) { return arr.map(f, opt_obj); } if (Array.map) { return Array.map(arr, f, opt_obj); } var l = arr.length; // must be fixed during loop... see docs var res = []; var resLength = 0; var arr2 = goog.isString(arr) ? arr.split('') : arr; for (var i = 0; i < l; i++) { if (i in arr2) { res[resLength++] = f.call(opt_obj, arr2[i], i, arr); } } return res; }; /** * Passes every element of an array into a function and accumulates the result. * We're google; we can't have "map" without "reduce" can we? * * Passes through to: * http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference: * Objects:Array:reduce * when available. * * For example: * var a = [1, 2, 3, 4]; * goog.array.reduce(a, function(r, v, i, arr) {return r + v;}, 0); * returns 10 * * @param {goog.array.ArrayLike} arr The array over which to iterate. * @param {Function} f The function to call for every element. This function * takes 4 arguments (the function's previous result or the initial value, * the value of the current array element, the current array index, and the * array itself) * function(previousValue, currentValue, index, array). * @param {*} val The initial value to pass into the function on the first call. * @param {Object} opt_obj The object to be used as the value of 'this' * within f. * @return {*} Result of evaluating f repeatedly across the values of the array. * @notypecheck See http://b/1342779 */ goog.array.reduce = function(arr, f, val, opt_obj) { if (arr.reduce) { if (opt_obj) { return arr.reduce(goog.bind(f, opt_obj), val); } else { return arr.reduce(f, val); } } var rval = val; goog.array.forEach(arr, function(val, index) { rval = f.call(opt_obj, rval, val, index, arr); }); return rval; }; /** * Passes every element of an array into a function and accumulates the result, * starting from the last element and working towards the first. * * Passes through to: * http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference: * Objects:Array:reduceRight * when available. * * For example: * var a = ['a', 'b', 'c']; * goog.array.reduceRight(a, function(r, v, i, arr) {return r + v;}, ''); * returns 'cba' * * @param {goog.array.ArrayLike} arr The array over which to iterate. * @param {Function} f The function to call for every element. This function * takes 4 arguments (the function's previous result or the initial value, * the value of the current array element, the current array index, and the * array itself) * function(previousValue, currentValue, index, array). * @param {*} val The initial value to pass into the function on the first call. * @param {Object} opt_obj The object to be used as the value of 'this' * within f. * @return {*} Object returned as a result of evaluating f repeatedly across the * values of the array. * @notypecheck See http://b/1342779 */ goog.array.reduceRight = function(arr, f, val, opt_obj) { if (arr.reduceRight) { if (opt_obj) { return arr.reduceRight(goog.bind(f, opt_obj), val); } else { return arr.reduceRight(f, val); } } var rval = val; goog.array.forEachRight(arr, function(val, index) { rval = f.call(opt_obj, rval, val, index, arr); }); return rval; }; /** * Calls f for each element of an array. If any call returns true, some() * returns true (without checking the remaining elements). If all calls * return false, some() returns false. * * See {@link http://tinyurl.com/ekkc2} * * @param {Array} arr The array to check. * @param {Function} f The function to call for every element. This function * takes 3 arguments (the element, the index and the array) and must * return a Boolean. * @param {Object} opt_obj The object to be used as the value of 'this' * within f. * @return {boolean} true if any element passes the test. */ goog.array.some = function(arr, f, opt_obj) { if (arr.some) { return arr.some(f, opt_obj); } if (Array.some) { return Array.some(arr, f, opt_obj); } var l = arr.length; // must be fixed during loop... see docs var arr2 = goog.isString(arr) ? arr.split('') : arr; for (var i = 0; i < l; i++) { if (i in arr2 && f.call(opt_obj, arr2[i], i, arr)) { return true; } } return false; }; /** * Call f for each element of an array. If all calls return true, every() * returns true. If any call returns false, every() returns false and * does not continue to check the remaining elements. * * See {@link http://tinyurl.com/rx3mg} * * @param {Array} arr The array to check. * @param {Function} f The function to call for every element. This function * takes 3 arguments (the element, the index and the array) and must * return a Boolean. * @param {Object} opt_obj The object to be used as the value of 'this' * within f. * @return {boolean} false if any element fails the test. */ goog.array.every = function(arr, f, opt_obj) { if (arr.every) { return arr.every(f, opt_obj); } if (Array.every) { return Array.every(arr, f, opt_obj); } var l = arr.length; // must be fixed during loop... see docs var arr2 = goog.isString(arr) ? arr.split('') : arr; for (var i = 0; i < l; i++) { if (i in arr2 && !f.call(opt_obj, arr2[i], i, arr)) { return false; } } return true; }; /** * Search an array for the first element that satisfies a given condition and * return that element. * @param {goog.array.ArrayLike} arr The array to search. * @param {Function} f The function to call for every element. This function * takes 3 arguments (the element, the index and the array) and should * return a boolean. * @param {Object} opt_obj An optional "this" context for the function. * @return {*} The first array element that passes the test, or null if no * element is found. */ goog.array.find = function(arr, f, opt_obj) { var i = goog.array.findIndex(arr, f, opt_obj); return i < 0 ? null : goog.isString(arr) ? arr.charAt(i) : arr[i]; }; /** * Search an array for the first element that satisfies a given condition and * return its index. * @param {goog.array.ArrayLike} arr The array to search. * @param {Function} f The function to call for every element. This function * takes 3 arguments (the element, the index and the array) and should * return a boolean. * @param {Object} opt_obj An optional "this" context for the function. * @return {number} The index of the first array element that passes the test, * or -1 if no element is found. */ goog.array.findIndex = function(arr, f, opt_obj) { var l = arr.length; // must be fixed during loop... see docs var arr2 = goog.isString(arr) ? arr.split('') : arr; for (var i = 0; i < l; i++) { if (i in arr2 && f.call(opt_obj, arr2[i], i, arr)) { return i; } } return -1; }; /** * Search an array (in reverse order) for the last element that satisfies a * given condition and return that element. * @param {goog.array.ArrayLike} arr The array to search. * @param {Function} f The function to call for every element. This function * takes 3 arguments (the element, the index and the array) and should * return a boolean. * @param {Object} opt_obj An optional "this" context for the function. * @return {*} The last array element that passes the test, or null if no * element is found. */ goog.array.findRight = function(arr, f, opt_obj) { var i = goog.array.findIndexRight(arr, f, opt_obj); return i < 0 ? null : goog.isString(arr) ? arr.charAt(i) : arr[i]; }; /** * Search an array (in reverse order) for the last element that satisfies a * given condition and return its index. * @param {goog.array.ArrayLike} arr The array to search. * @param {Function} f The function to call for every element. This function * takes 3 arguments (the element, the index and the array) and should * return a boolean. * @param {Object} opt_obj An optional "this" context for the function. * @return {number} The index of the last array element that passes the test, * or -1 if no element is found. */ goog.array.findIndexRight = function(arr, f, opt_obj) { var l = arr.length; // must be fixed during loop... see docs var arr2 = goog.isString(arr) ? arr.split('') : arr; for (var i = l - 1; i >= 0; i--) { if (i in arr2 && f.call(opt_obj, arr2[i], i, arr)) { return i; } } return -1; }; /** * Whether the array contains the given object. * @param {goog.array.ArrayLike} arr The array to test for the presence of the * element. * @param {*} obj The object for which to test. * @return {boolean} true if obj is present. */ goog.array.contains = function(arr, obj) { if (arr.contains) { return arr.contains(obj); } return goog.array.indexOf(arr, obj) > -1; }; /** * Whether the array is empty. * @param {goog.array.ArrayLike} arr The array to test. * @return {boolean} true if empty. */ goog.array.isEmpty = function(arr) { return arr.length == 0; }; /** * Clears the array. * @param {goog.array.ArrayLike} arr Array or array like object to clear. */ goog.array.clear = function(arr) { // for non real arrays we don't have the magic length so we delete the // indices if (!goog.isArray(arr)) { for (var i = arr.length - 1; i >= 0; i--) { delete arr[i]; } } arr.length = 0; }; /** * Pushes an item into an array, if it's not already in the array. * @param {Array} arr Array into which to insert the item. * @param {*} obj Value to add. */ goog.array.insert = function(arr, obj) { if (!goog.array.contains(arr, obj)) { arr.push(obj); } }; /** * Inserts an object at the given index of the array. * @param {goog.array.ArrayLike} arr The array to modify. * @param {*} obj The object to insert. * @param {number} opt_i The index at which to insert the object. If omitted, * treated as 0. A negative index is counted from the end of the array. */ goog.array.insertAt = function(arr, obj, opt_i) { goog.array.splice(arr, opt_i, 0, obj); }; /** * Inserts at the given index of the array, all elements of another array. * @param {goog.array.ArrayLike} arr The array to modify. * @param {goog.array.ArrayLike} elementsToAdd The array of elements to add. * @param {number} opt_i The index at which to insert the object. If omitted, * treated as 0. A negative index is counted from the end of the array. */ goog.array.insertArrayAt = function(arr, elementsToAdd, opt_i) { goog.partial(goog.array.splice, arr, opt_i, 0).apply(null, elementsToAdd); }; /** * Inserts an object into an array before a specified object. * @param {Array} arr The array to modify. * @param {*} obj The object to insert. * @param {*} opt_obj2 The object before which obj should be inserted. If obj2 * is omitted or not found, obj is inserted at the end of the array. */ goog.array.insertBefore = function(arr, obj, opt_obj2) { var i; if (arguments.length == 2 || (i = goog.array.indexOf(arr, opt_obj2)) == -1) { arr.push(obj); } else { goog.array.insertAt(arr, obj, i); } }; /** * Removes the first occurrence of a particular value from an array. * @param {goog.array.ArrayLike} arr Array from which to remove value. * @param {*} obj Object to remove. * @return {boolean} True if an element was removed. */ goog.array.remove = function(arr, obj) { var i = goog.array.indexOf(arr, obj); var rv; if ((rv = i != -1)) { goog.array.removeAt(arr, i); } return rv; }; /** * Removes from an array the element at index i * @param {goog.array.ArrayLike} arr Array or array like object from which to * remove value. * @param {number} i The index to remove. * @return {boolean} True if an element was removed. */ goog.array.removeAt = function(arr, i) { // use generic form of splice // splice returns the removed items and if successful the length of that // will be 1 return Array.prototype.splice.call(arr, i, 1).length == 1; }; /** * Removes the first value that satisfies the given condition. * @param {goog.array.ArrayLike} arr Array from which to remove value. * @param {Function} f The function to call for every element. This function * takes 3 arguments (the element, the index and the array) and should * return a boolean. * @param {Object} opt_obj An optional "this" context for the function. * @return {boolean} True if an element was removed. */ goog.array.removeIf = function(arr, f, opt_obj) { var i = goog.array.findIndex(arr, f, opt_obj); if (i >= 0) { goog.array.removeAt(arr, i); return true; } return false; }; /** * Does a shallow copy of an array. * @param {goog.array.ArrayLike} arr Array or array-like object to clone. * @return {Array} Clone of the input array. */ goog.array.clone = function(arr) { if (goog.isArray(arr)) { // Generic concat does not seem to work so lets just use the plain old // instance method. return arr.concat(); } else { // array like // Concat does not work with non arrays var rv = []; for (var i = 0, len = arr.length; i < len; i++) { rv[i] = arr[i]; } return rv; } }; /** * Converts an object to an array. * @param {goog.array.ArrayLike} object The object to convert to an array. * @return {Array} The object converted into an array. If object has a * length property, every property indexed with a non-negative number * less than length will be included in the result. If object does not * have a length property, an empty array will be returned. */ goog.array.toArray = function(object) { if (goog.isArray(object)) { // This fixes the JS compiler warning and forces the Object to an Array type return object.concat(); } // Clone what we hope to be an array-like object to an array. // We could check isArrayLike() first, but no check we perform would be as // reliable as simply making the call. return goog.array.clone(/** @type {Array} */ (object)); }; /** * Extends an array with another array, element, or "array like" object. * This function operates 'in-place', it does not create a new Array. * * Example: * var a = []; * goog.array.extend(a, [0, 1]); * a; // [0, 1] * goog.array.extend(a, 2); * a; // [0, 1, 2] * * @param {Array} arr1 The array to modify. * @param {*} var_args The elements or arrays of elements to add to arr1. */ goog.array.extend = function(arr1, var_args) { for (var i = 1; i < arguments.length; i++) { var arr2 = arguments[i]; if (goog.isArrayLike(arr2)) { // Make sure arr2 is a real array, and not just "array like." arr2 = goog.array.toArray(arr2); arr1.push.apply(arr1, arr2); } else { arr1.push(arr2); } } }; /** * Adds or removes elements from an array. This is a generic version of Array * splice. This means that it might work on other objects similar to arrays, * such as the arguments object. * * @param {goog.array.ArrayLike} arr The array to modify. * @param {number|undefined} index The index at which to start changing the * array. If not defined, treated as 0. * @param {number} howMany How many elements to remove (0 means no removal. A * value below 0 is treated as zero and so is any other non number. Numbers * are floored). * @param {*} var_args Optional, additional elements to insert into the * array. * @return {Array} the removed elements. */ goog.array.splice = function(arr, index, howMany, var_args) { return Array.prototype.splice.apply(arr, goog.array.slice(arguments, 1)); }; /** * Returns a new array from a segment of an array. This is a generic version of * Array slice. This means that it might work on other objects similar to * arrays, such as the arguments object. * * @param {goog.array.ArrayLike} arr The array from which to copy a segment. * @param {number} start The index of the first element to copy. * @param {number} opt_end The index after the last element to copy. * @return {Array} A new array containing the specified segment of the original * array. */ goog.array.slice = function(arr, start, opt_end) { // passing 1 arg to slice is not the same as passing 2 where the second is // null or undefined (in that case the second argument is treated as 0). // we could use slice on the arguments object and then use apply instead of // testing the length if (arguments.length <= 2) { return Array.prototype.slice.call(arr, start); } else { return Array.prototype.slice.call(arr, start, opt_end); } }; /** * Removes all duplicates from an array (retaining only the first * occurrence of each array element). This function modifies the * array in place and doesn't change the order of the non-duplicate items. * * For objects, duplicates are identified as having the same hash code property * as defined by {@see goog.getHashCode}. * * Runtime: N, * Worstcase space: 2N (no dupes) * * @param {goog.array.ArrayLike} arr The array from which to remove duplicates. * @param {Array} opt_rv An optional array in which to return the results, * instead of performing the removal inplace. If specified, the original * array will remain unchanged. */ goog.array.removeDuplicates = function(arr, opt_rv) { var rv = opt_rv || arr; var seen = {}, cursorInsert = 0, cursorRead = 0; while (cursorRead < arr.length) { var current = arr[cursorRead++]; var hc = goog.isObject(current) ? goog.getHashCode(current) : current; if (!(hc in seen)) { seen[hc] = true; rv[cursorInsert++] = current; } } rv.length = cursorInsert; }; /** * Searches the specified array for the specified target using the binary * search algorithm. If no opt_compareFn is specified, elements are compared * using goog.array.defaultCompare, which compares the elements * using the built in < and > operators. This will produce the expected * behavior for homogeneous arrays of String(s) and Number(s). The array * specified must be sorted in ascending order (as defined by the * comparison function). If the array is not sorted, results are undefined. * If the array contains multiple instances of the specified target value, any * of these instances may be found. * * Runtime: O(log n) * * @param {goog.array.ArrayLike} arr The array to be searched. * @param {*} target The sought value. * @param {Function} opt_compareFn Optional comparison function by which the * array is ordered. Should take 2 arguments to compare, and return a * negative integer, zero, or a positive integer depending on whether the * first argument is less than, equal to, or greater than the second. * @return {number} Index of the target value if found, otherwise * (-(insertion point) - 1). The insertion point is where the value should * be inserted into arr to preserve the sorted property. Return value >= 0 * iff target is found. */ goog.array.binarySearch = function(arr, target, opt_compareFn) { var left = 0; var right = arr.length - 1; var compareFn = opt_compareFn || goog.array.defaultCompare; while (left <= right) { var mid = (left + right) >> 1; var compareResult = compareFn(target, arr[mid]); if (compareResult > 0) { left = mid + 1; } else if (compareResult < 0) { right = mid - 1; } else { return mid; } } // Not found, left is the insertion point. return -(left + 1); }; /** * Sorts the specified array into ascending order. If no opt_compareFn is * specified, elements are compared using * goog.array.defaultCompare, which compares the elements using * the built in < and > operators. This will produce the expected behavior * for homogeneous arrays of String(s) and Number(s). * * This sort is not guaranteed to be stable. * * Runtime: Same as Array.prototype.sort * * @param {Array} arr The array to be sorted. * @param {Function} opt_compareFn Optional comparison function by which the * array is to be ordered. Should take 2 arguments to compare, and return a * negative integer, zero, or a positive integer depending on whether the * first argument is less than, equal to, or greater than the second. */ goog.array.sort = function(arr, opt_compareFn) { Array.prototype.sort.call(arr, opt_compareFn || goog.array.defaultCompare); }; /** * Sorts the specified array into ascending order in a stable way. If no * opt_compareFn is specified, elements are compared using * goog.array.defaultCompare, which compares the elements using * the built in < and > operators. This will produce the expected behavior * for homogeneous arrays of String(s) and Number(s). * * Runtime: Same as Array.prototype.sort, plus an additional * O(n) overhead of copying the array twice. * * @param {Array} arr The array to be sorted. * @param {function(*, *): number} opt_compareFn Optional comparison function by * which the array is to be ordered. Should take 2 arguments to compare, and * return a negative integer, zero, or a positive integer depending on * whether the first argument is less than, equal to, or greater than the * second. */ goog.array.stableSort = function(arr, opt_compareFn) { for (var i = 0; i < arr.length; i++) { arr[i] = {index: i, value: arr[i]}; } var valueCompareFn = opt_compareFn || goog.array.defaultCompare; function stableCompareFn(obj1, obj2) { return valueCompareFn(obj1.value, obj2.value) || obj1.index - obj2.index; }; goog.array.sort(arr, stableCompareFn); for (var i = 0; i < arr.length; i++) { arr[i] = arr[i].value; } }; /** * Sorts an array of objects by the specified object key and compare * function. If no compare function is provided, the key values are * compared in ascending order using goog.array.defaultCompare. * This won't work for keys that get renamed by the compiler. So use * {'foo': 1, 'bar': 2} rather than {foo: 1, bar: 2}. * @param {Array.} arr An array of objects to sort. * @param {string} key The object key to sort by. * @param {Function} opt_compareFn The function to use to compare key * values. */ goog.array.sortObjectsByKey = function(arr, key, opt_compareFn) { var compare = opt_compareFn || goog.array.defaultCompare; goog.array.sort(arr, function(a, b) { return compare(a[key], b[key]); }); }; /** * Compares two arrays for equality. If no opt_compareFn is specified, uses * goog.array.defaultCompareEquality, which compares the elements * using the built-in '===' operator. Two arrays are considered equal if they * have the same length and their corresponding elements are equal according to * the comparison function. * * @param {Array} arr1 The first array to compare. * @param {Array} arr2 The second array to compare. * @param {Function} opt_compareFn Optional comparison function. * Should take 2 arguments to compare, and return true if the arguments * are equal. * @return {boolean} True if the two arrays are equal. */ goog.array.compare = function(arr1, arr2, opt_compareFn) { if (!goog.isArrayLike(arr1) || !goog.isArrayLike(arr2) || arr1.length != arr2.length) { return false; } var l = arr1.length; var compareFn = opt_compareFn || goog.array.defaultCompareEquality; for (var i = 0; i < l; i++) { if (!compareFn.call(null, arr1[i], arr2[i])) { return false; } } return true; }; /** * Compares its two arguments for order, using the built in < and > * operators. * @param {*} a The first object to be compared. * @param {*} b The second object to be compared. * @return {number} a negative integer, zero, or a positive integer * as the first argument is less than, equal to, or greater than the * second. */ goog.array.defaultCompare = function(a, b) { return a > b ? 1 : a < b ? -1 : 0; }; /** * Compares its two arguments for equality, using the built in === operator. * @param {*} a The first object to compare. * @param {*} b The second object to compare. * @return {boolean} True if the two arguments are equal, false otherwise. */ goog.array.defaultCompareEquality = function(a, b) { return a === b; }; /** * Inserts a value into a sorted array. The array is not modified if the * value is already present. * @param {Array} array The array to modify. * @param {*} value The object to insert. * @param {Function} opt_compareFn Optional comparison function by which the * array is ordered. Should take 2 arguments to compare, and * return a negative integer, zero, or a positive integer depending on * whether the first argument is less than, equal to, or greater than the * second. * @return {boolean} True if an element was inserted. */ goog.array.binaryInsert = function(array, value, opt_compareFn) { var index = goog.array.binarySearch(array, value, opt_compareFn); if (index < 0) { goog.array.insertAt(array, value, -(index + 1)); return true; } return false; }; /** * Removes a value from a sorted array. * @param {Array} array The array to modify. * @param {*} value The object to remove. * @param {Function} opt_compareFn Optional comparison function by which the * array is ordered. Should take 2 arguments to compare, and * return a negative integer, zero, or a positive integer depending on * whether the first argument is less than, equal to, or greater than the * second. * @return {boolean} True if an element was removed. */ goog.array.binaryRemove = function(array, value, opt_compareFn) { var index = goog.array.binarySearch(array, value, opt_compareFn); return (index >= 0) ? goog.array.removeAt(array, index) : false; }; /** * Splits an array into disjoint buckets according to a splitting function. * @param {Array} array The array. * @param {Function} sorter Function to call for every element. This * takes 3 arguments (the element, the index and the array) and must * return a valid object key (a string, number, etc), or undefined, if * that object should not be placed in a bucket. * @return {Object} An object, with keys being all of the unique return values * of sorter, and values being arrays containing the items for * which the splitter returned that key. */ goog.array.bucket = function(array, sorter) { var buckets = {}; for (var i = 0; i < array.length; i++) { var value = array[i]; var key = sorter(value, i, array); if (goog.isDef(key)) { // Push the value to the right bucket, creating it if necessary. var bucket = buckets[key] || (buckets[key] = []); bucket.push(value); } } return buckets; }; /** * Returns an array consisting of the given value repeated N times. * * @param {*} value The value to repeat. * @param {number} n The repeat count. * @return {Array.<*>} An array with the repeated value. */ goog.array.repeat = function(value, n) { var array = []; for (var i = 0; i < n; i++) { array[i] = value; } return array; }; /** * Returns an array consisting of every argument with all arrays * expanded in-place recursively. * * @param {*} var_args The values to flatten. * @return {Array.<*>} An array containing the flattened values. */ goog.array.flatten = function(var_args) { var result = []; for (var i = 0; i < arguments.length; i++) { var element = arguments[i]; if (goog.isArray(element)) { result.push.apply(result, goog.array.flatten.apply(null, element)); } else { result.push(element); } } return result; };