Array.hx 9.3 KB

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