Array.hx 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /*
  2. * Copyright (c) 2005, The haXe Project Contributors
  3. * All rights reserved.
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. *
  7. * - Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * - Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY THE HAXE PROJECT CONTRIBUTORS "AS IS" AND ANY
  14. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  15. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  16. * DISCLAIMED. IN NO EVENT SHALL THE HAXE PROJECT CONTRIBUTORS BE LIABLE FOR
  17. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  18. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  19. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  20. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  21. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  22. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  23. * DAMAGE.
  24. */
  25. @:core_api @:final class Array<T> {
  26. private var __a : neko.NativeArray<T>;
  27. public var length(default,null) : Int;
  28. public function new() : Void {
  29. this.__a = neko.NativeArray.alloc(0);
  30. this.length = 0;
  31. }
  32. private static function new1<T>(a:neko.NativeArray<T>,l:Int) : Array<T> {
  33. var inst = new Array<T>();
  34. inst.__a = a;
  35. inst.length = l;
  36. return inst;
  37. }
  38. public function concat( a : Array<T>) : Array<T> {
  39. var a1 = this.__a;
  40. var a2 = a.__a;
  41. var s1 = this.length;
  42. var s2 = a.length;
  43. var a = neko.NativeArray.alloc(s1+s2);
  44. neko.NativeArray.blit(a,0,a1,0,s1);
  45. neko.NativeArray.blit(a,s1,a2,0,s2);
  46. return new1(a,s1+s2);
  47. }
  48. public function copy() : Array<T> {
  49. return new1(neko.NativeArray.sub(this.__a,0,this.length),this.length);
  50. }
  51. public function iterator() : Iterator<Null<T>> {
  52. return untyped {
  53. a : this,
  54. p : 0,
  55. hasNext : function() {
  56. return __this__.p < __this__.a.length;
  57. },
  58. next : function() {
  59. var i = __this__.a.__a[__this__.p];
  60. __this__.p += 1;
  61. return i;
  62. }
  63. };
  64. }
  65. public function insert( pos : Int, x : T ) : Void {
  66. var l = this.length;
  67. if( pos < 0 ) {
  68. pos = l + pos;
  69. if( pos < 0 ) pos = 0;
  70. }
  71. if( pos > l ) pos = l;
  72. this.__double(l+1);
  73. var a = this.__a;
  74. neko.NativeArray.blit(a,pos+1,a,pos,l-pos);
  75. a[pos] = x;
  76. }
  77. public function join( sep : String ) : String {
  78. var s = new StringBuf();
  79. var a = this.__a;
  80. var max = this.length - 1;
  81. for( p in 0...this.length ) {
  82. s.add(a[p]);
  83. if( p != max )
  84. s.add(sep);
  85. }
  86. return s.toString();
  87. }
  88. public function toString() : String {
  89. var s = new StringBuf();
  90. s.add("[");
  91. var it = iterator();
  92. for( i in it ) {
  93. s.add(i);
  94. if( it.hasNext() )
  95. s.add(", ");
  96. }
  97. s.add("]");
  98. return s.toString();
  99. }
  100. public function pop() : Null<T> {
  101. if( this.length == 0 )
  102. return null;
  103. this.length -= 1;
  104. var x = this.__a[this.length];
  105. this.__a[this.length] = null;
  106. return x;
  107. }
  108. public function push(x:T) : Int {
  109. var l = this.length;
  110. this.__double(l + 1);
  111. this.__a[l] = x;
  112. return l + 1;
  113. }
  114. public function unshift(x : T) : Void {
  115. var l = this.length;
  116. this.__double(l + 1);
  117. var a = this.__a;
  118. neko.NativeArray.blit(a,1,a,0,l);
  119. a[0] = x;
  120. }
  121. public function remove(x : T) : Bool {
  122. var i = 0;
  123. var l = this.length;
  124. var a = this.__a;
  125. while( i < l ) {
  126. if( a[i] == x ) {
  127. neko.NativeArray.blit(a,i,a,i+1,l - i - 1);
  128. l -= 1;
  129. this.length = l;
  130. a[l] = null;
  131. return true;
  132. }
  133. i += 1;
  134. }
  135. return false;
  136. }
  137. public function reverse() : Void {
  138. var i = 0;
  139. var l = this.length;
  140. var a = this.__a;
  141. var half = l >> 1;
  142. l -= 1;
  143. while( i < half ) {
  144. var tmp = a[i];
  145. a[i] = a[l-i];
  146. a[l-i] = tmp;
  147. i += 1;
  148. }
  149. }
  150. public function shift() : Null<T> {
  151. var l = this.length;
  152. if( l == 0 )
  153. return null;
  154. var a = this.__a;
  155. var x = a[0];
  156. l -= 1;
  157. neko.NativeArray.blit(a,0,a,1,l);
  158. a[l] = null;
  159. this.length = l;
  160. return x;
  161. }
  162. public function slice( pos : Int, ?end : Int ) : Array<T> {
  163. if( pos < 0 ){
  164. pos = this.length + pos;
  165. if( pos < 0 )
  166. pos = 0;
  167. }
  168. if( end == null )
  169. end = this.length;
  170. else if( end < 0 )
  171. end = this.length + end;
  172. if( end > this.length )
  173. end = this.length;
  174. var len = end - pos;
  175. if( len < 0 ) return new Array();
  176. return new1(neko.NativeArray.sub(this.__a,pos,len),len);
  177. }
  178. public function sort(f:T->T->Int) : Void {
  179. var a = this.__a;
  180. var i = 0;
  181. var l = this.length;
  182. while( i < l ) {
  183. var swap = false;
  184. var j = 0;
  185. var max = l - i - 1;
  186. while( j < max ) {
  187. if( f(a[j],a[j+1]) > 0 ) {
  188. var tmp = a[j+1];
  189. a[j+1] = a[j];
  190. a[j] = tmp;
  191. swap = true;
  192. }
  193. j += 1;
  194. }
  195. if( !swap )
  196. break;
  197. i += 1;
  198. }
  199. }
  200. public function splice( pos : Int, len : Int ) : Array<T> {
  201. if( len < 0 ) return new Array();
  202. if( pos < 0 ){
  203. pos = this.length + pos;
  204. if( pos < 0 ) pos = 0;
  205. }
  206. if( pos > this.length ) {
  207. pos = 0;
  208. len = 0;
  209. } else if( pos + len > this.length ) {
  210. len = this.length - pos;
  211. if( len < 0 ) len = 0;
  212. }
  213. var a = this.__a;
  214. var ret = new1(neko.NativeArray.sub(a,pos,len),len);
  215. var end = pos + len;
  216. neko.NativeArray.blit(a,pos,a,end,this.length-end);
  217. this.length -= len;
  218. while( --len >= 0 )
  219. a[this.length + len] = null;
  220. return ret;
  221. }
  222. /* NEKO INTERNAL */
  223. private function __get( pos : Int ) : T {
  224. return this.__a[pos];
  225. }
  226. private function __set( pos : Int, v : T ) : T {
  227. var a = this.__a;
  228. if( this.length <= pos ) {
  229. var l = pos + 1;
  230. if( neko.NativeArray.length(a) < l ) {
  231. a = neko.NativeArray.alloc(l);
  232. neko.NativeArray.blit(a,0,this.__a,0,this.length);
  233. this.__a = a;
  234. }
  235. this.length = l;
  236. }
  237. a[pos] = v;
  238. return v;
  239. }
  240. private function __double(l:Int) : Void {
  241. var a = this.__a;
  242. var sz = neko.NativeArray.length(a);
  243. if( sz >= l ) {
  244. this.length = l;
  245. return;
  246. }
  247. var big = sz * 2;
  248. if( big < l ) big = l;
  249. var a2 = neko.NativeArray.alloc(big);
  250. neko.NativeArray.blit(a2,0,a,0,this.length);
  251. this.__a = a2;
  252. this.length = l;
  253. }
  254. private function __neko() : neko.NativeArray<T> {
  255. var a = this.__a;
  256. var sz = neko.NativeArray.length(a);
  257. if( sz != this.length ) {
  258. a = neko.NativeArray.sub(a,0,this.length);
  259. this.__a = a;
  260. }
  261. return a;
  262. }
  263. static function __init__() : Void {
  264. try {
  265. var msort : Dynamic = neko.Lib.load("std","merge_sort",3);
  266. untyped Array.prototype.sort = function(cmp) msort(__this__.__a,__this__.length,cmp);
  267. } catch( e : Dynamic ) {
  268. }
  269. }
  270. }