AS3 | ArrayQueue


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



Copy this code and paste it in your HTML
  1. package kc.tda {
  2. import kc.api.IQueue;
  3. import kc.events.CollectionEvent;
  4.  
  5. [Event( name="addAll", type="kc.events.CollectionEvent" )]
  6. [Event( name="clear", type="kc.events.CollectionEvent" )]
  7. [Event( name="remove", type="kc.events.CollectionEvent" )]
  8. [Event( name="removeAll", type="kc.events.CollectionEvent" )]
  9. [Event( name="retain", type="kc.events.CollectionEvent" )]
  10. [Event( name="enqueue", type="kc.events.CollectionEvent" )]
  11. [Event( name="dequeue", type="kc.events.CollectionEvent" )]
  12.  
  13. public class ArrayQueue extends ArrayCollection implements IQueue {
  14.  
  15. // @constructor
  16.  
  17. public function ArrayQueue( capacity:int = undefined, expandableCapacity:Boolean = false, loadFactor:Number = NaN ) {
  18.  
  19. super( capacity, expandableCapacity, loadFactor );
  20.  
  21. _events = [
  22. CollectionEvent.ADD_ALL,
  23. CollectionEvent.CLEAR,
  24. CollectionEvent.REMOVE,
  25. CollectionEvent.REMOVE_ALL,
  26. CollectionEvent.RETAIN,
  27. CollectionEvent.ENQUEUE,
  28. CollectionEvent.DEQUEUE
  29. ];
  30.  
  31. }
  32.  
  33. // @override
  34.  
  35. override public function add( value:* ):Boolean {
  36. return enqueue( value );
  37. }
  38.  
  39. override public function remove( value:* ):Boolean {
  40. return ( value != null )
  41. ? super.remove( value )
  42. : ( dequeue() != null )
  43. ? true
  44. : false;
  45. }
  46.  
  47. // @methods
  48.  
  49. public function element():* {
  50. ThrowIsEmpty();
  51. return _records[0];
  52. }
  53.  
  54. public function peek():* {
  55. try{
  56. var value:* = element();
  57. }catch(e:Error){
  58. return null;
  59. } return value;
  60. }
  61.  
  62. public function enqueue( value:* ):Boolean {
  63.  
  64. if( ! ResolveStatus() ) {
  65. return false;
  66. }
  67.  
  68. var quantity:int = size();
  69. _records.push( value );
  70.  
  71. if( size() != quantity ){
  72. ResolveDispatchEvent( CollectionEvent.ENQUEUE );
  73. return true;
  74. } return false;
  75.  
  76. }
  77.  
  78. public function dequeue():* {
  79. var quantity:int = size();
  80. var value:* = _records.shift();
  81. if( size() != quantity ) {
  82. ResolveDispatchEvent( CollectionEvent.DEQUEUE );
  83. return value;
  84. } return null;
  85. }
  86.  
  87. }
  88.  
  89. }
  90.  
  91. // @INTERFACE
  92.  
  93.  
  94. package kc.api {
  95.  
  96. public interface IQueue extends ICollection {
  97.  
  98. // @methods
  99.  
  100. function element():*;
  101. function peek():*;
  102. function enqueue(value:*):Boolean;
  103. function dequeue():*;
  104.  
  105. }
  106.  
  107. }

URL: http://www.kirikacode.com

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.