float32array.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /**
  2. * @license
  3. * Copyright The Closure Library Authors.
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. /**
  7. * @fileoverview Supplies a Float32Array implementation that implements
  8. * most of the Float32Array spec and that can be used when a built-in
  9. * implementation is not available.
  10. *
  11. * Note that if no existing Float32Array implementation is found then
  12. * this class and all its public properties are exported as Float32Array.
  13. *
  14. * Adding support for the other TypedArray classes here does not make sense
  15. * since this vector math library only needs Float32Array.
  16. */
  17. goog.provide('goog.vec.Float32Array');
  18. /**
  19. * Constructs a new Float32Array. The new array is initialized to all zeros.
  20. *
  21. * @param {goog.vec.Float32Array|Array|ArrayBuffer|number} p0
  22. * The length of the array, or an array to initialize the contents of the
  23. * new Float32Array.
  24. * @constructor
  25. * @implements {IArrayLike<number>}
  26. * @final
  27. */
  28. goog.vec.Float32Array = function(p0) {
  29. /** @type {number} */
  30. this.length = /** @type {number} */ (p0.length || p0);
  31. for (let i = 0; i < this.length; i++) {
  32. this[i] = p0[i] || 0;
  33. }
  34. };
  35. /**
  36. * The number of bytes in an element (as defined by the Typed Array
  37. * specification).
  38. *
  39. * @type {number}
  40. */
  41. goog.vec.Float32Array.BYTES_PER_ELEMENT = 4;
  42. /**
  43. * The number of bytes in an element (as defined by the Typed Array
  44. * specification).
  45. *
  46. * @type {number}
  47. */
  48. goog.vec.Float32Array.prototype.BYTES_PER_ELEMENT = 4;
  49. /**
  50. * Sets elements of the array.
  51. * @param {Array<number>|Float32Array} values The array of values.
  52. * @param {number=} opt_offset The offset in this array to start.
  53. */
  54. goog.vec.Float32Array.prototype.set = function(values, opt_offset) {
  55. opt_offset = opt_offset || 0;
  56. for (let i = 0; i < values.length && opt_offset + i < this.length; i++) {
  57. this[opt_offset + i] = values[i];
  58. }
  59. };
  60. /**
  61. * Creates a string representation of this array.
  62. * @return {string} The string version of this array.
  63. * @override
  64. */
  65. goog.vec.Float32Array.prototype.toString = Array.prototype.join;
  66. /**
  67. * Note that we cannot implement the subarray() or (deprecated) slice()
  68. * methods properly since doing so would require being able to overload
  69. * the [] operator which is not possible in javascript. So we leave
  70. * them unimplemented. Any attempt to call these methods will just result
  71. * in a javascript error since we leave them undefined.
  72. */
  73. /**
  74. * If no existing Float32Array implementation is found then we export
  75. * goog.vec.Float32Array as Float32Array.
  76. */
  77. if (typeof Float32Array == 'undefined') {
  78. goog.exportProperty(
  79. goog.vec.Float32Array, 'BYTES_PER_ELEMENT',
  80. goog.vec.Float32Array.BYTES_PER_ELEMENT);
  81. goog.exportProperty(
  82. goog.vec.Float32Array.prototype, 'BYTES_PER_ELEMENT',
  83. goog.vec.Float32Array.prototype.BYTES_PER_ELEMENT);
  84. goog.exportProperty(
  85. goog.vec.Float32Array.prototype, 'set',
  86. goog.vec.Float32Array.prototype.set);
  87. goog.exportProperty(
  88. goog.vec.Float32Array.prototype, 'toString',
  89. goog.vec.Float32Array.prototype.toString);
  90. goog.exportSymbol('Float32Array', goog.vec.Float32Array);
  91. }