Array.hx 10 KB

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