Posted By

alejandromb on 12/20/09


Tagged

collection stack as3 actionscript3 adt


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

tspitzr


AS3 | ArrayStack


 / Published in: ActionScript 3
 

URL: http://www.kirikacode.com

  1. package kc.tda {
  2. import kc.api.IStack;
  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="push", type="kc.events.CollectionEvent" )]
  11. [Event( name="pop", type="kc.events.CollectionEvent" )]
  12.  
  13. public class ArrayStack extends ArrayCollection implements IStack {
  14.  
  15. // @constructor
  16.  
  17. public function ArrayStack( 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.PUSH,
  28. CollectionEvent.POP
  29. ];
  30.  
  31. }
  32.  
  33. // @override
  34.  
  35. override public function add( value:* ):Boolean {
  36. return push( value );
  37. }
  38.  
  39. override public function remove( value:* ):Boolean {
  40. return ( value != null )
  41. ? super.remove( value )
  42. : ( pop() != 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 push( value:* ):Boolean {
  63.  
  64. if( ! ResolveStatus() ) {
  65. return false;
  66. }
  67.  
  68. var quantity:int = size();
  69. _records.unshift( value );
  70.  
  71. if( size() != quantity ){
  72. ResolveDispatchEvent( CollectionEvent.PUSH );
  73. return true;
  74. } return false;
  75.  
  76. }
  77.  
  78. public function pop():* {
  79. var quantity:int = size();
  80. var value:* = _records.shift();
  81. if( size() != quantity ) {
  82. ResolveDispatchEvent( CollectionEvent.POP );
  83. return value;
  84. } return null;
  85. }
  86.  
  87. }
  88.  
  89. }
  90.  
  91. // @INTERFACE
  92.  
  93. package kc.api {
  94.  
  95. public interface IStack extends ICollection {
  96.  
  97. // @methods
  98.  
  99. function element():*;
  100. function peek():*;
  101. function push(value:*):Boolean;
  102. function pop():*;
  103.  
  104. }
  105.  
  106. }

Report this snippet  

You need to login to post a comment.