Array.hx 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. @:php.attribute('\\ReturnTypeWillChange')
  182. function offsetExists(offset:Int):Bool {
  183. return offset < length;
  184. }
  185. @:noCompletion @:keep
  186. @:php.attribute('\\ReturnTypeWillChange')
  187. function offsetGet(offset:Int):Ref<T> {
  188. try {
  189. return arr[offset];
  190. } catch (e:Dynamic) {
  191. return null;
  192. }
  193. }
  194. @:noCompletion @:keep
  195. @:php.attribute('\\ReturnTypeWillChange')
  196. function offsetSet(offset:Int, value:T):Void {
  197. if (length <= offset) {
  198. for(i in length...offset + 1) {
  199. arr[i] = null;
  200. }
  201. length = offset + 1;
  202. }
  203. arr[offset] = value;
  204. Syntax.code("return {0}", value);
  205. }
  206. @:noCompletion @:keep
  207. @:php.attribute('\\ReturnTypeWillChange')
  208. function offsetUnset(offset:Int):Void {
  209. if (offset >= 0 && offset < length) {
  210. Global.array_splice(arr, offset, 1);
  211. --length;
  212. }
  213. }
  214. @:noCompletion @:keep
  215. @:php.attribute('\\ReturnTypeWillChange')
  216. private function getIterator():Traversable {
  217. return new NativeArrayIterator(arr);
  218. }
  219. @:noCompletion @:keep
  220. @:native('count') //to not interfere with `Lambda.count`
  221. @:php.attribute('\\ReturnTypeWillChange')
  222. private function _hx_count():Int {
  223. return length;
  224. }
  225. @:noCompletion @:keep
  226. @:php.attribute('\\ReturnTypeWillChange')
  227. function jsonSerialize():NativeIndexedArray<T> {
  228. return arr;
  229. }
  230. static function wrap<T>(arr:NativeIndexedArray<T>):Array<T> {
  231. var a = new Array();
  232. a.arr = arr;
  233. a.length = Global.count(arr);
  234. return a;
  235. }
  236. }
  237. /**
  238. Following interfaces are required to make `Array` mimic native arrays for usage
  239. from a 3rd party PHP code.
  240. **/
  241. @:native('ArrayAccess')
  242. private extern interface ArrayAccess<K, V> {
  243. private function offsetExists(offset:K):Bool;
  244. private function offsetGet(offset:K):V;
  245. private function offsetSet(offset:K, value:V):Void;
  246. private function offsetUnset(offset:K):Void;
  247. }
  248. @:native('JsonSerializable')
  249. private extern interface JsonSerializable<T> {
  250. private function jsonSerialize():T;
  251. }
  252. @:native('IteratorAggregate')
  253. private extern interface IteratorAggregate<T> extends Traversable {
  254. private function getIterator():Traversable;
  255. }
  256. @:native('Countable')
  257. private extern interface Countable {
  258. @:native('count') //to not interfere with `Lambda.count`
  259. private function _hx_count():Int;
  260. }