Return to Snippet

Revision: 21752
at December 20, 2009 02:32 by alejandromb


Initial Code
package kc.tda {
	import kc.api.IQueue;
	import kc.events.CollectionEvent;

	[Event( name="addAll", type="kc.events.CollectionEvent" )]
	[Event( name="clear", type="kc.events.CollectionEvent" )]
	[Event( name="remove", type="kc.events.CollectionEvent" )]
	[Event( name="removeAll", type="kc.events.CollectionEvent" )]
	[Event( name="retain", type="kc.events.CollectionEvent" )]	
	[Event( name="enqueue", type="kc.events.CollectionEvent" )]
	[Event( name="dequeue", type="kc.events.CollectionEvent" )]

	public class ArrayQueue extends ArrayCollection implements IQueue {
		
		// @constructor

		public function ArrayQueue( capacity:int = undefined, expandableCapacity:Boolean = false, loadFactor:Number = NaN ) {
			
			super( capacity, expandableCapacity, loadFactor );
			
			_events = [
				CollectionEvent.ADD_ALL,
				CollectionEvent.CLEAR,
				CollectionEvent.REMOVE,
				CollectionEvent.REMOVE_ALL,
				CollectionEvent.RETAIN,
				CollectionEvent.ENQUEUE,
				CollectionEvent.DEQUEUE				
			];
			
		}

		// @override
		
		override public function add( value:* ):Boolean {			
			return enqueue( value );			
		}
		
		override public function remove( value:* ):Boolean {
			return ( value != null )
				? super.remove( value )
				: ( dequeue() != null )
					? true
					: false;
		}

		// @methods
		
		public function element():* {
			ThrowIsEmpty();
			return _records[0];
		}
		
		public function peek():* {
			try{
				var value:* = element();
			}catch(e:Error){
				return null;
			} return value;
		}
		
		public function enqueue( value:* ):Boolean {
			
			if( ! ResolveStatus() ) {
				return false;
			}
				
			var quantity:int = size();
			_records.push( value ); 
			
			if( size() != quantity ){
				ResolveDispatchEvent( CollectionEvent.ENQUEUE ); 
				return true;
			} return false;
			
		}
		
		public function dequeue():* {
			var quantity:int = size();
			var value:* = _records.shift();
			if( size() != quantity ) {
				ResolveDispatchEvent( CollectionEvent.DEQUEUE );
				return value;
			} return null;
		}
		
	}
	
}

// @INTERFACE


package kc.api {

	public interface IQueue  extends ICollection {
	
		// @methods
	
		function element():*;
		function peek():*;
		function enqueue(value:*):Boolean;
		function dequeue():*;
	
	}
	
}

Initial URL
http://www.kirikacode.com

Initial Description

                                

Initial Title
AS3 | ArrayQueue

Initial Tags

                                

Initial Language
ActionScript 3