2
0

Array.hx 6.1 KB

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