package flare.util { /** * Utility methods for working with arrays. */ public class Arrays { public static const EMPTY:Array = new Array(0); /** * Constructor, throws an error if called, as this is an abstract class. */ public function Arrays() { throw new ArgumentError("This is an abstract class."); } /** * Returns the maximum value in an array. Comparison is determined * using the greater-than operator against arbitrary types. * @param a the array * @param p an optional property from which to extract the value. * If this is null, the immediate contents of the array are compared. * @return the maximum value */ public static function max(a:Array, p:Property=null):Number { var x:Number = Number.MIN_VALUE; if (p) { var v:Number; for (var i:uint=0; i x) x = v; } } else { for (i=0; i x) x = a[i]; } } return x; } /** * Returns the index of a maximum value in an array. Comparison is * determined using the greater-than operator against arbitrary types. * @param a the array * @param p an optional property from which to extract the value. * If this is null, the immediate contents of the array are compared. * @return the index of a maximum value */ public static function maxIndex(a:Array, p:Property=null):Number { var x:Number = Number.MIN_VALUE; var idx:int = -1; if (p) { var v:Number; for (var i:uint=0; i x) { x = v; idx = i; } } } else { for (i=0; i x) { x = a[i]; idx = i; } } } return idx; } /** * Returns the minimum value in an array. Comparison is determined * using the less-than operator against arbitrary types. * @param a the array * @param p an optional property from which to extract the value. * If this is null, the immediate contents of the array are compared. * @return the minimum value */ public static function min(a:Array, p:Property=null):Number { var x:Number = Number.MAX_VALUE; if (p) { var v:Number; for (var i:uint=0; i 0) a.pop(); } /** * Removes an element from an array. Only the first instance of the * value is removed. * @param a the array * @param o the value to remove * @return the index location at which the removed element was found, * negative if the value was not found. */ public static function remove(a:Array, o:Object) : int { var idx:int = a.indexOf(o); if (idx == a.length-1) { a.pop(); } else if (idx >= 0) { a.splice(idx, 1); } return idx; } /** * Removes the array element at the given index. * @param a the array * @param idx the index at which to remove an element * @return the removed element */ public static function removeAt(a:Array, idx:uint) : Object { if (idx == a.length-1) { return a.pop(); } else { var x:Object = a[idx]; a.splice(idx,1); return x; } } /** * Performs a binary search over the input array for the given key * value, optionally using a provided property to extract from array * items and a custom comparison function. * @param a the array to search over * @param key the key value to search for * @param prop the property to retrieve from objecs in the array. If null * (the default) the array values will be used directly. * @param cmp an optional comparison function * @return the index of the given key if it exists in the array, * otherwise -1 times the index value at the insertion point that * would be used if the key were added to the array. */ public static function binarySearch(a:Array, key:Object, prop:String=null, cmp:Function=null) : int { var p:Property = prop ? Property.$(prop) : null; if (cmp == null) cmp = function(a:*,b:*):int {return a>b ? 1 : a>1); while (x1 < x2) { var c:int = cmp(p ? p.getValue(a[i]) : a[i], key); if (c == 0) { return i; } else if (c < 0) { x1 = i + 1; } else { x2 = i; } i = x1 + ((x2 - x1)>>1); } return -1*(i+1); } /** * Sets a named property value for items stored in an array. * The value can take a number of forms: *
    *
  • If the value is a Function, it will be evaluated * for each element and the result will be used as the property * value for that element.
  • *
  • If the value is an IEvaluable instance, it will be * evaluated for each element and the result will be used as the * property value for that element.
  • *
  • In all other cases, the property value will be treated as a * literal and assigned for all elements.
  • *
* @param list the array to set property values for * @param name the name of the property to set * @param value the value of the property to set * @param filter a filter function determining which items * in the array should be processed * @param p an optional IValueProxy for setting the values */ public static function setProperty(a:Array, name:String, value:*, filter:Function, p:IValueProxy=null):void { if (p == null) p = Property.proxy; var v:Function = value is Function ? value as Function : value is IEvaluable ? IEvaluable(value).eval : null; for each (var o:Object in a) if (filter==null || filter(o)) p.setValue(o, name, v!=null ? v(p.$(o)) : value); } } // end of class Arrays }