GenericStack.hx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. * Copyright (C)2005-2016 Haxe Foundation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  20. * DEALINGS IN THE SOFTWARE.
  21. */
  22. package haxe.ds;
  23. #if (flash || cpp)
  24. @:generic
  25. #end
  26. class GenericCell<T> {
  27. public var elt : T;
  28. public var next : GenericCell<T>;
  29. public function new(elt,next) { this.elt = elt; this.next = next; }
  30. }
  31. #if cpp
  32. @:generic
  33. private class GenericStackIterator<T> extends cpp.FastIterator<T> {
  34. public var current : GenericCell<T>;
  35. override public function hasNext():Bool { return current!=null; }
  36. override public function next():T { var result = current.elt; current = current.next; return result; }
  37. public function new(head) { current = head; }
  38. }
  39. #end
  40. /**
  41. A stack of elements.
  42. This class is generic, which means one type is generated for each type
  43. parameter T on static targets. For example:
  44. - `new GenericStack<Int>()` generates `GenericStack_Int`
  45. - `new GenericStack<String>()` generates `GenericStack_String`
  46. The generated name is an implementation detail and should not be relied
  47. upon.
  48. @see http://haxe.org/manual/std-GenericStack.html
  49. **/
  50. #if (flash || cpp)
  51. @:generic
  52. #end
  53. class GenericStack<T> {
  54. public var head : GenericCell<T>;
  55. /**
  56. Creates a new empty GenericStack.
  57. **/
  58. public function new() {
  59. }
  60. /**
  61. Pushes element `item` onto the stack.
  62. **/
  63. public inline function add( item : T ) {
  64. head = new GenericCell<T>(item,head);
  65. }
  66. /**
  67. Returns the topmost stack element without removing it.
  68. If the stack is empty, null is returned.
  69. **/
  70. public inline function first() : Null<T> {
  71. return if( head == null ) null else head.elt;
  72. }
  73. /**
  74. Returns the topmost stack element and removes it.
  75. If the stack is empty, null is returned.
  76. **/
  77. public inline function pop() : Null<T> {
  78. var k = head;
  79. if( k== null )
  80. return null;
  81. else {
  82. head = k.next;
  83. return k.elt;
  84. }
  85. }
  86. /**
  87. Tells if the stack is empty.
  88. **/
  89. public inline function isEmpty() : Bool {
  90. return (head == null);
  91. }
  92. /**
  93. Removes the first element which is equal to `v` according to the `==`
  94. operator.
  95. This method traverses the stack until it finds a matching element and
  96. unlinks it, returning true.
  97. If no matching element is found, false is returned.
  98. **/
  99. public function remove( v : T ) : Bool {
  100. var prev:GenericCell<T> = null;
  101. var l = head;
  102. while( l != null ) {
  103. if( l.elt == v ) {
  104. if( prev == null )
  105. head = l.next;
  106. else
  107. prev.next = l.next;
  108. break;
  109. }
  110. prev = l;
  111. l = l.next;
  112. }
  113. return (l != null);
  114. }
  115. #if cpp
  116. /**
  117. Returns an iterator over the elements of `this` GenericStack.
  118. **/
  119. public function iterator() : Iterator<T> {
  120. return new GenericStackIterator<T>(head);
  121. }
  122. #else
  123. /**
  124. Returns an iterator over the elements of `this` GenericStack.
  125. **/
  126. public function iterator() : Iterator<T> {
  127. var l = head;
  128. return {
  129. hasNext : function() {
  130. return l != null;
  131. },
  132. next : function() {
  133. var k = l;
  134. l = k.next;
  135. return k.elt;
  136. }
  137. };
  138. }
  139. #end
  140. /**
  141. Returns a String representation of `this` GenericStack.
  142. **/
  143. public function toString() {
  144. var a = new Array();
  145. var l = head;
  146. while( l != null ) {
  147. a.push(l.elt);
  148. l = l.next;
  149. }
  150. return "{"+a.join(",")+"}";
  151. }
  152. }