float64array.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /**
  2. * @license
  3. * Copyright The Closure Library Authors.
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. /**
  7. * @fileoverview Supplies a Float64Array implementation that implements
  8. * most of the Float64Array spec and that can be used when a built-in
  9. * implementation is not available.
  10. *
  11. * Note that if no existing Float64Array implementation is found then this
  12. * class and all its public properties are exported as Float64Array.
  13. *
  14. * Adding support for the other TypedArray classes here does not make sense
  15. * since this vector math library only needs Float32Array and Float64Array.
  16. */
  17. goog.provide('goog.vec.Float64Array');
  18. /**
  19. * Constructs a new Float64Array. The new array is initialized to all zeros.
  20. *
  21. * @param {goog.vec.Float64Array|Array|ArrayBuffer|number} p0
  22. * The length of the array, or an array to initialize the contents of the
  23. * new Float64Array.
  24. * @constructor
  25. * @implements {IArrayLike<number>}
  26. * @final
  27. */
  28. goog.vec.Float64Array = 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.Float64Array.BYTES_PER_ELEMENT = 8;
  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.Float64Array.prototype.BYTES_PER_ELEMENT = 8;
  49. /**
  50. * Sets elements of the array.
  51. * @param {Array<number>|Float64Array} values The array of values.
  52. * @param {number=} opt_offset The offset in this array to start.
  53. */
  54. goog.vec.Float64Array.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.Float64Array.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 Float64Array implementation is found then we export
  75. * goog.vec.Float64Array as Float64Array.
  76. */
  77. if (typeof Float64Array == 'undefined') {
  78. try {
  79. goog.exportProperty(
  80. goog.vec.Float64Array, 'BYTES_PER_ELEMENT',
  81. goog.vec.Float64Array.BYTES_PER_ELEMENT);
  82. } catch (float64ArrayError) {
  83. // Do nothing. This code is in place to fix b/7225850, in which an error
  84. // is incorrectly thrown for Google TV on an old Chrome.
  85. // TODO(user): remove after that version is retired.
  86. }
  87. goog.exportProperty(
  88. goog.vec.Float64Array.prototype, 'BYTES_PER_ELEMENT',
  89. goog.vec.Float64Array.prototype.BYTES_PER_ELEMENT);
  90. goog.exportProperty(
  91. goog.vec.Float64Array.prototype, 'set',
  92. goog.vec.Float64Array.prototype.set);
  93. goog.exportProperty(
  94. goog.vec.Float64Array.prototype, 'toString',
  95. goog.vec.Float64Array.prototype.toString);
  96. goog.exportSymbol('Float64Array', goog.vec.Float64Array);
  97. }