Array.hx 8.1 KB

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