UNPKG

9.78 kBTypeScriptView Raw
1export type ArrayLikeIterable<T> = ArrayLike<T> & Iterable<T>;
2export type NumericArray = number[] | TypedArray;
3export type TypedArray = Float32Array | Float64Array | Int8Array | Int16Array | Int32Array | Uint8Array | Uint8ClampedArray | Uint16Array | Uint32Array;
4export type BigTypedArray = BigInt64Array | BigUint64Array;
5export type FloatArray = Float32Array | Float64Array;
6export type IntArray = Int8Array | Int16Array | Int32Array;
7export type UIntArray = Uint8Array | Uint8ClampedArray | Uint16Array | Uint32Array;
8export type FloatArrayConstructor = Float32ArrayConstructor | Float64ArrayConstructor;
9export type IntArrayConstructor = Int8ArrayConstructor | Int16ArrayConstructor | Int32ArrayConstructor;
10export type UIntArrayConstructor = Uint8ArrayConstructor | Uint8ClampedArrayConstructor | Uint16ArrayConstructor | Uint32ArrayConstructor;
11export type BigIntArrayConstructor = BigInt64ArrayConstructor | BigUint64ArrayConstructor;
12export type TypedArrayConstructor = FloatArrayConstructor | IntArrayConstructor | UIntArrayConstructor;
13/**
14 * Type IDs for typed array backed buffers and generally describing binary data
15 * values.
16 *
17 * {@link GLType} {@link GL2TYPE} {@link TYPE2GL}
18 */
19export type Type = "u8" | "u8c" | "i8" | "u16" | "i16" | "u32" | "i32" | "f32" | "f64";
20export type BigType = "i64" | "u64";
21export type UintType = "u8" | "u8c" | "u16" | "u32";
22export type IntType = "i8" | "i16" | "i32";
23export type FloatType = "f32" | "f64";
24/**
25 * WebGL numeric type constants. Use {@link GL2TYPE} to convert, if needed.
26 *
27 * {@link Type}
28 * {@link GL2TYPE}
29 * {@link TYPE2GL}
30 */
31export declare enum GLType {
32 I8 = 5120,
33 U8 = 5121,
34 I16 = 5122,
35 U16 = 5123,
36 I32 = 5124,
37 U32 = 5125,
38 F32 = 5126
39}
40/**
41 * Conversion from {@link GLType} to {@link Type} enums.
42 */
43export declare const GL2TYPE: Record<GLType, Type>;
44/**
45 * Potentially lossy conversion from {@link Type} to {@link GLType} enums.
46 *
47 * Not all enums are mappable:
48 *
49 * - `F64` maps to `undefined`, since unsupported by WebGL
50 * - `U8C` maps to "u8"
51 */
52export declare const TYPE2GL: Record<Type, GLType | undefined>;
53/**
54 * Size information (in bytes) for {@link Type} and {@link BigType}. Also see
55 * {@link sizeOf}.
56 */
57export declare const SIZEOF: {
58 u8: number;
59 u8c: number;
60 i8: number;
61 u16: number;
62 i16: number;
63 u32: number;
64 i32: number;
65 i64: number;
66 u64: number;
67 f32: number;
68 f64: number;
69};
70/**
71 * Bit shift values to convert byte addresses into array indices for all
72 * {@link Type}s and {@link BigType}s.
73 */
74export declare const BIT_SHIFTS: {
75 i8: number;
76 u8: number;
77 u8c: number;
78 i16: number;
79 u16: number;
80 i32: number;
81 u32: number;
82 i64: number;
83 u64: number;
84 f32: number;
85 f64: number;
86};
87export declare const FLOAT_ARRAY_CTORS: Record<FloatType, FloatArrayConstructor>;
88export declare const INT_ARRAY_CTORS: Record<IntType, IntArrayConstructor>;
89export declare const UINT_ARRAY_CTORS: Record<UintType, UIntArrayConstructor>;
90export declare const BIGINT_ARRAY_CTORS: Record<BigType, BigIntArrayConstructor>;
91export declare const TYPEDARRAY_CTORS: Record<Type, TypedArrayConstructor>;
92export interface TypedArrayTypeMap extends Record<Type | GLType, TypedArray> {
93 u8: Uint8Array;
94 u8c: Uint8ClampedArray;
95 i8: Int8Array;
96 u16: Uint16Array;
97 i16: Int16Array;
98 u32: Uint32Array;
99 i32: Int32Array;
100 f32: Float32Array;
101 f64: Float64Array;
102 [GLType.U8]: Uint8Array;
103 [GLType.I8]: Int8Array;
104 [GLType.U16]: Uint16Array;
105 [GLType.I16]: Int16Array;
106 [GLType.U32]: Uint32Array;
107 [GLType.I32]: Int32Array;
108 [GLType.F32]: Float32Array;
109}
110export interface BigTypedArrayTypeMap extends Record<BigType, BigTypedArray> {
111 i64: BigInt64Array;
112 u64: BigUint64Array;
113}
114/**
115 * Returns canonical {@link Type} value of `type` by first
116 * attempting to resolve it as {@link GLType} enum.
117 *
118 * @example
119 * ```ts tangle:../export/as-native-type.ts
120 * import { asNativeType, GLType } from "@thi.ng/api";
121 *
122 * console.log(
123 * asNativeType(GLType.F32)
124 * );
125 * // "f32"
126 *
127 * console.log(
128 * asNativeType("f32")
129 * );
130 * // "f32"
131 * ```
132 *
133 * @param type -
134 */
135export declare const asNativeType: (type: GLType | Type) => Type;
136/**
137 * Returns suitable {@link GLType} enum of `type`.
138 *
139 * @example
140 * ```ts tangle:../export/as-gl-type.ts
141 * import { asGLType, GLType } from "@thi.ng/api";
142 *
143 * console.log(
144 * asGLType("f32")
145 * );
146 * // 5126 (aka GLType.F32)
147 *
148 * console.log(
149 * asGLType(GLType.F32)
150 * );
151 * // 5126 (aka GLType.F32)
152 * ```
153 *
154 * @param type -
155 */
156export declare const asGLType: (type: GLType | Type) => GLType;
157/**
158 * Coerces given numeric args to integer values.
159 */
160export declare const asInt: (...args: number[]) => number[];
161/**
162 * Returns byte size for given {@link Type} ID or {@link GLType} enum.
163 *
164 * @param type -
165 */
166export declare const sizeOf: (type: Type | BigType | GLType) => number;
167/**
168 * Constructs new typed array of given {@link Type}, {@link GLType} or
169 * {@link BigType}. Supports all arities of standard typed array ctors.
170 *
171 * @param type - array type enum
172 */
173export declare function typedArray<T extends Type | GLType>(type: T, length: number): TypedArrayTypeMap[T];
174export declare function typedArray<T extends Type | GLType>(type: T, src: ArrayLike<number> | ArrayBufferLike): TypedArrayTypeMap[T];
175export declare function typedArray<T extends Type | GLType>(type: T, buf: ArrayBufferLike, byteOffset: number, length?: number): TypedArrayTypeMap[T];
176export declare function typedArray<T extends BigType>(type: T, length: number): BigTypedArrayTypeMap[T];
177export declare function typedArray<T extends BigType>(type: T, src: ArrayLike<bigint> | ArrayBufferLike): BigTypedArrayTypeMap[T];
178export declare function typedArray<T extends BigType>(type: T, buf: ArrayBufferLike, byteOffset: number, length?: number): BigTypedArrayTypeMap[T];
179/**
180 * Constructs a typed array for given `type` and populates it with given vector
181 * values.
182 *
183 * @remarks
184 * The size of the array will be `data.length * stride`, where `stride` is the
185 * number of elements per item and defaulting to the size of the first data
186 * item/vector given.
187 *
188 * @example
189 * ```ts tangle:../export/typed-array.ts
190 * import { typedArrayOfVec } from "@thi.ng/api";
191 *
192 * // inferred stride=2 (2d vectors)
193 * console.log(
194 * typedArrayOfVec("f32", [[1,2], [3,4], [-10,20]])
195 * );
196 * // Float32Array(6) [ 1, 2, 3, 4, -10, 20 ]
197 *
198 * // with custom stride=4
199 * console.log(
200 * typedArrayOfVec("f32", [[1,2], [3,4], [-10,20]], 4)
201 * );
202 * // Float32Array(12) [ 1, 2, 0, 0, 3,4, 0, 0, -10, 20, 0, 0 ]
203 * ```
204 *
205 * @param type
206 * @param data
207 * @param stride
208 */
209export declare function typedArrayOfVec<T extends Type | GLType>(type: T, data: Iterable<ArrayLike<number>>, stride?: number): TypedArrayTypeMap[T];
210export declare function typedArrayOfVec<T extends BigType>(type: T, data: Iterable<ArrayLike<bigint>>, stride?: number): BigTypedArrayTypeMap[T];
211/**
212 * Takes an {@link NumericArray} and returns its corresponding {@link Type} ID.
213 * Standard JS arrays will default to "f64".
214 *
215 * @param x -
216 */
217export declare const typedArrayType: (x: NumericArray) => Type;
218/**
219 * Returns the smallest possible *unsigned* int type enum for given `x`.
220 * E.g. if `x <= 256`, the function returns `"u8"`.
221 *
222 * @param x - value to classify
223 */
224export declare const uintTypeForSize: (x: number) => UintType;
225/**
226 * Returns the smallest possible *signed* int type enum for given `x`.
227 * E.g. if `x >= -128 && x < 128`, the function returns `"i8"`.
228 *
229 * @param x - value to classify
230 */
231export declare const intTypeForSize: (x: number) => IntType;
232/**
233 * Returns suitable {@link UintType} for given bit size (`[0,32]` range)
234 *
235 * @param x -
236 */
237export declare const uintTypeForBits: (x: number) => UintType;
238/**
239 * Returns suitable {@link IntType} for given bit size (`[0,32]` range)
240 *
241 * @param x -
242 */
243export declare const intTypeForBits: (x: number) => IntType;
244/**
245 * Returns the next smaller {@link IntType} for given type (or the same type if
246 * already the narrowest).
247 *
248 * @param t
249 */
250export declare const narrowInt: (t: IntType | "i64") => "i8" | "i16" | "i32";
251/**
252 * Returns the next larger {@link IntType} for given type (or the same type if
253 * already the widest).
254 *
255 * @param t
256 */
257export declare const widenInt: (t: IntType) => "i16" | "i32" | "i64";
258/**
259 * Returns the next smaller {@link UintType} for given type (or the same type if
260 * already the narrowest).
261 *
262 * @remarks
263 * If type is `u8c`, returns `u8`.
264 *
265 * @param t
266 */
267export declare const narrowUint: (t: UintType | "u64") => "u8" | "u16" | "u32";
268/**
269 * Returns the next larger {@link UintType} for given type (or the same type if
270 * already the widest).
271 *
272 * @param t
273 */
274export declare const widenUint: (t: UintType) => "u16" | "u32" | "u64";
275/**
276 * Returns the next smaller {@link FloatType} for given type (or the same type
277 * if already the narrowest).
278 *
279 * @param t
280 */
281export declare const narrowFloat: (t: FloatType) => string;
282/**
283 * Returns the next larger {@link FloatType} for given type (or the same type if
284 * already the widest).
285 *
286 * @param t
287 */
288export declare const widenFloat: (t: FloatType) => string;
289/**
290 * Returns the next smaller type (i.e. {@link IntType}, {@link UintType} or
291 * {@link FloatType}) for given type (or the same type if already the smallest).
292 *
293 * @param t
294 */
295export declare const narrowType: (t: Type | BigType) => string;
296/**
297 * Returns the next larger type (i.e. {@link IntType}, {@link UintType} or
298 * {@link FloatType}) for given type (or the same type if already the widest).
299 *
300 * @param t
301 */
302export declare const widenType: (t: Type | BigType) => string;
303//# sourceMappingURL=typedarray.d.ts.map
\No newline at end of file