ArrayBytes.hx 8.5 KB

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