Array.hx 6.1 KB

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