ArrayBase.hx 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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 allocI16( bytes : BytesAccess<I16>, length : Int ) @:privateAccess {
  93. var a : ArrayI16 = untyped $new(ArrayI16);
  94. a.length = length;
  95. a.bytes = bytes;
  96. a.size = length;
  97. return a;
  98. }
  99. public static function allocF32( bytes : BytesAccess<F32>, length : Int ) @:privateAccess {
  100. var a : ArrayF32 = untyped $new(ArrayF32);
  101. a.length = length;
  102. a.bytes = bytes;
  103. a.size = length;
  104. return a;
  105. }
  106. public static function allocF64( bytes : BytesAccess<Float>, length : Int ) @:privateAccess {
  107. var a : ArrayF64 = untyped $new(ArrayF64);
  108. a.length = length;
  109. a.bytes = bytes;
  110. a.size = length;
  111. return a;
  112. }
  113. }
  114. @:keep
  115. @:generic
  116. class BasicIterator<T> {
  117. var pos : Int;
  118. var a : ArrayBasic<T>;
  119. public function new(a) {
  120. this.a = a;
  121. }
  122. public function hasNext() {
  123. return pos < a.length;
  124. }
  125. public function next() : T {
  126. return @:privateAccess a.bytes.get(pos++);
  127. }
  128. }
  129. @:keep
  130. @:generic class ArrayBasic<T> extends ArrayBase {
  131. var bytes : hl.types.BytesAccess<T>;
  132. var size : Int;
  133. public function new() {
  134. size = length = 0;
  135. bytes = new Bytes(0);
  136. }
  137. public function concat( a : ArrayBasic<T> ) : ArrayBasic<T> {
  138. var ac = new ArrayBasic<T>();
  139. ac.length = ac.size = length + a.length;
  140. ac.bytes = new Bytes(ac.length << bytes.sizeBits);
  141. var offset = length << bytes.sizeBits;
  142. (ac.bytes:Bytes).blit(0, this.bytes, 0, offset);
  143. (ac.bytes:Bytes).blit(offset, a.bytes, 0, a.length << bytes.sizeBits);
  144. return ac;
  145. }
  146. override function join( sep : String ) : String {
  147. var s = new StringBuf();
  148. for( i in 0...length ) {
  149. if( i > 0 ) s.add(sep);
  150. s.add(bytes[i]);
  151. }
  152. return s.toString();
  153. }
  154. public function pop() : Null<T> {
  155. if( length == 0 )
  156. return null;
  157. length--;
  158. return bytes[length];
  159. }
  160. public function push(x : T) : Int {
  161. var len = length;
  162. if( size == len )
  163. __expand(len);
  164. else
  165. length++;
  166. bytes[len] = x;
  167. return length;
  168. }
  169. override function reverse() : Void {
  170. for( i in 0...length >> 1 ) {
  171. var k = length - 1 - i;
  172. var tmp = bytes[i];
  173. bytes[i] = bytes[k];
  174. bytes[k] = tmp;
  175. }
  176. }
  177. public function shift() : Null<T> {
  178. if( length == 0 )
  179. return null;
  180. var v = bytes[0];
  181. length--;
  182. (bytes:Bytes).blit(0, bytes, 1 << bytes.sizeBits, length << bytes.sizeBits);
  183. return v;
  184. }
  185. override function slice( pos : Int, ?end : Int ) : ArrayBasic<T> {
  186. if( pos < 0 ) {
  187. pos = this.length + pos;
  188. if( pos < 0 )
  189. pos = 0;
  190. }
  191. var pend : Int;
  192. if( end == null )
  193. pend = this.length;
  194. else {
  195. pend = end;
  196. if( pend < 0 )
  197. pend += this.length;
  198. if( pend > this.length )
  199. pend = this.length;
  200. }
  201. var len = pend - pos;
  202. if( len < 0 )
  203. return new ArrayBasic<T>();
  204. var a = new ArrayBasic<T>();
  205. a.length = a.size = len;
  206. a.bytes = (bytes:Bytes).sub(pos << bytes.sizeBits, len << bytes.sizeBits);
  207. return a;
  208. }
  209. public function sort( f : T -> T -> Int ) : Void {
  210. if( Type.get((cast null : T)) == Type.get(0) )
  211. (bytes:Bytes).sortI32(0, length, cast f);
  212. else
  213. (bytes:Bytes).sortF64(0, length, cast f);
  214. }
  215. override function splice( pos : Int, len : Int ) : ArrayBasic<T> {
  216. throw "TODO";
  217. return null;
  218. }
  219. override function toString() : String {
  220. var b = new StringBuf();
  221. b.addChar("[".code);
  222. for( i in 0...length ) {
  223. if( i > 0 ) b.addChar(",".code);
  224. b.add(bytes[i]);
  225. }
  226. b.addChar("]".code);
  227. return b.toString();
  228. }
  229. public function unshift( x : T ) : Void {
  230. if( length == size ) __expand(length) else length++;
  231. (bytes:Bytes).blit(1<<bytes.sizeBits, bytes, 0, (length - 1) << bytes.sizeBits);
  232. bytes[0] = x;
  233. }
  234. public function insert( pos : Int, x : T ) : Void {
  235. if( pos < 0 ) {
  236. pos = length + pos;
  237. if( pos < 0 ) pos = 0;
  238. } else if( pos > length ) pos = length;
  239. if( length == size ) __expand(length) else length++;
  240. (bytes:Bytes).blit((pos + 1)<<bytes.sizeBits, bytes, pos<<bytes.sizeBits, (length - pos - 1) << bytes.sizeBits);
  241. bytes[pos] = x;
  242. }
  243. public function remove( x : T ) : Bool {
  244. var idx = indexOf(x);
  245. if( idx < 0 )
  246. return false;
  247. length--;
  248. (bytes : hl.types.Bytes).blit(idx<<bytes.sizeBits,bytes,(idx + 1)<<bytes.sizeBits,(length - idx)<<bytes.sizeBits);
  249. return true;
  250. }
  251. public function indexOf( x : T, ?fromIndex:Int ) : Int {
  252. var idx : Int = fromIndex == null ? 0 : fromIndex;
  253. if( idx < 0 ) {
  254. idx += length;
  255. if( idx < 0 ) idx = 0;
  256. }
  257. for( i in idx...length )
  258. if( bytes[i] == x )
  259. return i;
  260. return -1;
  261. }
  262. public function lastIndexOf( x : T, ?fromIndex:Int ) : Int {
  263. var len = length;
  264. var i:Int = fromIndex != null ? fromIndex : len - 1;
  265. if( i >= len )
  266. i = len - 1;
  267. else if( i < 0 )
  268. i += len;
  269. while( i >= 0 ) {
  270. if( bytes[i] == x )
  271. return i;
  272. i--;
  273. }
  274. return -1;
  275. }
  276. public function copy() : ArrayBasic<T> {
  277. var a = new ArrayBasic<T>();
  278. a.length = a.size = length;
  279. a.bytes = new Bytes(length << bytes.sizeBits);
  280. (a.bytes:Bytes).blit(0, bytes, 0, length << bytes.sizeBits);
  281. return a;
  282. }
  283. public function iterator() : Iterator<T> {
  284. return new BasicIterator(this);
  285. }
  286. public function map<S>( f : T -> S ) : ArrayDyn @:privateAccess {
  287. var a = new ArrayObj();
  288. if( length > 0 ) a.__expand(length - 1);
  289. for( i in 0...length )
  290. a.array[i] = f(bytes[i]);
  291. return ArrayDyn.alloc(a,true);
  292. }
  293. public function filter( f : T -> Bool ) : ArrayBasic<T> {
  294. var a = new ArrayBasic<T>();
  295. for( i in 0...length ) {
  296. var v = bytes[i];
  297. if( f(v) ) a.push(v);
  298. }
  299. return a;
  300. }
  301. override function getDyn( pos : Int ) : Dynamic {
  302. var pos : UInt = pos;
  303. if( pos >= length )
  304. return bytes.nullValue;
  305. return bytes[pos];
  306. }
  307. override function setDyn( pos : Int, v : Dynamic ) {
  308. var pos : UInt = pos;
  309. if( pos >= length )
  310. __expand(pos);
  311. bytes[pos] = v;
  312. }
  313. override function pushDyn( v : Dynamic ) return push(v);
  314. override function popDyn() : Null<Dynamic> return pop();
  315. override function shiftDyn() : Null<Dynamic> return shift();
  316. override function unshiftDyn( v : Dynamic ) unshift(v);
  317. override function insertDyn( pos : Int, v : Dynamic ) insert(pos, v);
  318. override function removeDyn( v : Dynamic ) return remove(v);
  319. override function sortDyn( f : Dynamic -> Dynamic -> Int ) sort(f);
  320. // called by compiler when accessing the array outside of its bounds, might trigger resize
  321. function __expand( index : Int ) {
  322. if( index < 0 ) throw "Invalid array access";
  323. var newlen = index + 1;
  324. if( newlen > size ) {
  325. var next = (size * 3) >> 1;
  326. if( next < newlen ) next = newlen;
  327. var bytes2 = new hl.types.Bytes(next << bytes.sizeBits);
  328. var bsize = length << bytes.sizeBits;
  329. bytes2.blit(0, bytes, 0, bsize);
  330. bytes2.fill(bsize, (next << bytes.sizeBits) - bsize, 0);
  331. bytes = bytes2;
  332. size = next;
  333. }
  334. length = newlen;
  335. }
  336. }
  337. typedef ArrayI32 = ArrayBasic<Int>;
  338. typedef ArrayI16 = ArrayBasic<I16>;
  339. typedef ArrayF32 = ArrayBasic<F32>;
  340. typedef ArrayF64 = ArrayBasic<Float>;