Array.hx 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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. @:coreApi final class Array<T> {
  23. private var __a:neko.NativeArray<T>;
  24. public var length(default, null):Int;
  25. public function new():Void {
  26. this.__a = neko.NativeArray.alloc(0);
  27. this.length = 0;
  28. }
  29. private static function new1<T>(a:neko.NativeArray<T>, l:Int):Array<T> {
  30. var inst = new Array<T>();
  31. inst.__a = a;
  32. inst.length = l;
  33. return inst;
  34. }
  35. public function concat(a:Array<T>):Array<T> {
  36. var a1 = this.__a;
  37. var a2 = a.__a;
  38. var s1 = this.length;
  39. var s2 = a.length;
  40. var a = neko.NativeArray.alloc(s1 + s2);
  41. neko.NativeArray.blit(a, 0, a1, 0, s1);
  42. neko.NativeArray.blit(a, s1, a2, 0, s2);
  43. return new1(a, s1 + s2);
  44. }
  45. public function copy():Array<T> {
  46. return new1(neko.NativeArray.sub(this.__a, 0, this.length), this.length);
  47. }
  48. public function iterator():Iterator<T> {
  49. return untyped {
  50. a: this,
  51. p: 0,
  52. hasNext: function() {
  53. return __this__.p < __this__.a.length;
  54. },
  55. next: function() {
  56. var i = __this__.a.__a[__this__.p];
  57. __this__.p += 1;
  58. return i;
  59. }
  60. };
  61. }
  62. public function insert(pos:Int, x:T):Void {
  63. var l = this.length;
  64. if (pos < 0) {
  65. pos = l + pos;
  66. if (pos < 0)
  67. pos = 0;
  68. }
  69. if (pos > l)
  70. pos = l;
  71. this.__grow(l + 1);
  72. var a = this.__a;
  73. neko.NativeArray.blit(a, pos + 1, a, pos, l - pos);
  74. a[pos] = x;
  75. }
  76. public function join(sep:String):String {
  77. var s = new StringBuf();
  78. var a = this.__a;
  79. var max = this.length - 1;
  80. for (p in 0...this.length) {
  81. s.add(a[p]);
  82. if (p != max)
  83. s.add(sep);
  84. }
  85. return s.toString();
  86. }
  87. static var __hx_toString_depth = 0;
  88. public function toString():String {
  89. if (__hx_toString_depth >= 5) {
  90. return "...";
  91. }
  92. var s = new StringBuf();
  93. s.add("[");
  94. var it = iterator();
  95. __hx_toString_depth++;
  96. try {
  97. for (i in it) {
  98. s.add(i);
  99. if (it.hasNext())
  100. s.addChar(",".code);
  101. }
  102. __hx_toString_depth--;
  103. } catch (e:Dynamic) {
  104. __hx_toString_depth--;
  105. neko.Lib.rethrow(e);
  106. }
  107. s.add("]");
  108. return s.toString();
  109. }
  110. public function pop():Null<T> {
  111. if (this.length == 0)
  112. return null;
  113. this.length -= 1;
  114. var x = this.__a[this.length];
  115. this.__a[this.length] = null;
  116. return x;
  117. }
  118. public function push(x:T):Int {
  119. var l = this.length;
  120. this.__grow(l + 1);
  121. this.__a[l] = x;
  122. return l + 1;
  123. }
  124. public function unshift(x:T):Void {
  125. var l = this.length;
  126. this.__grow(l + 1);
  127. var a = this.__a;
  128. neko.NativeArray.blit(a, 1, a, 0, l);
  129. a[0] = x;
  130. }
  131. public function remove(x:T):Bool {
  132. var i = 0;
  133. var l = this.length;
  134. var a = this.__a;
  135. while (i < l) {
  136. if (a[i] == x) {
  137. neko.NativeArray.blit(a, i, a, i + 1, l - i - 1);
  138. l -= 1;
  139. this.length = l;
  140. a[l] = null;
  141. return true;
  142. }
  143. i += 1;
  144. }
  145. return false;
  146. }
  147. public function indexOf(x:T, ?fromIndex:Int):Int {
  148. var len = length;
  149. var i:Int = (fromIndex != null) ? fromIndex : 0;
  150. var a = __a;
  151. if (i < 0) {
  152. i += len;
  153. if (i < 0)
  154. i = 0;
  155. }
  156. while (i < len) {
  157. if (a[i] == x)
  158. return i;
  159. i++;
  160. }
  161. return -1;
  162. }
  163. public function lastIndexOf(x:T, ?fromIndex:Int):Int {
  164. var len = length;
  165. var i:Int = (fromIndex != null) ? fromIndex : len - 1;
  166. var a = __a;
  167. if (i >= len)
  168. i = len - 1;
  169. else if (i < 0)
  170. i += len;
  171. while (i >= 0) {
  172. if (a[i] == x)
  173. return i;
  174. i--;
  175. }
  176. return -1;
  177. }
  178. public function reverse():Void {
  179. var i = 0;
  180. var l = this.length;
  181. var a = this.__a;
  182. var half = l >> 1;
  183. l -= 1;
  184. while (i < half) {
  185. var tmp = a[i];
  186. a[i] = a[l - i];
  187. a[l - i] = tmp;
  188. i += 1;
  189. }
  190. }
  191. public function shift():Null<T> {
  192. var l = this.length;
  193. if (l == 0)
  194. return null;
  195. var a = this.__a;
  196. var x = a[0];
  197. l -= 1;
  198. neko.NativeArray.blit(a, 0, a, 1, l);
  199. a[l] = null;
  200. this.length = l;
  201. return x;
  202. }
  203. public function slice(pos:Int, ?end:Int):Array<T> {
  204. if (pos < 0) {
  205. pos = this.length + pos;
  206. if (pos < 0)
  207. pos = 0;
  208. }
  209. if (end == null)
  210. end = this.length;
  211. else if (end < 0)
  212. end = this.length + end;
  213. if (end > this.length)
  214. end = this.length;
  215. var len = end - pos;
  216. if (len < 0)
  217. return new Array();
  218. return new1(neko.NativeArray.sub(this.__a, pos, len), len);
  219. }
  220. public function sort(f:T->T->Int):Void {
  221. var a = this.__a;
  222. var i = 0;
  223. var l = this.length;
  224. while (i < l) {
  225. var swap = false;
  226. var j = 0;
  227. var max = l - i - 1;
  228. while (j < max) {
  229. if (f(a[j], a[j + 1]) > 0) {
  230. var tmp = a[j + 1];
  231. a[j + 1] = a[j];
  232. a[j] = tmp;
  233. swap = true;
  234. }
  235. j += 1;
  236. }
  237. if (!swap)
  238. break;
  239. i += 1;
  240. }
  241. }
  242. public function splice(pos:Int, len:Int):Array<T> {
  243. if (len < 0)
  244. return new Array();
  245. if (pos < 0) {
  246. pos = this.length + pos;
  247. if (pos < 0)
  248. pos = 0;
  249. }
  250. if (pos > this.length) {
  251. pos = 0;
  252. len = 0;
  253. } else if (pos + len > this.length) {
  254. len = this.length - pos;
  255. if (len < 0)
  256. len = 0;
  257. }
  258. var a = this.__a;
  259. var ret = new1(neko.NativeArray.sub(a, pos, len), len);
  260. var end = pos + len;
  261. neko.NativeArray.blit(a, pos, a, end, this.length - end);
  262. this.length -= len;
  263. while (--len >= 0)
  264. a[this.length + len] = null;
  265. return ret;
  266. }
  267. public inline function map<S>(f:T->S):Array<S> {
  268. var l = length;
  269. var ret = new1(neko.NativeArray.alloc(l), l);
  270. for (i in 0...l) {
  271. ret[i] = f(this[i]);
  272. }
  273. return ret;
  274. }
  275. public inline function filter(f:T->Bool):Array<T> {
  276. var ret = [];
  277. for (elt in this)
  278. if (f(elt))
  279. ret.push(elt);
  280. return ret;
  281. }
  282. public function resize(len:Int):Void {
  283. if (length < len) {
  284. __set(len - 1, null);
  285. } else if (length > len) {
  286. for (i in len...length) {
  287. __a[i] = null;
  288. }
  289. this.length = len;
  290. }
  291. }
  292. /* NEKO INTERNAL */
  293. private function __get(pos:Int):T {
  294. return this.__a[pos];
  295. }
  296. private function __set(pos:Int, v:T):T {
  297. var a = this.__a;
  298. if (this.length <= pos) {
  299. var l = pos + 1;
  300. var dlen = l - neko.NativeArray.length(a);
  301. if (dlen > 0) {
  302. if (dlen == 1) {
  303. this.__grow(l);
  304. a = this.__a;
  305. } else {
  306. a = neko.NativeArray.alloc(l);
  307. neko.NativeArray.blit(a, 0, this.__a, 0, this.length);
  308. this.__a = a;
  309. }
  310. }
  311. this.length = l;
  312. }
  313. a[pos] = v;
  314. return v;
  315. }
  316. private function __grow(l:Int):Void {
  317. var a = this.__a;
  318. var sz = neko.NativeArray.length(a);
  319. if (sz >= l) {
  320. this.length = l;
  321. return;
  322. }
  323. var big = (sz * 3) >> 1;
  324. if (big < l)
  325. big = l;
  326. var a2 = neko.NativeArray.alloc(big);
  327. neko.NativeArray.blit(a2, 0, a, 0, this.length);
  328. this.__a = a2;
  329. this.length = l;
  330. }
  331. private function __neko():neko.NativeArray<T> {
  332. var a = this.__a;
  333. var sz = neko.NativeArray.length(a);
  334. if (sz != this.length) {
  335. a = neko.NativeArray.sub(a, 0, this.length);
  336. this.__a = a;
  337. }
  338. return a;
  339. }
  340. #if !(macro || interp)
  341. static function __init__():Void {
  342. try {
  343. var msort:Dynamic = neko.Lib.load("std", "merge_sort", 3);
  344. untyped Array.prototype.sort = function(cmp) msort(__this__.__a, __this__.length, cmp);
  345. } catch (e:Dynamic) {}
  346. }
  347. #end
  348. }