UInt16Array.hx 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 haxe.io;
  23. typedef UInt16ArrayData = js.lib.Uint16Array;
  24. @:coreApi
  25. abstract UInt16Array(UInt16ArrayData) {
  26. public static inline var BYTES_PER_ELEMENT = 2;
  27. public var length(get, never):Int;
  28. public var view(get, never):ArrayBufferView;
  29. public inline function new(elements:Int) {
  30. this = new UInt16ArrayData(elements);
  31. }
  32. inline function get_length():Int {
  33. return this.length;
  34. }
  35. public inline function get_view():ArrayBufferView {
  36. return ArrayBufferView.fromData(this);
  37. }
  38. @:arrayAccess public inline function get(index:Int):Int {
  39. return this[index];
  40. }
  41. @:arrayAccess public inline function set(index:Int, value:Int):Int {
  42. return this[index] = value;
  43. }
  44. public inline function sub(begin:Int, ?length:Int):UInt16Array {
  45. return fromData(this.subarray(begin, length == null ? this.length : begin + length));
  46. }
  47. public inline function subarray(?begin:Int, ?end:Int):UInt16Array {
  48. return fromData(this.subarray(begin, end));
  49. }
  50. public inline function getData():UInt16ArrayData {
  51. return this;
  52. }
  53. public static inline function fromData(d:UInt16ArrayData):UInt16Array {
  54. return cast d;
  55. }
  56. public static function fromArray(a:Array<Int>, pos:Int = 0, ?length:Int):UInt16Array {
  57. if (length == null)
  58. length = a.length - pos;
  59. if (pos < 0 || length < 0 || pos + length > a.length)
  60. throw Error.OutsideBounds;
  61. if (pos == 0 && length == a.length)
  62. return fromData(new UInt16ArrayData(a));
  63. var i = new UInt16Array(a.length);
  64. for (idx in 0...length)
  65. i[idx] = a[idx + pos];
  66. return i;
  67. }
  68. public static function fromBytes(bytes:haxe.io.Bytes, bytePos:Int = 0, ?length:Int):UInt16Array {
  69. if (length == null)
  70. length = (bytes.length - bytePos) >> 1;
  71. return fromData(new UInt16ArrayData(bytes.getData(), bytePos, length));
  72. }
  73. }