Array.hx 6.1 KB

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