Array.hx 9.3 KB

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