Array.hx 10.0 KB

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