Array.hx 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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. @:coreApi @:final class Array<T> {
  23. private var __a : neko.NativeArray<T>;
  24. public var length(default,null) : Int;
  25. public function new() : Void {
  26. this.__a = neko.NativeArray.alloc(0);
  27. this.length = 0;
  28. }
  29. private static function new1<T>(a:neko.NativeArray<T>,l:Int) : Array<T> {
  30. var inst = new Array<T>();
  31. inst.__a = a;
  32. inst.length = l;
  33. return inst;
  34. }
  35. public function concat( a : Array<T>) : Array<T> {
  36. var a1 = this.__a;
  37. var a2 = a.__a;
  38. var s1 = this.length;
  39. var s2 = a.length;
  40. var a = neko.NativeArray.alloc(s1+s2);
  41. neko.NativeArray.blit(a,0,a1,0,s1);
  42. neko.NativeArray.blit(a,s1,a2,0,s2);
  43. return new1(a,s1+s2);
  44. }
  45. public function copy() : Array<T> {
  46. return new1(neko.NativeArray.sub(this.__a,0,this.length),this.length);
  47. }
  48. public function iterator() : Iterator<T> {
  49. return untyped {
  50. a : this,
  51. p : 0,
  52. hasNext : function() {
  53. return __this__.p < __this__.a.length;
  54. },
  55. next : function() {
  56. var i = __this__.a.__a[__this__.p];
  57. __this__.p += 1;
  58. return i;
  59. }
  60. };
  61. }
  62. public function insert( pos : Int, x : T ) : Void {
  63. var l = this.length;
  64. if( pos < 0 ) {
  65. pos = l + pos;
  66. if( pos < 0 ) pos = 0;
  67. }
  68. if( pos > l ) pos = l;
  69. this.__grow(l+1);
  70. var a = this.__a;
  71. neko.NativeArray.blit(a,pos+1,a,pos,l-pos);
  72. a[pos] = x;
  73. }
  74. public function join( sep : String ) : String {
  75. var s = new StringBuf();
  76. var a = this.__a;
  77. var max = this.length - 1;
  78. for( p in 0...this.length ) {
  79. s.add(a[p]);
  80. if( p != max )
  81. s.add(sep);
  82. }
  83. return s.toString();
  84. }
  85. public function toString() : String {
  86. var s = new StringBuf();
  87. s.add("[");
  88. var it = iterator();
  89. for( i in it ) {
  90. s.add(i);
  91. if( it.hasNext() )
  92. s.addChar(",".code);
  93. }
  94. s.add("]");
  95. return s.toString();
  96. }
  97. public function pop() : Null<T> {
  98. if( this.length == 0 )
  99. return null;
  100. this.length -= 1;
  101. var x = this.__a[this.length];
  102. this.__a[this.length] = null;
  103. return x;
  104. }
  105. public function push(x:T) : Int {
  106. var l = this.length;
  107. this.__grow(l + 1);
  108. this.__a[l] = x;
  109. return l + 1;
  110. }
  111. public function unshift(x : T) : Void {
  112. var l = this.length;
  113. this.__grow(l + 1);
  114. var a = this.__a;
  115. neko.NativeArray.blit(a,1,a,0,l);
  116. a[0] = x;
  117. }
  118. public function remove(x : T) : Bool {
  119. var i = 0;
  120. var l = this.length;
  121. var a = this.__a;
  122. while( i < l ) {
  123. if( a[i] == x ) {
  124. neko.NativeArray.blit(a,i,a,i+1,l - i - 1);
  125. l -= 1;
  126. this.length = l;
  127. a[l] = null;
  128. return true;
  129. }
  130. i += 1;
  131. }
  132. return false;
  133. }
  134. public function reverse() : Void {
  135. var i = 0;
  136. var l = this.length;
  137. var a = this.__a;
  138. var half = l >> 1;
  139. l -= 1;
  140. while( i < half ) {
  141. var tmp = a[i];
  142. a[i] = a[l-i];
  143. a[l-i] = tmp;
  144. i += 1;
  145. }
  146. }
  147. public function shift() : Null<T> {
  148. var l = this.length;
  149. if( l == 0 )
  150. return null;
  151. var a = this.__a;
  152. var x = a[0];
  153. l -= 1;
  154. neko.NativeArray.blit(a,0,a,1,l);
  155. a[l] = null;
  156. this.length = l;
  157. return x;
  158. }
  159. public function slice( pos : Int, ?end : Int ) : Array<T> {
  160. if( pos < 0 ){
  161. pos = this.length + pos;
  162. if( pos < 0 )
  163. pos = 0;
  164. }
  165. if( end == null )
  166. end = this.length;
  167. else if( end < 0 )
  168. end = this.length + end;
  169. if( end > this.length )
  170. end = this.length;
  171. var len = end - pos;
  172. if( len < 0 ) return new Array();
  173. return new1(neko.NativeArray.sub(this.__a,pos,len),len);
  174. }
  175. public function sort(f:T->T->Int) : Void {
  176. var a = this.__a;
  177. var i = 0;
  178. var l = this.length;
  179. while( i < l ) {
  180. var swap = false;
  181. var j = 0;
  182. var max = l - i - 1;
  183. while( j < max ) {
  184. if( f(a[j],a[j+1]) > 0 ) {
  185. var tmp = a[j+1];
  186. a[j+1] = a[j];
  187. a[j] = tmp;
  188. swap = true;
  189. }
  190. j += 1;
  191. }
  192. if( !swap )
  193. break;
  194. i += 1;
  195. }
  196. }
  197. public function splice( pos : Int, len : Int ) : Array<T> {
  198. if( len < 0 ) return new Array();
  199. if( pos < 0 ){
  200. pos = this.length + pos;
  201. if( pos < 0 ) pos = 0;
  202. }
  203. if( pos > this.length ) {
  204. pos = 0;
  205. len = 0;
  206. } else if( pos + len > this.length ) {
  207. len = this.length - pos;
  208. if( len < 0 ) len = 0;
  209. }
  210. var a = this.__a;
  211. var ret = new1(neko.NativeArray.sub(a,pos,len),len);
  212. var end = pos + len;
  213. neko.NativeArray.blit(a,pos,a,end,this.length-end);
  214. this.length -= len;
  215. while( --len >= 0 )
  216. a[this.length + len] = null;
  217. return ret;
  218. }
  219. public function map<S>( f : T -> S ) : Array<S> {
  220. var ret = [];
  221. for (elt in this)
  222. ret.push(f(elt));
  223. return ret;
  224. }
  225. public function filter( f : T -> Bool ) : Array<T> {
  226. var ret = [];
  227. for (elt in this)
  228. if (f(elt))
  229. ret.push(elt);
  230. return ret;
  231. }
  232. /* NEKO INTERNAL */
  233. private function __get( pos : Int ) : T {
  234. return this.__a[pos];
  235. }
  236. private function __set( pos : Int, v : T ) : T {
  237. var a = this.__a;
  238. if( this.length <= pos ) {
  239. var l = pos + 1;
  240. var dlen = l - neko.NativeArray.length(a);
  241. if( dlen > 0 ) {
  242. if( dlen == 1 ) {
  243. this.__grow(l);
  244. a = this.__a;
  245. } else {
  246. a = neko.NativeArray.alloc(l);
  247. neko.NativeArray.blit(a,0,this.__a,0,this.length);
  248. this.__a = a;
  249. }
  250. }
  251. this.length = l;
  252. }
  253. a[pos] = v;
  254. return v;
  255. }
  256. private function __grow(l:Int) : Void {
  257. var a = this.__a;
  258. var sz = neko.NativeArray.length(a);
  259. if( sz >= l ) {
  260. this.length = l;
  261. return;
  262. }
  263. var big = (sz * 3) >> 1;
  264. if( big < l ) big = l;
  265. var a2 = neko.NativeArray.alloc(big);
  266. neko.NativeArray.blit(a2,0,a,0,this.length);
  267. this.__a = a2;
  268. this.length = l;
  269. }
  270. private function __neko() : neko.NativeArray<T> {
  271. var a = this.__a;
  272. var sz = neko.NativeArray.length(a);
  273. if( sz != this.length ) {
  274. a = neko.NativeArray.sub(a,0,this.length);
  275. this.__a = a;
  276. }
  277. return a;
  278. }
  279. #if !(macro || interp)
  280. static function __init__() : Void {
  281. try {
  282. var msort : Dynamic = neko.Lib.load("std","merge_sort",3);
  283. untyped Array.prototype.sort = function(cmp) msort(__this__.__a,__this__.length,cmp);
  284. } catch( e : Dynamic ) {
  285. }
  286. }
  287. #end
  288. }