2
0

Array.hx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  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 java.lang.System;
  23. import java.NativeArray;
  24. import haxe.iterators.ArrayKeyValueIterator;
  25. @:classCode('
  26. public Array(T[] _native)
  27. {
  28. this.__a = _native;
  29. this.length = _native.length;
  30. }
  31. ')
  32. @:coreApi final class Array<T> implements ArrayAccess<T> {
  33. public var length(default, null):Int;
  34. private var __a:NativeArray<T>;
  35. @:skipReflection static var __hx_toString_depth = 0;
  36. @:skipReflection static inline final __hx_defaultCapacity = 4;
  37. @:functionCode('
  38. return new Array<X>(_native);
  39. ')
  40. private static function ofNative<X>(native:NativeArray<X>):Array<X> {
  41. var a = new Array();
  42. a.length = native.length;
  43. a.__a = native;
  44. return a;
  45. }
  46. @:functionCode('
  47. return new Array<Y>((Y[]) ((java.lang.Object)new java.lang.Object[size]));
  48. ')
  49. private static function alloc<Y>(size:Int):Array<Y> {
  50. var a = new Array();
  51. a.length = size;
  52. a.__a = new java.NativeArray(size);
  53. return a;
  54. }
  55. #if jvm
  56. function getNative():NativeArray<T> {
  57. var a = new NativeArray(length);
  58. System.arraycopy(__a, 0, a, 0, length);
  59. return a;
  60. }
  61. #end
  62. public function new():Void {
  63. this.length = 0;
  64. this.__a = new NativeArray(0);
  65. }
  66. public function concat(a:Array<T>):Array<T> {
  67. var length = length;
  68. var len = length + a.length;
  69. var retarr = new NativeArray(len);
  70. System.arraycopy(__a, 0, retarr, 0, length);
  71. System.arraycopy(a.__a, 0, retarr, length, a.length);
  72. return ofNative(retarr);
  73. }
  74. private function concatNative(a:NativeArray<T>):Void {
  75. var __a = __a;
  76. var length = length;
  77. var len = length + a.length;
  78. if (__a.length >= len) {
  79. System.arraycopy(a, 0, __a, length, length);
  80. } else {
  81. var newarr = new NativeArray(len);
  82. System.arraycopy(__a, 0, newarr, 0, length);
  83. System.arraycopy(a, 0, newarr, length, a.length);
  84. this.__a = newarr;
  85. }
  86. this.length = len;
  87. }
  88. public function join(sep:String):String {
  89. var buf = new StringBuf();
  90. var i = -1;
  91. var first = true;
  92. var length = length;
  93. while (++i < length) {
  94. if (first)
  95. first = false;
  96. else
  97. buf.add(sep);
  98. buf.add(__a[i]);
  99. }
  100. return buf.toString();
  101. }
  102. public function pop():Null<T> {
  103. var __a = __a;
  104. var length = length;
  105. if (length > 0) {
  106. var val = __a[--length];
  107. __a[length] = null;
  108. this.length = length;
  109. return val;
  110. } else {
  111. return null;
  112. }
  113. }
  114. public function push(x:T):Int {
  115. var length = length;
  116. if (length >= __a.length) {
  117. var newLen = length == 0 ? __hx_defaultCapacity : (length << 1);
  118. var newarr = new NativeArray(newLen);
  119. System.arraycopy(__a, 0, newarr, 0, __a.length);
  120. this.__a = newarr;
  121. }
  122. __a[length] = x;
  123. return ++this.length;
  124. }
  125. public function reverse():Void {
  126. var i = 0;
  127. var l = this.length;
  128. var a = this.__a;
  129. var half = l >> 1;
  130. l -= 1;
  131. while (i < half) {
  132. var tmp = a[i];
  133. a[i] = a[l - i];
  134. a[l - i] = tmp;
  135. i += 1;
  136. }
  137. }
  138. public function shift():Null<T> {
  139. var l = this.length;
  140. if (l == 0)
  141. return null;
  142. var a = this.__a;
  143. var x = a[0];
  144. l -= 1;
  145. System.arraycopy(a, 1, a, 0, length - 1);
  146. a[l] = null;
  147. this.length = l;
  148. return x;
  149. }
  150. public function slice(pos:Int, ?end:Int):Array<T> {
  151. if (pos < 0) {
  152. pos = this.length + pos;
  153. if (pos < 0)
  154. pos = 0;
  155. }
  156. if (end == null)
  157. end = this.length;
  158. else if (end < 0)
  159. end = this.length + end;
  160. if (end > this.length)
  161. end = this.length;
  162. var len = end - pos;
  163. if (len < 0)
  164. return new Array();
  165. var newarr = new NativeArray(len);
  166. System.arraycopy(__a, pos, newarr, 0, len);
  167. return ofNative(newarr);
  168. }
  169. public function sort(f:T->T->Int):Void {
  170. if (length == 0)
  171. return;
  172. quicksort(0, length - 1, f);
  173. }
  174. private function quicksort(lo:Int, hi:Int, f:T->T->Int):Void {
  175. var buf = __a;
  176. var i = lo, j = hi;
  177. var p = buf[(i + j) >> 1];
  178. while (i <= j) {
  179. while (i < hi && f(buf[i], p) < 0)
  180. i++;
  181. while (j > lo && f(buf[j], p) > 0)
  182. j--;
  183. if (i <= j) {
  184. var t = buf[i];
  185. buf[i++] = buf[j];
  186. buf[j--] = t;
  187. }
  188. }
  189. if (lo < j)
  190. quicksort(lo, j, f);
  191. if (i < hi)
  192. quicksort(i, hi, f);
  193. }
  194. public function splice(pos:Int, len:Int):Array<T> {
  195. if (len < 0)
  196. return new Array();
  197. if (pos < 0) {
  198. pos = this.length + pos;
  199. if (pos < 0)
  200. pos = 0;
  201. }
  202. if (pos > this.length) {
  203. pos = 0;
  204. len = 0;
  205. } else if (pos + len > this.length) {
  206. len = this.length - pos;
  207. if (len < 0)
  208. len = 0;
  209. }
  210. var a = this.__a;
  211. var ret = new NativeArray(len);
  212. System.arraycopy(a, pos, ret, 0, len);
  213. var ret = ofNative(ret);
  214. var end = pos + len;
  215. System.arraycopy(a, end, a, pos, this.length - end);
  216. this.length -= len;
  217. while (--len >= 0)
  218. a[this.length + len] = null;
  219. return ret;
  220. }
  221. private function spliceVoid(pos:Int, len:Int):Void {
  222. if (len < 0)
  223. return;
  224. if (pos < 0) {
  225. pos = this.length + pos;
  226. if (pos < 0)
  227. pos = 0;
  228. }
  229. if (pos > this.length) {
  230. pos = 0;
  231. len = 0;
  232. } else if (pos + len > this.length) {
  233. len = this.length - pos;
  234. if (len < 0)
  235. len = 0;
  236. }
  237. var a = this.__a;
  238. var end = pos + len;
  239. System.arraycopy(a, end, a, pos, this.length - end);
  240. this.length -= len;
  241. while (--len >= 0)
  242. a[this.length + len] = null;
  243. }
  244. public function toString():String {
  245. if (__hx_toString_depth >= 5) {
  246. return "...";
  247. }
  248. ++__hx_toString_depth;
  249. try {
  250. var s = __hx_toString();
  251. --__hx_toString_depth;
  252. return s;
  253. } catch (e:Dynamic) {
  254. --__hx_toString_depth;
  255. throw(e);
  256. }
  257. }
  258. function __hx_toString():String {
  259. var ret = new StringBuf();
  260. var a = __a;
  261. ret.add("[");
  262. var first = true;
  263. for (i in 0...length) {
  264. if (first)
  265. first = false;
  266. else
  267. ret.add(",");
  268. ret.add(a[i]);
  269. }
  270. ret.add("]");
  271. return ret.toString();
  272. }
  273. public function unshift(x:T):Void {
  274. var __a = __a;
  275. var length = length;
  276. if (length >= __a.length) {
  277. var newLen = (length << 1) + 1;
  278. var newarr = new NativeArray(newLen);
  279. System.arraycopy(__a, 0, newarr, 1, length);
  280. this.__a = newarr;
  281. } else {
  282. System.arraycopy(__a, 0, __a, 1, length);
  283. }
  284. this.__a[0] = x;
  285. ++this.length;
  286. }
  287. public function insert(pos:Int, x:T):Void {
  288. var l = this.length;
  289. if (pos < 0) {
  290. pos = l + pos;
  291. if (pos < 0)
  292. pos = 0;
  293. }
  294. if (pos >= l) {
  295. this.push(x);
  296. return;
  297. } else if (pos == 0) {
  298. this.unshift(x);
  299. return;
  300. }
  301. if (l >= __a.length) {
  302. var newLen = (length << 1) + 1;
  303. var newarr = new NativeArray(newLen);
  304. System.arraycopy(__a, 0, newarr, 0, pos);
  305. newarr[pos] = x;
  306. System.arraycopy(__a, pos, newarr, pos + 1, l - pos);
  307. this.__a = newarr;
  308. ++this.length;
  309. } else {
  310. var __a = __a;
  311. System.arraycopy(__a, pos, __a, pos + 1, l - pos);
  312. System.arraycopy(__a, 0, __a, 0, pos);
  313. __a[pos] = x;
  314. ++this.length;
  315. }
  316. }
  317. public function remove(x:T):Bool {
  318. var __a = __a;
  319. var i = -1;
  320. var length = length;
  321. while (++i < length) {
  322. if (__a[i] == x) {
  323. System.arraycopy(__a, i + 1, __a, i, length - i - 1);
  324. __a[--this.length] = null;
  325. return true;
  326. }
  327. }
  328. return false;
  329. }
  330. public function contains(x:T):Bool {
  331. var __a = __a;
  332. var i = -1;
  333. var length = length;
  334. while (++i < length) {
  335. if (__a[i] == x)
  336. return true;
  337. }
  338. return false;
  339. }
  340. public function indexOf(x:T, ?fromIndex:Int):Int {
  341. var len = length, a = __a, i:Int = (fromIndex == null) ? 0 : fromIndex;
  342. if (i < 0) {
  343. i += len;
  344. if (i < 0)
  345. i = 0;
  346. }
  347. while (i < len) {
  348. if (a[i] == x)
  349. return i;
  350. i++;
  351. }
  352. return -1;
  353. }
  354. public function lastIndexOf(x:T, ?fromIndex:Int):Int {
  355. var len = length,
  356. a = __a,
  357. i:Int = (fromIndex == null) ? len - 1 : fromIndex;
  358. if (i >= len)
  359. i = len - 1;
  360. else if (i < 0)
  361. i += len;
  362. while (i >= 0) {
  363. if (a[i] == x)
  364. return i;
  365. i--;
  366. }
  367. return -1;
  368. }
  369. public function copy():Array<T> {
  370. var len = length;
  371. var __a = __a;
  372. var newarr = new NativeArray(len);
  373. System.arraycopy(__a, 0, newarr, 0, len);
  374. return ofNative(newarr);
  375. }
  376. public inline function iterator():haxe.iterators.ArrayIterator<T> {
  377. return new haxe.iterators.ArrayIterator(this);
  378. }
  379. public inline function keyValueIterator() : ArrayKeyValueIterator<T> {
  380. return new ArrayKeyValueIterator(this);
  381. }
  382. public function resize(len:Int):Void {
  383. if (length < len) {
  384. if (__a.length < len) {
  385. var newArr = new NativeArray<T>(len);
  386. if (length > 0)
  387. System.arraycopy(__a, 0, newArr, 0, length);
  388. this.__a = __a = newArr;
  389. }
  390. this.length = len;
  391. } else if (length > len) {
  392. spliceVoid(len, length - len);
  393. }
  394. }
  395. public inline function map<S>(f:T->S):Array<S> {
  396. var ret = alloc(length);
  397. for (i in 0...length)
  398. ret.__set(i, f(__get(i)));
  399. return ret;
  400. }
  401. public inline function filter(f:T->Bool):Array<T> {
  402. var ret = [];
  403. for (i in 0...length) {
  404. var elt = __get(i);
  405. if (f(elt))
  406. ret.push(elt);
  407. }
  408. return ret;
  409. }
  410. private function __get(idx:Int):T {
  411. var __a = __a;
  412. if (idx >= __a.length || idx < 0)
  413. return null;
  414. return __a[idx];
  415. }
  416. private function __set(idx:Int, v:T):#if jvm Void #else T #end
  417. {
  418. var __a = __a;
  419. if (idx >= __a.length) {
  420. var newl = idx + 1;
  421. if (idx == __a.length)
  422. newl = (idx << 1) + 1;
  423. var newArr = new NativeArray<T>(newl);
  424. if (length > 0)
  425. System.arraycopy(__a, 0, newArr, 0, length);
  426. this.__a = __a = newArr;
  427. }
  428. if (idx >= length)
  429. this.length = idx + 1;
  430. #if !jvm return #end __a[idx] = v;
  431. }
  432. private inline function __unsafe_get(idx:Int):T {
  433. return __a[idx];
  434. }
  435. private inline function __unsafe_set(idx:Int, val:T):T {
  436. return __a[idx] = val;
  437. }
  438. }