ArrayBase.hx 10 KB

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