Class DynamicArray<TypedArrayCtorType>

A dynamic container that acts very similarly to C++ vectors. Wraps a typed array. Provides safe and unsafe accessors. Unsafe accessors are faster, but should only be used when your code is stable.

Type Parameters

Hierarchy

  • DynamicArray

Constructors

  • Create a new dynamic array from a TypedArray constructor and a starting capacity.

    Type Parameters

    Parameters

    • ctor: TypedArrayCtorType

      The TypedArray constructor. For example, if this dynamic array will be finalized to a Float32Array, then pass the Float32Array constructor to this parameter.

    • capacity: number = START_CAP

      The starting capacity of the dynamic array, which is the length of the internal array.

    Returns DynamicArray<TypedArrayCtorType>

Properties

_length: number = 0
array: null | TypedArray<TypedArrayCtorType>

The actual typed array created by this dynamic array. If the dynamic array has been invalidated, then this will be null.

ctor: TypedArrayCtorType

The TypedArray constructor. For example, if this dynamic array will be finalized to a Float32Array, then pass the Float32Array constructor to this parameter.

Accessors

  • get length(): number
  • The length of the dynamic array. Setting this will expand the dynamic array if necessary, but will not shrink.

    Returns number

  • set length(newLength: number): void
  • Parameters

    • newLength: number

    Returns void

Methods

  • Calls TypedArray.set on the internal array; copies a given array of values to a given offset.

    Parameters

    • offset: number

      The index to start copying to in the internal array.

    • values: ArrayLike<TypedArrayValue<TypedArrayCtorType>>

      The values to copy. All values in this array will be copied.

    Returns void

  • Expand the capacity of the dynamic array to at least fit a given wanted length.

    Parameters

    • wantedLength: number

      The minimum array length that the new capacity should support.

    Returns void

  • Calls TypedArray.fill on the internal array; fills the internal array with a value.

    Parameters

    • fillValue: TypedArrayValue<TypedArrayCtorType>

      The value to fill the array with.

    • Optional startIndex: number

      The start index of the range. 0 by default. Can be negative so that it's relative to the end of the array.

    • Optional endIndex: number

      The end index of the range, exclusive. By default, goes to the end of the array. Can be negative so that it's relative to the end of the array.

    Returns void

  • Turns this dynamic array into a TypedArray ready to be used by external APIs. The dynamic array will be invalidated after this call.

    Creates a new typed array as a view into the same buffer as array (array is invalidated, but not the underlying buffer).

    Returns

    A TypedArray instance with the right length.

    Returns TypedArray<TypedArrayCtorType>

  • Invalidates the internal array. Length will be set to 0, and further operations on the dynamic array will be invalid.

    Returns void

  • Add an element to the end of the array. Increases the length by 1, but does not expand the capacity to fit the new length, for optimisation purposes. Make sure to call expandCapacity before calling this method.

    Parameters

    • value: TypedArrayValue<TypedArrayCtorType>

      The value to push to the end of the array.

    Returns void

  • Calls TypedArray.slice on the internal array; copies a slice of the internal array, and returns that copy.

    Parameters

    • startIndex: number

      The start index of the range.

    • endIndex: number

      The end index of the range, exclusive.

    Returns TypedArray<TypedArrayCtorType>

  • Get the next capacity corresponding to an array length. For example, a wanted length of 0 will return the default starting capacity of a dynamic array. Capacity expands exponentially until a certain threshold, after which capacity expands linearly.

    Parameters

    • wantedLength: number

      The array length used for the capacity calculations.

    Returns number

Generated using TypeDoc