/** * Copyright (c) 2006 Peter Goodman * Licensed under the MIT License: * http://www.opensource.org/licenses/mit-license.php */ //-------------------------------------------- // A class for dealing with associative stacks. // Think of how a normal stack would work in php // with an associative array. This mimicks that // although the process is much more involved. //-------------------------------------------- class FA.Stack { private var stack_object:Object = { } private var stack_keys:Array = [ ]; private var stack_vals:Array = [ ]; //-------------------------------------------- // Push a value on to the end of the stack //-------------------------------------------- public function push (key:String, value, _index) : Void { var index:Number = _index == undefined ? stack_keys.length : _index; if (stack_object[key] != undefined) { index = stack_object[key]; } // this is such an annoying thing to do to // get _all_ the functionality I want. stack_vals[index] = value; stack_keys[index] = key; stack_object[key] = index; } //-------------------------------------------- // Get a value and delete all stuff relating to it // in the stack. //-------------------------------------------- private function _get (index:Number) { var ret; if (stack_keys[index] != undefined) { ret = stack_vals[index]; // delete the stuff off of the end of the array delete stack_object[stack_keys[index]]; delete stack_keys[index]; delete stack_vals[index]; } return ret; } //-------------------------------------------- // Pop a value off of the end of the stack. //-------------------------------------------- public function pop () { //return this._get(stack_keys.length); var index:Number; var ret; for (index = stack_keys.length-1; index >= 0; index--) { ret = this._get(index); break; } return ret; } //-------------------------------------------- // Shift a value off of the start of the stack. //-------------------------------------------- public function shift () { var ret; var index:Number; for (index in stack_keys) { ret = this._get(index); break; } return ret; } //-------------------------------------------- // Insert a value (and key) onto the start of the // stack. The hope is this function will never be // used :P //-------------------------------------------- public function unshift (key:String, value) : Void { var index:Number; // oof, this is an ugly thing to do, we need to // change all of the keys in the the arrays. for (index = stack_keys.length-1; index >= 0; index--) { if(stack_keys[index] != undefined) { stack_object[stack_keys[index]] = index+1; stack_keys[index+1] = stack_keys[index]; stack_vals[index+1] = stack_vals[index]; // this serves to delete all the old stuff. this._get(index); } } // unshift the value :D this.push(key, value, 0); } //-------------------------------------------- // Get a value from the stack by the key that // it was assigned. //-------------------------------------------- public function get (key:String) { var ret; if (stack_object[key] != undefined) { ret = stack_vals[stack_object[key]]; } return ret; } //-------------------------------------------- // Just another name for the 'push' function. //-------------------------------------------- public function set (key, value) : Void { this.push(key, value); } }