float32array.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // Copyright 2011 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /**
  15. * @fileoverview Supplies a Float32Array implementation that implements
  16. * most of the Float32Array spec and that can be used when a built-in
  17. * implementation is not available.
  18. *
  19. * Note that if no existing Float32Array implementation is found then
  20. * this class and all its public properties are exported as Float32Array.
  21. *
  22. * Adding support for the other TypedArray classes here does not make sense
  23. * since this vector math library only needs Float32Array.
  24. *
  25. */
  26. goog.provide('goog.vec.Float32Array');
  27. /**
  28. * Constructs a new Float32Array. The new array is initialized to all zeros.
  29. *
  30. * @param {goog.vec.Float32Array|Array|ArrayBuffer|number} p0
  31. * The length of the array, or an array to initialize the contents of the
  32. * new Float32Array.
  33. * @constructor
  34. * @implements {IArrayLike<number>}
  35. * @final
  36. */
  37. goog.vec.Float32Array = function(p0) {
  38. /** @type {number} */
  39. this.length = /** @type {number} */ (p0.length || p0);
  40. for (var i = 0; i < this.length; i++) {
  41. this[i] = p0[i] || 0;
  42. }
  43. };
  44. /**
  45. * The number of bytes in an element (as defined by the Typed Array
  46. * specification).
  47. *
  48. * @type {number}
  49. */
  50. goog.vec.Float32Array.BYTES_PER_ELEMENT = 4;
  51. /**
  52. * The number of bytes in an element (as defined by the Typed Array
  53. * specification).
  54. *
  55. * @type {number}
  56. */
  57. goog.vec.Float32Array.prototype.BYTES_PER_ELEMENT = 4;
  58. /**
  59. * Sets elements of the array.
  60. * @param {Array<number>|Float32Array} values The array of values.
  61. * @param {number=} opt_offset The offset in this array to start.
  62. */
  63. goog.vec.Float32Array.prototype.set = function(values, opt_offset) {
  64. opt_offset = opt_offset || 0;
  65. for (var i = 0; i < values.length && opt_offset + i < this.length; i++) {
  66. this[opt_offset + i] = values[i];
  67. }
  68. };
  69. /**
  70. * Creates a string representation of this array.
  71. * @return {string} The string version of this array.
  72. * @override
  73. */
  74. goog.vec.Float32Array.prototype.toString = Array.prototype.join;
  75. /**
  76. * Note that we cannot implement the subarray() or (deprecated) slice()
  77. * methods properly since doing so would require being able to overload
  78. * the [] operator which is not possible in javascript. So we leave
  79. * them unimplemented. Any attempt to call these methods will just result
  80. * in a javascript error since we leave them undefined.
  81. */
  82. /**
  83. * If no existing Float32Array implementation is found then we export
  84. * goog.vec.Float32Array as Float32Array.
  85. */
  86. if (typeof Float32Array == 'undefined') {
  87. goog.exportProperty(
  88. goog.vec.Float32Array, 'BYTES_PER_ELEMENT',
  89. goog.vec.Float32Array.BYTES_PER_ELEMENT);
  90. goog.exportProperty(
  91. goog.vec.Float32Array.prototype, 'BYTES_PER_ELEMENT',
  92. goog.vec.Float32Array.prototype.BYTES_PER_ELEMENT);
  93. goog.exportProperty(
  94. goog.vec.Float32Array.prototype, 'set',
  95. goog.vec.Float32Array.prototype.set);
  96. goog.exportProperty(
  97. goog.vec.Float32Array.prototype, 'toString',
  98. goog.vec.Float32Array.prototype.toString);
  99. goog.exportSymbol('Float32Array', goog.vec.Float32Array);
  100. }