2
0

Array.hx 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. using php.Global;
  25. @:coreApi
  26. final class Array<T> implements ArrayAccess<Int, T> implements IteratorAggregate<T> implements JsonSerializable<NativeIndexedArray<T>> {
  27. public var length(default, null):Int;
  28. var arr:NativeIndexedArray<T>;
  29. public function new() {
  30. arr = new NativeIndexedArray<T>();
  31. length = 0;
  32. }
  33. public function concat(a:Array<T>):Array<T> {
  34. return wrap(Global.array_merge(arr, a.arr));
  35. }
  36. public inline function copy():Array<T> {
  37. return Syntax.clone(this);
  38. }
  39. public inline function filter(f:T->Bool):Array<T> {
  40. var result = Syntax.arrayDecl();
  41. for(item in arr) {
  42. if (f(item)) {
  43. result.push(item);
  44. }
  45. }
  46. return wrap(result);
  47. }
  48. public inline function contains(x:T):Bool {
  49. return indexOf(x) != -1;
  50. }
  51. public function indexOf(x:T, ?fromIndex:Int):Int {
  52. if (fromIndex == null && !Boot.isHxClosure(x) && !Boot.isNumber(x)) {
  53. var index = Global.array_search(x, arr, true);
  54. if (index == false) {
  55. return -1;
  56. } else {
  57. return index;
  58. }
  59. }
  60. if (fromIndex == null) {
  61. fromIndex = 0;
  62. } else {
  63. if (fromIndex < 0)
  64. fromIndex += length;
  65. if (fromIndex < 0)
  66. fromIndex = 0;
  67. }
  68. while (fromIndex < length) {
  69. if (arr[fromIndex] == x)
  70. return fromIndex;
  71. fromIndex++;
  72. }
  73. return -1;
  74. }
  75. public function insert(pos:Int, x:T):Void {
  76. length++;
  77. Global.array_splice(arr, pos, 0, Syntax.arrayDecl(x));
  78. }
  79. @:ifFeature("dynamic_read.iterator", "anon_optional_read.iterator", "anon_read.iterator")
  80. public inline function iterator():haxe.iterators.ArrayIterator<T> {
  81. return new haxe.iterators.ArrayIterator(this);
  82. }
  83. public function join(sep:String):String {
  84. return Global.implode(sep, Global.array_map(Syntax.nativeClassName(Boot) + '::stringify', arr));
  85. }
  86. public function lastIndexOf(x:T, ?fromIndex:Int):Int {
  87. if (fromIndex == null || fromIndex >= length)
  88. fromIndex = length - 1;
  89. if (fromIndex < 0)
  90. fromIndex += length;
  91. while (fromIndex >= 0) {
  92. if (arr[fromIndex] == x)
  93. return fromIndex;
  94. fromIndex--;
  95. }
  96. return -1;
  97. }
  98. public inline function map<S>(f:T->S):Array<S> {
  99. var result = Syntax.arrayDecl();
  100. for(item in arr) {
  101. result.push(f(item));
  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. for(index in 0...length) {
  117. if (arr[index] == x) {
  118. Global.array_splice(arr, index, 1);
  119. length--;
  120. result = true;
  121. 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. for(i in length...offset + 1) {
  191. arr[i] = 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. @:noCompletion @:keep
  206. private function getIterator():Traversable {
  207. return new NativeArrayIterator(arr);
  208. }
  209. @:noCompletion @:keep
  210. function jsonSerialize():NativeIndexedArray<T> {
  211. return arr;
  212. }
  213. static function wrap<T>(arr:NativeIndexedArray<T>):Array<T> {
  214. var a = new Array();
  215. a.arr = arr;
  216. a.length = Global.count(arr);
  217. return a;
  218. }
  219. }
  220. /**
  221. This one is required for `Array`
  222. **/
  223. @:native('ArrayAccess')
  224. private extern interface ArrayAccess<K, V> {
  225. private function offsetExists(offset:K):Bool;
  226. private function offsetGet(offset:K):V;
  227. private function offsetSet(offset:K, value:V):Void;
  228. private function offsetUnset(offset:K):Void;
  229. }