Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

/// <reference types="@stdlib/types"/>

import { Collection, Complex64Array, Complex128Array, AccessorArrayLike } from '@stdlib/types/array';
import { Collection, Complex64Array, Complex128Array, Float16Array, AccessorArrayLike } from '@stdlib/types/array';
import { Complex64, Complex128, ComplexLike } from '@stdlib/types/complex';

/**
Expand All @@ -43,6 +43,15 @@ type GetFloat64 = ( arr: Float64Array, idx: number ) => number | void;
*/
type GetFloat32 = ( arr: Float32Array, idx: number ) => number | void;

/**
* Returns an element from a `Float16Array`.
*
* @param arr - input array
* @param idx - element index
* @returns element value
*/
type GetFloat16 = ( arr: Float16Array, idx: number ) => number | void;

/**
* Returns an element from an `Int32Array`.
*
Expand Down Expand Up @@ -169,6 +178,15 @@ type SetFloat64 = ( arr: Float64Array, idx: number, value: number ) => void;
*/
type SetFloat32 = ( arr: Float32Array, idx: number, value: number ) => void;

/**
* Sets an element in a `Float16Array`.
*
* @param arr - input array
* @param idx - element index
* @param value - value to set
*/
type SetFloat16 = ( arr: Float16Array, idx: number, value: number ) => void;

/**
* Sets an element in an `Int32Array`.
*
Expand Down Expand Up @@ -347,6 +365,31 @@ interface Float32AccessorObject extends OutputObject {
accessors: [ GetFloat32, SetFloat32 ];
}

/**
* Interface describing the output object for a `Float16Array`.
*/
interface Float16AccessorObject extends OutputObject {
/**
* Reference to the original array-like object.
*/
data: Float16Array;

/**
* Data type.
*/
dtype: 'float16';

/**
* Boolean indicating whether the provided array-like object supports the get/set protocol (i.e., uses accessors for getting and setting elements).
*/
accessorProtocol: false;

/**
* Two-element array whose first element is an accessor for retrieving an array element and whose second element is an accessor for setting an array element.
*/
accessors: [ GetFloat16, SetFloat16 ];
}

/**
* Interface describing the output object for an `Int32Array`.
*/
Expand Down Expand Up @@ -683,7 +726,7 @@ interface GetSetAccessorObject<T> extends OutputObject {
* var fcns = obj.accessors;
* // returns [ <Function>, <Function> ]
*
* var v = fcns[ 0 ]( x.data, 2 );
* var v = fcns[ 0 ]( x, 2 );
* // returns 3
*/
declare function strided2object( N: number, x: Float64Array, stride: number, offset: number ): Float64AccessorObject;
Expand Down Expand Up @@ -724,11 +767,52 @@ declare function strided2object( N: number, x: Float64Array, stride: number, off
* var fcns = obj.accessors;
* // returns [ <Function>, <Function> ]
*
* var v = fcns[ 0 ]( x.data, 2 );
* var v = fcns[ 0 ]( x, 2 );
* // returns 3
*/
declare function strided2object( N: number, x: Float32Array, stride: number, offset: number ): Float32AccessorObject;

/**
* Converts a strided array and associated metadata to an object likely to have the same "shape".
*
* ## Notes
*
* - This function is intended as a potential performance optimization. In V8, for example, even if two objects share common properties, if those properties were added in different orders or if one object has additional properties not shared by the other object, then those objects will have different "hidden" classes. If a function is provided many objects having different "shapes", some JavaScript VMs (e.g., V8) will consider the function "megamorphic" and fail to perform various runtime optimizations. Accordingly, the intent of this function is to standardize the "shape" of the object holding strided array metadata to ensure that internal functions operating on strided arrays are provided consistent argument "shapes".
*
* - The returned object has the following properties:
*
* - **data**: reference to the input array.
* - **dtype**: array data type.
* - **length**: number of indexed elements.
* - **stride**: index increment.
* - **offset**: starting index.
* - **accessorProtocol**: boolean indicating whether the input array uses accessors for getting and setting elements.
* - **accessors**: a two-element array whose first element is an accessor for retrieving an array element and whose second element is an accessor for setting an array element.
*
* @param N - number of indexed elements
* @param x - input array
* @param stride - index increment
* @param offset - index offset
* @returns object containing strided array data
*
* @example
* var Float16Array = require( '@stdlib/array/float16' );
*
* var x = new Float16Array( [ 1, 2, 3, 4 ] );
* var obj = strided2object( 4, x, 1, 0 );
* // returns {...}
*
* var bool = obj.accessorProtocol;
* // returns false
*
* var fcns = obj.accessors;
* // returns [ <Function>, <Function> ]
*
* var v = fcns[ 0 ]( x, 2 );
* // returns 3
*/
declare function strided2object( N: number, x: Float16Array, stride: number, offset: number ): Float16AccessorObject;

/**
* Converts a strided array and associated metadata to an object likely to have the same "shape".
*
Expand Down Expand Up @@ -765,7 +849,7 @@ declare function strided2object( N: number, x: Float32Array, stride: number, off
* var fcns = obj.accessors;
* // returns [ <Function>, <Function> ]
*
* var v = fcns[ 0 ]( x.data, 2 );
* var v = fcns[ 0 ]( x, 2 );
* // returns 3
*/
declare function strided2object( N: number, x: Int32Array, stride: number, offset: number ): Int32AccessorObject;
Expand Down Expand Up @@ -806,7 +890,7 @@ declare function strided2object( N: number, x: Int32Array, stride: number, offse
* var fcns = obj.accessors;
* // returns [ <Function>, <Function> ]
*
* var v = fcns[ 0 ]( x.data, 2 );
* var v = fcns[ 0 ]( x, 2 );
* // returns 3
*/
declare function strided2object( N: number, x: Int16Array, stride: number, offset: number ): Int16AccessorObject;
Expand Down Expand Up @@ -847,7 +931,7 @@ declare function strided2object( N: number, x: Int16Array, stride: number, offse
* var fcns = obj.accessors;
* // returns [ <Function>, <Function> ]
*
* var v = fcns[ 0 ]( x.data, 2 );
* var v = fcns[ 0 ]( x, 2 );
* // returns 3
*/
declare function strided2object( N: number, x: Int8Array, stride: number, offset: number ): Int8AccessorObject;
Expand Down Expand Up @@ -888,7 +972,7 @@ declare function strided2object( N: number, x: Int8Array, stride: number, offset
* var fcns = obj.accessors;
* // returns [ <Function>, <Function> ]
*
* var v = fcns[ 0 ]( x.data, 2 );
* var v = fcns[ 0 ]( x, 2 );
* // returns 3
*/
declare function strided2object( N: number, x: Uint32Array, stride: number, offset: number ): Uint32AccessorObject;
Expand Down Expand Up @@ -929,7 +1013,7 @@ declare function strided2object( N: number, x: Uint32Array, stride: number, offs
* var fcns = obj.accessors;
* // returns [ <Function>, <Function> ]
*
* var v = fcns[ 0 ]( x.data, 2 );
* var v = fcns[ 0 ]( x, 2 );
* // returns 3
*/
declare function strided2object( N: number, x: Uint16Array, stride: number, offset: number ): Uint16AccessorObject;
Expand Down Expand Up @@ -970,7 +1054,7 @@ declare function strided2object( N: number, x: Uint16Array, stride: number, offs
* var fcns = obj.accessors;
* // returns [ <Function>, <Function> ]
*
* var v = fcns[ 0 ]( x.data, 2 );
* var v = fcns[ 0 ]( x, 2 );
* // returns 3
*/
declare function strided2object( N: number, x: Uint8Array, stride: number, offset: number ): Uint8AccessorObject;
Expand Down Expand Up @@ -1011,7 +1095,7 @@ declare function strided2object( N: number, x: Uint8Array, stride: number, offse
* var fcns = obj.accessors;
* // returns [ <Function>, <Function> ]
*
* var v = fcns[ 0 ]( x.data, 2 );
* var v = fcns[ 0 ]( x, 2 );
* // returns 3
*/
declare function strided2object( N: number, x: Uint8ClampedArray, stride: number, offset: number ): Uint8cAccessorObject;
Expand Down Expand Up @@ -1144,7 +1228,7 @@ declare function strided2object( N: number, x: Complex64Array, stride: number, o
* var fcns = obj.accessors;
* // returns [ <Function>, <Function> ]
*
* var v = fcns[ 0 ]( x.data, 2 );
* var v = fcns[ 0 ]( x, 2 );
* // returns 3
*/
declare function strided2object<T = unknown>( N: number, x: AccessorArrayLike<T>, stride: number, offset: number ): GetSetAccessorObject<T>;
Expand Down Expand Up @@ -1183,7 +1267,7 @@ declare function strided2object<T = unknown>( N: number, x: AccessorArrayLike<T>
* var fcns = obj.accessors;
* // returns [ <Function>, <Function> ]
*
* var v = fcns[ 0 ]( x.data, 2 );
* var v = fcns[ 0 ]( x, 2 );
* // returns 3
*/
declare function strided2object<T = unknown>( N: number, x: Array<T>, stride: number, offset: number ): GenericAccessorObject<T>;
Expand Down Expand Up @@ -1228,7 +1312,7 @@ declare function strided2object<T = unknown>( N: number, x: Array<T>, stride: nu
* var fcns = obj.accessors;
* // returns [ <Function>, <Function> ]
*
* var v = fcns[ 0 ]( x.data, 2 );
* var v = fcns[ 0 ]( x, 2 );
* // returns 3
*/
declare function strided2object<T = unknown>( N: number, x: Collection<T>, stride: number, offset: number ): IndexedAccessorObject<T>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import { AccessorArrayLike, ArrayLike } from '@stdlib/types/array';
import Complex128Array = require( '@stdlib/array/complex128' );
import Complex64Array = require( '@stdlib/array/complex64' );
import Float16Array = require( '@stdlib/array/float16' );
import strided2object = require( './index' );

/**
Expand Down Expand Up @@ -79,38 +80,41 @@ function arrayLike(): ArrayLike<number> {
const x3 = new Float32Array( [ 1, 2, 3, 4, 5, 6 ] );
strided2object( 6, x3, 1, 0 ); // $ExpectType Float32AccessorObject

const x4 = new Int32Array( [ 1, 2, 3, 4, 5, 6 ] );
strided2object( 6, x4, 1, 0 ); // $ExpectType Int32AccessorObject
const x4 = new Float16Array( [ 1, 2, 3, 4, 5, 6 ] );
strided2object( 6, x4, 1, 0 ); // $ExpectType Float16AccessorObject

const x5 = new Int16Array( [ 1, 2, 3, 4, 5, 6 ] );
strided2object( 6, x5, 1, 0 ); // $ExpectType Int16AccessorObject
const x5 = new Int32Array( [ 1, 2, 3, 4, 5, 6 ] );
strided2object( 6, x5, 1, 0 ); // $ExpectType Int32AccessorObject

const x6 = new Int8Array( [ 1, 2, 3, 4, 5, 6 ] );
strided2object( 6, x6, 1, 0 ); // $ExpectType Int8AccessorObject
const x6 = new Int16Array( [ 1, 2, 3, 4, 5, 6 ] );
strided2object( 6, x6, 1, 0 ); // $ExpectType Int16AccessorObject

const x7 = new Uint32Array( [ 1, 2, 3, 4, 5, 6 ] );
strided2object( 6, x7, 1, 0 ); // $ExpectType Uint32AccessorObject
const x7 = new Int8Array( [ 1, 2, 3, 4, 5, 6 ] );
strided2object( 6, x7, 1, 0 ); // $ExpectType Int8AccessorObject

const x8 = new Uint16Array( [ 1, 2, 3, 4, 5, 6 ] );
strided2object( 6, x8, 1, 0 ); // $ExpectType Uint16AccessorObject
const x8 = new Uint32Array( [ 1, 2, 3, 4, 5, 6 ] );
strided2object( 6, x8, 1, 0 ); // $ExpectType Uint32AccessorObject

const x9 = new Uint8Array( [ 1, 2, 3, 4, 5, 6 ] );
strided2object( 6, x9, 1, 0 ); // $ExpectType Uint8AccessorObject
const x9 = new Uint16Array( [ 1, 2, 3, 4, 5, 6 ] );
strided2object( 6, x9, 1, 0 ); // $ExpectType Uint16AccessorObject

const x10 = new Uint8ClampedArray( [ 1, 2, 3, 4, 5, 6 ] );
strided2object( 6, x10, 1, 0 ); // $ExpectType Uint8cAccessorObject
const x10 = new Uint8Array( [ 1, 2, 3, 4, 5, 6 ] );
strided2object( 6, x10, 1, 0 ); // $ExpectType Uint8AccessorObject

const x11 = new Complex128Array( [ 1, 2, 3, 4, 5, 6 ] );
strided2object( 6, x11, 1, 0 ); // $ExpectType Complex128AccessorObject
const x11 = new Uint8ClampedArray( [ 1, 2, 3, 4, 5, 6 ] );
strided2object( 6, x11, 1, 0 ); // $ExpectType Uint8cAccessorObject

const x12 = new Complex64Array( [ 1, 2, 3, 4, 5, 6 ] );
strided2object( 6, x12, 1, 0 ); // $ExpectType Complex64AccessorObject
const x12 = new Complex128Array( [ 1, 2, 3, 4, 5, 6 ] );
strided2object( 6, x12, 1, 0 ); // $ExpectType Complex128AccessorObject

const x13 = accessorArray();
strided2object( 6, x13, 1, 0 ); // $ExpectType GetSetAccessorObject<number>
const x13 = new Complex64Array( [ 1, 2, 3, 4, 5, 6 ] );
strided2object( 6, x13, 1, 0 ); // $ExpectType Complex64AccessorObject

const x14 = arrayLike();
strided2object( 6, x14, 1, 0 ); // $ExpectType IndexedAccessorObject<number>
const x14 = accessorArray();
strided2object( 6, x14, 1, 0 ); // $ExpectType GetSetAccessorObject<number>

const x15 = arrayLike();
strided2object( 6, x15, 1, 0 ); // $ExpectType IndexedAccessorObject<number>
}

// The compiler throws an error if the function is provided a first argument which is not a number...
Expand Down
Loading