Array.hx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  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. /**
  24. An Array is a storage for values. You can access it using indexes or
  25. with its API. On the server side, it's often better to use a [List] which
  26. is less memory and CPU consuming, unless you really need indexed access.
  27. **/
  28. @:classCode('
  29. public Array(T[] native)
  30. {
  31. this.__a = native;
  32. this.length = native.Length;
  33. }
  34. ')
  35. @:final @:coreApi class Array<T> implements ArrayAccess<T> {
  36. /**
  37. The length of the Array
  38. **/
  39. public var length(default,null) : Int;
  40. private var __a:NativeArray<T>;
  41. @:functionCode('
  42. return new Array<X>(native);
  43. ')
  44. private static function ofNative<X>(native:NativeArray<X>):Array<X>
  45. {
  46. return null;
  47. }
  48. @:functionCode('
  49. return new Array<Y>(new Y[size]);
  50. ')
  51. private static function alloc<Y>(size:Int):Array<Y>
  52. {
  53. return null;
  54. }
  55. /**
  56. Creates a new Array.
  57. **/
  58. public function new() : Void
  59. {
  60. this.length = 0;
  61. this.__a = new NativeArray(0);
  62. }
  63. /**
  64. Returns a new Array by appending [a] to [this].
  65. **/
  66. public function concat( a : Array<T> ) : Array<T>
  67. {
  68. var len = length + a.length;
  69. var retarr = new NativeArray(len);
  70. cs.system.Array.Copy(__a, 0, retarr, 0, length);
  71. cs.system.Array.Copy(a.__a, 0, retarr, length, a.length);
  72. return ofNative(retarr);
  73. }
  74. private function concatNative( a : NativeArray<T> ) : Void
  75. {
  76. var __a = __a;
  77. var len = length + a.Length;
  78. if (__a.Length >= len)
  79. {
  80. cs.system.Array.Copy(a, 0, __a, length, length);
  81. } else {
  82. var newarr = new NativeArray(len);
  83. cs.system.Array.Copy(__a, 0, newarr, 0, length);
  84. cs.system.Array.Copy(a, 0, newarr, length, a.Length);
  85. this.__a = newarr;
  86. }
  87. this.length = len;
  88. }
  89. /**
  90. Returns a representation of an array with [sep] for separating each element.
  91. **/
  92. public function join( sep : String ) : String
  93. {
  94. var buf = new StringBuf();
  95. var i = -1;
  96. var first = true;
  97. var length = length;
  98. while (++i < length)
  99. {
  100. if (first)
  101. first = false;
  102. else
  103. buf.add(sep);
  104. buf.add(__a[i]);
  105. }
  106. return buf.toString();
  107. }
  108. /**
  109. Removes the last element of the array and returns it.
  110. **/
  111. public function pop() : Null<T>
  112. {
  113. var __a = __a;
  114. var length = length;
  115. if (length > 0)
  116. {
  117. var val = __a[--length];
  118. __a[length] = null;
  119. this.length = length;
  120. return val;
  121. } else {
  122. return null;
  123. }
  124. }
  125. /**
  126. Adds the element [x] at the end of the array.
  127. **/
  128. public function push(x : T) : Int
  129. {
  130. if (length >= __a.Length)
  131. {
  132. var newLen = (length << 1) + 1;
  133. var newarr = new NativeArray(newLen);
  134. __a.CopyTo(newarr, 0);
  135. this.__a = newarr;
  136. }
  137. __a[length] = x;
  138. return ++length;
  139. }
  140. /**
  141. Reverse the order of elements of the Array.
  142. **/
  143. public function reverse() : Void
  144. {
  145. var i = 0;
  146. var l = this.length;
  147. var a = this.__a;
  148. var half = l >> 1;
  149. l -= 1;
  150. while ( i < half )
  151. {
  152. var tmp = a[i];
  153. a[i] = a[l-i];
  154. a[l-i] = tmp;
  155. i += 1;
  156. }
  157. }
  158. /**
  159. Removes the first element and returns it.
  160. **/
  161. public function shift() : Null<T>
  162. {
  163. var l = this.length;
  164. if( l == 0 )
  165. return null;
  166. var a = this.__a;
  167. var x = a[0];
  168. l -= 1;
  169. cs.system.Array.Copy(a, 1, a, 0, length-1);
  170. a[l] = null;
  171. this.length = l;
  172. return x;
  173. }
  174. /**
  175. Copies the range of the array starting at [pos] up to,
  176. but not including, [end]. Both [pos] and [end] can be
  177. negative to count from the end: -1 is the last item in
  178. the array.
  179. **/
  180. public function slice( pos : Int, ?end : Int ) : Array<T>
  181. {
  182. if( pos < 0 ){
  183. pos = this.length + pos;
  184. if( pos < 0 )
  185. pos = 0;
  186. }
  187. if( end == null )
  188. end = this.length;
  189. else if( end < 0 )
  190. end = this.length + end;
  191. if( end > this.length )
  192. end = this.length;
  193. var len = end - pos;
  194. if ( len < 0 ) return new Array();
  195. var newarr = new NativeArray(len);
  196. cs.system.Array.Copy(__a, pos, newarr, 0, len);
  197. return ofNative(newarr);
  198. }
  199. /**
  200. Sort the Array according to the comparison public function [f].
  201. [f(x,y)] should return [0] if [x == y], [>0] if [x > y]
  202. and [<0] if [x < y].
  203. **/
  204. public function sort( f : T -> T -> Int ) : Void
  205. {
  206. if (length == 0)
  207. return;
  208. quicksort(0, length - 1, f);
  209. }
  210. /**
  211. quicksort author: tong disktree
  212. http://blog.disktree.net/2008/10/26/array-sort-performance.html
  213. */
  214. private function quicksort( lo : Int, hi : Int, f : T -> T -> Int ) : Void
  215. {
  216. var buf = __a;
  217. var i = lo, j = hi;
  218. var p = buf[(i + j) >> 1];
  219. while ( i <= j )
  220. {
  221. while ( f(buf[i], p) < 0 ) i++;
  222. while ( f(buf[j], p) > 0 ) j--;
  223. if ( i <= j )
  224. {
  225. var t = buf[i];
  226. buf[i++] = buf[j];
  227. buf[j--] = t;
  228. }
  229. }
  230. if( lo < j ) quicksort( lo, j, f );
  231. if( i < hi ) quicksort( i, hi, f );
  232. }
  233. /**
  234. Removes [len] elements starting from [pos] an returns them.
  235. **/
  236. public function splice( pos : Int, len : Int ) : Array<T>
  237. {
  238. if( len < 0 ) return new Array();
  239. if( pos < 0 ) {
  240. pos = this.length + pos;
  241. if( pos < 0 ) pos = 0;
  242. }
  243. if( pos > this.length ) {
  244. pos = 0;
  245. len = 0;
  246. } else if( pos + len > this.length ) {
  247. len = this.length - pos;
  248. if( len < 0 ) len = 0;
  249. }
  250. var a = this.__a;
  251. var ret = new NativeArray(len);
  252. cs.system.Array.Copy(a, pos, ret, 0, len);
  253. var ret = ofNative(ret);
  254. var end = pos + len;
  255. cs.system.Array.Copy(a, end, a, pos, this.length - end);
  256. this.length -= len;
  257. while( --len >= 0 )
  258. a[this.length + len] = null;
  259. return ret;
  260. }
  261. private function spliceVoid( pos : Int, len : Int ) : Void
  262. {
  263. if( len < 0 ) return;
  264. if( pos < 0 ) {
  265. pos = this.length + pos;
  266. if( pos < 0 ) pos = 0;
  267. }
  268. if( pos > this.length ) {
  269. pos = 0;
  270. len = 0;
  271. } else if( pos + len > this.length ) {
  272. len = this.length - pos;
  273. if( len < 0 ) len = 0;
  274. }
  275. var a = this.__a;
  276. var end = pos + len;
  277. cs.system.Array.Copy(a, end, a, pos, this.length - end);
  278. this.length -= len;
  279. while( --len >= 0 )
  280. a[this.length + len] = null;
  281. }
  282. /**
  283. Returns a displayable representation of the Array content.
  284. **/
  285. public function toString() : String
  286. {
  287. var ret = new StringBuf();
  288. var a = __a;
  289. ret.add("[");
  290. var first = true;
  291. for (i in 0...length)
  292. {
  293. if (first)
  294. first = false;
  295. else
  296. ret.add(",");
  297. ret.add(a[i]);
  298. }
  299. ret.add("]");
  300. return ret.toString();
  301. }
  302. /**
  303. Adds the element [x] at the start of the array.
  304. **/
  305. public function unshift( x : T ) : Void
  306. {
  307. var __a = __a;
  308. var length = length;
  309. if (length >= __a.Length)
  310. {
  311. var newLen = (length << 1) + 1;
  312. var newarr = new NativeArray(newLen);
  313. cs.system.Array.Copy(__a, 0, newarr, 1, length);
  314. this.__a = newarr;
  315. } else {
  316. cs.system.Array.Copy(__a, 0, __a, 1, length);
  317. }
  318. this.__a[0] = x;
  319. ++this.length;
  320. }
  321. /**
  322. Inserts the element [x] at the position [pos].
  323. All elements after [pos] are moved one index ahead.
  324. **/
  325. public function insert( pos : Int, x : T ) : Void
  326. {
  327. var l = this.length;
  328. if( pos < 0 ) {
  329. pos = l + pos;
  330. if( pos < 0 ) pos = 0;
  331. }
  332. if ( pos >= l ) {
  333. this.push(x);
  334. return;
  335. } else if (pos == 0) {
  336. this.unshift(x);
  337. return;
  338. }
  339. if (l >= __a.Length)
  340. {
  341. var newLen = (length << 1) + 1;
  342. var newarr = new NativeArray(newLen);
  343. cs.system.Array.Copy(__a, 0, newarr, 0, pos);
  344. newarr[pos] = x;
  345. cs.system.Array.Copy(__a, pos, newarr, pos + 1, l - pos);
  346. this.__a = newarr;
  347. ++this.length;
  348. } else {
  349. var __a = __a;
  350. cs.system.Array.Copy(__a, pos, __a, pos + 1, l - pos);
  351. cs.system.Array.Copy(__a, 0, __a, 0, pos);
  352. __a[pos] = x;
  353. ++this.length;
  354. }
  355. }
  356. /**
  357. Removes the first occurence of [x].
  358. Returns false if [x] was not present.
  359. Elements are compared by using standard equality.
  360. **/
  361. public function remove( x : T ) : Bool
  362. {
  363. var __a = __a;
  364. var i = -1;
  365. var length = length;
  366. while (++i < length)
  367. {
  368. if (__a[i] == x)
  369. {
  370. cs.system.Array.Copy(__a, i + 1, __a, i, length - i - 1);
  371. __a[--this.length] = null;
  372. return true;
  373. }
  374. }
  375. return false;
  376. }
  377. public function map<S>( f : T -> S ) : Array<S> {
  378. var ret = [];
  379. for (elt in this)
  380. ret.push(f(elt));
  381. return ret;
  382. }
  383. public function filter( f : T -> Bool ) : Array<T> {
  384. var ret = [];
  385. for (elt in this)
  386. if (f(elt))
  387. ret.push(elt);
  388. return ret;
  389. }
  390. /**
  391. Returns a copy of the Array. The values are not
  392. copied, only the Array structure.
  393. **/
  394. public function copy() : Array<T>
  395. {
  396. var len = length;
  397. var __a = __a;
  398. var newarr = new NativeArray(len);
  399. cs.system.Array.Copy(__a, 0, newarr, 0, len);
  400. return ofNative(newarr);
  401. }
  402. /**
  403. Returns an iterator of the Array values.
  404. **/
  405. public function iterator() : Iterator<T>
  406. {
  407. var i = 0;
  408. var len = length;
  409. return
  410. {
  411. hasNext:function() return i < len,
  412. next:function() return __a[i++]
  413. };
  414. }
  415. private function __get(idx:Int):T
  416. {
  417. var __a = __a;
  418. var idx:UInt = idx;
  419. if (idx >= length)
  420. return null;
  421. return __a[idx];
  422. }
  423. private function __set(idx:Int, v:T):T
  424. {
  425. var idx:UInt = idx;
  426. var __a = __a;
  427. if (idx >= __a.Length)
  428. {
  429. var len = idx + 1;
  430. if (idx == __a.Length)
  431. len = (idx << 1) + 1;
  432. var newArr = new NativeArray<T>(len);
  433. __a.CopyTo(newArr, 0);
  434. this.__a = __a = newArr;
  435. }
  436. if (idx >= length)
  437. this.length = idx + 1;
  438. return __a[idx] = v;
  439. }
  440. private inline function __unsafe_get(idx:Int):T
  441. {
  442. return __a[idx];
  443. }
  444. private inline function __unsafe_set(idx:Int, val:T):T
  445. {
  446. return __a[idx] = val;
  447. }
  448. }