ArrayBytes.hx 9.1 KB

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