List.hx 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. #if php
  32. private var h : ArrayAccess<Dynamic>;
  33. private var q : ArrayAccess<Dynamic>;
  34. #else
  35. private var h : Array<Dynamic>;
  36. private var q : Array<Dynamic>;
  37. #end
  38. /**
  39. The number of elements in this list.
  40. **/
  41. public var length(default,null) : Int;
  42. /**
  43. Creates a new empty list.
  44. **/
  45. public function new() {
  46. length = 0;
  47. }
  48. /**
  49. Add an element at the end of the list.
  50. **/
  51. public function add( item : T ) {
  52. var x = #if neko untyped __dollar__array(item,null) #elseif php untyped __call__('array', item, null) #else [item] #end;
  53. if( h == null )
  54. #if php
  55. untyped __php__("$this->h =& $x");
  56. else
  57. untyped __php__("$this->q[1] =& $x");
  58. untyped __php__("$this->q =& $x");
  59. #else
  60. h = x;
  61. else
  62. q[1] = x;
  63. q = x;
  64. #end
  65. length++;
  66. }
  67. /**
  68. Push an element at the beginning of the list.
  69. **/
  70. public function push( item : T ) {
  71. var x = #if neko
  72. untyped __dollar__array(item,h)
  73. #elseif php
  74. untyped __call__('array', item, __php__("&$this->h"))
  75. #else
  76. [item,h]
  77. #end;
  78. #if php
  79. untyped __php__("$this->h =& $x");
  80. if( q == null )
  81. untyped __php__("$this->q =& $x");
  82. #else
  83. h = x;
  84. if( q == null )
  85. q = x;
  86. #end
  87. length++;
  88. }
  89. /**
  90. Returns the first element of the list, or null
  91. if the list is empty.
  92. **/
  93. public function first() : T {
  94. return if( h == null ) null else h[0];
  95. }
  96. /**
  97. Returns the last element of the list, or null
  98. if the list is empty.
  99. **/
  100. public function last() : T {
  101. return if( q == null ) null else q[0];
  102. }
  103. /**
  104. Removes the first element of the list and
  105. returns it or simply returns null if the
  106. list is empty.
  107. **/
  108. public function pop() : T {
  109. if( h == null )
  110. return null;
  111. var x = h[0];
  112. h = h[1];
  113. if( h == null )
  114. q = null;
  115. length--;
  116. return x;
  117. }
  118. /**
  119. Tells if a list is empty.
  120. **/
  121. public function isEmpty() : Bool {
  122. return (h == null);
  123. }
  124. /**
  125. Makes the list empty.
  126. **/
  127. public function clear() : Void {
  128. h = null;
  129. length = 0;
  130. }
  131. /**
  132. Remove the first element that is [== v] from the list.
  133. Returns [true] if an element was removed, [false] otherwise.
  134. **/
  135. public function remove( v : T ) : Bool {
  136. var prev = null;
  137. #if php
  138. var l = null;
  139. untyped __php__("$l =& $this->h");
  140. while( l != null ) {
  141. if( l[0] == v ) {
  142. if( prev == null )
  143. untyped __php__("$this->h =& $l[1]");
  144. else
  145. untyped __php__("$prev[1] =& $l[1]");
  146. if(untyped __physeq__(q, l))
  147. untyped __php__("$this->q =& $prev");
  148. length--;
  149. return true;
  150. }
  151. untyped __php__("$prev =& $l");
  152. untyped __php__("$l =& $l[1]");
  153. }
  154. #else
  155. var l = h;
  156. while( l != null ) {
  157. if( l[0] == v ) {
  158. if( prev == null )
  159. h = l[1];
  160. else
  161. prev[1] = l[1];
  162. if( q == l )
  163. q = prev;
  164. length--;
  165. return true;
  166. }
  167. prev = l;
  168. l = l[1];
  169. }
  170. #end
  171. return false;
  172. }
  173. /**
  174. Returns an iterator on the elements of the list.
  175. **/
  176. public function iterator() : Iterator<T> {
  177. #if php
  178. var it = null;
  179. it = untyped {
  180. h : h,
  181. hasNext : function() {
  182. return it.h != null;
  183. },
  184. next : function() {
  185. if( it.h == null )
  186. return null;
  187. var x = it.h[0];
  188. it.h = it.h[1];
  189. return x;
  190. }
  191. };
  192. return cast it;
  193. #else
  194. return cast {
  195. h : h,
  196. hasNext : function() {
  197. return untyped (this.h != null);
  198. },
  199. next : function() {
  200. untyped {
  201. if( this.h == null )
  202. return null;
  203. var x = this.h[0];
  204. this.h = this.h[1];
  205. return x;
  206. }
  207. }
  208. }
  209. #end
  210. }
  211. /**
  212. Returns a displayable representation of the String.
  213. **/
  214. public function toString() {
  215. var s = new StringBuf();
  216. var first = true;
  217. var l = h;
  218. s.add("{");
  219. while( l != null ) {
  220. if( first )
  221. first = false;
  222. else
  223. s.add(", ");
  224. s.add(l[0]);
  225. l = l[1];
  226. }
  227. s.add("}");
  228. return s.toString();
  229. }
  230. /**
  231. Join the element of the list by using the separator [sep].
  232. **/
  233. public function join(sep : String) {
  234. var s = new StringBuf();
  235. var first = true;
  236. var l = h;
  237. while( l != null ) {
  238. if( first )
  239. first = false;
  240. else
  241. s.add(sep);
  242. s.add(l[0]);
  243. l = l[1];
  244. }
  245. return s.toString();
  246. }
  247. /**
  248. Returns a list filtered with [f]. The returned list
  249. will contain all elements [x] for which [f(x) = true].
  250. **/
  251. public function filter( f : T -> Bool ) {
  252. var l2 = new List();
  253. var l = h;
  254. while( l != null ) {
  255. var v = l[0];
  256. l = l[1];
  257. if( f(v) )
  258. l2.add(v);
  259. }
  260. return l2;
  261. }
  262. /**
  263. Returns a new list where all elements have been converted
  264. by the function [f].
  265. **/
  266. public function map<X>(f : T -> X) : List<X> {
  267. var b = new List();
  268. var l = h;
  269. while( l != null ) {
  270. var v = l[0];
  271. l = l[1];
  272. b.add(f(v));
  273. }
  274. return b;
  275. }
  276. }