PageRenderTime 25ms CodeModel.GetById 39ms RepoModel.GetById 1ms app.codeStats 0ms

/std/neko/_std/Array.hx

https://bitbucket.org/kodetech/haxe_bin_lfs
Haxe | 350 lines | 298 code | 30 blank | 22 comment | 57 complexity | 2b128fea6dd58f4606dc74e8b6aea1ea MD5 | raw file
Possible License(s): AGPL-3.0, MPL-2.0-no-copyleft-exception
  1. /*
  2. * Copyright (C)2005-2017 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 indexOf(x : T, ?fromIndex:Int) : Int {
  135. var len = length;
  136. var i:Int = (fromIndex != null) ? fromIndex : 0;
  137. var a = __a;
  138. if (i < 0) {
  139. i += len;
  140. if (i < 0) i = 0;
  141. }
  142. while (i < len)
  143. {
  144. if (a[i] == x)
  145. return i;
  146. i++;
  147. }
  148. return -1;
  149. }
  150. public function lastIndexOf(x : T, ?fromIndex:Int) : Int {
  151. var len = length;
  152. var i:Int = (fromIndex != null) ? fromIndex : len - 1;
  153. var a = __a;
  154. if (i >= len)
  155. i = len - 1;
  156. else if (i < 0)
  157. i += len;
  158. while (i >= 0)
  159. {
  160. if (a[i] == x)
  161. return i;
  162. i--;
  163. }
  164. return -1;
  165. }
  166. public function reverse() : Void {
  167. var i = 0;
  168. var l = this.length;
  169. var a = this.__a;
  170. var half = l >> 1;
  171. l -= 1;
  172. while( i < half ) {
  173. var tmp = a[i];
  174. a[i] = a[l-i];
  175. a[l-i] = tmp;
  176. i += 1;
  177. }
  178. }
  179. public function shift() : Null<T> {
  180. var l = this.length;
  181. if( l == 0 )
  182. return null;
  183. var a = this.__a;
  184. var x = a[0];
  185. l -= 1;
  186. neko.NativeArray.blit(a,0,a,1,l);
  187. a[l] = null;
  188. this.length = l;
  189. return x;
  190. }
  191. public function slice( pos : Int, ?end : Int ) : Array<T> {
  192. if( pos < 0 ){
  193. pos = this.length + pos;
  194. if( pos < 0 )
  195. pos = 0;
  196. }
  197. if( end == null )
  198. end = this.length;
  199. else if( end < 0 )
  200. end = this.length + end;
  201. if( end > this.length )
  202. end = this.length;
  203. var len = end - pos;
  204. if( len < 0 ) return new Array();
  205. return new1(neko.NativeArray.sub(this.__a,pos,len),len);
  206. }
  207. public function sort(f:T->T->Int) : Void {
  208. var a = this.__a;
  209. var i = 0;
  210. var l = this.length;
  211. while( i < l ) {
  212. var swap = false;
  213. var j = 0;
  214. var max = l - i - 1;
  215. while( j < max ) {
  216. if( f(a[j],a[j+1]) > 0 ) {
  217. var tmp = a[j+1];
  218. a[j+1] = a[j];
  219. a[j] = tmp;
  220. swap = true;
  221. }
  222. j += 1;
  223. }
  224. if( !swap )
  225. break;
  226. i += 1;
  227. }
  228. }
  229. public function splice( pos : Int, len : Int ) : Array<T> {
  230. if( len < 0 ) return new Array();
  231. if( pos < 0 ){
  232. pos = this.length + pos;
  233. if( pos < 0 ) pos = 0;
  234. }
  235. if( pos > this.length ) {
  236. pos = 0;
  237. len = 0;
  238. } else if( pos + len > this.length ) {
  239. len = this.length - pos;
  240. if( len < 0 ) len = 0;
  241. }
  242. var a = this.__a;
  243. var ret = new1(neko.NativeArray.sub(a,pos,len),len);
  244. var end = pos + len;
  245. neko.NativeArray.blit(a,pos,a,end,this.length-end);
  246. this.length -= len;
  247. while( --len >= 0 )
  248. a[this.length + len] = null;
  249. return ret;
  250. }
  251. public function map<S>( f : T -> S ) : Array<S> {
  252. var ret = [];
  253. for (elt in this)
  254. ret.push(f(elt));
  255. return ret;
  256. }
  257. public function filter( f : T -> Bool ) : Array<T> {
  258. var ret = [];
  259. for (elt in this)
  260. if (f(elt))
  261. ret.push(elt);
  262. return ret;
  263. }
  264. /* NEKO INTERNAL */
  265. private function __get( pos : Int ) : T {
  266. return this.__a[pos];
  267. }
  268. private function __set( pos : Int, v : T ) : T {
  269. var a = this.__a;
  270. if( this.length <= pos ) {
  271. var l = pos + 1;
  272. var dlen = l - neko.NativeArray.length(a);
  273. if( dlen > 0 ) {
  274. if( dlen == 1 ) {
  275. this.__grow(l);
  276. a = this.__a;
  277. } else {
  278. a = neko.NativeArray.alloc(l);
  279. neko.NativeArray.blit(a,0,this.__a,0,this.length);
  280. this.__a = a;
  281. }
  282. }
  283. this.length = l;
  284. }
  285. a[pos] = v;
  286. return v;
  287. }
  288. private function __grow(l:Int) : Void {
  289. var a = this.__a;
  290. var sz = neko.NativeArray.length(a);
  291. if( sz >= l ) {
  292. this.length = l;
  293. return;
  294. }
  295. var big = (sz * 3) >> 1;
  296. if( big < l ) big = l;
  297. var a2 = neko.NativeArray.alloc(big);
  298. neko.NativeArray.blit(a2,0,a,0,this.length);
  299. this.__a = a2;
  300. this.length = l;
  301. }
  302. private function __neko() : neko.NativeArray<T> {
  303. var a = this.__a;
  304. var sz = neko.NativeArray.length(a);
  305. if( sz != this.length ) {
  306. a = neko.NativeArray.sub(a,0,this.length);
  307. this.__a = a;
  308. }
  309. return a;
  310. }
  311. #if !(macro || interp)
  312. static function __init__() : Void {
  313. try {
  314. var msort : Dynamic = neko.Lib.load("std","merge_sort",3);
  315. untyped Array.prototype.sort = function(cmp) msort(__this__.__a,__this__.length,cmp);
  316. } catch( e : Dynamic ) {
  317. }
  318. }
  319. #end
  320. }