List.hx 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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:Array<Dynamic> = #if neko untyped __dollar__array(item,null) #else [item] #end;
  48. if( h == null )
  49. h = x;
  50. else
  51. q[1] = x;
  52. q = x;
  53. length++;
  54. }
  55. /**
  56. Push an element at the beginning of the list.
  57. **/
  58. public function push( item : T ) {
  59. var x : Array<Dynamic> = #if neko
  60. untyped __dollar__array(item,h)
  61. #else
  62. [item,h]
  63. #end;
  64. h = x;
  65. if( q == null )
  66. q = x;
  67. length++;
  68. }
  69. /**
  70. Returns the first element of the list, or null
  71. if the list is empty.
  72. **/
  73. public function first() : Null<T> {
  74. return if( h == null ) null else h[0];
  75. }
  76. /**
  77. Returns the last element of the list, or null
  78. if the list is empty.
  79. **/
  80. public function last() : Null<T> {
  81. return if( q == null ) null else q[0];
  82. }
  83. /**
  84. Removes the first element of the list and
  85. returns it or simply returns null if the
  86. list is empty.
  87. **/
  88. public function pop() : Null<T> {
  89. if( h == null )
  90. return null;
  91. var x = h[0];
  92. h = h[1];
  93. if( h == null )
  94. q = null;
  95. length--;
  96. return x;
  97. }
  98. /**
  99. Tells if a list is empty.
  100. **/
  101. public function isEmpty() : Bool {
  102. return (h == null);
  103. }
  104. /**
  105. Makes the list empty.
  106. **/
  107. public function clear() : Void {
  108. h = null;
  109. q = null;
  110. length = 0;
  111. }
  112. /**
  113. Remove the first element that is [== v] from the list.
  114. Returns [true] if an element was removed, [false] otherwise.
  115. **/
  116. public function remove( v : T ) : Bool {
  117. var prev = null;
  118. var l = h;
  119. while( l != null ) {
  120. if( l[0] == v ) {
  121. if( prev == null )
  122. h = l[1];
  123. else
  124. prev[1] = l[1];
  125. if( q == l )
  126. q = prev;
  127. length--;
  128. return true;
  129. }
  130. prev = l;
  131. l = l[1];
  132. }
  133. return false;
  134. }
  135. /**
  136. Returns an iterator on the elements of the list.
  137. **/
  138. public function iterator() : Iterator<T> {
  139. #if (java || cs)
  140. var h = h;
  141. return cast {
  142. hasNext : function() {
  143. return (h != null);
  144. },
  145. next : function() {
  146. {
  147. if( h == null )
  148. return null;
  149. var x = h[0];
  150. h = h[1];
  151. return x;
  152. }
  153. }
  154. }
  155. #else
  156. return cast {
  157. h : h,
  158. hasNext : function() {
  159. return untyped (__this__.h != null);
  160. },
  161. next : function() {
  162. untyped {
  163. if( __this__.h == null )
  164. return null;
  165. var x = __this__.h[0];
  166. __this__.h = __this__.h[1];
  167. return x;
  168. }
  169. }
  170. }
  171. #end
  172. }
  173. /**
  174. Returns a displayable representation of the String.
  175. **/
  176. public function toString() {
  177. var s = new StringBuf();
  178. var first = true;
  179. var l = h;
  180. s.add("{");
  181. while( l != null ) {
  182. if( first )
  183. first = false;
  184. else
  185. s.add(", ");
  186. s.add(Std.string(l[0]));
  187. l = l[1];
  188. }
  189. s.add("}");
  190. return s.toString();
  191. }
  192. /**
  193. Join the element of the list by using the separator [sep].
  194. **/
  195. public function join(sep : String) {
  196. var s = new StringBuf();
  197. var first = true;
  198. var l = h;
  199. while( l != null ) {
  200. if( first )
  201. first = false;
  202. else
  203. s.add(sep);
  204. s.add(l[0]);
  205. l = l[1];
  206. }
  207. return s.toString();
  208. }
  209. /**
  210. Returns a list filtered with [f]. The returned list
  211. will contain all elements [x] for which [f(x) = true].
  212. **/
  213. public function filter( f : T -> Bool ) {
  214. var l2 = new List();
  215. var l = h;
  216. while( l != null ) {
  217. var v = l[0];
  218. l = l[1];
  219. if( f(v) )
  220. l2.add(v);
  221. }
  222. return l2;
  223. }
  224. /**
  225. Returns a new list where all elements have been converted
  226. by the function [f].
  227. **/
  228. public function map<X>(f : T -> X) : List<X> {
  229. var b = new List();
  230. var l = h;
  231. while( l != null ) {
  232. var v = l[0];
  233. l = l[1];
  234. b.add(f(v));
  235. }
  236. return b;
  237. }
  238. }