Array.hx 8.5 KB

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