List.hx 4.1 KB

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