/*---------------- FILE HEADER --------------------------------------- This file is part of Geoide. Copyright (C) 2005-2006 by: IDgis B.V. http://www.idgis.nl This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA Contact: Herman Assink IDgis bv P.O. Box 15 7450 AA Holten The Netherlands E-Mail: herman.assink@idgis.nl * @version 1.4.0 * @author IDgis team * ------------------------------------------------------------------------*/ class nl.idgis.giclient.util.ArrayTools { static public function clone(array:Array):Array { var cloneArray:Array = new Array(); for (var i:Number = 0; i < array.length; i++) { cloneArray.push(array[i]); } return cloneArray; } static public function equals(arrayA:Array, arrayB:Array):Boolean { if ((getLeftOver(arrayA, arrayB).length = 0) && (getLeftOver(arrayB, arrayA).length = 0)) { return true; } return false; } static public function split(string:String):Array { var array:Array = null; if (string.length == 0) { array = new Array(); // Without the above check, the split method would not result in an empty array. } else { array = string.split(","); } return array; } static public function getUniqueConcat(arrayA:Array, arrayB:Array):Array { var uniqueConcat:Array = concat(arrayA, arrayB); makeUnique(uniqueConcat); return uniqueConcat; } static public function concat(arrayA:Array, arrayB:Array):Array { var concatArray:Array = new Array(); for (var i:Number = 0; i < arrayA.length; i++) { concatArray.push(arrayA[i]); } for (var i:Number = 0; i < arrayB.length; i++) { concatArray.push(arrayB[i]); } return concatArray; } static public function makeUnique(array:Array):Void { for (var i:Number = 0; i < array.length; i++) { for (var j:Number = i + 1; j < array.length; j++) { if (array[i] == array[j]) { array.splice(j--, 1); } } } } static public function intersect(arrayA:Array, arrayB:Array):Array { var intersectArray:Array = new Array(); for (var i:Number = 0; i < arrayA.length; i++) { if (numInArray(arrayB, arrayA[i]) > 0) { intersectArray.push(arrayA[i]); } } return intersectArray; } static public function numInArray(array:Array, element:Object):Number { var numInArray:Number = 0; for (var i:Number = 0; i < array.length; i++) { if (array[i] == element) { numInArray++; } } return numInArray; } static public function posInArray(array:Array, element:Object):Number { for (var i:Number = 0; i < array.length; i++) { if (array[i] == element) { return i; } } return -1; } static public function getLeftOver(leftArray:Array, rightArray:Array):Array { var elementFound:Boolean = false; var leftOver:Array = new Array(); for (var i:Number = 0; i < leftArray.length; i++) { elementFound = false; for (var j:Number = 0; j < rightArray.length; j++) { if (leftArray[i] == rightArray[j]) { elementFound = true; break; } } if (!elementFound) { leftOver.push(leftArray[i]); } } return leftOver; } static public function getSum(array:Array):Number { var sum:Number = 0; for (var i:String in array) { sum += array[i]; } return sum; } }