List.hx 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*
  2. * Copyright (c) 2005, The haXe Project Contributors
  3. * All rights reserved.
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. *
  7. * - Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * - Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY THE HAXE PROJECT CONTRIBUTORS "AS IS" AND ANY
  14. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  15. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  16. * DISCLAIMED. IN NO EVENT SHALL THE HAXE PROJECT CONTRIBUTORS BE LIABLE FOR
  17. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  18. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  19. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  20. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  21. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  22. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  23. * DAMAGE.
  24. */
  25. /**
  26. A linked-list of elements. The list is composed of two-elements arrays
  27. that are chained together. It's optimized so that adding or removing an
  28. element doesn't imply to copy the whole array content everytime.
  29. **/
  30. class List<T> {
  31. private var h : Array<Dynamic>;
  32. private var q : Array<Dynamic>;
  33. /**
  34. The number of elements in this list.
  35. **/
  36. public var length(default,null) : Int;
  37. /**
  38. Creates a new empty list.
  39. **/
  40. public function new() {
  41. length = 0;
  42. }
  43. /**
  44. Add an element at the end of the list.
  45. **/
  46. public function add( item : T ) {
  47. var x = #if neko
  48. untyped __dollar__array(item,null)
  49. #else true
  50. [item,null]
  51. #end;
  52. if( h == null )
  53. h = x;
  54. else
  55. q[1] = x;
  56. q = x;
  57. length++;
  58. }
  59. /**
  60. Push an element at the beginning of the list.
  61. **/
  62. public function push( item : T ) {
  63. var x = #if neko
  64. untyped __dollar__array(item,h)
  65. #else true
  66. [item,h]
  67. #end;
  68. h = x;
  69. if( q == null )
  70. q = x;
  71. length++;
  72. }
  73. /**
  74. Returns the first element of the list, or null
  75. if the list is empty.
  76. **/
  77. public function first() : T {
  78. return if( h == null ) null else h[0];
  79. }
  80. /**
  81. Returns the last element of the list, or null
  82. if the list is empty.
  83. **/
  84. public function last() : T {
  85. return if( q == null ) null else q[0];
  86. }
  87. /**
  88. Removes the first element of the list and
  89. returns it or simply returns null if the
  90. list is empty.
  91. **/
  92. public function pop() : T {
  93. if( h == null )
  94. return null;
  95. var x = h[0];
  96. h = h[1];
  97. if( h == null )
  98. q = null;
  99. length--;
  100. return x;
  101. }
  102. /**
  103. Tells if a list is empty.
  104. **/
  105. public function isEmpty() : Bool {
  106. return (h == null);
  107. }
  108. /**
  109. Makes the list empty.
  110. **/
  111. public function clear() : Void {
  112. h = null;
  113. length = 0;
  114. }
  115. /**
  116. Remove the first element that is [== v] from the list.
  117. Returns [true] if an element was removed, [false] otherwise.
  118. **/
  119. public function remove( v : T ) : Bool {
  120. var prev = null;
  121. var l = h;
  122. while( l != null ) {
  123. if( l[0] == v ) {
  124. if( prev == null )
  125. h = l[1];
  126. else
  127. prev[1] = l[1];
  128. if( q == l )
  129. q = prev;
  130. length--;
  131. return true;
  132. }
  133. prev = l;
  134. l = l[1];
  135. }
  136. return false;
  137. }
  138. /**
  139. Returns an iterator on the elements of the list.
  140. **/
  141. public function iterator() : Iterator<T> {
  142. return cast {
  143. h : h,
  144. hasNext : function() {
  145. return untyped (this.h != null);
  146. },
  147. next : function() {
  148. untyped {
  149. if( this.h == null )
  150. return null;
  151. var x = this.h[0];
  152. this.h = this.h[1];
  153. return x;
  154. }
  155. }
  156. }
  157. }
  158. /**
  159. Returns a displayable representation of the String.
  160. **/
  161. public function toString() {
  162. var s = new StringBuf();
  163. var first = true;
  164. var l = h;
  165. s.add("{");
  166. while( l != null ) {
  167. if( first )
  168. first = false;
  169. else
  170. s.add(", ");
  171. s.add(l[0]);
  172. l = l[1];
  173. }
  174. s.add("}");
  175. return s.toString();
  176. }
  177. /**
  178. Join the element of the list by using the separator [sep].
  179. **/
  180. public function join(sep : String) {
  181. var s = new StringBuf();
  182. var first = true;
  183. var l = h;
  184. while( l != null ) {
  185. if( first )
  186. first = false;
  187. else
  188. s.add(sep);
  189. s.add(l[0]);
  190. l = l[1];
  191. }
  192. return s.toString();
  193. }
  194. /**
  195. Returns a list filtered with [f]. The returned list
  196. will contain all elements [x] for which [f(x) = true].
  197. **/
  198. public function filter( f : T -> Bool ) {
  199. var l2 = new List();
  200. var l = h;
  201. while( l != null ) {
  202. var v = l[0];
  203. l = l[1];
  204. if( f(v) )
  205. l2.add(v);
  206. }
  207. return l2;
  208. }
  209. /**
  210. Returns a new list where all elements have been converted
  211. by the function [f].
  212. **/
  213. public function map<X>(f : T -> X) : List<X> {
  214. var b = new List();
  215. var l = h;
  216. while( l != null ) {
  217. var v = l[0];
  218. l = l[1];
  219. b.add(f(v));
  220. }
  221. return b;
  222. }
  223. }