ArrayBytes.hx 8.1 KB

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