ArrayBytes.hx 8.3 KB

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