List.hx 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /*
  2. * Copyright (C)2005-2017 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 element container objects
  24. that are chained together. It is optimized so that adding or removing an
  25. element does not imply copying the whole list content every time.
  26. @see https://haxe.org/manual/std-List.html
  27. **/
  28. class List<T> {
  29. private var h : ListNode<T>;
  30. private var q : ListNode<T>;
  31. /**
  32. The length of `this` List.
  33. **/
  34. public var length(default,null) : Int;
  35. /**
  36. Creates a new empty list.
  37. **/
  38. public function new() {
  39. length = 0;
  40. }
  41. /**
  42. Adds element `item` at the end of `this` List.
  43. `this.length` increases by 1.
  44. **/
  45. public function add( item : T ) {
  46. var x = ListNode.create(item, null);
  47. if( h == null )
  48. h = x;
  49. else
  50. q.next = x;
  51. q = x;
  52. length++;
  53. }
  54. /**
  55. Adds element `item` at the beginning of `this` List.
  56. `this.length` increases by 1.
  57. **/
  58. public function push( item : T ) {
  59. var x = ListNode.create(item, h);
  60. h = x;
  61. if( q == null )
  62. q = x;
  63. length++;
  64. }
  65. /**
  66. Returns the first element of `this` List, or null if no elements exist.
  67. This function does not modify `this` List.
  68. **/
  69. public function first() : Null<T> {
  70. return if( h == null ) null else h.item;
  71. }
  72. /**
  73. Returns the last element of `this` List, or null if no elements exist.
  74. This function does not modify `this` List.
  75. **/
  76. public function last() : Null<T> {
  77. return if( q == null ) null else q.item;
  78. }
  79. /**
  80. Returns the first element of `this` List, or null if no elements exist.
  81. The element is removed from `this` List.
  82. **/
  83. public function pop() : Null<T> {
  84. if( h == null )
  85. return null;
  86. var x = h.item;
  87. h = h.next;
  88. if( h == null )
  89. q = null;
  90. length--;
  91. return x;
  92. }
  93. /**
  94. Tells if `this` List is empty.
  95. **/
  96. public function isEmpty() : Bool {
  97. return (h == null);
  98. }
  99. /**
  100. Empties `this` List.
  101. This function does not traverse the elements, but simply sets the
  102. internal references to null and `this.length` to 0.
  103. **/
  104. public function clear() : Void {
  105. h = null;
  106. q = null;
  107. length = 0;
  108. }
  109. /**
  110. Removes the first occurrence of `v` in `this` List.
  111. If `v` is found by checking standard equality, it is removed from `this`
  112. List and the function returns true.
  113. Otherwise, false is returned.
  114. **/
  115. public function remove( v : T ) : Bool {
  116. var prev:ListNode<T> = null;
  117. var l = h;
  118. while( l != null ) {
  119. if( l.item == v ) {
  120. if( prev == null )
  121. h = l.next;
  122. else
  123. prev.next = l.next;
  124. if( q == l )
  125. q = prev;
  126. length--;
  127. return true;
  128. }
  129. prev = l;
  130. l = l.next;
  131. }
  132. return false;
  133. }
  134. /**
  135. Returns an iterator on the elements of the list.
  136. **/
  137. public inline function iterator() : ListIterator<T> {
  138. return new ListIterator<T>(h);
  139. }
  140. /**
  141. Returns a string representation of `this` List.
  142. The result is enclosed in { } with the individual elements being
  143. separated by a comma.
  144. **/
  145. public function toString() {
  146. var s = new StringBuf();
  147. var first = true;
  148. var l = h;
  149. s.add("{");
  150. while( l != null ) {
  151. if( first )
  152. first = false;
  153. else
  154. s.add(", ");
  155. s.add(Std.string(l.item));
  156. l = l.next;
  157. }
  158. s.add("}");
  159. return s.toString();
  160. }
  161. /**
  162. Returns a string representation of `this` List, with `sep` separating
  163. each element.
  164. **/
  165. public function join(sep : String) {
  166. var s = new StringBuf();
  167. var first = true;
  168. var l = h;
  169. while( l != null ) {
  170. if( first )
  171. first = false;
  172. else
  173. s.add(sep);
  174. s.add(l.item);
  175. l = l.next;
  176. }
  177. return s.toString();
  178. }
  179. /**
  180. Returns a list filtered with `f`. The returned list will contain all
  181. elements for which `f(x) == true`.
  182. **/
  183. public function filter( f : T -> Bool ) {
  184. var l2 = new List();
  185. var l = h;
  186. while( l != null ) {
  187. var v = l.item;
  188. l = l.next;
  189. if( f(v) )
  190. l2.add(v);
  191. }
  192. return l2;
  193. }
  194. /**
  195. Returns a new list where all elements have been converted by the
  196. function `f`.
  197. **/
  198. public function map<X>(f : T -> X) : List<X> {
  199. var b = new List();
  200. var l = h;
  201. while( l != null ) {
  202. var v = l.item;
  203. l = l.next;
  204. b.add(f(v));
  205. }
  206. return b;
  207. }
  208. }
  209. #if neko
  210. private extern class ListNode<T> extends neko.NativeArray<Dynamic> {
  211. var item(get,set):T;
  212. var next(get,set):ListNode<T>;
  213. private inline function get_item():T return this[0];
  214. private inline function set_item(v:T):T return this[0] = v;
  215. private inline function get_next():ListNode<T> return this[1];
  216. private inline function set_next(v:ListNode<T>):ListNode<T> return this[1] = v;
  217. inline static function create<T>(item:T, next:ListNode<T>):ListNode<T> {
  218. return untyped __dollar__array(item, next);
  219. }
  220. }
  221. #else
  222. private class ListNode<T> {
  223. public var item:T;
  224. public var next:ListNode<T>;
  225. public function new(item:T, next:ListNode<T>) {
  226. this.item = item;
  227. this.next = next;
  228. }
  229. @:extern public inline static function create<T>(item:T, next:ListNode<T>):ListNode<T> {
  230. return new ListNode(item, next);
  231. }
  232. }
  233. #end
  234. private class ListIterator<T> {
  235. var head:ListNode<T>;
  236. public inline function new(head:ListNode<T>) {
  237. this.head = head;
  238. }
  239. public inline function hasNext():Bool {
  240. return head != null;
  241. }
  242. public inline function next():T {
  243. var val = head.item;
  244. head = head.next;
  245. return val;
  246. }
  247. }