2
0

Array.hx 9.8 KB

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