ActionScript 3 AS3 | ArrayQueue

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ActionScript 3 AS3 | ArrayQueue相关的知识,希望对你有一定的参考价值。

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():*;
	
	}
	
}

以上是关于ActionScript 3 AS3 | ArrayQueue的主要内容,如果未能解决你的问题,请参考以下文章

[ActionScript 3.0] AS3.0 水面波纹效果

ActionScript 3 AS3:使用SWFObject2和AS3传递变量

ActionScript 3 克隆数组(AS3)

ActionScript 3 AS3序列化

ActionScript 3 As3初始上限

ActionScript 3 AS3 HTML清理