mas01mj@732: /* mas01mj@732: Copyright (c) 2008, Adobe Systems Incorporated mas01mj@732: All rights reserved. mas01mj@732: mas01mj@732: Redistribution and use in source and binary forms, with or without mas01mj@732: modification, are permitted provided that the following conditions are mas01mj@732: met: mas01mj@732: mas01mj@732: * Redistributions of source code must retain the above copyright notice, mas01mj@732: this list of conditions and the following disclaimer. mas01mj@732: mas01mj@732: * Redistributions in binary form must reproduce the above copyright mas01mj@732: notice, this list of conditions and the following disclaimer in the mas01mj@732: documentation and/or other materials provided with the distribution. mas01mj@732: mas01mj@732: * Neither the name of Adobe Systems Incorporated nor the names of its mas01mj@732: contributors may be used to endorse or promote products derived from mas01mj@732: this software without specific prior written permission. mas01mj@732: mas01mj@732: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS mas01mj@732: IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, mas01mj@732: THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR mas01mj@732: PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR mas01mj@732: CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, mas01mj@732: EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, mas01mj@732: PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR mas01mj@732: PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF mas01mj@732: LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING mas01mj@732: NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS mas01mj@732: SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. mas01mj@732: */ mas01mj@732: mas01mj@732: package com.adobe.utils mas01mj@732: { mas01mj@732: mas01mj@732: /** mas01mj@732: * Class that contains static utility methods for manipulating and working mas01mj@732: * with Arrays. mas01mj@732: * mas01mj@732: * Note that all APIs assume that they are working with well formed arrays. mas01mj@732: * i.e. they will only manipulate indexed values. mas01mj@732: * mas01mj@732: * @langversion ActionScript 3.0 mas01mj@732: * @playerversion Flash 9.0 mas01mj@732: * @tiptext mas01mj@732: */ mas01mj@732: public class ArrayUtil mas01mj@732: { mas01mj@732: mas01mj@732: /** mas01mj@732: * Determines whether the specified array contains the specified value. mas01mj@732: * mas01mj@732: * @param arr The array that will be checked for the specified value. mas01mj@732: * mas01mj@732: * @param value The object which will be searched for within the array mas01mj@732: * mas01mj@732: * @return True if the array contains the value, False if it does not. mas01mj@732: * mas01mj@732: * @langversion ActionScript 3.0 mas01mj@732: * @playerversion Flash 9.0 mas01mj@732: * @tiptext mas01mj@732: */ mas01mj@732: public static function arrayContainsValue(arr:Array, value:Object):Boolean mas01mj@732: { mas01mj@732: return (arr.indexOf(value) != -1); mas01mj@732: } mas01mj@732: mas01mj@732: /** mas01mj@732: * Remove all instances of the specified value from the array, mas01mj@732: * mas01mj@732: * @param arr The array from which the value will be removed mas01mj@732: * mas01mj@732: * @param value The object that will be removed from the array. mas01mj@732: * mas01mj@732: * @langversion ActionScript 3.0 mas01mj@732: * @playerversion Flash 9.0 mas01mj@732: * @tiptext mas01mj@732: */ mas01mj@732: public static function removeValueFromArray(arr:Array, value:Object):void mas01mj@732: { mas01mj@732: var len:uint = arr.length; mas01mj@732: mas01mj@732: for(var i:Number = len; i > -1; i--) mas01mj@732: { mas01mj@732: if(arr[i] === value) mas01mj@732: { mas01mj@732: arr.splice(i, 1); mas01mj@732: } mas01mj@732: } mas01mj@732: } mas01mj@732: mas01mj@732: /** mas01mj@732: * Create a new array that only contains unique instances of objects mas01mj@732: * in the specified array. mas01mj@732: * mas01mj@732: * Basically, this can be used to remove duplication object instances mas01mj@732: * from an array mas01mj@732: * mas01mj@732: * @param arr The array which contains the values that will be used to mas01mj@732: * create the new array that contains no duplicate values. mas01mj@732: * mas01mj@732: * @return A new array which only contains unique items from the specified mas01mj@732: * array. mas01mj@732: * mas01mj@732: * @langversion ActionScript 3.0 mas01mj@732: * @playerversion Flash 9.0 mas01mj@732: * @tiptext mas01mj@732: */ mas01mj@732: public static function createUniqueCopy(a:Array):Array mas01mj@732: { mas01mj@732: var newArray:Array = new Array(); mas01mj@732: mas01mj@732: var len:Number = a.length; mas01mj@732: var item:Object; mas01mj@732: mas01mj@732: for (var i:uint = 0; i < len; ++i) mas01mj@732: { mas01mj@732: item = a[i]; mas01mj@732: mas01mj@732: if(ArrayUtil.arrayContainsValue(newArray, item)) mas01mj@732: { mas01mj@732: continue; mas01mj@732: } mas01mj@732: mas01mj@732: newArray.push(item); mas01mj@732: } mas01mj@732: mas01mj@732: return newArray; mas01mj@732: } mas01mj@732: mas01mj@732: /** mas01mj@732: * Creates a copy of the specified array. mas01mj@732: * mas01mj@732: * Note that the array returned is a new array but the items within the mas01mj@732: * array are not copies of the items in the original array (but rather mas01mj@732: * references to the same items) mas01mj@732: * mas01mj@732: * @param arr The array that will be copies mas01mj@732: * mas01mj@732: * @return A new array which contains the same items as the array passed mas01mj@732: * in. mas01mj@732: * mas01mj@732: * @langversion ActionScript 3.0 mas01mj@732: * @playerversion Flash 9.0 mas01mj@732: * @tiptext mas01mj@732: */ mas01mj@732: public static function copyArray(arr:Array):Array mas01mj@732: { mas01mj@732: return arr.slice(); mas01mj@732: } mas01mj@732: mas01mj@732: /** mas01mj@732: * Compares two arrays and returns a boolean indicating whether the arrays mas01mj@732: * contain the same values at the same indexes. mas01mj@732: * mas01mj@732: * @param arr1 The first array that will be compared to the second. mas01mj@732: * mas01mj@732: * @param arr2 The second array that will be compared to the first. mas01mj@732: * mas01mj@732: * @return True if the arrays contains the same values at the same indexes. mas01mj@732: False if they do not. mas01mj@732: * mas01mj@732: * @langversion ActionScript 3.0 mas01mj@732: * @playerversion Flash 9.0 mas01mj@732: * @tiptext mas01mj@732: */ mas01mj@732: public static function arraysAreEqual(arr1:Array, arr2:Array):Boolean mas01mj@732: { mas01mj@732: if(arr1.length != arr2.length) mas01mj@732: { mas01mj@732: return false; mas01mj@732: } mas01mj@732: mas01mj@732: var len:Number = arr1.length; mas01mj@732: mas01mj@732: for(var i:Number = 0; i < len; i++) mas01mj@732: { mas01mj@732: if(arr1[i] !== arr2[i]) mas01mj@732: { mas01mj@732: return false; mas01mj@732: } mas01mj@732: } mas01mj@732: mas01mj@732: return true; mas01mj@732: } mas01mj@732: } mas01mj@732: }