AS3 ArrayQueue ADT Class


/ Published in: ActionScript 3
Save to your folder(s)



Copy this code and paste it in your HTML
  1. package
  2. {
  3. import flash.display.Sprite;
  4.  
  5. /***********************************************************************************************\
  6. * ArrayQueue Class - Designed to give generic functionality to a queue collection type. *
  7. * This collection is to be implemented in a FIFO principles. With adding, removing, searching, *
  8. * trace output, and state functions of the collection. *
  9. * @author : Richard Vacheresse /|\ http://www.rvacheresse.com /|\ *
  10. * Licensed for free Commercial and Private use creative commons license agreement. *
  11. * The provided code is in an "as-is" state. Richard Vacheresse makes no warranties *
  12. * regarding the provided code, and disclaims liability for damages resulting from its use. *
  13. * @version 1.0 *
  14. \***********************************************************************************************/
  15. public class ArrayQueue extends Sprite implements QueueADT
  16. {
  17. //- default capapcity of queue
  18. private var DEFAULT_CAPACITY:int = 100;
  19. //- indicates the value of the last
  20. private var rear:int;
  21. //- array object to hold the objects
  22. private var queue:Array;
  23.  
  24. /**
  25. * ArrayQueue() Constructor - Creates an empty queue using the default capacity.
  26. * @variable - DEFAULT_CAPACITY - The default value for the queue's capacity.
  27. * @variable - rear - Indicates the value of the last object + one as an Integer object.
  28. * @variable - queue - The container object that will hold the objects of type array.
  29. **/
  30. public function ArrayQueue():void
  31. {
  32. //- set rear to 0 since the queue is empty
  33. rear = 0;
  34.  
  35. // - instantiate a new array object at the default capacity of 100
  36. queue = new Array(DEFAULT_CAPACITY);
  37.  
  38. //- output to console
  39. trace("New ArrayQueue Created At Default Capacity");
  40. }
  41.  
  42. /**
  43. * ArrayQueueDefined(queueSize:int) function - Creates an empty queue using the passed in value.
  44. **/
  45. public function ArrayQueueDefined(queueSize:int):void
  46. {
  47. //- set rear to 0 since the queue is empty
  48. rear = 0;
  49.  
  50. // - instantiate a new array object at the passed value size
  51. queue = new Array(queueSize);
  52.  
  53. //- output to console
  54. trace("New ArrayQueue Created At: " + queueSize);
  55. }
  56.  
  57. /**
  58. * dequeue() function - Removes the first item in the queue.
  59. * - Then shifts the queue minus the removed object
  60. * - Decrement rear to reflect minus one object.
  61. * @return - Object
  62. **/
  63. public function dequeue():Object
  64. {
  65. //- if the collection is empty throw an error
  66. if(isEmpty())
  67. throw new Error("Queue contains no objects");
  68.  
  69. //- else remove the object at the first position
  70. var result:Object = queue[0];
  71.  
  72. //- decrement the rear by one
  73. rear--;
  74.  
  75. //- shift the elements forward one position
  76. for(var scan:int = 0; scan < rear; scan++)
  77. queue[scan] = queue[scan+1];
  78.  
  79. //- set the rear null again to null
  80. queue[rear] = null;
  81.  
  82. //- output to console
  83. trace("Item " + result + " dequeued.");
  84.  
  85. //- return the first objec in the array
  86. return result;
  87. }
  88.  
  89. /**
  90. * enqueue(obj:Object) function - Takes the passed object and adds it to the
  91. * rear indicated position.
  92. * - Increment the rear value + one to reflect
  93. * the additional object.
  94. **/
  95. public function enqueue(obj:Object):void
  96. {
  97. queue[rear] = obj;
  98. rear++;
  99. }
  100.  
  101. /**
  102. * first() function - Returns the first object in the queue but does not remove it.
  103. * @return - Object
  104. **/
  105. public function first():Object
  106. {
  107. var result:Object = "null";
  108.  
  109. if(isEmpty())
  110. throw new Error("The queue is empty");
  111. //- set result pointer equal to first item but do not remove
  112. result = queue[0];
  113. //- output to console
  114. trace("Item " + queue[0] + " is next.");
  115.  
  116. return result;
  117. }
  118.  
  119. /**
  120. * size() function - Returns the number of objects in the queue.
  121. * @return - Integer Object
  122. **/
  123. public function size():int
  124. {
  125. return rear;
  126. }
  127.  
  128. /**
  129. * getLength() accessor function - Returns an integer value of the length of the queue.
  130. * @return - Integer Object
  131. **/
  132. public function getLength():int
  133. {
  134. return queue.length;
  135. }
  136.  
  137. /**
  138. * isEmpty() function - Returns True if the value of rear is equal to zero.
  139. * @return - Boolean Object
  140. **/
  141. public function isEmpty():Boolean
  142. {
  143. return (rear == 0);
  144. }
  145.  
  146. /**
  147. * expandCapacity() function - Creates a new array of twice the size of the current
  148. * array queue.
  149. * - Then it repopulates the new larger Array with the
  150. * original values.
  151. **/
  152. public function expandCapacity():void
  153. {
  154. var result:int = (queue.length*2);
  155. var larger:Array = new Array(result);
  156.  
  157. for(var scan:int = 0; scan < queue.length; scan++)
  158. larger[scan] = queue[scan];
  159.  
  160. queue = larger;
  161. }
  162.  
  163. /**
  164. * toString():String function - Returns a custom String object to represent the queue.
  165. * - Overriden only because it is of type Sprite, (which has
  166. * by default its' own toString() function), therefore we
  167. * need more information about the queue.
  168. * @return - String Object
  169. **/
  170. override public function toString():String
  171. {
  172. var result:String = ("------------------\n" +
  173. "Queue toString()\n" +
  174. "------------------\n" +
  175. "Queue has " + size() + " items.\n");
  176.  
  177. for(var scan:int = 0; scan < rear; scan++)
  178. {
  179. result += ("Item: " + scan + " is a: " + queue[scan] + "\n");
  180. }
  181. return result;
  182. }
  183.  
  184. }
  185. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.