ArrayBytes.hx 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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. if (Type.get((cast null : T)) == Type.get(0))
  132. (bytes : Bytes).sortI32(0, length, cast f);
  133. else
  134. (bytes : Bytes).sortF64(0, length, cast f);
  135. }
  136. override function splice(pos:Int, len:Int):ArrayBytes<T> {
  137. if (len < 0)
  138. return new ArrayBytes<T>();
  139. if (pos < 0) {
  140. pos = this.length + pos;
  141. if (pos < 0)
  142. pos = 0;
  143. }
  144. if (pos > this.length) {
  145. pos = 0;
  146. len = 0;
  147. } else if (pos + len > this.length) {
  148. len = this.length - pos;
  149. if (len < 0)
  150. len = 0;
  151. }
  152. if (len == 0)
  153. return new ArrayBytes<T>();
  154. var ret = new ArrayBytes<T>();
  155. ret.bytes = (bytes : Bytes).sub(pos << bytes.sizeBits, len << bytes.sizeBits);
  156. ret.size = ret.length = len;
  157. var end = pos + len;
  158. (bytes : Bytes).blit(pos << bytes.sizeBits, bytes, end << bytes.sizeBits, (length - end) << bytes.sizeBits);
  159. (bytes : Bytes).fill((length - len) << bytes.sizeBits, (len) << bytes.sizeBits, 0);
  160. length -= len;
  161. return ret;
  162. }
  163. override function toString():String {
  164. var b = new StringBuf();
  165. b.addChar("[".code);
  166. for (i in 0...length) {
  167. if (i > 0)
  168. b.addChar(",".code);
  169. b.add(bytes[i]);
  170. }
  171. b.addChar("]".code);
  172. return b.toString();
  173. }
  174. public function unshift(x:T):Void {
  175. if (length == size)
  176. __expand(length)
  177. else
  178. length++;
  179. (bytes : Bytes).blit(1 << bytes.sizeBits, bytes, 0, (length - 1) << bytes.sizeBits);
  180. bytes[0] = x;
  181. }
  182. public function insert(pos:Int, x:T):Void {
  183. if (pos < 0) {
  184. pos = length + pos;
  185. if (pos < 0)
  186. pos = 0;
  187. } else if (pos > length)
  188. pos = length;
  189. if (length == size)
  190. __expand(length)
  191. else
  192. length++;
  193. (bytes : Bytes).blit((pos + 1) << bytes.sizeBits, bytes, pos << bytes.sizeBits, (length - pos - 1) << bytes.sizeBits);
  194. bytes[pos] = x;
  195. }
  196. public function contains(x:T):Bool {
  197. return indexOf(x) != -1;
  198. }
  199. public function remove(x:T):Bool {
  200. var idx = indexOf(x);
  201. if (idx < 0)
  202. return false;
  203. length--;
  204. (bytes : hl.Bytes).blit(idx << bytes.sizeBits, bytes, (idx + 1) << bytes.sizeBits, (length - idx) << bytes.sizeBits);
  205. return true;
  206. }
  207. public function indexOf(x:T, ?fromIndex:Int):Int {
  208. var idx:Int = fromIndex == null ? 0 : fromIndex;
  209. if (idx < 0) {
  210. idx += length;
  211. if (idx < 0)
  212. idx = 0;
  213. }
  214. for (i in idx...length)
  215. if (bytes[i] == x)
  216. return i;
  217. return -1;
  218. }
  219. public function lastIndexOf(x:T, ?fromIndex:Int):Int {
  220. var len = length;
  221. var i:Int = fromIndex != null ? fromIndex : len - 1;
  222. if (i >= len)
  223. i = len - 1;
  224. else if (i < 0)
  225. i += len;
  226. while (i >= 0) {
  227. if (bytes[i] == x)
  228. return i;
  229. i--;
  230. }
  231. return -1;
  232. }
  233. public function copy():ArrayBytes<T> {
  234. var a = new ArrayBytes<T>();
  235. a.length = a.size = length;
  236. a.bytes = new Bytes(length << bytes.sizeBits);
  237. (a.bytes : Bytes).blit(0, bytes, 0, length << bytes.sizeBits);
  238. return a;
  239. }
  240. public function iterator():ArrayIterator<T> {
  241. return new BytesIterator(this);
  242. }
  243. public function keyValueIterator() : ArrayKeyValueIterator<T> {
  244. return new ArrayKeyValueIterator<T>(cast this);
  245. }
  246. public function map<S>(f:T->S):ArrayDyn@:privateAccess {
  247. var a = new ArrayObj();
  248. if (length > 0)
  249. a.__expand(length - 1);
  250. for (i in 0...length)
  251. a.array[i] = f(bytes[i]);
  252. return ArrayDyn.alloc(a, true);
  253. }
  254. public function filter(f:T->Bool):ArrayBytes<T> {
  255. var a = new ArrayBytes<T>();
  256. for (i in 0...length) {
  257. var v = bytes[i];
  258. if (f(v))
  259. a.push(v);
  260. }
  261. return a;
  262. }
  263. override public function resize(len:Int):Void {
  264. if (length < len) {
  265. __expand(len - 1);
  266. } else if (length > len) {
  267. (bytes : Bytes).fill(len << bytes.sizeBits, (length - len) << bytes.sizeBits, 0);
  268. this.length = len;
  269. }
  270. }
  271. override function getDyn(pos:Int):Dynamic {
  272. var pos:UInt = pos;
  273. if (pos >= (length : UInt))
  274. return bytes.nullValue;
  275. return bytes[pos];
  276. }
  277. override function setDyn(pos:Int, v:Dynamic) {
  278. var pos:UInt = pos;
  279. if (pos >= (length : UInt))
  280. __expand(pos);
  281. bytes[pos] = v;
  282. }
  283. override function pushDyn(v:Dynamic)
  284. return push(v);
  285. override function popDyn():Null<Dynamic>
  286. return pop();
  287. override function shiftDyn():Null<Dynamic>
  288. return shift();
  289. override function unshiftDyn(v:Dynamic)
  290. unshift(v);
  291. override function insertDyn(pos:Int, v:Dynamic)
  292. insert(pos, v);
  293. override function containsDyn(v:Dynamic)
  294. return contains(v);
  295. override function removeDyn(v:Dynamic)
  296. return remove(v);
  297. override function sortDyn(f:Dynamic->Dynamic->Int)
  298. sort(f);
  299. // called by compiler when accessing the array outside of its bounds, might trigger resize
  300. function __expand(index:Int) {
  301. if (index < 0)
  302. throw "Invalid array index " + index;
  303. var newlen = index + 1;
  304. if (newlen > size) {
  305. var next = (size * 3) >> 1;
  306. if (next < newlen)
  307. next = newlen;
  308. var bytes2 = new hl.Bytes(next << bytes.sizeBits);
  309. var bsize = length << bytes.sizeBits;
  310. bytes2.blit(0, bytes, 0, bsize);
  311. bytes2.fill(bsize, (next << bytes.sizeBits) - bsize, 0);
  312. bytes = bytes2;
  313. size = next;
  314. }
  315. length = newlen;
  316. }
  317. }
  318. typedef ArrayI32 = ArrayBytes<Int>;
  319. typedef ArrayUI16 = ArrayBytes<UI16>;
  320. typedef ArrayF32 = ArrayBytes<F32>;
  321. typedef ArrayF64 = ArrayBytes<Float>;
  322. #if !hl_legacy32
  323. typedef ArrayI64 = ArrayBytes<I64>;
  324. #end