WrappedTypedArray

require.mx('mxjs/base/utilities/wrapped_typed_array.js')

Wrap TypedArray objects to provide functions that rely on dynamic sizing (push, pop, etc.).

StatusName

Class: WrappedTypedArray

A WrappedTypedArray object wraps a native TypedArray object and provides convenience functions for dynamically changing the length of the internal TypedArray. Because Javascript typed arrays have a fixed length, this is simulated by storing a separate 'used length' variable and only considering elements up to the 'used length'. When the 'used length' exceeds the real length of the internal typed array, a larger internal typed array is constructed and the values from the old internal typed array are copied into the new one.

Note that WrappedTypedArray is a placeholder for one of the following names, all of which provide the same interface described here, but use a different native TypedArray internally:

  • WrappedInt8Array
  • WrappedUint8Array
  • WrappedUint8ClampedArray
  • WrappedInt16Array
  • WrappedUint16Array
  • WrappedInt32Array
  • WrappedUint32Array
  • WrappedFloat32Array
  • WrappedFloat64Array


With the current version of Javascript, it is not possible to fully emulate the interface of a native TypedArray (or Array), so some elemental operations such as iteration or square-bracket access (i.e. array[0]) are not possible with the WrappedTypedArray object itself. For these purposes, you can access the internal TypedArray using the internal property, but you must be careful not to iterate past the length defined in the parent WrappedTypedArray.

Correct:
const wta = new LibWTA.WrappedFloat32Array(...);
 for (let i = 0; i < wta.length; ++i) {
   print(wta.internal[i]);
 }
 


Incorrect:
const wta = new LibWTA.WrappedFloat32Array(...);
 for (let i = 0; i < wta.internal.length; ++i) {
   print(wta.internal[i]);
 }
 


Incorrect:
const wta = new LibWTA.WrappedFloat32Array(...);
 for (let i in wta.internal) {
   print(wta.internal[i]);
 }
 

StatusName
Member TypedArray TypedArray

the underlying type of TypedArray used by this WrappedTypedArray.

Member Number length

sets or returns the number of elements in this array (equivalent to Array.length).

Member property

{TypedArray} internal gets the internal (fixed-length) TypedArray. In general, it is not safe to save and reuse this object (because the internal TypedArray may be replaced when extending the length of the WrappedTypedArray, at which point the saved TypedArray would no longer refer to the same object as the internal TypedArray).

Boolean every ( Function callback, Object thisArg )

Equivalent to Array.prototype.every(): https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/every

Number get ( Number index )

Get the value of the element at the given index.

Number indexOf ( Number searchElement, Number (optional) )

Equivalent to TypedArray.prototype.indexOf(): https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/indexOf

String join ( String separator )

Equivalent to Array.prototype.join(): https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/join

Number pop ( )

Equivalent to Array.prototype.pop(): https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/pop

push ( Number value )

Equivalent to Array.prototype.push(): https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push

push_array ( Array array )

Push all values in the given array onto the end of this array. This is a convenience function, equivalent to calling push() independently for each element in the passed array, but simpler and more performant.

reserve ( Number length )

Ensure that the underlying TypedArray has enough room for length elements. If you know that you're going to be adding a lot of elements to a WrappedTypedArray, it may improve performance to reserve the space up-front (as when writing to output tables).

reverse ( )

Equivalent to Array.prototype.reverse(): https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reverse

set ( Number index, Number value )

Sets the value of the element at the given index to the supplied value.

WrappedTypedArray slice ( Number begin, Number end )

Equivalent to Array.prototype.slice(): https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice

SmallSubarray smallsubarray ( Number begin, Number end )

Creates a "view" of a section of this array, starting from the index begin and continuing while the index is less than end. This is similar to TypedArray.prototype.subarray(): https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/subarray

Note that the existing builtin TypedArray.prototype.subarray() method has poor performance, so this uses a workaround object that has better performance, but does not have all of the methods that would be present on a real subarray. Furthermore, because of the nature of the workaround object, it should only be used for small subarrays (e.g. to use for a 3-length subarray defining an XYZ point).

Boolean some ( Function callback, Object thisArg )

Equivalent to Array.prototype.some(): https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some

toTypedArray ( )

WrappedTypedArray.every ( callback, thisArg )

Equivalent to Array.prototype.every(): https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/every

Parameters: Returns: Boolean result
WrappedTypedArray.get ( index )

Get the value of the element at the given index.

Parameters: Returns: Number value - the value of the element at the given index
WrappedTypedArray.indexOf ( searchElement, (optional) )

Equivalent to TypedArray.prototype.indexOf(): https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/indexOf

Parameters: Returns: Number index
WrappedTypedArray.join ( separator )

Equivalent to Array.prototype.join(): https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/join

Parameters:
  • String separator
Returns: String result
WrappedTypedArray.pop ( )

Equivalent to Array.prototype.pop(): https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/pop

Parameters:
Returns: Number popped
WrappedTypedArray.push ( value )

Equivalent to Array.prototype.push(): https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push

Parameters:
WrappedTypedArray.push_array ( array )

Push all values in the given array onto the end of this array. This is a convenience function, equivalent to calling push() independently for each element in the passed array, but simpler and more performant.

Parameters:
WrappedTypedArray.reserve ( length )

Ensure that the underlying TypedArray has enough room for length elements. If you know that you're going to be adding a lot of elements to a WrappedTypedArray, it may improve performance to reserve the space up-front (as when writing to output tables).

Parameters:
  • Number length - attempt to reserve space for length elements
WrappedTypedArray.reverse ( )

Equivalent to Array.prototype.reverse(): https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reverse

Parameters:
WrappedTypedArray.set ( index, value )

Sets the value of the element at the given index to the supplied value.

Parameters:
WrappedTypedArray.slice ( begin, end )

Equivalent to Array.prototype.slice(): https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice

Parameters: Returns: WrappedTypedArray slice
WrappedTypedArray.smallsubarray ( begin, end )

Creates a "view" of a section of this array, starting from the index begin and continuing while the index is less than end. This is similar to TypedArray.prototype.subarray(): https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/subarray

Note that the existing builtin TypedArray.prototype.subarray() method has poor performance, so this uses a workaround object that has better performance, but does not have all of the methods that would be present on a real subarray. Furthermore, because of the nature of the workaround object, it should only be used for small subarrays (e.g. to use for a 3-length subarray defining an XYZ point).

Parameters: Returns: SmallSubarray subarray
WrappedTypedArray.some ( callback, thisArg )

Equivalent to Array.prototype.some(): https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some

Parameters: Returns: Boolean result
WrappedTypedArray.toTypedArray ( )

Parameters:
Returns: {TypedArray}