List.hx 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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 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 = #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() : 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() : 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() : 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. return cast {
  140. h : h,
  141. hasNext : function() {
  142. return untyped (__this__.h != null);
  143. },
  144. next : function() {
  145. untyped {
  146. if( __this__.h == null )
  147. return null;
  148. var x = __this__.h[0];
  149. __this__.h = __this__.h[1];
  150. return x;
  151. }
  152. }
  153. }
  154. }
  155. /**
  156. Returns a displayable representation of the String.
  157. **/
  158. public function toString() {
  159. var s = new StringBuf();
  160. var first = true;
  161. var l = h;
  162. s.add("{");
  163. while( l != null ) {
  164. if( first )
  165. first = false;
  166. else
  167. s.add(", ");
  168. s.add(Std.string(l[0]));
  169. l = l[1];
  170. }
  171. s.add("}");
  172. return s.toString();
  173. }
  174. /**
  175. Join the element of the list by using the separator [sep].
  176. **/
  177. public function join(sep : String) {
  178. var s = new StringBuf();
  179. var first = true;
  180. var l = h;
  181. while( l != null ) {
  182. if( first )
  183. first = false;
  184. else
  185. s.add(sep);
  186. s.add(l[0]);
  187. l = l[1];
  188. }
  189. return s.toString();
  190. }
  191. /**
  192. Returns a list filtered with [f]. The returned list
  193. will contain all elements [x] for which [f(x) = true].
  194. **/
  195. public function filter( f : T -> Bool ) {
  196. var l2 = new List();
  197. var l = h;
  198. while( l != null ) {
  199. var v = l[0];
  200. l = l[1];
  201. if( f(v) )
  202. l2.add(v);
  203. }
  204. return l2;
  205. }
  206. /**
  207. Returns a new list where all elements have been converted
  208. by the function [f].
  209. **/
  210. public function map<X>(f : T -> X) : List<X> {
  211. var b = new List();
  212. var l = h;
  213. while( l != null ) {
  214. var v = l[0];
  215. l = l[1];
  216. b.add(f(v));
  217. }
  218. return b;
  219. }
  220. }