ArrayBytes.hx 8.7 KB

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