Array.hx 7.4 KB

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