Array.hx 10 KB

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