float64array.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 Float64Array implementation that implements
  16. * most of the Float64Array spec and that can be used when a built-in
  17. * implementation is not available.
  18. *
  19. * Note that if no existing Float64Array implementation is found then this
  20. * class and all its public properties are exported as Float64Array.
  21. *
  22. * Adding support for the other TypedArray classes here does not make sense
  23. * since this vector math library only needs Float32Array and Float64Array.
  24. *
  25. */
  26. goog.provide('goog.vec.Float64Array');
  27. /**
  28. * Constructs a new Float64Array. The new array is initialized to all zeros.
  29. *
  30. * @param {goog.vec.Float64Array|Array|ArrayBuffer|number} p0
  31. * The length of the array, or an array to initialize the contents of the
  32. * new Float64Array.
  33. * @constructor
  34. * @implements {IArrayLike<number>}
  35. * @final
  36. */
  37. goog.vec.Float64Array = 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.Float64Array.BYTES_PER_ELEMENT = 8;
  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.Float64Array.prototype.BYTES_PER_ELEMENT = 8;
  58. /**
  59. * Sets elements of the array.
  60. * @param {Array<number>|Float64Array} values The array of values.
  61. * @param {number=} opt_offset The offset in this array to start.
  62. */
  63. goog.vec.Float64Array.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.Float64Array.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 Float64Array implementation is found then we export
  84. * goog.vec.Float64Array as Float64Array.
  85. */
  86. if (typeof Float64Array == 'undefined') {
  87. try {
  88. goog.exportProperty(
  89. goog.vec.Float64Array, 'BYTES_PER_ELEMENT',
  90. goog.vec.Float64Array.BYTES_PER_ELEMENT);
  91. } catch (float64ArrayError) {
  92. // Do nothing. This code is in place to fix b/7225850, in which an error
  93. // is incorrectly thrown for Google TV on an old Chrome.
  94. // TODO(user): remove after that version is retired.
  95. }
  96. goog.exportProperty(
  97. goog.vec.Float64Array.prototype, 'BYTES_PER_ELEMENT',
  98. goog.vec.Float64Array.prototype.BYTES_PER_ELEMENT);
  99. goog.exportProperty(
  100. goog.vec.Float64Array.prototype, 'set',
  101. goog.vec.Float64Array.prototype.set);
  102. goog.exportProperty(
  103. goog.vec.Float64Array.prototype, 'toString',
  104. goog.vec.Float64Array.prototype.toString);
  105. goog.exportSymbol('Float64Array', goog.vec.Float64Array);
  106. }