UNPKG

@ain1084/array-buffer-partitioner

Version:

Partition an ArrayBuffer into multiple TypedArray views efficiently.

51 lines (50 loc) 2.06 kB
/** * Creates multiple TypedArray views on a single ArrayBuffer or SharedArrayBuffer. * The function takes a constructor (either ArrayBuffer or SharedArrayBuffer) and a configuration object, * then allocates the buffer and returns the specified TypedArray views. * * @typeParam T - A record specifying the TypedArray types and sizes. * @param BufferType - The constructor for the buffer, either ArrayBuffer or SharedArrayBuffer. * @param config - An object specifying the desired views. Each key represents the name of the view, and the value is a tuple where: * - The first element is the TypedArray class name (e.g., Float32Array, Uint32Array). * - The second element is the number of elements for that view. * * @returns An object containing the views, with each key corresponding to the provided configuration. * * @example * ```typescript * import { createArrayBufferViews } from '@ain1084/array-buffer-partitioner'; * * const views = createArrayBufferViews(ArrayBuffer, { * data: [Float32Array, 1024], * index: [Uint32Array, 1], * flag: [Uint8Array, 1] * }); * * console.log(views.data.length); // 1024 * console.log(views.data.byteOffset); // 0 * console.log(views.index.length); // 1 * console.log(views.flag.length); // 1 * console.log(views.flag.byteOffset); // 4100 * console.log(views.data.buffer.byteLength); // 4104 * ``` */ export declare function createArrayBufferViews<T extends Record<string, [ArrayTypeConstructor<SupportedArrays>, number]>>(BufferType: { new (size: number): ArrayBuffer; }, config: T): { [K in keyof T]: InstanceType<T[K][0]>; }; /** * Represents the constructor for supported TypedArray types. * @internal */ type ArrayTypeConstructor<R> = { new (buffer: ArrayBuffer, byteOffset: number, length: number): R; BYTES_PER_ELEMENT: number; }; /** * Lists the supported TypedArray types. * @internal */ type SupportedArrays = Float32Array | Uint32Array | Uint8Array | Int32Array | Int16Array | Uint16Array | Float64Array | BigInt64Array | BigUint64Array; export {};