AS3 ArrayQueue ADT类
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了AS3 ArrayQueue ADT类相关的知识,希望对你有一定的参考价值。
package { import flash.display.Sprite; /*********************************************************************************************** * ArrayQueue Class - Designed to give generic functionality to a queue collection type. * * This collection is to be implemented in a FIFO principles. With adding, removing, searching, * * trace output, and state functions of the collection. * * @author : Richard Vacheresse /| http://www.rvacheresse.com /| * * Licensed for free Commercial and Private use creative commons license agreement. * * The provided code is in an "as-is" state. Richard Vacheresse makes no warranties * * regarding the provided code, and disclaims liability for damages resulting from its use. * * @version 1.0 * ***********************************************************************************************/ public class ArrayQueue extends Sprite implements QueueADT { //- default capapcity of queue private var DEFAULT_CAPACITY:int = 100; //- indicates the value of the last private var rear:int; //- array object to hold the objects private var queue:Array; /** * ArrayQueue() Constructor - Creates an empty queue using the default capacity. * @variable - DEFAULT_CAPACITY - The default value for the queue's capacity. * @variable - rear - Indicates the value of the last object + one as an Integer object. * @variable - queue - The container object that will hold the objects of type array. **/ public function ArrayQueue():void { //- set rear to 0 since the queue is empty rear = 0; // - instantiate a new array object at the default capacity of 100 queue = new Array(DEFAULT_CAPACITY); //- output to console trace("New ArrayQueue Created At Default Capacity"); } /** * ArrayQueueDefined(queueSize:int) function - Creates an empty queue using the passed in value. **/ public function ArrayQueueDefined(queueSize:int):void { //- set rear to 0 since the queue is empty rear = 0; // - instantiate a new array object at the passed value size queue = new Array(queueSize); //- output to console trace("New ArrayQueue Created At: " + queueSize); } /** * dequeue() function - Removes the first item in the queue. * - Then shifts the queue minus the removed object * - Decrement rear to reflect minus one object. * @return - Object **/ public function dequeue():Object { //- if the collection is empty throw an error if(isEmpty()) throw new Error("Queue contains no objects"); //- else remove the object at the first position var result:Object = queue[0]; //- decrement the rear by one rear--; //- shift the elements forward one position for(var scan:int = 0; scan < rear; scan++) queue[scan] = queue[scan+1]; //- set the rear null again to null queue[rear] = null; //- output to console trace("Item " + result + " dequeued."); //- return the first objec in the array return result; } /** * enqueue(obj:Object) function - Takes the passed object and adds it to the * rear indicated position. * - Increment the rear value + one to reflect * the additional object. **/ public function enqueue(obj:Object):void { queue[rear] = obj; rear++; } /** * first() function - Returns the first object in the queue but does not remove it. * @return - Object **/ public function first():Object { var result:Object = "null"; if(isEmpty()) throw new Error("The queue is empty"); //- set result pointer equal to first item but do not remove result = queue[0]; //- output to console trace("Item " + queue[0] + " is next."); return result; } /** * size() function - Returns the number of objects in the queue. * @return - Integer Object **/ public function size():int { return rear; } /** * getLength() accessor function - Returns an integer value of the length of the queue. * @return - Integer Object **/ public function getLength():int { return queue.length; } /** * isEmpty() function - Returns True if the value of rear is equal to zero. * @return - Boolean Object **/ public function isEmpty():Boolean { return (rear == 0); } /** * expandCapacity() function - Creates a new array of twice the size of the current * array queue. * - Then it repopulates the new larger Array with the * original values. **/ public function expandCapacity():void { var result:int = (queue.length*2); var larger:Array = new Array(result); for(var scan:int = 0; scan < queue.length; scan++) larger[scan] = queue[scan]; queue = larger; } /** * toString():String function - Returns a custom String object to represent the queue. * - Overriden only because it is of type Sprite, (which has * by default its' own toString() function), therefore we * need more information about the queue. * @return - String Object **/ override public function toString():String { var result:String = ("------------------ " + "Queue toString() " + "------------------ " + "Queue has " + size() + " items. "); for(var scan:int = 0; scan < rear; scan++) { result += ("Item: " + scan + " is a: " + queue[scan] + " "); } return result; } } }
以上是关于AS3 ArrayQueue ADT类的主要内容,如果未能解决你的问题,请参考以下文章