ArrayBytes.hx 9.0 KB

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