Array.hx 9.1 KB

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