1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-28 16:20:57 -05:00

feat(type): make Float16Array generic

This commit is contained in:
Kenta Moriuchi 2024-11-20 03:06:16 +09:00
parent 628816448e
commit 3d7e71c59c
No known key found for this signature in database
GPG key ID: AC843C584A91BE0C

View file

@ -3087,7 +3087,7 @@ declare namespace Intl {
* @category Platform * @category Platform
* @experimental * @experimental
*/ */
interface Float16Array { interface Float16Array<TArrayBuffer extends ArrayBufferLike = ArrayBufferLike> {
/** /**
* The size in bytes of each element in the array. * The size in bytes of each element in the array.
*/ */
@ -3096,7 +3096,7 @@ interface Float16Array {
/** /**
* The ArrayBuffer instance referenced by the array. * The ArrayBuffer instance referenced by the array.
*/ */
readonly buffer: ArrayBufferLike; readonly buffer: TArrayBuffer;
/** /**
* The length in bytes of the array. * The length in bytes of the array.
@ -3108,6 +3108,12 @@ interface Float16Array {
*/ */
readonly byteOffset: number; readonly byteOffset: number;
/**
* Returns the item located at the specified index.
* @param index The zero-based index of the desired code unit. A negative index will count back from the last item.
*/
at(index: number): number | undefined;
/** /**
* Returns the this object after copying a section of the array identified by start and end * Returns the this object after copying a section of the array identified by start and end
* to the same array starting at position target * to the same array starting at position target
@ -3128,7 +3134,7 @@ interface Float16Array {
* If thisArg is omitted, undefined is used as the this value. * If thisArg is omitted, undefined is used as the this value.
*/ */
every( every(
predicate: (value: number, index: number, array: Float16Array) => unknown, predicate: (value: number, index: number, array: this) => unknown,
thisArg?: any, thisArg?: any,
): boolean; ): boolean;
@ -3150,9 +3156,9 @@ interface Float16Array {
* If thisArg is omitted, undefined is used as the this value. * If thisArg is omitted, undefined is used as the this value.
*/ */
filter( filter(
predicate: (value: number, index: number, array: Float16Array) => any, predicate: (value: number, index: number, array: this) => any,
thisArg?: any, thisArg?: any,
): Float16Array; ): Float16Array<ArrayBuffer>;
/** /**
* Returns the value of the first element in the array where predicate is true, and undefined * Returns the value of the first element in the array where predicate is true, and undefined
@ -3164,7 +3170,7 @@ interface Float16Array {
* predicate. If it is not provided, undefined is used instead. * predicate. If it is not provided, undefined is used instead.
*/ */
find( find(
predicate: (value: number, index: number, obj: Float16Array) => boolean, predicate: (value: number, index: number, obj: this) => boolean,
thisArg?: any, thisArg?: any,
): number | undefined; ): number | undefined;
@ -3178,7 +3184,51 @@ interface Float16Array {
* predicate. If it is not provided, undefined is used instead. * predicate. If it is not provided, undefined is used instead.
*/ */
findIndex( findIndex(
predicate: (value: number, index: number, obj: Float16Array) => boolean, predicate: (value: number, index: number, obj: this) => boolean,
thisArg?: any,
): number;
/**
* Returns the value of the last element in the array where predicate is true, and undefined
* otherwise.
* @param predicate findLast calls predicate once for each element of the array, in descending
* order, until it finds one where predicate returns true. If such an element is found, findLast
* immediately returns that element value. Otherwise, findLast returns undefined.
* @param thisArg If provided, it will be used as the this value for each invocation of
* predicate. If it is not provided, undefined is used instead.
*/
findLast<S extends number>(
predicate: (
value: number,
index: number,
array: this,
) => value is S,
thisArg?: any,
): S | undefined;
findLast(
predicate: (
value: number,
index: number,
array: this,
) => unknown,
thisArg?: any,
): number | undefined;
/**
* Returns the index of the last element in the array where predicate is true, and -1
* otherwise.
* @param predicate findLastIndex calls predicate once for each element of the array, in descending
* order, until it finds one where predicate returns true. If such an element is found,
* findLastIndex immediately returns that element index. Otherwise, findLastIndex returns -1.
* @param thisArg If provided, it will be used as the this value for each invocation of
* predicate. If it is not provided, undefined is used instead.
*/
findLastIndex(
predicate: (
value: number,
index: number,
array: this,
) => unknown,
thisArg?: any, thisArg?: any,
): number; ): number;
@ -3190,10 +3240,17 @@ interface Float16Array {
* If thisArg is omitted, undefined is used as the this value. * If thisArg is omitted, undefined is used as the this value.
*/ */
forEach( forEach(
callbackfn: (value: number, index: number, array: Float16Array) => void, callbackfn: (value: number, index: number, array: this) => void,
thisArg?: any, thisArg?: any,
): void; ): void;
/**
* Determines whether an array includes a certain element, returning true or false as appropriate.
* @param searchElement The element to search for.
* @param fromIndex The position in this array at which to begin searching for searchElement.
*/
includes(searchElement: number, fromIndex?: number): boolean;
/** /**
* Returns the index of the first occurrence of a value in an array. * Returns the index of the first occurrence of a value in an array.
* @param searchElement The value to locate in the array. * @param searchElement The value to locate in the array.
@ -3231,9 +3288,9 @@ interface Float16Array {
* If thisArg is omitted, undefined is used as the this value. * If thisArg is omitted, undefined is used as the this value.
*/ */
map( map(
callbackfn: (value: number, index: number, array: Float16Array) => number, callbackfn: (value: number, index: number, array: this) => number,
thisArg?: any, thisArg?: any,
): Float16Array; ): Float16Array<ArrayBuffer>;
/** /**
* Calls the specified callback function for all the elements in an array. The return value of * Calls the specified callback function for all the elements in an array. The return value of
@ -3250,7 +3307,7 @@ interface Float16Array {
previousValue: number, previousValue: number,
currentValue: number, currentValue: number,
currentIndex: number, currentIndex: number,
array: Float16Array, array: this,
) => number, ) => number,
): number; ): number;
reduce( reduce(
@ -3258,7 +3315,7 @@ interface Float16Array {
previousValue: number, previousValue: number,
currentValue: number, currentValue: number,
currentIndex: number, currentIndex: number,
array: Float16Array, array: this,
) => number, ) => number,
initialValue: number, initialValue: number,
): number; ): number;
@ -3278,7 +3335,7 @@ interface Float16Array {
previousValue: U, previousValue: U,
currentValue: number, currentValue: number,
currentIndex: number, currentIndex: number,
array: Float16Array, array: this,
) => U, ) => U,
initialValue: U, initialValue: U,
): U; ): U;
@ -3298,7 +3355,7 @@ interface Float16Array {
previousValue: number, previousValue: number,
currentValue: number, currentValue: number,
currentIndex: number, currentIndex: number,
array: Float16Array, array: this,
) => number, ) => number,
): number; ): number;
reduceRight( reduceRight(
@ -3306,7 +3363,7 @@ interface Float16Array {
previousValue: number, previousValue: number,
currentValue: number, currentValue: number,
currentIndex: number, currentIndex: number,
array: Float16Array, array: this,
) => number, ) => number,
initialValue: number, initialValue: number,
): number; ): number;
@ -3326,7 +3383,7 @@ interface Float16Array {
previousValue: U, previousValue: U,
currentValue: number, currentValue: number,
currentIndex: number, currentIndex: number,
array: Float16Array, array: this,
) => U, ) => U,
initialValue: U, initialValue: U,
): U; ): U;
@ -3334,7 +3391,7 @@ interface Float16Array {
/** /**
* Reverses the elements in an Array. * Reverses the elements in an Array.
*/ */
reverse(): Float16Array; reverse(): this;
/** /**
* Sets a value or an array of values. * Sets a value or an array of values.
@ -3348,7 +3405,7 @@ interface Float16Array {
* @param start The beginning of the specified portion of the array. * @param start The beginning of the specified portion of the array.
* @param end The end of the specified portion of the array. This is exclusive of the element at the index 'end'. * @param end The end of the specified portion of the array. This is exclusive of the element at the index 'end'.
*/ */
slice(start?: number, end?: number): Float16Array; slice(start?: number, end?: number): Float16Array<ArrayBuffer>;
/** /**
* Determines whether the specified callback function returns true for any element of an array. * Determines whether the specified callback function returns true for any element of an array.
@ -3359,7 +3416,7 @@ interface Float16Array {
* If thisArg is omitted, undefined is used as the this value. * If thisArg is omitted, undefined is used as the this value.
*/ */
some( some(
predicate: (value: number, index: number, array: Float16Array) => unknown, predicate: (value: number, index: number, array: this) => unknown,
thisArg?: any, thisArg?: any,
): boolean; ): boolean;
@ -3380,12 +3437,34 @@ interface Float16Array {
* @param begin The index of the beginning of the array. * @param begin The index of the beginning of the array.
* @param end The index of the end of the array. * @param end The index of the end of the array.
*/ */
subarray(begin?: number, end?: number): Float16Array; subarray(begin?: number, end?: number): Float16Array<TArrayBuffer>;
/** /**
* Converts a number to a string by using the current locale. * Converts a number to a string by using the current locale.
*/ */
toLocaleString(): string; toLocaleString(
locales?: string | string[],
options?: Intl.NumberFormatOptions,
): string;
/**
* Copies the array and returns the copy with the elements in reverse order.
*/
toReversed(): Float16Array<ArrayBuffer>;
/**
* Copies and sorts the array.
* @param compareFn Function used to determine the order of the elements. It is expected to return
* a negative value if the first argument is less than the second argument, zero if they're equal, and a positive
* value otherwise. If omitted, the elements are sorted in ascending order.
* ```ts
* const myNums = Float16Array<Buffer>.from([11.25, 2, -22.5, 1]);
* myNums.toSorted((a, b) => a - b) // Float16Array<Buffer>(4) [-22.5, 1, 2, 11.5]
* ```
*/
toSorted(
compareFn?: (a: number, b: number) => number,
): Float16Array<ArrayBuffer>;
/** /**
* Returns a string representation of an array. * Returns a string representation of an array.
@ -3393,9 +3472,37 @@ interface Float16Array {
toString(): string; toString(): string;
/** Returns the primitive value of the specified object. */ /** Returns the primitive value of the specified object. */
valueOf(): Float16Array; valueOf(): this;
/**
* Copies the array and inserts the given number at the provided index.
* @param index The index of the value to overwrite. If the index is
* negative, then it replaces from the end of the array.
* @param value The value to insert into the copied array.
* @returns A copy of the original array with the inserted value.
*/
with(index: number, value: number): Float16Array<ArrayBuffer>;
[index: number]: number; [index: number]: number;
[Symbol.iterator](): ArrayIterator<number>;
/**
* Returns an array of key, value pairs for every entry in the array
*/
entries(): ArrayIterator<[number, number]>;
/**
* Returns an list of keys in the array
*/
keys(): ArrayIterator<number>;
/**
* Returns an list of values in the array
*/
values(): ArrayIterator<number>;
readonly [Symbol.toStringTag]: "Float16Array";
} }
/** /**
@ -3403,14 +3510,14 @@ interface Float16Array {
* @experimental * @experimental
*/ */
interface Float16ArrayConstructor { interface Float16ArrayConstructor {
readonly prototype: Float16Array; readonly prototype: Float16Array<ArrayBufferLike>;
new (length: number): Float16Array; new (length?: number): Float16Array<ArrayBuffer>;
new (array: ArrayLike<number> | ArrayBufferLike): Float16Array; new (array: ArrayLike<number> | Iterable<number>): Float16Array<ArrayBuffer>;
new ( new <TArrayBuffer extends ArrayBufferLike = ArrayBuffer>(
buffer: ArrayBufferLike, buffer: TArrayBuffer,
byteOffset?: number, byteOffset?: number,
length?: number, length?: number,
): Float16Array; ): Float16Array<TArrayBuffer>;
/** /**
* The size in bytes of each element in the array. * The size in bytes of each element in the array.
@ -3421,17 +3528,17 @@ interface Float16ArrayConstructor {
* Returns a new array from a set of elements. * Returns a new array from a set of elements.
* @param items A set of elements to include in the new array object. * @param items A set of elements to include in the new array object.
*/ */
of(...items: number[]): Float16Array; of(...items: number[]): Float16Array<ArrayBuffer>;
/** /**
* Creates an array from an array-like or iterable object. * Creates an array from an array-like or iterable object.
* @param arrayLike An array-like or iterable object to convert to an array. * @param arrayLike An array-like object to convert to an array.
*/ */
from(arrayLike: ArrayLike<number>): Float16Array; from(arrayLike: ArrayLike<number>): Float16Array<ArrayBuffer>;
/** /**
* Creates an array from an array-like or iterable object. * Creates an array from an array-like or iterable object.
* @param arrayLike An array-like or iterable object to convert to an array. * @param arrayLike An array-like object to convert to an array.
* @param mapfn A mapping function to call on every element of the array. * @param mapfn A mapping function to call on every element of the array.
* @param thisArg Value of 'this' used to invoke the mapfn. * @param thisArg Value of 'this' used to invoke the mapfn.
*/ */
@ -3439,181 +3546,53 @@ interface Float16ArrayConstructor {
arrayLike: ArrayLike<T>, arrayLike: ArrayLike<T>,
mapfn: (v: T, k: number) => number, mapfn: (v: T, k: number) => number,
thisArg?: any, thisArg?: any,
): Float16Array; ): Float16Array<ArrayBuffer>;
/**
* Creates an array from an array-like or iterable object.
* @param elements An iterable object to convert to an array.
*/
from(elements: Iterable<number>): Float16Array<ArrayBuffer>;
/**
* Creates an array from an array-like or iterable object.
* @param elements An iterable object to convert to an array.
* @param mapfn A mapping function to call on every element of the array.
* @param thisArg Value of 'this' used to invoke the mapfn.
*/
from<T>(
elements: Iterable<T>,
mapfn?: (v: T, k: number) => number,
thisArg?: any,
): Float16Array<ArrayBuffer>;
} }
/** /**
* @category Platform * @category Platform
* @experimental * @experimental
*/ */
declare var Float16Array: Float16ArrayConstructor; declare var Float16Array: Float16ArrayConstructor;
interface Math {
/** /**
* Returns the nearest half precision float representation of a number.
* @param x A numeric expression.
*
* @category Platform * @category Platform
* @experimental * @experimental
*/ */
interface Float16Array { f16round(x: number): number;
[Symbol.iterator](): IterableIterator<number>;
/**
* Returns an array of key, value pairs for every entry in the array
*/
entries(): IterableIterator<[number, number]>;
/**
* Returns an list of keys in the array
*/
keys(): IterableIterator<number>;
/**
* Returns an list of values in the array
*/
values(): IterableIterator<number>;
} }
/** interface DataView<TArrayBuffer extends ArrayBufferLike> {
* @category Platform
* @experimental
*/
interface Float16Constructor {
new (elements: Iterable<number>): Float16Array;
/**
* Creates an array from an array-like or iterable object.
* @param arrayLike An array-like or iterable object to convert to an array.
* @param mapfn A mapping function to call on every element of the array.
* @param thisArg Value of 'this' used to invoke the mapfn.
*/
from(
arrayLike: Iterable<number>,
mapfn?: (v: number, k: number) => number,
thisArg?: any,
): Float16Array;
}
/**
* @category Platform
* @experimental
*/
interface Float16Array {
readonly [Symbol.toStringTag]: "Float16Array";
}
/**
* @category Platform
* @experimental
*/
interface Float16Array {
/**
* Determines whether an array includes a certain element, returning true or false as appropriate.
* @param searchElement The element to search for.
* @param fromIndex The position in this array at which to begin searching for searchElement.
*/
includes(searchElement: number, fromIndex?: number): boolean;
}
/**
* @category Platform
* @experimental
*/
interface Float16ArrayConstructor {
new (): Float16Array;
}
/**
* @category Platform
* @experimental
*/
interface Float16Array {
/**
* Returns the item located at the specified index.
* @param index The zero-based index of the desired code unit. A negative index will count back from the last item.
*/
at(index: number): number | undefined;
}
/**
* @category Platform
* @experimental
*/
interface Float16Array {
/**
* Returns the value of the last element in the array where predicate is true, and undefined
* otherwise.
* @param predicate findLast calls predicate once for each element of the array, in descending
* order, until it finds one where predicate returns true. If such an element is found, findLast
* immediately returns that element value. Otherwise, findLast returns undefined.
* @param thisArg If provided, it will be used as the this value for each invocation of
* predicate. If it is not provided, undefined is used instead.
*/
findLast<S extends number>(
predicate: (
value: number,
index: number,
array: Float16Array,
) => value is S,
thisArg?: any,
): S | undefined;
findLast(
predicate: (
value: number,
index: number,
array: Float16Array,
) => unknown,
thisArg?: any,
): number | undefined;
/**
* Returns the index of the last element in the array where predicate is true, and -1
* otherwise.
* @param predicate findLastIndex calls predicate once for each element of the array, in descending
* order, until it finds one where predicate returns true. If such an element is found,
* findLastIndex immediately returns that element index. Otherwise, findLastIndex returns -1.
* @param thisArg If provided, it will be used as the this value for each invocation of
* predicate. If it is not provided, undefined is used instead.
*/
findLastIndex(
predicate: (
value: number,
index: number,
array: Float16Array,
) => unknown,
thisArg?: any,
): number;
/**
* Copies the array and returns the copy with the elements in reverse order.
*/
toReversed(): Float16Array;
/**
* Copies and sorts the array.
* @param compareFn Function used to determine the order of the elements. It is expected to return
* a negative value if the first argument is less than the second argument, zero if they're equal, and a positive
* value otherwise. If omitted, the elements are sorted in ascending order.
* ```ts
* const myNums = Float16Array.from([11.25, 2, -22.5, 1]);
* myNums.toSorted((a, b) => a - b) // Float16Array(4) [-22.5, 1, 2, 11.5]
* ```
*/
toSorted(compareFn?: (a: number, b: number) => number): Float16Array;
/**
* Copies the array and inserts the given number at the provided index.
* @param index The index of the value to overwrite. If the index is
* negative, then it replaces from the end of the array.
* @param value The value to insert into the copied array.
* @returns A copy of the original array with the inserted value.
*/
with(index: number, value: number): Float16Array;
}
/**
* @category Platform
* @experimental
*/
interface DataView {
/** /**
* Gets the Float16 value at the specified byte offset from the start of the view. There is * Gets the Float16 value at the specified byte offset from the start of the view. There is
* no alignment constraint; multi-byte values may be fetched from any offset. * no alignment constraint; multi-byte values may be fetched from any offset.
* @param byteOffset The place in the buffer at which the value should be retrieved. * @param byteOffset The place in the buffer at which the value should be retrieved.
* @param littleEndian If false or undefined, a big-endian value should be read. * @param littleEndian If false or undefined, a big-endian value should be read.
*
* @category Platform
* @experimental
*/ */
getFloat16(byteOffset: number, littleEndian?: boolean): number; getFloat16(byteOffset: number, littleEndian?: boolean): number;
@ -3622,6 +3601,9 @@ interface DataView {
* @param byteOffset The place in the buffer at which the value should be set. * @param byteOffset The place in the buffer at which the value should be set.
* @param value The value to set. * @param value The value to set.
* @param littleEndian If false or undefined, a big-endian value should be written. * @param littleEndian If false or undefined, a big-endian value should be written.
*
* @category Platform
* @experimental
*/ */
setFloat16(byteOffset: number, value: number, littleEndian?: boolean): void; setFloat16(byteOffset: number, value: number, littleEndian?: boolean): void;
} }