Array.hx 10 KB

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