Array.hx 7.9 KB

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