ArrayBase.hx 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. /*
  2. * Copyright (C)2005-2016 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. package hl.types;
  23. @:keep
  24. class ArrayAccess {
  25. public function getDyn( pos : Int ) : Dynamic {
  26. throw "Not implemented";
  27. return 0;
  28. }
  29. public function setDyn( pos : Int, v : Dynamic ) {
  30. throw "Not implemented";
  31. }
  32. }
  33. @:keep
  34. class ArrayBase extends ArrayAccess {
  35. public var length(default,null) : Int;
  36. public function pushDyn( v : Dynamic ) : Int {
  37. throw "Not implemented";
  38. return 0;
  39. }
  40. public function popDyn() : Null<Dynamic> {
  41. throw "Not implemented";
  42. return null;
  43. }
  44. public function shiftDyn() : Null<Dynamic> {
  45. throw "Not implemented";
  46. return null;
  47. }
  48. public function unshiftDyn( v : Dynamic ) : Void {
  49. throw "Not implemented";
  50. }
  51. public function insertDyn( pos : Int, v : Dynamic ) : Void {
  52. throw "Not implemented";
  53. }
  54. public function removeDyn( v : Dynamic ) : Bool {
  55. throw "Not implemented";
  56. return false;
  57. }
  58. public function sortDyn( f : Dynamic -> Dynamic -> Int ) : Void {
  59. throw "Not implemented";
  60. }
  61. public function slice( pos : Int, ?end : Int ) : ArrayBase{
  62. throw "Not implemented";
  63. return null;
  64. }
  65. public function splice( pos : Int, len : Int ) : ArrayBase{
  66. throw "Not implemented";
  67. return null;
  68. }
  69. public function join( sep : String ) : String {
  70. throw "Not implemented";
  71. return null;
  72. }
  73. public function reverse() {
  74. throw "Not implemented";
  75. }
  76. public function toString() : String {
  77. throw "Not implemented";
  78. return null;
  79. }
  80. function __cast( t : Type ) : Dynamic {
  81. if( t == Type.get(new ArrayDyn()) )
  82. return ArrayDyn.alloc(this, false);
  83. return null;
  84. }
  85. public static function allocI32( bytes : BytesAccess<Int>, length : Int ) @:privateAccess {
  86. var a : ArrayI32 = untyped $new(ArrayI32);
  87. a.length = length;
  88. a.bytes = bytes;
  89. a.size = length;
  90. return a;
  91. }
  92. public static function allocF64( bytes : BytesAccess<Float>, length : Int ) @:privateAccess {
  93. var a : ArrayF64 = untyped $new(ArrayF64);
  94. a.length = length;
  95. a.bytes = bytes;
  96. a.size = length;
  97. return a;
  98. }
  99. }
  100. @:keep
  101. @:generic
  102. class BasicIterator<T> {
  103. var pos : Int;
  104. var a : ArrayBasic<T>;
  105. public function new(a) {
  106. this.a = a;
  107. }
  108. public function hasNext() {
  109. return pos < a.length;
  110. }
  111. public function next() : T {
  112. return @:privateAccess a.bytes.get(pos++);
  113. }
  114. }
  115. @:keep
  116. @:generic class ArrayBasic<T> extends ArrayBase {
  117. var bytes : hl.types.BytesAccess<T>;
  118. var size : Int;
  119. public function new() {
  120. size = length = 0;
  121. bytes = new Bytes(0);
  122. }
  123. public function concat( a : ArrayBasic<T> ) : ArrayBasic<T> {
  124. var ac = new ArrayBasic<T>();
  125. ac.length = ac.size = length + a.length;
  126. ac.bytes = new Bytes(ac.length << bytes.sizeBits);
  127. var offset = length << bytes.sizeBits;
  128. (ac.bytes:Bytes).blit(0, this.bytes, 0, offset);
  129. (ac.bytes:Bytes).blit(offset, a.bytes, 0, a.length << bytes.sizeBits);
  130. return ac;
  131. }
  132. override function join( sep : String ) : String {
  133. var s = new StringBuf();
  134. for( i in 0...length ) {
  135. if( i > 0 ) s.add(sep);
  136. s.add(bytes[i]);
  137. }
  138. return s.toString();
  139. }
  140. public function pop() : Null<T> {
  141. if( length == 0 )
  142. return null;
  143. length--;
  144. return bytes[length];
  145. }
  146. public function push(x : T) : Int {
  147. var len = length;
  148. if( size == len )
  149. __expand(len);
  150. else
  151. length++;
  152. bytes[len] = x;
  153. return length;
  154. }
  155. override function reverse() : Void {
  156. for( i in 0...length >> 1 ) {
  157. var k = length - 1 - i;
  158. var tmp = bytes[i];
  159. bytes[i] = bytes[k];
  160. bytes[k] = tmp;
  161. }
  162. }
  163. public function shift() : Null<T> {
  164. if( length == 0 )
  165. return null;
  166. var v = bytes[0];
  167. length--;
  168. (bytes:Bytes).blit(0, bytes, 1 << bytes.sizeBits, length << bytes.sizeBits);
  169. return v;
  170. }
  171. override function slice( pos : Int, ?end : Int ) : ArrayBasic<T> {
  172. if( pos < 0 ) {
  173. pos = this.length + pos;
  174. if( pos < 0 )
  175. pos = 0;
  176. }
  177. var pend : Int;
  178. if( end == null )
  179. pend = this.length;
  180. else {
  181. pend = end;
  182. if( pend < 0 )
  183. pend += this.length;
  184. if( pend > this.length )
  185. pend = this.length;
  186. }
  187. var len = pend - pos;
  188. if( len < 0 )
  189. return new ArrayBasic<T>();
  190. var a = new ArrayBasic<T>();
  191. a.length = a.size = len;
  192. a.bytes = (bytes:Bytes).sub(pos << bytes.sizeBits, len << bytes.sizeBits);
  193. return a;
  194. }
  195. public function sort( f : T -> T -> Int ) : Void {
  196. if( Type.get((cast null : T)) == Type.get(0) )
  197. (bytes:Bytes).sortI32(0, length, cast f);
  198. else
  199. (bytes:Bytes).sortF64(0, length, cast f);
  200. }
  201. override function splice( pos : Int, len : Int ) : ArrayBasic<T> {
  202. throw "TODO";
  203. return null;
  204. }
  205. override function toString() : String {
  206. var b = new StringBuf();
  207. b.addChar("[".code);
  208. for( i in 0...length ) {
  209. if( i > 0 ) b.addChar(",".code);
  210. b.add(bytes[i]);
  211. }
  212. b.addChar("]".code);
  213. return b.toString();
  214. }
  215. public function unshift( x : T ) : Void {
  216. if( length == size ) __expand(length) else length++;
  217. (bytes:Bytes).blit(1<<bytes.sizeBits, bytes, 0, (length - 1) << bytes.sizeBits);
  218. bytes[0] = x;
  219. }
  220. public function insert( pos : Int, x : T ) : Void {
  221. if( pos < 0 ) {
  222. pos = length + pos;
  223. if( pos < 0 ) pos = 0;
  224. } else if( pos > length ) pos = length;
  225. if( length == size ) __expand(length) else length++;
  226. (bytes:Bytes).blit((pos + 1)<<bytes.sizeBits, bytes, pos<<bytes.sizeBits, (length - pos - 1) << bytes.sizeBits);
  227. bytes[pos] = x;
  228. }
  229. public function remove( x : T ) : Bool {
  230. var idx = indexOf(x);
  231. if( idx < 0 )
  232. return false;
  233. length--;
  234. (bytes : hl.types.Bytes).blit(idx<<bytes.sizeBits,bytes,(idx + 1)<<bytes.sizeBits,(length - idx)<<bytes.sizeBits);
  235. return true;
  236. }
  237. public function indexOf( x : T, ?fromIndex:Int ) : Int {
  238. var idx : Int = fromIndex == null ? 0 : fromIndex;
  239. if( idx < 0 ) {
  240. idx += length;
  241. if( idx < 0 ) idx = 0;
  242. }
  243. for( i in idx...length )
  244. if( bytes[i] == x )
  245. return i;
  246. return -1;
  247. }
  248. public function lastIndexOf( x : T, ?fromIndex:Int ) : Int {
  249. var len = length;
  250. var i:Int = fromIndex != null ? fromIndex : len - 1;
  251. if( i >= len )
  252. i = len - 1;
  253. else if( i < 0 )
  254. i += len;
  255. while( i >= 0 ) {
  256. if( bytes[i] == x )
  257. return i;
  258. i--;
  259. }
  260. return -1;
  261. }
  262. public function copy() : ArrayBasic<T> {
  263. var a = new ArrayBasic<T>();
  264. a.length = a.size = length;
  265. a.bytes = new Bytes(length << bytes.sizeBits);
  266. (a.bytes:Bytes).blit(0, bytes, 0, length << bytes.sizeBits);
  267. return a;
  268. }
  269. public function iterator() : Iterator<T> {
  270. return new BasicIterator(this);
  271. }
  272. public function map<S>( f : T -> S ) : ArrayDyn @:privateAccess {
  273. var a = new ArrayObj();
  274. if( length > 0 ) a.__expand(length - 1);
  275. for( i in 0...length )
  276. a.array[i] = f(bytes[i]);
  277. return ArrayDyn.alloc(a,true);
  278. }
  279. public function filter( f : T -> Bool ) : ArrayBasic<T> {
  280. var a = new ArrayBasic<T>();
  281. for( i in 0...length ) {
  282. var v = bytes[i];
  283. if( f(v) ) a.push(v);
  284. }
  285. return a;
  286. }
  287. override function getDyn( pos : Int ) : Dynamic {
  288. var pos : UInt = pos;
  289. if( pos >= length )
  290. return bytes.nullValue;
  291. return bytes[pos];
  292. }
  293. override function setDyn( pos : Int, v : Dynamic ) {
  294. var pos : UInt = pos;
  295. if( pos >= length )
  296. __expand(pos);
  297. bytes[pos] = v;
  298. }
  299. override function pushDyn( v : Dynamic ) return push(v);
  300. override function popDyn() : Null<Dynamic> return pop();
  301. override function shiftDyn() : Null<Dynamic> return shift();
  302. override function unshiftDyn( v : Dynamic ) unshift(v);
  303. override function insertDyn( pos : Int, v : Dynamic ) insert(pos, v);
  304. override function removeDyn( v : Dynamic ) return remove(v);
  305. override function sortDyn( f : Dynamic -> Dynamic -> Int ) sort(f);
  306. // called by compiler when accessing the array outside of its bounds, might trigger resize
  307. function __expand( index : Int ) {
  308. if( index < 0 ) throw "Invalid array access";
  309. var newlen = index + 1;
  310. if( newlen > size ) {
  311. var next = (size * 3) >> 1;
  312. if( next < newlen ) next = newlen;
  313. var bytes2 = new hl.types.Bytes(next << bytes.sizeBits);
  314. var bsize = length << bytes.sizeBits;
  315. bytes2.blit(0, bytes, 0, bsize);
  316. bytes2.fill(bsize, (next << bytes.sizeBits) - bsize, 0);
  317. bytes = bytes2;
  318. size = next;
  319. }
  320. length = newlen;
  321. }
  322. }
  323. typedef ArrayI32 = ArrayBasic<Int>;
  324. typedef ArrayI16 = ArrayBasic<I16>;
  325. typedef ArrayF32 = ArrayBasic<F32>;
  326. typedef ArrayF64 = ArrayBasic<Float>;