Array.hx 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. import php.*;
  23. using php.Global;
  24. @:coreApi
  25. @:final
  26. class Array<T> implements ArrayAccess<Int,T> {
  27. public var length(default, null):Int;
  28. var arr:NativeIndexedArray<T>;
  29. public function new() {
  30. arr = new NativeIndexedArray<T>();
  31. length = 0;
  32. }
  33. public function concat(a:Array<T>):Array<T> {
  34. return wrap(Global.array_merge(arr, a.arr));
  35. }
  36. public function copy():Array<T> {
  37. return wrap(arr);
  38. }
  39. public function filter(f:T->Bool):Array<T> {
  40. return wrap(Global.array_values(Global.array_filter(arr, f)));
  41. }
  42. public function indexOf(x:T, ?fromIndex:Int):Int {
  43. if (fromIndex == null) {
  44. var index = Global.array_search(x, arr, true);
  45. if (index == false) {
  46. return -1;
  47. } else {
  48. return index;
  49. }
  50. }
  51. if (fromIndex < 0) fromIndex += length;
  52. if (fromIndex < 0) fromIndex = 0;
  53. while (fromIndex < length) {
  54. if (arr[fromIndex] == x)
  55. return fromIndex;
  56. fromIndex++;
  57. }
  58. return -1;
  59. }
  60. public function insert(pos:Int, x:T):Void {
  61. length++;
  62. Global.array_splice(arr, pos, 0, Syntax.arrayDecl(x));
  63. }
  64. @:keep
  65. public function iterator() : Iterator<T> {
  66. return new ArrayIterator(this);
  67. }
  68. public function join(sep:String):String {
  69. return Global.implode(sep, Global.array_map('strval', arr));
  70. }
  71. public function lastIndexOf(x:T, ?fromIndex:Int):Int {
  72. if (fromIndex == null || fromIndex >= length) fromIndex = length - 1;
  73. if (fromIndex < 0) fromIndex += length;
  74. while (fromIndex >= 0) {
  75. if (arr[fromIndex] == x)
  76. return fromIndex;
  77. fromIndex--;
  78. }
  79. return -1;
  80. }
  81. public inline function map<S>(f:T->S):Array<S> {
  82. var result = Syntax.arrayDecl();
  83. for(i in 0...length) {
  84. result[i] = f(arr[i]);
  85. }
  86. return wrap(result);
  87. }
  88. public inline function pop():Null<T> {
  89. if (length > 0) length--;
  90. return Global.array_pop(arr);
  91. }
  92. public inline function push(x:T):Int {
  93. arr[length] = x;
  94. return ++length;
  95. }
  96. public function remove(x:T):Bool {
  97. for (i in 0...length) {
  98. if (arr[i] == x) {
  99. Global.array_splice(arr, i, 1);
  100. length--;
  101. return true;
  102. }
  103. }
  104. return false;
  105. }
  106. public inline function reverse():Void {
  107. arr = Global.array_reverse(arr);
  108. }
  109. public inline function shift():Null<T> {
  110. if (length > 0) length--;
  111. return Global.array_shift(arr);
  112. }
  113. public function slice(pos:Int, ?end:Int):Array<T> {
  114. if (pos < 0) pos += length;
  115. if (pos < 0) pos = 0;
  116. if (end == null) {
  117. return wrap(Global.array_slice(arr, pos));
  118. } else {
  119. if (end < 0) end += length;
  120. if (end <= pos) {
  121. return [];
  122. } else {
  123. return wrap(Global.array_slice(arr, pos, end - pos));
  124. }
  125. }
  126. }
  127. public inline function sort(f:T->T->Int):Void {
  128. arr.usort(f);
  129. }
  130. public function splice(pos:Int, len:Int):Array<T> {
  131. if (len < 0) return [];
  132. var result = wrap(Global.array_splice(arr, pos, len));
  133. length -= result.length;
  134. return result;
  135. }
  136. public inline function unshift(x:T):Void {
  137. length = Global.array_unshift(arr, x);
  138. }
  139. public function toString():String {
  140. var strings = Syntax.arrayDecl();
  141. Syntax.foreach(arr, function(index:Int, value:T) {
  142. strings[index] = Boot.stringify(value);
  143. });
  144. return '[' + Global.implode(',', strings) + ']';
  145. }
  146. @:noCompletion
  147. function offsetExists( offset:Int ) : Bool {
  148. return offset < length;
  149. }
  150. @:noCompletion
  151. function offsetGet( offset:Int ) : Ref<T> {
  152. try {
  153. return arr[offset];
  154. } catch(e:Dynamic) {
  155. return null;
  156. }
  157. }
  158. @:noCompletion
  159. function offsetSet( offset:Int, value:T ) : Void {
  160. if (length <= offset) {
  161. arr = Global.array_merge(arr, Global.array_fill(0, offset + 1 - length, null));
  162. length = offset + 1;
  163. }
  164. arr[offset] = value;
  165. }
  166. @:noCompletion
  167. function offsetUnset( offset:Int ) : Void {
  168. if (offset >= 0 && offset < length ) {
  169. Global.array_splice(arr, offset, 1);
  170. --length;
  171. }
  172. }
  173. static function wrap<T>(arr:NativeIndexedArray<T>):Array<T> {
  174. var a = new Array();
  175. a.arr = arr;
  176. a.length = Global.count(arr);
  177. return a;
  178. }
  179. }
  180. private class ArrayIterator<T> {
  181. var idx:Int;
  182. var arr:Array<T>;
  183. public inline function new(arr:Array<T>) {
  184. this.arr = arr;
  185. idx = 0;
  186. }
  187. public inline function hasNext():Bool {
  188. return idx < arr.length;
  189. }
  190. public inline function next():T {
  191. return arr[idx++];
  192. }
  193. @:keep
  194. @:phpMagic
  195. function __get(method:String) {
  196. return switch(method) {
  197. case 'hasNext', 'next': Boot.closure(this, method);
  198. case _: null;
  199. }
  200. }
  201. }
  202. /**
  203. This one is required for `Array`
  204. **/
  205. @:native('ArrayAccess')
  206. private extern interface ArrayAccess<K,V> {
  207. private function offsetExists( offset:K ) : Bool;
  208. private function offsetGet( offset:K ) : V;
  209. private function offsetSet( offset:K, value:V ) : Void;
  210. private function offsetUnset( offset:K ) : Void;
  211. }