Array.hx 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  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. else if (i >= len)
  87. {
  88. return -1;
  89. }
  90. return cs.system.Array.IndexOf(__a, x, i, len - i);
  91. }
  92. public function lastIndexOf( x : T, ?fromIndex:Int ) : Int
  93. {
  94. var len = length, i:Int = (fromIndex == null) ? len - 1 : fromIndex;
  95. if (i >= len)
  96. {
  97. i = len - 1;
  98. }
  99. else if (i < 0)
  100. {
  101. i += len;
  102. if (i < 0) return -1;
  103. }
  104. return cs.system.Array.LastIndexOf(__a, x, i, i + 1);
  105. }
  106. public function join( sep : String ) : String
  107. {
  108. var buf = new StringBuf();
  109. var i = -1;
  110. var first = true;
  111. var length = length;
  112. while (++i < length)
  113. {
  114. if (first)
  115. first = false;
  116. else
  117. buf.add(sep);
  118. buf.add(__a[i]);
  119. }
  120. return buf.toString();
  121. }
  122. public function pop() : Null<T>
  123. {
  124. var __a = __a;
  125. var length = length;
  126. if (length > 0)
  127. {
  128. var val = __a[--length];
  129. __a[length] = null;
  130. this.length = length;
  131. return val;
  132. } else {
  133. return null;
  134. }
  135. }
  136. public function push(x : T) : Int
  137. {
  138. if (length >= __a.Length)
  139. {
  140. var newLen = (length << 1) + 1;
  141. var newarr = new NativeArray(newLen);
  142. __a.CopyTo(newarr, 0);
  143. this.__a = newarr;
  144. }
  145. __a[length] = x;
  146. return ++length;
  147. }
  148. public function reverse() : Void
  149. {
  150. var i = 0;
  151. var l = this.length;
  152. var a = this.__a;
  153. var half = l >> 1;
  154. l -= 1;
  155. while ( i < half )
  156. {
  157. var tmp = a[i];
  158. a[i] = a[l-i];
  159. a[l-i] = tmp;
  160. i += 1;
  161. }
  162. }
  163. public function shift() : Null<T>
  164. {
  165. var l = this.length;
  166. if( l == 0 )
  167. return null;
  168. var a = this.__a;
  169. var x = a[0];
  170. l -= 1;
  171. cs.system.Array.Copy(a, 1, a, 0, length-1);
  172. a[l] = null;
  173. this.length = l;
  174. return x;
  175. }
  176. public function slice( pos : Int, ?end : Int ) : Array<T>
  177. {
  178. if( pos < 0 ){
  179. pos = this.length + pos;
  180. if( pos < 0 )
  181. pos = 0;
  182. }
  183. if( end == null )
  184. end = this.length;
  185. else if( end < 0 )
  186. end = this.length + end;
  187. if( end > this.length )
  188. end = this.length;
  189. var len = end - pos;
  190. if ( len < 0 ) return new Array();
  191. var newarr = new NativeArray(len);
  192. cs.system.Array.Copy(__a, pos, newarr, 0, len);
  193. return ofNative(newarr);
  194. }
  195. public function sort( f : T -> T -> Int ) : Void
  196. {
  197. if (length == 0)
  198. return;
  199. quicksort(0, length - 1, f);
  200. }
  201. private function quicksort( lo : Int, hi : Int, f : T -> T -> Int ) : Void
  202. {
  203. var buf = __a;
  204. var i = lo, j = hi;
  205. var p = buf[(i + j) >> 1];
  206. while ( i <= j )
  207. {
  208. while ( f(buf[i], p) < 0 ) i++;
  209. while ( f(buf[j], p) > 0 ) j--;
  210. if ( i <= j )
  211. {
  212. var t = buf[i];
  213. buf[i++] = buf[j];
  214. buf[j--] = t;
  215. }
  216. }
  217. if( lo < j ) quicksort( lo, j, f );
  218. if( i < hi ) quicksort( i, hi, f );
  219. }
  220. public function splice( pos : Int, len : Int ) : Array<T>
  221. {
  222. if( len < 0 ) return new Array();
  223. if( pos < 0 ) {
  224. pos = this.length + pos;
  225. if( pos < 0 ) pos = 0;
  226. }
  227. if( pos > this.length ) {
  228. pos = 0;
  229. len = 0;
  230. } else if( pos + len > this.length ) {
  231. len = this.length - pos;
  232. if( len < 0 ) len = 0;
  233. }
  234. var a = this.__a;
  235. var ret = new NativeArray(len);
  236. cs.system.Array.Copy(a, pos, ret, 0, len);
  237. var ret = ofNative(ret);
  238. var end = pos + len;
  239. cs.system.Array.Copy(a, end, a, pos, this.length - end);
  240. this.length -= len;
  241. while( --len >= 0 )
  242. a[this.length + len] = null;
  243. return ret;
  244. }
  245. private function spliceVoid( pos : Int, len : Int ) : Void
  246. {
  247. if( len < 0 ) return;
  248. if( pos < 0 ) {
  249. pos = this.length + pos;
  250. if( pos < 0 ) pos = 0;
  251. }
  252. if( pos > this.length ) {
  253. pos = 0;
  254. len = 0;
  255. } else if( pos + len > this.length ) {
  256. len = this.length - pos;
  257. if( len < 0 ) len = 0;
  258. }
  259. var a = this.__a;
  260. var end = pos + len;
  261. cs.system.Array.Copy(a, end, a, pos, this.length - end);
  262. this.length -= len;
  263. while( --len >= 0 )
  264. a[this.length + len] = null;
  265. }
  266. public function toString() : String
  267. {
  268. var ret = new StringBuf();
  269. var a = __a;
  270. ret.add("[");
  271. var first = true;
  272. for (i in 0...length)
  273. {
  274. if (first)
  275. first = false;
  276. else
  277. ret.add(",");
  278. ret.add(a[i]);
  279. }
  280. ret.add("]");
  281. return ret.toString();
  282. }
  283. public function unshift( x : T ) : Void
  284. {
  285. var __a = __a;
  286. var length = length;
  287. if (length >= __a.Length)
  288. {
  289. var newLen = (length << 1) + 1;
  290. var newarr = new NativeArray(newLen);
  291. cs.system.Array.Copy(__a, 0, newarr, 1, length);
  292. this.__a = newarr;
  293. } else {
  294. cs.system.Array.Copy(__a, 0, __a, 1, length);
  295. }
  296. this.__a[0] = x;
  297. ++this.length;
  298. }
  299. public function insert( pos : Int, x : T ) : Void
  300. {
  301. var l = this.length;
  302. if( pos < 0 ) {
  303. pos = l + pos;
  304. if( pos < 0 ) pos = 0;
  305. }
  306. if ( pos >= l ) {
  307. this.push(x);
  308. return;
  309. } else if (pos == 0) {
  310. this.unshift(x);
  311. return;
  312. }
  313. if (l >= __a.Length)
  314. {
  315. var newLen = (length << 1) + 1;
  316. var newarr = new NativeArray(newLen);
  317. cs.system.Array.Copy(__a, 0, newarr, 0, pos);
  318. newarr[pos] = x;
  319. cs.system.Array.Copy(__a, pos, newarr, pos + 1, l - pos);
  320. this.__a = newarr;
  321. ++this.length;
  322. } else {
  323. var __a = __a;
  324. cs.system.Array.Copy(__a, pos, __a, pos + 1, l - pos);
  325. cs.system.Array.Copy(__a, 0, __a, 0, pos);
  326. __a[pos] = x;
  327. ++this.length;
  328. }
  329. }
  330. public function remove( x : T ) : Bool
  331. {
  332. var __a = __a;
  333. var i = -1;
  334. var length = length;
  335. while (++i < length)
  336. {
  337. if (__a[i] == x)
  338. {
  339. cs.system.Array.Copy(__a, i + 1, __a, i, length - i - 1);
  340. __a[--this.length] = null;
  341. return true;
  342. }
  343. }
  344. return false;
  345. }
  346. public function map<S>( f : T -> S ) : Array<S> {
  347. var ret = [];
  348. for (elt in this)
  349. ret.push(f(elt));
  350. return ret;
  351. }
  352. public function filter( f : T -> Bool ) : Array<T> {
  353. var ret = [];
  354. for (elt in this)
  355. if (f(elt))
  356. ret.push(elt);
  357. return ret;
  358. }
  359. public function copy() : Array<T>
  360. {
  361. var len = length;
  362. var __a = __a;
  363. var newarr = new NativeArray(len);
  364. cs.system.Array.Copy(__a, 0, newarr, 0, len);
  365. return ofNative(newarr);
  366. }
  367. public inline function iterator() : Iterator<T>
  368. {
  369. return new ArrayIterator<T>(this);
  370. }
  371. private function __get(idx:Int):T
  372. {
  373. return if ((cast idx : UInt) >= length) null else __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. }