UNPKG

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