List.hx 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*
  2. * Copyright (C)2005-2012 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. /**
  23. A linked-list of elements. The list is composed of two-elements arrays
  24. that are chained together. It is optimized so that adding or removing an
  25. element does not imply copying the whole array content every time.
  26. **/
  27. class List<T> {
  28. private var h : Array<Dynamic>;
  29. private var q : Array<Dynamic>;
  30. /**
  31. The length of `this` List.
  32. **/
  33. public var length(default,null) : Int;
  34. /**
  35. Creates a new empty list.
  36. **/
  37. public function new() {
  38. length = 0;
  39. }
  40. /**
  41. Adds element `item` at the end of `this` List.
  42. `this.length` increases by 1.
  43. **/
  44. public function add( item : T ) {
  45. var x:Array<Dynamic> = [item];
  46. if( h == null )
  47. h = x;
  48. else
  49. q[1] = x;
  50. q = x;
  51. length++;
  52. }
  53. /**
  54. Adds element `item` at the beginning of `this` List.
  55. `this.length` increases by 1.
  56. **/
  57. public function push( item : T ) {
  58. var x : Array<Dynamic> = [item,h];
  59. h = x;
  60. if( q == null )
  61. q = x;
  62. length++;
  63. }
  64. /**
  65. Returns the first element of `this` List, or null if no elements exist.
  66. This function does not modify `this` List.
  67. **/
  68. public function first() : Null<T> {
  69. return if( h == null ) null else h[0];
  70. }
  71. /**
  72. Returns the last element of `this` List, or null if no elements exist.
  73. This function does not modify `this` List.
  74. **/
  75. public function last() : Null<T> {
  76. return if( q == null ) null else q[0];
  77. }
  78. /**
  79. Returns the first element of `this` List, or null if no elements exist.
  80. The element is removed from `this` List.
  81. **/
  82. public function pop() : Null<T> {
  83. if( h == null )
  84. return null;
  85. var x = h[0];
  86. h = h[1];
  87. if( h == null )
  88. q = null;
  89. length--;
  90. return x;
  91. }
  92. /**
  93. Tells if `this` List is empty.
  94. **/
  95. public function isEmpty() : Bool {
  96. return (h == null);
  97. }
  98. /**
  99. Empties `this` List.
  100. This function does not traverse the elements, but simply sets the
  101. internal references to null and `this.length` to 0.
  102. **/
  103. public function clear() : Void {
  104. h = null;
  105. q = null;
  106. length = 0;
  107. }
  108. /**
  109. Removes the first occurence of `v` in `this` List.
  110. If `v` is found by checking standard equality, it is removed from `this`
  111. List and the function returns true.
  112. Otherwise, false is returned.
  113. **/
  114. public function remove( v : T ) : Bool {
  115. var prev = null;
  116. var l = h;
  117. while( l != null ) {
  118. if( l[0] == v ) {
  119. if( prev == null )
  120. h = l[1];
  121. else
  122. prev[1] = l[1];
  123. if( q == l )
  124. q = prev;
  125. length--;
  126. return true;
  127. }
  128. prev = l;
  129. l = l[1];
  130. }
  131. return false;
  132. }
  133. /**
  134. Returns an iterator on the elements of the list.
  135. **/
  136. public function iterator() : Iterator<T> {
  137. var h = h;
  138. return cast {
  139. hasNext : function() {
  140. return (h != null);
  141. },
  142. next : function() {
  143. {
  144. if( h == null )
  145. return null;
  146. var x = h[0];
  147. h = h[1];
  148. return x;
  149. }
  150. }
  151. }
  152. }
  153. /**
  154. Returns a string representation of `this` List.
  155. The result is enclosed in { } with the individual elements being
  156. separated by a comma.
  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. Returns a string representation of `this` List, with `sep` separating
  176. each element.
  177. **/
  178. public function join(sep : String) {
  179. var s = new StringBuf();
  180. var first = true;
  181. var l = h;
  182. while( l != null ) {
  183. if( first )
  184. first = false;
  185. else
  186. s.add(sep);
  187. s.add(l[0]);
  188. l = l[1];
  189. }
  190. return s.toString();
  191. }
  192. /**
  193. Returns a list filtered with `f`. The returned list will contain all
  194. elements for which `f(x) == true`.
  195. **/
  196. public function filter( f : T -> Bool ) {
  197. var l2 = new List();
  198. var l = h;
  199. while( l != null ) {
  200. var v = l[0];
  201. l = l[1];
  202. if( f(v) )
  203. l2.add(v);
  204. }
  205. return l2;
  206. }
  207. /**
  208. Returns a new list where all elements have been converted by the
  209. function `f`.
  210. **/
  211. public function map<X>(f : T -> X) : List<X> {
  212. var b = new List();
  213. var l = h;
  214. while( l != null ) {
  215. var v = l[0];
  216. l = l[1];
  217. b.add(f(v));
  218. }
  219. return b;
  220. }
  221. }