List.hx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. @:coreApi class List<T> implements php.IteratorAggregate<T> {
  23. @:ifFeature("List.iterator") private var h : ArrayAccess<Dynamic>;
  24. @:ifFeature("List.iterator") private var q : ArrayAccess<Dynamic>;
  25. public var length(default,null) : Int;
  26. public function new() : Void {
  27. length = 0;
  28. }
  29. public function add( item : T ) : Void {
  30. var x = untyped __call__('array', item, null);
  31. if( h == null )
  32. untyped __php__("$this->h =& $x");
  33. else
  34. untyped __php__("$this->q[1] =& $x");
  35. untyped __php__("$this->q =& $x");
  36. length++;
  37. }
  38. public function push( item : T ) : Void {
  39. var x = untyped __call__('array', item, __php__("&$this->h"));
  40. untyped __php__("$this->h =& $x");
  41. if( q == null )
  42. untyped __php__("$this->q =& $x");
  43. length++;
  44. }
  45. public function first() : Null<T> {
  46. return if( h == null ) null else h[0];
  47. }
  48. public function last() : Null<T> {
  49. return if( q == null ) null else q[0];
  50. }
  51. public function pop() : Null<T> {
  52. if( h == null )
  53. return null;
  54. var x = h[0];
  55. h = h[1];
  56. if( h == null )
  57. q = null;
  58. length--;
  59. return x;
  60. }
  61. public function isEmpty() : Bool {
  62. return (h == null);
  63. }
  64. public function clear() : Void {
  65. h = null;
  66. q = null;
  67. length = 0;
  68. }
  69. public function remove( v : T ) : Bool {
  70. var prev = null;
  71. var l = untyped __php__("& $this->h");
  72. while( l != null ) {
  73. if(untyped __php__("$l[0] === $v")) {
  74. if( prev == null )
  75. untyped __php__("$this->h =& $l[1]");
  76. else
  77. untyped __php__("$prev[1] =& $l[1]");
  78. if(untyped __physeq__(q, l))
  79. untyped __php__("$this->q =& $prev");
  80. length--;
  81. return true;
  82. }
  83. untyped __php__("$prev =& $l");
  84. untyped __php__("$l =& $l[1]");
  85. }
  86. return false;
  87. }
  88. public function iterator() : ListIterator<T> {
  89. return untyped __call__("new _hx_list_iterator", this);
  90. }
  91. public function toString() : String {
  92. var s = "";
  93. var first = true;
  94. var l = h;
  95. while( l != null ) {
  96. if( first )
  97. first = false;
  98. else
  99. s += ", ";
  100. s += Std.string(l[0]);
  101. l = l[1];
  102. }
  103. return "{" + s + "}";
  104. }
  105. public function join(sep : String) : String {
  106. var s = "";
  107. var first = true;
  108. var l = h;
  109. while( l != null ) {
  110. if( first )
  111. first = false;
  112. else
  113. s += sep;
  114. s += l[0];
  115. l = l[1];
  116. }
  117. return s;
  118. }
  119. public function filter( f : T -> Bool ) : List<T> {
  120. var l2 = new List();
  121. var l = h;
  122. while( l != null ) {
  123. var v = l[0];
  124. l = l[1];
  125. if( f(v) )
  126. l2.add(v);
  127. }
  128. return l2;
  129. }
  130. public function map<X>(f : T -> X) : List<X> {
  131. var b = new List();
  132. var l = h;
  133. while( l != null ) {
  134. var v = l[0];
  135. l = l[1];
  136. b.add(f(v));
  137. }
  138. return b;
  139. }
  140. function getIterator() : Iterator<T> {
  141. return iterator();
  142. }
  143. }
  144. @:coreType private extern class ListIterator<T> {
  145. function hasNext():Bool;
  146. function next():T;
  147. }