Array.hx 6.9 KB

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