NekoArray__.hx 6.5 KB

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