Skip to content

Instantly share code, notes, and snippets.

@og2t
Created March 10, 2010 11:59
Show Gist options
  • Save og2t/327799 to your computer and use it in GitHub Desktop.
Save og2t/327799 to your computer and use it in GitHub Desktop.
AS3 Extended array (using proxy)
/*---------------------------------------------------------------------------------------------
[AS3] ExtendedArray
=======================================================================================
Some methods by Yossy, http://www.libspark.org/
VERSION HISTORY:
v0.1 Born on 2008-04-18
USAGE:
TODOs:
- add more Yossy's methods
DEV IDEAS:
KNOWN ISSUES:
---------------------------------------------------------------------------------------------*/
package net.blog2t.util
{
// IMPORTS ////////////////////////////////////////////////////////////////////////////////
import flash.utils.Proxy;
import flash.utils.flash_proxy;
// CLASS //////////////////////////////////////////////////////////////////////////////////
public dynamic class ExtendedArray extends Proxy
{
// CONSTANTS //////////////////////////////////////////////////////////////////////////
// MEMBERS ////////////////////////////////////////////////////////////////////////////
private var itemsArray:Object = [];
// CONSTRUCTOR ////////////////////////////////////////////////////////////////////////
public function ExtendedArray(... initialArray:Array)
{
itemsArray = initialArray;
}
// PUBLIC METHODS /////////////////////////////////////////////////////////////////////
public function lastGreater(value:Number):void
{
var lastGreater:Number;
var numElements:int = itemsArray.length;
for (var i:int = 0; i < numElements; i++)
{
if (itemsArray[i] < value) lastGreater = itemsArray[i];
else break;
}
return lastGreater;
}
public function max():Number
{
var numElements:int = itemsArray.length;
var maxEl:Number = itemsArray[0];
for each (var value:Number in itemsArray)
{
if (value > maxEl) maxEl = value;
}
/*for (var i:int = 0; i < numElements; i++)
{
if (itemsArray[i] > maxEl) maxEl = itemsArray[i];
}*/
return maxEl;
}
public function min():Number
{
var numElements:int = itemsArray.length;
var minEl:Number = itemsArray[0];
for each (var value:Number in itemsArray)
{
if (value < maxEl) maxEl = value;
}
/*for (var i:int = 0; i < numElements; i++)
{
if (itemsArray[i] < minEl) minEl = itemsArray[i];
}*/
return minEl;
}
public function findPull(value:String):int
{
if (itemsArray.indexOf(value) != -1)
{
itemsArray.splice(itemsArray.indexOf(value), 1);
return itemsArray.length;
}
return -1;
}
public function findPush(value:String):Boolean
{
if (itemsArray.indexOf(value) != -1)
{
return false;
//return itemsArray.indexOf(value);
}
itemsArray.push(value);
return true;
//return itemsArray.length - 1;
}
public function findPullPush(value:String):Boolean
{
if (itemsArray.indexOf(value) != -1)
{
itemsArray.splice(itemsArray.indexOf(value), 1);
return true;
//return itemsArray.length;
}
itemsArray.push(value);
return false;
//return itemsArray.length;
}
public function findAnyElementOf(array:*):Boolean
{
var counter:int = 0;
for each (var option:String in array)
{
for each (var element:String in itemsArray)
{
if (option.indexOf(element) != -1) return true;
}
}
return false;
}
/**
* Returns true if all elements of array has been found in extended array
*/
public function findAllElementsOf(array:*):Boolean
{
var counter:int = 0;
for each (var element:String in itemsArray)
{
for each (var option:String in array)
{
if (option.indexOf(element) != -1) counter++;
}
}
if (counter > 0 && counter == itemsArray.length) return true;
return false;
}
/**
* Adds element at a given index
*
* @param element
* @param index, if negative counts from the end of the array
* @return new array length
* @throws RangeError when index out of range
*/
public function addElementAt(element:*, index:int):uint
{
if (index < 0)
{
index += itemsArray.length;
}
if (index < 0 || itemsArray.length < index)
{
throw new RangeError("ExtendedArray: Index out of range");
}
itemsArray.splice(index, 0, element);
return itemsArray.length;
}
// OVERRIDES //////////////////////////////////////////////////////////////////////////
override flash_proxy function callProperty(methodName:*, ... args):*
{
var res:*;
//trace(methodName, args);
switch (methodName.toString())
{
case 'clear':
itemsArray = new Array();
break;
default:
res = itemsArray[methodName].apply(itemsArray, args);
break;
}
return res;
}
override flash_proxy function getProperty(name:*):*
{
return itemsArray[name];
}
override flash_proxy function setProperty(name:*, value:*):void
{
itemsArray[name] = value;
}
//needed for: for each()
override flash_proxy function nextValue(index:int):*
{
return itemsArray[index - 1];
}
override flash_proxy function nextNameIndex(index:int):int
{
if (index < itemsArray.length) return index + 1;
return 0;
}
//needed for: for (var a:String in ExtendedArray)
override flash_proxy function nextName(index:int):String
{
return String(index - 1);
}
/*//delete foo.bar or delete foo['bar'] => foo.deleteProperty('bar')
override flash_proxy function deleteProperty(name:*):Boolean
{
return delete itemsArray[name];
}
//'bar' in foo => foo.hasProperty('bar')
override flash_proxy function hasProperty(name:*):Boolean
{
return name in itemsArray;
}*/
}
// END OF CLASS ///////////////////////////////////////////////////////////////////////////
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment