Array.hx 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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 indexOf(x:T, ?fromIndex:Int):Int {
  137. var len = length;
  138. var i:Int = (fromIndex != null) ? fromIndex : 0;
  139. var a = __a;
  140. if (i < 0) {
  141. i += len;
  142. if (i < 0)
  143. i = 0;
  144. }
  145. while (i < len) {
  146. if (a[i] == x)
  147. return i;
  148. i++;
  149. }
  150. return -1;
  151. }
  152. public function lastIndexOf(x:T, ?fromIndex:Int):Int {
  153. var len = length;
  154. var i:Int = (fromIndex != null) ? fromIndex : len - 1;
  155. var a = __a;
  156. if (i >= len)
  157. i = len - 1;
  158. else if (i < 0)
  159. i += len;
  160. while (i >= 0) {
  161. if (a[i] == x)
  162. return i;
  163. i--;
  164. }
  165. return -1;
  166. }
  167. public function reverse():Void {
  168. var i = 0;
  169. var l = this.length;
  170. var a = this.__a;
  171. var half = l >> 1;
  172. l -= 1;
  173. while (i < half) {
  174. var tmp = a[i];
  175. a[i] = a[l - i];
  176. a[l - i] = tmp;
  177. i += 1;
  178. }
  179. }
  180. public function shift():Null<T> {
  181. var l = this.length;
  182. if (l == 0)
  183. return null;
  184. var a = this.__a;
  185. var x = a[0];
  186. l -= 1;
  187. neko.NativeArray.blit(a, 0, a, 1, l);
  188. a[l] = null;
  189. this.length = l;
  190. return x;
  191. }
  192. public function slice(pos:Int, ?end:Int):Array<T> {
  193. if (pos < 0) {
  194. pos = this.length + pos;
  195. if (pos < 0)
  196. pos = 0;
  197. }
  198. if (end == null)
  199. end = this.length;
  200. else if (end < 0)
  201. end = this.length + end;
  202. if (end > this.length)
  203. end = this.length;
  204. var len = end - pos;
  205. if (len < 0)
  206. return new Array();
  207. return new1(neko.NativeArray.sub(this.__a, pos, len), len);
  208. }
  209. public function sort(f:T->T->Int):Void {
  210. var a = this.__a;
  211. var i = 0;
  212. var l = this.length;
  213. while (i < l) {
  214. var swap = false;
  215. var j = 0;
  216. var max = l - i - 1;
  217. while (j < max) {
  218. if (f(a[j], a[j + 1]) > 0) {
  219. var tmp = a[j + 1];
  220. a[j + 1] = a[j];
  221. a[j] = tmp;
  222. swap = true;
  223. }
  224. j += 1;
  225. }
  226. if (!swap)
  227. break;
  228. i += 1;
  229. }
  230. }
  231. public function splice(pos:Int, len:Int):Array<T> {
  232. if (len < 0)
  233. return new Array();
  234. if (pos < 0) {
  235. pos = this.length + pos;
  236. if (pos < 0)
  237. pos = 0;
  238. }
  239. if (pos > this.length) {
  240. pos = 0;
  241. len = 0;
  242. } else if (pos + len > this.length) {
  243. len = this.length - pos;
  244. if (len < 0)
  245. len = 0;
  246. }
  247. var a = this.__a;
  248. var ret = new1(neko.NativeArray.sub(a, pos, len), len);
  249. var end = pos + len;
  250. neko.NativeArray.blit(a, pos, a, end, this.length - end);
  251. this.length -= len;
  252. while (--len >= 0)
  253. a[this.length + len] = null;
  254. return ret;
  255. }
  256. public inline function map<S>(f:T->S):Array<S> {
  257. var l = length;
  258. var ret = new1(neko.NativeArray.alloc(l), l);
  259. for (i in 0...l) {
  260. ret[i] = f(this[i]);
  261. }
  262. return ret;
  263. }
  264. public inline function filter(f:T->Bool):Array<T> {
  265. var ret = [];
  266. for (elt in this)
  267. if (f(elt))
  268. ret.push(elt);
  269. return ret;
  270. }
  271. public function resize(len:Int):Void {
  272. if (length < len) {
  273. __set(len - 1, null);
  274. } else if (length > len) {
  275. for (i in len...length) {
  276. __a[i] = null;
  277. }
  278. this.length = len;
  279. }
  280. }
  281. /* NEKO INTERNAL */
  282. private function __get(pos:Int):T {
  283. return this.__a[pos];
  284. }
  285. private function __set(pos:Int, v:T):T {
  286. var a = this.__a;
  287. if (this.length <= pos) {
  288. var l = pos + 1;
  289. var dlen = l - neko.NativeArray.length(a);
  290. if (dlen > 0) {
  291. if (dlen == 1) {
  292. this.__grow(l);
  293. a = this.__a;
  294. } else {
  295. a = neko.NativeArray.alloc(l);
  296. neko.NativeArray.blit(a, 0, this.__a, 0, this.length);
  297. this.__a = a;
  298. }
  299. }
  300. this.length = l;
  301. }
  302. a[pos] = v;
  303. return v;
  304. }
  305. private function __grow(l:Int):Void {
  306. var a = this.__a;
  307. var sz = neko.NativeArray.length(a);
  308. if (sz >= l) {
  309. this.length = l;
  310. return;
  311. }
  312. var big = (sz * 3) >> 1;
  313. if (big < l)
  314. big = l;
  315. var a2 = neko.NativeArray.alloc(big);
  316. neko.NativeArray.blit(a2, 0, a, 0, this.length);
  317. this.__a = a2;
  318. this.length = l;
  319. }
  320. private function __neko():neko.NativeArray<T> {
  321. var a = this.__a;
  322. var sz = neko.NativeArray.length(a);
  323. if (sz != this.length) {
  324. a = neko.NativeArray.sub(a, 0, this.length);
  325. this.__a = a;
  326. }
  327. return a;
  328. }
  329. #if !(macro || interp)
  330. static function __init__():Void {
  331. try {
  332. var msort:Dynamic = neko.Lib.load("std", "merge_sort", 3);
  333. untyped Array.prototype.sort = function(cmp) msort(__this__.__a, __this__.length, cmp);
  334. } catch (e:Dynamic) {}
  335. }
  336. #end
  337. }