From 4e48d21dfdc4ea30bb80c24b07adc0f8db5f0123 Mon Sep 17 00:00:00 2001 From: Uday Kakade Date: Sat, 21 Jun 2025 13:03:11 +0530 Subject: [PATCH 01/45] feat: add array/float16 --- .../@stdlib/array/float16/README.md | 1489 +++++++++++++++++ .../benchmark/benchmark.copy_within.js | 50 + .../benchmark/benchmark.copy_within.length.js | 93 + .../array/float16/benchmark/benchmark.data.js | 75 + .../float16/benchmark/benchmark.entries.js | 50 + .../float16/benchmark/benchmark.every.js | 81 + .../benchmark/benchmark.every.length.js | 105 ++ .../array/float16/benchmark/benchmark.fill.js | 51 + .../benchmark/benchmark.fill.length.js | 94 ++ .../float16/benchmark/benchmark.filter.js | 81 + .../benchmark/benchmark.filter.length.js | 105 ++ .../array/float16/benchmark/benchmark.find.js | 82 + .../benchmark/benchmark.find.length.js | 105 ++ .../float16/benchmark/benchmark.find_index.js | 82 + .../benchmark/benchmark.find_index.length.js | 104 ++ .../float16/benchmark/benchmark.for_each.js | 88 + .../benchmark/benchmark.for_each.length.js | 106 ++ .../array/float16/benchmark/benchmark.from.js | 237 +++ .../float16/benchmark/benchmark.includes.js | 53 + .../benchmark/benchmark.includes.length.js | 96 ++ .../float16/benchmark/benchmark.index_of.js | 53 + .../benchmark/benchmark.index_of.length.js | 95 ++ .../array/float16/benchmark/benchmark.join.js | 51 + .../benchmark/benchmark.join.length.js | 94 ++ .../array/float16/benchmark/benchmark.js | 49 + .../array/float16/benchmark/benchmark.keys.js | 50 + .../benchmark/benchmark.last_index_of.js | 53 + .../benchmark.last_index_of.length.js | 95 ++ .../float16/benchmark/benchmark.length.js | 93 + .../array/float16/benchmark/benchmark.map.js | 81 + .../float16/benchmark/benchmark.map.length.js | 105 ++ .../array/float16/benchmark/benchmark.of.js | 48 + .../float16/benchmark/benchmark.properties.js | 145 ++ .../float16/benchmark/benchmark.reduce.js | 80 + .../benchmark/benchmark.reduce.length.js | 105 ++ .../benchmark/benchmark.reduce_right.js | 80 + .../benchmark.reduce_right.length.js | 105 ++ .../float16/benchmark/benchmark.reverse.js | 52 + .../benchmark/benchmark.reverse.length.js | 95 ++ .../array/float16/benchmark/benchmark.set.js | 94 ++ .../float16/benchmark/benchmark.set.length.js | 114 ++ .../float16/benchmark/benchmark.slice.js | 52 + .../benchmark/benchmark.slice.length.js | 95 ++ .../array/float16/benchmark/benchmark.some.js | 81 + .../benchmark/benchmark.some.length.js | 105 ++ .../array/float16/benchmark/benchmark.sort.js | 53 + .../benchmark/benchmark.sort.length.js | 105 ++ .../float16/benchmark/benchmark.subarray.js | 51 + .../benchmark/benchmark.subarray.length.js | 94 ++ .../benchmark/benchmark.to_locale_string.js | 51 + .../benchmark.to_locale_string.length.js | 94 ++ .../float16/benchmark/benchmark.to_string.js | 51 + .../benchmark/benchmark.to_string.length.js | 94 ++ .../float16/benchmark/benchmark.values.js | 50 + .../@stdlib/array/float16/docs/repl.txt | 959 +++++++++++ .../array/float16/docs/types/index.d.ts | 26 + .../@stdlib/array/float16/docs/types/test.ts | 36 + .../@stdlib/array/float16/examples/index.js | 31 + .../@stdlib/array/float16/lib/index.js | 52 + .../@stdlib/array/float16/lib/main.js | 28 + .../@stdlib/array/float16/lib/polyfill.js | 538 ++++++ .../@stdlib/array/float16/package.json | 69 + .../@stdlib/array/float16/test/test.js | 80 + .../array/float16/test/test.polyfill.js | 44 + 64 files changed, 7803 insertions(+) create mode 100644 lib/node_modules/@stdlib/array/float16/README.md create mode 100644 lib/node_modules/@stdlib/array/float16/benchmark/benchmark.copy_within.js create mode 100644 lib/node_modules/@stdlib/array/float16/benchmark/benchmark.copy_within.length.js create mode 100644 lib/node_modules/@stdlib/array/float16/benchmark/benchmark.data.js create mode 100644 lib/node_modules/@stdlib/array/float16/benchmark/benchmark.entries.js create mode 100644 lib/node_modules/@stdlib/array/float16/benchmark/benchmark.every.js create mode 100644 lib/node_modules/@stdlib/array/float16/benchmark/benchmark.every.length.js create mode 100644 lib/node_modules/@stdlib/array/float16/benchmark/benchmark.fill.js create mode 100644 lib/node_modules/@stdlib/array/float16/benchmark/benchmark.fill.length.js create mode 100644 lib/node_modules/@stdlib/array/float16/benchmark/benchmark.filter.js create mode 100644 lib/node_modules/@stdlib/array/float16/benchmark/benchmark.filter.length.js create mode 100644 lib/node_modules/@stdlib/array/float16/benchmark/benchmark.find.js create mode 100644 lib/node_modules/@stdlib/array/float16/benchmark/benchmark.find.length.js create mode 100644 lib/node_modules/@stdlib/array/float16/benchmark/benchmark.find_index.js create mode 100644 lib/node_modules/@stdlib/array/float16/benchmark/benchmark.find_index.length.js create mode 100644 lib/node_modules/@stdlib/array/float16/benchmark/benchmark.for_each.js create mode 100644 lib/node_modules/@stdlib/array/float16/benchmark/benchmark.for_each.length.js create mode 100644 lib/node_modules/@stdlib/array/float16/benchmark/benchmark.from.js create mode 100644 lib/node_modules/@stdlib/array/float16/benchmark/benchmark.includes.js create mode 100644 lib/node_modules/@stdlib/array/float16/benchmark/benchmark.includes.length.js create mode 100644 lib/node_modules/@stdlib/array/float16/benchmark/benchmark.index_of.js create mode 100644 lib/node_modules/@stdlib/array/float16/benchmark/benchmark.index_of.length.js create mode 100644 lib/node_modules/@stdlib/array/float16/benchmark/benchmark.join.js create mode 100644 lib/node_modules/@stdlib/array/float16/benchmark/benchmark.join.length.js create mode 100644 lib/node_modules/@stdlib/array/float16/benchmark/benchmark.js create mode 100644 lib/node_modules/@stdlib/array/float16/benchmark/benchmark.keys.js create mode 100644 lib/node_modules/@stdlib/array/float16/benchmark/benchmark.last_index_of.js create mode 100644 lib/node_modules/@stdlib/array/float16/benchmark/benchmark.last_index_of.length.js create mode 100644 lib/node_modules/@stdlib/array/float16/benchmark/benchmark.length.js create mode 100644 lib/node_modules/@stdlib/array/float16/benchmark/benchmark.map.js create mode 100644 lib/node_modules/@stdlib/array/float16/benchmark/benchmark.map.length.js create mode 100644 lib/node_modules/@stdlib/array/float16/benchmark/benchmark.of.js create mode 100644 lib/node_modules/@stdlib/array/float16/benchmark/benchmark.properties.js create mode 100644 lib/node_modules/@stdlib/array/float16/benchmark/benchmark.reduce.js create mode 100644 lib/node_modules/@stdlib/array/float16/benchmark/benchmark.reduce.length.js create mode 100644 lib/node_modules/@stdlib/array/float16/benchmark/benchmark.reduce_right.js create mode 100644 lib/node_modules/@stdlib/array/float16/benchmark/benchmark.reduce_right.length.js create mode 100644 lib/node_modules/@stdlib/array/float16/benchmark/benchmark.reverse.js create mode 100644 lib/node_modules/@stdlib/array/float16/benchmark/benchmark.reverse.length.js create mode 100644 lib/node_modules/@stdlib/array/float16/benchmark/benchmark.set.js create mode 100644 lib/node_modules/@stdlib/array/float16/benchmark/benchmark.set.length.js create mode 100644 lib/node_modules/@stdlib/array/float16/benchmark/benchmark.slice.js create mode 100644 lib/node_modules/@stdlib/array/float16/benchmark/benchmark.slice.length.js create mode 100644 lib/node_modules/@stdlib/array/float16/benchmark/benchmark.some.js create mode 100644 lib/node_modules/@stdlib/array/float16/benchmark/benchmark.some.length.js create mode 100644 lib/node_modules/@stdlib/array/float16/benchmark/benchmark.sort.js create mode 100644 lib/node_modules/@stdlib/array/float16/benchmark/benchmark.sort.length.js create mode 100644 lib/node_modules/@stdlib/array/float16/benchmark/benchmark.subarray.js create mode 100644 lib/node_modules/@stdlib/array/float16/benchmark/benchmark.subarray.length.js create mode 100644 lib/node_modules/@stdlib/array/float16/benchmark/benchmark.to_locale_string.js create mode 100644 lib/node_modules/@stdlib/array/float16/benchmark/benchmark.to_locale_string.length.js create mode 100644 lib/node_modules/@stdlib/array/float16/benchmark/benchmark.to_string.js create mode 100644 lib/node_modules/@stdlib/array/float16/benchmark/benchmark.to_string.length.js create mode 100644 lib/node_modules/@stdlib/array/float16/benchmark/benchmark.values.js create mode 100644 lib/node_modules/@stdlib/array/float16/docs/repl.txt create mode 100644 lib/node_modules/@stdlib/array/float16/docs/types/index.d.ts create mode 100644 lib/node_modules/@stdlib/array/float16/docs/types/test.ts create mode 100644 lib/node_modules/@stdlib/array/float16/examples/index.js create mode 100644 lib/node_modules/@stdlib/array/float16/lib/index.js create mode 100644 lib/node_modules/@stdlib/array/float16/lib/main.js create mode 100644 lib/node_modules/@stdlib/array/float16/lib/polyfill.js create mode 100644 lib/node_modules/@stdlib/array/float16/package.json create mode 100644 lib/node_modules/@stdlib/array/float16/test/test.js create mode 100644 lib/node_modules/@stdlib/array/float16/test/test.polyfill.js diff --git a/lib/node_modules/@stdlib/array/float16/README.md b/lib/node_modules/@stdlib/array/float16/README.md new file mode 100644 index 000000000000..7f1bb63c6dae --- /dev/null +++ b/lib/node_modules/@stdlib/array/float16/README.md @@ -0,0 +1,1489 @@ + + +# Float16Array + +> [Typed array][mdn-typed-array] constructor which returns a [typed array][mdn-typed-array] representing an array of half-precision (16-bit) floating-point numbers in the platform byte order. + + + +
+ +
+ + + + + +
+ +## Usage + +```javascript +var Float16Array = require( '@stdlib/array/float16' ); + + +#### Float16Array() + +A [typed array][mdn-typed-array] constructor which returns a [typed array][mdn-typed-array] representing an array of half-precision floating-point numbers in the platform byte order. + + + +```javascript +var arr = new Float16Array(); +// returns +``` + +#### Float16Array( length ) + +Returns a [typed array][mdn-typed-array] having a specified length. + + + +```javascript +var arr = new Float16Array( 5 ); +// returns [ 0.0, 0.0, 0.0, 0.0, 0.0 ] +``` + +#### Float16Array( typedarray ) + +Creates a [typed array][mdn-typed-array] from another [typed array][mdn-typed-array]. + + + +```javascript +var Float64Array = require( '@stdlib/array/float64' ); + +var arr1 = new Float64Array( [ 0.5, 0.5, 0.5 ] ); +var arr2 = new Float16Array( arr1 ); +// returns [ 0.5, 0.5, 0.5 ] +``` + +#### Float16Array( obj ) + +Creates a [typed array][mdn-typed-array] from an array-like `object` or iterable. + + + +```javascript +var arr = new Float16Array( [ 0.5, 0.5, 0.5 ] ); +// returns [ 0.5, 0.5, 0.5 ] +``` + +#### Float16Array( buffer\[, byteOffset\[, length]] ) + +Returns a [typed array][mdn-typed-array] view of an [`ArrayBuffer`][@stdlib/array/buffer]. + + + +```javascript +var ArrayBuffer = require( '@stdlib/array/buffer' ); + +var buf = new ArrayBuffer( 16 ); +var arr = new Float16Array( buf, 0, 4 ); +// returns [ 0.0, 0.0, 0.0, 0.0 ] +``` + +* * * + +### Properties + + + +#### Float16Array.BYTES_PER_ELEMENT + +Number of bytes per view element. + + + +```javascript +var nbytes = Float16Array.BYTES_PER_ELEMENT; +// returns 2 +``` + + + +#### Float16Array.name + +[Typed array][mdn-typed-array] constructor name. + + + +```javascript +var str = Float16Array.name; +// returns 'Float16Array' +``` + + + +#### Float16Array.prototype.buffer + +**Read-only** property which returns the [`ArrayBuffer`][@stdlib/array/buffer] referenced by the [typed array][mdn-typed-array]. + + + +```javascript +var arr = new Float16Array( 5 ); +var buf = arr.buffer; +// returns +``` + + + +#### Float16Array.prototype.byteLength + +**Read-only** property which returns the length (in bytes) of the [typed array][mdn-typed-array]. + + + +```javascript +var arr = new Float16Array( 5 ); +var byteLength = arr.byteLength; +// returns 10 +``` + + + +#### Float16Array.prototype.byteOffset + +**Read-only** property which returns the offset (in bytes) of the [typed array][mdn-typed-array] from the start of its [`ArrayBuffer`][@stdlib/array/buffer]. + + + +```javascript +var arr = new Float16Array( 5 ); +var byteOffset = arr.byteOffset; +// returns 0 +``` + + + +#### Float16Array.prototype.BYTES_PER_ELEMENT + +Number of bytes per view element. + + + +```javascript +var arr = new Float16Array( 5 ); +var nbytes = arr.BYTES_PER_ELEMENT; +// returns 2 +``` + + + +#### Float16Array.prototype.length + +**Read-only** property which returns the number of view elements. + + + +```javascript +var arr = new Float16Array( 5 ); +var len = arr.length; +// returns 5 +``` + +* * * + +### Methods + + + +#### Float16Array.from( src\[, map\[, thisArg]] ) + +Creates a new typed array from an array-like `object` or an iterable. + + + +```javascript +var arr = Float16Array.from( [ 1.0, 2.0 ] ); +// returns [ 1.0, 2.0 ] +``` + +To invoke a function for each `src` value, provide a callback function. + + + +```javascript +function mapFcn( v ) { + return v * 2.0; +} + +var arr = Float16Array.from( [ 1.0, 2.0 ], mapFcn ); +// returns [ 2.0, 4.0 ] +``` + +A callback function is provided two arguments: + +- `value`: source value. +- `index`: source index. + +To set the callback execution context, provide a `thisArg`. + + + +```javascript +function mapFcn( v ) { + this.count += 1; + return v * 2.0; +} + +var ctx = { + 'count': 0 +}; + +var arr = Float16Array.from( [ 1.0, 2.0 ], mapFcn, ctx ); +// returns [ 2.0, 4.0 ] + +var n = ctx.count; +// returns 2 +``` + + + +#### Float16Array.of( element0\[, element1\[, ...elementN]] ) + +Creates a new typed array from a variable number of arguments. + + + +```javascript +var arr = Float16Array.of( 1.0, 2.0 ); +// returns [ 1.0, 2.0 ] +``` + + + +#### Float16Array.prototype.copyWithin( target, start\[, end] ) + +Copies a sequence of elements within an array starting at `start` and ending at `end` (non-inclusive) to the position starting at `target`. + + + +```javascript +var arr = new Float16Array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] ); + +// Copy the last two elements to the first two elements: +arr.copyWithin( 0, 3 ); + +var v = arr[ 0 ]; +// returns 4.0 + +v = arr[ 1 ]; +// returns 5.0 +``` + +By default, `end` equals the number of array elements (i.e., one more than the last array index). To limit the sequence length, provide an `end` argument. + + + +```javascript +var arr = new Float16Array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] ); + +// Copy the first two elements to the last two elements: +arr.copyWithin( 3, 0, 2 ); + +var v = arr[ 3 ]; +// returns 1.0 + +v = arr[ 4 ]; +// returns 2.0 +``` + +When a `target`, `start`, and/or `end` index is negative, the respective index is determined relative to the last array element. The following example achieves the same behavior as the previous example: + + + +```javascript +var arr = new Float16Array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] ); + +// Copy the first two elements to the last two elements: +arr.copyWithin( -2, -5, -3 ); + +var v = arr[ 3 ]; +// returns 1.0 + +v = arr[ 4 ]; +// returns 2.0 +``` + + + +#### Float16Array.prototype.entries() + +Returns an iterator for iterating over array key-value pairs. + + + +```javascript +var arr = new Float16Array( [ 1.0, 2.0 ] ); + +// Create an iterator: +var it = arr.entries(); + +// Iterate over key-value pairs... +var v = it.next().value; +// returns [ 0, 1.0 ] + +v = it.next().value; +// returns [ 1, 2.0 ] + +var bool = it.next().done; +// returns true +``` + + + +#### Float16Array.prototype.every( predicate\[, thisArg] ) + +Tests whether all array elements pass a test implemented by a `predicate` function. + + + +```javascript +function predicate( v ) { + return ( v <= 1.0 ); +} + +var arr = new Float16Array( [ 1.0, 2.0 ] ); + +var bool = arr.every( predicate ); +// returns false +``` + +A `predicate` function is provided three arguments: + +- `value`: array element. +- `index`: array index. +- `arr`: array on which the method is invoked. + +To set the callback execution context, provide a `thisArg`. + + + +```javascript +function predicate( v ) { + this.count += 1; + return ( v >= 1.0 ); +} + +var ctx = { + 'count': 0 +}; + +var arr = new Float16Array( [ 1.0, 2.0 ] ); + +var bool = arr.every( predicate, ctx ); +// returns true + +var n = ctx.count; +// returns 2 +``` + + + +#### Float16Array.prototype.fill( value\[, start\[, end]] ) + +Fills an array from a `start` index to an `end` index (non-inclusive) with a provided `value`. + + + +```javascript +var arr = new Float16Array( 2 ); + +// Set all array elements to the same value: +arr.fill( 2.0 ); + +var v = arr[ 0 ]; +// returns 2.0 + +v = arr[ 1 ]; +// returns 2.0 + +// Set all array elements starting from the first index to the same value: +arr.fill( 3.0, 1 ); + +v = arr[ 0 ]; +// returns 2.0 + +v = arr[ 1 ]; +// returns 3.0 + +// Set all array elements, except the last element, to the same value: +arr.fill( 4.0, 0, arr.length-1 ); + +v = arr[ 0 ]; +// returns 4.0 + +v = arr[ 1 ]; +// returns 3.0 +``` + +When a `start` and/or `end` index is negative, the respective index is determined relative to the last array element. + + + +```javascript +var arr = new Float16Array( 2 ); + +// Set all array elements, except the last element, to the same value: +arr.fill( 2.0, -arr.length, -1 ); + +var v = arr[ 0 ]; +// returns 2.0 + +v = arr[ 1 ]; +// returns 0.0 +``` + + + +#### Float16Array.prototype.filter( predicate\[, thisArg] ) + +Creates a new array (of the same data type as the host array) which includes those elements for which a `predicate` function returns a truthy value. + + + +```javascript +function predicate( v ) { + return ( v >= 2.0 ); +} + +var arr1 = new Float16Array( [ 1.0, 2.0, 3.0 ] ); + +var arr2 = arr1.filter( predicate ); +// returns [ 2.0, 3.0 ] +``` + +If a `predicate` function does not return a truthy value for any array element, the method returns an empty array. + + + +```javascript +function predicate( v ) { + return ( v >= 10.0 ); +} + +var arr1 = new Float16Array( [ 1.0, 2.0, 3.0 ] ); + +var arr2 = arr1.filter( predicate ); +// returns [] +``` + +A `predicate` function is provided three arguments: + +- `value`: array element. +- `index`: array index. +- `arr`: array on which the method is invoked. + +To set the callback execution context, provide a `thisArg`. + + + +```javascript +function predicate( v ) { + this.count += 1; + return ( v >= 2.0 ); +} + +var ctx = { + 'count': 0 +}; + +var arr1 = new Float16Array( [ 1.0, 2.0, 3.0 ] ); + +var arr2 = arr1.filter( predicate, ctx ); + +var n = ctx.count; +// returns 3 +``` + + + +#### Float16Array.prototype.find( predicate\[, thisArg] ) + +Returns the first array element for which a provided `predicate` function returns a truthy value. + + + +```javascript +function predicate( v ) { + return ( v > 2.0 ); +} + +var arr = new Float16Array( [ 1.0, 2.0, 3.0 ] ); + +var v = arr.find( predicate ); +// returns 3.0 +``` + +If a `predicate` function does not return a truthy value for any array element, the method returns `undefined`. + + + +```javascript +function predicate( v ) { + return ( v < 1.0 ); +} + +var arr = new Float16Array( [ 1.0, 2.0, 3.0 ] ); + +var v = arr.find( predicate ); +// returns undefined +``` + +A `predicate` function is provided three arguments: + +- `value`: array element. +- `index`: array index. +- `arr`: array on which the method is invoked. + +To set the callback execution context, provide a `thisArg`. + + + +```javascript +function predicate( v ) { + this.count += 1; + return ( v > 2.0 ); +} + +var ctx = { + 'count': 0 +}; + +var arr = new Float16Array( [ 1.0, 2.0, 3.0 ] ); + +var v = arr.find( predicate, ctx ); +// returns 3.0 + +var n = ctx.count; +// returns 3 +``` + + + +#### Float16Array.prototype.findIndex( predicate\[, thisArg] ) + +Returns the index of the first array element for which a provided `predicate` function returns a truthy value. + + + +```javascript +function predicate( v ) { + return ( v >= 3.0 ); +} + +var arr = new Float16Array( [ 1.0, 2.0, 3.0 ] ); + +var idx = arr.findIndex( predicate ); +// returns 2 +``` + +If a `predicate` function does not return a truthy value for any array element, the method returns `-1`. + + + +```javascript +function predicate( v ) { + return ( v < 1.0 ); +} + +var arr = new Float16Array( [ 1.0, 2.0, 3.0 ] ); + +var idx = arr.findIndex( predicate ); +// returns -1 +``` + +A `predicate` function is provided three arguments: + +- `value`: array element. +- `index`: array index. +- `arr`: array on which the method is invoked. + +To set the callback execution context, provide a `thisArg`. + + + +```javascript +function predicate( v ) { + this.count += 1; + return ( v >= 3.0 ); +} + +var ctx = { + 'count': 0 +}; + +var arr = new Float16Array( [ 1.0, 2.0, 3.0 ] ); + +var idx = arr.findIndex( predicate, ctx ); +// returns 2 + +var n = ctx.count; +// returns 3 +``` + + + +#### Float16Array.prototype.forEach( fcn\[, thisArg] ) + +Invokes a callback for each array element. + + + +```javascript +var arr = new Float16Array( [ 1.0, 2.0, 3.0 ] ); + +var str = ''; + +function fcn( v, i ) { + str += i + ':' + v; + if ( i < arr.length-1 ) { + str += ' '; + } +} + +arr.forEach( fcn ); + +console.log( str ); +// => '0:1 1:2 2:3' +``` + +The callback is provided three arguments: + +- `value`: array element. +- `index`: array index. +- `arr`: array on which the method is invoked. + +To set the callback execution context, provide a `thisArg`. + + + +```javascript +function fcn() { + this.count += 1; +} + +var ctx = { + 'count': 0 +}; + +var arr = new Float16Array( [ 1.0, 2.0, 3.0 ] ); + +arr.forEach( fcn, ctx ); + +var n = ctx.count; +// returns 3 +``` + + + +#### Float16Array.prototype.includes( searchElement\[, fromIndex] ) + +Returns a `boolean` indicating whether an array includes a search element. + + + +```javascript +var arr = new Float16Array( [ 1.0, 2.0, 3.0 ] ); + +var bool = arr.includes( 3.0 ); +// returns true + +bool = arr.includes( 0.0 ); +// returns false +``` + +By default, the method searches the entire array (`fromIndex = 0`). To begin searching from a specific array index, provide a `fromIndex`. + + + +```javascript +var arr = new Float16Array( [ 1.0, 2.0, 3.0 ] ); + +var bool = arr.includes( 1.0, 1 ); +// returns false +``` + +When a `fromIndex` is negative, the starting index is resolved relative to the last array element. + + + +```javascript +var arr = new Float16Array( [ 1.0, 2.0, 3.0 ] ); + +var bool = arr.includes( 1.0, -2 ); +// returns false +``` + +The method does **not** distinguish between signed and unsigned zero. + + + +#### Float16Array.prototype.indexOf( searchElement\[, fromIndex] ) + +Returns the index of the first array element strictly equal to a search element. + + + +```javascript +var arr = new Float16Array( [ 1.0, 2.0, 3.0 ] ); + +var idx = arr.indexOf( 3.0 ); +// returns 2 + +idx = arr.indexOf( 0.0 ); +// returns -1 +``` + +By default, the method searches the entire array (`fromIndex = 0`). To begin searching from a specific array index, provide a `fromIndex`. + + + +```javascript +var arr = new Float16Array( [ 1.0, 2.0, 3.0 ] ); + +var idx = arr.indexOf( 1.0, 1 ); +// returns -1 +``` + +When a `fromIndex` is negative, the starting index is resolved relative to the last array element. + + + +```javascript +var arr = new Float16Array( [ 1.0, 2.0, 3.0 ] ); + +var idx = arr.indexOf( 1.0, -2 ); +// returns -1 +``` + +The method does **not** distinguish between signed and unsigned zero. + + + +#### Float16Array.prototype.join( \[separator] ) + +Serializes an array by joining all array elements as a string. + + + +```javascript +var arr = new Float16Array( [ 1.0, 2.0, 3.0 ] ); + +var str = arr.join(); +// returns '1,2,3' +``` + +By default, the method delineates array elements using a comma `,`. To specify a custom separator, provide a `separator` string. + + + +```javascript +var arr = new Float16Array( [ 1.0, 2.0, 3.0 ] ); + +var str = arr.join( '|' ); +// returns '1|2|3' +``` + + + +#### Float16Array.prototype.keys() + +Returns an iterator for iterating over array keys. + + + +```javascript +var arr = new Float16Array( [ 1.0, 2.0 ] ); + +// Create an iterator: +var it = arr.keys(); + +// Iterate over keys... +var v = it.next().value; +// returns 0 + +v = it.next().value; +// returns 1 + +var bool = it.next().done; +// returns true +``` + + + +#### Float16Array.prototype.lastIndexOf( searchElement\[, fromIndex] ) + +Returns the index of the last array element strictly equal to a search element, iterating from right to left. + + + +```javascript +var arr = new Float16Array( [ 1.0, 0.0, 2.0, 0.0, 1.0 ] ); + +var idx = arr.lastIndexOf( 0.0 ); +// returns 3 + +idx = arr.lastIndexOf( 3.0 ); +// returns -1 +``` + +By default, the method searches the entire array (`fromIndex = -1`). To begin searching from a specific array index, provide a `fromIndex`. + + + +```javascript +var arr = new Float16Array( [ 1.0, 0.0, 2.0, 0.0, 1.0 ] ); + +var idx = arr.lastIndexOf( 0.0, 2 ); +// returns 1 +``` + +When a `fromIndex` is negative, the starting index is resolved relative to the last array element. + + + +```javascript +var arr = new Float16Array( [ 1.0, 0.0, 2.0, 0.0, 1.0 ] ); + +var idx = arr.lastIndexOf( 0.0, -3 ); +// returns 1 +``` + +The method does **not** distinguish between signed and unsigned zero. + + + +#### Float16Array.prototype.map( fcn\[, thisArg] ) + +Maps each array element to an element in a new array having the same data type as the host array. + + + +```javascript +function fcn( v ) { + return v * 2.0; +} + +var arr1 = new Float16Array( [ 1.0, 2.0, 3.0 ] ); + +var arr2 = arr1.map( fcn ); +// returns [ 2.0, 4.0, 6.0 ] +``` + +A callback is provided three arguments: + +- `value`: array element. +- `index`: array index. +- `arr`: array on which the method is invoked. + +To set the callback execution context, provide a `thisArg`. + + + +```javascript +function fcn( v ) { + this.count += 1; + return v * 2.0; +} + +var ctx = { + 'count': 0 +}; + +var arr1 = new Float16Array( [ 1.0, 2.0, 3.0 ] ); + +var arr2 = arr1.map( fcn, ctx ); + +var n = ctx.count; +// returns 3 +``` + + + +#### Float16Array.prototype.reduce( fcn\[, initialValue] ) + +Applies a function against an accumulator and each element in an array and returns the accumulated result. + + + +```javascript +function fcn( acc, v ) { + return acc + ( v*v ); +} + +var arr = new Float16Array( [ 2.0, 1.0, 3.0 ] ); + +var v = arr.reduce( fcn ); +// returns 12.0 +``` + +If not provided an initial value, the method invokes a provided function with the first array element as the first argument and the second array element as the second argument. + +If provided an initial value, the method invokes a provided function with the initial value as the first argument and the first array element as the second argument. + + + +```javascript +function fcn( acc, v ) { + return acc + ( v*v ); +} + +var arr = new Float16Array( [ 2.0, 1.0, 3.0 ] ); + +var v = arr.reduce( fcn, 0.0 ); +// returns 14.0 +``` + +A callback is provided four arguments: + +- `acc`: accumulated result. +- `value`: array element. +- `index`: array index. +- `arr`: array on which the method is invoked. + + + +#### Float16Array.prototype.reduceRight( fcn\[, initialValue] ) + +Applies a function against an accumulator and each element in an array and returns the accumulated result, iterating from right to left. + + + +```javascript +function fcn( acc, v ) { + return acc + ( v*v ); +} + +var arr = new Float16Array( [ 2.0, 1.0, 3.0 ] ); + +var v = arr.reduceRight( fcn ); +// returns 8.0 +``` + +If not provided an initial value, the method invokes a provided function with the last array element as the first argument and the second-to-last array element as the second argument. + +If provided an initial value, the method invokes a provided function with the initial value as the first argument and the last array element as the second argument. + + + +```javascript +function fcn( acc, v ) { + return acc + ( v*v ); +} + +var arr = new Float16Array( [ 2.0, 1.0, 3.0 ] ); + +var v = arr.reduce( fcn, 0.0 ); +// returns 14.0 +``` + +A callback is provided four arguments: + +- `acc`: accumulated result. +- `value`: array element. +- `index`: array index. +- `arr`: array on which the method is invoked. + + + +#### Float16Array.prototype.reverse() + +Reverses an array **in-place** (thus mutating the array on which the method is invoked). + + + +```javascript +var arr = new Float16Array( [ 2.0, 0.0, 3.0 ] ); + +// Reverse the array: +arr.reverse(); + +var v = arr[ 0 ]; +// returns 3.0 + +v = arr[ 1 ]; +// returns 0.0 + +v = arr[ 2 ]; +// returns 2.0 +``` + + + +#### Float16Array.prototype.set( arr\[, offset] ) + +Sets array elements. + + + +```javascript +var arr = new Float16Array( [ 1.0, 2.0, 3.0 ] ); +// returns [ 1.0, 2.0, 3.0 ] + +// Set the first two array elements: +arr.set( [ 4.0, 5.0 ] ); + +var v = arr[ 0 ]; +// returns 4.0 + +v = arr[ 1 ]; +// returns 5.0 +``` + +By default, the method starts writing values at the first array index. To specify an alternative index, provide an index `offset`. + + + +```javascript +var arr = new Float16Array( [ 1.0, 2.0, 3.0 ] ); +// returns [ 1.0, 2.0, 3.0 ] + +// Set the last two array elements: +arr.set( [ 4.0, 5.0 ], 1 ); + +var v = arr[ 1 ]; +// returns 4.0 + +v = arr[ 2 ]; +// returns 5.0 +``` + + + +#### Float16Array.prototype.slice( \[begin\[, end]] ) + +Copies array elements to a new array with the same underlying data type as the host array. + + + +```javascript +var arr1 = new Float16Array( [ 1.0, 2.0, 3.0 ] ); + +var arr2 = arr1.slice(); + +var bool = ( arr1 === arr2 ); +// returns false + +bool = ( arr1.buffer === arr2.buffer ); +// returns false + +var v = arr2[ 0 ]; +// returns 1.0 + +v = arr2[ 1 ]; +// returns 2.0 + +v = arr2[ 2 ]; +// returns 3.0 +``` + +By default, the method copies elements beginning with the first array element. To specify an alternative array index at which to begin copying, provide a `begin` index (inclusive). + + + +```javascript +var arr1 = new Float16Array( [ 1.0, 2.0, 3.0 ] ); + +var arr2 = arr1.slice( 1 ); + +var len = arr2.length; +// returns 2 + +var v = arr2[ 0 ]; +// returns 2.0 + +v = arr2[ 1 ]; +// returns 3.0 +``` + +By default, the method copies all array elements after `begin`. To specify an alternative array index at which to end copying, provide an `end` index (exclusive). + + + +```javascript +var arr1 = new Float16Array( [ 1.0, 2.0, 3.0 ] ); + +var arr2 = arr1.slice( 0, 2 ); + +var len = arr2.length; +// returns 2 + +var v = arr2[ 0 ]; +// returns 1.0 + +v = arr2[ 1 ]; +// returns 2.0 +``` + +When a `begin` and/or `end` index is negative, the respective index is determined relative to the last array element. + + + +```javascript +var arr1 = new Float16Array( [ 1.0, 2.0, 3.0 ] ); + +var arr2 = arr1.slice( -arr1.length, -1 ); + +var len = arr2.length; +// returns 2 + +var v = arr2[ 0 ]; +// returns 1.0 + +v = arr2[ 1 ]; +// returns 2.0 +``` + + + +#### Float16Array.prototype.some( predicate\[, thisArg] ) + +Tests whether at least one array element passes a test implemented by a `predicate` function. + + + +```javascript +function predicate( v ) { + return ( v >= 2.0 ); +} + +var arr = new Float16Array( [ 1.0, 2.0 ] ); + +var bool = arr.some( predicate ); +// returns true +``` + +A `predicate` function is provided three arguments: + +- `value`: array element. +- `index`: array index. +- `arr`: array on which the method is invoked. + +To set the callback execution context, provide a `thisArg`. + + + +```javascript +function predicate( v ) { + this.count += 1; + return ( v >= 2.0 ); +} + +var ctx = { + 'count': 0 +}; + +var arr = new Float16Array( [ 1.0, 1.0 ] ); + +var bool = arr.some( predicate, ctx ); +// returns false + +var n = ctx.count; +// returns 2 +``` + + + +#### Float16Array.prototype.sort( \[compareFunction] ) + +Sorts an array **in-place** (thus mutating the array on which the method is invoked). + + + +```javascript +var arr = new Float16Array( [ 2.0, 3.0, 0.0 ] ); + +// Sort the array (in ascending order): +arr.sort(); + +var v = arr[ 0 ]; +// returns 0.0 + +v = arr[ 1 ]; +// returns 2.0 + +v = arr[ 2 ]; +// returns 3.0 +``` + +By default, the method sorts array elements in ascending order. To impose a custom order, provide a `compareFunction`. + + + +```javascript +function descending( a, b ) { + return b - a; +} + +var arr = new Float16Array( [ 2.0, 3.0, 0.0 ] ); + +// Sort the array (in descending order): +arr.sort( descending ); + +var v = arr[ 0 ]; +// returns 3.0 + +v = arr[ 1 ]; +// returns 2.0 + +v = arr[ 2 ]; +// returns 0.0 +``` + +The comparison function is provided two array elements, `a` and `b`, per invocation, and its return value determines the sort order as follows: + +- If the comparison function returns a value **less** than zero, then the method sorts `a` to an index lower than `b` (i.e., `a` should come **before** `b`). +- If the comparison function returns a value **greater** than zero, then the method sorts `a` to an index higher than `b` (i.e., `b` should come **before** `a`). +- If the comparison function returns **zero**, then the relative order of `a` and `b` _should_ remain unchanged. + + + +#### Float16Array.prototype.subarray( \[begin\[, end]] ) + +Creates a new typed array view over the same underlying [`ArrayBuffer`][@stdlib/array/buffer] and with the same underlying data type as the host array. + + + +```javascript +var arr1 = new Float16Array( [ 1.0, 2.0, 3.0 ] ); + +var arr2 = arr1.subarray(); +// returns [ 1.0, 2.0, 3.0 ] + +var bool = ( arr1.buffer === arr2.buffer ); +// returns true +``` + +By default, the method creates a typed array view beginning with the first array element. To specify an alternative array index at which to begin, provide a `begin` index (inclusive). + + + +```javascript +var arr1 = new Float16Array( [ 1.0, 2.0, 3.0 ] ); + +var arr2 = arr1.subarray( 1 ); +// returns [ 2.0, 3.0 ] + +var bool = ( arr1.buffer === arr2.buffer ); +// returns true +``` + +By default, the method creates a typed array view which includes all array elements after `begin`. To limit the number of array elements after `begin`, provide an `end` index (exclusive). + + + +```javascript +var arr1 = new Float16Array( [ 1.0, 2.0, 3.0 ] ); + +var arr2 = arr1.subarray( 0, 2 ); +// returns [ 1.0, 2.0 ] + +var bool = ( arr1.buffer === arr2.buffer ); +// returns true +``` + +When a `begin` and/or `end` index is negative, the respective index is determined relative to the last array element. + + + +```javascript +var arr1 = new Float16Array( [ 1.0, 2.0, 3.0 ] ); + +var arr2 = arr1.subarray( -arr1.length, -1 ); +// returns [ 1.0, 2.0 ] + +var bool = ( arr1.buffer === arr2.buffer ); +// returns true +``` + +If the method is unable to resolve indices to a non-empty array subsequence, the method returns an empty typed array. + + + +```javascript +var arr1 = new Float16Array( [ 1.0, 2.0, 3.0 ] ); + +var arr2 = arr1.subarray( 10, -1 ); +// returns [] +``` + + + +#### Float16Array.prototype.toLocaleString( \[locales\[, options]] ) + +Serializes an array as a locale-specific `string`. + + + +```javascript +var arr = new Float16Array( [ 1.0, 2.0, 3.0 ] ); + +var str = arr.toLocaleString(); +// returns '1,2,3' +``` + + + +#### Float16Array.prototype.toString() + +Serializes an array as a `string`. + + + +```javascript +var arr = new Float16Array( [ 1.0, 2.0, 3.0 ] ); + +var str = arr.toString(); +// returns '1,2,3' +``` + + + +#### Float16Array.prototype.values() + +Returns an iterator for iterating over array elements. + + + +```javascript +var arr = new Float16Array( [ 1.0, 2.0 ] ); + +// Create an iterator: +var it = arr.values(); + +// Iterate over array elements... +var v = it.next().value; +// returns 1.0 + +v = it.next().value; +// returns 2.0 + +var bool = it.next().done; +// returns true +``` + +
+ + + +* * * + + + +
+ +
+ + + + + +
+ +## Examples + + + +```javascript +var randu = require( '@stdlib/random/base/randu' ); +var ctor = require( '@stdlib/array/float16' ); + +var arr; +var i; + +arr = new ctor( 10 ); +for ( i = 0; i < arr.length; i++ ) { + arr[ i ] = randu() * 100.0; +} +console.log( arr ); +``` + +
+ + + + + +
+ +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.copy_within.js b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.copy_within.js new file mode 100644 index 000000000000..dbf8a2837a3c --- /dev/null +++ b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.copy_within.js @@ -0,0 +1,50 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var pkg = require( './../package.json' ).name; +var Float16Array = require( './../lib' ); + + +// MAIN // + +bench( pkg+':copyWithin', function benchmark( b ) { + var arr; + var i; + + arr = new Float16Array( 2 ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + arr[ 0 ] = i; + arr = arr.copyWithin( 1, 0 ); + if ( arr[ 0 ] !== i ) { + b.fail( 'unexpected result' ); + } + } + b.toc(); + if ( arr[ 0 ] !== arr[ 0 ] ) { + b.fail( 'should not be NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.copy_within.length.js b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.copy_within.length.js new file mode 100644 index 000000000000..01668f1284ad --- /dev/null +++ b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.copy_within.length.js @@ -0,0 +1,93 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var pkg = require( './../package.json' ).name; +var Float16Array = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var arr = new Float16Array( len ); + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + arr[ 0 ] = i; + arr = arr.copyWithin( 1, 0 ); + if ( arr[ 0 ] !== i ) { + b.fail( 'unexpected result' ); + } + } + b.toc(); + if ( arr[ 0 ] !== arr[ 0 ] ) { + b.fail( 'should not be NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var len; + var min; + var max; + var f; + var i; + + min = 1; // 10^min + max = 6; // 10^max + + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + f = createBenchmark( len ); + bench( pkg+':copyWithin:len='+len, f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.data.js b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.data.js new file mode 100644 index 000000000000..d8aa253d5caf --- /dev/null +++ b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.data.js @@ -0,0 +1,75 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var pkg = require( './../package.json' ).name; +var Float16Array = require( './../lib' ); + + +// MAIN // + +bench( pkg+'::get,index', function benchmark( b ) { + var arr; + var N; + var v; + var i; + + arr = new Float16Array( 2 ); + N = arr.length; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = arr[ i%N ]; + if ( v !== v ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( v !== v ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::set,index', function benchmark( b ) { + var arr; + var N; + var i; + + arr = new Float16Array( 2 ); + N = arr.length; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + arr[ i%N ] = i; + if ( arr[ 0 ] !== arr[ 0 ] ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( arr[ 0 ] !== arr[ 0 ] || arr[ 1 ] !== arr[ 1 ] ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.entries.js b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.entries.js new file mode 100644 index 000000000000..f4ac2cf53b99 --- /dev/null +++ b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.entries.js @@ -0,0 +1,50 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var pkg = require( './../package.json' ).name; +var Float16Array = require( './../lib' ); + + +// MAIN // + +bench( pkg+':entries', function benchmark( b ) { + var iter; + var arr; + var i; + + arr = new Float16Array( [ 1.0, 2.0 ] ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + iter = arr.entries(); + if ( typeof iter !== 'object' ) { + b.fail( 'should return an object' ); + } + } + b.toc(); + if ( typeof iter !== 'object' || typeof iter.next !== 'function' ) { + b.fail( 'should return an iterator protocol-compliant object' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.every.js b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.every.js new file mode 100644 index 000000000000..32e70afd9d8b --- /dev/null +++ b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.every.js @@ -0,0 +1,81 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var Float16Array = require( './../lib' ); + + +// MAIN // + +bench( pkg+':every', function benchmark( b ) { + var bool; + var arr; + var i; + + arr = new Float16Array( 2 ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = arr.every( predicate ); + if ( typeof bool !== 'boolean' ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); + + function predicate( v ) { + return v >= 0.0; + } +}); + +bench( pkg+'::this_context:every', function benchmark( b ) { + var bool; + var arr; + var i; + + arr = new Float16Array( 2 ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = arr.every( predicate, {} ); + if ( typeof bool !== 'boolean' ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); + + function predicate( v ) { + return v >= 0.0; + } +}); diff --git a/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.every.length.js b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.every.length.js new file mode 100644 index 000000000000..01f8bacbf7da --- /dev/null +++ b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.every.length.js @@ -0,0 +1,105 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var Float16Array = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Returns a boolean indicating whether an array element passes a test. +* +* @private +* @param {*} value - value to test +* @returns {boolean} boolean indicating whether an array element passes a test +*/ +function predicate( value ) { + return value >= 0.0; +} + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var arr = new Float16Array( len ); + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var bool; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = arr.every( predicate ); + if ( typeof bool !== 'boolean' ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var len; + var min; + var max; + var f; + var i; + + min = 1; // 10^min + max = 6; // 10^max + + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + f = createBenchmark( len ); + bench( pkg+':every:len='+len, f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.fill.js b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.fill.js new file mode 100644 index 000000000000..3cdf17366c8c --- /dev/null +++ b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.fill.js @@ -0,0 +1,51 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var pkg = require( './../package.json' ).name; +var Float16Array = require( './../lib' ); + + +// MAIN // + +bench( pkg+':fill', function benchmark( b ) { + var arr; + var v; + var i; + + arr = new Float16Array( 2 ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = i % 128; + arr = arr.fill( v ); + if ( arr[ 0 ] !== v ) { + b.fail( 'unexpected result' ); + } + } + b.toc(); + if ( arr[ 0 ] !== arr[ 0 ] ) { + b.fail( 'should not be NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.fill.length.js b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.fill.length.js new file mode 100644 index 000000000000..4c4338095ba0 --- /dev/null +++ b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.fill.length.js @@ -0,0 +1,94 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var pkg = require( './../package.json' ).name; +var Float16Array = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var arr = new Float16Array( len ); + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var v; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = i % 128; + arr = arr.fill( v ); + if ( arr[ 0 ] !== v ) { + b.fail( 'unexpected result' ); + } + } + b.toc(); + if ( arr[ 0 ] !== arr[ 0 ] ) { + b.fail( 'should not be NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var len; + var min; + var max; + var f; + var i; + + min = 1; // 10^min + max = 6; // 10^max + + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + f = createBenchmark( len ); + bench( pkg+':fill:len='+len, f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.filter.js b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.filter.js new file mode 100644 index 000000000000..13852ea3a9f7 --- /dev/null +++ b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.filter.js @@ -0,0 +1,81 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isFloat16Array = require( '@stdlib/assert/is-float16array' ); +var pkg = require( './../package.json' ).name; +var Float16Array = require( './../lib' ); + + +// MAIN // + +bench( pkg+':filter', function benchmark( b ) { + var arr; + var out; + var i; + + arr = new Float16Array( 2 ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + out = arr.filter( predicate ); + if ( typeof out !== 'object' ) { + b.fail( 'should return an object' ); + } + } + b.toc(); + if ( !isFloat16Array( out ) ) { + b.fail( 'should return a Float16Array' ); + } + b.pass( 'benchmark finished' ); + b.end(); + + function predicate( v ) { + return v >= 0.0; + } +}); + +bench( pkg+'::this_context:filter', function benchmark( b ) { + var arr; + var out; + var i; + + arr = new Float16Array( 2 ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + out = arr.filter( predicate, {} ); + if ( typeof out !== 'object' ) { + b.fail( 'should return an object' ); + } + } + b.toc(); + if ( !isFloat16Array( out ) ) { + b.fail( 'should return a Float16Array' ); + } + b.pass( 'benchmark finished' ); + b.end(); + + function predicate( v ) { + return v >= 0.0; + } +}); diff --git a/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.filter.length.js b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.filter.length.js new file mode 100644 index 000000000000..34b7e7c5bd37 --- /dev/null +++ b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.filter.length.js @@ -0,0 +1,105 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var isFloat16Array = require( '@stdlib/assert/is-float16array' ); +var pkg = require( './../package.json' ).name; +var Float16Array = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Returns a boolean indicating whether an array element passes a test. +* +* @private +* @param {*} value - value to test +* @returns {boolean} boolean indicating whether an array element passes a test +*/ +function predicate( value ) { + return value >= 0.0; +} + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var arr = new Float16Array( len ); + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var out; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + out = arr.filter( predicate ); + if ( typeof out !== 'object' ) { + b.fail( 'should return an object' ); + } + } + b.toc(); + if ( !isFloat16Array( out ) ) { + b.fail( 'should return a Float16Array' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var len; + var min; + var max; + var f; + var i; + + min = 1; // 10^min + max = 5; // 10^max + + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + f = createBenchmark( len ); + bench( pkg+':filter:len='+len, f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.find.js b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.find.js new file mode 100644 index 000000000000..82b56b2e226e --- /dev/null +++ b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.find.js @@ -0,0 +1,82 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var pkg = require( './../package.json' ).name; +var Float16Array = require( './../lib' ); + + +// MAIN // + +bench( pkg+':find', function benchmark( b ) { + var out; + var arr; + var i; + + arr = new Float16Array( 2 ); + + // Benchmark worst case scenario... + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + out = arr.find( predicate ); + if ( typeof out !== 'undefined' ) { + b.fail( 'should return undefined' ); + } + } + b.toc(); + if ( typeof out !== 'undefined' ) { + b.fail( 'should return undefined' ); + } + b.pass( 'benchmark finished' ); + b.end(); + + function predicate( v ) { + return v > 0.0; + } +}); + +bench( pkg+'::this_context:find', function benchmark( b ) { + var out; + var arr; + var i; + + arr = new Float16Array( 2 ); + + // Benchmark worst case scenario... + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + out = arr.find( predicate, {} ); + if ( typeof out !== 'undefined' ) { + b.fail( 'should return undefined' ); + } + } + b.toc(); + if ( typeof out !== 'undefined' ) { + b.fail( 'should return undefined' ); + } + b.pass( 'benchmark finished' ); + b.end(); + + function predicate( v ) { + return v > 0.0; + } +}); diff --git a/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.find.length.js b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.find.length.js new file mode 100644 index 000000000000..d81885e5f655 --- /dev/null +++ b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.find.length.js @@ -0,0 +1,105 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var pkg = require( './../package.json' ).name; +var Float16Array = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Returns a boolean indicating whether an array element passes a test. +* +* @private +* @param {*} value - value to test +* @returns {boolean} boolean indicating whether an array element passes a test +*/ +function predicate( value ) { + return value > 0.0; +} + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - tuple length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var arr = new Float16Array( len ); + + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var out; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + out = arr.find( predicate ); + if ( typeof out !== 'undefined' ) { + b.fail( 'should return undefined' ); + } + } + b.toc(); + if ( typeof out !== 'undefined' ) { + b.fail( 'should return undefined' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var len; + var min; + var max; + var f; + var i; + + min = 1; // 10^min + max = 6; // 10^max + + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + f = createBenchmark( len ); + bench( pkg+':find:len='+len, f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.find_index.js b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.find_index.js new file mode 100644 index 000000000000..88bb9d03a72c --- /dev/null +++ b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.find_index.js @@ -0,0 +1,82 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var pkg = require( './../package.json' ).name; +var Float16Array = require( './../lib' ); + + +// MAIN // + +bench( pkg+':findIndex', function benchmark( b ) { + var out; + var arr; + var i; + + arr = new Float16Array( 2 ); + + // Benchmark worst case scenario... + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + out = arr.findIndex( predicate ); + if ( out !== -1 ) { + b.fail( 'should return -1' ); + } + } + b.toc(); + if ( out !== -1 ) { + b.fail( 'should return -1' ); + } + b.pass( 'benchmark finished' ); + b.end(); + + function predicate( v ) { + return v > 0.0; + } +}); + +bench( pkg+'::this_context:findIndex', function benchmark( b ) { + var out; + var arr; + var i; + + arr = new Float16Array( 2 ); + + // Benchmark worst case scenario... + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + out = arr.findIndex( predicate, {} ); + if ( out !== -1 ) { + b.fail( 'should return -1' ); + } + } + b.toc(); + if ( out !== -1 ) { + b.fail( 'should return -1' ); + } + b.pass( 'benchmark finished' ); + b.end(); + + function predicate( v ) { + return v > 0.0; + } +}); diff --git a/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.find_index.length.js b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.find_index.length.js new file mode 100644 index 000000000000..ec18df389885 --- /dev/null +++ b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.find_index.length.js @@ -0,0 +1,104 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var pkg = require( './../package.json' ).name; +var Float16Array = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Returns a boolean indicating whether an array element passes a test. +* +* @private +* @param {*} value - value to test +* @returns {boolean} boolean indicating whether an array element passes a test +*/ +function predicate( value ) { + return value > 0.0; +} + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var arr = new Float16Array( len ); + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var out; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + out = arr.findIndex( predicate ); + if ( out !== -1 ) { + b.fail( 'should return -1' ); + } + } + b.toc(); + if ( out !== -1 ) { + b.fail( 'should return -1' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var len; + var min; + var max; + var f; + var i; + + min = 1; // 10^min + max = 6; // 10^max + + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + f = createBenchmark( len ); + bench( pkg+':findIndex:len='+len, f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.for_each.js b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.for_each.js new file mode 100644 index 000000000000..95b95fddf272 --- /dev/null +++ b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.for_each.js @@ -0,0 +1,88 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var pkg = require( './../package.json' ).name; +var Float16Array = require( './../lib/polyfill.js' ); + + +// MAIN // + +bench( pkg+':forEach', function benchmark( b ) { + var count; + var arr; + var N; + var i; + + arr = new Float16Array( 2 ); + N = arr.length; + + count = 0; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + arr.forEach( fcn ); + if ( count !== N*(i+1) ) { + b.fail( 'unexpected result' ); + } + } + b.toc(); + if ( count !== N*i ) { + b.fail( 'unexpected result' ); + } + b.pass( 'benchmark finished' ); + b.end(); + + function fcn() { + count += 1; + } +}); + +bench( pkg+'::this_context:forEach', function benchmark( b ) { + var count; + var arr; + var N; + var i; + + arr = new Float16Array( 2 ); + N = arr.length; + + count = 0; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + arr.forEach( fcn, {} ); + if ( count !== N*(i+1) ) { + b.fail( 'unexpected result' ); + } + } + b.toc(); + if ( count !== N*i ) { + b.fail( 'unexpected result' ); + } + b.pass( 'benchmark finished' ); + b.end(); + + function fcn() { + count += 1; + } +}); diff --git a/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.for_each.length.js b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.for_each.length.js new file mode 100644 index 000000000000..06a3593cfcfe --- /dev/null +++ b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.for_each.length.js @@ -0,0 +1,106 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var pkg = require( './../package.json' ).name; +var Float16Array = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var count; + var arr; + + arr = new Float16Array( len ); + count = 0; + + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + arr.forEach( fcn ); + if ( count !== count ) { + b.fail( 'should not be NaN' ); + } + } + b.toc(); + if ( count !== count ) { + b.fail( 'should not be NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } + + /** + * Callback invoked for each tuple element. + * + * @private + */ + function fcn() { + count += 1; + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var len; + var min; + var max; + var f; + var i; + + min = 1; // 10^min + max = 6; // 10^max + + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + f = createBenchmark( len ); + bench( pkg+':forEach:len='+len, f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.from.js b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.from.js new file mode 100644 index 000000000000..3476a001aec1 --- /dev/null +++ b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.from.js @@ -0,0 +1,237 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var ITERATOR_SYMBOL = require( '@stdlib/symbol/iterator' ); +var isFloat16Array = require( '@stdlib/assert/is-float16array' ); +var pkg = require( './../package.json' ).name; +var Float16Array = require( './../lib' ); + + +// VARIABLES // + +var opts = { + 'skip': ( ITERATOR_SYMBOL === null ) +}; + + +// MAIN // + +bench( pkg+'::typed_array:from', function benchmark( b ) { + var buf; + var arr; + var i; + + buf = new Float16Array( [ 1.0, 2.0 ] ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + arr = Float16Array.from( buf ); + if ( arr.length !== 2 ) { + b.fail( 'should have length 2' ); + } + } + b.toc(); + if ( !isFloat16Array( arr ) ) { + b.fail( 'should return a Float32Array' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::typed_array,clbk:from', function benchmark( b ) { + var buf; + var arr; + var i; + + buf = new Float16Array( [ 1.0, 2.0 ] ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + arr = Float16Array.from( buf, clbk ); + if ( arr.length !== 2 ) { + b.fail( 'should have length 2' ); + } + } + b.toc(); + if ( !isFloat16Array( arr ) ) { + b.fail( 'should return a Float32Array' ); + } + b.pass( 'benchmark finished' ); + b.end(); + + function clbk( v ) { + return v + 1.0; + } +}); + +bench( pkg+'::array:from', function benchmark( b ) { + var buf; + var arr; + var i; + + buf = [ 1.0, 2.0 ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + arr = Float16Array.from( buf ); + if ( arr.length !== 2 ) { + b.fail( 'should have length 2' ); + } + } + b.toc(); + if ( !isFloat16Array( arr ) ) { + b.fail( 'should return a Float32Array' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::array,clbk:from', function benchmark( b ) { + var buf; + var arr; + var i; + + buf = [ 1.0, 2.0 ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + arr = Float16Array.from( buf, clbk ); + if ( arr.length !== 2 ) { + b.fail( 'should have length 2' ); + } + } + b.toc(); + if ( !isFloat16Array( arr ) ) { + b.fail( 'should return a Float32Array' ); + } + b.pass( 'benchmark finished' ); + b.end(); + + function clbk( v ) { + return v + 1.0; + } +}); + +bench( pkg+'::iterable:from', opts, function benchmark( b ) { + var arr; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + arr = Float16Array.from( createIterable() ); + if ( arr.length !== 2 ) { + b.fail( 'should have length 2' ); + } + } + b.toc(); + if ( !isFloat16Array( arr ) ) { + b.fail( 'should return a Float32Array' ); + } + b.pass( 'benchmark finished' ); + b.end(); + + function createIterable() { + var out; + var i; + + out = {}; + out[ ITERATOR_SYMBOL ] = iterator; + + i = 0; + + return out; + + function iterator() { + return { + 'next': next + }; + } + + function next() { + i += 1; + if ( i <= 2 ) { + return { + 'value': 0.0, + 'done': false + }; + } + return { + 'done': true + }; + } + } +}); + +bench( pkg+'::iterable,clbk:from:', opts, function benchmark( b ) { + var arr; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + arr = Float16Array.from( createIterable(), clbk ); + if ( arr.length !== 2 ) { + b.fail( 'should have length 2' ); + } + } + b.toc(); + if ( !isFloat16Array( arr ) ) { + b.fail( 'should return a Float32Array' ); + } + b.pass( 'benchmark finished' ); + b.end(); + + function createIterable() { + var out; + var i; + + out = {}; + out[ ITERATOR_SYMBOL ] = iterator; + + i = 0; + + return out; + + function iterator() { + return { + 'next': next + }; + } + + function next() { + i += 1; + if ( i <= 2 ) { + return { + 'value': 1.0, + 'done': false + }; + } + return { + 'done': true + }; + } + } + + function clbk( v ) { + return v + 1.0; + } +}); diff --git a/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.includes.js b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.includes.js new file mode 100644 index 000000000000..9b941b96ef33 --- /dev/null +++ b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.includes.js @@ -0,0 +1,53 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var Float16Array = require( './../lib' ); + + +// MAIN // + +bench( pkg+':includes', function benchmark( b ) { + var bool; + var arr; + var v; + var i; + + arr = new Float16Array( 2 ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = (i%127) + 1.0; + bool = arr.includes( v ); + if ( typeof bool !== 'boolean' ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.includes.length.js b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.includes.length.js new file mode 100644 index 000000000000..3a1def4cd881 --- /dev/null +++ b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.includes.length.js @@ -0,0 +1,96 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var Float16Array = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var arr = new Float16Array( len ); + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var bool; + var v; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = (i%127) + 1.0; + bool = arr.includes( v ); + if ( typeof bool !== 'boolean' ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var len; + var min; + var max; + var f; + var i; + + min = 1; // 10^min + max = 6; // 10^max + + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + f = createBenchmark( len ); + bench( pkg+':includes:len='+len, f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.index_of.js b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.index_of.js new file mode 100644 index 000000000000..5dd1bc0244cc --- /dev/null +++ b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.index_of.js @@ -0,0 +1,53 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var pkg = require( './../package.json' ).name; +var Float16Array = require( './../lib' ); + + +// MAIN // + +bench( pkg+':indexOf', function benchmark( b ) { + var out; + var arr; + var v; + var i; + + arr = new Float16Array( 2 ); + + // Benchmark worst case scenario... + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = (i%127) + 1.0; + out = arr.indexOf( v ); + if ( out !== -1 ) { + b.fail( 'should return -1' ); + } + } + b.toc(); + if ( out !== -1 ) { + b.fail( 'should return -1' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.index_of.length.js b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.index_of.length.js new file mode 100644 index 000000000000..b86daf85d60c --- /dev/null +++ b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.index_of.length.js @@ -0,0 +1,95 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var pkg = require( './../package.json' ).name; +var Float16Array = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var arr = new Float16Array( len ); + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var out; + var v; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = (i%127) + 1.0; + out = arr.indexOf( v ); + if ( out !== -1 ) { + b.fail( 'should return -1' ); + } + } + b.toc(); + if ( out !== -1 ) { + b.fail( 'should return -1' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var len; + var min; + var max; + var f; + var i; + + min = 1; // 10^min + max = 6; // 10^max + + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + f = createBenchmark( len ); + bench( pkg+':indexOf:len='+len, f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.join.js b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.join.js new file mode 100644 index 000000000000..8b8a15bf3bba --- /dev/null +++ b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.join.js @@ -0,0 +1,51 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var pkg = require( './../package.json' ).name; +var Float16Array = require( './../lib' ); + + +// MAIN // + +bench( pkg+':join', function benchmark( b ) { + var out; + var arr; + var i; + + arr = new Float16Array( 2 ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + arr[ 0 ] = i % 127; + out = arr.join(); + if ( typeof out !== 'string' ) { + b.fail( 'should return a string' ); + } + } + b.toc(); + if ( typeof out !== 'string' ) { + b.fail( 'should return a string' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.join.length.js b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.join.length.js new file mode 100644 index 000000000000..543fe9f7bdfa --- /dev/null +++ b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.join.length.js @@ -0,0 +1,94 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var pkg = require( './../package.json' ).name; +var Float16Array = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var arr = new Float16Array( len ); + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var out; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + arr[ 0 ] = i % 127; + out = arr.join(); + if ( typeof out !== 'string' ) { + b.fail( 'should return a string' ); + } + } + b.toc(); + if ( typeof out !== 'string' ) { + b.fail( 'should return a string' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var len; + var min; + var max; + var f; + var i; + + min = 1; // 10^min + max = 6; // 10^max + + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + f = createBenchmark( len ); + bench( pkg+':join:len='+len, f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.js b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.js new file mode 100644 index 000000000000..3c9a81e61892 --- /dev/null +++ b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.js @@ -0,0 +1,49 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isFloat16Array = require( '@stdlib/assert/is-float16array' ); +var pkg = require( './../package.json' ).name; +var ctor = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var arr; + var i; + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + arr = new ctor( 0 ); + if ( arr.length !== 0 ) { + b.fail( 'should have length 0' ); + } + } + b.toc(); + if ( !isFloat16Array( arr ) ) { + b.fail( 'should return a Float16Array' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +// TODO: add additional instantiation benchmarks (e.g., ArrayBuffer, etc) diff --git a/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.keys.js b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.keys.js new file mode 100644 index 000000000000..0e79e851a2d1 --- /dev/null +++ b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.keys.js @@ -0,0 +1,50 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var pkg = require( './../package.json' ).name; +var Float16Array = require( './../lib' ); + + +// MAIN // + +bench( pkg+':keys', function benchmark( b ) { + var iter; + var arr; + var i; + + arr = new Float16Array( 2 ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + iter = arr.keys(); + if ( typeof iter !== 'object' ) { + b.fail( 'should return an object' ); + } + } + b.toc(); + if ( typeof iter !== 'object' || typeof iter.next !== 'function' ) { + b.fail( 'should return an iterator protocol-compliant object' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.last_index_of.js b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.last_index_of.js new file mode 100644 index 000000000000..be16c1644652 --- /dev/null +++ b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.last_index_of.js @@ -0,0 +1,53 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var pkg = require( './../package.json' ).name; +var Float16Array = require( './../lib' ); + + +// MAIN // + +bench( pkg+':lastIndexOf', function benchmark( b ) { + var out; + var arr; + var v; + var i; + + arr = new Float16Array( 2 ); + + // Benchmark worst case scenario... + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = (i%127) + 1.0; + out = arr.lastIndexOf( v ); + if ( out !== -1 ) { + b.fail( 'should return -1' ); + } + } + b.toc(); + if ( out !== -1 ) { + b.fail( 'should return -1' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.last_index_of.length.js b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.last_index_of.length.js new file mode 100644 index 000000000000..ab87f85041b3 --- /dev/null +++ b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.last_index_of.length.js @@ -0,0 +1,95 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var pkg = require( './../package.json' ).name; +var Float16Array = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var arr = new Float16Array( len ); + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var out; + var v; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = (i%127) + 1.0; + out = arr.lastIndexOf( v ); + if ( out !== -1 ) { + b.fail( 'should return -1' ); + } + } + b.toc(); + if ( out !== -1 ) { + b.fail( 'should return -1' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var len; + var min; + var max; + var f; + var i; + + min = 1; // 10^min + max = 6; // 10^max + + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + f = createBenchmark( len ); + bench( pkg+':lastIndexOf:len='+len, f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.length.js b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.length.js new file mode 100644 index 000000000000..2050edf7f209 --- /dev/null +++ b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.length.js @@ -0,0 +1,93 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var isFloat16Array = require( '@stdlib/assert/is-float16array' ); +var pkg = require( './../package.json' ).name; +var ctor = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var arr; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + arr = new ctor( len ); + if ( arr.length !== len ) { + b.fail( 'unexpected length' ); + } + } + b.toc(); + if ( !isFloat16Array( arr ) ) { + b.fail( 'should return a Float16Array' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var len; + var min; + var max; + var f; + var i; + + min = 1; // 10^min + max = 6; // 10^max + + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + f = createBenchmark( len ); + bench( pkg+':len='+len, f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.map.js b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.map.js new file mode 100644 index 000000000000..997e46d36a0a --- /dev/null +++ b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.map.js @@ -0,0 +1,81 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isFloat16Array = require( '@stdlib/assert/is-float16array' ); +var pkg = require( './../package.json' ).name; +var Float16Array = require( './../lib' ); + + +// MAIN // + +bench( pkg+':map', function benchmark( b ) { + var out; + var arr; + var i; + + arr = new Float16Array( 2 ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + out = arr.map( fcn ); + if ( typeof out !== 'object' ) { + b.fail( 'should return an object' ); + } + } + b.toc(); + if ( !isFloat16Array( out ) ) { + b.fail( 'should return a Float16Array' ); + } + b.pass( 'benchmark finished' ); + b.end(); + + function fcn( v ) { + return v + 1.0; + } +}); + +bench( pkg+'::this_context:map', function benchmark( b ) { + var out; + var arr; + var i; + + arr = new Float16Array( 2 ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + out = arr.map( fcn, {} ); + if ( typeof out !== 'object' ) { + b.fail( 'should return an object' ); + } + } + b.toc(); + if ( !isFloat16Array( out ) ) { + b.fail( 'should return a Float16Array' ); + } + b.pass( 'benchmark finished' ); + b.end(); + + function fcn( v ) { + return v + 1.0; + } +}); diff --git a/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.map.length.js b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.map.length.js new file mode 100644 index 000000000000..e2429481fbf7 --- /dev/null +++ b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.map.length.js @@ -0,0 +1,105 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var isFloat16Array = require( '@stdlib/assert/is-float16array' ); +var pkg = require( './../package.json' ).name; +var Float16Array = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Maps an array element to a new value. +* +* @private +* @param {*} value - array element +* @returns {*} new value +*/ +function fcn( value ) { + return value + 1.0; +} + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var arr = new Float16Array( len ); + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var out; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + out = arr.map( fcn ); + if ( typeof out !== 'object' ) { + b.fail( 'should return an object' ); + } + } + b.toc(); + if ( !isFloat16Array( out ) ) { + b.fail( 'should return a Float16Array' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var len; + var min; + var max; + var f; + var i; + + min = 1; // 10^min + max = 5; // 10^max + + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + f = createBenchmark( len ); + bench( pkg+':map:len='+len, f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.of.js b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.of.js new file mode 100644 index 000000000000..fcd777e7c643 --- /dev/null +++ b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.of.js @@ -0,0 +1,48 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isFloat16Array = require( '@stdlib/assert/is-float16array' ); +var pkg = require( './../package.json' ).name; +var Float16Array = require( './../lib' ); + + +// MAIN // + +bench( pkg+':of', function benchmark( b ) { + var arr; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + arr = Float16Array.of( i, 2.0 ); + if ( arr.length !== 2 ) { + b.fail( 'should have length 2' ); + } + } + b.toc(); + if ( !isFloat16Array( arr ) ) { + b.fail( 'should return a Float16Array' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.properties.js b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.properties.js new file mode 100644 index 000000000000..333b6d52a60e --- /dev/null +++ b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.properties.js @@ -0,0 +1,145 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isArrayBuffer = require( '@stdlib/assert/is-arraybuffer' ); +var isNonNegativeInteger = require( '@stdlib/assert/is-nonnegative-integer' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var Float16Array = require( './../lib/polyfill.js' ); + + +// MAIN // + +bench( pkg+'::get:buffer', function benchmark( b ) { + var arr; + var v; + var i; + + arr = new Float16Array( 2 ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + // Note: the following may be optimized away due to loop invariant code motion and/or other compiler optimizations, rendering this benchmark meaningless... + v = arr.buffer; + if ( typeof v !== 'object' ) { + b.fail( 'should return an object' ); + } + } + b.toc(); + if ( !isArrayBuffer( v ) ) { + b.fail( 'should return an ArrayBuffer' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::get:byteLength', function benchmark( b ) { + var arr; + var v; + var i; + + arr = new Float16Array( 2 ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + // Note: the following may be optimized away due to loop invariant code motion and/or other compiler optimizations, rendering this benchmark meaningless... + v = arr.byteLength; + if ( v !== v ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( !isNonNegativeInteger( v ) ) { + b.fail( 'should return a nonnegative integer' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::get:byteOffset', function benchmark( b ) { + var arr; + var v; + var i; + + arr = new Float16Array( 2 ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + // Note: the following may be optimized away due to loop invariant code motion and/or other compiler optimizations, rendering this benchmark meaningless... + v = arr.byteOffset; + if ( v !== v ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( !isNonNegativeInteger( v ) ) { + b.fail( 'should return a nonnegative integer' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::get:BYTES_PER_ELEMENT', function benchmark( b ) { + var arr; + var v; + var i; + + arr = new Float16Array( 2 ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + // Note: the following may be optimized away due to loop invariant code motion and/or other compiler optimizations, rendering this benchmark meaningless... + v = arr.BYTES_PER_ELEMENT; + if ( v !== v ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( !isNonNegativeInteger( v ) ) { + b.fail( 'should return a nonnegative integer' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::get:length', function benchmark( b ) { + var arr; + var v; + var i; + + arr = new Float16Array( 2 ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + // Note: the following may be optimized away due to loop invariant code motion and/or other compiler optimizations, rendering this benchmark meaningless... + v = arr.length; + if ( v !== v ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( !isNonNegativeInteger( v ) ) { + b.fail( 'should return a nonnegative integer' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.reduce.js b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.reduce.js new file mode 100644 index 000000000000..c87b8e8ece8b --- /dev/null +++ b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.reduce.js @@ -0,0 +1,80 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var pkg = require( './../package.json' ).name; +var Float16Array = require( './../lib/polyfill.js' ); + + +// MAIN // + +bench( pkg+':reduce', function benchmark( b ) { + var out; + var arr; + var i; + + arr = new Float16Array( 2 ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + out = arr.reduce( fcn ); + if ( out !== out ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( out !== out ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); + + function fcn( acc, v ) { + return acc + v + 1.0; + } +}); + +bench( pkg+'::initial_value:reduce', function benchmark( b ) { + var out; + var arr; + var i; + + arr = new Float16Array( 2 ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + out = arr.reduce( fcn, 3.14 ); + if ( out !== out ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( out !== out ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); + + function fcn( v ) { + return v + 1.0; + } +}); diff --git a/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.reduce.length.js b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.reduce.length.js new file mode 100644 index 000000000000..d1c2c8f3e909 --- /dev/null +++ b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.reduce.length.js @@ -0,0 +1,105 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var pkg = require( './../package.json' ).name; +var Float16Array = require( './../lib/polyfill.js' ); + + +// FUNCTIONS // + +/** +* Updates an accumulated value. +* +* @private +* @param {*} acc - accumulated value +* @param {*} value - array element +* @returns {*} accumulated value +*/ +function fcn( acc, value ) { + return acc + value + 1.0; +} + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var arr = new Float16Array( len ); + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var out; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + out = arr.reduce( fcn ); + if ( out !== out ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( out !== out ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var len; + var min; + var max; + var f; + var i; + + min = 1; // 10^min + max = 6; // 10^max + + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + f = createBenchmark( len ); + bench( pkg+':reduce:len='+len, f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.reduce_right.js b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.reduce_right.js new file mode 100644 index 000000000000..10e28ffc5ae0 --- /dev/null +++ b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.reduce_right.js @@ -0,0 +1,80 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var pkg = require( './../package.json' ).name; +var Float16Array = require( './../lib/polyfill.js' ); + + +// MAIN // + +bench( pkg+':reduceRight', function benchmark( b ) { + var out; + var arr; + var i; + + arr = new Float16Array( 2 ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + out = arr.reduceRight( fcn ); + if ( out !== out ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( out !== out ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); + + function fcn( acc, v ) { + return acc + v + 1.0; + } +}); + +bench( pkg+'::initial_value:reduceRight', function benchmark( b ) { + var out; + var arr; + var i; + + arr = new Float16Array( 2 ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + out = arr.reduceRight( fcn, 3.14 ); + if ( out !== out ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( out !== out ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); + + function fcn( v ) { + return v + 1.0; + } +}); diff --git a/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.reduce_right.length.js b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.reduce_right.length.js new file mode 100644 index 000000000000..50dc92c4d5b4 --- /dev/null +++ b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.reduce_right.length.js @@ -0,0 +1,105 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var pkg = require( './../package.json' ).name; +var Float16Array = require( './../lib/polyfill.js' ); + + +// FUNCTIONS // + +/** +* Updates an accumulated value. +* +* @private +* @param {*} acc - accumulated value +* @param {*} value - array element +* @returns {*} accumulated value +*/ +function fcn( acc, value ) { + return acc + value + 1.0; +} + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var arr = new Float16Array( len ); + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var out; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + out = arr.reduceRight( fcn ); + if ( out !== out ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( out !== out ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var len; + var min; + var max; + var f; + var i; + + min = 1; // 10^min + max = 6; // 10^max + + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + f = createBenchmark( len ); + bench( pkg+':reduceRight:len='+len, f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.reverse.js b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.reverse.js new file mode 100644 index 000000000000..40cb7b1f86db --- /dev/null +++ b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.reverse.js @@ -0,0 +1,52 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isFloat16Array = require( '@stdlib/assert/is-float16array' ); +var pkg = require( './../package.json' ).name; +var Float16Array = require( './../lib' ); + + +// MAIN // + +bench( pkg+':reverse', function benchmark( b ) { + var out; + var arr; + var i; + + arr = new Float16Array( 2 ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + arr[ 0 ] = i; + out = arr.reverse(); + if ( typeof out !== 'object' ) { + b.fail( 'should return an object' ); + } + } + b.toc(); + if ( !isFloat16Array( out ) ) { + b.fail( 'should return a Float16Array' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.reverse.length.js b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.reverse.length.js new file mode 100644 index 000000000000..28cdc4e8bc69 --- /dev/null +++ b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.reverse.length.js @@ -0,0 +1,95 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var isFloat16Array = require( '@stdlib/assert/is-float16array' ); +var pkg = require( './../package.json' ).name; +var Float16Array = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var arr = new Float16Array( len ); + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var out; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + arr[ 0 ] = i; + out = arr.reverse(); + if ( typeof out !== 'object' ) { + b.fail( 'should return an object' ); + } + } + b.toc(); + if ( !isFloat16Array( out ) ) { + b.fail( 'should return a Float16Array' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var len; + var min; + var max; + var f; + var i; + + min = 1; // 10^min + max = 6; // 10^max + + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + f = createBenchmark( len ); + bench( pkg+':reverse:len='+len, f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.set.js b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.set.js new file mode 100644 index 000000000000..a9b65fe212b0 --- /dev/null +++ b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.set.js @@ -0,0 +1,94 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var pkg = require( './../package.json' ).name; +var Float16Array = require( './../lib' ); + + +// MAIN // + +bench( pkg+'::array:set', function benchmark( b ) { + var values; + var buf; + var arr; + var N; + var v; + var i; + + values = []; + for ( i = 0; i < 10; i++ ) { + values.push( i ); + } + N = values.length; + + arr = new Float16Array( 2 ); + buf = [ 0.0 ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + buf[ 0 ] = values[ i%N ]; + v = arr.set( buf ); + if ( typeof v !== 'undefined' ) { + b.fail( 'should return undefined' ); + } + } + b.toc(); + if ( typeof v !== 'undefined' ) { + b.fail( 'should return undefined' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::typed_array:set', function benchmark( b ) { + var values; + var buf; + var arr; + var N; + var v; + var i; + + values = new Float16Array( 20 ); + N = values.length; + for ( i = 0; i < N; i++ ) { + values[ i ] = i; + } + + arr = new Float16Array( 2 ); + buf = new Float16Array( 1 ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + buf[ 0 ] = values[ i%N ]; + v = arr.set( buf ); + if ( typeof v !== 'undefined' ) { + b.fail( 'should return undefined' ); + } + } + b.toc(); + if ( typeof v !== 'undefined' ) { + b.fail( 'should return undefined' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.set.length.js b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.set.length.js new file mode 100644 index 000000000000..9d9de7a9807d --- /dev/null +++ b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.set.length.js @@ -0,0 +1,114 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var randi = require( '@stdlib/random/base/randi' ); +var pkg = require( './../package.json' ).name; +var Float16Array = require( './../lib/polyfill.js' ); + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var values; + var arr1; + var arr2; + var arr; + var N; + var i; + + arr1 = []; + arr2 = []; + for ( i = 0; i < len; i++ ) { + arr1.push( randi() ); + arr2.push( randi() ); + } + arr = new Float16Array( len ); + + values = [ + arr1, + arr2 + ]; + N = values.length; + + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var v; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = arr.set( values[ i%N ] ); + if ( typeof v !== 'undefined' ) { + b.fail( 'should return undefined' ); + } + } + b.toc(); + if ( typeof v !== 'undefined' ) { + b.fail( 'should return undefined' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var len; + var min; + var max; + var f; + var i; + + min = 1; // 10^min + max = 6; // 10^max + + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + f = createBenchmark( len ); + bench( pkg+':set:len='+len, f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.slice.js b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.slice.js new file mode 100644 index 000000000000..8a85d40bd3ca --- /dev/null +++ b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.slice.js @@ -0,0 +1,52 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isFloat16Array = require( '@stdlib/assert/is-float16array' ); +var pkg = require( './../package.json' ).name; +var Float16Array = require( './../lib' ); + + +// MAIN // + +bench( pkg+':slice', function benchmark( b ) { + var out; + var arr; + var i; + + arr = new Float16Array( 2 ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + arr[ 0 ] = i; + out = arr.slice(); + if ( typeof out !== 'object' ) { + b.fail( 'should return an object' ); + } + } + b.toc(); + if ( !isFloat16Array( out ) ) { + b.fail( 'should return a Float16Array' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.slice.length.js b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.slice.length.js new file mode 100644 index 000000000000..a0859b021d78 --- /dev/null +++ b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.slice.length.js @@ -0,0 +1,95 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var isFloat16Array = require( '@stdlib/assert/is-float16array' ); +var pkg = require( './../package.json' ).name; +var Float16Array = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var arr = new Float16Array( len ); + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var out; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + arr[ 0 ] = i; + out = arr.slice(); + if ( typeof out !== 'object' ) { + b.fail( 'should return an object' ); + } + } + b.toc(); + if ( !isFloat16Array( out ) ) { + b.fail( 'should return a Float16Array' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var len; + var min; + var max; + var f; + var i; + + min = 1; // 10^min + max = 6; // 10^max + + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + f = createBenchmark( len ); + bench( pkg+':slice:len='+len, f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.some.js b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.some.js new file mode 100644 index 000000000000..df8b7323d49e --- /dev/null +++ b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.some.js @@ -0,0 +1,81 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var Float16Array = require( './../lib/polyfill.js' ); + + +// MAIN // + +bench( pkg+':some', function benchmark( b ) { + var bool; + var arr; + var i; + + arr = new Float16Array( 2 ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = arr.some( predicate ); + if ( typeof bool !== 'boolean' ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); + + function predicate( v ) { + return v > 0.0; + } +}); + +bench( pkg+'::this_context:some', function benchmark( b ) { + var bool; + var arr; + var i; + + arr = new Float16Array( 2 ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = arr.some( predicate, {} ); + if ( typeof bool !== 'boolean' ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); + + function predicate( v ) { + return v > 0.0; + } +}); diff --git a/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.some.length.js b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.some.length.js new file mode 100644 index 000000000000..d88927ada099 --- /dev/null +++ b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.some.length.js @@ -0,0 +1,105 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var Float16Array = require( './../lib/polyfill.js' ); + + +// FUNCTIONS // + +/** +* Returns a boolean indicating whether an array element passes a test. +* +* @private +* @param {*} value - value to test +* @returns {boolean} boolean indicating whether an array element passes a test +*/ +function predicate( value ) { + return value > 0.0; +} + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var arr = new Float16Array( len ); + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var bool; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = arr.some( predicate ); + if ( typeof bool !== 'boolean' ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var len; + var min; + var max; + var f; + var i; + + min = 1; // 10^min + max = 6; // 10^max + + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + f = createBenchmark( len ); + bench( pkg+':some:len='+len, f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.sort.js b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.sort.js new file mode 100644 index 000000000000..e9b021455231 --- /dev/null +++ b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.sort.js @@ -0,0 +1,53 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var randi = require( '@stdlib/random/base/randi' ); +var isFloat16Array = require( '@stdlib/assert/is-float16array' ); +var pkg = require( './../package.json' ).name; +var Float16Array = require( './../lib' ); + + +// MAIN // + +bench( pkg+':sort', function benchmark( b ) { + var out; + var arr; + var i; + + arr = new Float16Array( [ randi(), randi() ] ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + arr[ 0 ] = randi(); + out = arr.sort(); + if ( typeof out !== 'object' ) { + b.fail( 'should return an object' ); + } + } + b.toc(); + if ( !isFloat16Array( out ) ) { + b.fail( 'should return a Float16Array' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.sort.length.js b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.sort.length.js new file mode 100644 index 000000000000..590a46b516b3 --- /dev/null +++ b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.sort.length.js @@ -0,0 +1,105 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var randi = require( '@stdlib/random/base/randi' ); +var isFloat16Array = require( '@stdlib/assert/is-float16array' ); +var pkg = require( './../package.json' ).name; +var Float16Array = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var data; + var arr; + var i; + + data = []; + for ( i = 0; i < len; i++ ) { + data.push( randi() ); + } + arr = new Float16Array( data ); + + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var out; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + arr[ i%len ] = randi(); + out = arr.sort(); + if ( typeof out !== 'object' ) { + b.fail( 'should return an object' ); + } + } + b.toc(); + if ( !isFloat16Array( out ) ) { + b.fail( 'should return a Float16Array' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var len; + var min; + var max; + var f; + var i; + + min = 1; // 10^min + max = 5; // 10^max + + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + f = createBenchmark( len ); + bench( pkg+':sort:len='+len, f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.subarray.js b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.subarray.js new file mode 100644 index 000000000000..c53df7bebba3 --- /dev/null +++ b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.subarray.js @@ -0,0 +1,51 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isFloat16Array = require( '@stdlib/assert/is-float16array' ); +var pkg = require( './../package.json' ).name; +var Float16Array = require( './../lib/polyfill.js' ); + + +// MAIN // + +bench( pkg+':subarray', function benchmark( b ) { + var out; + var arr; + var i; + + arr = new Float16Array( 2 ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + out = arr.subarray(); + if ( typeof out !== 'object' ) { + b.fail( 'should return an object' ); + } + } + b.toc(); + if ( !isFloat16Array( out ) ) { + b.fail( 'should return a Float16Array' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.subarray.length.js b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.subarray.length.js new file mode 100644 index 000000000000..01a22d71f180 --- /dev/null +++ b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.subarray.length.js @@ -0,0 +1,94 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var isFloat16Array = require( '@stdlib/assert/is-float16array' ); +var pkg = require( './../package.json' ).name; +var Float16Array = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var arr = new Float16Array( len ); + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var out; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + out = arr.subarray(); + if ( typeof out !== 'object' ) { + b.fail( 'should return an object' ); + } + } + b.toc(); + if ( !isFloat16Array( out ) ) { + b.fail( 'should return a Float16Array' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var len; + var min; + var max; + var f; + var i; + + min = 1; // 10^min + max = 6; // 10^max + + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + f = createBenchmark( len ); + bench( pkg+':subarray:len='+len, f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.to_locale_string.js b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.to_locale_string.js new file mode 100644 index 000000000000..e00a683d1b16 --- /dev/null +++ b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.to_locale_string.js @@ -0,0 +1,51 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var pkg = require( './../package.json' ).name; +var Float16Array = require( './../lib' ); + + +// MAIN // + +bench( pkg+':toLocaleString', function benchmark( b ) { + var out; + var arr; + var i; + + arr = new Float16Array( 2 ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + arr[ 0 ] = i; + out = arr.toLocaleString(); + if ( typeof out !== 'string' ) { + b.fail( 'should return a string' ); + } + } + b.toc(); + if ( typeof out !== 'string' ) { + b.fail( 'should return a string' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.to_locale_string.length.js b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.to_locale_string.length.js new file mode 100644 index 000000000000..ac8fd111e353 --- /dev/null +++ b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.to_locale_string.length.js @@ -0,0 +1,94 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var pkg = require( './../package.json' ).name; +var Float16Array = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var arr = new Float16Array( len ); + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var out; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + arr[ 0 ] = i; + out = arr.toLocaleString(); + if ( typeof out !== 'string' ) { + b.fail( 'should return a string' ); + } + } + b.toc(); + if ( typeof out !== 'string' ) { + b.fail( 'should return a string' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var len; + var min; + var max; + var f; + var i; + + min = 1; // 10^min + max = 5; // 10^max + + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + f = createBenchmark( len ); + bench( pkg+':toLocaleString:len='+len, f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.to_string.js b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.to_string.js new file mode 100644 index 000000000000..9156b61dc86c --- /dev/null +++ b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.to_string.js @@ -0,0 +1,51 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var pkg = require( './../package.json' ).name; +var Float16Array = require( './../lib' ); + + +// MAIN // + +bench( pkg+':toString', function benchmark( b ) { + var out; + var arr; + var i; + + arr = new Float16Array( 2 ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + arr[ 0 ] = i; + out = arr.toString(); + if ( typeof out !== 'string' ) { + b.fail( 'should return a string' ); + } + } + b.toc(); + if ( typeof out !== 'string' ) { + b.fail( 'should return a string' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.to_string.length.js b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.to_string.length.js new file mode 100644 index 000000000000..da63ec444db6 --- /dev/null +++ b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.to_string.length.js @@ -0,0 +1,94 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var pkg = require( './../package.json' ).name; +var Float16Array = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var arr = new Float16Array( len ); + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var out; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + arr[ 0 ] = i; + out = arr.toString(); + if ( typeof out !== 'string' ) { + b.fail( 'should return a string' ); + } + } + b.toc(); + if ( typeof out !== 'string' ) { + b.fail( 'should return a string' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var len; + var min; + var max; + var f; + var i; + + min = 1; // 10^min + max = 5; // 10^max + + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + f = createBenchmark( len ); + bench( pkg+':toString:len='+len, f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.values.js b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.values.js new file mode 100644 index 000000000000..52caf4535cbb --- /dev/null +++ b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.values.js @@ -0,0 +1,50 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var pkg = require( './../package.json' ).name; +var Float16Array = require( './../lib/polyfill.js' ); + + +// MAIN // + +bench( pkg+':values', function benchmark( b ) { + var iter; + var arr; + var i; + + arr = new Float16Array( 2 ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + iter = arr.values(); + if ( typeof iter !== 'object' ) { + b.fail( 'should return an object' ); + } + } + b.toc(); + if ( typeof iter !== 'object' || typeof iter.next !== 'function' ) { + b.fail( 'should return an iterator protocol-compliant object' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/lib/node_modules/@stdlib/array/float16/docs/repl.txt b/lib/node_modules/@stdlib/array/float16/docs/repl.txt new file mode 100644 index 000000000000..9a39d53fada5 --- /dev/null +++ b/lib/node_modules/@stdlib/array/float16/docs/repl.txt @@ -0,0 +1,959 @@ + +{{alias}}() + A typed array constructor which returns a typed array representing an array + of single-precision floating-point numbers in the platform byte order. + + Returns + ------- + out: Float16Array + A typed array. + + Examples + -------- + > var arr = new {{alias}}() + + + +{{alias}}( length ) + Returns a typed array having a specified length. + + Parameters + ---------- + length: integer + Typed array length. + + Returns + ------- + out: Float16Array + A typed array. + + Examples + -------- + > var arr = new {{alias}}( 5 ) + [ 0.0, 0.0, 0.0, 0.0, 0.0 ] + + +{{alias}}( typedarray ) + Creates a typed array from another typed array. + + Parameters + ---------- + typedarray: TypedArray + Typed array from which to generate another typed array. + + Returns + ------- + out: Float16Array + A typed array. + + Examples + -------- + > var arr1 = new {{alias:@stdlib/array/float64}}( [ 0.5, 0.5, 0.5 ] ); + > var arr2 = new {{alias}}( arr1 ) + [ 0.5, 0.5, 0.5 ] + + +{{alias}}( obj ) + Creates a typed array from an array-like object or iterable. + + Parameters + ---------- + obj: Object + Array-like object or iterable from which to generate a typed array. + + Returns + ------- + out: Float16Array + A typed array. + + Examples + -------- + > var arr1 = [ 0.5, 0.5, 0.5 ]; + > var arr2 = new {{alias}}( arr1 ) + [ 0.5, 0.5, 0.5 ] + + +{{alias}}( buffer[, byteOffset[, length]] ) + Returns a typed array view of an ArrayBuffer. + + Parameters + ---------- + buffer: ArrayBuffer + Underlying ArrayBuffer. + + byteOffset: integer (optional) + Integer byte offset specifying the location of the first typed array + element. Default: 0. + + length: integer (optional) + View length. If not provided, the view spans from the byteOffset to + the end of the underlying ArrayBuffer. + + Returns + ------- + out: Float16Array + A typed array. + + Examples + -------- + > var buf = new {{alias:@stdlib/array/buffer}}( 16 ); + > var arr = new {{alias}}( buf, 0, 4 ) + [ 0.0, 0.0, 0.0, 0.0 ] + + +{{alias}}.from( src[, map[, thisArg]] ) + Creates a new typed array from an array-like object or an iterable. + + A callback is provided the following arguments: + + - value: source value. + - index: source index. + + Parameters + ---------- + src: ArrayLike|Iterable + Source of array elements. + + map: Function (optional) + Callback to invoke for each source element. + + thisArg: Any (optional) + Callback execution context. + + Returns + ------- + out: Float16Array + A typed array. + + Examples + -------- + > function mapFcn( v ) { return v * 2.0; }; + > var arr = {{alias}}.from( [ 1.0, -1.0 ], mapFcn ) + [ 2.0, -2.0 ] + + +{{alias}}.of( element0[, element1[, ...elementN]] ) + Creates a new typed array from a variable number of arguments. + + Parameters + ---------- + element0: number + Array element. + + element1: number (optional) + Array element. + + elementN: ...number (optional) + Array elements. + + Returns + ------- + out: Float16Array + A typed array. + + Examples + -------- + > var arr = {{alias}}.of( 2.0, -2.0 ) + [ 2.0, -2.0 ] + + +{{alias}}.BYTES_PER_ELEMENT + Number of bytes per view element. + + Examples + -------- + > {{alias}}.BYTES_PER_ELEMENT + 4 + + +{{alias}}.name + Typed array constructor name. + + Examples + -------- + > {{alias}}.name + 'Float16Array' + + +{{alias}}.prototype.buffer + Read-only property which returns the ArrayBuffer referenced by the typed + array. + + Examples + -------- + > var arr = new {{alias}}( 5 ); + > arr.buffer + + + +{{alias}}.prototype.byteLength + Read-only property which returns the length (in bytes) of the typed array. + + Examples + -------- + > var arr = new {{alias}}( 5 ); + > arr.byteLength + 20 + + +{{alias}}.prototype.byteOffset + Read-only property which returns the offset (in bytes) of the typed array + from the start of its ArrayBuffer. + + Examples + -------- + > var arr = new {{alias}}( 5 ); + > arr.byteOffset + 0 + + +{{alias}}.prototype.BYTES_PER_ELEMENT + Number of bytes per view element. + + Examples + -------- + > var arr = new {{alias}}( 5 ); + > arr.BYTES_PER_ELEMENT + 4 + + +{{alias}}.prototype.length + Read-only property which returns the number of view elements. + + Examples + -------- + > var arr = new {{alias}}( 5 ); + > arr.length + 5 + + +{{alias}}.prototype.copyWithin( target, start[, end] ) + Copies a sequence of elements within the array starting at `start` and + ending at `end` (non-inclusive) to the position starting at `target`. + + Parameters + ---------- + target: integer + Target start index position. + + start: integer + Source start index position. + + end: integer (optional) + Source end index position. Default: out.length. + + Returns + ------- + out: Float16Array + Modified array. + + Examples + -------- + > var arr = new {{alias}}( [ 2.0, -2.0, 1.0, -1.0, 1.0 ] ); + > arr.copyWithin( 3, 0, 2 ); + > arr[ 3 ] + 2.0 + > arr[ 4 ] + -2.0 + + +{{alias}}.prototype.entries() + Returns an iterator for iterating over array key-value pairs. + + Returns + ------- + iter: Iterator + Iterator for iterating over array key-value pairs. + + Examples + -------- + > var arr = new {{alias}}( [ 1.0, -1.0 ] ); + > it = arr.entries(); + > it.next().value + [ 0, 1.0 ] + > it.next().value + [ 1, -1.0 ] + > it.next().done + true + + +{{alias}}.prototype.every( predicate[, thisArg] ) + Tests whether all array elements pass a test implemented by a predicate + function. + + A predicate function is provided the following arguments: + + - value: array element. + - index: array index. + - arr: array on which the method is invoked. + + Parameters + ---------- + predicate: Function + Predicate function which tests array elements. If a predicate function + returns a truthy value, an array element passes; otherwise, an array + element fails. + + thisArg: Any (optional) + Callback execution context. + + Returns + ------- + bool: boolean + Boolean indicating whether all array elements pass. + + Examples + -------- + > var arr = new {{alias}}( [ 1.0, -1.0 ] ); + > function predicate( v ) { return ( v >= 0.0 ); }; + > arr.every( predicate ) + false + + +{{alias}}.prototype.fill( value[, start[, end]] ) + Fills an array from a start index to an end index (non-inclusive) with a + provided value. + + Parameters + ---------- + value: number + Fill value. + + start: integer (optional) + Start index. If less than zero, the start index is resolved relative to + the last array element. Default: 0. + + end: integer (optional) + End index (non-inclusive). If less than zero, the end index is resolved + relative to the last array element. Default: out.length. + + Returns + ------- + out: Float16Array + Modified array. + + Examples + -------- + > var arr = new {{alias}}( [ 1.0, -1.0 ] ); + > arr.fill( 2.0 ); + > arr[ 0 ] + 2.0 + > arr[ 1 ] + 2.0 + + +{{alias}}.prototype.filter( predicate[, thisArg] ) + Creates a new array which includes those elements for which a predicate + function returns a truthy value. + + A predicate function is provided the following arguments: + + - value: array element. + - index: array index. + - arr: array on which the method is invoked. + + The returned array has the same data type as the host array. + + If a predicate function does not return a truthy value for any array + element, the method returns `null`. + + Parameters + ---------- + predicate: Function + Predicate function which filters array elements. If a predicate function + returns a truthy value, an array element is included in the output + array; otherwise, an array element is not included in the output array. + + thisArg: Any (optional) + Callback execution context. + + Returns + ------- + out: Float16Array + A typed array. + + Examples + -------- + > var arr1 = new {{alias}}( [ 1.0, 0.0, -1.0 ] ); + > function predicate( v ) { return ( v >= 0.0 ); }; + > var arr2 = arr1.filter( predicate ); + > arr2.length + 2 + + +{{alias}}.prototype.find( predicate[, thisArg] ) + Returns the first array element for which a provided predicate function + returns a truthy value. + + A predicate function is provided the following arguments: + + - value: array element. + - index: array index. + - arr: array on which the method is invoked. + + If a predicate function never returns a truthy value, the method returns + `undefined`. + + Parameters + ---------- + predicate: Function + Predicate function which tests array elements. + + thisArg: Any (optional) + Callback execution context. + + Returns + ------- + value: number|undefined + Array element. + + Examples + -------- + > var arr = new {{alias}}( [ 1.0, 0.0, -1.0 ] ); + > function predicate( v ) { return ( v < 0.0 ); }; + > var v = arr.find( predicate ) + -1.0 + + +{{alias}}.prototype.findIndex( predicate[, thisArg] ) + Returns the index of the first array element for which a provided predicate + function returns a truthy value. + + A predicate function is provided the following arguments: + + - value: array element. + - index: array index. + - arr: array on which the method is invoked. + + If a predicate function never returns a truthy value, the method returns + `-1`. + + Parameters + ---------- + predicate: Function + Predicate function which tests array elements. + + thisArg: Any (optional) + Callback execution context. + + Returns + ------- + idx: integer + Array index. + + Examples + -------- + > var arr = new {{alias}}( [ 1.0, 0.0, -1.0 ] ); + > function predicate( v ) { return ( v < 0.0 ); }; + > var idx = arr.findIndex( predicate ) + 2 + + +{{alias}}.prototype.forEach( fcn[, thisArg] ) + Invokes a callback for each array element. + + A provided function is provided the following arguments: + + - value: array element. + - index: array index. + - arr: array on which the method is invoked. + + Parameters + ---------- + fcn: Function + Function to invoke for each array element. + + thisArg: Any (optional) + Callback execution context. + + Examples + -------- + > var arr = new {{alias}}( [ 1.0, 0.0, -1.0 ] ); + > var str = ' '; + > function fcn( v, i ) { str += i + ':' + v + ' '; }; + > arr.forEach( fcn ); + > str + ' 0:1 1:0 2:-1 ' + + +{{alias}}.prototype.includes( searchElement[, fromIndex] ) + Returns a boolean indicating whether an array includes a search element. + + The method does not distinguish between signed and unsigned zero. + + Parameters + ---------- + searchElement: number + Search element. + + fromIndex: integer (optional) + Array index from which to begin searching. If provided a negative value, + the method resolves the start index relative to the last array element. + Default: 0. + + Returns + ------- + bool: boolean + Boolean indicating whether an array includes a search element. + + Examples + -------- + > var arr = new {{alias}}( [ 1.0, 0.0, -1.0 ] ); + > var bool = arr.includes( 2.0 ) + false + > bool = arr.includes( -1.0 ) + true + + +{{alias}}.prototype.indexOf( searchElement[, fromIndex] ) + Returns the index of the first array element strictly equal to a search + element. + + The method does not distinguish between signed and unsigned zero. + + If unable to locate a search element, the method returns `-1`. + + Parameters + ---------- + searchElement: number + Search element. + + fromIndex: integer (optional) + Array index from which to begin searching. If provided a negative value, + the method resolves the start index relative to the last array element. + Default: 0. + + Returns + ------- + idx: integer + Array index. + + Examples + -------- + > var arr = new {{alias}}( [ 1.0, 0.0, -1.0 ] ); + > var idx = arr.indexOf( 2.0 ) + -1 + > idx = arr.indexOf( -1.0 ) + 2 + + +{{alias}}.prototype.join( [separator] ) + Serializes an array by joining all array elements as a string. + + Parameters + ---------- + separator: string (optional) + String delineating array elements. Default: ','. + + Returns + ------- + str: string + Array serialized as a string. + + Examples + -------- + > var arr = new {{alias}}( [ 1.0, 0.0, -1.0 ] ); + > arr.join( '|' ) + '1|0|-1' + + +{{alias}}.prototype.keys() + Returns an iterator for iterating over array keys. + + Returns + ------- + iter: Iterator + Iterator for iterating over array keys. + + Examples + -------- + > var arr = new {{alias}}( [ 1.0, -1.0 ] ); + > it = arr.keys(); + > it.next().value + 0 + > it.next().value + 1 + > it.next().done + true + + +{{alias}}.prototype.lastIndexOf( searchElement[, fromIndex] ) + Returns the index of the last array element strictly equal to a search + element. + + The method iterates from the last array element to the first array element. + + The method does not distinguish between signed and unsigned zero. + + If unable to locate a search element, the method returns `-1`. + + Parameters + ---------- + searchElement: number + Search element. + + fromIndex: integer (optional) + Array index from which to begin searching. If provided a negative value, + the method resolves the start index relative to the last array element. + Default: -1. + + Returns + ------- + idx: integer + Array index. + + Examples + -------- + > var arr = new {{alias}}( [ 1.0, 0.0, -1.0, 0.0, 1.0 ] ); + > var idx = arr.lastIndexOf( 2.0 ) + -1 + > idx = arr.lastIndexOf( 0.0 ) + 3 + + +{{alias}}.prototype.map( fcn[, thisArg] ) + Maps each array element to an element in a new typed array. + + A provided function is provided the following arguments: + + - value: array element. + - index: array index. + - arr: array on which the method is invoked. + + The returned array has the same data type as the host array. + + Parameters + ---------- + fcn: Function + Function which maps array elements to elements in the new array. + + thisArg: Any (optional) + Callback execution context. + + Returns + ------- + out: Float16Array + A typed array. + + Examples + -------- + > var arr1 = new {{alias}}( [ 1.0, 0.0, -1.0 ] ); + > function fcn( v ) { return v * 2.0; }; + > var arr2 = arr1.map( fcn ) + [ 2.0, 0.0, -2.0 ] + + +{{alias}}.prototype.reduce( fcn[, initialValue] ) + Applies a function against an accumulator and each element in an array and + returns the accumulated result. + + The provided function is provided the following arguments: + + - acc: accumulated result. + - value: current array element. + - index: index of the current array element. + - arr: array on which the method is invoked. + + If provided an initial value, the method invokes a provided function with + the initial value as the first argument and the first array element as the + second argument. + + If not provided an initial value, the method invokes a provided function + with the first array element as the first argument and the second array + element as the second argument. + + Parameters + ---------- + fcn: Function + Function to apply. + + initialValue: Any (optional) + Initial accumulation value. + + Returns + ------- + out: Any + Accumulated result. + + Examples + -------- + > var arr = new {{alias}}( [ 1.0, 0.0, -1.0 ] ); + > function fcn( acc, v ) { return acc + (v*v); }; + > var v = arr.reduce( fcn, 0.0 ) + 2.0 + + +{{alias}}.prototype.reduceRight( fcn[, initialValue] ) + Applies a function against an accumulator and each element in an array and + returns the accumulated result, iterating from right to left. + + The provided function is provided the following arguments: + + - acc: accumulated result. + - value: current array element. + - index: index of the current array element. + - arr: array on which the method is invoked. + + If provided an initial value, the method invokes a provided function with + the initial value as the first argument and the last array element as the + second argument. + + If not provided an initial value, the method invokes a provided function + with the last array element as the first argument and the second-to-last + array element as the second argument. + + Parameters + ---------- + fcn: Function + Function to apply. + + initialValue: Any (optional) + Initial accumulation value. + + Returns + ------- + out: Any + Accumulated result. + + Examples + -------- + > var arr = new {{alias}}( [ 1.0, 0.0, -1.0 ] ); + > function fcn( acc, v ) { return acc + (v*v); }; + > var v = arr.reduceRight( fcn, 0.0 ) + 2.0 + + +{{alias}}.prototype.reverse() + Reverses an array *in-place*. + + This method mutates the array on which the method is invoked. + + Returns + ------- + out: Float16Array + Modified array. + + Examples + -------- + > var arr = new {{alias}}( [ 1.0, 0.0, -1.0 ] ) + + > arr.reverse() + [ -1.0, 0.0, 1.0 ] + + +{{alias}}.prototype.set( arr[, offset] ) + Sets array elements. + + Parameters + ---------- + arr: ArrayLike + Source array containing array values to set. + + offset: integer (optional) + Array index at which to start writing values. Default: 0. + + Examples + -------- + > var arr = new {{alias}}( [ 1.0, 0.0, -1.0 ] ); + > arr.set( [ -2.0, 2.0 ], 1 ); + > arr[ 1 ] + -2.0 + > arr[ 2 ] + 2.0 + + +{{alias}}.prototype.slice( [begin[, end]] ) + Copies array elements to a new array with the same underlying data type as + the host array. + + If the method is unable to resolve indices to a non-empty array subsequence, + the method returns `null`. + + Parameters + ---------- + begin: integer (optional) + Start element index (inclusive). If less than zero, the start index is + resolved relative to the last array element. Default: 0. + + end: integer (optional) + End element index (exclusive). If less than zero, the end index is + resolved relative to the last array element. Default: arr.length. + + Returns + ------- + out: Float16Array + A typed array. + + Examples + -------- + > var arr1 = new {{alias}}( [ 1.0, 0.0, -1.0 ] ); + > var arr2 = arr1.slice( 1 ); + > arr2.length + 2 + > arr2[ 0 ] + 0.0 + > arr2[ 1 ] + -1.0 + + +{{alias}}.prototype.some( predicate[, thisArg] ) + Tests whether at least one array element passes a test implemented by a + predicate function. + + A predicate function is provided the following arguments: + + - value: array element. + - index: array index. + - arr: array on which the method is invoked. + + Parameters + ---------- + predicate: Function + Predicate function which tests array elements. If a predicate function + returns a truthy value, a array element passes; otherwise, an array + element fails. + + thisArg: Any (optional) + Callback execution context. + + Returns + ------- + bool: boolean + Boolean indicating whether at least one array element passes. + + Examples + -------- + > var arr = new {{alias}}( [ 1.0, -1.0 ] ); + > function predicate( v ) { return ( v < 0.0 ); }; + > arr.some( predicate ) + true + + +{{alias}}.prototype.sort( [compareFunction] ) + Sorts an array *in-place*. + + The comparison function is provided two array elements per invocation: `a` + and `b`. + + The comparison function return value determines the sort order as follows: + + - If the comparison function returns a value less than zero, then the method + sorts `a` to an index lower than `b` (i.e., `a` should come *before* `b`). + + - If the comparison function returns a value greater than zero, then the + method sorts `a` to an index higher than `b` (i.e., `b` should come *before* + `a`). + + - If the comparison function returns zero, then the relative order of `a` + and `b` should remain unchanged. + + This method mutates the array on which the method is invoked. + + Parameters + ---------- + compareFunction: Function (optional) + Function which specifies the sort order. The default sort order is + ascending order. + + Returns + ------- + out: Float16Array + Modified array. + + Examples + -------- + > var arr = new {{alias}}( [ 1.0, -1.0, 0.0, 2.0, -2.0 ] ); + > arr.sort() + [ -2.0, -1.0, 0.0, 1.0, 2.0 ] + + +{{alias}}.prototype.subarray( [begin[, end]] ) + Creates a new typed array over the same underlying ArrayBuffer and with the + same underlying data type as the host array. + + If the method is unable to resolve indices to a non-empty array subsequence, + the method returns an empty typed array. + + Parameters + ---------- + begin: integer (optional) + Start element index (inclusive). If less than zero, the start index is + resolved relative to the last array element. Default: 0. + + end: integer (optional) + End element index (exclusive). If less than zero, the end index is + resolved relative to the last array element. Default: arr.length. + + Returns + ------- + out: Float16Array + A new typed array view. + + Examples + -------- + > var arr1 = new {{alias}}( [ 1.0, -1.0, 0.0, 2.0, -2.0 ] ); + > var arr2 = arr1.subarray( 2 ) + [ 0.0, 2.0, -2.0 ] + + +{{alias}}.prototype.toLocaleString( [locales[, options]] ) + Serializes an array as a locale-specific string. + + Parameters + ---------- + locales: string|Array (optional) + A BCP 47 language tag, or an array of such tags. + + options: Object (optional) + Options. + + Returns + ------- + str: string + A typed array string representation. + + Examples + -------- + > var arr = new {{alias}}( [ 1.0, -1.0, 0.0 ] ); + > arr.toLocaleString() + '1,-1,0' + + +{{alias}}.prototype.toString() + Serializes an array as a string. + + Returns + ------- + str: string + A typed array string representation. + + Examples + -------- + > var arr = new {{alias}}( [ 1.0, -1.0, 0.0 ] ); + > arr.toString() + '1,-1,0' + + +{{alias}}.prototype.values() + Returns an iterator for iterating over array elements. + + Returns + ------- + iter: Iterator + Iterator for iterating over array elements. + + Examples + -------- + > var arr = new {{alias}}( [ 1.0, -1.0 ] ); + > it = arr.values(); + > it.next().value + 1.0 + > it.next().value + -1.0 + > it.next().done + true + + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/array/float16/docs/types/index.d.ts b/lib/node_modules/@stdlib/array/float16/docs/types/index.d.ts new file mode 100644 index 000000000000..1dbbcdebaaf6 --- /dev/null +++ b/lib/node_modules/@stdlib/array/float16/docs/types/index.d.ts @@ -0,0 +1,26 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 4.1 + +// EXPORTS // + +/** +* Typed array constructor which returns a typed array representing an array of half-precision floating-point numbers in the platform byte order. +*/ +export = Float16Array; diff --git a/lib/node_modules/@stdlib/array/float16/docs/types/test.ts b/lib/node_modules/@stdlib/array/float16/docs/types/test.ts new file mode 100644 index 000000000000..5d2ceb4cdfaf --- /dev/null +++ b/lib/node_modules/@stdlib/array/float16/docs/types/test.ts @@ -0,0 +1,36 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable @typescript-eslint/no-unused-expressions */ + +import Float16Array = require( './index' ); + + +// TESTS // + +// The function returns a typed array instance... +{ + new Float16Array( 10 ); // $ExpectType Float16Array + new Float16Array( [ 2.1, 3.9, 5.2, 7.3 ] ); // $ExpectType Float16Array +} + +// The constructor function has to be invoked with `new`... +{ + Float16Array( 10 ); // $ExpectError + Float16Array( [ 2.1, 3.9, 5.2, 7.3 ] ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/array/float16/examples/index.js b/lib/node_modules/@stdlib/array/float16/examples/index.js new file mode 100644 index 000000000000..1653629b4680 --- /dev/null +++ b/lib/node_modules/@stdlib/array/float16/examples/index.js @@ -0,0 +1,31 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var randu = require( '@stdlib/random/base/randu' ); +var ctor = require( './../lib' ); + +var arr; +var i; + +arr = new ctor( 10 ); +for ( i = 0; i < arr.length; i++ ) { + arr[ i ] = randu() * 100.0; +} +console.log( arr ); diff --git a/lib/node_modules/@stdlib/array/float16/lib/index.js b/lib/node_modules/@stdlib/array/float16/lib/index.js new file mode 100644 index 000000000000..e7c13c3a4f93 --- /dev/null +++ b/lib/node_modules/@stdlib/array/float16/lib/index.js @@ -0,0 +1,52 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Typed array constructor which returns a typed array representing an array of half-precision floating-point numbers in the platform byte order. +* +* @module @stdlib/array/float16 +* +* @example +* var ctor = require( '@stdlib/array/float16' ); +* +* var arr = new ctor( 10 ); +* // returns +*/ + +// MODULES // + +var hasFloat16ArraySupport = require( '@stdlib/assert/has-float16array-support' ); +var builtin = require( './main.js' ); +var polyfill = require( './polyfill.js' ); + + +// MAIN // + +var ctor; +if ( hasFloat16ArraySupport() ) { + ctor = builtin; +} else { + ctor = polyfill; +} + + +// EXPORTS // + +module.exports = ctor; diff --git a/lib/node_modules/@stdlib/array/float16/lib/main.js b/lib/node_modules/@stdlib/array/float16/lib/main.js new file mode 100644 index 000000000000..9c7250c3f661 --- /dev/null +++ b/lib/node_modules/@stdlib/array/float16/lib/main.js @@ -0,0 +1,28 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MAIN // + +var ctor = ( typeof Float16Array === 'function' ) ? Float16Array : void 0; // eslint-disable-line stdlib/require-globals + + +// EXPORTS // + +module.exports = ctor; diff --git a/lib/node_modules/@stdlib/array/float16/lib/polyfill.js b/lib/node_modules/@stdlib/array/float16/lib/polyfill.js new file mode 100644 index 000000000000..498388d5a0cd --- /dev/null +++ b/lib/node_modules/@stdlib/array/float16/lib/polyfill.js @@ -0,0 +1,538 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MAIN // + +class Float16Array { + constructor(input) { + if (typeof input === 'number') { + this._buffer = new Uint16Array(input); + } else if (Array.isArray(input)) { + this._buffer = new Uint16Array(input.length); + for (let i = 0; i < input.length; i++) { + this._buffer[i] = Float16Array.toFloat16(input[i]); + } + } else { + throw new TypeError('Unsupported constructor argument'); + } + this.length = this._buffer.length; + } + + get(index) { + return Float16Array.fromFloat16(this._buffer[index]); + } + + get buffer() { + return this._buffer.buffer; + } + + get byteLength() { + return this._buffer.byteLength; + } + + get byteOffset() { + return this._buffer.byteOffset; + } + + get BYTES_PER_ELEMENT() { + return 2; + } + + get [Symbol.toStringTag]() { + return 'Float16Array'; + } + + copyWithin(target, start, end) { + target = target >> 0; + start = start >> 0; + end = end === undefined ? this.length : end >> 0; + + const to = target < 0 ? this.length + target : target; + const from = start < 0 ? this.length + start : start; + const final = end < 0 ? this.length + end : end; + + const count = Math.min(final - from, this.length - to); + const direction = from < to && to < (from + count) ? -1 : 1; + + if (direction === -1) { + for (let i = count - 1; i >= 0; i--) { + this._buffer[to + i] = this._buffer[from + i]; + } + } else { + for (let i = 0; i < count; i++) { + this._buffer[to + i] = this._buffer[from + i]; + } + } + return this; + } + + set(indexOrArray, value) { + if (typeof indexOrArray === 'number') { + if (indexOrArray < 0 || indexOrArray >= this.length) { + throw new RangeError('Index out of bounds'); + } + this._buffer[indexOrArray] = Float16Array.toFloat16(value); + return; + } + + const source = indexOrArray; + const offset = value !== undefined ? Number(value) : 0; + + if (offset < 0 || offset > this.length) { + throw new RangeError('Offset is out of bounds'); + } + + if ( + (Array.isArray(source) || + (ArrayBuffer.isView(source) && typeof source.length === 'number') || + (source instanceof Float16Array)) + ) { + const len = Math.min(source.length, this.length - offset); + for (let i = 0; i < len; i++) { + const val = typeof source.get === 'function' ? source.get(i) : source[i]; + this._buffer[offset + i] = Float16Array.toFloat16(val); + } + return; + } + + throw new TypeError('Invalid arguments for set'); + } + + entries() { + return this._entriesGenerator(); +} + +*_entriesGenerator() { + for (let i = 0; i < this.length; i++) { + yield [i, this.get(i)]; + } +} + +every(callback, thisArg) { + if (typeof callback !== 'function') { + throw new TypeError('callback must be a function'); + } + for (let i = 0; i < this.length; i++) { + const value = this.get(i); + if (!callback.call(thisArg, value, i, this)) { + return false; + } + } + return true; +} + +filter(callback, thisArg) { + if (typeof callback !== 'function') { + throw new TypeError('callback must be a function'); + } + + const result = []; + for (let i = 0; i < this.length; i++) { + const value = this.get(i); + if (callback.call(thisArg, value, i, this)) { + result.push(value); + } + } + + return new Float16Array(result); +} + +findIndex(callback, thisArg) { + if (typeof callback !== 'function') { + throw new TypeError('callback must be a function'); + } + + for (let i = 0; i < this.length; i++) { + const value = this.get(i); + if (callback.call(thisArg, value, i, this)) { + return i; + } + } + return -1; +} + +find(callback, thisArg) { + if (typeof callback !== 'function') { + throw new TypeError('callback must be a function'); + } + + for (let i = 0; i < this.length; i++) { + const value = this.get(i); + if (callback.call(thisArg, value, i, this)) { + return value; + } + } + return undefined; +} + +forEach(callback, thisArg) { + if (typeof callback !== 'function') { + throw new TypeError('callback must be a function'); + } + for (let i = 0; i < this.length; i++) { + callback.call(thisArg, this.get(i), i, this); + } +} + +static from(source, mapFn, thisArg) { + if (source == null) { + throw new TypeError('source cannot be null or undefined'); + } + if (mapFn !== undefined && typeof mapFn !== 'function') { + throw new TypeError('when provided, mapFn must be a function'); + } + + let values = []; + const iteratorSymbol = typeof Symbol === 'function' && Symbol.iterator; + + if (iteratorSymbol && source[iteratorSymbol]) { + const iterator = source[iteratorSymbol](); + let step; + while (!(step = iterator.next()).done) { + values.push(step.value); + } + } else if (typeof source.length === 'number') { + for (let i = 0; i < source.length; i++) { + values.push(source[i]); + } + } else { + throw new TypeError('source is not iterable or array-like'); + } + + if (mapFn) { + if (thisArg) { + values = values.map(function(v, i) { + return mapFn.call(thisArg, v, i); + }); + } else { + values = values.map(mapFn); + } + } + + const arr = new Float16Array(values.length); + for (let i = 0; i < values.length; i++) { + arr.set(i, values[i]); + } + return arr; +} + +includes(searchElement, fromIndex = 0) { + if (fromIndex < 0) { + fromIndex = Math.max(this.length + fromIndex, 0); + } + for (let i = fromIndex; i < this.length; i++) { + if (this.get(i) === searchElement) { + return true; + } + } + return false; +} + +indexOf(searchElement, fromIndex = 0) { + fromIndex = Math.max(fromIndex >= 0 ? fromIndex : this.length + fromIndex, 0); + for (let i = fromIndex; i < this.length; i++) { + if (this.get(i) === searchElement) { + return i; + } + } + return -1; +} + +join(separator = ',') { + let result = ''; + for (let i = 0; i < this.length; i++) { + if (i > 0) result += separator; + result += String(this.get(i)); + } + return result; +} + +keys() { + return this._keysGenerator(); +} + +*_keysGenerator() { + for (let i = 0; i < this.length; i++) { + yield i; + } +} + +lastIndexOf(searchElement, fromIndex) { + let i = fromIndex !== undefined ? fromIndex : this.length - 1; + if (i >= this.length) i = this.length - 1; + if (i < 0) i = this.length + i; + + for (; i >= 0; i--) { + if (Object.is(this.get(i), searchElement)) { + return i; + } + } + return -1; +} + +map(callback, thisArg) { + if (typeof callback !== 'function') { + throw new TypeError(callback + ' is not a function'); + } + + const result = new this.constructor(this.length); + + for (let i = 0; i < this.length; i++) { + if (Object.prototype.hasOwnProperty.call(this, i)) { + const mappedValue = callback.call(thisArg, this.get(i), i, this); + result.set(i, mappedValue); + } + } + + return result; +} + +slice(start = 0, end = this.length) { + const len = this.length; + + let relativeStart = start < 0 ? Math.max(len + start, 0) : Math.min(start, len); + let relativeEnd = end < 0 ? Math.max(len + end, 0) : Math.min(end, len); + + const count = Math.max(relativeEnd - relativeStart, 0); + const result = new this.constructor(count); + + for (let i = 0; i < count; i++) { + result.set(i, this.get(relativeStart + i)); + } + return result; +} + +sort(compareFn) { + const values = []; + for (let i = 0; i < this.length; i++) { + values.push(this.get(i)); + } + + values.sort((a, b) => { + if (compareFn) return compareFn(a, b); + if (a !== a) return 1; + if (b !== b) return -1; + return a - b; + }); + + for (let i = 0; i < this.length; i++) { + this.set(i, values[i]); + } + return this; +} + +values() { + return this._valuesGenerator(); +} + +*_valuesGenerator() { + for (let i = 0; i < this.length; i++) { + yield this.get(i); + } +} + +some(callback, thisArg) { + if (typeof callback !== 'function') { + throw new TypeError(callback + ' is not a function'); + } + for (let i = 0; i < this.length; i++) { + const value = this.get(i); + if (callback.call(thisArg, value, i, this)) { + return true; + } + } + return false; +} + +reduce(callback, initialValue) { + const length = this.length; + if (typeof callback !== 'function') { + throw new TypeError(callback + ' is not a function'); + } + + let accumulator; + let startIndex = 0; + + if (arguments.length >= 2) { + accumulator = initialValue; + } else { + if (length === 0) { + throw new TypeError('Reduce of empty array with no initial value'); + } + accumulator = this.get(0); + startIndex = 1; + } + + for (let i = startIndex; i < length; i++) { + accumulator = callback(accumulator, this.get(i), i, this); + } + + return accumulator; +} + +reduceRight(callback, initialValue) { + const length = this.length; + if (typeof callback !== 'function') { + throw new TypeError(callback + ' is not a function'); + } + + let accumulator; + let startIndex = length - 1; + + if (arguments.length >= 2) { + accumulator = initialValue; + } else { + if (length === 0) { + throw new TypeError('Reduce of empty array with no initial value'); + } + accumulator = this.get(startIndex); + startIndex--; + } + + for (let i = startIndex; i >= 0; i--) { + accumulator = callback(accumulator, this.get(i), i, this); + } + + return accumulator; +} + +subarray(begin = 0, end = this.length) { + const len = this.length; + + let start = begin < 0 ? Math.max(len + begin, 0) : Math.min(begin, len); + let finish = end < 0 ? Math.max(len + end, 0) : Math.min(end, len); + + start = Math.min(start, len); + finish = Math.min(Math.max(finish, start), len); + + const count = Math.max(finish - start, 0); + const SubArrayCtor = this.constructor; + + const result = new SubArrayCtor(count); + + for (let i = 0; i < count; i++) { + result.set(i, this.get(start + i)); + } + return result; +} + +reverse() { + const mid = Math.floor(this.length / 2); + for (let i = 0; i < mid; i++) { + const j = this.length - 1 - i; + + const temp = this._buffer[i]; + this._buffer[i] = this._buffer[j]; + this._buffer[j] = temp; + } + return this; +} + + fill(value, start = 0, end = this.length) { + start = start < 0 ? Math.max(this.length + start, 0) : Math.min(start, this.length); + end = end < 0 ? Math.max(this.length + end, 0) : Math.min(end, this.length); + + const bits = Float16Array.toFloat16(value); + + for (let i = start; i < end; i++) { + this._buffer[i] = bits; + + Object.defineProperty(this, i, { + get: () => this.get(i), + set: (val) => this.set(i, val), + enumerable: true, + configurable: true + }); + } + return this; +} + + static of(...args) { + const arr = new Float16Array(args.length); + for (let i = 0; i < args.length; i++) { + arr.set(i, args[i]); + } + return arr; +} + + static toFloat16(val) { + const floatView = new Float32Array(1); + const intView = new Uint32Array(floatView.buffer); + + floatView[0] = val; + const x = intView[0]; + + const sign = (x >> 16) & 0x8000; + const exponent = ((x >> 23) & 0xff) - 127; + const fraction = x & 0x7fffff; + + if (exponent <= -15) { + if (exponent < -24) return sign; + const sub = (fraction | 0x800000) >> (-exponent - 1); + return sign | (sub >> 13); + } + + if (exponent >= 16) { + return sign | 0x7c00; + } + + const exp = exponent + 15; + const frac = fraction >> 13; + return sign | (exp << 10) | frac; + } + + static fromFloat16(bits) { + const sign = (bits & 0x8000) << 16; + let exponent = (bits & 0x7C00) >> 10; + const fraction = bits & 0x03FF; + + let result; + if (exponent === 0) { + if (fraction === 0) { + result = sign; + } else { + exponent = 1; + while ((fraction & 0x0400) === 0) { + fraction <<= 1; + exponent--; + } + fraction &= ~0x0400; + const exp32 = 127 - 15 - exponent; + const frac32 = fraction << 13; + result = sign | (exp32 << 23) | frac32; + } + } else if (exponent === 0x1F) { + result = sign | 0x7F800000 | (fraction << 13); + } else { + const exp32 = exponent - 15 + 127; + result = sign | (exp32 << 23) | (fraction << 13); + } + + const intView = new Uint32Array(1); + intView[0] = result; + const floatView = new Float32Array(intView.buffer); + return floatView[0]; + } +} + + +// EXPORTS // + +module.exports = Float16Array; diff --git a/lib/node_modules/@stdlib/array/float16/package.json b/lib/node_modules/@stdlib/array/float16/package.json new file mode 100644 index 000000000000..34eb6b834170 --- /dev/null +++ b/lib/node_modules/@stdlib/array/float16/package.json @@ -0,0 +1,69 @@ +{ + "name": "@stdlib/array/float16", + "version": "0.0.0", + "description": "Float16Array polyfill and fallback interface.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib/index.js", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdtypes", + "types", + "data", + "structure", + "array", + "typed", + "typed array", + "typed-array", + "float16array", + "float16", + "float", + "half", + "half-precision", + "ieee754" + ], + "type": "commonjs" +} diff --git a/lib/node_modules/@stdlib/array/float16/test/test.js b/lib/node_modules/@stdlib/array/float16/test/test.js new file mode 100644 index 000000000000..364340deb159 --- /dev/null +++ b/lib/node_modules/@stdlib/array/float16/test/test.js @@ -0,0 +1,80 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var proxyquire = require( 'proxyquire' ); +var hasFloat16ArraySupport = require( '@stdlib/assert/has-float16array-support' ); +var polyfill = require( './../lib/polyfill.js' ); +var ctor = require( './../lib' ); + + +// VARIABLES // + +var hasFloat16Arrays = hasFloat16ArraySupport(); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof ctor, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'if an environment supports `Float16Array`, the export is an alias for `Float16Array`', function test( t ) { + var Foo; + + Foo = proxyquire( './../lib', { + '@stdlib/assert/has-float16array-support': isTrue, + './main.js': Mock + }); + t.strictEqual( Foo, Mock, 'returns builtin' ); + + if ( hasFloat16Arrays ) { + t.strictEqual( ctor, Float16Array, 'is alias' ); // eslint-disable-line stdlib/require-globals + } + + t.end(); + + function Mock() { + return this; + } + + function isTrue() { + return true; + } +}); + +tape( 'if an environment does not support `Float16Array`, the export is a polyfill', function test( t ) { + var Foo; + + Foo = proxyquire( './../lib', { + '@stdlib/assert/has-float16array-support': isFalse + }); + + t.strictEqual( Foo, polyfill, 'returns polyfill' ); + t.end(); + + function isFalse() { + return false; + } +}); diff --git a/lib/node_modules/@stdlib/array/float16/test/test.polyfill.js b/lib/node_modules/@stdlib/array/float16/test/test.polyfill.js new file mode 100644 index 000000000000..7ede661f99ee --- /dev/null +++ b/lib/node_modules/@stdlib/array/float16/test/test.polyfill.js @@ -0,0 +1,44 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var ctor = require( './../lib/polyfill.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof ctor, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function throws an error when invoked', function test( t ) { + t.throws( foo, Error, 'throws an error' ); + t.end(); + + function foo() { + var f = new ctor(); // eslint-disable-line no-unused-vars + } +}); + +// TODO: tests From 8c52f252b972e0afd4fe490c35a3d1338a13ca07 Mon Sep 17 00:00:00 2001 From: stdlib-bot <82920195+stdlib-bot@users.noreply.github.com> Date: Sat, 21 Jun 2025 07:43:37 +0000 Subject: [PATCH 02/45] chore: update copyright years --- .../@stdlib/array/float16/benchmark/benchmark.filter.length.js | 2 +- .../@stdlib/array/float16/benchmark/benchmark.from.js | 2 +- .../@stdlib/array/float16/benchmark/benchmark.length.js | 2 +- .../@stdlib/array/float16/benchmark/benchmark.map.js | 2 +- .../@stdlib/array/float16/benchmark/benchmark.map.length.js | 2 +- .../@stdlib/array/float16/benchmark/benchmark.of.js | 2 +- .../@stdlib/array/float16/benchmark/benchmark.reverse.length.js | 2 +- .../@stdlib/array/float16/benchmark/benchmark.slice.js | 2 +- .../@stdlib/array/float16/benchmark/benchmark.slice.length.js | 2 +- .../@stdlib/array/float16/benchmark/benchmark.sort.length.js | 2 +- .../float16/benchmark/benchmark.to_locale_string.length.js | 2 +- 11 files changed, 11 insertions(+), 11 deletions(-) diff --git a/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.filter.length.js b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.filter.length.js index 34b7e7c5bd37..a5db89b74105 100644 --- a/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.filter.length.js +++ b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.filter.length.js @@ -1,7 +1,7 @@ /** * @license Apache-2.0 * -* Copyright (c) 2018 The Stdlib Authors. +* Copyright (c) 2025 The Stdlib Authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.from.js b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.from.js index 3476a001aec1..01d161d647d4 100644 --- a/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.from.js +++ b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.from.js @@ -1,7 +1,7 @@ /** * @license Apache-2.0 * -* Copyright (c) 2018 The Stdlib Authors. +* Copyright (c) 2025 The Stdlib Authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.length.js b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.length.js index 2050edf7f209..3256f4f5f8e6 100644 --- a/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.length.js +++ b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.length.js @@ -1,7 +1,7 @@ /** * @license Apache-2.0 * -* Copyright (c) 2018 The Stdlib Authors. +* Copyright (c) 2025 The Stdlib Authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.map.js b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.map.js index 997e46d36a0a..410dadb2e6b0 100644 --- a/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.map.js +++ b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.map.js @@ -1,7 +1,7 @@ /** * @license Apache-2.0 * -* Copyright (c) 2018 The Stdlib Authors. +* Copyright (c) 2025 The Stdlib Authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.map.length.js b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.map.length.js index e2429481fbf7..9c33a9e6186f 100644 --- a/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.map.length.js +++ b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.map.length.js @@ -1,7 +1,7 @@ /** * @license Apache-2.0 * -* Copyright (c) 2018 The Stdlib Authors. +* Copyright (c) 2025 The Stdlib Authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.of.js b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.of.js index fcd777e7c643..62122c2fd8a7 100644 --- a/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.of.js +++ b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.of.js @@ -1,7 +1,7 @@ /** * @license Apache-2.0 * -* Copyright (c) 2018 The Stdlib Authors. +* Copyright (c) 2025 The Stdlib Authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.reverse.length.js b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.reverse.length.js index 28cdc4e8bc69..9f720967d445 100644 --- a/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.reverse.length.js +++ b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.reverse.length.js @@ -1,7 +1,7 @@ /** * @license Apache-2.0 * -* Copyright (c) 2018 The Stdlib Authors. +* Copyright (c) 2025 The Stdlib Authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.slice.js b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.slice.js index 8a85d40bd3ca..b4742b949cae 100644 --- a/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.slice.js +++ b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.slice.js @@ -1,7 +1,7 @@ /** * @license Apache-2.0 * -* Copyright (c) 2018 The Stdlib Authors. +* Copyright (c) 2025 The Stdlib Authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.slice.length.js b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.slice.length.js index a0859b021d78..bd3e7fd4c47b 100644 --- a/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.slice.length.js +++ b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.slice.length.js @@ -1,7 +1,7 @@ /** * @license Apache-2.0 * -* Copyright (c) 2018 The Stdlib Authors. +* Copyright (c) 2025 The Stdlib Authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.sort.length.js b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.sort.length.js index 590a46b516b3..9c5733512bcf 100644 --- a/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.sort.length.js +++ b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.sort.length.js @@ -1,7 +1,7 @@ /** * @license Apache-2.0 * -* Copyright (c) 2018 The Stdlib Authors. +* Copyright (c) 2025 The Stdlib Authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.to_locale_string.length.js b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.to_locale_string.length.js index ac8fd111e353..604006f98df4 100644 --- a/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.to_locale_string.length.js +++ b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.to_locale_string.length.js @@ -1,7 +1,7 @@ /** * @license Apache-2.0 * -* Copyright (c) 2018 The Stdlib Authors. +* Copyright (c) 2025 The Stdlib Authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. From 8611f89659c9b1c44cc853b48cc1518cbaf7c475 Mon Sep 17 00:00:00 2001 From: Uday Kakade <141299403+udaykakade25@users.noreply.github.com> Date: Sun, 29 Jun 2025 11:44:18 +0530 Subject: [PATCH 03/45] Refactored Polyfill.js Signed-off-by: Uday Kakade <141299403+udaykakade25@users.noreply.github.com> --- .../@stdlib/array/float16/lib/polyfill.js | 1305 +++++++++++------ 1 file changed, 835 insertions(+), 470 deletions(-) diff --git a/lib/node_modules/@stdlib/array/float16/lib/polyfill.js b/lib/node_modules/@stdlib/array/float16/lib/polyfill.js index 498388d5a0cd..f35849f885cb 100644 --- a/lib/node_modules/@stdlib/array/float16/lib/polyfill.js +++ b/lib/node_modules/@stdlib/array/float16/lib/polyfill.js @@ -18,520 +18,885 @@ 'use strict'; -// MAIN // +// MODULES // -class Float16Array { - constructor(input) { - if (typeof input === 'number') { - this._buffer = new Uint16Array(input); - } else if (Array.isArray(input)) { - this._buffer = new Uint16Array(input.length); - for (let i = 0; i < input.length; i++) { - this._buffer[i] = Float16Array.toFloat16(input[i]); - } - } else { - throw new TypeError('Unsupported constructor argument'); - } - this.length = this._buffer.length; - } - - get(index) { - return Float16Array.fromFloat16(this._buffer[index]); - } - - get buffer() { - return this._buffer.buffer; - } - - get byteLength() { - return this._buffer.byteLength; - } - - get byteOffset() { - return this._buffer.byteOffset; - } - - get BYTES_PER_ELEMENT() { - return 2; - } - - get [Symbol.toStringTag]() { - return 'Float16Array'; - } - - copyWithin(target, start, end) { - target = target >> 0; - start = start >> 0; - end = end === undefined ? this.length : end >> 0; - - const to = target < 0 ? this.length + target : target; - const from = start < 0 ? this.length + start : start; - const final = end < 0 ? this.length + end : end; - - const count = Math.min(final - from, this.length - to); - const direction = from < to && to < (from + count) ? -1 : 1; - - if (direction === -1) { - for (let i = count - 1; i >= 0; i--) { - this._buffer[to + i] = this._buffer[from + i]; - } - } else { - for (let i = 0; i < count; i++) { - this._buffer[to + i] = this._buffer[from + i]; - } - } - return this; - } - - set(indexOrArray, value) { - if (typeof indexOrArray === 'number') { - if (indexOrArray < 0 || indexOrArray >= this.length) { - throw new RangeError('Index out of bounds'); - } - this._buffer[indexOrArray] = Float16Array.toFloat16(value); - return; - } - - const source = indexOrArray; - const offset = value !== undefined ? Number(value) : 0; - - if (offset < 0 || offset > this.length) { - throw new RangeError('Offset is out of bounds'); - } - - if ( - (Array.isArray(source) || - (ArrayBuffer.isView(source) && typeof source.length === 'number') || - (source instanceof Float16Array)) - ) { - const len = Math.min(source.length, this.length - offset); - for (let i = 0; i < len; i++) { - const val = typeof source.get === 'function' ? source.get(i) : source[i]; - this._buffer[offset + i] = Float16Array.toFloat16(val); - } - return; - } - - throw new TypeError('Invalid arguments for set'); - } - - entries() { - return this._entriesGenerator(); -} +var min = require( '@stdlib/math/base/special/min' ); +var max = require( '@stdlib/math/base/special/max' ); +var floor = require( '@stdlib/math/base/special/floor' ); -*_entriesGenerator() { - for (let i = 0; i < this.length; i++) { - yield [i, this.get(i)]; - } -} - -every(callback, thisArg) { - if (typeof callback !== 'function') { - throw new TypeError('callback must be a function'); - } - for (let i = 0; i < this.length; i++) { - const value = this.get(i); - if (!callback.call(thisArg, value, i, this)) { - return false; - } - } - return true; -} -filter(callback, thisArg) { - if (typeof callback !== 'function') { - throw new TypeError('callback must be a function'); - } - - const result = []; - for (let i = 0; i < this.length; i++) { - const value = this.get(i); - if (callback.call(thisArg, value, i, this)) { - result.push(value); - } - } +// MAIN // - return new Float16Array(result); +/** +* Creates a new Float16Array. +* +* @constructor +* @param {(number|Array|ArrayBuffer|TypedArray)} input - Input data or buffer +* @param {NonNegativeInteger} [byteOffset=0] - Byte offset +* @param {NonNegativeInteger} [length] - Number of elements +* @throws {TypeError} Must provide valid input +*/ +function Float16Array( input, byteOffset, length ) { + var i; + var len; + var offset; + + byteOffset = byteOffset || 0; + + if ( typeof input === 'number' ) { + this._buffer = new Uint16Array( input ); + } else if ( input instanceof ArrayBuffer ) { + offset = byteOffset >>> 0; + len = ( typeof length !== 'undefined' ) ? ( length >>> 0 ) : ( ( input.byteLength - offset) / 2 ); + this._buffer = new Uint16Array( input, offset, len ); + } else if (Object.prototype.toString.call( input ) === '[object Array]' ) { + this._buffer = new Uint16Array( input.length ); + for (i = 0; i < input.length; i++) { + this._buffer[ i ] = Float16Array.toFloat16( input[ i ] ); + } + } else if ( typeof input === 'object' && typeof input.length === 'number' ) { + this._buffer = new Uint16Array( input.length ); + for ( i = 0; i < input.length; i++ ) { + this._buffer[ i ] = Float16Array.toFloat16( input[ i ] ); + } + } else { + throw new TypeError( 'Invalid argument. Must provide a number, Array, ArrayBuffer, or typed array. Value: ' + input + '.' ); + } + + this.length = this._buffer.length; } -findIndex(callback, thisArg) { - if (typeof callback !== 'function') { - throw new TypeError('callback must be a function'); - } - - for (let i = 0; i < this.length; i++) { - const value = this.get(i); - if (callback.call(thisArg, value, i, this)) { - return i; - } - } - return -1; -} +/** +* Get the underlying `ArrayBuffer`. +* +* @name buffer +* @memberof Float16Array.prototype +* @readonly +* @type {ArrayBuffer} +*/ +Object.defineProperty( Float16Array.prototype, 'buffer', { + get: function() { + return this._buffer.buffer; + } +}); -find(callback, thisArg) { - if (typeof callback !== 'function') { - throw new TypeError('callback must be a function'); - } - - for (let i = 0; i < this.length; i++) { - const value = this.get(i); - if (callback.call(thisArg, value, i, this)) { - return value; - } - } - return undefined; -} +/** +* Get the total number of bytes in the `Float16Array`. +* +* @name byteLength +* @memberof Float16Array.prototype +* @readonly +* @type {NonNegativeInteger} +*/ +Object.defineProperty( Float16Array.prototype, 'byteLength', { + get: function() { + return this._buffer.byteLength; + } +}); -forEach(callback, thisArg) { - if (typeof callback !== 'function') { - throw new TypeError('callback must be a function'); - } - for (let i = 0; i < this.length; i++) { - callback.call(thisArg, this.get(i), i, this); - } -} +/** +* Get the byte offset within the underlying `ArrayBuffer`. +* +* @name byteOffset +* @memberof Float16Array.prototype +* @readonly +* @type {NonNegativeInteger} +*/ +Object.defineProperty( Float16Array.prototype, 'byteOffset', { + get: function() { + return this._buffer.byteOffset; + } +}); -static from(source, mapFn, thisArg) { - if (source == null) { - throw new TypeError('source cannot be null or undefined'); - } - if (mapFn !== undefined && typeof mapFn !== 'function') { - throw new TypeError('when provided, mapFn must be a function'); - } - - let values = []; - const iteratorSymbol = typeof Symbol === 'function' && Symbol.iterator; - - if (iteratorSymbol && source[iteratorSymbol]) { - const iterator = source[iteratorSymbol](); - let step; - while (!(step = iterator.next()).done) { - values.push(step.value); - } - } else if (typeof source.length === 'number') { - for (let i = 0; i < source.length; i++) { - values.push(source[i]); - } - } else { - throw new TypeError('source is not iterable or array-like'); - } - - if (mapFn) { - if (thisArg) { - values = values.map(function(v, i) { - return mapFn.call(thisArg, v, i); - }); - } else { - values = values.map(mapFn); - } - } - - const arr = new Float16Array(values.length); - for (let i = 0; i < values.length; i++) { - arr.set(i, values[i]); - } - return arr; -} +/** +* Size in bytes of each element. +* +* @name BYTES_PER_ELEMENT +* @memberof Float16Array.prototype +* @readonly +* @type {PositiveInteger} +*/ +Object.defineProperty( Float16Array.prototype, 'BYTES_PER_ELEMENT', { + get: function() { + return 2; + } +}); -includes(searchElement, fromIndex = 0) { - if (fromIndex < 0) { - fromIndex = Math.max(this.length + fromIndex, 0); - } - for (let i = fromIndex; i < this.length; i++) { - if (this.get(i) === searchElement) { - return true; - } - } - return false; -} +/** +* Returns the default string tag. +* +* @name [Symbol.toStringTag] +* @memberof Float16Array.prototype +* @readonly +* @type {string} +*/ +Object.defineProperty( Float16Array.prototype, ( typeof Symbol !== 'undefined' && Symbol.toStringTag ) || '__toStringTag__', { + get: function() { + return 'Float16Array'; + } +}); -indexOf(searchElement, fromIndex = 0) { - fromIndex = Math.max(fromIndex >= 0 ? fromIndex : this.length + fromIndex, 0); - for (let i = fromIndex; i < this.length; i++) { - if (this.get(i) === searchElement) { - return i; - } - } - return -1; -} +/** +* Converts a float32 number to a float16 (binary) representation. +* +* @param {number} val - Float32 value +* @returns {integer} Float16 bit pattern +*/ +Float16Array.toFloat16 = function( val ) { + var floatView = new Float32Array( 1 ); + var intView = new Uint32Array( floatView.buffer ); + var sign; + var exponent; + var fraction; + var exp; + var frac; + + floatView[ 0 ] = val; + var x = intView[ 0 ]; + + sign = ( x >> 16 ) & 0x8000; + exponent = ( ( x >> 23 ) & 0xff ) - 127; + fraction = x & 0x7fffff; + + if ( exponent <= -15 ) { + if ( exponent < -24 ) { + return sign; + } + return sign | ( ( ( fraction | 0x800000 ) >> ( -exponent - 1 ) ) >> 13 ); + } + + if ( exponent >= 16 ) { + return sign | 0x7c00; + } + + exp = exponent + 15; + frac = fraction >> 13; + return sign | ( exp << 10 ) | frac; +}; -join(separator = ',') { - let result = ''; - for (let i = 0; i < this.length; i++) { - if (i > 0) result += separator; - result += String(this.get(i)); - } - return result; -} +/** +* Converts a float16 binary representation to float32. +* +* @param {integer} bits - Float16 bit pattern +* @returns {number} Float32 value +*/ +Float16Array.fromFloat16 = function( bits ) { + var sign = ( bits & 0x8000 ) << 16; + var exponent = ( bits & 0x7C00 ) >> 10; + var fraction = bits & 0x03FF; + var intView = new Uint32Array( 1 ); + var floatView; + var result; + var exp32; + var frac32; + + if ( exponent === 0 ) { + if ( fraction === 0 ) { + result = sign; + } else { + exponent = 1; + while ( ( fraction & 0x0400 ) === 0 ) { + fraction <<= 1; + exponent -= 1; + } + fraction &= ~0x0400; + exp32 = 127 - 15 - exponent; + frac32 = fraction << 13; + result = sign | ( exp32 << 23 ) | frac32; + } + } else if ( exponent === 0x1F ) { + result = sign | 0x7F800000 | ( fraction << 13 ); + } else { + exp32 = exponent - 15 + 127; + result = sign | ( exp32 << 23 ) | ( fraction << 13 ); + } + + intView[ 0 ] = result; + floatView = new Float32Array( intView.buffer ); + return floatView[ 0 ]; +}; -keys() { - return this._keysGenerator(); -} +/** +* Create a new Float16Array from a variable number of arguments. +* +* @returns {Float16Array} New instance +*/ +Float16Array.of = function() { + var len = arguments.length; + var result = new Float16Array( len ); + var i; -*_keysGenerator() { - for (let i = 0; i < this.length; i++) { - yield i; - } -} + for ( i = 0; i < len; i++ ) { + result.set( i, arguments[ i ] ); + } + return result; +}; -lastIndexOf(searchElement, fromIndex) { - let i = fromIndex !== undefined ? fromIndex : this.length - 1; - if (i >= this.length) i = this.length - 1; - if (i < 0) i = this.length + i; - - for (; i >= 0; i--) { - if (Object.is(this.get(i), searchElement)) { - return i; - } - } - return -1; -} +/** +* Create a new Float16Array from an array-like or iterable object. +* +* @param {*} source - Source to convert +* @param {Function} [mapFn] - Optional mapping function +* @param {*} [thisArg] - Execution context for mapFn +* @returns {Float16Array} New instance +*/ +Float16Array.from = function( source, mapFn, thisArg ) { + if ( source == null ) { + throw new TypeError( 'invalid argument. Source cannot be null or undefined. Value: `' + source + '`.' ); + } + if ( typeof mapFn !== 'undefined' && typeof mapFn !== 'function' ) { + throw new TypeError( 'invalid argument. When provided, mapFn must be a function. Value: `' + mapFn + '`.' ); + } + + var values = []; + var i = 0; + var item; + + // Handle iterable (with Symbol.iterator) or array-like + if ( typeof source.length === 'number' ) { + for ( i = 0; i < source.length; i++ ) { + values.push( source[ i ] ); + } + } else if ( typeof source === 'object' && typeof source[ Symbol.iterator ] === 'function' ) { + var iterator = source[ Symbol.iterator ](); + while ( true ) { + var next = iterator.next(); + if ( next.done ) { + break; + } + values.push( next.value ); + } + } else { + throw new TypeError( 'invalid argument. Source must be iterable or array-like. Value: `' + source + '`.' ); + } + + if ( mapFn ) { + for ( i = 0; i < values.length; i++ ) { + values[ i ] = mapFn.call( thisArg, values[ i ], i ); + } + } + + var result = new Float16Array( values.length ); + for ( i = 0; i < values.length; i++ ) { + result.set( i, values[ i ] ); + } + return result; +}; -map(callback, thisArg) { - if (typeof callback !== 'function') { - throw new TypeError(callback + ' is not a function'); - } +/** +* Get a value at a given index. +* +* @param {NonNegativeInteger} index - Array index +* @returns {number} Float32 value at index +*/ +Float16Array.prototype.get = function( index ) { + return Float16Array.fromFloat16( this._buffer[ index ] ); +}; - const result = new this.constructor(this.length); +/** +* Set a value at a given index, or copy values from an array-like source. +* +* @param {(NonNegativeInteger|ArrayLike)} indexOrArray - Index or source array +* @param {*} [value] - Value to set or offset if copying +* @throws {RangeError} Index or offset out of bounds +* @throws {TypeError} Invalid arguments +*/ +Float16Array.prototype.set = function( indexOrArray, value ) { + var i; + var len; + var offset; + var srcVal; + + if ( typeof indexOrArray === 'number' ) { + if ( indexOrArray < 0 || indexOrArray >= this.length ) { + throw new RangeError( 'invalid argument. Index must be a nonnegative integer less than the array length. Value: `' + indexOrArray + '`.' ); + } + this._buffer[ indexOrArray ] = Float16Array.toFloat16( value ); + return; + } + + offset = ( value !== undefined ) ? Number( value ) : 0; + + if ( offset < 0 || offset > this.length ) { + throw new RangeError( 'invalid argument. Offset must be a nonnegative integer less than or equal to the array length. Value: `' + offset + '`.' ); + } + + if ( + Object.prototype.toString.call( indexOrArray ) === '[object Array]' || + ( typeof indexOrArray === 'object' && typeof indexOrArray.length === 'number' ) + ) { + len = min( indexOrArray.length, this.length - offset ); + for ( i = 0; i < len; i++ ) { + srcVal = ( typeof indexOrArray.get === 'function' ) ? indexOrArray.get( i ) : indexOrArray[ i ]; + this._buffer[ offset + i ] = Float16Array.toFloat16( srcVal ); + } + return; + } + + throw new TypeError( 'invalid argument. Must provide index and value or source and offset. Value: `' + indexOrArray + '`.' ); +}; - for (let i = 0; i < this.length; i++) { - if (Object.prototype.hasOwnProperty.call(this, i)) { - const mappedValue = callback.call(thisArg, this.get(i), i, this); - result.set(i, mappedValue); - } - } +/** +* Copy a sequence of array elements within the array. +* +* @param {integer} target - Target index +* @param {integer} start - Start index +* @param {integer} [end] - End index (exclusive) +* @returns {Float16Array} Modified array +*/ +Float16Array.prototype.copyWithin = function( target, start, end ) { + var to = ( target < 0 ) ? this.length + target : target; + var from = ( start < 0 ) ? this.length + start : start; + var final = ( typeof end === 'undefined' ) ? this.length : ( end < 0 ? this.length + end : end ); + var count = min( final - from, this.length - to ); + var i; + + if ( from < to && to < ( from + count ) ) { + for ( i = count - 1; i >= 0; i-- ) { + this._buffer[ to + i ] = this._buffer[ from + i ]; + } + } else { + for ( i = 0; i < count; i++ ) { + this._buffer[ to + i ] = this._buffer[ from + i ]; + } + } + return this; +}; - return result; -} +/** +* Returns a shallow copy of a portion of the array. +* +* @param {integer} [start=0] - Start index +* @param {integer} [end=array.length] - End index (exclusive) +* @returns {Float16Array} New subarray +*/ +Float16Array.prototype.slice = function( start, end ) { + var len = this.length; + var i; + var count; + var relativeStart; + var relativeEnd; + var result; + + relativeStart = ( typeof start === 'undefined' ) ? 0 : ( start < 0 ? max( len + start, 0 ) : min( start, len ) ); + relativeEnd = ( typeof end === 'undefined' ) ? len : ( end < 0 ? max( len + end, 0 ) : min( end, len ) ); + + count = max( relativeEnd - relativeStart, 0 ); + result = new Float16Array( count ); + + for ( i = 0; i < count; i++ ) { + result.set( i, this.get( relativeStart + i ) ); + } + return result; +}; -slice(start = 0, end = this.length) { - const len = this.length; +/** +* Fill the array with a static value. +* +* @param {*} value - Value to fill +* @param {integer} [start=0] - Start index +* @param {integer} [end=array.length] - End index +* @returns {Float16Array} Modified array +*/ +Float16Array.prototype.fill = function( value, start, end ) { + var i; + var bits; + var s; + var e; + + s = ( typeof start === 'undefined' ) ? 0 : ( start < 0 ? max( this.length + start, 0 ) : min( start, this.length ) ); + e = ( typeof end === 'undefined' ) ? this.length : ( end < 0 ? max( this.length + end, 0 ) : min( end, this.length ) ); + + bits = Float16Array.toFloat16( value ); + + for ( i = s; i < e; i++ ) { + this._buffer[ i ] = bits; + Object.defineProperty( this, i, { + get: (function( index ) { + return function() { + return this.get( index ); + }; + })( i ), + set: (function( index ) { + return function( val ) { + this.set( index, val ); + }; + })( i ), + enumerable: true, + configurable: true + }); + } + return this; +}; - let relativeStart = start < 0 ? Math.max(len + start, 0) : Math.min(start, len); - let relativeEnd = end < 0 ? Math.max(len + end, 0) : Math.min(end, len); +/** +* Returns a new Float16Array view on the same buffer, for the specified range. +* +* @param {integer} [begin=0] - Start index +* @param {integer} [end=this.length] - End index +* @returns {Float16Array} New Float16Array +*/ +Float16Array.prototype.subarray = function( begin, end ) { + var len = this.length; + var i; + var start = ( typeof begin === 'undefined' ) ? 0 : ( begin < 0 ? max( len + begin, 0 ) : min( begin, len ) ); + var finish = ( typeof end === 'undefined' ) ? len : ( end < 0 ? max( len + end, 0 ) : min( end, len ) ); + var count = max( finish - start, 0 ); + var result = new Float16Array( count ); + + for ( i = 0; i < count; i++ ) { + result.set( i, this.get( start + i ) ); + } + return result; +}; - const count = Math.max(relativeEnd - relativeStart, 0); - const result = new this.constructor(count); +/** +* Reverse the array in place. +* +* @returns {Float16Array} Modified array +*/ +Float16Array.prototype.reverse = function() { + var i; + var j; + var tmp; + var mid = floor( this.length / 2 ); + for ( i = 0; i < mid; i++ ) { + j = this.length - 1 - i; + tmp = this._buffer[ i ]; + this._buffer[ i ] = this._buffer[ j ]; + this._buffer[ j ] = tmp; + } + return this; +}; - for (let i = 0; i < count; i++) { - result.set(i, this.get(relativeStart + i)); - } - return result; -} +/** +* Sort the array in place. +* +* @param {Function} [compareFn] - Optional comparison function +* @returns {Float16Array} Sorted array +*/ +Float16Array.prototype.sort = function( compareFn ) { + var values = []; + var i; + var j; + + for ( i = 0; i < this.length; i++ ) { + values.push( this.get( i ) ); + } + + values.sort( function( a, b ) { + if ( compareFn ) { + return compareFn( a, b ); + } + if ( a !== a ) { + return 1; + } + if ( b !== b ) { + return -1; + } + return a - b; + }); + + for ( j = 0; j < this.length; j++ ) { + this.set( j, values[ j ] ); + } + return this; +}; -sort(compareFn) { - const values = []; - for (let i = 0; i < this.length; i++) { - values.push(this.get(i)); - } - - values.sort((a, b) => { - if (compareFn) return compareFn(a, b); - if (a !== a) return 1; - if (b !== b) return -1; - return a - b; - }); - - for (let i = 0; i < this.length; i++) { - this.set(i, values[i]); - } - return this; -} +/** +* Apply a function to each element and return a new Float16Array. +* +* @param {Function} callback - Mapping function +* @param {*} [thisArg] - Context for callback +* @returns {Float16Array} Mapped array +*/ +Float16Array.prototype.map = function( callback, thisArg ) { + var result = new Float16Array( this.length ); + var i; + var val; + if ( typeof callback !== 'function' ) { + throw new TypeError( 'invalid argument. Callback must be a function. Value: `' + callback + '`.' ); + } + for ( i = 0; i < this.length; i++ ) { + val = callback.call( thisArg, this.get( i ), i, this ); + result.set( i, val ); + } + return result; +}; -values() { - return this._valuesGenerator(); -} +/** +* Execute a function for each element of the array. +* +* @param {Function} callback - Function to invoke +* @param {*} [thisArg] - Context for callback +*/ +Float16Array.prototype.forEach = function( callback, thisArg ) { + var i; + if ( typeof callback !== 'function' ) { + throw new TypeError( 'invalid argument. Callback must be a function. Value: `' + callback + '`.' ); + } + for ( i = 0; i < this.length; i++ ) { + callback.call( thisArg, this.get( i ), i, this ); + } +}; -*_valuesGenerator() { - for (let i = 0; i < this.length; i++) { - yield this.get(i); - } -} +/** +* Reduce the array to a single value (left-to-right). +* +* @param {Function} callback - Reducer function +* @param {*} [initialValue] - Initial value +* @returns {*} Accumulated result +*/ +Float16Array.prototype.reduce = function( callback, initialValue ) { + var i; + var acc; + var len; + + if ( typeof callback !== 'function' ) { + throw new TypeError( 'invalid argument. Callback must be a function. Value: `' + callback + '`.' ); + } + len = this.length; + if ( arguments.length >= 2 ) { + acc = initialValue; + i = 0; + } else { + if ( len === 0 ) { + throw new TypeError( 'invalid operation. Reduce of empty array with no initial value.' ); + } + acc = this.get( 0 ); + i = 1; + } + for ( ; i < len; i++ ) { + acc = callback( acc, this.get( i ), i, this ); + } + return acc; +}; -some(callback, thisArg) { - if (typeof callback !== 'function') { - throw new TypeError(callback + ' is not a function'); - } - for (let i = 0; i < this.length; i++) { - const value = this.get(i); - if (callback.call(thisArg, value, i, this)) { - return true; - } - } - return false; -} +/** +* Reduce the array to a single value (right-to-left). +* +* @param {Function} callback - Reducer function +* @param {*} [initialValue] - Initial value +* @returns {*} Accumulated result +*/ +Float16Array.prototype.reduceRight = function( callback, initialValue ) { + var i; + var acc; + var len; + + if ( typeof callback !== 'function' ) { + throw new TypeError( 'invalid argument. Callback must be a function. Value: `' + callback + '`.' ); + } + len = this.length; + if ( arguments.length >= 2 ) { + acc = initialValue; + i = len - 1; + } else { + if ( len === 0 ) { + throw new TypeError( 'invalid operation. Reduce of empty array with no initial value.' ); + } + acc = this.get( len - 1 ); + i = len - 2; + } + for ( ; i >= 0; i-- ) { + acc = callback( acc, this.get( i ), i, this ); + } + return acc; +}; -reduce(callback, initialValue) { - const length = this.length; - if (typeof callback !== 'function') { - throw new TypeError(callback + ' is not a function'); - } - - let accumulator; - let startIndex = 0; - - if (arguments.length >= 2) { - accumulator = initialValue; - } else { - if (length === 0) { - throw new TypeError('Reduce of empty array with no initial value'); - } - accumulator = this.get(0); - startIndex = 1; - } - - for (let i = startIndex; i < length; i++) { - accumulator = callback(accumulator, this.get(i), i, this); - } - - return accumulator; -} +/** +* Test whether at least one element passes the test. +* +* @param {Function} callback - Predicate function +* @param {*} [thisArg] - Context +* @returns {boolean} Whether at least one passes +*/ +Float16Array.prototype.some = function( callback, thisArg ) { + var i; + var value; + + if ( typeof callback !== 'function' ) { + throw new TypeError( 'invalid argument. Callback must be a function. Value: `' + callback + '`.' ); + } + for ( i = 0; i < this.length; i++ ) { + value = this.get( i ); + if ( callback.call( thisArg, value, i, this ) ) { + return true; + } + } + return false; +}; -reduceRight(callback, initialValue) { - const length = this.length; - if (typeof callback !== 'function') { - throw new TypeError(callback + ' is not a function'); - } - - let accumulator; - let startIndex = length - 1; - - if (arguments.length >= 2) { - accumulator = initialValue; - } else { - if (length === 0) { - throw new TypeError('Reduce of empty array with no initial value'); - } - accumulator = this.get(startIndex); - startIndex--; - } - - for (let i = startIndex; i >= 0; i--) { - accumulator = callback(accumulator, this.get(i), i, this); - } - - return accumulator; -} +/** +* Test whether all elements pass the test. +* +* @param {Function} callback - Predicate function +* @param {*} [thisArg] - Context +* @returns {boolean} Whether all pass +*/ +Float16Array.prototype.every = function( callback, thisArg ) { + var i; + if ( typeof callback !== 'function' ) { + throw new TypeError( 'invalid argument. Callback must be a function. Value: `' + callback + '`.' ); + } + for ( i = 0; i < this.length; i++ ) { + if ( !callback.call( thisArg, this.get( i ), i, this ) ) { + return false; + } + } + return true; +}; -subarray(begin = 0, end = this.length) { - const len = this.length; +/** +* Returns the first index at which a given element can be found in the array, or -1 if it is not present. +* +* @param {*} searchElement - Element to locate +* @param {integer} [fromIndex=0] - Index to start the search at +* @returns {integer} Index of the element, or -1 if not found +*/ +Float16Array.prototype.indexOf = function( searchElement, fromIndex ) { + var i; + var start; - let start = begin < 0 ? Math.max(len + begin, 0) : Math.min(begin, len); - let finish = end < 0 ? Math.max(len + end, 0) : Math.min(end, len); + start = ( typeof fromIndex === 'undefined' ) ? 0 : fromIndex; - start = Math.min(start, len); - finish = Math.min(Math.max(finish, start), len); + if ( start < 0 ) { + start = max( this.length + start, 0 ); + } - const count = Math.max(finish - start, 0); - const SubArrayCtor = this.constructor; + for ( i = start; i < this.length; i++ ) { + if ( this.get( i ) === searchElement ) { + return i; + } + } + return -1; +}; - const result = new SubArrayCtor(count); +/** +* Returns the last index at which a given element can be found in the array, or -1 if it is not present. +* +* @param {*} searchElement - Element to locate +* @param {integer} [fromIndex=this.length-1] - Index to start the search backwards from +* @returns {integer} Last index of the element, or -1 if not found +*/ +Float16Array.prototype.lastIndexOf = function( searchElement, fromIndex ) { + var i = ( typeof fromIndex !== 'undefined' ) ? fromIndex : this.length - 1; + + if ( i >= this.length ) { + i = this.length - 1; + } + if ( i < 0 ) { + i = this.length + i; + } + + for ( ; i >= 0; i-- ) { + if ( Object.is( this.get( i ), searchElement ) ) { + return i; + } + } + return -1; +}; - for (let i = 0; i < count; i++) { - result.set(i, this.get(start + i)); - } - return result; -} +/** +* Joins all elements of the Float16Array into a string, separated by a specified separator. +* +* @param {string} [separator=','] - Separator to insert between values +* @returns {string} Joined string +*/ +Float16Array.prototype.join = function( separator ) { + var result = ''; + var i; -reverse() { - const mid = Math.floor(this.length / 2); - for (let i = 0; i < mid; i++) { - const j = this.length - 1 - i; + separator = ( typeof separator === 'undefined' ) ? ',' : String( separator ); - const temp = this._buffer[i]; - this._buffer[i] = this._buffer[j]; - this._buffer[j] = temp; - } - return this; -} + for ( i = 0; i < this.length; i++ ) { + if ( i > 0 ) { + result += separator; + } + result += String( this.get( i ) ); + } + return result; +}; - fill(value, start = 0, end = this.length) { - start = start < 0 ? Math.max(this.length + start, 0) : Math.min(start, this.length); - end = end < 0 ? Math.max(this.length + end, 0) : Math.min(end, this.length); +/** +* Determines whether the array includes a certain value among its entries. +* +* @param {*} searchElement - Element to search for +* @param {integer} [fromIndex=0] - Index to start the search at +* @returns {boolean} True if the value is found, false otherwise +*/ +Float16Array.prototype.includes = function( searchElement, fromIndex ) { + var i; + var start = ( typeof fromIndex === 'undefined' ) ? 0 : fromIndex; + + if ( start < 0 ) { + start = max( this.length + start, 0 ); + } + + for ( i = start; i < this.length; i++ ) { + if ( this.get( i ) === searchElement ) { + return true; + } + } + return false; +}; - const bits = Float16Array.toFloat16(value); +/** +* Creates a new Float16Array with all elements that pass the test implemented by the provided function. +* +* @param {Function} callback - Function to test each element +* @param {*} [thisArg] - Value to use as `this` when executing `callback` +* @throws {TypeError} Must provide a function as callback +* @returns {Float16Array} New Float16Array with filtered elements +*/ +Float16Array.prototype.filter = function( callback, thisArg ) { + var i; + var value; + var result; + + if ( typeof callback !== 'function' ) { + throw new TypeError( 'invalid argument. Must provide a function. Value: `' + callback + '`.' ); + } + + result = []; + for ( i = 0; i < this.length; i++ ) { + value = this.get( i ); + if ( callback.call( thisArg, value, i, this ) ) { + result.push( value ); + } + } + return new Float16Array( result ); +}; - for (let i = start; i < end; i++) { - this._buffer[i] = bits; +/** +* Returns the value of the first element that satisfies the provided testing function. +* +* @param {Function} callback - Function to execute on each value +* @param {*} [thisArg] - Value to use as `this` when executing `callback` +* @throws {TypeError} Must provide a function as callback +* @returns {*} First matching element, or undefined if no match +*/ +Float16Array.prototype.find = function( callback, thisArg ) { + var i; + var value; + + if ( typeof callback !== 'function' ) { + throw new TypeError( 'invalid argument. Must provide a function. Value: `' + callback + '`.' ); + } + + for ( i = 0; i < this.length; i++ ) { + value = this.get( i ); + if ( callback.call( thisArg, value, i, this ) ) { + return value; + } + } + return void 0; // returns undefined +}; - Object.defineProperty(this, i, { - get: () => this.get(i), - set: (val) => this.set(i, val), - enumerable: true, - configurable: true - }); - } - return this; -} +/** +* Returns the index of the first element that satisfies the provided testing function. +* +* @param {Function} callback - Function to execute on each value +* @param {*} [thisArg] - Value to use as `this` when executing `callback` +* @throws {TypeError} Must provide a function as callback +* @returns {integer} Index of found element, or -1 if none found +*/ +Float16Array.prototype.findIndex = function( callback, thisArg ) { + var i; + var value; + + if ( typeof callback !== 'function' ) { + throw new TypeError( 'invalid argument. Must provide a function. Value: `' + callback + '`.' ); + } + for ( i = 0; i < this.length; i++ ) { + value = this.get( i ); + if ( callback.call( thisArg, value, i, this ) ) { + return i; + } + } + return -1; +}; - static of(...args) { - const arr = new Float16Array(args.length); - for (let i = 0; i < args.length; i++) { - arr.set(i, args[i]); - } - return arr; -} +/** +* Return an iterator over [index, value] pairs. +* +* @returns {Object} Iterator +*/ +Float16Array.prototype.entries = function() { + var self = this; + var i = 0; + + return { + next: function() { + if ( i < self.length ) { + var v = [ i, self.get( i ) ]; + i += 1; + return { + value: v, + done: false + }; + } + return { + value: void 0, + done: true + }; + } + }; +}; - static toFloat16(val) { - const floatView = new Float32Array(1); - const intView = new Uint32Array(floatView.buffer); - - floatView[0] = val; - const x = intView[0]; - - const sign = (x >> 16) & 0x8000; - const exponent = ((x >> 23) & 0xff) - 127; - const fraction = x & 0x7fffff; - - if (exponent <= -15) { - if (exponent < -24) return sign; - const sub = (fraction | 0x800000) >> (-exponent - 1); - return sign | (sub >> 13); - } - - if (exponent >= 16) { - return sign | 0x7c00; - } - - const exp = exponent + 15; - const frac = fraction >> 13; - return sign | (exp << 10) | frac; - } - - static fromFloat16(bits) { - const sign = (bits & 0x8000) << 16; - let exponent = (bits & 0x7C00) >> 10; - const fraction = bits & 0x03FF; - - let result; - if (exponent === 0) { - if (fraction === 0) { - result = sign; - } else { - exponent = 1; - while ((fraction & 0x0400) === 0) { - fraction <<= 1; - exponent--; - } - fraction &= ~0x0400; - const exp32 = 127 - 15 - exponent; - const frac32 = fraction << 13; - result = sign | (exp32 << 23) | frac32; - } - } else if (exponent === 0x1F) { - result = sign | 0x7F800000 | (fraction << 13); - } else { - const exp32 = exponent - 15 + 127; - result = sign | (exp32 << 23) | (fraction << 13); - } - - const intView = new Uint32Array(1); - intView[0] = result; - const floatView = new Float32Array(intView.buffer); - return floatView[0]; - } -} +/** +* Return an iterator over indices. +* +* @returns {Object} Iterator +*/ +Float16Array.prototype.keys = function() { + var self = this; + var i = 0; + + return { + next: function() { + if ( i < self.length ) { + var v = i; + i += 1; + return { + value: v, + done: false + }; + } + return { + value: void 0, + done: true + }; + } + }; +}; +/** +* Return an iterator over values. +* +* @returns {Object} Iterator +*/ +Float16Array.prototype.values = function() { + var self = this; + var i = 0; + + return { + next: function() { + if ( i < self.length ) { + var v = self.get( i ); + i += 1; + return { + value: v, + done: false + }; + } + return { + value: void 0, + done: true + }; + } + }; +}; // EXPORTS // From 12e481429a4eecdd77d8ddb69c3c0af4d5dfea91 Mon Sep 17 00:00:00 2001 From: Uday Kakade <141299403+udaykakade25@users.noreply.github.com> Date: Mon, 30 Jun 2025 01:33:46 +0530 Subject: [PATCH 04/45] Refactored polyfill.js Signed-off-by: Uday Kakade <141299403+udaykakade25@users.noreply.github.com> --- .../@stdlib/array/float16/lib/polyfill.js | 1485 +++++++++-------- 1 file changed, 813 insertions(+), 672 deletions(-) diff --git a/lib/node_modules/@stdlib/array/float16/lib/polyfill.js b/lib/node_modules/@stdlib/array/float16/lib/polyfill.js index f35849f885cb..47d8e126927d 100644 --- a/lib/node_modules/@stdlib/array/float16/lib/polyfill.js +++ b/lib/node_modules/@stdlib/array/float16/lib/polyfill.js @@ -20,9 +20,17 @@ // MODULES // +var setReadOnlyAccessor = require( '@stdlib/utils/define-nonenumerable-read-only-accessor' ); +var isNonNegativeInteger = require( '@stdlib/assert/is-nonnegative-integer' ).isPrimitive; +var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); +var isInteger = require( '@stdlib/assert/is-integer' ).isPrimitive; +var isCollection = require( '@stdlib/assert/is-collection' ); +var isFunction = require( '@stdlib/assert/is-function' ); +var floor = require( '@stdlib/math/base/special/floor' ); var min = require( '@stdlib/math/base/special/min' ); var max = require( '@stdlib/math/base/special/max' ); -var floor = require( '@stdlib/math/base/special/floor' ); +var isArray = require( '@stdlib/assert/is-array' ); +var format = require( '@stdlib/string/format' ); // MAIN // @@ -37,35 +45,50 @@ var floor = require( '@stdlib/math/base/special/floor' ); * @throws {TypeError} Must provide valid input */ function Float16Array( input, byteOffset, length ) { - var i; - var len; - var offset; - - byteOffset = byteOffset || 0; - - if ( typeof input === 'number' ) { - this._buffer = new Uint16Array( input ); - } else if ( input instanceof ArrayBuffer ) { - offset = byteOffset >>> 0; - len = ( typeof length !== 'undefined' ) ? ( length >>> 0 ) : ( ( input.byteLength - offset) / 2 ); - this._buffer = new Uint16Array( input, offset, len ); - } else if (Object.prototype.toString.call( input ) === '[object Array]' ) { - this._buffer = new Uint16Array( input.length ); - for (i = 0; i < input.length; i++) { - this._buffer[ i ] = Float16Array.toFloat16( input[ i ] ); - } - } else if ( typeof input === 'object' && typeof input.length === 'number' ) { - this._buffer = new Uint16Array( input.length ); - for ( i = 0; i < input.length; i++ ) { - this._buffer[ i ] = Float16Array.toFloat16( input[ i ] ); - } - } else { - throw new TypeError( 'Invalid argument. Must provide a number, Array, ArrayBuffer, or typed array. Value: ' + input + '.' ); - } - - this.length = this._buffer.length; + var i; + var len; + var offset; + + byteOffset = byteOffset || 0; + + if ( isNonNegativeInteger( input ) ) { + this._buffer = new Uint16Array( input ); + } else if ( input instanceof ArrayBuffer ) { + offset = Number( byteOffset ); + if ( !isNonNegativeInteger( offset ) ) { + throw new RangeError( format( 'invalid argument. Byte offset must be a non-negative integer. Value: `%s`.', byteOffset ) ); + } + + if ( length !== void 0 ) { + if ( !isNonNegativeInteger( length ) ) { + throw new RangeError( format( 'invalid argument. Length must be a non-negative integer. Value: `%s`.', length ) ); + } + len = length; + } else { + len = ( input.byteLength - offset ) / 2; + } + + this._buffer = new Uint16Array( input, offset, len ); + } else if ( isArray( input ) ) { + this._buffer = new Uint16Array( input.length ); + for ( i = 0; i < input.length; i++ ) { + this._buffer[ i ] = Float16Array.toFloat16( input[ i ] ); + } + } else if ( isCollection( input ) ) { + this._buffer = new Uint16Array( input.length ); + for ( i = 0; i < input.length; i++ ) { + this._buffer[ i ] = Float16Array.toFloat16( input[ i ] ); + } + } else { + throw new TypeError( format( 'invalid argument. Must provide a number, Array, ArrayBuffer, or typed array. Value: `%s`.', input ) ); + } + + this.length = this._buffer.length; } +setReadOnly( Float16Array, 'name', 'Float16Array' ); +setReadOnly( Float16Array, 'BYTES_PER_ELEMENT', 2 ); + /** * Get the underlying `ArrayBuffer`. * @@ -74,11 +97,10 @@ function Float16Array( input, byteOffset, length ) { * @readonly * @type {ArrayBuffer} */ -Object.defineProperty( Float16Array.prototype, 'buffer', { - get: function() { - return this._buffer.buffer; - } -}); +function getBuffer() { + return this._buffer.buffer; +} +setReadOnlyAccessor( Float16Array.prototype, 'buffer', getBuffer ); /** * Get the total number of bytes in the `Float16Array`. @@ -88,11 +110,10 @@ Object.defineProperty( Float16Array.prototype, 'buffer', { * @readonly * @type {NonNegativeInteger} */ -Object.defineProperty( Float16Array.prototype, 'byteLength', { - get: function() { - return this._buffer.byteLength; - } -}); +function getByteLength() { + return this._buffer.byteLength; +} +setReadOnlyAccessor( Float16Array.prototype, 'byteLength', getByteLength ); /** * Get the byte offset within the underlying `ArrayBuffer`. @@ -102,11 +123,10 @@ Object.defineProperty( Float16Array.prototype, 'byteLength', { * @readonly * @type {NonNegativeInteger} */ -Object.defineProperty( Float16Array.prototype, 'byteOffset', { - get: function() { - return this._buffer.byteOffset; - } -}); +function getByteOffset() { + return this._buffer.byteOffset; +} +setReadOnlyAccessor( Float16Array.prototype, 'byteOffset', getByteOffset ); /** * Size in bytes of each element. @@ -116,11 +136,10 @@ Object.defineProperty( Float16Array.prototype, 'byteOffset', { * @readonly * @type {PositiveInteger} */ -Object.defineProperty( Float16Array.prototype, 'BYTES_PER_ELEMENT', { - get: function() { - return 2; - } -}); +function getBytesPerElement() { + return 2; +} +setReadOnlyAccessor( Float16Array.prototype, 'BYTES_PER_ELEMENT', getBytesPerElement ); /** * Returns the default string tag. @@ -130,745 +149,854 @@ Object.defineProperty( Float16Array.prototype, 'BYTES_PER_ELEMENT', { * @readonly * @type {string} */ -Object.defineProperty( Float16Array.prototype, ( typeof Symbol !== 'undefined' && Symbol.toStringTag ) || '__toStringTag__', { - get: function() { - return 'Float16Array'; - } -}); +var symbolToStringTag = ( typeof Symbol !== 'undefined' && Symbol.toStringTag ) || '__toStringTag__'; + +function getToStringTag() { + return 'Float16Array'; +} +setReadOnlyAccessor( Float16Array.prototype, symbolToStringTag, getToStringTag ); /** -* Converts a float32 number to a float16 (binary) representation. +* Copy a sequence of array elements within the array. * -* @param {number} val - Float32 value -* @returns {integer} Float16 bit pattern +* @param {integer} target - Target index +* @param {integer} start - Start index +* @param {integer} [end] - End index (exclusive) +* @throws {TypeError} If any index is not an integer +* @throws {RangeError} If any index is out of bounds +* @returns {Float16Array} Modified array */ -Float16Array.toFloat16 = function( val ) { - var floatView = new Float32Array( 1 ); - var intView = new Uint32Array( floatView.buffer ); - var sign; - var exponent; - var fraction; - var exp; - var frac; - - floatView[ 0 ] = val; - var x = intView[ 0 ]; - - sign = ( x >> 16 ) & 0x8000; - exponent = ( ( x >> 23 ) & 0xff ) - 127; - fraction = x & 0x7fffff; - - if ( exponent <= -15 ) { - if ( exponent < -24 ) { - return sign; - } - return sign | ( ( ( fraction | 0x800000 ) >> ( -exponent - 1 ) ) >> 13 ); - } - - if ( exponent >= 16 ) { - return sign | 0x7c00; - } - - exp = exponent + 15; - frac = fraction >> 13; - return sign | ( exp << 10 ) | frac; +Float16Array.prototype.copyWithin = function( target, start, end ) { + var to; + var from; + var final; + var count; + var i; + + if ( !isInteger( target ) ) { + throw new TypeError( format( 'invalid argument. Target index must be an integer. Value: `%s`.', target ) ); + } + if ( !isInteger( start ) ) { + throw new TypeError( format( 'invalid argument. Start index must be an integer. Value: `%s`.', start ) ); + } + if ( end !== void 0 && !isInteger( end ) ) { + throw new TypeError( format( 'invalid argument. End index must be an integer when provided. Value: `%s`.', end ) ); + } + + to = ( target < 0 ) ? this.length + target : target; + from = ( start < 0 ) ? this.length + start : start; + final = ( end === void 0 ) ? this.length : ( end < 0 ? this.length + end : end ); + + if ( to < 0 || to > this.length ) { + throw new RangeError( format( 'invalid argument. Target index is out of bounds. Value: `%s`.', target ) ); + } + if ( from < 0 || from > this.length ) { + throw new RangeError( format( 'invalid argument. Start index is out of bounds. Value: `%s`.', start ) ); + } + if ( final < 0 || final > this.length ) { + throw new RangeError( format( 'invalid argument. End index is out of bounds. Value: `%s`.', end ) ); + } + + count = min( final - from, this.length - to ); + + if ( from < to && to < ( from + count ) ) { + for ( i = count - 1; i >= 0; i-- ) { + this._buffer[ to + i ] = this._buffer[ from + i ]; + } + } else { + for ( i = 0; i < count; i++ ) { + this._buffer[ to + i ] = this._buffer[ from + i ]; + } + } + return this; }; /** -* Converts a float16 binary representation to float32. +* Return an iterator over [index, value] pairs. * -* @param {integer} bits - Float16 bit pattern -* @returns {number} Float32 value +* @returns {Object} Iterator */ -Float16Array.fromFloat16 = function( bits ) { - var sign = ( bits & 0x8000 ) << 16; - var exponent = ( bits & 0x7C00 ) >> 10; - var fraction = bits & 0x03FF; - var intView = new Uint32Array( 1 ); - var floatView; - var result; - var exp32; - var frac32; - - if ( exponent === 0 ) { - if ( fraction === 0 ) { - result = sign; - } else { - exponent = 1; - while ( ( fraction & 0x0400 ) === 0 ) { - fraction <<= 1; - exponent -= 1; - } - fraction &= ~0x0400; - exp32 = 127 - 15 - exponent; - frac32 = fraction << 13; - result = sign | ( exp32 << 23 ) | frac32; - } - } else if ( exponent === 0x1F ) { - result = sign | 0x7F800000 | ( fraction << 13 ); - } else { - exp32 = exponent - 15 + 127; - result = sign | ( exp32 << 23 ) | ( fraction << 13 ); - } - - intView[ 0 ] = result; - floatView = new Float32Array( intView.buffer ); - return floatView[ 0 ]; +Float16Array.prototype.entries = function() { + var self = this; + var i = 0; + + function nextEntries() { + if ( i < self.length ) { + var v = [ i, self.get( i ) ]; + i += 1; + return { + value: v, + done: false + }; + } + return { + value: void 0, + done: true + }; + } + + function getEntriesIterator() { + return iterator; + } + + var iterator = { + next: nextEntries + }; + + if ( typeof Symbol === 'function' && Symbol.iterator ) { + iterator[ Symbol.iterator ] = getEntriesIterator; + } + + return iterator; }; /** -* Create a new Float16Array from a variable number of arguments. +* Test whether all elements pass the test. * -* @returns {Float16Array} New instance +* @param {Function} callback - Predicate function +* @param {*} [thisArg] - Context +* @returns {boolean} Whether all pass */ -Float16Array.of = function() { - var len = arguments.length; - var result = new Float16Array( len ); - var i; - - for ( i = 0; i < len; i++ ) { - result.set( i, arguments[ i ] ); - } - return result; +Float16Array.prototype.every = function( callback, thisArg ) { + var i; + if ( !isFunction( callback ) ) { + throw new TypeError( format( 'invalid argument. Callback must be a function. Value: `%s`.', callback ) ); + } + for ( i = 0; i < this.length; i++ ) { + if ( !callback.call( thisArg, this.get( i ), i, this ) ) { + return false; + } + } + return true; }; /** -* Create a new Float16Array from an array-like or iterable object. +* Fill the array with a static value. * -* @param {*} source - Source to convert -* @param {Function} [mapFn] - Optional mapping function -* @param {*} [thisArg] - Execution context for mapFn -* @returns {Float16Array} New instance +* @param {*} value - Value to fill +* @param {integer} [start=0] - Start index +* @param {integer} [end=array.length] - End index +* @throws {TypeError} If start or end are not integers +* @returns {Float16Array} Modified array */ -Float16Array.from = function( source, mapFn, thisArg ) { - if ( source == null ) { - throw new TypeError( 'invalid argument. Source cannot be null or undefined. Value: `' + source + '`.' ); - } - if ( typeof mapFn !== 'undefined' && typeof mapFn !== 'function' ) { - throw new TypeError( 'invalid argument. When provided, mapFn must be a function. Value: `' + mapFn + '`.' ); - } - - var values = []; - var i = 0; - var item; - - // Handle iterable (with Symbol.iterator) or array-like - if ( typeof source.length === 'number' ) { - for ( i = 0; i < source.length; i++ ) { - values.push( source[ i ] ); - } - } else if ( typeof source === 'object' && typeof source[ Symbol.iterator ] === 'function' ) { - var iterator = source[ Symbol.iterator ](); - while ( true ) { - var next = iterator.next(); - if ( next.done ) { - break; - } - values.push( next.value ); - } - } else { - throw new TypeError( 'invalid argument. Source must be iterable or array-like. Value: `' + source + '`.' ); - } - - if ( mapFn ) { - for ( i = 0; i < values.length; i++ ) { - values[ i ] = mapFn.call( thisArg, values[ i ], i ); - } - } - - var result = new Float16Array( values.length ); - for ( i = 0; i < values.length; i++ ) { - result.set( i, values[ i ] ); - } - return result; +function createFloat16Getter( index ) { + return function getFloat16Index() { + return this.get( index ); + }; +} + +function createFloat16Setter( index ) { + return function setFloat16Index( val ) { + this.set( index, val ); + }; +} +Float16Array.prototype.fill = function( value, start, end ) { + var i; + var bits; + var s; + var e; + + if ( start !== void 0 && !isInteger( start ) ) { + throw new TypeError( format( 'invalid argument. Start index must be an integer. Value: `%s`.', start ) ); + } + if ( end !== void 0 && !isInteger( end ) ) { + throw new TypeError( format( 'invalid argument. End index must be an integer. Value: `%s`.', end ) ); + } + + s = ( start === void 0 ) ? 0 : ( start < 0 ? max( this.length + start, 0 ) : min( start, this.length ) ); + e = ( end === void 0 ) ? this.length : ( end < 0 ? max( this.length + end, 0 ) : min( end, this.length ) ); + + bits = Float16Array.toFloat16( value ); + + for ( i = s; i < e; i++ ) { + this._buffer[ i ] = bits; + Object.defineProperty( this, i, { + get: createFloat16Getter( i ), + set: createFloat16Setter( i ), + enumerable: true, + configurable: true + }); + } + return this; }; /** -* Get a value at a given index. +* Creates a new Float16Array with all elements that pass the test implemented by the provided function. * -* @param {NonNegativeInteger} index - Array index -* @returns {number} Float32 value at index +* @param {Function} callback - Function to test each element +* @param {*} [thisArg] - Value to use as `this` when executing `callback` +* @throws {TypeError} Must provide a function as callback +* @returns {Float16Array} New Float16Array with filtered elements */ -Float16Array.prototype.get = function( index ) { - return Float16Array.fromFloat16( this._buffer[ index ] ); +Float16Array.prototype.filter = function( callback, thisArg ) { + var i; + var value; + var result; + + if ( !isFunction( callback ) ) { + throw new TypeError( format( 'invalid argument. Callback must be a function. Value: `%s`.', callback ) ); + } + + result = []; + for ( i = 0; i < this.length; i++ ) { + value = this.get( i ); + if ( callback.call( thisArg, value, i, this ) ) { + result.push( value ); + } + } + return new Float16Array( result ); }; /** -* Set a value at a given index, or copy values from an array-like source. +* Returns the value of the first element that satisfies the provided testing function. * -* @param {(NonNegativeInteger|ArrayLike)} indexOrArray - Index or source array -* @param {*} [value] - Value to set or offset if copying -* @throws {RangeError} Index or offset out of bounds -* @throws {TypeError} Invalid arguments +* @param {Function} callback - Function to execute on each value +* @param {*} [thisArg] - Value to use as `this` when executing `callback` +* @throws {TypeError} Must provide a function as callback +* @returns {*} First matching element, or undefined if no match */ -Float16Array.prototype.set = function( indexOrArray, value ) { - var i; - var len; - var offset; - var srcVal; - - if ( typeof indexOrArray === 'number' ) { - if ( indexOrArray < 0 || indexOrArray >= this.length ) { - throw new RangeError( 'invalid argument. Index must be a nonnegative integer less than the array length. Value: `' + indexOrArray + '`.' ); - } - this._buffer[ indexOrArray ] = Float16Array.toFloat16( value ); - return; - } - - offset = ( value !== undefined ) ? Number( value ) : 0; - - if ( offset < 0 || offset > this.length ) { - throw new RangeError( 'invalid argument. Offset must be a nonnegative integer less than or equal to the array length. Value: `' + offset + '`.' ); - } - - if ( - Object.prototype.toString.call( indexOrArray ) === '[object Array]' || - ( typeof indexOrArray === 'object' && typeof indexOrArray.length === 'number' ) - ) { - len = min( indexOrArray.length, this.length - offset ); - for ( i = 0; i < len; i++ ) { - srcVal = ( typeof indexOrArray.get === 'function' ) ? indexOrArray.get( i ) : indexOrArray[ i ]; - this._buffer[ offset + i ] = Float16Array.toFloat16( srcVal ); - } - return; - } - - throw new TypeError( 'invalid argument. Must provide index and value or source and offset. Value: `' + indexOrArray + '`.' ); +Float16Array.prototype.find = function( callback, thisArg ) { + var i; + var value; + + if ( !isFunction( callback ) ) { + throw new TypeError( format( 'invalid argument. Callback must be a function. Value: `%s`.', callback ) ); + } + + for ( i = 0; i < this.length; i++ ) { + value = this.get( i ); + if ( callback.call( thisArg, value, i, this ) ) { + return value; + } + } + return void 0; }; /** -* Copy a sequence of array elements within the array. +* Returns the index of the first element that satisfies the provided testing function. * -* @param {integer} target - Target index -* @param {integer} start - Start index -* @param {integer} [end] - End index (exclusive) -* @returns {Float16Array} Modified array +* @param {Function} callback - Function to execute on each value +* @param {*} [thisArg] - Value to use as `this` when executing `callback` +* @throws {TypeError} Must provide a function as callback +* @returns {integer} Index of found element, or -1 if none found */ -Float16Array.prototype.copyWithin = function( target, start, end ) { - var to = ( target < 0 ) ? this.length + target : target; - var from = ( start < 0 ) ? this.length + start : start; - var final = ( typeof end === 'undefined' ) ? this.length : ( end < 0 ? this.length + end : end ); - var count = min( final - from, this.length - to ); - var i; - - if ( from < to && to < ( from + count ) ) { - for ( i = count - 1; i >= 0; i-- ) { - this._buffer[ to + i ] = this._buffer[ from + i ]; - } - } else { - for ( i = 0; i < count; i++ ) { - this._buffer[ to + i ] = this._buffer[ from + i ]; - } - } - return this; +Float16Array.prototype.findIndex = function( callback, thisArg ) { + var i; + var value; + + if ( !isFunction( callback ) ) { + throw new TypeError( format( 'invalid argument. Callback must be a function. Value: `%s`.', callback ) ); + } + for ( i = 0; i < this.length; i++ ) { + value = this.get( i ); + if ( callback.call( thisArg, value, i, this ) ) { + return i; + } + } + return -1; }; /** -* Returns a shallow copy of a portion of the array. +* Execute a function for each element of the array. * -* @param {integer} [start=0] - Start index -* @param {integer} [end=array.length] - End index (exclusive) -* @returns {Float16Array} New subarray +* @param {Function} callback - Function to invoke +* @param {*} [thisArg] - Context for callback */ -Float16Array.prototype.slice = function( start, end ) { - var len = this.length; - var i; - var count; - var relativeStart; - var relativeEnd; - var result; - - relativeStart = ( typeof start === 'undefined' ) ? 0 : ( start < 0 ? max( len + start, 0 ) : min( start, len ) ); - relativeEnd = ( typeof end === 'undefined' ) ? len : ( end < 0 ? max( len + end, 0 ) : min( end, len ) ); - - count = max( relativeEnd - relativeStart, 0 ); - result = new Float16Array( count ); - - for ( i = 0; i < count; i++ ) { - result.set( i, this.get( relativeStart + i ) ); - } - return result; +Float16Array.prototype.forEach = function( callback, thisArg ) { + var i; + if ( !isFunction( callback ) ) { + throw new TypeError( format( 'invalid argument. Callback must be a function. Value: `%s`.', callback ) ); + } + for ( i = 0; i < this.length; i++ ) { + callback.call( thisArg, this.get( i ), i, this ); + } }; /** -* Fill the array with a static value. +* Create a new Float16Array from an array-like or iterable object. * -* @param {*} value - Value to fill -* @param {integer} [start=0] - Start index -* @param {integer} [end=array.length] - End index -* @returns {Float16Array} Modified array +* @param {*} source - Source to convert +* @param {Function} [mapFn] - Optional mapping function +* @param {*} [thisArg] - Execution context for mapFn +* @returns {Float16Array} New instance +* @throws {TypeError} If source is invalid or mapFn is not a function */ -Float16Array.prototype.fill = function( value, start, end ) { - var i; - var bits; - var s; - var e; - - s = ( typeof start === 'undefined' ) ? 0 : ( start < 0 ? max( this.length + start, 0 ) : min( start, this.length ) ); - e = ( typeof end === 'undefined' ) ? this.length : ( end < 0 ? max( this.length + end, 0 ) : min( end, this.length ) ); - - bits = Float16Array.toFloat16( value ); - - for ( i = s; i < e; i++ ) { - this._buffer[ i ] = bits; - Object.defineProperty( this, i, { - get: (function( index ) { - return function() { - return this.get( index ); - }; - })( i ), - set: (function( index ) { - return function( val ) { - this.set( index, val ); - }; - })( i ), - enumerable: true, - configurable: true - }); - } - return this; +Float16Array.from = function( source, mapFn, thisArg ) { + if ( source == null ) { + throw new TypeError( format( 'invalid argument. Source cannot be null or undefined. Value: %s.', source ) ); + } + if ( mapFn !== void 0 && !isFunction( mapFn ) ) { + throw new TypeError( format( 'invalid argument. When provided, mapFn must be a function. Value: %s.', mapFn ) ); + } + + var values = []; + var i = 0; + + if ( typeof source.length === 'number' ) { + for ( i = 0; i < source.length; i++ ) { + values.push( source[ i ] ); + } + } + else if ( typeof source === 'object' && typeof source[ Symbol.iterator ] === 'function' ) { + var iterator = source[ Symbol.iterator ](); + while ( true ) { + var next = iterator.next(); + if ( next.done ) { + break; + } + values.push( next.value ); + } + } else { + throw new TypeError( format( 'invalid argument. Source must be iterable or array-like. Value: %s.', source ) ); + } + + if ( mapFn ) { + for ( i = 0; i < values.length; i++ ) { + values[ i ] = mapFn.call( thisArg, values[ i ], i ); + } + } + + var result = new Float16Array( values.length ); + for ( i = 0; i < values.length; i++ ) { + result.set( i, values[ i ] ); + } + return result; }; /** -* Returns a new Float16Array view on the same buffer, for the specified range. +* Converts a float16 binary representation to float32. * -* @param {integer} [begin=0] - Start index -* @param {integer} [end=this.length] - End index -* @returns {Float16Array} New Float16Array +* @param {integer} bits - Float16 bit pattern +* @throws {TypeError} Must provide a 16-bit unsigned integer +* @returns {number} Float32 value */ -Float16Array.prototype.subarray = function( begin, end ) { - var len = this.length; - var i; - var start = ( typeof begin === 'undefined' ) ? 0 : ( begin < 0 ? max( len + begin, 0 ) : min( begin, len ) ); - var finish = ( typeof end === 'undefined' ) ? len : ( end < 0 ? max( len + end, 0 ) : min( end, len ) ); - var count = max( finish - start, 0 ); - var result = new Float16Array( count ); - - for ( i = 0; i < count; i++ ) { - result.set( i, this.get( start + i ) ); - } - return result; +Float16Array.fromFloat16 = function( bits ) { + var sign; + var exponent; + var fraction; + var intView; + var floatView; + var result; + var exp32; + var frac32; + + if ( !isNonNegativeInteger( bits ) || bits > 0xFFFF ) { + throw new TypeError( format( 'invalid argument. Must provide a 16-bit unsigned integer. Value: `%s`.', bits ) ); + } + + sign = ( bits & 0x8000 ) << 16; + exponent = ( bits & 0x7C00 ) >> 10; + fraction = bits & 0x03FF; + intView = new Uint32Array( 1 ); + + if ( exponent === 0 ) { + if ( fraction === 0 ) { + result = sign; + } else { + exponent = 1; + while ( ( fraction & 0x0400 ) === 0 ) { + fraction <<= 1; + exponent -= 1; + } + fraction &= ~0x0400; + exp32 = 127 - 15 - exponent; + frac32 = fraction << 13; + result = sign | ( exp32 << 23 ) | frac32; + } + } else if ( exponent === 0x1F ) { + result = sign | 0x7F800000 | ( fraction << 13 ); + } else { + exp32 = exponent - 15 + 127; + result = sign | ( exp32 << 23 ) | ( fraction << 13 ); + } + + intView[ 0 ] = result; + floatView = new Float32Array( intView.buffer ); + return floatView[ 0 ]; }; /** -* Reverse the array in place. +* Get a value at a given index. * -* @returns {Float16Array} Modified array +* @param {NonNegativeInteger} index - Array index +* @throws {RangeError} If index is out of bounds +* @returns {number} Float32 value at index */ -Float16Array.prototype.reverse = function() { - var i; - var j; - var tmp; - var mid = floor( this.length / 2 ); - for ( i = 0; i < mid; i++ ) { - j = this.length - 1 - i; - tmp = this._buffer[ i ]; - this._buffer[ i ] = this._buffer[ j ]; - this._buffer[ j ] = tmp; - } - return this; +Float16Array.prototype.get = function( index ) { + if ( !isNonNegativeInteger( index ) || index >= this.length ) { + throw new RangeError( format( 'invalid argument. Index out of bounds. Value: `%s`.', index ) ); + } + return Float16Array.fromFloat16( this._buffer[ index ] ); }; /** -* Sort the array in place. +* Determines whether the array includes a certain value among its entries. * -* @param {Function} [compareFn] - Optional comparison function -* @returns {Float16Array} Sorted array +* @param {*} searchElement - Element to search for +* @param {integer} [fromIndex=0] - Index to start the search at +* @throws {TypeError} If fromIndex is not an integer +* @returns {boolean} True if the value is found, false otherwise */ -Float16Array.prototype.sort = function( compareFn ) { - var values = []; - var i; - var j; - - for ( i = 0; i < this.length; i++ ) { - values.push( this.get( i ) ); - } - - values.sort( function( a, b ) { - if ( compareFn ) { - return compareFn( a, b ); - } - if ( a !== a ) { - return 1; - } - if ( b !== b ) { - return -1; - } - return a - b; - }); - - for ( j = 0; j < this.length; j++ ) { - this.set( j, values[ j ] ); - } - return this; +Float16Array.prototype.includes = function( searchElement, fromIndex ) { + var i; + var start; + + if ( fromIndex !== void 0 && !isInteger( fromIndex ) ) { + throw new TypeError( format( 'invalid argument. FromIndex must be an integer. Value: `%s`.', fromIndex ) ); + } + + start = ( fromIndex === void 0 ) ? 0 : fromIndex; + if ( start < 0 ) { + start = max( this.length + start, 0 ); + } + + for ( i = start; i < this.length; i++ ) { + if ( this.get( i ) === searchElement ) { + return true; + } + } + return false; }; /** -* Apply a function to each element and return a new Float16Array. +* Returns the first index at which a given element can be found in the array, or -1 if it is not present. * -* @param {Function} callback - Mapping function -* @param {*} [thisArg] - Context for callback -* @returns {Float16Array} Mapped array +* @param {*} searchElement - Element to locate +* @param {integer} [fromIndex=0] - Index to start the search at +* @throws {TypeError} If fromIndex is not an integer +* @returns {integer} Index of the element, or -1 if not found */ -Float16Array.prototype.map = function( callback, thisArg ) { - var result = new Float16Array( this.length ); - var i; - var val; - if ( typeof callback !== 'function' ) { - throw new TypeError( 'invalid argument. Callback must be a function. Value: `' + callback + '`.' ); - } - for ( i = 0; i < this.length; i++ ) { - val = callback.call( thisArg, this.get( i ), i, this ); - result.set( i, val ); - } - return result; +Float16Array.prototype.indexOf = function( searchElement, fromIndex ) { + var i; + var start; + + if ( fromIndex !== void 0 && !isInteger( fromIndex ) ) { + throw new TypeError( format( 'invalid argument. FromIndex must be an integer. Value: `%s`.', fromIndex ) ); + } + + start = ( fromIndex === void 0 ) ? 0 : fromIndex; + if ( start < 0 ) { + start = max( this.length + start, 0 ); + } + + for ( i = start; i < this.length; i++ ) { + if ( this.get( i ) === searchElement ) { + return i; + } + } + return -1; }; /** -* Execute a function for each element of the array. +* Joins all elements of the Float16Array into a string, separated by a specified separator. * -* @param {Function} callback - Function to invoke -* @param {*} [thisArg] - Context for callback +* @param {string} [separator=','] - Separator to insert between values +* @returns {string} Joined string */ -Float16Array.prototype.forEach = function( callback, thisArg ) { - var i; - if ( typeof callback !== 'function' ) { - throw new TypeError( 'invalid argument. Callback must be a function. Value: `' + callback + '`.' ); - } - for ( i = 0; i < this.length; i++ ) { - callback.call( thisArg, this.get( i ), i, this ); - } +Float16Array.prototype.join = function( separator ) { + var result = ''; + var i; + + separator = ( typeof separator === 'undefined' ) ? ',' : String( separator ); + + for ( i = 0; i < this.length; i++ ) { + if ( i > 0 ) { + result += separator; + } + result += String( this.get( i ) ); + } + return result; }; /** -* Reduce the array to a single value (left-to-right). +* Return an iterator over indices. * -* @param {Function} callback - Reducer function -* @param {*} [initialValue] - Initial value -* @returns {*} Accumulated result +* @returns {Object} Iterator */ -Float16Array.prototype.reduce = function( callback, initialValue ) { - var i; - var acc; - var len; - - if ( typeof callback !== 'function' ) { - throw new TypeError( 'invalid argument. Callback must be a function. Value: `' + callback + '`.' ); - } - len = this.length; - if ( arguments.length >= 2 ) { - acc = initialValue; - i = 0; - } else { - if ( len === 0 ) { - throw new TypeError( 'invalid operation. Reduce of empty array with no initial value.' ); - } - acc = this.get( 0 ); - i = 1; - } - for ( ; i < len; i++ ) { - acc = callback( acc, this.get( i ), i, this ); - } - return acc; +Float16Array.prototype.keys = function() { + var self = this; + var i = 0; + + function nextKeys() { + if ( i < self.length ) { + var v = i; + i += 1; + return { + value: v, + done: false + }; + } + return { + value: void 0, + done: true + }; + } + + function getKeysIterator() { + return iterator; + } + + var iterator = { + next: nextKeys + }; + + if ( typeof Symbol === 'function' && Symbol.iterator ) { + iterator[ Symbol.iterator ] = getKeysIterator; + } + + return iterator; }; /** -* Reduce the array to a single value (right-to-left). +* Returns the last index at which a given element can be found in the array, or -1 if it is not present. * -* @param {Function} callback - Reducer function -* @param {*} [initialValue] - Initial value -* @returns {*} Accumulated result +* @param {*} searchElement - Element to locate +* @param {integer} [fromIndex=this.length-1] - Index to start the search backwards from +* @throws {TypeError} If fromIndex is not an integer +* @returns {integer} Last index of the element, or -1 if not found */ -Float16Array.prototype.reduceRight = function( callback, initialValue ) { - var i; - var acc; - var len; - - if ( typeof callback !== 'function' ) { - throw new TypeError( 'invalid argument. Callback must be a function. Value: `' + callback + '`.' ); - } - len = this.length; - if ( arguments.length >= 2 ) { - acc = initialValue; - i = len - 1; - } else { - if ( len === 0 ) { - throw new TypeError( 'invalid operation. Reduce of empty array with no initial value.' ); - } - acc = this.get( len - 1 ); - i = len - 2; - } - for ( ; i >= 0; i-- ) { - acc = callback( acc, this.get( i ), i, this ); - } - return acc; +Float16Array.prototype.lastIndexOf = function( searchElement, fromIndex ) { + var i; + + if ( fromIndex !== void 0 && !isInteger( fromIndex ) ) { + throw new TypeError( format( 'invalid argument. FromIndex must be an integer. Value: `%s`.', fromIndex ) ); + } + + i = ( fromIndex !== void 0 ) ? fromIndex : this.length - 1; + + if ( i >= this.length ) { + i = this.length - 1; + } + if ( i < 0 ) { + i = this.length + i; + } + + for ( ; i >= 0; i-- ) { + if ( Object.is( this.get( i ), searchElement ) ) { + return i; + } + } + return -1; }; /** -* Test whether at least one element passes the test. +* Apply a function to each element and return a new Float16Array. * -* @param {Function} callback - Predicate function -* @param {*} [thisArg] - Context -* @returns {boolean} Whether at least one passes +* @param {Function} callback - Mapping function +* @param {*} [thisArg] - Context for callback +* @returns {Float16Array} Mapped array */ -Float16Array.prototype.some = function( callback, thisArg ) { - var i; - var value; - - if ( typeof callback !== 'function' ) { - throw new TypeError( 'invalid argument. Callback must be a function. Value: `' + callback + '`.' ); - } - for ( i = 0; i < this.length; i++ ) { - value = this.get( i ); - if ( callback.call( thisArg, value, i, this ) ) { - return true; - } - } - return false; +Float16Array.prototype.map = function( callback, thisArg ) { + var result = new Float16Array( this.length ); + var i; + var val; + if ( !isFunction( callback ) ) { + throw new TypeError( format( 'invalid argument. Callback must be a function. Value: `%s`.', callback ) ); + } + for ( i = 0; i < this.length; i++ ) { + val = callback.call( thisArg, this.get( i ), i, this ); + result.set( i, val ); + } + return result; }; /** -* Test whether all elements pass the test. +* Create a new Float16Array from a variable number of arguments. * -* @param {Function} callback - Predicate function -* @param {*} [thisArg] - Context -* @returns {boolean} Whether all pass +* @returns {Float16Array} New instance */ -Float16Array.prototype.every = function( callback, thisArg ) { - var i; - if ( typeof callback !== 'function' ) { - throw new TypeError( 'invalid argument. Callback must be a function. Value: `' + callback + '`.' ); - } - for ( i = 0; i < this.length; i++ ) { - if ( !callback.call( thisArg, this.get( i ), i, this ) ) { - return false; - } - } - return true; +Float16Array.of = function() { + var len = arguments.length; + var result = new Float16Array( len ); + var i; + + for ( i = 0; i < len; i++ ) { + result.set( i, arguments[ i ] ); + } + return result; }; /** -* Returns the first index at which a given element can be found in the array, or -1 if it is not present. +* Reduce the array to a single value (left-to-right). * -* @param {*} searchElement - Element to locate -* @param {integer} [fromIndex=0] - Index to start the search at -* @returns {integer} Index of the element, or -1 if not found +* @param {Function} callback - Reducer function +* @param {*} [initialValue] - Initial value +* @returns {*} Accumulated result */ -Float16Array.prototype.indexOf = function( searchElement, fromIndex ) { - var i; - var start; - - start = ( typeof fromIndex === 'undefined' ) ? 0 : fromIndex; - - if ( start < 0 ) { - start = max( this.length + start, 0 ); - } - - for ( i = start; i < this.length; i++ ) { - if ( this.get( i ) === searchElement ) { - return i; - } - } - return -1; +Float16Array.prototype.reduce = function( callback, initialValue ) { + var i; + var acc; + var len; + + if ( !isFunction( callback ) ) { + throw new TypeError( format( 'invalid argument. Callback must be a function. Value: `%s`.', callback ) ); + } + len = this.length; + if ( arguments.length >= 2 ) { + acc = initialValue; + i = 0; + } else { + if ( len === 0 ) { + throw new TypeError( format( 'invalid operation. Reduce of empty array with no initial value.' ) ); + } + acc = this.get( 0 ); + i = 1; + } + for ( ; i < len; i++ ) { + acc = callback( acc, this.get( i ), i, this ); + } + return acc; }; /** -* Returns the last index at which a given element can be found in the array, or -1 if it is not present. +* Reduce the array to a single value (right-to-left). * -* @param {*} searchElement - Element to locate -* @param {integer} [fromIndex=this.length-1] - Index to start the search backwards from -* @returns {integer} Last index of the element, or -1 if not found +* @param {Function} callback - Reducer function +* @param {*} [initialValue] - Initial value +* @returns {*} Accumulated result */ -Float16Array.prototype.lastIndexOf = function( searchElement, fromIndex ) { - var i = ( typeof fromIndex !== 'undefined' ) ? fromIndex : this.length - 1; - - if ( i >= this.length ) { - i = this.length - 1; - } - if ( i < 0 ) { - i = this.length + i; - } - - for ( ; i >= 0; i-- ) { - if ( Object.is( this.get( i ), searchElement ) ) { - return i; - } - } - return -1; +Float16Array.prototype.reduceRight = function( callback, initialValue ) { + var i; + var acc; + var len; + + if ( !isFunction( callback ) ) { + throw new TypeError( format( 'invalid argument. Callback must be a function. Value: `%s`.', callback ) ); + } + len = this.length; + if ( arguments.length >= 2 ) { + acc = initialValue; + i = len - 1; + } else { + if ( len === 0 ) { + throw new TypeError( format( 'invalid operation. Reduce of empty array with no initial value.' ) ); + } + acc = this.get( len - 1 ); + i = len - 2; + } + for ( ; i >= 0; i-- ) { + acc = callback( acc, this.get( i ), i, this ); + } + return acc; }; /** -* Joins all elements of the Float16Array into a string, separated by a specified separator. +* Reverse the array in place. * -* @param {string} [separator=','] - Separator to insert between values -* @returns {string} Joined string +* @returns {Float16Array} Modified array */ -Float16Array.prototype.join = function( separator ) { - var result = ''; - var i; - - separator = ( typeof separator === 'undefined' ) ? ',' : String( separator ); - - for ( i = 0; i < this.length; i++ ) { - if ( i > 0 ) { - result += separator; - } - result += String( this.get( i ) ); - } - return result; +Float16Array.prototype.reverse = function() { + var i; + var j; + var tmp; + var mid = floor( this.length / 2 ); + for ( i = 0; i < mid; i++ ) { + j = this.length - 1 - i; + tmp = this._buffer[ i ]; + this._buffer[ i ] = this._buffer[ j ]; + this._buffer[ j ] = tmp; + } + return this; }; /** -* Determines whether the array includes a certain value among its entries. +* Set a value at a given index, or copy values from an array-like source. * -* @param {*} searchElement - Element to search for -* @param {integer} [fromIndex=0] - Index to start the search at -* @returns {boolean} True if the value is found, false otherwise +* @param {(NonNegativeInteger|ArrayLike)} indexOrArray - Index or source array +* @param {*} [value] - Value to set or offset if copying +* @throws {RangeError} Index or offset out of bounds +* @throws {TypeError} Invalid arguments */ -Float16Array.prototype.includes = function( searchElement, fromIndex ) { - var i; - var start = ( typeof fromIndex === 'undefined' ) ? 0 : fromIndex; - - if ( start < 0 ) { - start = max( this.length + start, 0 ); - } - - for ( i = start; i < this.length; i++ ) { - if ( this.get( i ) === searchElement ) { - return true; - } - } - return false; +Float16Array.prototype.set = function( indexOrArray, value ) { + var i; + var len; + var offset; + var srcVal; + + if ( isNonNegativeInteger( indexOrArray ) ) { + if ( indexOrArray >= this.length ) { + throw new RangeError( format( 'invalid argument. Index must be a nonnegative integer less than the array length. Value: `%s`.', indexOrArray ) ); + } + this._buffer[ indexOrArray ] = Float16Array.toFloat16( value ); + return; + } + + offset = ( value !== void 0 ) ? Number( value ) : 0; + if ( !isNonNegativeInteger( offset ) || offset > this.length ) { + throw new RangeError( format( 'invalid argument. Offset must be a nonnegative integer less than or equal to the array length. Value: `%s`.', offset ) ); + } + + if ( isArray( indexOrArray ) || isCollection( indexOrArray ) ) { + len = min( indexOrArray.length, this.length - offset ); + for ( i = 0; i < len; i++ ) { + srcVal = ( typeof indexOrArray.get === 'function' ) ? indexOrArray.get( i ) : indexOrArray[ i ]; + this._buffer[ offset + i ] = Float16Array.toFloat16( srcVal ); + } + return; + } + + throw new TypeError( format( 'invalid argument. Must provide index and value or source and offset. Value: `%s`.', indexOrArray ) ); }; /** -* Creates a new Float16Array with all elements that pass the test implemented by the provided function. +* Returns a shallow copy of a portion of the array. * -* @param {Function} callback - Function to test each element -* @param {*} [thisArg] - Value to use as `this` when executing `callback` -* @throws {TypeError} Must provide a function as callback -* @returns {Float16Array} New Float16Array with filtered elements +* @param {integer} [start=0] - Start index +* @param {integer} [end=array.length] - End index (exclusive) +* @throws {TypeError} If start or end is not an integer +* @returns {Float16Array} New subarray */ -Float16Array.prototype.filter = function( callback, thisArg ) { - var i; - var value; - var result; - - if ( typeof callback !== 'function' ) { - throw new TypeError( 'invalid argument. Must provide a function. Value: `' + callback + '`.' ); - } - - result = []; - for ( i = 0; i < this.length; i++ ) { - value = this.get( i ); - if ( callback.call( thisArg, value, i, this ) ) { - result.push( value ); - } - } - return new Float16Array( result ); +Float16Array.prototype.slice = function( start, end ) { + var len = this.length; + var i; + var count; + var relativeStart; + var relativeEnd; + var result; + + if ( start !== void 0 && !isInteger( start ) ) { + throw new TypeError( format( 'invalid argument. Start index must be an integer. Value: `%s`.', start ) ); + } + if ( end !== void 0 && !isInteger( end ) ) { + throw new TypeError( format( 'invalid argument. End index must be an integer. Value: `%s`.', end ) ); + } + + relativeStart = ( start === void 0 ) ? 0 : ( start < 0 ? max( len + start, 0 ) : min( start, len ) ); + relativeEnd = ( end === void 0 ) ? len : ( end < 0 ? max( len + end, 0 ) : min( end, len ) ); + + count = max( relativeEnd - relativeStart, 0 ); + result = new Float16Array( count ); + + for ( i = 0; i < count; i++ ) { + result.set( i, this.get( relativeStart + i ) ); + } + return result; }; /** -* Returns the value of the first element that satisfies the provided testing function. +* Test whether at least one element passes the test. * -* @param {Function} callback - Function to execute on each value -* @param {*} [thisArg] - Value to use as `this` when executing `callback` -* @throws {TypeError} Must provide a function as callback -* @returns {*} First matching element, or undefined if no match +* @param {Function} callback - Predicate function +* @param {*} [thisArg] - Context +* @returns {boolean} Whether at least one passes */ -Float16Array.prototype.find = function( callback, thisArg ) { - var i; - var value; - - if ( typeof callback !== 'function' ) { - throw new TypeError( 'invalid argument. Must provide a function. Value: `' + callback + '`.' ); - } - - for ( i = 0; i < this.length; i++ ) { - value = this.get( i ); - if ( callback.call( thisArg, value, i, this ) ) { - return value; - } - } - return void 0; // returns undefined +Float16Array.prototype.some = function( callback, thisArg ) { + var i; + var value; + + if ( !isFunction( callback ) ) { + throw new TypeError( format( 'invalid argument. Callback must be a function. Value: `%s`.', callback ) ); + } + for ( i = 0; i < this.length; i++ ) { + value = this.get( i ); + if ( callback.call( thisArg, value, i, this ) ) { + return true; + } + } + return false; }; /** -* Returns the index of the first element that satisfies the provided testing function. +* Sort the array in place. * -* @param {Function} callback - Function to execute on each value -* @param {*} [thisArg] - Value to use as `this` when executing `callback` -* @throws {TypeError} Must provide a function as callback -* @returns {integer} Index of found element, or -1 if none found +* @param {Function} [compareFn] - Optional comparison function +* @throws {TypeError} If compareFn is not a function +* @returns {Float16Array} Sorted array */ -Float16Array.prototype.findIndex = function( callback, thisArg ) { - var i; - var value; - - if ( typeof callback !== 'function' ) { - throw new TypeError( 'invalid argument. Must provide a function. Value: `' + callback + '`.' ); - } - for ( i = 0; i < this.length; i++ ) { - value = this.get( i ); - if ( callback.call( thisArg, value, i, this ) ) { - return i; - } - } - return -1; +function getComparator( compareFn ) { + return function compareFloat16( a, b ) { + if ( compareFn ) { + return compareFn( a, b ); + } + if ( a !== a ) { + return 1; + } + if ( b !== b ) { + return -1; + } + return a - b; + }; +} + +Float16Array.prototype.sort = function( compareFn ) { + var values = []; + var i; + var j; + + if ( compareFn !== void 0 && !isFunction( compareFn ) ) { + throw new TypeError( format( 'invalid argument. Comparison function must be a function. Value: `%s`.', compareFn ) ); + } + + for ( i = 0; i < this.length; i++ ) { + values.push( this.get( i ) ); + } + + values.sort( getComparator( compareFn ) ); + + for ( j = 0; j < this.length; j++ ) { + this.set( j, values[ j ] ); + } + return this; }; /** -* Return an iterator over [index, value] pairs. +* Returns a new Float16Array view on the same buffer, for the specified range. * -* @returns {Object} Iterator +* @param {integer} [begin=0] - Start index +* @param {integer} [end=this.length] - End index +* @returns {Float16Array} New Float16Array */ -Float16Array.prototype.entries = function() { - var self = this; - var i = 0; - - return { - next: function() { - if ( i < self.length ) { - var v = [ i, self.get( i ) ]; - i += 1; - return { - value: v, - done: false - }; - } - return { - value: void 0, - done: true - }; - } - }; +Float16Array.prototype.subarray = function( begin, end ) { + var len = this.length; + var i; + var start = ( typeof begin === 'undefined' ) ? 0 : ( begin < 0 ? max( len + begin, 0 ) : min( begin, len ) ); + var finish = ( typeof end === 'undefined' ) ? len : ( end < 0 ? max( len + end, 0 ) : min( end, len ) ); + var count = max( finish - start, 0 ); + var result = new Float16Array( count ); + + for ( i = 0; i < count; i++ ) { + result.set( i, this.get( start + i ) ); + } + return result; }; /** -* Return an iterator over indices. +* Converts a float32 number to a float16 (binary) representation. * -* @returns {Object} Iterator +* @param {*} val - Float32 value (coerced if not number) +* @returns {integer} Float16 bit pattern */ -Float16Array.prototype.keys = function() { - var self = this; - var i = 0; - - return { - next: function() { - if ( i < self.length ) { - var v = i; - i += 1; - return { - value: v, - done: false - }; - } - return { - value: void 0, - done: true - }; - } - }; +Float16Array.toFloat16 = function( val ) { + var num = Number( val ); + + var floatView = new Float32Array( 1 ); + var intView = new Uint32Array( floatView.buffer ); + var sign; + var exponent; + var fraction; + var exp; + var frac; + var x; + + floatView[ 0 ] = num; + x = intView[ 0 ]; + + sign = ( x >> 16 ) & 0x8000; + exponent = ( ( x >> 23 ) & 0xff ) - 127; + fraction = x & 0x7fffff; + + if ( exponent <= -15 ) { + if ( exponent < -24 ) { + return sign; + } + return sign | ( ( ( fraction | 0x800000 ) >> ( -exponent - 1 ) ) >> 13 ); + } + if ( exponent >= 16 ) { + return sign | 0x7c00; + } + exp = exponent + 15; + frac = fraction >> 13; + return sign | ( exp << 10 ) | frac; }; /** @@ -877,27 +1005,40 @@ Float16Array.prototype.keys = function() { * @returns {Object} Iterator */ Float16Array.prototype.values = function() { - var self = this; - var i = 0; - - return { - next: function() { - if ( i < self.length ) { - var v = self.get( i ); - i += 1; - return { - value: v, - done: false - }; - } - return { - value: void 0, - done: true - }; - } - }; + var self = this; + var i = 0; + + function nextValues() { + if ( i < self.length ) { + var v = self.get( i ); + i += 1; + return { + value: v, + done: false + }; + } + return { + value: void 0, + done: true + }; + } + + function getValuesIterator() { + return iterator; + } + + var iterator = { + next: nextValues + }; + + if ( typeof Symbol === 'function' && Symbol.iterator ) { + iterator[ Symbol.iterator ] = getValuesIterator; + } + + return iterator; }; + // EXPORTS // module.exports = Float16Array; From 1f18925d1660cf12d7f4ec45619087fd03ad4b6a Mon Sep 17 00:00:00 2001 From: Uday Kakade <141299403+udaykakade25@users.noreply.github.com> Date: Tue, 1 Jul 2025 17:13:07 +0530 Subject: [PATCH 05/45] Update package.json Signed-off-by: Uday Kakade <141299403+udaykakade25@users.noreply.github.com> --- lib/node_modules/@stdlib/array/float16/package.json | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/node_modules/@stdlib/array/float16/package.json b/lib/node_modules/@stdlib/array/float16/package.json index 34eb6b834170..63d51e83fa90 100644 --- a/lib/node_modules/@stdlib/array/float16/package.json +++ b/lib/node_modules/@stdlib/array/float16/package.json @@ -1,7 +1,7 @@ { "name": "@stdlib/array/float16", "version": "0.0.0", - "description": "Float16Array polyfill and fallback interface.", + "description": "Float16Array.", "license": "Apache-2.0", "author": { "name": "The Stdlib Authors", @@ -64,6 +64,5 @@ "half", "half-precision", "ieee754" - ], - "type": "commonjs" + ] } From f73e84e7d32f3b8b10e1e90ce1e71c8bfb8aa277 Mon Sep 17 00:00:00 2001 From: Uday Kakade <141299403+udaykakade25@users.noreply.github.com> Date: Tue, 22 Jul 2025 21:51:41 +0530 Subject: [PATCH 06/45] Update polyfill.js Signed-off-by: Uday Kakade <141299403+udaykakade25@users.noreply.github.com> --- .../@stdlib/array/float16/lib/polyfill.js | 537 ++++++++++-------- 1 file changed, 307 insertions(+), 230 deletions(-) diff --git a/lib/node_modules/@stdlib/array/float16/lib/polyfill.js b/lib/node_modules/@stdlib/array/float16/lib/polyfill.js index 47d8e126927d..89b7cbf72b51 100644 --- a/lib/node_modules/@stdlib/array/float16/lib/polyfill.js +++ b/lib/node_modules/@stdlib/array/float16/lib/polyfill.js @@ -7,7 +7,7 @@ * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * -* http://www.apache.org/licenses/LICENSE-2.0 +* http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, @@ -22,6 +22,7 @@ var setReadOnlyAccessor = require( '@stdlib/utils/define-nonenumerable-read-only-accessor' ); var isNonNegativeInteger = require( '@stdlib/assert/is-nonnegative-integer' ).isPrimitive; +var isObject = require( '@stdlib/assert/is-object' ); var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); var isInteger = require( '@stdlib/assert/is-integer' ).isPrimitive; var isCollection = require( '@stdlib/assert/is-collection' ); @@ -31,18 +32,20 @@ var min = require( '@stdlib/math/base/special/min' ); var max = require( '@stdlib/math/base/special/max' ); var isArray = require( '@stdlib/assert/is-array' ); var format = require( '@stdlib/string/format' ); +var iteratorSymbol = require( '@stdlib/symbol/iterator' ); +var hasToStringTag = require( '@stdlib/assert/has-tostringtag-support' ); // MAIN // /** -* Creates a new Float16Array. +* Creates a new Float16Array constructor. * * @constructor -* @param {(number|Array|ArrayBuffer|TypedArray)} input - Input data or buffer -* @param {NonNegativeInteger} [byteOffset=0] - Byte offset -* @param {NonNegativeInteger} [length] - Number of elements -* @throws {TypeError} Must provide valid input +* @param {(number|Array|ArrayBuffer|TypedArray)} input - input data or buffer +* @param {NonNegativeInteger} [byteOffset=0] - byte offset +* @param {NonNegativeInteger} [length] - number of elements +* @throws {TypeError} must provide valid input */ function Float16Array( input, byteOffset, length ) { var i; @@ -59,7 +62,7 @@ function Float16Array( input, byteOffset, length ) { throw new RangeError( format( 'invalid argument. Byte offset must be a non-negative integer. Value: `%s`.', byteOffset ) ); } - if ( length !== void 0 ) { + if ( arguments.length > 2 ) { if ( !isNonNegativeInteger( length ) ) { throw new RangeError( format( 'invalid argument. Length must be a non-negative integer. Value: `%s`.', length ) ); } @@ -97,10 +100,9 @@ setReadOnly( Float16Array, 'BYTES_PER_ELEMENT', 2 ); * @readonly * @type {ArrayBuffer} */ -function getBuffer() { +setReadOnlyAccessor( Float16Array.prototype, 'buffer', function getBuffer() { return this._buffer.buffer; -} -setReadOnlyAccessor( Float16Array.prototype, 'buffer', getBuffer ); +}); /** * Get the total number of bytes in the `Float16Array`. @@ -110,10 +112,9 @@ setReadOnlyAccessor( Float16Array.prototype, 'buffer', getBuffer ); * @readonly * @type {NonNegativeInteger} */ -function getByteLength() { +setReadOnlyAccessor( Float16Array.prototype, 'byteLength', function getByteLength() { return this._buffer.byteLength; -} -setReadOnlyAccessor( Float16Array.prototype, 'byteLength', getByteLength ); +}); /** * Get the byte offset within the underlying `ArrayBuffer`. @@ -123,10 +124,9 @@ setReadOnlyAccessor( Float16Array.prototype, 'byteLength', getByteLength ); * @readonly * @type {NonNegativeInteger} */ -function getByteOffset() { +setReadOnlyAccessor( Float16Array.prototype, 'byteOffset', function getByteOffset() { return this._buffer.byteOffset; -} -setReadOnlyAccessor( Float16Array.prototype, 'byteOffset', getByteOffset ); +}); /** * Size in bytes of each element. @@ -136,10 +136,9 @@ setReadOnlyAccessor( Float16Array.prototype, 'byteOffset', getByteOffset ); * @readonly * @type {PositiveInteger} */ -function getBytesPerElement() { +setReadOnlyAccessor( Float16Array.prototype, 'BYTES_PER_ELEMENT', function getBytesPerElement() { return 2; -} -setReadOnlyAccessor( Float16Array.prototype, 'BYTES_PER_ELEMENT', getBytesPerElement ); +}); /** * Returns the default string tag. @@ -149,24 +148,25 @@ setReadOnlyAccessor( Float16Array.prototype, 'BYTES_PER_ELEMENT', getBytesPerEle * @readonly * @type {string} */ -var symbolToStringTag = ( typeof Symbol !== 'undefined' && Symbol.toStringTag ) || '__toStringTag__'; - -function getToStringTag() { +var symbolToStringTag = hasToStringTag() ? Symbol.toStringTag : '__toStringTag__'; +setReadOnlyAccessor( Float16Array.prototype, symbolToStringTag, function getToStringTag() { return 'Float16Array'; -} -setReadOnlyAccessor( Float16Array.prototype, symbolToStringTag, getToStringTag ); +}); /** * Copy a sequence of array elements within the array. * -* @param {integer} target - Target index -* @param {integer} start - Start index -* @param {integer} [end] - End index (exclusive) -* @throws {TypeError} If any index is not an integer -* @throws {RangeError} If any index is out of bounds -* @returns {Float16Array} Modified array +* @name copyWithin +* @memberof Float16Array.prototype +* @type {Function} +* @param {integer} target - target index +* @param {integer} start - start index +* @param {integer} [end] - end index (exclusive) +* @throws {TypeError} if any index is not an integer +* @throws {RangeError} if any index is out of bounds +* @returns {Float16Array} modified array */ -Float16Array.prototype.copyWithin = function( target, start, end ) { +setReadOnly( Float16Array.prototype, 'copyWithin', function copyWithin( target, start, end ) { var to; var from; var final; @@ -179,13 +179,13 @@ Float16Array.prototype.copyWithin = function( target, start, end ) { if ( !isInteger( start ) ) { throw new TypeError( format( 'invalid argument. Start index must be an integer. Value: `%s`.', start ) ); } - if ( end !== void 0 && !isInteger( end ) ) { + if ( arguments.length > 2 && !isInteger( end ) ) { throw new TypeError( format( 'invalid argument. End index must be an integer when provided. Value: `%s`.', end ) ); } to = ( target < 0 ) ? this.length + target : target; from = ( start < 0 ) ? this.length + start : start; - final = ( end === void 0 ) ? this.length : ( end < 0 ? this.length + end : end ); + final = ( arguments.length < 3 ) ? this.length : ( end < 0 ? this.length + end : end ); if ( to < 0 || to > this.length ) { throw new RangeError( format( 'invalid argument. Target index is out of bounds. Value: `%s`.', target ) ); @@ -209,14 +209,17 @@ Float16Array.prototype.copyWithin = function( target, start, end ) { } } return this; -}; +}); /** * Return an iterator over [index, value] pairs. * +* @name entries +* @memberof Float16Array.prototype +* @type {Function} * @returns {Object} Iterator */ -Float16Array.prototype.entries = function() { +setReadOnly( Float16Array.prototype, 'entries', function entries() { var self = this; var i = 0; @@ -243,21 +246,24 @@ Float16Array.prototype.entries = function() { next: nextEntries }; - if ( typeof Symbol === 'function' && Symbol.iterator ) { - iterator[ Symbol.iterator ] = getEntriesIterator; + if ( iteratorSymbol ) { + iterator[ iteratorSymbol ] = getEntriesIterator; } return iterator; -}; +}); /** * Test whether all elements pass the test. * -* @param {Function} callback - Predicate function -* @param {*} [thisArg] - Context -* @returns {boolean} Whether all pass +* @name every +* @memberof Float16Array.prototype +* @type {Function} +* @param {Function} callback - predicate function +* @param {*} [thisArg] - context +* @returns {boolean} whether all pass */ -Float16Array.prototype.every = function( callback, thisArg ) { +setReadOnly( Float16Array.prototype, 'every', function every( callback, thisArg ) { var i; if ( !isFunction( callback ) ) { throw new TypeError( format( 'invalid argument. Callback must be a function. Value: `%s`.', callback ) ); @@ -268,43 +274,47 @@ Float16Array.prototype.every = function( callback, thisArg ) { } } return true; -}; +}); /** * Fill the array with a static value. * -* @param {*} value - Value to fill -* @param {integer} [start=0] - Start index -* @param {integer} [end=array.length] - End index -* @throws {TypeError} If start or end are not integers -* @returns {Float16Array} Modified array +* @name fill +* @memberof Float16Array.prototype +* @type {Function} +* @param {*} value - value to fill +* @param {integer} [start=0] - start index +* @param {integer} [end=array.length] - end index +* @throws {TypeError} if start or end are not integers +* @returns {Float16Array} modified array */ -function createFloat16Getter( index ) { - return function getFloat16Index() { - return this.get( index ); - }; -} - -function createFloat16Setter( index ) { - return function setFloat16Index( val ) { - this.set( index, val ); - }; -} -Float16Array.prototype.fill = function( value, start, end ) { +setReadOnly( Float16Array.prototype, 'fill', function fill( value, start, end ) { var i; var bits; var s; var e; - if ( start !== void 0 && !isInteger( start ) ) { + function createFloat16Getter( index ) { + return function getFloat16Index() { + return this.get( index ); + }; + } + + function createFloat16Setter( index ) { + return function setFloat16Index( val ) { + this.set( index, val ); + }; + } + + if ( arguments.length > 1 && !isInteger( start ) ) { throw new TypeError( format( 'invalid argument. Start index must be an integer. Value: `%s`.', start ) ); } - if ( end !== void 0 && !isInteger( end ) ) { - throw new TypeError( format( 'invalid argument. End index must be an integer. Value: `%s`.', end ) ); + if ( arguments.length > 2 && !isInteger( end ) ) { + throw new TypeError( format( 'invalid argument. End index must be an integer when provided. Value: `%s`.', end ) ); } - s = ( start === void 0 ) ? 0 : ( start < 0 ? max( this.length + start, 0 ) : min( start, this.length ) ); - e = ( end === void 0 ) ? this.length : ( end < 0 ? max( this.length + end, 0 ) : min( end, this.length ) ); + s = ( arguments.length < 2 ) ? 0 : ( start < 0 ? max( this.length + start, 0 ) : min( start, this.length ) ); + e = ( arguments.length < 3 ) ? this.length : ( end < 0 ? max( this.length + end, 0 ) : min( end, this.length ) ); bits = Float16Array.toFloat16( value ); @@ -318,17 +328,20 @@ Float16Array.prototype.fill = function( value, start, end ) { }); } return this; -}; +}); /** * Creates a new Float16Array with all elements that pass the test implemented by the provided function. * -* @param {Function} callback - Function to test each element -* @param {*} [thisArg] - Value to use as `this` when executing `callback` -* @throws {TypeError} Must provide a function as callback -* @returns {Float16Array} New Float16Array with filtered elements +* @name filter +* @memberof Float16Array.prototype +* @type {Function} +* @param {Function} callback - function to test each element +* @param {*} [thisArg] - value to use as `this` when executing `callback` +* @throws {TypeError} must provide a function as callback +* @returns {Float16Array} new Float16Array with filtered elements */ -Float16Array.prototype.filter = function( callback, thisArg ) { +setReadOnly( Float16Array.prototype, 'filter', function filter( callback, thisArg ) { var i; var value; var result; @@ -345,17 +358,20 @@ Float16Array.prototype.filter = function( callback, thisArg ) { } } return new Float16Array( result ); -}; +}); /** * Returns the value of the first element that satisfies the provided testing function. * -* @param {Function} callback - Function to execute on each value -* @param {*} [thisArg] - Value to use as `this` when executing `callback` -* @throws {TypeError} Must provide a function as callback -* @returns {*} First matching element, or undefined if no match +* @name find +* @memberof Float16Array.prototype +* @type {Function} +* @param {Function} callback - function to execute on each value +* @param {*} [thisArg] - value to use as `this` when executing `callback` +* @throws {TypeError} must provide a function as callback +* @returns {*} first matching element, or undefined if no match */ -Float16Array.prototype.find = function( callback, thisArg ) { +setReadOnly( Float16Array.prototype, 'find', function find( callback, thisArg ) { var i; var value; @@ -370,17 +386,20 @@ Float16Array.prototype.find = function( callback, thisArg ) { } } return void 0; -}; +}); /** * Returns the index of the first element that satisfies the provided testing function. * -* @param {Function} callback - Function to execute on each value -* @param {*} [thisArg] - Value to use as `this` when executing `callback` -* @throws {TypeError} Must provide a function as callback -* @returns {integer} Index of found element, or -1 if none found +* @name findIndex +* @memberof Float16Array.prototype +* @type {Function} +* @param {Function} callback - function to execute on each value +* @param {*} [thisArg] - value to use as `this` when executing `callback` +* @throws {TypeError} must provide a function as callback +* @returns {integer} index of found element, or -1 if none found */ -Float16Array.prototype.findIndex = function( callback, thisArg ) { +setReadOnly( Float16Array.prototype, 'findIndex', function findIndexfunction( callback, thisArg ) { var i; var value; @@ -394,15 +413,18 @@ Float16Array.prototype.findIndex = function( callback, thisArg ) { } } return -1; -}; +}); /** * Execute a function for each element of the array. * -* @param {Function} callback - Function to invoke -* @param {*} [thisArg] - Context for callback +* @name forEach +* @memberof Float16Array.prototype +* @type {Function} +* @param {Function} callback - function to invoke +* @param {*} [thisArg] - context for callback */ -Float16Array.prototype.forEach = function( callback, thisArg ) { +setReadOnly( Float16Array.prototype, 'forEach', function forEachfunction( callback, thisArg ) { var i; if ( !isFunction( callback ) ) { throw new TypeError( format( 'invalid argument. Callback must be a function. Value: `%s`.', callback ) ); @@ -410,34 +432,37 @@ Float16Array.prototype.forEach = function( callback, thisArg ) { for ( i = 0; i < this.length; i++ ) { callback.call( thisArg, this.get( i ), i, this ); } -}; +}); /** * Create a new Float16Array from an array-like or iterable object. * -* @param {*} source - Source to convert -* @param {Function} [mapFn] - Optional mapping function -* @param {*} [thisArg] - Execution context for mapFn -* @returns {Float16Array} New instance -* @throws {TypeError} If source is invalid or mapFn is not a function +* @name from +* @memberof Float16Array +* @type {Function} +* @param {*} source - source to convert +* @param {Function} [mapFn] - optional mapping function +* @param {*} [thisArg] - execution context for mapFn +* @returns {Float16Array} new instance +* @throws {TypeError} if source is invalid or mapFn is not a function */ -Float16Array.from = function( source, mapFn, thisArg ) { - if ( source == null ) { +setReadOnly( Float16Array, 'from', function from( source, mapFn, thisArg ) { + if ( source === null ) { throw new TypeError( format( 'invalid argument. Source cannot be null or undefined. Value: %s.', source ) ); } - if ( mapFn !== void 0 && !isFunction( mapFn ) ) { + if ( arguments.length > 1 && !isFunction( mapFn ) ) { throw new TypeError( format( 'invalid argument. When provided, mapFn must be a function. Value: %s.', mapFn ) ); } var values = []; var i = 0; - if ( typeof source.length === 'number' ) { + if ( isCollection( source ) ) { for ( i = 0; i < source.length; i++ ) { values.push( source[ i ] ); } } - else if ( typeof source === 'object' && typeof source[ Symbol.iterator ] === 'function' ) { + else if ( isObject( source ) && isFunction( source[ Symbol.iterator ] ) ) { var iterator = source[ Symbol.iterator ](); while ( true ) { var next = iterator.next(); @@ -461,16 +486,19 @@ Float16Array.from = function( source, mapFn, thisArg ) { result.set( i, values[ i ] ); } return result; -}; +}); /** * Converts a float16 binary representation to float32. * +* @name fromFloat16 +* @memberof Float16Array +* @type {Function} * @param {integer} bits - Float16 bit pattern -* @throws {TypeError} Must provide a 16-bit unsigned integer +* @throws {TypeError} must provide a 16-bit unsigned integer * @returns {number} Float32 value */ -Float16Array.fromFloat16 = function( bits ) { +setReadOnly( Float16Array, 'fromFloat16', function fromFloat16( bits ) { var sign; var exponent; var fraction; @@ -513,39 +541,45 @@ Float16Array.fromFloat16 = function( bits ) { intView[ 0 ] = result; floatView = new Float32Array( intView.buffer ); return floatView[ 0 ]; -}; +}); /** * Get a value at a given index. * -* @param {NonNegativeInteger} index - Array index -* @throws {RangeError} If index is out of bounds +* @name get +* @memberof Float16Array.prototype +* @type {Function} +* @param {NonNegativeInteger} index - array index +* @throws {RangeError} if index is out of bounds * @returns {number} Float32 value at index */ -Float16Array.prototype.get = function( index ) { +setReadOnly( Float16Array.prototype, 'get', function getfunction( index ) { if ( !isNonNegativeInteger( index ) || index >= this.length ) { throw new RangeError( format( 'invalid argument. Index out of bounds. Value: `%s`.', index ) ); } return Float16Array.fromFloat16( this._buffer[ index ] ); -}; +}); /** * Determines whether the array includes a certain value among its entries. * -* @param {*} searchElement - Element to search for -* @param {integer} [fromIndex=0] - Index to start the search at -* @throws {TypeError} If fromIndex is not an integer -* @returns {boolean} True if the value is found, false otherwise +* @name includes +* @memberof Float16Array.prototype +* @type {Function} +* @param {*} searchElement - element to search for +* @param {integer} [fromIndex=0] - index to start the search at +* @throws {TypeError} if fromIndex is not an integer +* @returns {boolean} true if the value is found, false otherwise */ -Float16Array.prototype.includes = function( searchElement, fromIndex ) { +setReadOnly( Float16Array.prototype, 'includes', function includes( searchElement, fromIndex ) { var i; var start; - if ( fromIndex !== void 0 && !isInteger( fromIndex ) ) { + if ( arguments.length > 1 && !isInteger( fromIndex ) ) { throw new TypeError( format( 'invalid argument. FromIndex must be an integer. Value: `%s`.', fromIndex ) ); } - start = ( fromIndex === void 0 ) ? 0 : fromIndex; + start = ( arguments.length < 2 ) ? 0 : fromIndex; if ( start < 0 ) { start = max( this.length + start, 0 ); } @@ -556,25 +590,28 @@ Float16Array.prototype.includes = function( searchElement, fromIndex ) { } } return false; -}; +}); /** * Returns the first index at which a given element can be found in the array, or -1 if it is not present. * -* @param {*} searchElement - Element to locate -* @param {integer} [fromIndex=0] - Index to start the search at -* @throws {TypeError} If fromIndex is not an integer -* @returns {integer} Index of the element, or -1 if not found +* @name indexOf +* @memberof Float16Array.prototype +* @type {Function} +* @param {*} searchElement - element to locate +* @param {integer} [fromIndex=0] - index to start the search at +* @throws {TypeError} if fromIndex is not an integer +* @returns {integer} index of the element, or -1 if not found */ -Float16Array.prototype.indexOf = function( searchElement, fromIndex ) { +setReadOnly( Float16Array.prototype, 'indexOf', function indexOf( searchElement, fromIndex ) { var i; var start; - if ( fromIndex !== void 0 && !isInteger( fromIndex ) ) { + if ( arguments.length > 1 && !isInteger( fromIndex ) ) { throw new TypeError( format( 'invalid argument. FromIndex must be an integer. Value: `%s`.', fromIndex ) ); } - start = ( fromIndex === void 0 ) ? 0 : fromIndex; + start = ( arguments.length < 2 ) ? 0 : fromIndex; if ( start < 0 ) { start = max( this.length + start, 0 ); } @@ -585,19 +622,22 @@ Float16Array.prototype.indexOf = function( searchElement, fromIndex ) { } } return -1; -}; +}); /** * Joins all elements of the Float16Array into a string, separated by a specified separator. * -* @param {string} [separator=','] - Separator to insert between values -* @returns {string} Joined string +* @name join +* @memberof Float16Array.prototype +* @type {Function} +* @param {string} [separator=','] - separator to insert between values +* @returns {string} joined string */ -Float16Array.prototype.join = function( separator ) { +setReadOnly( Float16Array.prototype, 'join', function join( separator ) { var result = ''; var i; - separator = ( typeof separator === 'undefined' ) ? ',' : String( separator ); + separator = ( arguments.length < 0 ) ? ',' : String( separator ); for ( i = 0; i < this.length; i++ ) { if ( i > 0 ) { @@ -606,14 +646,17 @@ Float16Array.prototype.join = function( separator ) { result += String( this.get( i ) ); } return result; -}; +}); /** * Return an iterator over indices. * +* @name keys +* @memberof Float16Array.prototype +* @type {Function} * @returns {Object} Iterator */ -Float16Array.prototype.keys = function() { +setReadOnly( Float16Array.prototype, 'keys', function keys() { var self = this; var i = 0; @@ -640,29 +683,32 @@ Float16Array.prototype.keys = function() { next: nextKeys }; - if ( typeof Symbol === 'function' && Symbol.iterator ) { - iterator[ Symbol.iterator ] = getKeysIterator; + if ( iteratorSymbol ) { + iterator[ iteratorSymbol ] = getKeysIterator; } return iterator; -}; +}); /** * Returns the last index at which a given element can be found in the array, or -1 if it is not present. * -* @param {*} searchElement - Element to locate -* @param {integer} [fromIndex=this.length-1] - Index to start the search backwards from -* @throws {TypeError} If fromIndex is not an integer -* @returns {integer} Last index of the element, or -1 if not found +* @name lastIndexOf +* @memberof Float16Array.prototype +* @type {Function} +* @param {*} searchElement - element to locate +* @param {integer} [fromIndex=this.length-1] - index to start the search backwards from +* @throws {TypeError} if fromIndex is not an integer +* @returns {integer} last index of the element, or -1 if not found */ -Float16Array.prototype.lastIndexOf = function( searchElement, fromIndex ) { +setReadOnly( Float16Array.prototype, 'lastIndexOf', function lastIndexOffunction( searchElement, fromIndex ) { var i; - if ( fromIndex !== void 0 && !isInteger( fromIndex ) ) { + if ( arguments.length > 1 && !isInteger( fromIndex ) ) { throw new TypeError( format( 'invalid argument. FromIndex must be an integer. Value: `%s`.', fromIndex ) ); } - i = ( fromIndex !== void 0 ) ? fromIndex : this.length - 1; + i = ( arguments.length > 1 ) ? fromIndex : this.length - 1; if ( i >= this.length ) { i = this.length - 1; @@ -677,16 +723,19 @@ Float16Array.prototype.lastIndexOf = function( searchElement, fromIndex ) { } } return -1; -}; +}); /** * Apply a function to each element and return a new Float16Array. * -* @param {Function} callback - Mapping function -* @param {*} [thisArg] - Context for callback -* @returns {Float16Array} Mapped array +* @name map +* @memberof Float16Array.prototype +* @type {Function} +* @param {Function} callback - mapping function +* @param {*} [thisArg] - context for callback +* @returns {Float16Array} mapped array */ -Float16Array.prototype.map = function( callback, thisArg ) { +setReadOnly( Float16Array.prototype, 'map', function mapfunction( callback, thisArg ) { var result = new Float16Array( this.length ); var i; var val; @@ -698,14 +747,17 @@ Float16Array.prototype.map = function( callback, thisArg ) { result.set( i, val ); } return result; -}; +}); /** * Create a new Float16Array from a variable number of arguments. * -* @returns {Float16Array} New instance +* @name of +* @memberof Float16Array +* @type {Function} +* @returns {Float16Array} new instance */ -Float16Array.of = function() { +setReadOnly( Float16Array, 'of', function of() { var len = arguments.length; var result = new Float16Array( len ); var i; @@ -714,16 +766,19 @@ Float16Array.of = function() { result.set( i, arguments[ i ] ); } return result; -}; +}); /** * Reduce the array to a single value (left-to-right). * -* @param {Function} callback - Reducer function -* @param {*} [initialValue] - Initial value -* @returns {*} Accumulated result +* @name reduce +* @memberof Float16Array.prototype +* @type {Function} +* @param {Function} callback - reducer function +* @param {*} [initialValue] - initial value +* @returns {*} accumulated result */ -Float16Array.prototype.reduce = function( callback, initialValue ) { +setReadOnly( Float16Array.prototype, 'reduce', function reduce( callback, initialValue ) { var i; var acc; var len; @@ -746,16 +801,19 @@ Float16Array.prototype.reduce = function( callback, initialValue ) { acc = callback( acc, this.get( i ), i, this ); } return acc; -}; +}); /** * Reduce the array to a single value (right-to-left). * -* @param {Function} callback - Reducer function -* @param {*} [initialValue] - Initial value -* @returns {*} Accumulated result +* @name reduceRight +* @memberof Float16Array.prototype +* @type {Function} +* @param {Function} callback - reducer function +* @param {*} [initialValue] - initial value +* @returns {*} accumulated result */ -Float16Array.prototype.reduceRight = function( callback, initialValue ) { +setReadOnly( Float16Array.prototype, 'reduceRight', function reduceRight( callback, initialValue ) { var i; var acc; var len; @@ -778,14 +836,17 @@ Float16Array.prototype.reduceRight = function( callback, initialValue ) { acc = callback( acc, this.get( i ), i, this ); } return acc; -}; +}); /** * Reverse the array in place. * -* @returns {Float16Array} Modified array +* @name reverse +* @memberof Float16Array.prototype +* @type {Function} +* @returns {Float16Array} modified array */ -Float16Array.prototype.reverse = function() { +setReadOnly( Float16Array.prototype, 'reverse', function reverse() { var i; var j; var tmp; @@ -797,17 +858,20 @@ Float16Array.prototype.reverse = function() { this._buffer[ j ] = tmp; } return this; -}; +}); /** * Set a value at a given index, or copy values from an array-like source. * -* @param {(NonNegativeInteger|ArrayLike)} indexOrArray - Index or source array -* @param {*} [value] - Value to set or offset if copying -* @throws {RangeError} Index or offset out of bounds -* @throws {TypeError} Invalid arguments +* @name set +* @memberof Float16Array.prototype +* @type {Function} +* @param {(NonNegativeInteger|ArrayLike)} indexOrArray - index or source array +* @param {*} [value] - value to set or offset if copying +* @throws {RangeError} index or offset out of bounds +* @throws {TypeError} invalid arguments */ -Float16Array.prototype.set = function( indexOrArray, value ) { +setReadOnly( Float16Array.prototype, 'set', function set( indexOrArray, value ) { var i; var len; var offset; @@ -821,7 +885,7 @@ Float16Array.prototype.set = function( indexOrArray, value ) { return; } - offset = ( value !== void 0 ) ? Number( value ) : 0; + offset = ( arguments.length > 1 ) ? Number( value ) : 0; if ( !isNonNegativeInteger( offset ) || offset > this.length ) { throw new RangeError( format( 'invalid argument. Offset must be a nonnegative integer less than or equal to the array length. Value: `%s`.', offset ) ); } @@ -829,24 +893,27 @@ Float16Array.prototype.set = function( indexOrArray, value ) { if ( isArray( indexOrArray ) || isCollection( indexOrArray ) ) { len = min( indexOrArray.length, this.length - offset ); for ( i = 0; i < len; i++ ) { - srcVal = ( typeof indexOrArray.get === 'function' ) ? indexOrArray.get( i ) : indexOrArray[ i ]; + srcVal = isFunction( indexOrArray.get ) ? indexOrArray.get( i ) : indexOrArray[ i ]; this._buffer[ offset + i ] = Float16Array.toFloat16( srcVal ); } return; } throw new TypeError( format( 'invalid argument. Must provide index and value or source and offset. Value: `%s`.', indexOrArray ) ); -}; +}); /** * Returns a shallow copy of a portion of the array. * -* @param {integer} [start=0] - Start index -* @param {integer} [end=array.length] - End index (exclusive) -* @throws {TypeError} If start or end is not an integer -* @returns {Float16Array} New subarray +* @name slice +* @memberof Float16Array.prototype +* @type {Function} +* @param {integer} [start=0] - start index +* @param {integer} [end=array.length] - end index (exclusive) +* @throws {TypeError} if start or end is not an integer +* @returns {Float16Array} new subarray */ -Float16Array.prototype.slice = function( start, end ) { +setReadOnly( Float16Array.prototype, 'slice', function slice( start, end ) { var len = this.length; var i; var count; @@ -854,15 +921,15 @@ Float16Array.prototype.slice = function( start, end ) { var relativeEnd; var result; - if ( start !== void 0 && !isInteger( start ) ) { + if ( arguments.length > 0 && !isInteger( start ) ) { throw new TypeError( format( 'invalid argument. Start index must be an integer. Value: `%s`.', start ) ); } - if ( end !== void 0 && !isInteger( end ) ) { + if ( arguments.length > 1 && !isInteger( end ) ) { throw new TypeError( format( 'invalid argument. End index must be an integer. Value: `%s`.', end ) ); } - relativeStart = ( start === void 0 ) ? 0 : ( start < 0 ? max( len + start, 0 ) : min( start, len ) ); - relativeEnd = ( end === void 0 ) ? len : ( end < 0 ? max( len + end, 0 ) : min( end, len ) ); + relativeStart = ( arguments.length < 1 ) ? 0 : ( start < 0 ? max( len + start, 0 ) : min( start, len ) ); + relativeEnd = ( arguments.length < 2 ) ? len : ( end < 0 ? max( len + end, 0 ) : min( end, len ) ); count = max( relativeEnd - relativeStart, 0 ); result = new Float16Array( count ); @@ -871,16 +938,19 @@ Float16Array.prototype.slice = function( start, end ) { result.set( i, this.get( relativeStart + i ) ); } return result; -}; +}); /** * Test whether at least one element passes the test. * -* @param {Function} callback - Predicate function -* @param {*} [thisArg] - Context -* @returns {boolean} Whether at least one passes +* @name some +* @memberof Float16Array.prototype +* @type {Function} +* @param {Function} callback - predicate function +* @param {*} [thisArg] - context +* @returns {boolean} whether at least one passes */ -Float16Array.prototype.some = function( callback, thisArg ) { +setReadOnly( Float16Array.prototype, 'some', function some( callback, thisArg ) { var i; var value; @@ -894,16 +964,28 @@ Float16Array.prototype.some = function( callback, thisArg ) { } } return false; -}; +}); /** * Sort the array in place. * -* @param {Function} [compareFn] - Optional comparison function -* @throws {TypeError} If compareFn is not a function -* @returns {Float16Array} Sorted array +* @name sort +* @memberof Float16Array.prototype +* @type {Function} +* @param {Function} [compareFn] - optional comparison function +* @throws {TypeError} if compareFn is not a function +* @returns {Float16Array} sorted array */ -function getComparator( compareFn ) { +setReadOnly( Float16Array.prototype, 'sort', function sort( compareFn ) { + var values = []; + var i; + var j; + + if ( arguments.length > 0 && !isFunction( compareFn ) ) { + throw new TypeError( format( 'invalid argument. Comparison function must be a function. Value: `%s`.', compareFn ) ); + } + + function getComparator( compareFn ) { return function compareFloat16( a, b ) { if ( compareFn ) { return compareFn( a, b ); @@ -918,15 +1000,6 @@ function getComparator( compareFn ) { }; } -Float16Array.prototype.sort = function( compareFn ) { - var values = []; - var i; - var j; - - if ( compareFn !== void 0 && !isFunction( compareFn ) ) { - throw new TypeError( format( 'invalid argument. Comparison function must be a function. Value: `%s`.', compareFn ) ); - } - for ( i = 0; i < this.length; i++ ) { values.push( this.get( i ) ); } @@ -937,36 +1010,41 @@ Float16Array.prototype.sort = function( compareFn ) { this.set( j, values[ j ] ); } return this; -}; +}); /** * Returns a new Float16Array view on the same buffer, for the specified range. * -* @param {integer} [begin=0] - Start index -* @param {integer} [end=this.length] - End index -* @returns {Float16Array} New Float16Array +* @name subarray +* @memberof Float16Array.prototype +* @type {Function} +* @param {integer} [begin=0] - start index +* @param {integer} [end=this.length] - end index +* @returns {Float16Array} new Float16Array */ -Float16Array.prototype.subarray = function( begin, end ) { +setReadOnly( Float16Array.prototype, 'subarray', function subarray( begin, end ) { var len = this.length; - var i; - var start = ( typeof begin === 'undefined' ) ? 0 : ( begin < 0 ? max( len + begin, 0 ) : min( begin, len ) ); - var finish = ( typeof end === 'undefined' ) ? len : ( end < 0 ? max( len + end, 0 ) : min( end, len ) ); + var start = ( arguments.length < 1 ) ? 0 : ( begin < 0 ? max( len + begin, 0 ) : min( begin, len ) ); + var finish = ( arguments.length < 2 ) ? len : ( end < 0 ? max( len + end, 0 ) : min( end, len ) ); var count = max( finish - start, 0 ); var result = new Float16Array( count ); - for ( i = 0; i < count; i++ ) { + for ( var i = 0; i < count; i++ ) { result.set( i, this.get( start + i ) ); } return result; -}; +}); /** * Converts a float32 number to a float16 (binary) representation. * +* @name toFloat16 +* @memberof Float16Array +* @type {Function} * @param {*} val - Float32 value (coerced if not number) * @returns {integer} Float16 bit pattern */ -Float16Array.toFloat16 = function( val ) { +setReadOnly( Float16Array, 'toFloat16', function toFloat16( val ) { var num = Number( val ); var floatView = new Float32Array( 1 ); @@ -997,46 +1075,45 @@ Float16Array.toFloat16 = function( val ) { exp = exponent + 15; frac = fraction >> 13; return sign | ( exp << 10 ) | frac; -}; +}); /** * Return an iterator over values. * +* @name values +* @memberof Float16Array.prototype +* @type {Function} * @returns {Object} Iterator */ -Float16Array.prototype.values = function() { +setReadOnly( Float16Array.prototype, 'values', function values() { var self = this; var i = 0; - function nextValues() { - if ( i < self.length ) { - var v = self.get( i ); - i += 1; + var iterator = { + next: function nextValues() { + if ( i < self.length ) { + var v = self.get( i ); + i += 1; + return { + value: v, + done: false + }; + } return { - value: v, - done: false + value: void 0, + done: true }; } - return { - value: void 0, - done: true - }; - } - - function getValuesIterator() { - return iterator; - } - - var iterator = { - next: nextValues }; - if ( typeof Symbol === 'function' && Symbol.iterator ) { - iterator[ Symbol.iterator ] = getValuesIterator; + if ( iteratorSymbol ) { + iterator[ iteratorSymbol ] = function getValuesIterator() { + return iterator; + }; } return iterator; -}; +}); // EXPORTS // From 004f5b8b5e135d29c6a9a06040e842cc9cf9dd03 Mon Sep 17 00:00:00 2001 From: gururaj1512 Date: Sun, 19 Oct 2025 03:13:18 +0530 Subject: [PATCH 07/45] fix: benchmarks --- .../array/float16/benchmark/benchmark.fill.js | 18 ++++++++++-------- .../float16/benchmark/benchmark.fill.length.js | 18 +++++++++++------- .../float16/benchmark/benchmark.for_each.js | 2 +- .../float16/benchmark/benchmark.properties.js | 2 +- .../float16/benchmark/benchmark.reduce.js | 2 +- .../benchmark/benchmark.reduce.length.js | 2 +- .../benchmark/benchmark.reduce_right.js | 2 +- .../benchmark/benchmark.reduce_right.length.js | 2 +- .../float16/benchmark/benchmark.set.length.js | 2 +- .../array/float16/benchmark/benchmark.some.js | 2 +- .../float16/benchmark/benchmark.some.length.js | 2 +- .../float16/benchmark/benchmark.subarray.js | 2 +- .../float16/benchmark/benchmark.values.js | 2 +- 13 files changed, 32 insertions(+), 26 deletions(-) diff --git a/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.fill.js b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.fill.js index 3cdf17366c8c..ca30dda4d455 100644 --- a/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.fill.js +++ b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.fill.js @@ -21,6 +21,7 @@ // MODULES // var bench = require( '@stdlib/bench' ); +var isFloat16Array = require( '@stdlib/assert/is-float16array' ); var pkg = require( './../package.json' ).name; var Float16Array = require( './../lib' ); @@ -28,23 +29,24 @@ var Float16Array = require( './../lib' ); // MAIN // bench( pkg+':fill', function benchmark( b ) { + var values; var arr; - var v; + var out; var i; - arr = new Float16Array( 2 ); + values = [ 1.0, 2.0, 3.0 ]; + arr = new Float16Array( 5 ); b.tic(); for ( i = 0; i < b.iterations; i++ ) { - v = i % 128; - arr = arr.fill( v ); - if ( arr[ 0 ] !== v ) { - b.fail( 'unexpected result' ); + out = arr.fill( values[ i%values.length ] ); + if ( typeof out !== 'object' ) { + b.fail( 'should return an object' ); } } b.toc(); - if ( arr[ 0 ] !== arr[ 0 ] ) { - b.fail( 'should not be NaN' ); + if ( !isFloat16Array( out ) ) { + b.fail( 'should return a Float16Array' ); } b.pass( 'benchmark finished' ); b.end(); diff --git a/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.fill.length.js b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.fill.length.js index 4c4338095ba0..0023ad38cf93 100644 --- a/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.fill.length.js +++ b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.fill.length.js @@ -22,6 +22,7 @@ var bench = require( '@stdlib/bench' ); var pow = require( '@stdlib/math/base/special/pow' ); +var isFloat16Array = require( '@stdlib/assert/is-float16array' ); var pkg = require( './../package.json' ).name; var Float16Array = require( './../lib' ); @@ -46,20 +47,23 @@ function createBenchmark( len ) { * @param {Benchmark} b - benchmark instance */ function benchmark( b ) { - var v; + var values; + var out; var i; + values = [ 1.0, 2.0, 3.0 ]; + arr = new Float16Array( 5 ); + b.tic(); for ( i = 0; i < b.iterations; i++ ) { - v = i % 128; - arr = arr.fill( v ); - if ( arr[ 0 ] !== v ) { - b.fail( 'unexpected result' ); + out = arr.fill( values[ i%values.length ] ); + if ( typeof out !== 'object' ) { + b.fail( 'should return an object' ); } } b.toc(); - if ( arr[ 0 ] !== arr[ 0 ] ) { - b.fail( 'should not be NaN' ); + if ( !isFloat16Array( out ) ) { + b.fail( 'should return a Float16Array' ); } b.pass( 'benchmark finished' ); b.end(); diff --git a/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.for_each.js b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.for_each.js index 95b95fddf272..cae0ff1f2aee 100644 --- a/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.for_each.js +++ b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.for_each.js @@ -22,7 +22,7 @@ var bench = require( '@stdlib/bench' ); var pkg = require( './../package.json' ).name; -var Float16Array = require( './../lib/polyfill.js' ); +var Float16Array = require( './../lib' ); // MAIN // diff --git a/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.properties.js b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.properties.js index 333b6d52a60e..5238739ac924 100644 --- a/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.properties.js +++ b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.properties.js @@ -24,7 +24,7 @@ var bench = require( '@stdlib/bench' ); var isArrayBuffer = require( '@stdlib/assert/is-arraybuffer' ); var isNonNegativeInteger = require( '@stdlib/assert/is-nonnegative-integer' ).isPrimitive; var pkg = require( './../package.json' ).name; -var Float16Array = require( './../lib/polyfill.js' ); +var Float16Array = require( './../lib' ); // MAIN // diff --git a/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.reduce.js b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.reduce.js index c87b8e8ece8b..13cdb5355013 100644 --- a/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.reduce.js +++ b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.reduce.js @@ -22,7 +22,7 @@ var bench = require( '@stdlib/bench' ); var pkg = require( './../package.json' ).name; -var Float16Array = require( './../lib/polyfill.js' ); +var Float16Array = require( './../lib' ); // MAIN // diff --git a/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.reduce.length.js b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.reduce.length.js index d1c2c8f3e909..d83014e82145 100644 --- a/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.reduce.length.js +++ b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.reduce.length.js @@ -23,7 +23,7 @@ var bench = require( '@stdlib/bench' ); var pow = require( '@stdlib/math/base/special/pow' ); var pkg = require( './../package.json' ).name; -var Float16Array = require( './../lib/polyfill.js' ); +var Float16Array = require( './../lib' ); // FUNCTIONS // diff --git a/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.reduce_right.js b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.reduce_right.js index 10e28ffc5ae0..d76e10422306 100644 --- a/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.reduce_right.js +++ b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.reduce_right.js @@ -22,7 +22,7 @@ var bench = require( '@stdlib/bench' ); var pkg = require( './../package.json' ).name; -var Float16Array = require( './../lib/polyfill.js' ); +var Float16Array = require( './../lib' ); // MAIN // diff --git a/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.reduce_right.length.js b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.reduce_right.length.js index 50dc92c4d5b4..1b221a7fcc30 100644 --- a/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.reduce_right.length.js +++ b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.reduce_right.length.js @@ -23,7 +23,7 @@ var bench = require( '@stdlib/bench' ); var pow = require( '@stdlib/math/base/special/pow' ); var pkg = require( './../package.json' ).name; -var Float16Array = require( './../lib/polyfill.js' ); +var Float16Array = require( './../lib' ); // FUNCTIONS // diff --git a/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.set.length.js b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.set.length.js index 9d9de7a9807d..1b8144895b35 100644 --- a/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.set.length.js +++ b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.set.length.js @@ -24,7 +24,7 @@ var bench = require( '@stdlib/bench' ); var pow = require( '@stdlib/math/base/special/pow' ); var randi = require( '@stdlib/random/base/randi' ); var pkg = require( './../package.json' ).name; -var Float16Array = require( './../lib/polyfill.js' ); +var Float16Array = require( './../lib' ); // FUNCTIONS // diff --git a/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.some.js b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.some.js index df8b7323d49e..3c20942c9e61 100644 --- a/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.some.js +++ b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.some.js @@ -23,7 +23,7 @@ var bench = require( '@stdlib/bench' ); var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; var pkg = require( './../package.json' ).name; -var Float16Array = require( './../lib/polyfill.js' ); +var Float16Array = require( './../lib' ); // MAIN // diff --git a/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.some.length.js b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.some.length.js index d88927ada099..33d5a282e248 100644 --- a/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.some.length.js +++ b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.some.length.js @@ -24,7 +24,7 @@ var bench = require( '@stdlib/bench' ); var pow = require( '@stdlib/math/base/special/pow' ); var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; var pkg = require( './../package.json' ).name; -var Float16Array = require( './../lib/polyfill.js' ); +var Float16Array = require( './../lib' ); // FUNCTIONS // diff --git a/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.subarray.js b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.subarray.js index c53df7bebba3..11c88f41c94a 100644 --- a/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.subarray.js +++ b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.subarray.js @@ -23,7 +23,7 @@ var bench = require( '@stdlib/bench' ); var isFloat16Array = require( '@stdlib/assert/is-float16array' ); var pkg = require( './../package.json' ).name; -var Float16Array = require( './../lib/polyfill.js' ); +var Float16Array = require( './../lib' ); // MAIN // diff --git a/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.values.js b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.values.js index 52caf4535cbb..739a8a31396c 100644 --- a/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.values.js +++ b/lib/node_modules/@stdlib/array/float16/benchmark/benchmark.values.js @@ -22,7 +22,7 @@ var bench = require( '@stdlib/bench' ); var pkg = require( './../package.json' ).name; -var Float16Array = require( './../lib/polyfill.js' ); +var Float16Array = require( './../lib' ); // MAIN // From 641fa200739e5c0deb4cdb43badd916f6c2e3bc2 Mon Sep 17 00:00:00 2001 From: gururaj1512 Date: Sun, 19 Oct 2025 03:14:36 +0530 Subject: [PATCH 08/45] refactor: implementation --- .../@stdlib/array/float16/README.md | 14 +- .../array/float16/lib/from_iterator.js | 48 + .../array/float16/lib/from_iterator_map.js | 53 + .../@stdlib/array/float16/lib/index.js | 76 +- .../@stdlib/array/float16/lib/main.js | 2469 ++++++++++++++++- .../@stdlib/array/float16/lib/polyfill.js | 1121 -------- 6 files changed, 2630 insertions(+), 1151 deletions(-) create mode 100644 lib/node_modules/@stdlib/array/float16/lib/from_iterator.js create mode 100644 lib/node_modules/@stdlib/array/float16/lib/from_iterator_map.js delete mode 100644 lib/node_modules/@stdlib/array/float16/lib/polyfill.js diff --git a/lib/node_modules/@stdlib/array/float16/README.md b/lib/node_modules/@stdlib/array/float16/README.md index 7f1bb63c6dae..a6131501442c 100644 --- a/lib/node_modules/@stdlib/array/float16/README.md +++ b/lib/node_modules/@stdlib/array/float16/README.md @@ -20,7 +20,7 @@ limitations under the License. # Float16Array -> [Typed array][mdn-typed-array] constructor which returns a [typed array][mdn-typed-array] representing an array of half-precision (16-bit) floating-point numbers in the platform byte order. +> [Typed array][mdn-typed-array] constructor which returns a [typed array][mdn-typed-array] representing an array of half-precision floating-point numbers in the platform byte order. @@ -38,7 +38,7 @@ limitations under the License. ```javascript var Float16Array = require( '@stdlib/array/float16' ); - +``` #### Float16Array() @@ -96,7 +96,7 @@ Returns a [typed array][mdn-typed-array] view of an [`ArrayBuffer`][@stdlib/arra ```javascript var ArrayBuffer = require( '@stdlib/array/buffer' ); -var buf = new ArrayBuffer( 16 ); +var buf = new ArrayBuffer( 8 ); var arr = new Float16Array( buf, 0, 4 ); // returns [ 0.0, 0.0, 0.0, 0.0 ] ``` @@ -211,8 +211,6 @@ var len = arr.length; Creates a new typed array from an array-like `object` or an iterable. - - ```javascript var arr = Float16Array.from( [ 1.0, 2.0 ] ); // returns [ 1.0, 2.0 ] @@ -220,8 +218,6 @@ var arr = Float16Array.from( [ 1.0, 2.0 ] ); To invoke a function for each `src` value, provide a callback function. - - ```javascript function mapFcn( v ) { return v * 2.0; @@ -238,8 +234,6 @@ A callback function is provided two arguments: To set the callback execution context, provide a `thisArg`. - - ```javascript function mapFcn( v ) { this.count += 1; @@ -263,8 +257,6 @@ var n = ctx.count; Creates a new typed array from a variable number of arguments. - - ```javascript var arr = Float16Array.of( 1.0, 2.0 ); // returns [ 1.0, 2.0 ] diff --git a/lib/node_modules/@stdlib/array/float16/lib/from_iterator.js b/lib/node_modules/@stdlib/array/float16/lib/from_iterator.js new file mode 100644 index 000000000000..b6190d00321b --- /dev/null +++ b/lib/node_modules/@stdlib/array/float16/lib/from_iterator.js @@ -0,0 +1,48 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MAIN // + +/** +* Returns an array of iterated values. +* +* @private +* @param {Object} it - iterator +* @returns {Array} output array +*/ +function fromIterator( it ) { + var out; + var v; + + out = []; + while ( true ) { + v = it.next(); + if ( v.done ) { + break; + } + out.push( v.value ); + } + return out; +} + + +// EXPORTS // + +module.exports = fromIterator; diff --git a/lib/node_modules/@stdlib/array/float16/lib/from_iterator_map.js b/lib/node_modules/@stdlib/array/float16/lib/from_iterator_map.js new file mode 100644 index 000000000000..346ff3c47a4f --- /dev/null +++ b/lib/node_modules/@stdlib/array/float16/lib/from_iterator_map.js @@ -0,0 +1,53 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MAIN // + +/** +* Returns an array of iterated values. +* +* @private +* @param {Object} it - iterator +* @param {Function} clbk - callback to invoke for each iterated value +* @param {*} thisArg - invocation context +* @returns {Array} output array +*/ +function fromIteratorMap( it, clbk, thisArg ) { + var out; + var v; + var i; + + out = []; + i = -1; + while ( true ) { + v = it.next(); + if ( v.done ) { + break; + } + i += 1; + out.push( clbk.call( thisArg, v.value, i ) ); + } + return out; +} + + +// EXPORTS // + +module.exports = fromIteratorMap; diff --git a/lib/node_modules/@stdlib/array/float16/lib/index.js b/lib/node_modules/@stdlib/array/float16/lib/index.js index e7c13c3a4f93..175d68ace350 100644 --- a/lib/node_modules/@stdlib/array/float16/lib/index.js +++ b/lib/node_modules/@stdlib/array/float16/lib/index.js @@ -19,34 +19,76 @@ 'use strict'; /** -* Typed array constructor which returns a typed array representing an array of half-precision floating-point numbers in the platform byte order. +* 16-bit floating-point number array constructor. * * @module @stdlib/array/float16 * * @example -* var ctor = require( '@stdlib/array/float16' ); +* var Float16Array = require( '@stdlib/array/float16' ); * -* var arr = new ctor( 10 ); +* var arr = new Float16Array(); * // returns +* +* var len = arr.length; +* // returns 0 +* +* @example +* var Float16Array = require( '@stdlib/array/float16' ); +* +* var arr = new Float16Array( 2 ); +* // returns +* +* var len = arr.length; +* // returns 2 +* +* @example +* var Float16Array = require( '@stdlib/array/float16' ); +* +* var arr = new Float16Array( [ true, false ] ); +* // returns +* +* var len = arr.length; +* // returns 2 +* +* @example +* var ArrayBuffer = require( '@stdlib/array/buffer' ); +* var Float16Array = require( '@stdlib/array/float16' ); +* +* var buf = new ArrayBuffer( 16 ); +* var arr = new Float16Array( buf ); +* // returns +* +* var len = arr.length; +* // returns 8 +* +* @example +* var ArrayBuffer = require( '@stdlib/array/buffer' ); +* var Float16Array = require( '@stdlib/array/float16' ); +* +* var buf = new ArrayBuffer( 16 ); +* var arr = new Float16Array( buf, 8 ); +* // returns +* +* var len = arr.length; +* // returns 4 +* +* @example +* var ArrayBuffer = require( '@stdlib/array/buffer' ); +* var Float16Array = require( '@stdlib/array/float16' ); +* +* var buf = new ArrayBuffer( 32 ); +* var arr = new Float16Array( buf, 8, 2 ); +* // returns +* +* var len = arr.length; +* // returns 2 */ // MODULES // -var hasFloat16ArraySupport = require( '@stdlib/assert/has-float16array-support' ); -var builtin = require( './main.js' ); -var polyfill = require( './polyfill.js' ); - - -// MAIN // - -var ctor; -if ( hasFloat16ArraySupport() ) { - ctor = builtin; -} else { - ctor = polyfill; -} +var main = require( './main.js' ); // EXPORTS // -module.exports = ctor; +module.exports = main; diff --git a/lib/node_modules/@stdlib/array/float16/lib/main.js b/lib/node_modules/@stdlib/array/float16/lib/main.js index 9c7250c3f661..50f9ecab5697 100644 --- a/lib/node_modules/@stdlib/array/float16/lib/main.js +++ b/lib/node_modules/@stdlib/array/float16/lib/main.js @@ -1,3 +1,7 @@ +/* eslint-disable stdlib/jsdoc-no-undefined-references */ +/* eslint-disable stdlib/jsdoc-no-shortcut-reference-link */ +/* eslint-disable stdlib/jsdoc-typedef-typos, max-lines */ + /** * @license Apache-2.0 * @@ -16,13 +20,2474 @@ * limitations under the License. */ +/* eslint-disable no-restricted-syntax, no-invalid-this */ + 'use strict'; +// MODULES // + +var setReadOnlyAccessor = require( '@stdlib/utils/define-nonenumerable-read-only-accessor' ); +var isNonNegativeInteger = require( '@stdlib/assert/is-nonnegative-integer' ).isPrimitive; +var isObject = require( '@stdlib/assert/is-object' ); +var isNumber = require( '@stdlib/assert/is-number' ).isPrimitive; +var isStringArray = require( '@stdlib/assert/is-string-array' ).primitives; +var isString = require( '@stdlib/assert/is-string' ).isPrimitive; +var isArrayLikeObject = require( '@stdlib/assert/is-array-like-object' ); +var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); +var isInteger = require( '@stdlib/assert/is-integer' ).isPrimitive; +var hasIteratorSymbolSupport = require( '@stdlib/assert/has-iterator-symbol-support' ); +var ITERATOR_SYMBOL = require( '@stdlib/symbol/iterator' ); +var isCollection = require( '@stdlib/assert/is-collection' ); +var isArrayBuffer = require( '@stdlib/assert/is-arraybuffer' ); +var isFunction = require( '@stdlib/assert/is-function' ); +var floor = require( '@stdlib/math/base/special/floor' ); +var format = require( '@stdlib/string/format' ); +var Uint16Array = require( '@stdlib/array/uint16' ); +var getter = require( '@stdlib/array/base/getter' ); +var accessorGetter = require( '@stdlib/array/base/accessor-getter' ); +var fromIterator = require( './from_iterator.js' ); +var fromIteratorMap = require( './from_iterator_map.js' ); + + +// VARIABLES // + +var BYTES_PER_ELEMENT = Uint16Array.BYTES_PER_ELEMENT; +var HAS_ITERATOR_SYMBOL = hasIteratorSymbolSupport(); + + +// FUNCTIONS // + +/** +* Returns a boolean indicating if a value is a `Float16Array`. +* +* @private +* @param {*} value - value to test +* @returns {boolean} boolean indicating if a value is a `Float16Array` +*/ +function isFloat16Array( value ) { + return ( + typeof value === 'object' && + value !== null && + value.constructor.name === 'Float16Array' && + value.BYTES_PER_ELEMENT === BYTES_PER_ELEMENT + ); +} + +/** +* Returns a boolean indicating if a value is a floating-point typed array constructor. +* +* @private +* @param {*} value - value to test +* @returns {boolean} boolean indicating if a value is a floating-point typed array constructor +*/ +function isFloatingPointArrayConstructor( value ) { + return ( value === Float16Array ); +} + + // MAIN // -var ctor = ( typeof Float16Array === 'function' ) ? Float16Array : void 0; // eslint-disable-line stdlib/require-globals +/** +* 16-bit floating-point number array constructor. +* +* @constructor +* @param {(NonNegativeInteger|Collection|ArrayBuffer|Iterable)} [arg] - length, typed array, array-like object, buffer, or an iterable +* @param {NonNegativeInteger} [byteOffset=0] - byte offset +* @param {NonNegativeInteger} [length] - view length +* @throws {RangeError} ArrayBuffer byte length must be a multiple of `2` +* @throws {TypeError} if provided only a single argument, must provide a valid argument +* @throws {TypeError} byte offset must be a nonnegative integer +* @throws {RangeError} byte offset must be a multiple of `2` +* @throws {TypeError} view length must be a positive multiple of `2` +* @throws {RangeError} must provide sufficient memory to accommodate byte offset and view length requirements +* @returns {Float16Array} floating-point number array +* +* @example +* var arr = new Float16Array(); +* // returns +* +* var len = arr.length; +* // returns 0 +* +* @example +* var arr = new Float16Array( 5 ); +* // returns +* +* var len = arr.length; +* // returns 5 +* +* @example +* var arr = new Float16Array( [ 1.0, 2.0, 3.0, 4.0 ] ); +* // returns +* +* var len = arr.length; +* // returns 4 +* +* @example +* var ArrayBuffer = require( '@stdlib/array/buffer' ); +* +* var buf = new ArrayBuffer( 16 ); +* var arr = new Float16Array( buf ); +* // returns +* +* var len = arr.length; +* // returns 8 +* +* @example +* var ArrayBuffer = require( '@stdlib/array/buffer' ); +* +* var buf = new ArrayBuffer( 16 ); +* var arr = new Float16Array( buf, 8 ); +* // returns +* +* var len = arr.length; +* // returns 4 +* +* @example +* var ArrayBuffer = require( '@stdlib/array/buffer' ); +* +* var buf = new ArrayBuffer( 32 ); +* var arr = new Float16Array( buf, 8, 2 ); +* // returns +* +* var len = arr.length; +* // returns 2 +*/ +function Float16Array() { + var byteOffset; + var nargs; + var buf; + var len; + + nargs = arguments.length; + if ( !(this instanceof Float16Array) ) { + if ( nargs === 0 ) { + return new Float16Array(); + } + if ( nargs === 1 ) { + return new Float16Array( arguments[0] ); + } + if ( nargs === 2 ) { + return new Float16Array( arguments[0], arguments[1] ); + } + return new Float16Array( arguments[0], arguments[1], arguments[2] ); + } + // Create the underlying data buffer... + if ( nargs === 0 ) { + buf = new Uint16Array( 0 ); // backward-compatibility + } else if ( nargs === 1 ) { + if ( isNonNegativeInteger( arguments[0] ) ) { + buf = new Uint16Array( arguments[0] ); + } else if ( isCollection( arguments[0] ) ) { + buf = arguments[0]; + buf = new Uint16Array( buf ); + } else if ( isArrayBuffer( arguments[0] ) ) { + buf = new Uint16Array( arguments[0] ); + } else if ( isObject( arguments[0] ) ) { + buf = arguments[ 0 ]; + if ( HAS_ITERATOR_SYMBOL === false ) { + throw new TypeError( format( 'invalid argument. Environment lacks Symbol.iterator support. Must provide a length, ArrayBuffer, typed array, or array-like object. Value: `%s`.', buf ) ); + } + if ( !isFunction( buf[ ITERATOR_SYMBOL ] ) ) { + throw new TypeError( format( 'invalid argument. Must provide a length, ArrayBuffer, typed array, array-like object, or an iterable. Value: `%s`.', buf ) ); + } + buf = buf[ ITERATOR_SYMBOL ](); + if ( !isFunction( buf.next ) ) { + throw new TypeError( format( 'invalid argument. Must provide a length, ArrayBuffer, typed array, array-like object, or an iterable. Value: `%s`.', buf ) ); + } + buf = new Uint16Array( fromIterator( buf ) ); + } else { + throw new TypeError( format( 'invalid argument. Must provide a length, ArrayBuffer, typed array, array-like object, or an iterable. Value: `%s`.', arguments[0] ) ); + } + } else { + buf = arguments[ 0 ]; + if ( !isArrayBuffer( buf ) ) { + throw new TypeError( format( 'invalid argument. First argument must be an ArrayBuffer. Value: `%s`.', buf ) ); + } + byteOffset = arguments[ 1 ]; + if ( !isNonNegativeInteger( byteOffset ) ) { + throw new TypeError( format( 'invalid argument. Byte offset must be a nonnegative integer. Value: `%s`.', byteOffset ) ); + } + if ( nargs === 2 ) { + buf = new Uint16Array( buf, byteOffset ); + } else { + len = arguments[ 2 ]; + if ( !isNonNegativeInteger( len ) ) { + throw new TypeError( format( 'invalid argument. Length must be a nonnegative integer. Value: `%s`.', len ) ); + } + if ( (len*BYTES_PER_ELEMENT) > (buf.byteLength-byteOffset) ) { + throw new RangeError( format( 'invalid arguments. ArrayBuffer has insufficient capacity. Either decrease the array length or provide a bigger buffer. Minimum capacity: `%u`.', len*BYTES_PER_ELEMENT ) ); + } + buf = new Uint16Array( buf, byteOffset, len ); + } + } + setReadOnly( this, '_buffer', buf ); + setReadOnly( this, '_length', buf.length ); + + return this; +} + +/** +* Size (in bytes) of each array element. +* +* @name BYTES_PER_ELEMENT +* @memberof Float16Array +* @readonly +* @type {PositiveInteger} +* @default 2 +* +* @example +* var nbytes = Float16Array.BYTES_PER_ELEMENT; +* // returns 2 +*/ +setReadOnly( Float16Array, 'BYTES_PER_ELEMENT', BYTES_PER_ELEMENT ); + +/** +* Constructor name. +* +* @name name +* @memberof Float16Array +* @readonly +* @type {string} +* @default 'Float16Array' +* +* @example +* var str = Float16Array.name; +* // returns 'Float16Array' +*/ +setReadOnly( Float16Array, 'name', 'Float16Array' ); + +/** +* Creates a new 16-bit floating-point number array from an array-like object or an iterable. +* +* @name from +* @memberof Float16Array +* @type {Function} +* @param {(Collection|Iterable)} src - array-like object or iterable +* @param {Function} [clbk] - callback to invoke for each source element +* @param {*} [thisArg] - context +* @throws {TypeError} `this` context must be a constructor +* @throws {TypeError} `this` must be a floating-point array +* @throws {TypeError} first argument must be an array-like object or an iterable +* @throws {TypeError} second argument must be a function +* @returns {Float16Array} floating-point number array +* +* @example +* var arr = Float16Array.from( [ 1.0, 2.0 ] ); +* // returns +* +* var len = arr.length; +* // returns 2 +* +* @example +* var arr = Float16Array.from( [ 1.0, 2.0 ] ); +* // returns +* +* var len = arr.length; +* // returns 2 +* +* @example +* function clbk( v ) { +* return v * 2.0; +* } +* +* var arr = Float16Array.from( [ 1.0, 2.0 ], clbk ); +* // returns +* +* var len = arr.length; +* // returns 2 +*/ +setReadOnly( Float16Array, 'from', function from( src ) { + var thisArg; + var nargs; + var clbk; + var out; + var buf; + var tmp; + var get; + var len; + var i; + if ( !isFunction( this ) ) { + throw new TypeError( 'invalid invocation. `this` context must be a constructor.' ); + } + if ( !isFloatingPointArrayConstructor( this ) ) { + throw new TypeError( 'invalid invocation. `this` is not a floating-point array.' ); + } + nargs = arguments.length; + if ( nargs > 1 ) { + clbk = arguments[ 1 ]; + if ( !isFunction( clbk ) ) { + throw new TypeError( format( 'invalid argument. Second argument must be a function. Value: `%s`.', clbk ) ); + } + if ( nargs > 2 ) { + thisArg = arguments[ 2 ]; + } + } + if ( isArrayLikeObject( src ) ) { + if ( clbk ) { + len = src.length; + if ( src.get && src.set ) { + get = accessorGetter( 'default' ); + } else { + get = getter( 'default' ); + } + out = new this( len ); + buf = out._buffer; // eslint-disable-line no-underscore-dangle + for ( i = 0; i < len; i++ ) { + buf[ i ] = clbk.call( thisArg, get( src, i ), i ); + } + return out; + } + return new this( src ); + } + if ( isCollection( src ) ) { + if ( clbk ) { + len = src.length; + if ( src.get && src.set ) { + get = accessorGetter( 'default' ); + } else { + get = getter( 'default' ); + } + out = new this( len ); + buf = out._buffer; // eslint-disable-line no-underscore-dangle + for ( i = 0; i < len; i++ ) { + buf[ i ] = clbk.call( thisArg, get( src, i ), i ); + } + return out; + } + return new this( src ); + } + if ( isObject( src ) && HAS_ITERATOR_SYMBOL && isFunction( src[ ITERATOR_SYMBOL ] ) ) { // eslint-disable-line max-len + buf = src[ ITERATOR_SYMBOL ](); + if ( !isFunction( buf.next ) ) { + throw new TypeError( format( 'invalid argument. First argument must be an array-like object or an iterable. Value: `%s`.', src ) ); + } + if ( clbk ) { + tmp = fromIteratorMap( buf, clbk, thisArg ); + } else { + tmp = fromIterator( buf ); + } + len = tmp.length; + out = new this( len ); + buf = out._buffer; // eslint-disable-line no-underscore-dangle + for ( i = 0; i < len; i++ ) { + buf[ i ] = tmp[ i ]; + } + return out; + } + throw new TypeError( format( 'invalid argument. First argument must be an array-like object or an iterable. Value: `%s`.', src ) ); +}); + +/** +* Creates a new 16-bit floating-point number array from a variable number of arguments. +* +* @name of +* @memberof Float16Array +* @type {Function} +* @param {...*} element - array elements +* @throws {TypeError} `this` context must be a constructor +* @throws {TypeError} `this` must be a floating-point number array +* @returns {Float16Array} 16-bit floating-point number array +* +* @example +* var arr = Float16Array.of( 1.0, 1.0, 1.0, 1.0 ); +* // returns +* +* var len = arr.length; +* // returns 4 +*/ +setReadOnly( Float16Array, 'of', function of() { + var args; + var i; + if ( !isFunction( this ) ) { + throw new TypeError( 'invalid invocation. `this` context must be a constructor.' ); + } + if ( !isFloatingPointArrayConstructor( this ) ) { + throw new TypeError( 'invalid invocation. `this` is not a floating-point number array.' ); + } + args = []; + for ( i = 0; i < arguments.length; i++ ) { + args.push( arguments[ i ] ); + } + return new this( args ); +}); + +/** +* Returns an array element with support for both nonnegative and negative integer indices. +* +* @name at +* @memberof Float16Array.prototype +* @type {Function} +* @param {integer} idx - element index +* @throws {TypeError} `this` must be a floating-point number array +* @throws {TypeError} must provide an integer +* @returns {(Complex64|void)} array element +* +* @example +* var arr = new Float16Array( 3 ); +* +* arr.set( 10.0, 0 ); +* arr.set( 20.0, 1 ); +* arr.set( 30.0, 2 ); +* +* var v = arr.at( 0 ); +* // returns 10 +* +* v = arr.at( -1 ); +* // returns 30 +* +* v = arr.at( 100 ); +* // returns undefined +*/ +setReadOnly( Float16Array.prototype, 'at', function at( idx ) { + if ( !isFloat16Array( this ) ) { + throw new TypeError( 'invalid invocation. `this` is not a floating-point number array.' ); + } + if ( !isInteger( idx ) ) { + throw new TypeError( format( 'invalid argument. Must provide an integer. Value: `%s`.', idx ) ); + } + if ( idx < 0 ) { + idx += this._length; + } + if ( idx < 0 || idx >= this._length ) { + return; + } + return this._buffer[ idx ]; +}); + +/** +* Pointer to the underlying data buffer. +* +* @name buffer +* @memberof Float16Array.prototype +* @readonly +* @type {ArrayBuffer} +* +* @example +* var arr = new Float16Array( 10 ); +* +* var buf = arr.buffer; +* // returns +*/ +setReadOnlyAccessor( Float16Array.prototype, 'buffer', function get() { + return this._buffer.buffer; +}); + +/** +* Size (in bytes) of the array. +* +* @name byteLength +* @memberof Float16Array.prototype +* @readonly +* @type {NonNegativeInteger} +* +* @example +* var arr = new Float16Array( 10 ); +* +* var byteLength = arr.byteLength; +* // returns 20 +*/ +setReadOnlyAccessor( Float16Array.prototype, 'byteLength', function get() { + return this._buffer.byteLength; +}); + +/** +* Offset (in bytes) of the array from the start of its underlying `ArrayBuffer`. +* +* @name byteOffset +* @memberof Float16Array.prototype +* @readonly +* @type {NonNegativeInteger} +* +* @example +* var arr = new Float16Array( 10 ); +* +* var byteOffset = arr.byteOffset; +* // returns 0 +*/ +setReadOnlyAccessor( Float16Array.prototype, 'byteOffset', function get() { + return this._buffer.byteOffset; +}); + +/** +* Size (in bytes) of each array element. +* +* @name BYTES_PER_ELEMENT +* @memberof Float16Array.prototype +* @readonly +* @type {PositiveInteger} +* @default 2 +* +* @example +* var arr = new Float16Array( 10 ); +* +* var nbytes = arr.BYTES_PER_ELEMENT; +* // returns 2 +*/ +setReadOnly( Float16Array.prototype, 'BYTES_PER_ELEMENT', Float16Array.BYTES_PER_ELEMENT ); + +/** +* Copies a sequence of elements within the array to the position starting at `target`. +* +* @name copyWithin +* @memberof Float16Array.prototype +* @type {Function} +* @param {integer} target - index at which to start copying elements +* @param {integer} start - source index at which to copy elements from +* @param {integer} [end] - source index at which to stop copying elements from +* @throws {TypeError} `this` must be a floating-point array +* @returns {Float16Array} modified array +* +* @example +* var arr = new Float16Array( 4 ); +* +* arr.set( 1.0, 0 ); +* arr.set( 2.0, 1 ); +* arr.set( 3.0, 2 ); +* arr.set( 4.0, 3 ); +* +* // Copy the first two elements to the last two elements: +* arr.copyWithin( 2, 0, 2 ); +* +* var v = arr.get( 2 ); +* // returns 1.0 +* +* v = arr.get( 3 ); +* // returns 2.0 +*/ +setReadOnly( Float16Array.prototype, 'copyWithin', function copyWithin( target, start ) { + if ( !isFloat16Array( this ) ) { + throw new TypeError( 'invalid invocation. `this` is not a floating-point number array.' ); + } + // FIXME: prefer a functional `copyWithin` implementation which addresses lack of universal browser support (e.g., IE11 and Safari) or ensure that typed arrays are polyfilled + if ( arguments.length === 2 ) { + this._buffer.copyWithin( target, start ); + } else { + this._buffer.copyWithin( target, start, arguments[2] ); + } + return this; +}); + +/** +* Returns an iterator for iterating over array key-value pairs. +* +* @name entries +* @memberof Float16Array.prototype +* @type {Function} +* @throws {TypeError} `this` must be a floating-point number array +* @returns {Iterator} iterator +* +* @example +* var arr = new Float16Array( 3 ); +* +* arr.set( 1.0, 0 ); +* arr.set( 2.0, 1 ); +* arr.set( 3.0, 2 ); +* +* var it = arr.entries(); +* +* var v = it.next().value; +* // returns [ 0, 1.0 ] +* +* v = it.next().value; +* // returns [ 1, 2.0 ] +* +* v = it.next().value; +* // returns [ 2, 3.0 ] +* +* var bool = it.next().done; +* // returns true +*/ +setReadOnly( Float16Array.prototype, 'entries', function entries() { + var self; + var iter; + var len; + var buf; + var FLG; + var i; + + if ( !isFloat16Array( this ) ) { + throw new TypeError( 'invalid invocation. `this` is not a floating-point number array.' ); + } + self = this; + buf = this._buffer; + len = this._length; + + // Initialize an iteration index: + i = -1; + + // Create an iterator protocol-compliant object: + iter = {}; + setReadOnly( iter, 'next', next ); + setReadOnly( iter, 'return', end ); + + if ( ITERATOR_SYMBOL ) { + setReadOnly( iter, ITERATOR_SYMBOL, factory ); + } + return iter; + + /** + * Returns an iterator protocol-compliant object containing the next iterated value. + * + * @private + * @returns {Object} iterator protocol-compliant object + */ + function next() { + i += 1; + if ( FLG || i >= len ) { + return { + 'done': true + }; + } + return { + 'value': [ i, buf[ i ] ], + 'done': false + }; + } + + /** + * Finishes an iterator. + * + * @private + * @param {*} [value] - value to return + * @returns {Object} iterator protocol-compliant object + */ + function end( value ) { + FLG = true; + if ( arguments.length ) { + return { + 'value': value, + 'done': true + }; + } + return { + 'done': true + }; + } + + /** + * Returns a new iterator. + * + * @private + * @returns {Iterator} iterator + */ + function factory() { + return self.entries(); + } +}); + +/** +* Tests whether all elements in an array pass a test implemented by a predicate function. +* +* @name every +* @memberof Float16Array.prototype +* @type {Function} +* @param {Function} predicate - predicate function +* @param {*} [thisArg] - predicate function execution context +* @throws {TypeError} `this` must be a floating-point number array +* @throws {TypeError} first argument must be a function +* @returns {boolean} boolean indicating whether all elements pass a test +* +* @example +* function predicate( v ) { +* return v === 0.0; +* } +* +* var arr = new Float16Array( 3 ); +* +* arr.set( 0.0, 0 ); +* arr.set( 0.0, 1 ); +* arr.set( 0.0, 2 ); +* +* var bool = arr.every( predicate ); +* // returns true +*/ +setReadOnly( Float16Array.prototype, 'every', function every( predicate, thisArg ) { + var buf; + var i; + + if ( !isFloat16Array( this ) ) { + throw new TypeError( 'invalid invocation. `this` is not a floating-point number array.' ); + } + if ( !isFunction( predicate ) ) { + throw new TypeError( format( 'invalid argument. First argument must be a function. Value: `%s`.', predicate ) ); + } + buf = this._buffer; + for ( i = 0; i < this._length; i++ ) { + if ( !predicate.call( thisArg, buf[ i ], i, this ) ) { + return false; + } + } + return true; +}); + +/** +* Returns a modified typed array filled with a fill value. +* +* @name fill +* @memberof Float16Array.prototype +* @type {Function} +* @param {number} value - fill value +* @param {integer} [start=0] - starting index (inclusive) +* @param {integer} [end] - ending index (exclusive) +* @throws {TypeError} `this` must be a floating-point number array +* @throws {TypeError} first argument must be a floating-point number +* @throws {TypeError} second argument must be an integer +* @throws {TypeError} third argument must be an integer +* @returns {Float16Array} modified array +* +* @example +* var arr = new Float16Array( 3 ); +* +* arr.fill( 1.0, 1 ); +* +* var v = arr.get( 0 ); +* // returns 0.0 +* +* v = arr.get( 1 ); +* // returns 1.0 +* +* v = arr.get( 2 ); +* // returns 1.0 +*/ +setReadOnly( Float16Array.prototype, 'fill', function fill( value, start, end ) { + var buf; + var len; + var i; + + if ( !isFloat16Array( this ) ) { + throw new TypeError( 'invalid invocation. `this` is not a floating-point number array.' ); + } + if ( !isNumber( value ) ) { + throw new TypeError( format( 'invalid argument. First argument must be a number. Value: `%s`.', value ) ); + } + buf = this._buffer; + len = this._length; + if ( arguments.length > 1 ) { + if ( !isInteger( start ) ) { + throw new TypeError( format( 'invalid argument. Second argument must be an integer. Value: `%s`.', start ) ); + } + if ( start < 0 ) { + start += len; + if ( start < 0 ) { + start = 0; + } + } + if ( arguments.length > 2 ) { + if ( !isInteger( end ) ) { + throw new TypeError( format( 'invalid argument. Third argument must be an integer. Value: `%s`.', end ) ); + } + if ( end < 0 ) { + end += len; + if ( end < 0 ) { + end = 0; + } + } + if ( end > len ) { + end = len; + } + } else { + end = len; + } + } else { + start = 0; + end = len; + } + for ( i = start; i < end; i++ ) { + buf[ i ] = value; + } + return this; +}); + +/** +* Returns a new array containing the elements of an array which pass a test implemented by a predicate function. +* +* @name filter +* @memberof Float16Array.prototype +* @type {Function} +* @param {Function} predicate - test function +* @param {*} [thisArg] - predicate function execution context +* @throws {TypeError} `this` must be a floating-point number array +* @throws {TypeError} first argument must be a function +* @returns {Float16Array} floating-point number array +* +* @example +* function predicate( v ) { +* return ( v === 0.0 ); +* } +* +* var arr = new Float16Array( 3 ); +* +* arr.set( 0.0, 0 ); +* arr.set( 1.0, 1 ); +* arr.set( 0.0, 2 ); +* +* var out = arr.filter( predicate ); +* // returns +* +* var len = out.length; +* // returns 2 +* +* var v = out.get( 0 ); +* // returns 0.0 +* +* v = out.get( 1 ); +* // returns 0.0 +*/ +setReadOnly( Float16Array.prototype, 'filter', function filter( predicate, thisArg ) { + var buf; + var out; + var i; + var v; + + if ( !isFloat16Array( this ) ) { + throw new TypeError( 'invalid invocation. `this` is not a floating-point number array.' ); + } + if ( !isFunction( predicate ) ) { + throw new TypeError( format( 'invalid argument. First argument must be a function. Value: `%s`.', predicate ) ); + } + buf = this._buffer; + out = []; + for ( i = 0; i < this._length; i++ ) { + v = buf[ i ]; + if ( predicate.call( thisArg, v, i, this ) ) { + out.push( v ); + } + } + return new this.constructor( out ); +}); + +/** +* Returns the first element in an array for which a predicate function returns a truthy value. +* +* @name find +* @memberof Float16Array.prototype +* @type {Function} +* @param {Function} predicate - predicate function +* @param {*} [thisArg] - predicate function execution context +* @throws {TypeError} `this` must be a floating-point number array +* @throws {TypeError} first argument must be a function +* @returns {(boolean|void)} array element or undefined +* +* @example +* function predicate( v ) { +* return v === 0.0; +* } +* +* var arr = new Float16Array( 3 ); +* +* arr.set( 0.0, 0 ); +* arr.set( 1.0, 1 ); +* arr.set( 0.0, 2 ); +* +* var v = arr.find( predicate ); +* // returns 0.0 +*/ +setReadOnly( Float16Array.prototype, 'find', function find( predicate, thisArg ) { + var buf; + var v; + var i; + + if ( !isFloat16Array( this ) ) { + throw new TypeError( 'invalid invocation. `this` is not a floating-point number array.' ); + } + if ( !isFunction( predicate ) ) { + throw new TypeError( format( 'invalid argument. First argument must be a function. Value: `%s`.', predicate ) ); + } + buf = this._buffer; + for ( i = 0; i < this._length; i++ ) { + v = buf[ i ]; + if ( predicate.call( thisArg, v, i, this ) ) { + return v; + } + } +}); + +/** +* Returns the index of the first element in an array for which a predicate function returns a truthy value. +* +* @name findIndex +* @memberof Float16Array.prototype +* @type {Function} +* @param {Function} predicate - predicate function +* @param {*} [thisArg] - predicate function execution context +* @throws {TypeError} `this` must be a floating-point number array +* @throws {TypeError} first argument must be a function +* @returns {integer} index or -1 +* +* @example +* function predicate( v ) { +* return v === 0.0; +* } +* +* var arr = new Float16Array( 3 ); +* +* arr.set( 0.0, 0 ); +* arr.set( 1.0, 1 ); +* arr.set( 2.0, 2 ); +* +* var v = arr.findIndex( predicate ); +* // returns 0 +*/ +setReadOnly( Float16Array.prototype, 'findIndex', function findIndex( predicate, thisArg ) { + var buf; + var v; + var i; + + if ( !isFloat16Array( this ) ) { + throw new TypeError( 'invalid invocation. `this` is not a floating-point number array.' ); + } + if ( !isFunction( predicate ) ) { + throw new TypeError( format( 'invalid argument. First argument must be a function. Value: `%s`.', predicate ) ); + } + buf = this._buffer; + for ( i = 0; i < this._length; i++ ) { + v = buf[ i ]; + if ( predicate.call( thisArg, v, i, this ) ) { + return i; + } + } + return -1; +}); + +/** +* Returns the last element in an array for which a predicate function returns a truthy value. +* +* @name findLast +* @memberof Float16Array.prototype +* @type {Function} +* @param {Function} predicate - predicate function +* @param {*} [thisArg] - predicate function execution context +* @throws {TypeError} `this` must be a floating-point number array +* @throws {TypeError} first argument must be a function +* @returns {(boolean|void)} array element or undefined +* +* @example +* function predicate( v ) { +* return v === 0.0; +* } +* +* var arr = new Float16Array( 3 ); +* +* arr.set( 0.0, 0 ); +* arr.set( 1.0, 1 ); +* arr.set( 0.0, 2 ); +* +* var v = arr.findLast( predicate ); +* // returns 0.0 +*/ +setReadOnly( Float16Array.prototype, 'findLast', function findLast( predicate, thisArg ) { + var buf; + var v; + var i; + + if ( !isFloat16Array( this ) ) { + throw new TypeError( 'invalid invocation. `this` is not a floating-point number array.' ); + } + if ( !isFunction( predicate ) ) { + throw new TypeError( format( 'invalid argument. First argument must be a function. Value: `%s`.', predicate ) ); + } + buf = this._buffer; + for ( i = this._length-1; i >= 0; i-- ) { + v = buf[ i ]; + if ( predicate.call( thisArg, v, i, this ) ) { + return v; + } + } +}); + +/** +* Returns the index of the last element in an array for which a predicate function returns a truthy value. +* +* @name findLastIndex +* @memberof Float16Array.prototype +* @type {Function} +* @param {Function} predicate - predicate function +* @param {*} [thisArg] - predicate function execution context +* @throws {TypeError} `this` must be a floating-point number array +* @throws {TypeError} first argument must be a function +* @returns {integer} index or -1 +* +* @example +* function predicate( v ) { +* return v === 0.0; +* } +* +* var arr = new Float16Array( 3 ); +* +* arr.set( 0.0, 0 ); +* arr.set( 1.0, 1 ); +* arr.set( 0.0, 2 ); +* +* var v = arr.findLastIndex( predicate ); +* // returns 2 +*/ +setReadOnly( Float16Array.prototype, 'findLastIndex', function findLastIndex( predicate, thisArg ) { + var buf; + var v; + var i; + + if ( !isFloat16Array( this ) ) { + throw new TypeError( 'invalid invocation. `this` is not a floating-point number array.' ); + } + if ( !isFunction( predicate ) ) { + throw new TypeError( format( 'invalid argument. First argument must be a function. Value: `%s`.', predicate ) ); + } + buf = this._buffer; + for ( i = this._length-1; i >= 0; i-- ) { + v = buf[ i ]; + if ( predicate.call( thisArg, v, i, this ) ) { + return i; + } + } + return -1; +}); + +/** +* Invokes a function once for each array element. +* +* @name forEach +* @memberof Float16Array.prototype +* @type {Function} +* @param {Function} fcn - function to invoke +* @param {*} [thisArg] - function invocation context +* @throws {TypeError} `this` must be a floating-point number array +* @throws {TypeError} first argument must be a function +* +* @example +* function log( v, i ) { +* console.log( '%s: %s', i, v.toString() ); +* } +* +* var arr = new Float16Array( 3 ); +* +* arr.set( 0.0, 0 ); +* arr.set( 1.0, 1 ); +* arr.set( 2.0, 2 ); +* +* arr.forEach( log ); +*/ +setReadOnly( Float16Array.prototype, 'forEach', function forEach( fcn, thisArg ) { + var buf; + var i; + + if ( !isFloat16Array( this ) ) { + throw new TypeError( 'invalid invocation. `this` is not a floating-point number array.' ); + } + if ( !isFunction( fcn ) ) { + throw new TypeError( format( 'invalid argument. First argument must be a function. Value: `%s`.', fcn ) ); + } + buf = this._buffer; + for ( i = 0; i < this._length; i++ ) { + fcn.call( thisArg, buf[ i ], i, this ); + } +}); + +/** +* Returns an array element. +* +* @name get +* @memberof Float16Array.prototype +* @type {Function} +* @param {NonNegativeInteger} idx - element index +* @throws {TypeError} `this` must be a floating-point number array +* @throws {TypeError} must provide a nonnegative integer +* @returns {(boolean|void)} array element +* +* @example +* var arr = new Float16Array( 10 ); +* +* var v = arr.get( 0 ); +* // returns 0.0 +* +* arr.set( [ 1.0, 0.0 ], 0 ); +* +* v = arr.get( 0 ); +* // returns 1.0 +* +* v = arr.get( 100 ); +* // returns undefined +*/ +setReadOnly( Float16Array.prototype, 'get', function get( idx ) { + if ( !isFloat16Array( this ) ) { + throw new TypeError( 'invalid invocation. `this` is not a floating-point number array.' ); + } + if ( !isNonNegativeInteger( idx ) ) { + throw new TypeError( format( 'invalid argument. Must provide a nonnegative integer. Value: `%s`.', idx ) ); + } + if ( idx >= this._length ) { + return; + } + return this._buffer[ idx ]; +}); + +/** +* Returns a boolean indicating whether an array includes a provided value. +* +* @name includes +* @memberof Float16Array.prototype +* @type {Function} +* @param {number} searchElement - search element +* @param {integer} [fromIndex=0] - starting index (inclusive) +* @throws {TypeError} `this` must be a floating-point number array +* @throws {TypeError} first argument must be a floating-point number +* @throws {TypeError} second argument must be an integer +* @returns {boolean} boolean indicating whether an array includes a value +* +* @example +* var arr = new Float16Array( 5 ); +* +* arr.set( 0.0, 0 ); +* arr.set( 1.0, 1 ); +* arr.set( 2.0, 2 ); +* arr.set( 3.0, 3 ); +* arr.set( 4.0, 4 ); +* +* var bool = arr.includes( 1.0 ); +* // returns true +* +* bool = arr.includes( 1.0, 2 ); +* // returns false +* +* bool = arr.includes( 5.0 ); +* // returns false +*/ +setReadOnly( Float16Array.prototype, 'includes', function includes( searchElement, fromIndex ) { + var buf; + var i; + + if ( !isFloat16Array( this ) ) { + throw new TypeError( 'invalid invocation. `this` is not a floating-point number array.' ); + } + if ( !isNumber( searchElement ) ) { + throw new TypeError( format( 'invalid argument. First argument must be a number. Value: `%s`.', searchElement ) ); + } + if ( arguments.length > 1 ) { + if ( !isInteger( fromIndex ) ) { + throw new TypeError( format( 'invalid argument. Second argument must be an integer. Value: `%s`.', fromIndex ) ); + } + if ( fromIndex < 0 ) { + fromIndex += this._length; + if ( fromIndex < 0 ) { + fromIndex = 0; + } + } + } else { + fromIndex = 0; + } + buf = this._buffer; + for ( i = fromIndex; i < this._length; i++ ) { + if ( searchElement === buf[ i ] ) { + return true; + } + } + return false; +}); + +/** +* Returns the first index at which a given element can be found. +* +* @name indexOf +* @memberof Float16Array.prototype +* @type {Function} +* @param {float16} searchElement - element to find +* @param {integer} [fromIndex=0] - starting index (inclusive) +* @throws {TypeError} `this` must be a floating-point number array +* @throws {TypeError} first argument must be a floating-point number +* @throws {TypeError} second argument must be an integer +* @returns {integer} index or -1 +* +* @example +* var arr = new Float16Array( 5 ); +* +* arr.set( 0.0, 0 ); +* arr.set( 1.0, 1 ); +* arr.set( 2.0, 2 ); +* arr.set( 3.0, 3 ); +* arr.set( 4.0, 4 ); +* +* var idx = arr.indexOf( 2.0 ); +* // returns 2 +* +* idx = arr.indexOf( 2.0, 3 ); +* // returns -1 +* +* idx = arr.indexOf( 3.0, 3 ); +* // returns 3 +*/ +setReadOnly( Float16Array.prototype, 'indexOf', function indexOf( searchElement, fromIndex ) { + var buf; + var i; + + if ( !isFloat16Array( this ) ) { + throw new TypeError( 'invalid invocation. `this` is not a floating-point number array.' ); + } + if ( !isNumber( searchElement ) ) { + throw new TypeError( format( 'invalid argument. First argument must be a number. Value: `%s`.', searchElement ) ); + } + if ( arguments.length > 1 ) { + if ( !isInteger( fromIndex ) ) { + throw new TypeError( format( 'invalid argument. Second argument must be an integer. Value: `%s`.', fromIndex ) ); + } + if ( fromIndex < 0 ) { + fromIndex += this._length; + if ( fromIndex < 0 ) { + fromIndex = 0; + } + } + } else { + fromIndex = 0; + } + buf = this._buffer; + for ( i = fromIndex; i < this._length; i++ ) { + if ( searchElement === buf[ i ] ) { + return i; + } + } + return -1; +}); + +/** +* Returns a new string by concatenating all array elements. +* +* @name join +* @memberof Float16Array.prototype +* @type {Function} +* @param {string} [separator=','] - element separator +* @throws {TypeError} `this` must be a floating-point number array +* @throws {TypeError} first argument must be a string +* @returns {string} string representation +* +* @example +* var arr = new Float16Array( 3 ); +* +* arr.set( 0.0, 0 ); +* arr.set( 1.0, 1 ); +* arr.set( 2.0, 2 ); +* +* var str = arr.join(); +* // returns '0,1,2' +* +* str = arr.join( '|' ); +* // returns '0|1|2' +*/ +setReadOnly( Float16Array.prototype, 'join', function join( separator ) { + var buf; + var out; + var i; + + if ( !isFloat16Array( this ) ) { + throw new TypeError( 'invalid invocation. `this` is not a floating-point number array.' ); + } + if ( arguments.length > 0 ) { + if ( !isString( separator ) ) { + throw new TypeError( format( 'invalid argument. First argument must be a string. Value: `%s`.', separator ) ); + } + } else { + separator = ','; + } + buf = this._buffer; + out = []; + for ( i = 0; i < this._length; i++ ) { + out.push( buf[i] ); + } + return out.join( separator ); +}); + +/** +* Returns an iterator for iterating over each index key in a typed array. +* +* @name keys +* @memberof Float16Array.prototype +* @type {Function} +* @throws {TypeError} `this` must be a floating-point number array +* @returns {Iterator} iterator +* +* @example +* var arr = new Float16Array( 2 ); +* +* arr.set( 1.0, 0 ); +* arr.set( 2.0, 1 ); +* +* var iter = arr.keys(); +* +* var v = iter.next().value; +* // returns 0 +* +* v = iter.next().value; +* // returns 1 +* +* var bool = iter.next().done; +* // returns true +*/ +setReadOnly( Float16Array.prototype, 'keys', function keys() { + var self; + var iter; + var len; + var FLG; + var i; + + if ( !isFloat16Array( this ) ) { + throw new TypeError( 'invalid invocation. `this` is not a floating-point number array.' ); + } + self = this; + len = this._length; + + // Initialize an iteration index: + i = -1; + + // Create an iterator protocol-compliant object: + iter = {}; + setReadOnly( iter, 'next', next ); + setReadOnly( iter, 'return', end ); + + if ( ITERATOR_SYMBOL ) { + setReadOnly( iter, ITERATOR_SYMBOL, factory ); + } + return iter; + + /** + * Returns an iterator protocol-compliant object containing the next iterated value. + * + * @private + * @returns {Object} iterator protocol-compliant object + */ + function next() { + i += 1; + if ( FLG || i >= len ) { + return { + 'done': true + }; + } + return { + 'value': i, + 'done': false + }; + } + + /** + * Finishes an iterator. + * + * @private + * @param {*} [value] - value to return + * @returns {Object} iterator protocol-compliant object + */ + function end( value ) { + FLG = true; + if ( arguments.length ) { + return { + 'value': value, + 'done': true + }; + } + return { + 'done': true + }; + } + + /** + * Returns a new iterator. + * + * @private + * @returns {Iterator} iterator + */ + function factory() { + return self.keys(); + } +}); + +/** +* Returns the last index at which a given element can be found. +* +* @name lastIndexOf +* @memberof Float16Array.prototype +* @type {Function} +* @param {number} searchElement - element to find +* @param {integer} [fromIndex] - starting index (inclusive) +* @throws {TypeError} `this` must be a boolean array +* @throws {TypeError} first argument must be a boolean value +* @throws {TypeError} second argument must be an integer +* @returns {integer} index or -1 +* +* @example +* var arr = new Float16Array( 5 ); +* +* arr.set( 0.0, 0 ); +* arr.set( 1.0, 1 ); +* arr.set( 2.0, 2 ); +* arr.set( 3.0, 3 ); +* arr.set( 2.0, 4 ); +* +* var idx = arr.lastIndexOf( 2.0 ); +* // returns 4 +* +* idx = arr.lastIndexOf( 2.0, 3 ); +* // returns 2 +* +* idx = arr.lastIndexOf( 4.0, 3 ); +* // returns -1 +* +* idx = arr.lastIndexOf( 1.0, -3 ); +* // returns 1 +*/ +setReadOnly( Float16Array.prototype, 'lastIndexOf', function lastIndexOf( searchElement, fromIndex ) { + var buf; + var i; + + if ( !isFloat16Array( this ) ) { + throw new TypeError( 'invalid invocation. `this` is not a floating-point number array.' ); + } + if ( !isNumber( searchElement ) ) { + throw new TypeError( format( 'invalid argument. First argument must be a number. Value: `%s`.', searchElement ) ); + } + if ( arguments.length > 1 ) { + if ( !isInteger( fromIndex ) ) { + throw new TypeError( format( 'invalid argument. Second argument must be an integer. Value: `%s`.', fromIndex ) ); + } + if ( fromIndex >= this._length ) { + fromIndex = this._length - 1; + } else if ( fromIndex < 0 ) { + fromIndex += this._length; + } + } else { + fromIndex = this._length - 1; + } + buf = this._buffer; + for ( i = fromIndex; i >= 0; i-- ) { + if ( searchElement === buf[ i ] ) { + return i; + } + } + return -1; +}); + +/** +* Number of array elements. +* +* @name length +* @memberof Float16Array.prototype +* @readonly +* @type {NonNegativeInteger} +* +* @example +* var arr = new Float16Array( 10 ); +* +* var len = arr.length; +* // returns 10 +*/ +setReadOnlyAccessor( Float16Array.prototype, 'length', function get() { + return this._length; +}); + +/** +* Returns a new array with each element being the result of a provided callback function. +* +* @name map +* @memberof Float16Array.prototype +* @type {Function} +* @param {Function} fcn - callback function +* @param {*} [thisArg] - callback function execution context +* @throws {TypeError} `this` must be a floating-point number array +* @throws {TypeError} first argument must be a function +* @returns {Float16Array} new floating-point number array +* +* @example +* function scale( v ) { +* return v*2.0; +* } +* +* var arr = new Float16Array( 3 ); +* +* arr.set( 0.0, 0 ); +* arr.set( 1.0, 1 ); +* arr.set( 2.0, 2 ); +* +* var out = arr.map( scale ); +* // returns +* +* var z = out.get( 0 ); +* // returns 0.0 +* +* z = out.get( 1 ); +* // returns 2.0 +* +* z = out.get( 2 ); +* // returns 4.0 +*/ +setReadOnly( Float16Array.prototype, 'map', function map( fcn, thisArg ) { + var outbuf; + var out; + var buf; + var i; + if ( !isFloat16Array( this ) ) { + throw new TypeError( 'invalid invocation. `this` is not a floating-point number array.' ); + } + if ( !isFunction( fcn ) ) { + throw new TypeError( 'invalid argument. First argument must be a function. Value: `%s`.', fcn ); + } + buf = this._buffer; + out = new this.constructor( this._length ); + outbuf = out._buffer; // eslint-disable-line no-underscore-dangle + for ( i = 0; i < this._length; i++ ) { + outbuf[ i ] = fcn.call( thisArg, buf[ i ], i, this ); + } + return out; +}); + +/** +* Applies a provided callback function to each element of the array, in order, passing in the return value from the calculation on the preceding element and returning the accumulated result upon completion. +* +* @name reduce +* @memberof Float16Array.prototype +* @type {Function} +* @param {Function} reducer - callback function +* @param {*} [initialValue] - initial value +* @throws {TypeError} `this` must be a floating-point number array +* @throws {Error} if not provided an initial value, the array must have at least one element +* @returns {*} accumulated result +* +* @example +* function add( acc, v ) { +* return acc + v; +* } +* +* var arr = new Float16Array( 3 ); +* +* arr.set( 1.0, 0 ); +* arr.set( 2.0, 1 ); +* arr.set( 3.0, 2 ); +* +* var out = arr.reduce( add, 0 ); +* // returns 6.0 +*/ +setReadOnly( Float16Array.prototype, 'reduce', function reduce( reducer, initialValue ) { + var buf; + var len; + var acc; + var i; + + if ( !isFloat16Array( this ) ) { + throw new TypeError( 'invalid invocation. `this` is not a floating-point number array.' ); + } + if ( !isFunction( reducer ) ) { + throw new TypeError( format( 'invalid argument. First argument must be a function. Value: `%s`.', reducer ) ); + } + buf = this._buffer; + len = this._length; + if ( arguments.length > 1 ) { + acc = initialValue; + i = 0; + } else { + if ( len === 0 ) { + throw new Error( 'invalid operation. If not provided an initial value, an array must contain at least one element.' ); + } + acc = buf[ 0 ]; + i = 1; + } + for ( ; i < len; i++ ) { + acc = reducer( acc, buf[ i ], i, this ); + } + return acc; +}); + +/** +* Applies a provided callback function to each element of the array, in reverse order, passing in the return value from the calculation on the preceding element and returning the accumulated result upon completion. +* +* @name reduceRight +* @memberof Float16Array.prototype +* @type {Function} +* @param {Function} reducer - callback function +* @param {*} [initialValue] - initial value +* @throws {TypeError} `this` must be a floating-point number array +* @throws {Error} if not provided an initial value, the array must have at least one element +* @returns {*} accumulated result +* +* @example +* function add( acc, v ) { +* return acc + v; +* } +* +* var arr = new Float16Array( 3 ); +* +* arr.set( 1.0, 0 ); +* arr.set( 2.0, 1 ); +* arr.set( 3.0, 2 ); +* +* var out = arr.reduceRight( add, 0 ); +* // returns 6.0 +*/ +setReadOnly( Float16Array.prototype, 'reduceRight', function reduceRight( reducer, initialValue ) { + var buf; + var len; + var acc; + var i; + + if ( !isFloat16Array( this ) ) { + throw new TypeError( 'invalid invocation. `this` is not a floating-point number array.' ); + } + if ( !isFunction( reducer ) ) { + throw new TypeError( format( 'invalid argument. First argument must be a function. Value: `%s`.', reducer ) ); + } + buf = this._buffer; + len = this._length; + if ( arguments.length > 1 ) { + acc = initialValue; + i = len - 1; + } else { + if ( len === 0 ) { + throw new Error( 'invalid operation. If not provided an initial value, an array must contain at least one element.' ); + } + acc = buf[ len-1 ]; + i = len - 2; + } + for ( ; i >= 0; i-- ) { + acc = reducer( acc, buf[ i ], i, this ); + } + return acc; +}); + +/** +* Reverses an array in-place. +* +* @name reverse +* @memberof Float16Array.prototype +* @type {Function} +* @throws {TypeError} `this` must be a floating-point number array +* @returns {Float16Array} reversed array +* +* @example +* var arr = new Float16Array( 3 ); +* +* arr.set( 0.0, 0 ); +* arr.set( 1.0, 1 ); +* arr.set( 2.0, 2 ); +* +* var out = arr.reverse(); +* // returns +* +* var v = out.get( 0 ); +* // returns 2.0 +* +* v = out.get( 1 ); +* // returns 1.0 +* +* v = out.get( 2 ); +* // returns 0.0 +*/ +setReadOnly( Float16Array.prototype, 'reverse', function reverse() { + var buf; + var tmp; + var len; + var N; + var i; + var j; + + if ( !isFloat16Array( this ) ) { + throw new TypeError( 'invalid invocation. `this` is not a floating-point number array.' ); + } + buf = this._buffer; + len = this._length; + N = floor( len / 2 ); + for ( i = 0; i < N; i++ ) { + j = len - i - 1; + tmp = buf[ i ]; + buf[ i ] = buf[ j ]; + buf[ j ] = tmp; + } + return this; +}); + +/** +* Sets an array element. +* +* ## Notes +* +* - When provided a typed array, we must check whether the source array shares the same buffer as the target array and whether the underlying memory overlaps. In particular, we are concerned with the following scenario: +* +* ```text +* buf: --------------------- +* src: --------------------- +* ``` +* +* In the above, as we copy values from `src`, we will overwrite values in the `src` view, resulting in duplicated values copied into the end of `buf`, which is not intended. Hence, to avoid overwriting source values, we must **copy** source values to a temporary array. +* +* In the other overlapping scenario, +* +* ```text +* buf: --------------------- +* src: --------------------- +* ``` +* +* by the time we begin copying into the overlapping region, we are copying from the end of `src`, a non-overlapping region, which means we don't run the risk of copying copied values, rather than the original `src` values, as intended. +* +* @name set +* @memberof Float16Array.prototype +* @type {Function} +* @param {(Collection|Float16Array|*)} value - value(s) +* @param {NonNegativeInteger} [i=0] - element index at which to start writing values +* @throws {TypeError} `this` must be a floating-point number array +* @throws {TypeError} index argument must be a nonnegative integer +* @throws {RangeError} index argument is out-of-bounds +* @throws {RangeError} target array lacks sufficient storage to accommodate source values +* @returns {void} +* +* @example +* var arr = new Float16Array( 10 ); +* +* var v = arr.get( 0 ); +* // returns 0.0 +* +* arr.set( [ 1.0, 2.0 ], 0 ); +* +* v = arr.get( 0 ); +* // returns 1.0 +*/ +setReadOnly( Float16Array.prototype, 'set', function set( value ) { + var sbuf; + var idx; + var buf; + var tmp; + var N; + var i; + var j; + if ( !isFloat16Array( this ) ) { + throw new TypeError( 'invalid invocation. `this` is not a floating-point number array.' ); + } + buf = this._buffer; + if ( arguments.length > 1 ) { + idx = arguments[ 1 ]; + if ( !isNonNegativeInteger( idx ) ) { + throw new TypeError( format( 'invalid argument. Index argument must be a nonnegative integer. Value: `%s`.', idx ) ); + } + } else { + idx = 0; + } + if ( isCollection( value ) ) { + N = value.length; + if ( idx+N > this._length ) { + throw new RangeError( 'invalid arguments. Target array lacks sufficient storage to accommodate source values.' ); + } + if ( isFloat16Array( value ) ) { + sbuf = value._buffer; // eslint-disable-line no-underscore-dangle + } else { + sbuf = value; + } + // Check for overlapping memory... + j = buf.byteOffset + (idx*BYTES_PER_ELEMENT); + if ( + sbuf.buffer === buf.buffer && + ( + sbuf.byteOffset < j && + sbuf.byteOffset+sbuf.byteLength > j + ) + ) { + // We need to copy source values... + tmp = new Uint16Array( sbuf.length ); + for ( i = 0; i < sbuf.length; i++ ) { + tmp[ i ] = sbuf[ i ]; // TODO: handle accessor arrays + } + sbuf = tmp; + } + for ( i = 0; i < N; idx++, i++ ) { + buf[ idx ] = sbuf[ i ]; + } + return; + } + if ( idx >= this._length ) { + throw new RangeError( format( 'invalid argument. Index argument is out-of-bounds. Value: `%u`.', idx ) ); + } + buf[ idx ] = value; +}); + +/** +* Copies a portion of a typed array to a new typed array. +* +* @name slice +* @memberof Float16Array.prototype +* @type {Function} +* @param {integer} [begin] - start index (inclusive) +* @param {integer} [end] - end index (exclusive) +* @throws {TypeError} `this` must be a floating-point number array +* @throws {TypeError} first argument must be integer +* @throws {TypeError} second argument must be integer +* @returns {Float16Array} floating-point number array +* +* @example +* var arr = new Float16Array( 5 ); +* +* arr.set( 0.0, 0 ); +* arr.set( 1.0, 1 ); +* arr.set( 2.0, 2 ); +* arr.set( 3.0, 3 ); +* arr.set( 4.0, 4 ); +* +* var out = arr.slice(); +* // returns +* +* var len = out.length; +* // returns 5 +* +* var bool = out.get( 0 ); +* // returns 0.0 +* +* bool = out.get( len-1 ); +* // returns 4.0 +* +* out = arr.slice( 1, -2 ); +* // returns +* +* len = out.length; +* // returns 2 +* +* bool = out.get( 0 ); +* // returns 1.0 +* +* bool = out.get( len-1 ); +* // returns 2.0 +*/ +setReadOnly( Float16Array.prototype, 'slice', function slice( begin, end ) { + var outlen; + var outbuf; + var out; + var buf; + var len; + var i; + + if ( !isFloat16Array( this ) ) { + throw new TypeError( 'invalid invocation. `this` is not a floating-point number array.' ); + } + buf = this._buffer; + len = this._length; + if ( arguments.length === 0 ) { + begin = 0; + end = len; + } else { + if ( !isInteger( begin ) ) { + throw new TypeError( format( 'invalid argument. First argument must be an integer. Value: `%s`.', begin ) ); + } + if ( begin < 0 ) { + begin += len; + if ( begin < 0 ) { + begin = 0; + } + } + if ( arguments.length === 1 ) { + end = len; + } else { + if ( !isInteger( end ) ) { + throw new TypeError( format( 'invalid argument. Second argument must be an integer. Value: `%s`.', end ) ); + } + if ( end < 0 ) { + end += len; + if ( end < 0 ) { + end = 0; + } + } else if ( end > len ) { + end = len; + } + } + } + if ( begin < end ) { + outlen = end - begin; + } else { + outlen = 0; + } + out = new this.constructor( outlen ); + outbuf = out._buffer; // eslint-disable-line no-underscore-dangle + for ( i = 0; i < outlen; i++ ) { + outbuf[ i ] = buf[ i+begin ]; + } + return out; +}); + +/** +* Tests whether at least one element in an array passes a test implemented by a predicate function. +* +* @name some +* @memberof Float16Array.prototype +* @type {Function} +* @param {Function} predicate - predicate function +* @param {*} [thisArg] - predicate function execution context +* @throws {TypeError} `this` must be a floating-point number array +* @throws {TypeError} first argument must be a function +* @returns {boolean} boolean indicating whether at least one element passes a test +* +* @example +* function predicate( v ) { +* return v === 0.0; +* } +* +* var arr = new Float16Array( 3 ); +* +* arr.set( 0.0, 0 ); +* arr.set( 1.0, 1 ); +* arr.set( 2.0, 2 ); +* +* var bool = arr.some( predicate ); +* // returns true +*/ +setReadOnly( Float16Array.prototype, 'some', function some( predicate, thisArg ) { + var buf; + var i; + + if ( !isFloat16Array( this ) ) { + throw new TypeError( 'invalid invocation. `this` is not a floating-point number array.' ); + } + if ( !isFunction( predicate ) ) { + throw new TypeError( format( 'invalid argument. First argument must be a function. Value: `%s`.', predicate ) ); + } + buf = this._buffer; + for ( i = 0; i < this._length; i++ ) { + if ( predicate.call( thisArg, buf[ i ], i, this ) ) { + return true; + } + } + return false; +}); + +/** +* Sorts an array in-place. +* +* @name sort +* @memberof Float16Array.prototype +* @type {Function} +* @param {Function} [compareFcn] - comparison function +* @throws {TypeError} `this` must be a floating-point number array +* @throws {TypeError} first argument must be a function +* @returns {Float16Array} sorted array +* +* @example +* function compare( a, b ) { +* if ( a < b ) { +* return -1; +* } +* if ( a > b ) { +* return 1; +* } +* return 0; +* } +* +* var arr = new Float16Array( 3 ); +* +* arr.set( 1.0, 0 ); +* arr.set( 0.0, 1 ); +* arr.set( 2.0, 2 ); +* +* arr.sort( compare ); +* +* var v = arr.get( 0 ); +* // returns 0.0 +* +* v = arr.get( 1 ); +* // returns 1.0 +* +* v = arr.get( 2 ); +* // returns 2.0 +*/ +setReadOnly( Float16Array.prototype, 'sort', function sort( compareFcn ) { + var buf; + + if ( !isFloat16Array( this ) ) { + throw new TypeError( 'invalid invocation. `this` is not a floating-point number array.' ); + } + buf = this._buffer; + if ( arguments.length === 0 ) { + buf.sort(); + return this; + } + if ( !isFunction( compareFcn ) ) { + throw new TypeError( format( 'invalid argument. First argument must be a function. Value: `%s`.', compareFcn ) ); + } + buf.sort( compare ); + return this; + + /** + * Comparison function for sorting. + * + * @private + * @param {float16} a - first floating-point number for comparison + * @param {float16} b - second floating-point number for comparison + * @returns {number} comparison result + */ + function compare( a, b ) { + return compareFcn( a, b ); + } +}); + +/** +* Creates a new typed array view over the same underlying `ArrayBuffer` and with the same underlying data type as the host array. +* +* @name subarray +* @memberof Float16Array.prototype +* @type {Function} +* @param {integer} [begin] - start index (inclusive) +* @param {integer} [end] - end index (exclusive) +* @throws {TypeError} `this` must be a floating-point number array +* @throws {TypeError} first argument must be an integer +* @throws {TypeError} second argument must be an integer +* @returns {Float16Array} subarray +* +* @example +* var arr = new Float16Array( 5 ); +* +* arr.set( 0.0, 0 ); +* arr.set( 1.0, 1 ); +* arr.set( 2.0, 2 ); +* arr.set( 3.0, 3 ); +* arr.set( 4.0, 4 ); +* +* var subarr = arr.subarray(); +* // returns +* +* var len = subarr.length; +* // returns 5 +* +* var bool = subarr.get( 0 ); +* // returns 0.0 +* +* bool = subarr.get( len-1 ); +* // returns 4.0 +* +* subarr = arr.subarray( 1, -2 ); +* // returns +* +* len = subarr.length; +* // returns 2 +* +* bool = subarr.get( 0 ); +* // returns 1.0 +* +* bool = subarr.get( len-1 ); +* // returns 2.0 +*/ +setReadOnly( Float16Array.prototype, 'subarray', function subarray( begin, end ) { + var offset; + var buf; + var len; + + if ( !isFloat16Array( this ) ) { + throw new TypeError( 'invalid invocation. `this` is not a floating-point number array.' ); + } + buf = this._buffer; + len = this._length; + if ( arguments.length === 0 ) { + begin = 0; + end = len; + } else { + if ( !isInteger( begin ) ) { + throw new TypeError( format( 'invalid argument. First argument must be an integer. Value: `%s`.', begin ) ); + } + if ( begin < 0 ) { + begin += len; + if ( begin < 0 ) { + begin = 0; + } + } + if ( arguments.length === 1 ) { + end = len; + } else { + if ( !isInteger( end ) ) { + throw new TypeError( format( 'invalid argument. Second argument must be an integer. Value: `%s`.', end ) ); + } + if ( end < 0 ) { + end += len; + if ( end < 0 ) { + end = 0; + } + } else if ( end > len ) { + end = len; + } + } + } + if ( begin >= len ) { + len = 0; + offset = buf.byteLength; + } else if ( begin >= end ) { + len = 0; + offset = buf.byteOffset + ( begin*BYTES_PER_ELEMENT ); + } else { + len = end - begin; + offset = buf.byteOffset + ( begin*BYTES_PER_ELEMENT ); + } + return new this.constructor( buf.buffer, offset, ( len < 0 ) ? 0 : len ); +}); + +/** +* Serializes an array as a locale-specific string. +* +* @name toLocaleString +* @memberof Float16Array.prototype +* @type {Function} +* @param {(string|Array)} [locales] - locale identifier(s) +* @param {Object} [options] - configuration options +* @throws {TypeError} `this` must be a floating-point number array +* @throws {TypeError} first argument must be a string or an array of strings +* @throws {TypeError} options argument must be an object +* @returns {string} string representation +* +* @example +* var arr = new Float16Array( 3 ); +* +* arr.set( 0.0, 0 ); +* arr.set( 1.0, 1 ); +* arr.set( 2.0, 2 ); +* +* var str = arr.toLocaleString(); +* // returns '0,1,2' +*/ +setReadOnly( Float16Array.prototype, 'toLocaleString', function toLocaleString( locales, options ) { + var opts; + var loc; + var out; + var buf; + var i; + + if ( !isFloat16Array( this ) ) { + throw new TypeError( 'invalid invocation. `this` is not a floating-point number array.' ); + } + if ( arguments.length === 0 ) { + loc = []; + } else if ( isString( locales ) || isStringArray( locales ) ) { + loc = locales; + } else { + throw new TypeError( format( 'invalid argument. First argument must be a string or an array of strings. Value: `%s`.', locales ) ); + } + if ( arguments.length < 2 ) { + opts = {}; + } else if ( isObject( options ) ) { + opts = options; + } else { + throw new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) ); + } + buf = this._buffer; + out = []; + for ( i = 0; i < this._length; i++ ) { + out.push( buf[ i ].toLocaleString( loc, opts ) ); + } + return out.join( ',' ); +}); + +/** +* Returns a new typed array containing the elements in reversed order. +* +* @name toReversed +* @memberof Float16Array.prototype +* @type {Function} +* @throws {TypeError} `this` must be a floating-point number array +* @returns {Float16Array} reversed array +* +* @example +* var arr = new Float16Array( 3 ); +* +* arr.set( 0.0, 0 ); +* arr.set( 1.0, 1 ); +* arr.set( 2.0, 2 ); +* +* var out = arr.toReversed(); +* // returns +* +* var v = out.get( 0 ); +* // returns 2.0 +* +* v = out.get( 1 ); +* // returns 1.0 +* +* v = out.get( 2 ); +* // returns 0.0 +*/ +setReadOnly( Float16Array.prototype, 'toReversed', function toReversed() { + var outbuf; + var out; + var len; + var buf; + var i; + + if ( !isFloat16Array( this ) ) { + throw new TypeError( 'invalid invocation. `this` is not a floating-point number array.' ); + } + len = this._length; + out = new this.constructor( len ); + buf = this._buffer; + outbuf = out._buffer; // eslint-disable-line no-underscore-dangle + for ( i = 0; i < len; i++ ) { + outbuf[ i ] = buf[ len - i - 1 ]; + } + return out; +}); + +/** +* Returns a new typed array containing the elements in sorted order. +* +* @name toSorted +* @memberof Float16Array.prototype +* @type {Function} +* @param {Function} [compareFcn] - comparison function +* @throws {TypeError} `this` must be a floating-point number array +* @throws {TypeError} first argument must be a function +* @returns {Float16Array} sorted array +* +* @example +* function compare( a, b ) { +* if ( a > b ) { +* return 1; +* } +* if ( a < b ) { +* return -1; +* } +* return 0; +* } +* +* var arr = new Float16Array( 3 ); +* +* arr.set( 2.0, 0 ); +* arr.set( 0.0, 1 ); +* arr.set( 1.0, 2 ); +* +* var out = arr.sort( compare ); +* // returns +* +* var v = out.get( 0 ); +* // returns 0.0 +* +* v = out.get( 1 ); +* // returns 1.0 +* +* v = out.get( 2 ); +* // returns 2.0 +*/ +setReadOnly( Float16Array.prototype, 'toSorted', function toSorted( compareFcn ) { + var outbuf; + var out; + var len; + var buf; + var i; + + if ( !isFloat16Array( this ) ) { + throw new TypeError( 'invalid invocation. `this` is not a floating-point number array.' ); + } + len = this._length; + out = new this.constructor( len ); + buf = this._buffer; + outbuf = out._buffer; // eslint-disable-line no-underscore-dangle + for ( i = 0; i < len; i++ ) { + outbuf[ i ] = buf[ i ]; + } + if ( arguments.length === 0 ) { + outbuf.sort(); + return out; + } + if ( !isFunction( compareFcn ) ) { + throw new TypeError( format( 'invalid argument. First argument must be a function. Value: `%s`.', compareFcn ) ); + } + outbuf.sort( compare ); + return out; + + /** + * Comparison function for sorting. + * + * @private + * @param {boolean} a - first floating-point number for comparison + * @param {boolean} b - second floating-point number for comparison + * @returns {number} comparison result + */ + function compare( a, b ) { + return compareFcn( a, b ); + } +}); + +/** +* Serializes an array as a string. +* +* @name toString +* @memberof Float16Array.prototype +* @type {Function} +* @throws {TypeError} `this` must be a floating-point number array +* @returns {string} string representation +* +* @example +* var arr = new Float16Array( 3 ); +* +* arr.set( 0.0, 0 ); +* arr.set( 1.0, 1 ); +* arr.set( 2.0, 2 ); +* +* var str = arr.toString(); +* // returns '0,1,2' +*/ +setReadOnly( Float16Array.prototype, 'toString', function toString() { + var out; + var buf; + var i; + if ( !isFloat16Array( this ) ) { + throw new TypeError( 'invalid invocation. `this` is not a floating-point number array.' ); + } + out = []; + buf = this._buffer; + for ( i = 0; i < this._length; i++ ) { + out.push( buf[i] ); + } + return out.join( ',' ); +}); + +/** +* Returns an iterator for iterating over each value in a typed array. +* +* @name values +* @memberof Float16Array.prototype +* @type {Function} +* @throws {TypeError} `this` must be a floating-point number array +* @returns {Iterator} iterator +* +* @example +* var arr = new Float16Array( 2 ); +* +* arr.set( 0.0, 0 ); +* arr.set( 1.0, 1 ); +* +* var iter = arr.values(); +* +* var v = iter.next().value; +* // returns 0.0 +* +* v = iter.next().value; +* // returns 1.0 +* +* var bool = iter.next().done; +* // returns true +*/ +setReadOnly( Float16Array.prototype, 'values', function values() { + var iter; + var self; + var len; + var FLG; + var buf; + var i; + + if ( !isFloat16Array( this ) ) { + throw new TypeError( 'invalid invocation. `this` is not a floating-point number array.' ); + } + self = this; + buf = this._buffer; + len = this._length; + + // Initialize an iteration index: + i = -1; + + // Create an iterator protocol-compliant object: + iter = {}; + setReadOnly( iter, 'next', next ); + setReadOnly( iter, 'return', end ); + + if ( ITERATOR_SYMBOL ) { + setReadOnly( iter, ITERATOR_SYMBOL, factory ); + } + return iter; + + /** + * Returns an iterator protocol-compliant object containing the next iterated value. + * + * @private + * @returns {Object} iterator protocol-compliant object + */ + function next() { + i += 1; + if ( FLG || i >= len ) { + return { + 'done': true + }; + } + return { + 'value': buf[ i ], + 'done': false + }; + } + + /** + * Finishes an iterator. + * + * @private + * @param {*} [value] - value to return + * @returns {Object} iterator protocol-compliant object + */ + function end( value ) { + FLG = true; + if ( arguments.length ) { + return { + 'value': value, + 'done': true + }; + } + return { + 'done': true + }; + } + + /** + * Returns a new iterator. + * + * @private + * @returns {Iterator} iterator + */ + function factory() { + return self.values(); + } +}); + +/** +* Returns a new typed array with the element at a provided index replaced with a provided value. +* +* @name with +* @memberof Float16Array.prototype +* @type {Function} +* @param {integer} index - element index +* @param {number} value - new value +* @throws {TypeError} `this` must be a floating-point number array +* @throws {TypeError} first argument must be an integer +* @throws {RangeError} index argument is out-of-bounds +* @throws {TypeError} second argument must be a floating-point number +* @returns {Float16Array} new typed array +* +* @example +* var arr = new Float16Array( 3 ); +* +* arr.set( 0.0, 0 ); +* arr.set( 1.0, 1 ); +* arr.set( 2.0, 2 ); +* +* var out = arr.with( 0, 3.0 ); +* // returns +* +* var v = out.get( 0 ); +* // returns 3.0 +*/ +setReadOnly( Float16Array.prototype, 'with', function copyWith( index, value ) { + var buf; + var out; + var len; + + if ( !isFloat16Array( this ) ) { + throw new TypeError( 'invalid invocation. `this` is not a floating-point number array.' ); + } + if ( !isInteger( index ) ) { + throw new TypeError( format( 'invalid argument. First argument must be an integer. Value: `%s`.', index ) ); + } + len = this._length; + if ( index < 0 ) { + index += len; + } + if ( index < 0 || index >= len ) { + throw new RangeError( format( 'invalid argument. Index argument is out-of-bounds. Value: `%s`.', index ) ); + } + if ( !isNumber( value ) ) { + throw new TypeError( format( 'invalid argument. Second argument must be a floating-point number. Value: `%s`.', value ) ); + } + out = new this.constructor( this._buffer ); + buf = out._buffer; // eslint-disable-line no-underscore-dangle + buf[ index ] = value; + return out; +}); // EXPORTS // -module.exports = ctor; +module.exports = Float16Array; diff --git a/lib/node_modules/@stdlib/array/float16/lib/polyfill.js b/lib/node_modules/@stdlib/array/float16/lib/polyfill.js deleted file mode 100644 index 89b7cbf72b51..000000000000 --- a/lib/node_modules/@stdlib/array/float16/lib/polyfill.js +++ /dev/null @@ -1,1121 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2025 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var setReadOnlyAccessor = require( '@stdlib/utils/define-nonenumerable-read-only-accessor' ); -var isNonNegativeInteger = require( '@stdlib/assert/is-nonnegative-integer' ).isPrimitive; -var isObject = require( '@stdlib/assert/is-object' ); -var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); -var isInteger = require( '@stdlib/assert/is-integer' ).isPrimitive; -var isCollection = require( '@stdlib/assert/is-collection' ); -var isFunction = require( '@stdlib/assert/is-function' ); -var floor = require( '@stdlib/math/base/special/floor' ); -var min = require( '@stdlib/math/base/special/min' ); -var max = require( '@stdlib/math/base/special/max' ); -var isArray = require( '@stdlib/assert/is-array' ); -var format = require( '@stdlib/string/format' ); -var iteratorSymbol = require( '@stdlib/symbol/iterator' ); -var hasToStringTag = require( '@stdlib/assert/has-tostringtag-support' ); - - -// MAIN // - -/** -* Creates a new Float16Array constructor. -* -* @constructor -* @param {(number|Array|ArrayBuffer|TypedArray)} input - input data or buffer -* @param {NonNegativeInteger} [byteOffset=0] - byte offset -* @param {NonNegativeInteger} [length] - number of elements -* @throws {TypeError} must provide valid input -*/ -function Float16Array( input, byteOffset, length ) { - var i; - var len; - var offset; - - byteOffset = byteOffset || 0; - - if ( isNonNegativeInteger( input ) ) { - this._buffer = new Uint16Array( input ); - } else if ( input instanceof ArrayBuffer ) { - offset = Number( byteOffset ); - if ( !isNonNegativeInteger( offset ) ) { - throw new RangeError( format( 'invalid argument. Byte offset must be a non-negative integer. Value: `%s`.', byteOffset ) ); - } - - if ( arguments.length > 2 ) { - if ( !isNonNegativeInteger( length ) ) { - throw new RangeError( format( 'invalid argument. Length must be a non-negative integer. Value: `%s`.', length ) ); - } - len = length; - } else { - len = ( input.byteLength - offset ) / 2; - } - - this._buffer = new Uint16Array( input, offset, len ); - } else if ( isArray( input ) ) { - this._buffer = new Uint16Array( input.length ); - for ( i = 0; i < input.length; i++ ) { - this._buffer[ i ] = Float16Array.toFloat16( input[ i ] ); - } - } else if ( isCollection( input ) ) { - this._buffer = new Uint16Array( input.length ); - for ( i = 0; i < input.length; i++ ) { - this._buffer[ i ] = Float16Array.toFloat16( input[ i ] ); - } - } else { - throw new TypeError( format( 'invalid argument. Must provide a number, Array, ArrayBuffer, or typed array. Value: `%s`.', input ) ); - } - - this.length = this._buffer.length; -} - -setReadOnly( Float16Array, 'name', 'Float16Array' ); -setReadOnly( Float16Array, 'BYTES_PER_ELEMENT', 2 ); - -/** -* Get the underlying `ArrayBuffer`. -* -* @name buffer -* @memberof Float16Array.prototype -* @readonly -* @type {ArrayBuffer} -*/ -setReadOnlyAccessor( Float16Array.prototype, 'buffer', function getBuffer() { - return this._buffer.buffer; -}); - -/** -* Get the total number of bytes in the `Float16Array`. -* -* @name byteLength -* @memberof Float16Array.prototype -* @readonly -* @type {NonNegativeInteger} -*/ -setReadOnlyAccessor( Float16Array.prototype, 'byteLength', function getByteLength() { - return this._buffer.byteLength; -}); - -/** -* Get the byte offset within the underlying `ArrayBuffer`. -* -* @name byteOffset -* @memberof Float16Array.prototype -* @readonly -* @type {NonNegativeInteger} -*/ -setReadOnlyAccessor( Float16Array.prototype, 'byteOffset', function getByteOffset() { - return this._buffer.byteOffset; -}); - -/** -* Size in bytes of each element. -* -* @name BYTES_PER_ELEMENT -* @memberof Float16Array.prototype -* @readonly -* @type {PositiveInteger} -*/ -setReadOnlyAccessor( Float16Array.prototype, 'BYTES_PER_ELEMENT', function getBytesPerElement() { - return 2; -}); - -/** -* Returns the default string tag. -* -* @name [Symbol.toStringTag] -* @memberof Float16Array.prototype -* @readonly -* @type {string} -*/ -var symbolToStringTag = hasToStringTag() ? Symbol.toStringTag : '__toStringTag__'; -setReadOnlyAccessor( Float16Array.prototype, symbolToStringTag, function getToStringTag() { - return 'Float16Array'; -}); - -/** -* Copy a sequence of array elements within the array. -* -* @name copyWithin -* @memberof Float16Array.prototype -* @type {Function} -* @param {integer} target - target index -* @param {integer} start - start index -* @param {integer} [end] - end index (exclusive) -* @throws {TypeError} if any index is not an integer -* @throws {RangeError} if any index is out of bounds -* @returns {Float16Array} modified array -*/ -setReadOnly( Float16Array.prototype, 'copyWithin', function copyWithin( target, start, end ) { - var to; - var from; - var final; - var count; - var i; - - if ( !isInteger( target ) ) { - throw new TypeError( format( 'invalid argument. Target index must be an integer. Value: `%s`.', target ) ); - } - if ( !isInteger( start ) ) { - throw new TypeError( format( 'invalid argument. Start index must be an integer. Value: `%s`.', start ) ); - } - if ( arguments.length > 2 && !isInteger( end ) ) { - throw new TypeError( format( 'invalid argument. End index must be an integer when provided. Value: `%s`.', end ) ); - } - - to = ( target < 0 ) ? this.length + target : target; - from = ( start < 0 ) ? this.length + start : start; - final = ( arguments.length < 3 ) ? this.length : ( end < 0 ? this.length + end : end ); - - if ( to < 0 || to > this.length ) { - throw new RangeError( format( 'invalid argument. Target index is out of bounds. Value: `%s`.', target ) ); - } - if ( from < 0 || from > this.length ) { - throw new RangeError( format( 'invalid argument. Start index is out of bounds. Value: `%s`.', start ) ); - } - if ( final < 0 || final > this.length ) { - throw new RangeError( format( 'invalid argument. End index is out of bounds. Value: `%s`.', end ) ); - } - - count = min( final - from, this.length - to ); - - if ( from < to && to < ( from + count ) ) { - for ( i = count - 1; i >= 0; i-- ) { - this._buffer[ to + i ] = this._buffer[ from + i ]; - } - } else { - for ( i = 0; i < count; i++ ) { - this._buffer[ to + i ] = this._buffer[ from + i ]; - } - } - return this; -}); - -/** -* Return an iterator over [index, value] pairs. -* -* @name entries -* @memberof Float16Array.prototype -* @type {Function} -* @returns {Object} Iterator -*/ -setReadOnly( Float16Array.prototype, 'entries', function entries() { - var self = this; - var i = 0; - - function nextEntries() { - if ( i < self.length ) { - var v = [ i, self.get( i ) ]; - i += 1; - return { - value: v, - done: false - }; - } - return { - value: void 0, - done: true - }; - } - - function getEntriesIterator() { - return iterator; - } - - var iterator = { - next: nextEntries - }; - - if ( iteratorSymbol ) { - iterator[ iteratorSymbol ] = getEntriesIterator; - } - - return iterator; -}); - -/** -* Test whether all elements pass the test. -* -* @name every -* @memberof Float16Array.prototype -* @type {Function} -* @param {Function} callback - predicate function -* @param {*} [thisArg] - context -* @returns {boolean} whether all pass -*/ -setReadOnly( Float16Array.prototype, 'every', function every( callback, thisArg ) { - var i; - if ( !isFunction( callback ) ) { - throw new TypeError( format( 'invalid argument. Callback must be a function. Value: `%s`.', callback ) ); - } - for ( i = 0; i < this.length; i++ ) { - if ( !callback.call( thisArg, this.get( i ), i, this ) ) { - return false; - } - } - return true; -}); - -/** -* Fill the array with a static value. -* -* @name fill -* @memberof Float16Array.prototype -* @type {Function} -* @param {*} value - value to fill -* @param {integer} [start=0] - start index -* @param {integer} [end=array.length] - end index -* @throws {TypeError} if start or end are not integers -* @returns {Float16Array} modified array -*/ -setReadOnly( Float16Array.prototype, 'fill', function fill( value, start, end ) { - var i; - var bits; - var s; - var e; - - function createFloat16Getter( index ) { - return function getFloat16Index() { - return this.get( index ); - }; - } - - function createFloat16Setter( index ) { - return function setFloat16Index( val ) { - this.set( index, val ); - }; - } - - if ( arguments.length > 1 && !isInteger( start ) ) { - throw new TypeError( format( 'invalid argument. Start index must be an integer. Value: `%s`.', start ) ); - } - if ( arguments.length > 2 && !isInteger( end ) ) { - throw new TypeError( format( 'invalid argument. End index must be an integer when provided. Value: `%s`.', end ) ); - } - - s = ( arguments.length < 2 ) ? 0 : ( start < 0 ? max( this.length + start, 0 ) : min( start, this.length ) ); - e = ( arguments.length < 3 ) ? this.length : ( end < 0 ? max( this.length + end, 0 ) : min( end, this.length ) ); - - bits = Float16Array.toFloat16( value ); - - for ( i = s; i < e; i++ ) { - this._buffer[ i ] = bits; - Object.defineProperty( this, i, { - get: createFloat16Getter( i ), - set: createFloat16Setter( i ), - enumerable: true, - configurable: true - }); - } - return this; -}); - -/** -* Creates a new Float16Array with all elements that pass the test implemented by the provided function. -* -* @name filter -* @memberof Float16Array.prototype -* @type {Function} -* @param {Function} callback - function to test each element -* @param {*} [thisArg] - value to use as `this` when executing `callback` -* @throws {TypeError} must provide a function as callback -* @returns {Float16Array} new Float16Array with filtered elements -*/ -setReadOnly( Float16Array.prototype, 'filter', function filter( callback, thisArg ) { - var i; - var value; - var result; - - if ( !isFunction( callback ) ) { - throw new TypeError( format( 'invalid argument. Callback must be a function. Value: `%s`.', callback ) ); - } - - result = []; - for ( i = 0; i < this.length; i++ ) { - value = this.get( i ); - if ( callback.call( thisArg, value, i, this ) ) { - result.push( value ); - } - } - return new Float16Array( result ); -}); - -/** -* Returns the value of the first element that satisfies the provided testing function. -* -* @name find -* @memberof Float16Array.prototype -* @type {Function} -* @param {Function} callback - function to execute on each value -* @param {*} [thisArg] - value to use as `this` when executing `callback` -* @throws {TypeError} must provide a function as callback -* @returns {*} first matching element, or undefined if no match -*/ -setReadOnly( Float16Array.prototype, 'find', function find( callback, thisArg ) { - var i; - var value; - - if ( !isFunction( callback ) ) { - throw new TypeError( format( 'invalid argument. Callback must be a function. Value: `%s`.', callback ) ); - } - - for ( i = 0; i < this.length; i++ ) { - value = this.get( i ); - if ( callback.call( thisArg, value, i, this ) ) { - return value; - } - } - return void 0; -}); - -/** -* Returns the index of the first element that satisfies the provided testing function. -* -* @name findIndex -* @memberof Float16Array.prototype -* @type {Function} -* @param {Function} callback - function to execute on each value -* @param {*} [thisArg] - value to use as `this` when executing `callback` -* @throws {TypeError} must provide a function as callback -* @returns {integer} index of found element, or -1 if none found -*/ -setReadOnly( Float16Array.prototype, 'findIndex', function findIndexfunction( callback, thisArg ) { - var i; - var value; - - if ( !isFunction( callback ) ) { - throw new TypeError( format( 'invalid argument. Callback must be a function. Value: `%s`.', callback ) ); - } - for ( i = 0; i < this.length; i++ ) { - value = this.get( i ); - if ( callback.call( thisArg, value, i, this ) ) { - return i; - } - } - return -1; -}); - -/** -* Execute a function for each element of the array. -* -* @name forEach -* @memberof Float16Array.prototype -* @type {Function} -* @param {Function} callback - function to invoke -* @param {*} [thisArg] - context for callback -*/ -setReadOnly( Float16Array.prototype, 'forEach', function forEachfunction( callback, thisArg ) { - var i; - if ( !isFunction( callback ) ) { - throw new TypeError( format( 'invalid argument. Callback must be a function. Value: `%s`.', callback ) ); - } - for ( i = 0; i < this.length; i++ ) { - callback.call( thisArg, this.get( i ), i, this ); - } -}); - -/** -* Create a new Float16Array from an array-like or iterable object. -* -* @name from -* @memberof Float16Array -* @type {Function} -* @param {*} source - source to convert -* @param {Function} [mapFn] - optional mapping function -* @param {*} [thisArg] - execution context for mapFn -* @returns {Float16Array} new instance -* @throws {TypeError} if source is invalid or mapFn is not a function -*/ -setReadOnly( Float16Array, 'from', function from( source, mapFn, thisArg ) { - if ( source === null ) { - throw new TypeError( format( 'invalid argument. Source cannot be null or undefined. Value: %s.', source ) ); - } - if ( arguments.length > 1 && !isFunction( mapFn ) ) { - throw new TypeError( format( 'invalid argument. When provided, mapFn must be a function. Value: %s.', mapFn ) ); - } - - var values = []; - var i = 0; - - if ( isCollection( source ) ) { - for ( i = 0; i < source.length; i++ ) { - values.push( source[ i ] ); - } - } - else if ( isObject( source ) && isFunction( source[ Symbol.iterator ] ) ) { - var iterator = source[ Symbol.iterator ](); - while ( true ) { - var next = iterator.next(); - if ( next.done ) { - break; - } - values.push( next.value ); - } - } else { - throw new TypeError( format( 'invalid argument. Source must be iterable or array-like. Value: %s.', source ) ); - } - - if ( mapFn ) { - for ( i = 0; i < values.length; i++ ) { - values[ i ] = mapFn.call( thisArg, values[ i ], i ); - } - } - - var result = new Float16Array( values.length ); - for ( i = 0; i < values.length; i++ ) { - result.set( i, values[ i ] ); - } - return result; -}); - -/** -* Converts a float16 binary representation to float32. -* -* @name fromFloat16 -* @memberof Float16Array -* @type {Function} -* @param {integer} bits - Float16 bit pattern -* @throws {TypeError} must provide a 16-bit unsigned integer -* @returns {number} Float32 value -*/ -setReadOnly( Float16Array, 'fromFloat16', function fromFloat16( bits ) { - var sign; - var exponent; - var fraction; - var intView; - var floatView; - var result; - var exp32; - var frac32; - - if ( !isNonNegativeInteger( bits ) || bits > 0xFFFF ) { - throw new TypeError( format( 'invalid argument. Must provide a 16-bit unsigned integer. Value: `%s`.', bits ) ); - } - - sign = ( bits & 0x8000 ) << 16; - exponent = ( bits & 0x7C00 ) >> 10; - fraction = bits & 0x03FF; - intView = new Uint32Array( 1 ); - - if ( exponent === 0 ) { - if ( fraction === 0 ) { - result = sign; - } else { - exponent = 1; - while ( ( fraction & 0x0400 ) === 0 ) { - fraction <<= 1; - exponent -= 1; - } - fraction &= ~0x0400; - exp32 = 127 - 15 - exponent; - frac32 = fraction << 13; - result = sign | ( exp32 << 23 ) | frac32; - } - } else if ( exponent === 0x1F ) { - result = sign | 0x7F800000 | ( fraction << 13 ); - } else { - exp32 = exponent - 15 + 127; - result = sign | ( exp32 << 23 ) | ( fraction << 13 ); - } - - intView[ 0 ] = result; - floatView = new Float32Array( intView.buffer ); - return floatView[ 0 ]; -}); - -/** -* Get a value at a given index. -* -* @name get -* @memberof Float16Array.prototype -* @type {Function} -* @param {NonNegativeInteger} index - array index -* @throws {RangeError} if index is out of bounds -* @returns {number} Float32 value at index -*/ -setReadOnly( Float16Array.prototype, 'get', function getfunction( index ) { - if ( !isNonNegativeInteger( index ) || index >= this.length ) { - throw new RangeError( format( 'invalid argument. Index out of bounds. Value: `%s`.', index ) ); - } - return Float16Array.fromFloat16( this._buffer[ index ] ); -}); - -/** -* Determines whether the array includes a certain value among its entries. -* -* @name includes -* @memberof Float16Array.prototype -* @type {Function} -* @param {*} searchElement - element to search for -* @param {integer} [fromIndex=0] - index to start the search at -* @throws {TypeError} if fromIndex is not an integer -* @returns {boolean} true if the value is found, false otherwise -*/ -setReadOnly( Float16Array.prototype, 'includes', function includes( searchElement, fromIndex ) { - var i; - var start; - - if ( arguments.length > 1 && !isInteger( fromIndex ) ) { - throw new TypeError( format( 'invalid argument. FromIndex must be an integer. Value: `%s`.', fromIndex ) ); - } - - start = ( arguments.length < 2 ) ? 0 : fromIndex; - if ( start < 0 ) { - start = max( this.length + start, 0 ); - } - - for ( i = start; i < this.length; i++ ) { - if ( this.get( i ) === searchElement ) { - return true; - } - } - return false; -}); - -/** -* Returns the first index at which a given element can be found in the array, or -1 if it is not present. -* -* @name indexOf -* @memberof Float16Array.prototype -* @type {Function} -* @param {*} searchElement - element to locate -* @param {integer} [fromIndex=0] - index to start the search at -* @throws {TypeError} if fromIndex is not an integer -* @returns {integer} index of the element, or -1 if not found -*/ -setReadOnly( Float16Array.prototype, 'indexOf', function indexOf( searchElement, fromIndex ) { - var i; - var start; - - if ( arguments.length > 1 && !isInteger( fromIndex ) ) { - throw new TypeError( format( 'invalid argument. FromIndex must be an integer. Value: `%s`.', fromIndex ) ); - } - - start = ( arguments.length < 2 ) ? 0 : fromIndex; - if ( start < 0 ) { - start = max( this.length + start, 0 ); - } - - for ( i = start; i < this.length; i++ ) { - if ( this.get( i ) === searchElement ) { - return i; - } - } - return -1; -}); - -/** -* Joins all elements of the Float16Array into a string, separated by a specified separator. -* -* @name join -* @memberof Float16Array.prototype -* @type {Function} -* @param {string} [separator=','] - separator to insert between values -* @returns {string} joined string -*/ -setReadOnly( Float16Array.prototype, 'join', function join( separator ) { - var result = ''; - var i; - - separator = ( arguments.length < 0 ) ? ',' : String( separator ); - - for ( i = 0; i < this.length; i++ ) { - if ( i > 0 ) { - result += separator; - } - result += String( this.get( i ) ); - } - return result; -}); - -/** -* Return an iterator over indices. -* -* @name keys -* @memberof Float16Array.prototype -* @type {Function} -* @returns {Object} Iterator -*/ -setReadOnly( Float16Array.prototype, 'keys', function keys() { - var self = this; - var i = 0; - - function nextKeys() { - if ( i < self.length ) { - var v = i; - i += 1; - return { - value: v, - done: false - }; - } - return { - value: void 0, - done: true - }; - } - - function getKeysIterator() { - return iterator; - } - - var iterator = { - next: nextKeys - }; - - if ( iteratorSymbol ) { - iterator[ iteratorSymbol ] = getKeysIterator; - } - - return iterator; -}); - -/** -* Returns the last index at which a given element can be found in the array, or -1 if it is not present. -* -* @name lastIndexOf -* @memberof Float16Array.prototype -* @type {Function} -* @param {*} searchElement - element to locate -* @param {integer} [fromIndex=this.length-1] - index to start the search backwards from -* @throws {TypeError} if fromIndex is not an integer -* @returns {integer} last index of the element, or -1 if not found -*/ -setReadOnly( Float16Array.prototype, 'lastIndexOf', function lastIndexOffunction( searchElement, fromIndex ) { - var i; - - if ( arguments.length > 1 && !isInteger( fromIndex ) ) { - throw new TypeError( format( 'invalid argument. FromIndex must be an integer. Value: `%s`.', fromIndex ) ); - } - - i = ( arguments.length > 1 ) ? fromIndex : this.length - 1; - - if ( i >= this.length ) { - i = this.length - 1; - } - if ( i < 0 ) { - i = this.length + i; - } - - for ( ; i >= 0; i-- ) { - if ( Object.is( this.get( i ), searchElement ) ) { - return i; - } - } - return -1; -}); - -/** -* Apply a function to each element and return a new Float16Array. -* -* @name map -* @memberof Float16Array.prototype -* @type {Function} -* @param {Function} callback - mapping function -* @param {*} [thisArg] - context for callback -* @returns {Float16Array} mapped array -*/ -setReadOnly( Float16Array.prototype, 'map', function mapfunction( callback, thisArg ) { - var result = new Float16Array( this.length ); - var i; - var val; - if ( !isFunction( callback ) ) { - throw new TypeError( format( 'invalid argument. Callback must be a function. Value: `%s`.', callback ) ); - } - for ( i = 0; i < this.length; i++ ) { - val = callback.call( thisArg, this.get( i ), i, this ); - result.set( i, val ); - } - return result; -}); - -/** -* Create a new Float16Array from a variable number of arguments. -* -* @name of -* @memberof Float16Array -* @type {Function} -* @returns {Float16Array} new instance -*/ -setReadOnly( Float16Array, 'of', function of() { - var len = arguments.length; - var result = new Float16Array( len ); - var i; - - for ( i = 0; i < len; i++ ) { - result.set( i, arguments[ i ] ); - } - return result; -}); - -/** -* Reduce the array to a single value (left-to-right). -* -* @name reduce -* @memberof Float16Array.prototype -* @type {Function} -* @param {Function} callback - reducer function -* @param {*} [initialValue] - initial value -* @returns {*} accumulated result -*/ -setReadOnly( Float16Array.prototype, 'reduce', function reduce( callback, initialValue ) { - var i; - var acc; - var len; - - if ( !isFunction( callback ) ) { - throw new TypeError( format( 'invalid argument. Callback must be a function. Value: `%s`.', callback ) ); - } - len = this.length; - if ( arguments.length >= 2 ) { - acc = initialValue; - i = 0; - } else { - if ( len === 0 ) { - throw new TypeError( format( 'invalid operation. Reduce of empty array with no initial value.' ) ); - } - acc = this.get( 0 ); - i = 1; - } - for ( ; i < len; i++ ) { - acc = callback( acc, this.get( i ), i, this ); - } - return acc; -}); - -/** -* Reduce the array to a single value (right-to-left). -* -* @name reduceRight -* @memberof Float16Array.prototype -* @type {Function} -* @param {Function} callback - reducer function -* @param {*} [initialValue] - initial value -* @returns {*} accumulated result -*/ -setReadOnly( Float16Array.prototype, 'reduceRight', function reduceRight( callback, initialValue ) { - var i; - var acc; - var len; - - if ( !isFunction( callback ) ) { - throw new TypeError( format( 'invalid argument. Callback must be a function. Value: `%s`.', callback ) ); - } - len = this.length; - if ( arguments.length >= 2 ) { - acc = initialValue; - i = len - 1; - } else { - if ( len === 0 ) { - throw new TypeError( format( 'invalid operation. Reduce of empty array with no initial value.' ) ); - } - acc = this.get( len - 1 ); - i = len - 2; - } - for ( ; i >= 0; i-- ) { - acc = callback( acc, this.get( i ), i, this ); - } - return acc; -}); - -/** -* Reverse the array in place. -* -* @name reverse -* @memberof Float16Array.prototype -* @type {Function} -* @returns {Float16Array} modified array -*/ -setReadOnly( Float16Array.prototype, 'reverse', function reverse() { - var i; - var j; - var tmp; - var mid = floor( this.length / 2 ); - for ( i = 0; i < mid; i++ ) { - j = this.length - 1 - i; - tmp = this._buffer[ i ]; - this._buffer[ i ] = this._buffer[ j ]; - this._buffer[ j ] = tmp; - } - return this; -}); - -/** -* Set a value at a given index, or copy values from an array-like source. -* -* @name set -* @memberof Float16Array.prototype -* @type {Function} -* @param {(NonNegativeInteger|ArrayLike)} indexOrArray - index or source array -* @param {*} [value] - value to set or offset if copying -* @throws {RangeError} index or offset out of bounds -* @throws {TypeError} invalid arguments -*/ -setReadOnly( Float16Array.prototype, 'set', function set( indexOrArray, value ) { - var i; - var len; - var offset; - var srcVal; - - if ( isNonNegativeInteger( indexOrArray ) ) { - if ( indexOrArray >= this.length ) { - throw new RangeError( format( 'invalid argument. Index must be a nonnegative integer less than the array length. Value: `%s`.', indexOrArray ) ); - } - this._buffer[ indexOrArray ] = Float16Array.toFloat16( value ); - return; - } - - offset = ( arguments.length > 1 ) ? Number( value ) : 0; - if ( !isNonNegativeInteger( offset ) || offset > this.length ) { - throw new RangeError( format( 'invalid argument. Offset must be a nonnegative integer less than or equal to the array length. Value: `%s`.', offset ) ); - } - - if ( isArray( indexOrArray ) || isCollection( indexOrArray ) ) { - len = min( indexOrArray.length, this.length - offset ); - for ( i = 0; i < len; i++ ) { - srcVal = isFunction( indexOrArray.get ) ? indexOrArray.get( i ) : indexOrArray[ i ]; - this._buffer[ offset + i ] = Float16Array.toFloat16( srcVal ); - } - return; - } - - throw new TypeError( format( 'invalid argument. Must provide index and value or source and offset. Value: `%s`.', indexOrArray ) ); -}); - -/** -* Returns a shallow copy of a portion of the array. -* -* @name slice -* @memberof Float16Array.prototype -* @type {Function} -* @param {integer} [start=0] - start index -* @param {integer} [end=array.length] - end index (exclusive) -* @throws {TypeError} if start or end is not an integer -* @returns {Float16Array} new subarray -*/ -setReadOnly( Float16Array.prototype, 'slice', function slice( start, end ) { - var len = this.length; - var i; - var count; - var relativeStart; - var relativeEnd; - var result; - - if ( arguments.length > 0 && !isInteger( start ) ) { - throw new TypeError( format( 'invalid argument. Start index must be an integer. Value: `%s`.', start ) ); - } - if ( arguments.length > 1 && !isInteger( end ) ) { - throw new TypeError( format( 'invalid argument. End index must be an integer. Value: `%s`.', end ) ); - } - - relativeStart = ( arguments.length < 1 ) ? 0 : ( start < 0 ? max( len + start, 0 ) : min( start, len ) ); - relativeEnd = ( arguments.length < 2 ) ? len : ( end < 0 ? max( len + end, 0 ) : min( end, len ) ); - - count = max( relativeEnd - relativeStart, 0 ); - result = new Float16Array( count ); - - for ( i = 0; i < count; i++ ) { - result.set( i, this.get( relativeStart + i ) ); - } - return result; -}); - -/** -* Test whether at least one element passes the test. -* -* @name some -* @memberof Float16Array.prototype -* @type {Function} -* @param {Function} callback - predicate function -* @param {*} [thisArg] - context -* @returns {boolean} whether at least one passes -*/ -setReadOnly( Float16Array.prototype, 'some', function some( callback, thisArg ) { - var i; - var value; - - if ( !isFunction( callback ) ) { - throw new TypeError( format( 'invalid argument. Callback must be a function. Value: `%s`.', callback ) ); - } - for ( i = 0; i < this.length; i++ ) { - value = this.get( i ); - if ( callback.call( thisArg, value, i, this ) ) { - return true; - } - } - return false; -}); - -/** -* Sort the array in place. -* -* @name sort -* @memberof Float16Array.prototype -* @type {Function} -* @param {Function} [compareFn] - optional comparison function -* @throws {TypeError} if compareFn is not a function -* @returns {Float16Array} sorted array -*/ -setReadOnly( Float16Array.prototype, 'sort', function sort( compareFn ) { - var values = []; - var i; - var j; - - if ( arguments.length > 0 && !isFunction( compareFn ) ) { - throw new TypeError( format( 'invalid argument. Comparison function must be a function. Value: `%s`.', compareFn ) ); - } - - function getComparator( compareFn ) { - return function compareFloat16( a, b ) { - if ( compareFn ) { - return compareFn( a, b ); - } - if ( a !== a ) { - return 1; - } - if ( b !== b ) { - return -1; - } - return a - b; - }; -} - - for ( i = 0; i < this.length; i++ ) { - values.push( this.get( i ) ); - } - - values.sort( getComparator( compareFn ) ); - - for ( j = 0; j < this.length; j++ ) { - this.set( j, values[ j ] ); - } - return this; -}); - -/** -* Returns a new Float16Array view on the same buffer, for the specified range. -* -* @name subarray -* @memberof Float16Array.prototype -* @type {Function} -* @param {integer} [begin=0] - start index -* @param {integer} [end=this.length] - end index -* @returns {Float16Array} new Float16Array -*/ -setReadOnly( Float16Array.prototype, 'subarray', function subarray( begin, end ) { - var len = this.length; - var start = ( arguments.length < 1 ) ? 0 : ( begin < 0 ? max( len + begin, 0 ) : min( begin, len ) ); - var finish = ( arguments.length < 2 ) ? len : ( end < 0 ? max( len + end, 0 ) : min( end, len ) ); - var count = max( finish - start, 0 ); - var result = new Float16Array( count ); - - for ( var i = 0; i < count; i++ ) { - result.set( i, this.get( start + i ) ); - } - return result; -}); - -/** -* Converts a float32 number to a float16 (binary) representation. -* -* @name toFloat16 -* @memberof Float16Array -* @type {Function} -* @param {*} val - Float32 value (coerced if not number) -* @returns {integer} Float16 bit pattern -*/ -setReadOnly( Float16Array, 'toFloat16', function toFloat16( val ) { - var num = Number( val ); - - var floatView = new Float32Array( 1 ); - var intView = new Uint32Array( floatView.buffer ); - var sign; - var exponent; - var fraction; - var exp; - var frac; - var x; - - floatView[ 0 ] = num; - x = intView[ 0 ]; - - sign = ( x >> 16 ) & 0x8000; - exponent = ( ( x >> 23 ) & 0xff ) - 127; - fraction = x & 0x7fffff; - - if ( exponent <= -15 ) { - if ( exponent < -24 ) { - return sign; - } - return sign | ( ( ( fraction | 0x800000 ) >> ( -exponent - 1 ) ) >> 13 ); - } - if ( exponent >= 16 ) { - return sign | 0x7c00; - } - exp = exponent + 15; - frac = fraction >> 13; - return sign | ( exp << 10 ) | frac; -}); - -/** -* Return an iterator over values. -* -* @name values -* @memberof Float16Array.prototype -* @type {Function} -* @returns {Object} Iterator -*/ -setReadOnly( Float16Array.prototype, 'values', function values() { - var self = this; - var i = 0; - - var iterator = { - next: function nextValues() { - if ( i < self.length ) { - var v = self.get( i ); - i += 1; - return { - value: v, - done: false - }; - } - return { - value: void 0, - done: true - }; - } - }; - - if ( iteratorSymbol ) { - iterator[ iteratorSymbol ] = function getValuesIterator() { - return iterator; - }; - } - - return iterator; -}); - - -// EXPORTS // - -module.exports = Float16Array; From 96f487a6ecbe21dd3e7c853f4d78621861e67c19 Mon Sep 17 00:00:00 2001 From: gururaj1512 Date: Sat, 13 Dec 2025 18:38:50 +0530 Subject: [PATCH 09/45] refactor: implmentation and add test cases --- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: na - task: lint_package_json status: na - task: lint_repl_help status: na - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: na - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: na - task: lint_license_headers status: passed --- --- .../test.polyfill.js => lib/from_array.js} | 39 +- .../array/float16/lib/from_iterator.js | 8 +- .../array/float16/lib/from_iterator_map.js | 8 +- .../@stdlib/array/float16/lib/index.js | 2 +- .../@stdlib/array/float16/lib/main.js | 178 ++--- .../@stdlib/array/float16/test/test.at.js | 144 ++++ .../array/float16/test/test.copy_within.js | 281 +++++++ .../array/float16/test/test.entries.js | 238 ++++++ .../@stdlib/array/float16/test/test.every.js | 174 +++++ .../@stdlib/array/float16/test/test.fill.js | 305 ++++++++ .../@stdlib/array/float16/test/test.filter.js | 226 ++++++ .../@stdlib/array/float16/test/test.find.js | 174 +++++ .../array/float16/test/test.find_index.js | 173 +++++ .../array/float16/test/test.find_last.js | 174 +++++ .../float16/test/test.find_last_index.js | 173 +++++ .../array/float16/test/test.for_each.js | 196 +++++ .../@stdlib/array/float16/test/test.from.js | 654 ++++++++++++++++ .../@stdlib/array/float16/test/test.get.js | 135 ++++ .../array/float16/test/test.includes.js | 222 ++++++ .../array/float16/test/test.index_of.js | 222 ++++++ .../@stdlib/array/float16/test/test.join.js | 173 +++++ .../@stdlib/array/float16/test/test.js | 712 +++++++++++++++++- .../@stdlib/array/float16/test/test.keys.js | 261 +++++++ .../array/float16/test/test.last_index_of.js | 211 ++++++ .../@stdlib/array/float16/test/test.map.js | 178 +++++ .../@stdlib/array/float16/test/test.of.js | 130 ++++ .../@stdlib/array/float16/test/test.reduce.js | 189 +++++ .../array/float16/test/test.reduce_right.js | 192 +++++ .../array/float16/test/test.reverse.js | 122 +++ .../@stdlib/array/float16/test/test.set.js | 389 ++++++++++ .../@stdlib/array/float16/test/test.slice.js | 311 ++++++++ .../@stdlib/array/float16/test/test.some.js | 196 +++++ .../@stdlib/array/float16/test/test.sort.js | 173 +++++ .../array/float16/test/test.subarray.js | 308 ++++++++ .../float16/test/test.to_locale_string.js | 187 +++++ .../array/float16/test/test.to_reversed.js | 121 +++ .../array/float16/test/test.to_sorted.js | 172 +++++ .../array/float16/test/test.to_string.js | 97 +++ .../@stdlib/array/float16/test/test.values.js | 264 +++++++ .../@stdlib/array/float16/test/test.with.js | 213 ++++++ 40 files changed, 8195 insertions(+), 130 deletions(-) rename lib/node_modules/@stdlib/array/float16/{test/test.polyfill.js => lib/from_array.js} (50%) create mode 100644 lib/node_modules/@stdlib/array/float16/test/test.at.js create mode 100644 lib/node_modules/@stdlib/array/float16/test/test.copy_within.js create mode 100644 lib/node_modules/@stdlib/array/float16/test/test.entries.js create mode 100644 lib/node_modules/@stdlib/array/float16/test/test.every.js create mode 100644 lib/node_modules/@stdlib/array/float16/test/test.fill.js create mode 100644 lib/node_modules/@stdlib/array/float16/test/test.filter.js create mode 100644 lib/node_modules/@stdlib/array/float16/test/test.find.js create mode 100644 lib/node_modules/@stdlib/array/float16/test/test.find_index.js create mode 100644 lib/node_modules/@stdlib/array/float16/test/test.find_last.js create mode 100644 lib/node_modules/@stdlib/array/float16/test/test.find_last_index.js create mode 100644 lib/node_modules/@stdlib/array/float16/test/test.for_each.js create mode 100644 lib/node_modules/@stdlib/array/float16/test/test.from.js create mode 100644 lib/node_modules/@stdlib/array/float16/test/test.get.js create mode 100644 lib/node_modules/@stdlib/array/float16/test/test.includes.js create mode 100644 lib/node_modules/@stdlib/array/float16/test/test.index_of.js create mode 100644 lib/node_modules/@stdlib/array/float16/test/test.join.js create mode 100644 lib/node_modules/@stdlib/array/float16/test/test.keys.js create mode 100644 lib/node_modules/@stdlib/array/float16/test/test.last_index_of.js create mode 100644 lib/node_modules/@stdlib/array/float16/test/test.map.js create mode 100644 lib/node_modules/@stdlib/array/float16/test/test.of.js create mode 100644 lib/node_modules/@stdlib/array/float16/test/test.reduce.js create mode 100644 lib/node_modules/@stdlib/array/float16/test/test.reduce_right.js create mode 100644 lib/node_modules/@stdlib/array/float16/test/test.reverse.js create mode 100644 lib/node_modules/@stdlib/array/float16/test/test.set.js create mode 100644 lib/node_modules/@stdlib/array/float16/test/test.slice.js create mode 100644 lib/node_modules/@stdlib/array/float16/test/test.some.js create mode 100644 lib/node_modules/@stdlib/array/float16/test/test.sort.js create mode 100644 lib/node_modules/@stdlib/array/float16/test/test.subarray.js create mode 100644 lib/node_modules/@stdlib/array/float16/test/test.to_locale_string.js create mode 100644 lib/node_modules/@stdlib/array/float16/test/test.to_reversed.js create mode 100644 lib/node_modules/@stdlib/array/float16/test/test.to_sorted.js create mode 100644 lib/node_modules/@stdlib/array/float16/test/test.to_string.js create mode 100644 lib/node_modules/@stdlib/array/float16/test/test.values.js create mode 100644 lib/node_modules/@stdlib/array/float16/test/test.with.js diff --git a/lib/node_modules/@stdlib/array/float16/test/test.polyfill.js b/lib/node_modules/@stdlib/array/float16/lib/from_array.js similarity index 50% rename from lib/node_modules/@stdlib/array/float16/test/test.polyfill.js rename to lib/node_modules/@stdlib/array/float16/lib/from_array.js index 7ede661f99ee..4059e4043151 100644 --- a/lib/node_modules/@stdlib/array/float16/test/test.polyfill.js +++ b/lib/node_modules/@stdlib/array/float16/lib/from_array.js @@ -20,25 +20,32 @@ // MODULES // -var tape = require( 'tape' ); -var ctor = require( './../lib/polyfill.js' ); +var float64ToFloat16 = require( '@stdlib/number/float64/base/to-float16' ); +var toWord = require( '@stdlib/number/float16/base/to-word' ); -// TESTS // +// MAIN // -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof ctor, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function throws an error when invoked', function test( t ) { - t.throws( foo, Error, 'throws an error' ); - t.end(); +/** +* Fills an output array with "unsigned 16-bit integer corresponding to the IEEE 754 binary representation of a half-precision floating-point number" values. +* +* @private +* @param {Uint16Array} buf - output array +* @param {Array} arr - input array +* @returns {Uint16Array} output array +*/ +function fromArray( buf, arr ) { + var len; + var i; - function foo() { - var f = new ctor(); // eslint-disable-line no-unused-vars + len = arr.length; + for ( i = 0; i < len; i++ ) { + buf[ i ] = toWord( float64ToFloat16( arr[ i ] ) ); } -}); + return buf; +} + + +// EXPORTS // -// TODO: tests +module.exports = fromArray; diff --git a/lib/node_modules/@stdlib/array/float16/lib/from_iterator.js b/lib/node_modules/@stdlib/array/float16/lib/from_iterator.js index b6190d00321b..57510e37fbf8 100644 --- a/lib/node_modules/@stdlib/array/float16/lib/from_iterator.js +++ b/lib/node_modules/@stdlib/array/float16/lib/from_iterator.js @@ -18,6 +18,12 @@ 'use strict'; +// MODULES // + +var float64ToFloat16 = require( '@stdlib/number/float64/base/to-float16' ); +var toWord = require( '@stdlib/number/float16/base/to-word' ); + + // MAIN // /** @@ -37,7 +43,7 @@ function fromIterator( it ) { if ( v.done ) { break; } - out.push( v.value ); + out.push( toWord( float64ToFloat16( v.value ) ) ); } return out; } diff --git a/lib/node_modules/@stdlib/array/float16/lib/from_iterator_map.js b/lib/node_modules/@stdlib/array/float16/lib/from_iterator_map.js index 346ff3c47a4f..0d2512c29a55 100644 --- a/lib/node_modules/@stdlib/array/float16/lib/from_iterator_map.js +++ b/lib/node_modules/@stdlib/array/float16/lib/from_iterator_map.js @@ -18,6 +18,12 @@ 'use strict'; +// MODULES // + +var float64ToFloat16 = require( '@stdlib/number/float64/base/to-float16' ); +var toWord = require( '@stdlib/number/float16/base/to-word' ); + + // MAIN // /** @@ -42,7 +48,7 @@ function fromIteratorMap( it, clbk, thisArg ) { break; } i += 1; - out.push( clbk.call( thisArg, v.value, i ) ); + out.push( toWord( float64ToFloat16( clbk.call( thisArg, v.value, i ) ) ) ); } return out; } diff --git a/lib/node_modules/@stdlib/array/float16/lib/index.js b/lib/node_modules/@stdlib/array/float16/lib/index.js index 175d68ace350..3915f06a9f15 100644 --- a/lib/node_modules/@stdlib/array/float16/lib/index.js +++ b/lib/node_modules/@stdlib/array/float16/lib/index.js @@ -44,7 +44,7 @@ * @example * var Float16Array = require( '@stdlib/array/float16' ); * -* var arr = new Float16Array( [ true, false ] ); +* var arr = new Float16Array( [ 1.0, 2.0 ] ); * // returns * * var len = arr.length; diff --git a/lib/node_modules/@stdlib/array/float16/lib/main.js b/lib/node_modules/@stdlib/array/float16/lib/main.js index 50f9ecab5697..a8cf5a2226d0 100644 --- a/lib/node_modules/@stdlib/array/float16/lib/main.js +++ b/lib/node_modules/@stdlib/array/float16/lib/main.js @@ -1,7 +1,3 @@ -/* eslint-disable stdlib/jsdoc-no-undefined-references */ -/* eslint-disable stdlib/jsdoc-no-shortcut-reference-link */ -/* eslint-disable stdlib/jsdoc-typedef-typos, max-lines */ - /** * @license Apache-2.0 * @@ -20,7 +16,7 @@ * limitations under the License. */ -/* eslint-disable no-restricted-syntax, no-invalid-this */ +/* eslint-disable no-restricted-syntax, no-invalid-this, stdlib/jsdoc-typedef-typos, max-lines */ 'use strict'; @@ -32,7 +28,6 @@ var isObject = require( '@stdlib/assert/is-object' ); var isNumber = require( '@stdlib/assert/is-number' ).isPrimitive; var isStringArray = require( '@stdlib/assert/is-string-array' ).primitives; var isString = require( '@stdlib/assert/is-string' ).isPrimitive; -var isArrayLikeObject = require( '@stdlib/assert/is-array-like-object' ); var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); var isInteger = require( '@stdlib/assert/is-integer' ).isPrimitive; var hasIteratorSymbolSupport = require( '@stdlib/assert/has-iterator-symbol-support' ); @@ -43,10 +38,15 @@ var isFunction = require( '@stdlib/assert/is-function' ); var floor = require( '@stdlib/math/base/special/floor' ); var format = require( '@stdlib/string/format' ); var Uint16Array = require( '@stdlib/array/uint16' ); +var float64ToFloat16 = require( '@stdlib/number/float64/base/to-float16' ); +var toWord = require( '@stdlib/number/float16/base/to-word' ); +var fromWord = require( '@stdlib/number/float16/base/from-word' ); var getter = require( '@stdlib/array/base/getter' ); var accessorGetter = require( '@stdlib/array/base/accessor-getter' ); +var isTypedArray = require( '@stdlib/assert/is-typed-array' ); var fromIterator = require( './from_iterator.js' ); var fromIteratorMap = require( './from_iterator_map.js' ); +var fromArray = require( './from_array.js' ); // VARIABLES // @@ -117,11 +117,11 @@ function isFloatingPointArrayConstructor( value ) { * // returns 5 * * @example -* var arr = new Float16Array( [ 1.0, 2.0, 3.0, 4.0 ] ); +* var arr = new Float16Array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] ); * // returns * * var len = arr.length; -* // returns 4 +* // returns 5 * * @example * var ArrayBuffer = require( '@stdlib/array/buffer' ); @@ -158,6 +158,7 @@ function Float16Array() { var nargs; var buf; var len; + var arg; nargs = arguments.length; if ( !(this instanceof Float16Array) ) { @@ -176,28 +177,34 @@ function Float16Array() { if ( nargs === 0 ) { buf = new Uint16Array( 0 ); // backward-compatibility } else if ( nargs === 1 ) { - if ( isNonNegativeInteger( arguments[0] ) ) { - buf = new Uint16Array( arguments[0] ); - } else if ( isCollection( arguments[0] ) ) { - buf = arguments[0]; - buf = new Uint16Array( buf ); - } else if ( isArrayBuffer( arguments[0] ) ) { - buf = new Uint16Array( arguments[0] ); - } else if ( isObject( arguments[0] ) ) { - buf = arguments[ 0 ]; + arg = arguments[ 0 ]; + if ( isNonNegativeInteger( arg ) ) { + buf = new Uint16Array( arg ); + } else if ( isCollection( arg ) ) { + buf = fromArray( new Uint16Array( arg.length ), arg ); + } else if ( isArrayBuffer( arg ) ) { + if ( !isInteger( arg.byteLength/BYTES_PER_ELEMENT ) ) { + throw new RangeError( format( 'invalid argument. ArrayBuffer byte length must be a multiple of %u. Byte length: `%u`.', BYTES_PER_ELEMENT, arg.byteLength ) ); + } + buf = new Uint16Array( arg ); + } else if ( isObject( arg ) ) { if ( HAS_ITERATOR_SYMBOL === false ) { - throw new TypeError( format( 'invalid argument. Environment lacks Symbol.iterator support. Must provide a length, ArrayBuffer, typed array, or array-like object. Value: `%s`.', buf ) ); + throw new TypeError( format( 'invalid argument. Environment lacks Symbol.iterator support. Must provide a length, ArrayBuffer, typed array, or array-like object. Value: `%s`.', arg ) ); } - if ( !isFunction( buf[ ITERATOR_SYMBOL ] ) ) { - throw new TypeError( format( 'invalid argument. Must provide a length, ArrayBuffer, typed array, array-like object, or an iterable. Value: `%s`.', buf ) ); + if ( !isFunction( arg[ ITERATOR_SYMBOL ] ) ) { + throw new TypeError( format( 'invalid argument. Must provide a length, ArrayBuffer, typed array, array-like object, or an iterable. Value: `%s`.', arg ) ); } - buf = buf[ ITERATOR_SYMBOL ](); + buf = arg[ ITERATOR_SYMBOL ](); if ( !isFunction( buf.next ) ) { - throw new TypeError( format( 'invalid argument. Must provide a length, ArrayBuffer, typed array, array-like object, or an iterable. Value: `%s`.', buf ) ); + throw new TypeError( format( 'invalid argument. Must provide a length, ArrayBuffer, typed array, array-like object, or an iterable. Value: `%s`.', arg ) ); + } + buf = fromIterator( buf ); + if ( buf instanceof Error ) { + throw buf; } - buf = new Uint16Array( fromIterator( buf ) ); + buf = new Uint16Array( buf ); } else { - throw new TypeError( format( 'invalid argument. Must provide a length, ArrayBuffer, typed array, array-like object, or an iterable. Value: `%s`.', arguments[0] ) ); + throw new TypeError( format( 'invalid argument. Must provide a length, ArrayBuffer, typed array, array-like object, or an iterable. Value: `%s`.', arg ) ); } } else { buf = arguments[ 0 ]; @@ -208,7 +215,14 @@ function Float16Array() { if ( !isNonNegativeInteger( byteOffset ) ) { throw new TypeError( format( 'invalid argument. Byte offset must be a nonnegative integer. Value: `%s`.', byteOffset ) ); } + if ( !isInteger( byteOffset/BYTES_PER_ELEMENT ) ) { + throw new RangeError( format( 'invalid argument. Byte offset must be a multiple of %u. Value: `%u`.', BYTES_PER_ELEMENT, byteOffset ) ); + } if ( nargs === 2 ) { + len = buf.byteLength - byteOffset; + if ( !isInteger( len/BYTES_PER_ELEMENT ) ) { + throw new RangeError( format( 'invalid arguments. ArrayBuffer view byte length must be a multiple of %u. View byte length: `%u`.', BYTES_PER_ELEMENT, len ) ); + } buf = new Uint16Array( buf, byteOffset ); } else { len = arguments[ 2 ]; @@ -270,14 +284,7 @@ setReadOnly( Float16Array, 'name', 'Float16Array' ); * @throws {TypeError} `this` must be a floating-point array * @throws {TypeError} first argument must be an array-like object or an iterable * @throws {TypeError} second argument must be a function -* @returns {Float16Array} floating-point number array -* -* @example -* var arr = Float16Array.from( [ 1.0, 2.0 ] ); -* // returns -* -* var len = arr.length; -* // returns 2 +* @returns {Float16Array} 16-bit floating-point number array * * @example * var arr = Float16Array.from( [ 1.0, 2.0 ] ); @@ -323,23 +330,6 @@ setReadOnly( Float16Array, 'from', function from( src ) { thisArg = arguments[ 2 ]; } } - if ( isArrayLikeObject( src ) ) { - if ( clbk ) { - len = src.length; - if ( src.get && src.set ) { - get = accessorGetter( 'default' ); - } else { - get = getter( 'default' ); - } - out = new this( len ); - buf = out._buffer; // eslint-disable-line no-underscore-dangle - for ( i = 0; i < len; i++ ) { - buf[ i ] = clbk.call( thisArg, get( src, i ), i ); - } - return out; - } - return new this( src ); - } if ( isCollection( src ) ) { if ( clbk ) { len = src.length; @@ -351,7 +341,7 @@ setReadOnly( Float16Array, 'from', function from( src ) { out = new this( len ); buf = out._buffer; // eslint-disable-line no-underscore-dangle for ( i = 0; i < len; i++ ) { - buf[ i ] = clbk.call( thisArg, get( src, i ), i ); + buf[ i ] = toWord( float64ToFloat16( clbk.call( thisArg, get( src, i ), i ) ) ); } return out; } @@ -367,6 +357,9 @@ setReadOnly( Float16Array, 'from', function from( src ) { } else { tmp = fromIterator( buf ); } + if ( tmp instanceof Error ) { + throw tmp; + } len = tmp.length; out = new this( len ); buf = out._buffer; // eslint-disable-line no-underscore-dangle @@ -393,6 +386,9 @@ setReadOnly( Float16Array, 'from', function from( src ) { * var arr = Float16Array.of( 1.0, 1.0, 1.0, 1.0 ); * // returns * +* var x = arr.get(0); +* // returns 1.0 +* * var len = arr.length; * // returns 4 */ @@ -452,7 +448,7 @@ setReadOnly( Float16Array.prototype, 'at', function at( idx ) { if ( idx < 0 || idx >= this._length ) { return; } - return this._buffer[ idx ]; + return fromWord( this._buffer[ idx ] ); }); /** @@ -640,7 +636,7 @@ setReadOnly( Float16Array.prototype, 'entries', function entries() { }; } return { - 'value': [ i, buf[ i ] ], + 'value': [ i, fromWord( buf[ i ] ) ], 'done': false }; } @@ -714,7 +710,7 @@ setReadOnly( Float16Array.prototype, 'every', function every( predicate, thisArg } buf = this._buffer; for ( i = 0; i < this._length; i++ ) { - if ( !predicate.call( thisArg, buf[ i ], i, this ) ) { + if ( !predicate.call( thisArg, fromWord( buf[ i ] ), i, this ) ) { return false; } } @@ -794,7 +790,7 @@ setReadOnly( Float16Array.prototype, 'fill', function fill( value, start, end ) end = len; } for ( i = start; i < end; i++ ) { - buf[ i ] = value; + buf[ i ] = toWord( value ); } return this; }); @@ -849,7 +845,7 @@ setReadOnly( Float16Array.prototype, 'filter', function filter( predicate, thisA buf = this._buffer; out = []; for ( i = 0; i < this._length; i++ ) { - v = buf[ i ]; + v = fromWord( buf[ i ] ); if ( predicate.call( thisArg, v, i, this ) ) { out.push( v ); } @@ -896,7 +892,7 @@ setReadOnly( Float16Array.prototype, 'find', function find( predicate, thisArg ) } buf = this._buffer; for ( i = 0; i < this._length; i++ ) { - v = buf[ i ]; + v = fromWord( buf[ i ] ); if ( predicate.call( thisArg, v, i, this ) ) { return v; } @@ -942,7 +938,7 @@ setReadOnly( Float16Array.prototype, 'findIndex', function findIndex( predicate, } buf = this._buffer; for ( i = 0; i < this._length; i++ ) { - v = buf[ i ]; + v = fromWord( buf[ i ] ); if ( predicate.call( thisArg, v, i, this ) ) { return i; } @@ -989,7 +985,7 @@ setReadOnly( Float16Array.prototype, 'findLast', function findLast( predicate, t } buf = this._buffer; for ( i = this._length-1; i >= 0; i-- ) { - v = buf[ i ]; + v = fromWord( buf[ i ] ); if ( predicate.call( thisArg, v, i, this ) ) { return v; } @@ -1035,7 +1031,7 @@ setReadOnly( Float16Array.prototype, 'findLastIndex', function findLastIndex( pr } buf = this._buffer; for ( i = this._length-1; i >= 0; i-- ) { - v = buf[ i ]; + v = fromWord( buf[ i ] ); if ( predicate.call( thisArg, v, i, this ) ) { return i; } @@ -1079,7 +1075,7 @@ setReadOnly( Float16Array.prototype, 'forEach', function forEach( fcn, thisArg ) } buf = this._buffer; for ( i = 0; i < this._length; i++ ) { - fcn.call( thisArg, buf[ i ], i, this ); + fcn.call( thisArg, fromWord( buf[ i ] ), i, this ); } }); @@ -1118,7 +1114,7 @@ setReadOnly( Float16Array.prototype, 'get', function get( idx ) { if ( idx >= this._length ) { return; } - return this._buffer[ idx ]; + return fromWord( this._buffer[ idx ] ); }); /** @@ -1177,7 +1173,7 @@ setReadOnly( Float16Array.prototype, 'includes', function includes( searchElemen } buf = this._buffer; for ( i = fromIndex; i < this._length; i++ ) { - if ( searchElement === buf[ i ] ) { + if ( searchElement === fromWord( buf[ i ] ) ) { return true; } } @@ -1240,7 +1236,7 @@ setReadOnly( Float16Array.prototype, 'indexOf', function indexOf( searchElement, } buf = this._buffer; for ( i = fromIndex; i < this._length; i++ ) { - if ( searchElement === buf[ i ] ) { + if ( searchElement === fromWord( buf[ i ] ) ) { return i; } } @@ -1289,7 +1285,7 @@ setReadOnly( Float16Array.prototype, 'join', function join( separator ) { buf = this._buffer; out = []; for ( i = 0; i < this._length; i++ ) { - out.push( buf[i] ); + out.push( fromWord( buf[i] ) ); } return out.join( separator ); }); @@ -1404,8 +1400,8 @@ setReadOnly( Float16Array.prototype, 'keys', function keys() { * @type {Function} * @param {number} searchElement - element to find * @param {integer} [fromIndex] - starting index (inclusive) -* @throws {TypeError} `this` must be a boolean array -* @throws {TypeError} first argument must be a boolean value +* @throws {TypeError} `this` must be a floating-point number +* @throws {TypeError} first argument must be a numeric value * @throws {TypeError} second argument must be an integer * @returns {integer} index or -1 * @@ -1454,7 +1450,7 @@ setReadOnly( Float16Array.prototype, 'lastIndexOf', function lastIndexOf( search } buf = this._buffer; for ( i = fromIndex; i >= 0; i-- ) { - if ( searchElement === buf[ i ] ) { + if ( searchElement === fromWord( buf[ i ] ) ) { return i; } } @@ -1529,7 +1525,7 @@ setReadOnly( Float16Array.prototype, 'map', function map( fcn, thisArg ) { out = new this.constructor( this._length ); outbuf = out._buffer; // eslint-disable-line no-underscore-dangle for ( i = 0; i < this._length; i++ ) { - outbuf[ i ] = fcn.call( thisArg, buf[ i ], i, this ); + outbuf[ i ] = toWord( fcn.call( thisArg, fromWord( buf[ i ] ), i, this ) ); // eslint-disable-line max-len } return out; }); @@ -1581,11 +1577,11 @@ setReadOnly( Float16Array.prototype, 'reduce', function reduce( reducer, initial if ( len === 0 ) { throw new Error( 'invalid operation. If not provided an initial value, an array must contain at least one element.' ); } - acc = buf[ 0 ]; + acc = fromWord( buf[ 0 ] ); i = 1; } for ( ; i < len; i++ ) { - acc = reducer( acc, buf[ i ], i, this ); + acc = reducer( acc, fromWord( buf[ i ] ), i, this ); } return acc; }); @@ -1637,11 +1633,11 @@ setReadOnly( Float16Array.prototype, 'reduceRight', function reduceRight( reduce if ( len === 0 ) { throw new Error( 'invalid operation. If not provided an initial value, an array must contain at least one element.' ); } - acc = buf[ len-1 ]; + acc = fromWord( buf[ len-1 ] ); i = len - 2; } for ( ; i >= 0; i-- ) { - acc = reducer( acc, buf[ i ], i, this ); + acc = reducer( acc, fromWord( buf[ i ] ), i, this ); } return acc; }); @@ -1788,15 +1784,21 @@ setReadOnly( Float16Array.prototype, 'set', function set( value ) { } sbuf = tmp; } - for ( i = 0; i < N; idx++, i++ ) { - buf[ idx ] = sbuf[ i ]; + if ( isTypedArray( value ) || isFloat16Array( value ) ) { + for ( i = 0; i < N; idx++, i++ ) { + buf[ idx ] = sbuf[ i ]; + } + } else { + for ( i = 0; i < N; idx++, i++ ) { + buf[ idx ] = toWord( sbuf[ i ] ); + } } return; } if ( idx >= this._length ) { throw new RangeError( format( 'invalid argument. Index argument is out-of-bounds. Value: `%u`.', idx ) ); } - buf[ idx ] = value; + buf[ idx ] = toWord( value ); }); /** @@ -1827,10 +1829,10 @@ setReadOnly( Float16Array.prototype, 'set', function set( value ) { * var len = out.length; * // returns 5 * -* var bool = out.get( 0 ); +* var v = out.get( 0 ); * // returns 0.0 * -* bool = out.get( len-1 ); +* v = out.get( len-1 ); * // returns 4.0 * * out = arr.slice( 1, -2 ); @@ -1839,10 +1841,10 @@ setReadOnly( Float16Array.prototype, 'set', function set( value ) { * len = out.length; * // returns 2 * -* bool = out.get( 0 ); +* v = out.get( 0 ); * // returns 1.0 * -* bool = out.get( len-1 ); +* v = out.get( len-1 ); * // returns 2.0 */ setReadOnly( Float16Array.prototype, 'slice', function slice( begin, end ) { @@ -1938,7 +1940,7 @@ setReadOnly( Float16Array.prototype, 'some', function some( predicate, thisArg ) } buf = this._buffer; for ( i = 0; i < this._length; i++ ) { - if ( predicate.call( thisArg, buf[ i ], i, this ) ) { + if ( predicate.call( thisArg, fromWord( buf[ i ] ), i, this ) ) { return true; } } @@ -2162,7 +2164,7 @@ setReadOnly( Float16Array.prototype, 'toLocaleString', function toLocaleString( buf = this._buffer; out = []; for ( i = 0; i < this._length; i++ ) { - out.push( buf[ i ].toLocaleString( loc, opts ) ); + out.push( fromWord( buf[ i ] ).toLocaleString( loc, opts ) ); } return out.join( ',' ); }); @@ -2324,7 +2326,7 @@ setReadOnly( Float16Array.prototype, 'toString', function toString() { out = []; buf = this._buffer; for ( i = 0; i < this._length; i++ ) { - out.push( buf[i] ); + out.push( fromWord( buf[i] ) ); } return out.join( ',' ); }); @@ -2397,7 +2399,7 @@ setReadOnly( Float16Array.prototype, 'values', function values() { }; } return { - 'value': buf[ i ], + 'value': fromWord( buf[ i ] ), 'done': false }; } @@ -2464,6 +2466,7 @@ setReadOnly( Float16Array.prototype, 'with', function copyWith( index, value ) { var buf; var out; var len; + var i; if ( !isFloat16Array( this ) ) { throw new TypeError( 'invalid invocation. `this` is not a floating-point number array.' ); @@ -2481,9 +2484,14 @@ setReadOnly( Float16Array.prototype, 'with', function copyWith( index, value ) { if ( !isNumber( value ) ) { throw new TypeError( format( 'invalid argument. Second argument must be a floating-point number. Value: `%s`.', value ) ); } - out = new this.constructor( this._buffer ); + out = []; + buf = this._buffer; + for ( i = 0; i < len; i++ ) { + out[ i ] = fromWord( buf[ i ] ); + } + out = new this.constructor( out ); buf = out._buffer; // eslint-disable-line no-underscore-dangle - buf[ index ] = value; + buf[ index ] = toWord( value ); return out; }); diff --git a/lib/node_modules/@stdlib/array/float16/test/test.at.js b/lib/node_modules/@stdlib/array/float16/test/test.at.js new file mode 100644 index 000000000000..9e43cd21a712 --- /dev/null +++ b/lib/node_modules/@stdlib/array/float16/test/test.at.js @@ -0,0 +1,144 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var hasOwnProp = require( '@stdlib/assert/has-own-property' ); +var isFunction = require( '@stdlib/assert/is-function' ); +var float64ToFloat16 = require( '@stdlib/number/float64/base/to-float16' ); +var Float16Array = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof Float16Array, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'attached to the prototype of the main export is an `at` method for returning an array element', function test( t ) { + t.strictEqual( hasOwnProp( Float16Array.prototype, 'at' ), true, 'has property' ); + t.strictEqual( isFunction( Float16Array.prototype.at ), true, 'has method' ); + t.end(); +}); + +tape( 'the method throws an error if invoked with a `this` context which is not a floating-point number array instance', function test( t ) { + var values; + var arr; + var i; + + arr = new Float16Array( 5 ); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + {}, + [] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + return arr.at.call( value, 0 ); + }; + } +}); + +tape( 'the method throws an error if provided an index argument which is not an integer', function test( t ) { + var values; + var arr; + var i; + + arr = new Float16Array( 10 ); + + values = [ + '5', + 3.14, + NaN, + true, + false, + null, + void 0, + {}, + [], + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + return arr.at( value ); + }; + } +}); + +tape( 'the method returns `undefined` if provided an index which exceeds array dimensions', function test( t ) { + var arr; + var v; + var i; + + arr = new Float16Array( 10 ); + for ( i = -arr.length; i < arr.length; i++ ) { + if ( i < 0 ) { + v = arr.at( i-arr.length ); + t.strictEqual( v, void 0, 'returns expected value for index '+(i-arr.length) ); + } else { + v = arr.at( arr.length+i ); + t.strictEqual( v, void 0, 'returns expected value for index '+(arr.length+i) ); + } + } + t.end(); +}); + +tape( 'the method returns an array element', function test( t ) { + var arr; + var v; + var i; + + arr = []; + for ( i = 0; i < 10; i++ ) { + arr.push( i+0.05 ); + } + + arr = new Float16Array( arr ); + + for ( i = -arr.length; i < arr.length; i++ ) { + v = arr.at( i ); + if ( i < 0 ) { + t.strictEqual( v, float64ToFloat16( arr.length + i + 0.05 ), 'returns expected value for index '+i ); + } else { + t.strictEqual( v, float64ToFloat16( i + 0.05 ), 'returns expected value for index '+i ); + } + } + t.end(); +}); diff --git a/lib/node_modules/@stdlib/array/float16/test/test.copy_within.js b/lib/node_modules/@stdlib/array/float16/test/test.copy_within.js new file mode 100644 index 000000000000..188e4187ac9f --- /dev/null +++ b/lib/node_modules/@stdlib/array/float16/test/test.copy_within.js @@ -0,0 +1,281 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var hasOwnProp = require( '@stdlib/assert/has-own-property' ); +var isFunction = require( '@stdlib/assert/is-function' ); +var Uint16Array = require( '@stdlib/array/uint16' ); +var toWord = require( '@stdlib/number/float16/base/to-word' ); +var Float16Array = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof Float16Array, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'attached to the prototype of the main export is a `copyWithin` method for copying a sequence of array elements within a floating-point number array', function test( t ) { + t.strictEqual( hasOwnProp( Float16Array.prototype, 'copyWithin' ), true, 'has property' ); + t.strictEqual( isFunction( Float16Array.prototype.copyWithin ), true, 'has method' ); + t.end(); +}); + +tape( 'the method throws an error if invoked with a `this` context which is not a floating-point number array instance', function test( t ) { + var values; + var arr; + var i; + + arr = new Float16Array( 5 ); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + {}, + [] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + return arr.copyWithin.call( value, 3, 0 ); + }; + } +}); + +tape( 'the method throws an error if invoked with a `this` context which is not a floating-point number array instance (end)', function test( t ) { + var values; + var arr; + var i; + + arr = new Float16Array( 5 ); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + {}, + [] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + return arr.copyWithin.call( value, 3, 0, 5 ); + }; + } +}); + +tape( 'the method copies a sequence of elements within an array', function test( t ) { + var arr; + var buf; + + arr = [ 0.05, 1.05, 2.05, 3.05, 4.05 ]; + arr = new Float16Array( arr ); + + arr.copyWithin( 0, 3 ); + buf = new Uint16Array( arr.buffer ); + + // Overwritten: + t.strictEqual( buf[ 0 ], toWord( 3.05 ), 'returns expected value' ); + t.strictEqual( buf[ 1 ], toWord( 4.05 ), 'returns expected value' ); + + // Remain the same: + t.strictEqual( buf[ 2 ], toWord( 2.05 ), 'returns expected value' ); + t.strictEqual( buf[ 3 ], toWord( 3.05 ), 'returns expected value' ); + t.strictEqual( buf[ 4 ], toWord( 4.05 ), 'returns expected value' ); + + t.end(); +}); + +tape( 'the method copies a sequence of elements within an array (negative target)', function test( t ) { + var arr; + var buf; + + arr = [ 0.05, 1.05, 2.05, 3.05, 4.05 ]; + arr = new Float16Array( arr ); + + arr.copyWithin( -arr.length, 3 ); + buf = new Uint16Array( arr.buffer ); + + // Overwritten: + t.strictEqual( buf[ 0 ], toWord( 3.05 ), 'returns expected value' ); + t.strictEqual( buf[ 1 ], toWord( 4.05 ), 'returns expected value' ); + + // Remain the same: + t.strictEqual( buf[ 2 ], toWord( 2.05 ), 'returns expected value' ); + t.strictEqual( buf[ 3 ], toWord( 3.05 ), 'returns expected value' ); + t.strictEqual( buf[ 4 ], toWord( 4.05 ), 'returns expected value' ); + + t.end(); +}); + +tape( 'the method copies a sequence of elements within an array (negative start)', function test( t ) { + var arr; + var buf; + + arr = [ 0.05, 1.05, 2.05, 3.05, 4.05 ]; + arr = new Float16Array( arr ); + + arr.copyWithin( 0, -2 ); + buf = new Uint16Array( arr.buffer ); + + // Overwritten: + t.strictEqual( buf[ 0 ], toWord( 3.05 ), 'returns expected value' ); + t.strictEqual( buf[ 1 ], toWord( 4.05 ), 'returns expected value' ); + + // Remain the same: + t.strictEqual( buf[ 2 ], toWord( 2.05 ), 'returns expected value' ); + t.strictEqual( buf[ 3 ], toWord( 3.05 ), 'returns expected value' ); + t.strictEqual( buf[ 4 ], toWord( 4.05 ), 'returns expected value' ); + + t.end(); +}); + +tape( 'the method copies a sequence of elements within an array (end=length)', function test( t ) { + var arr; + var buf; + + arr = [ 0.05, 1.05, 2.05, 3.05, 4.05 ]; + arr = new Float16Array( arr ); + + arr.copyWithin( 0, 3, arr.length ); + buf = new Uint16Array( arr.buffer ); + + // Overwritten: + t.strictEqual( buf[ 0 ], toWord( 3.05 ), 'returns expected value' ); + t.strictEqual( buf[ 1 ], toWord( 4.05 ), 'returns expected value' ); + + // Remain the same: + t.strictEqual( buf[ 2 ], toWord( 2.05 ), 'returns expected value' ); + t.strictEqual( buf[ 3 ], toWord( 3.05 ), 'returns expected value' ); + t.strictEqual( buf[ 4 ], toWord( 4.05 ), 'returns expected value' ); + + t.end(); +}); + +tape( 'the method copies a sequence of elements within an array (non-inclusive end)', function test( t ) { + var arr; + var buf; + + arr = [ 0.05, 1.05, 2.05, 3.05, 4.05 ]; + arr = new Float16Array( arr ); + + arr.copyWithin( 2, 0, 2 ); + buf = new Uint16Array( arr.buffer ); + + // Remain the same: + t.strictEqual( buf[ 0 ], toWord( 0.05 ), 'returns expected value' ); + t.strictEqual( buf[ 1 ], toWord( 1.05 ), 'returns expected value' ); + + // Overwritten: + t.strictEqual( buf[ 2 ], toWord( 0.05 ), 'returns expected value' ); + t.strictEqual( buf[ 3 ], toWord( 1.05 ), 'returns expected value' ); + + // Remain the same: + t.strictEqual( buf[ 4 ], toWord( 4.05 ), 'returns expected value' ); + + t.end(); +}); + +tape( 'the method copies a sequence of elements within an array (negative end)', function test( t ) { + var arr; + var buf; + + arr = [ 0.05, 1.05, 2.05, 3.05, 4.05 ]; + arr = new Float16Array( arr ); + + arr.copyWithin( 2, 0, -3 ); + buf = new Uint16Array( arr.buffer ); + + // Remain the same: + t.strictEqual( buf[ 0 ], toWord( 0.05 ), 'returns expected value' ); + t.strictEqual( buf[ 1 ], toWord( 1.05 ), 'returns expected value' ); + + // Overwritten: + t.strictEqual( buf[ 2 ], toWord( 0.05 ), 'returns expected value' ); + t.strictEqual( buf[ 3 ], toWord( 1.05 ), 'returns expected value' ); + + // Remain the same: + t.strictEqual( buf[ 4 ], toWord( 4.05 ), 'returns expected value' ); + + t.end(); +}); + +tape( 'the method copies a sequence of elements within an array (target >= length)', function test( t ) { + var arr; + var buf; + + arr = [ 0.05, 1.05, 2.05, 3.05, 4.05 ]; + arr = new Float16Array( arr ); + + arr.copyWithin( arr.length, 3 ); + buf = new Uint16Array( arr.buffer ); + + // Remain the same: + t.strictEqual( buf[ 0 ], toWord( 0.05 ), 'returns expected value' ); + t.strictEqual( buf[ 1 ], toWord( 1.05 ), 'returns expected value' ); + t.strictEqual( buf[ 2 ], toWord( 2.05 ), 'returns expected value' ); + t.strictEqual( buf[ 3 ], toWord( 3.05 ), 'returns expected value' ); + t.strictEqual( buf[ 4 ], toWord( 4.05 ), 'returns expected value' ); + + t.end(); +}); + +tape( 'the method copies a sequence of elements within an array (target > start)', function test( t ) { + var arr; + var buf; + + arr = [ 0.05, 1.05, 2.05, 3.05, 4.05 ]; + arr = new Float16Array( arr ); + + arr.copyWithin( 2, 0 ); + buf = new Uint16Array( arr.buffer ); + + // Remain the same: + t.strictEqual( buf[ 0 ], toWord( 0.05 ), 'returns expected value' ); + t.strictEqual( buf[ 1 ], toWord( 1.05 ), 'returns expected value' ); + + // Overwritten: + t.strictEqual( buf[ 2 ], toWord( 0.05 ), 'returns expected value' ); + t.strictEqual( buf[ 3 ], toWord( 1.05 ), 'returns expected value' ); + t.strictEqual( buf[ 4 ], toWord( 2.05 ), 'returns expected value' ); + + t.end(); +}); diff --git a/lib/node_modules/@stdlib/array/float16/test/test.entries.js b/lib/node_modules/@stdlib/array/float16/test/test.entries.js new file mode 100644 index 000000000000..66bbf710c23c --- /dev/null +++ b/lib/node_modules/@stdlib/array/float16/test/test.entries.js @@ -0,0 +1,238 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var proxyquire = require( 'proxyquire' ); +var hasOwnProp = require( '@stdlib/assert/has-own-property' ); +var isFunction = require( '@stdlib/assert/is-function' ); +var isArray = require( '@stdlib/assert/is-array' ); +var isNumber = require( '@stdlib/assert/is-number' ); +var ITERATOR_SYMBOL = require( '@stdlib/symbol/iterator' ); +var float64ToFloat16 = require( '@stdlib/number/float64/base/to-float16' ); +var Float16Array = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof Float16Array, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'attached to the prototype of the main export is an `entries` method for returning an iterator for iterating over array key-value pairs', function test( t ) { + t.strictEqual( hasOwnProp( Float16Array.prototype, 'entries' ), true, 'has property' ); + t.strictEqual( isFunction( Float16Array.prototype.entries ), true, 'has method' ); + t.end(); +}); + +tape( 'the method throws an error if invoked with a `this` context which is not a floating-point number array instance', function test( t ) { + var values; + var arr; + var i; + + arr = new Float16Array( 5 ); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + {}, + [] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + return arr.entries.call( value ); + }; + } +}); + +tape( 'the method returns an iterator protocol-compliant object', function test( t ) { + var buf; + var arr; + var it; + var v; + var i; + + buf = [ 1.05, 2.05, 3.05, 4.05 ]; + arr = new Float16Array( buf ); + + it = arr.entries(); + t.strictEqual( it.next.length, 0, 'has zero arity' ); + + for ( i = 0; i < arr.length; i++ ) { + v = it.next(); + t.strictEqual( isArray( v.value ), true, 'returns expected value' ); + t.strictEqual( v.value[ 0 ], i, 'returns expected value' ); + t.strictEqual( isNumber( v.value[ 1 ] ), true, 'returns expected value' ); + t.strictEqual( v.value[ 1 ], float64ToFloat16( buf[ i ] ), 'returns expected value' ); + t.strictEqual( typeof v.done, 'boolean', 'returns expected value' ); + } + t.end(); +}); + +tape( 'the method returns an iterator which has a `return` method for closing an iterator (no argument)', function test( t ) { + var buf; + var arr; + var it; + var v; + + buf = [ 1.05, 2.05, 3.05, 4.05 ]; + arr = new Float16Array( buf ); + + it = arr.entries(); + + v = it.next(); + t.strictEqual( v.value[ 1 ], float64ToFloat16( buf[ 0 ] ), 'returns expected value' ); + t.strictEqual( v.done, false, 'returns expected value' ); + + v = it.next(); + t.strictEqual( v.value[ 1 ], float64ToFloat16( buf[ 1 ] ), 'returns expected value' ); + t.strictEqual( v.done, false, 'returns expected value' ); + + v = it.return(); + t.strictEqual( v.value, void 0, 'returns expected value' ); + t.strictEqual( v.done, true, 'returns expected value' ); + + v = it.next(); + t.strictEqual( v.value, void 0, 'returns expected value' ); + t.strictEqual( v.done, true, 'returns expected value' ); + + v = it.next(); + t.strictEqual( v.value, void 0, 'returns expected value' ); + t.strictEqual( v.done, true, 'returns expected value' ); + + v = it.next(); + t.strictEqual( v.value, void 0, 'returns expected value' ); + t.strictEqual( v.done, true, 'returns expected value' ); + + v = it.next(); + t.strictEqual( v.value, void 0, 'returns expected value' ); + t.strictEqual( v.done, true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the method returns an iterator which has a `return` method for closing an iterator (argument)', function test( t ) { + var buf; + var arr; + var it; + var v; + + buf = [ 1.05, 2.05, 3.05, 4.05 ]; + arr = new Float16Array( buf ); + + it = arr.entries(); + + v = it.next(); + t.strictEqual( v.value[ 1 ], float64ToFloat16( buf[ 0 ] ), 'returns expected value' ); + t.strictEqual( v.done, false, 'returns expected value' ); + + v = it.next(); + t.strictEqual( v.value[ 1 ], float64ToFloat16( buf[ 1 ] ), 'returns expected value' ); + t.strictEqual( v.done, false, 'returns expected value' ); + + v = it.return( 'finished' ); + t.strictEqual( v.value, 'finished', 'returns expected value' ); + t.strictEqual( v.done, true, 'returns expected value' ); + + v = it.next(); + t.strictEqual( v.value, void 0, 'returns expected value' ); + t.strictEqual( v.done, true, 'returns expected value' ); + + v = it.next(); + t.strictEqual( v.value, void 0, 'returns expected value' ); + t.strictEqual( v.done, true, 'returns expected value' ); + + v = it.next(); + t.strictEqual( v.value, void 0, 'returns expected value' ); + t.strictEqual( v.done, true, 'returns expected value' ); + + v = it.next(); + t.strictEqual( v.value, void 0, 'returns expected value' ); + t.strictEqual( v.done, true, 'returns expected value' ); + + t.end(); +}); + +tape( 'if an environment supports `Symbol.iterator`, the method returns an iterable', function test( t ) { + var Float16Array; + var arr; + var buf; + var it1; + var it2; + var v1; + var v2; + var i; + + Float16Array = proxyquire( './../lib/main.js', { + '@stdlib/symbol/iterator': '__ITERATOR_SYMBOL__' + }); + + buf = [ 1.05, 2.05, 3.05, 4.05 ]; + arr = new Float16Array( buf ); + + it1 = arr.entries(); + t.strictEqual( typeof it1[ '__ITERATOR_SYMBOL__' ], 'function', 'has method' ); + t.strictEqual( it1[ '__ITERATOR_SYMBOL__' ].length, 0, 'has zero arity' ); + + it2 = it1[ '__ITERATOR_SYMBOL__' ](); + t.strictEqual( typeof it2, 'object', 'returns expected value' ); + t.strictEqual( typeof it2.next, 'function', 'has `next` method' ); + t.strictEqual( typeof it2.return, 'function', 'has `return` method' ); + + for ( i = 0; i < arr.length; i++ ) { + v1 = it1.next().value; + v2 = it2.next().value; + t.strictEqual( v1[ 0 ], v2[ 0 ], 'returns expected value' ); + t.strictEqual( v1[ 1 ], v2[ 1 ], 'returns expected value' ); + } + t.end(); +}); + +tape( 'if an environment does not support `Symbol.iterator`, the method does not return an "iterable"', function test( t ) { + var Float16Array; + var arr; + var buf; + var it; + + Float16Array = proxyquire( './../lib/main.js', { + '@stdlib/symbol/iterator': false + }); + + buf = [ 1.05, 2.05, 3.05, 4.05 ]; + arr = new Float16Array( buf ); + + it = arr.entries(); + t.strictEqual( it[ ITERATOR_SYMBOL ], void 0, 'does not have property' ); + + t.end(); +}); diff --git a/lib/node_modules/@stdlib/array/float16/test/test.every.js b/lib/node_modules/@stdlib/array/float16/test/test.every.js new file mode 100644 index 000000000000..28b1c75d93cf --- /dev/null +++ b/lib/node_modules/@stdlib/array/float16/test/test.every.js @@ -0,0 +1,174 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var hasOwnProp = require( '@stdlib/assert/has-own-property' ); +var isFunction = require( '@stdlib/assert/is-function' ); +var float64ToFloat16 = require( '@stdlib/number/float64/base/to-float16' ); +var Float16Array = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof Float16Array, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'attached to the prototype of the main export is an `every` method for returning boolean indicating whether all elements pass a test', function test( t ) { + t.strictEqual( hasOwnProp( Float16Array.prototype, 'every' ), true, 'has property' ); + t.strictEqual( isFunction( Float16Array.prototype.every ), true, 'has method' ); + t.end(); +}); + +tape( 'the method throws an error if invoked with a `this` context which is not a floating-point number array instance', function test( t ) { + var values; + var arr; + var i; + + arr = new Float16Array( 5 ); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + {}, + [], + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + return arr.every.call( value, predicate ); + }; + } + + function predicate( v ) { + return v === 0.0; + } +}); + +tape( 'the method throws an error if provided a first argument which is not a function', function test( t ) { + var values; + var arr; + var i; + + arr = new Float16Array( 10 ); + + values = [ + '5', + 3.14, + NaN, + true, + false, + null, + void 0, + {}, + [] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + return arr.every( value ); + }; + } +}); + +tape( 'the method returns `true` if operating on an empty floating-point number array', function test( t ) { + var bool; + var arr; + + arr = new Float16Array(); + bool = arr.every( predicate ); + + t.strictEqual( bool, true, 'returns expected value' ); + t.end(); + + function predicate() { + t.fail( 'should not be invoked' ); + } +}); + +tape( 'the method returns `true` if all elements pass a test', function test( t ) { + var bool; + var arr; + + arr = new Float16Array( [ 1.05, 1.05, 1.05, 1.05 ] ); + bool = arr.every( predicate ); + + t.strictEqual( bool, true, 'returns expected value' ); + t.end(); + + function predicate( v ) { + return v === float64ToFloat16( 1.05 ); + } +}); + +tape( 'the method returns `false` if one or more elements fail a test', function test( t ) { + var bool; + var arr; + + arr = new Float16Array( [ 1.05, 1.05, 0.05, 1.05 ] ); + bool = arr.every( predicate ); + + t.strictEqual( bool, false, 'returns expected value' ); + t.end(); + + function predicate( v ) { + return v === float64ToFloat16( 1.05 ); + } +}); + +tape( 'the method supports providing an execution context', function test( t ) { + var bool; + var ctx; + var arr; + + ctx = { + 'count': 0 + }; + arr = new Float16Array( [ 1.05, 1.05, 1.05, 1.05 ] ); + bool = arr.every( predicate, ctx ); + + t.strictEqual( bool, true, 'returns expected value' ); + t.strictEqual( ctx.count, 4, 'returns expected value' ); + + t.end(); + + function predicate( v ) { + this.count += 1; // eslint-disable-line no-invalid-this + return v === float64ToFloat16( 1.05 ); + } +}); diff --git a/lib/node_modules/@stdlib/array/float16/test/test.fill.js b/lib/node_modules/@stdlib/array/float16/test/test.fill.js new file mode 100644 index 000000000000..dcc0b2afd26c --- /dev/null +++ b/lib/node_modules/@stdlib/array/float16/test/test.fill.js @@ -0,0 +1,305 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var hasOwnProp = require( '@stdlib/assert/has-own-property' ); +var isFunction = require( '@stdlib/assert/is-function' ); +var Uint16Array = require( '@stdlib/array/uint16' ); +var toWord = require( '@stdlib/number/float16/base/to-word' ); +var Float16Array = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof Float16Array, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'attached to the prototype of the main export is a `fill` method', function test( t ) { + t.strictEqual( hasOwnProp( Float16Array.prototype, 'fill' ), true, 'has property' ); + t.strictEqual( isFunction( Float16Array.prototype.fill ), true, 'has method' ); + t.end(); +}); + +tape( 'the method throws an error if invoked with a `this` context which is not a floating-point number array instance', function test( t ) { + var values; + var arr; + var i; + + arr = new Float16Array( 5 ); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + {}, + [], + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + return arr.fill.call( value, 1.0 ); + }; + } +}); + +tape( 'the method throws an error if provided a first argument which is not a floating-point number', function test( t ) { + var values; + var arr; + var i; + + arr = new Float16Array( 5 ); + + values = [ + '5', + true, + false, + null, + void 0, + {}, + [], + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + return arr.fill( value ); + }; + } +}); + +tape( 'the method throws an error if provided a second argument which is not an integer', function test( t ) { + var values; + var arr; + var i; + + arr = new Float16Array( 10 ); + + values = [ + '5', + 3.14, + NaN, + true, + false, + null, + void 0, + {}, + [], + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + return arr.fill( 1.0, value ); + }; + } +}); + +tape( 'the method throws an error if provided a third argument which is not an integer', function test( t ) { + var values; + var arr; + var i; + + arr = new Float16Array( 10 ); + + values = [ + '5', + 3.14, + NaN, + true, + false, + null, + void 0, + {}, + [], + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + return arr.fill( 1.0, 0, value ); + }; + } +}); + +tape( 'the method returns an empty array if operating on an empty floating-point number array', function test( t ) { + var arr; + var out; + + arr = new Float16Array(); + out = arr.fill( 1.05 ); + + t.strictEqual( out instanceof Float16Array, true, 'returns expected value' ); + t.strictEqual( out.length, 0, 'returns expected value' ); + t.end(); +}); + +tape( 'the method does not change the array length', function test( t ) { + var arr; + var out; + + arr = new Float16Array( 10 ); + out = arr.fill( 1.05 ); + + t.strictEqual( out, arr, 'returns expected value' ); + t.strictEqual( arr.length, 10, 'returns expected value' ); + t.end(); +}); + +tape( 'if called with one argument, the method sets each array element to the provided value', function test( t ) { + var arr; + var buf; + var i; + + arr = new Float16Array( 3 ); + arr.fill( 1.05 ); + + buf = new Uint16Array( arr.buffer ); + + for ( i = 0; i < arr.length; i++ ) { + t.strictEqual( buf[ i ], toWord( 1.05 ), 'returns expected value' ); + } + + t.end(); +}); + +tape( 'if called with two arguments, the method sets each array element to the provided value starting from a start index (inclusive)', function test( t ) { + var arr; + var buf; + + arr = new Float16Array( 3 ); + buf = new Uint16Array( arr.buffer ); + + arr.fill( 1.05, 1 ); + + t.strictEqual( buf[ 0 ], toWord( 0.0 ), 'returns expected value' ); + t.strictEqual( buf[ 1 ], toWord( 1.05 ), 'returns expected value' ); + t.strictEqual( buf[ 2 ], toWord( 1.05 ), 'returns expected value' ); + + t.end(); +}); + +tape( 'if called with three arguments, the method sets each array element to the provided value starting from a start index (inclusive) until a specified end index (exclusive)', function test( t ) { + var arr; + var buf; + + arr = new Float16Array( 3 ); + buf = new Uint16Array( arr.buffer ); + + arr.fill( 1.05, 0, 2 ); + + t.strictEqual( buf[ 0 ], toWord( 1.05 ), 'returns expected value' ); + t.strictEqual( buf[ 1 ], toWord( 1.05 ), 'returns expected value' ); + t.strictEqual( buf[ 2 ], toWord( 0.0 ), 'returns expected value' ); + + t.end(); +}); + +tape( 'the method supports negative indices', function test( t ) { + var arr; + var buf; + + arr = new Float16Array( 3 ); + buf = new Uint16Array( arr.buffer ); + + arr.fill( 1.05, -3, -1 ); + + t.strictEqual( buf[ 0 ], toWord( 1.05 ), 'returns expected value' ); + t.strictEqual( buf[ 1 ], toWord( 1.05 ), 'returns expected value' ); + t.strictEqual( buf[ 2 ], toWord( 0.0 ), 'returns expected value' ); + + t.end(); +}); + +tape( 'if a provided start index resolves to a negative index, the method fills an array starting from the first element', function test( t ) { + var arr; + var buf; + var i; + + arr = new Float16Array( 3 ); + buf = new Uint16Array( arr.buffer ); + + arr.fill( 1.05, -10 ); + + for ( i = 0; i < arr.length; i++ ) { + t.strictEqual( buf[ i ], toWord( 1.05 ), 'returns expected value' ); + } + + t.end(); +}); + +tape( 'if a provided end index resolves to an index exceeding the last array element index, the method fills an array until the last element (inclusive)', function test( t ) { + var arr; + var buf; + var i; + + arr = new Float16Array( 3 ); + buf = new Uint16Array( arr.buffer ); + + arr.fill( 1.05, 1, 10 ); + + t.strictEqual( buf[ 0 ], toWord( 0.0 ), 'returns expected value' ); + for ( i = 1; i < arr.length; i++ ) { + t.strictEqual( buf[ i ], toWord( 1.05 ), 'returns expected value' ); + } + + t.end(); +}); + +tape( 'if a provided start index resolves to an index which is greater than or equal to a resolved end index, the method does not fill an array', function test( t ) { + var arr; + var buf; + + arr = new Float16Array( 3 ); + buf = new Uint16Array( arr.buffer ); + + arr.fill( 1.0, 2, 1 ); + + t.strictEqual( buf[ 0 ], toWord( 0.0 ), 'returns expected value' ); + t.strictEqual( buf[ 1 ], toWord( 0.0 ), 'returns expected value' ); + t.strictEqual( buf[ 2 ], toWord( 0.0 ), 'returns expected value' ); + + t.end(); +}); diff --git a/lib/node_modules/@stdlib/array/float16/test/test.filter.js b/lib/node_modules/@stdlib/array/float16/test/test.filter.js new file mode 100644 index 000000000000..19eeb23beec5 --- /dev/null +++ b/lib/node_modules/@stdlib/array/float16/test/test.filter.js @@ -0,0 +1,226 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var hasOwnProp = require( '@stdlib/assert/has-own-property' ); +var isFunction = require( '@stdlib/assert/is-function' ); +var instanceOf = require( '@stdlib/assert/instance-of' ); +var float64ToFloat16 = require( '@stdlib/number/float64/base/to-float16' ); +var toWord = require( '@stdlib/number/float16/base/to-word' ); +var Uint16Array = require( '@stdlib/array/uint16' ); +var Float16Array = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof Float16Array, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'attached to the prototype of the main export is a `filter` method', function test( t ) { + t.strictEqual( hasOwnProp( Float16Array.prototype, 'filter' ), true, 'has property' ); + t.strictEqual( isFunction( Float16Array.prototype.filter ), true, 'has method' ); + t.end(); +}); + +tape( 'the method throws an error if invoked with a `this` context which is not a floating-point number array instance', function test( t ) { + var values; + var arr; + var i; + + arr = new Float16Array( 5 ); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + {}, + [], + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + return arr.filter.call( value, predicate ); + }; + } + + function predicate( v ) { + return ( v === 1.0 ); + } +}); + +tape( 'the method throws an error if provided a first argument which is not a function', function test( t ) { + var values; + var arr; + var i; + + arr = new Float16Array( 10 ); + + values = [ + '5', + 3.14, + NaN, + true, + false, + null, + void 0, + {}, + [] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + return arr.filter( value ); + }; + } +}); + +tape( 'the method returns an empty array if operating on an empty floating-point number array', function test( t ) { + var arr; + var out; + + arr = new Float16Array(); + out = arr.filter( predicate ); + + t.strictEqual( out.length, 0, 'returns expected value' ); + t.end(); + + function predicate( v ) { + return ( v === 1.0 ); + } +}); + +tape( 'the method returns a new floating-point number array containing only those elements which satisfy a test condition', function test( t ) { + var expected; + var actual; + var arr; + var buf; + + arr = new Float16Array( [ 1.05, 1.05, 2.05 ] ); + expected = new Uint16Array( [ toWord( 1.05 ), toWord( 1.05 ) ] ); + actual = arr.filter( predicate ); + buf = new Uint16Array( actual.buffer ); + + t.strictEqual( instanceOf( actual, Float16Array ), true, 'returns expected value' ); + t.strictEqual( actual.length, expected.length, 'returns expected value' ); + t.deepEqual( buf, expected, 'returns expected value' ); + t.end(); + + function predicate( v ) { + return ( v === float64ToFloat16( 1.05 ) ); + } +}); + +tape( 'the method copies all elements to a new array if all elements satisfy a test condition', function test( t ) { + var expected; + var actual; + var arr; + var buf; + + arr = new Float16Array( [ 1.05, 2.05, 3.05, 4.05 ] ); + expected = new Uint16Array([ + toWord( 1.05 ), + toWord( 2.05 ), + toWord( 3.05 ), + toWord( 4.05 ) + ]); + actual = arr.filter( predicate ); + buf = new Uint16Array( actual.buffer ); + + t.strictEqual( instanceOf( actual, Float16Array ), true, 'returns expected value' ); + t.notEqual( actual, arr, 'returns a new instance' ); + t.strictEqual( actual.length, expected.length, 'returns expected value' ); + t.deepEqual( buf, expected, 'returns expected value' ); + t.end(); + + function predicate( v ) { + return ( v > 1.0 ); + } +}); + +tape( 'the method returns an empty array if no elements satisfy a test condition', function test( t ) { + var expected; + var actual; + var arr; + var buf; + + arr = new Float16Array( [ 1.05, 2.05, 3.05, 4.05 ] ); + expected = new Uint16Array( [] ); + actual = arr.filter( predicate ); + buf = new Uint16Array( actual.buffer ); + + t.strictEqual( instanceOf( actual, Float16Array ), true, 'returns expected value' ); + t.notEqual( actual, arr, 'returns a new instance' ); + t.strictEqual( actual.length, expected.length, 'returns expected value' ); + t.deepEqual( buf, expected, 'returns expected value' ); + t.end(); + + function predicate( v ) { + return ( v < 1.0 ); + } +}); + +tape( 'the method supports providing an execution context', function test( t ) { + var expected; + var actual; + var arr; + var buf; + var ctx; + + arr = new Float16Array( [ 1.05, 2.05, 1.05, 1.05 ] ); + expected = new Uint16Array([ + toWord( 1.05 ), + toWord( 1.05 ), + toWord( 1.05 ) + ]); + ctx = { + 'count': 0 + }; + actual = arr.filter( predicate, ctx ); + buf = new Uint16Array( actual.buffer ); + + t.strictEqual( instanceOf( actual, Float16Array ), true, 'returns expected value' ); + t.strictEqual( actual.length, expected.length, 'returns expected value' ); + t.deepEqual( buf, expected, 'returns expected value' ); + t.strictEqual( ctx.count, 4, 'returns expected value' ); + t.end(); + + function predicate( v ) { + this.count += 1; // eslint-disable-line no-invalid-this + return ( v === float64ToFloat16( 1.05 ) ); + } +}); diff --git a/lib/node_modules/@stdlib/array/float16/test/test.find.js b/lib/node_modules/@stdlib/array/float16/test/test.find.js new file mode 100644 index 000000000000..2c7f2c83ee16 --- /dev/null +++ b/lib/node_modules/@stdlib/array/float16/test/test.find.js @@ -0,0 +1,174 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var hasOwnProp = require( '@stdlib/assert/has-own-property' ); +var isFunction = require( '@stdlib/assert/is-function' ); +var float64ToFloat16 = require( '@stdlib/number/float64/base/to-float16' ); +var Float16Array = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof Float16Array, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'attached to the prototype of the main export is a `find` method', function test( t ) { + t.strictEqual( hasOwnProp( Float16Array.prototype, 'find' ), true, 'has property' ); + t.strictEqual( isFunction( Float16Array.prototype.find ), true, 'has method' ); + t.end(); +}); + +tape( 'the method throws an error if invoked with a `this` context which is not a floating-point number array instance', function test( t ) { + var values; + var arr; + var i; + + arr = new Float16Array( 5 ); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + {}, + [], + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + return arr.find.call( value, predicate ); + }; + } + + function predicate( v ) { + return ( v === 1.0 ); + } +}); + +tape( 'the method throws an error if provided a first argument which is not a function', function test( t ) { + var values; + var arr; + var i; + + arr = new Float16Array( 10 ); + + values = [ + '5', + 3.14, + NaN, + true, + false, + null, + void 0, + {}, + [] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + return arr.find( value ); + }; + } +}); + +tape( 'the method returns `undefined` if operating on an empty floating-point number array', function test( t ) { + var arr; + var v; + + arr = new Float16Array(); + v = arr.find( predicate ); + + t.strictEqual( v, void 0, 'returns expected value' ); + t.end(); + + function predicate() { + t.fail( 'should not be invoked' ); + } +}); + +tape( 'the method returns the first element which passes a test', function test( t ) { + var arr; + var v; + + arr = new Float16Array( [ 1.05, -2.05, 3.05, -4.05 ] ); + v = arr.find( predicate ); + + t.strictEqual( v, float64ToFloat16( -2.05 ), 'returns expected value' ); + t.end(); + + function predicate( v ) { + return ( v < 0.0 ); + } +}); + +tape( 'the method returns `undefined` if all elements fail a test', function test( t ) { + var arr; + var v; + + arr = new Float16Array( [ 1.05, 2.05, 3.05, 4.05 ] ); + v = arr.find( predicate ); + + t.strictEqual( v, void 0, 'returns expected value' ); + t.end(); + + function predicate( v ) { + return ( v < 0.0 ); + } +}); + +tape( 'the method supports providing an execution context', function test( t ) { + var ctx; + var arr; + var v; + + ctx = { + 'count': 0 + }; + arr = new Float16Array( [ -1.05, -2.05, 3.05, 4.05, 5.05 ] ); + v = arr.find( predicate, ctx ); + + t.strictEqual( v, float64ToFloat16( 3.05 ), 'returns expected value' ); + t.strictEqual( ctx.count, 3, 'returns expected value' ); + + t.end(); + + function predicate( v ) { + this.count += 1; // eslint-disable-line no-invalid-this + return ( v > 0.0 ); + } +}); diff --git a/lib/node_modules/@stdlib/array/float16/test/test.find_index.js b/lib/node_modules/@stdlib/array/float16/test/test.find_index.js new file mode 100644 index 000000000000..28550a24e57b --- /dev/null +++ b/lib/node_modules/@stdlib/array/float16/test/test.find_index.js @@ -0,0 +1,173 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var hasOwnProp = require( '@stdlib/assert/has-own-property' ); +var isFunction = require( '@stdlib/assert/is-function' ); +var Float16Array = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof Float16Array, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'attached to the prototype of the main export is a `findIndex` method', function test( t ) { + t.strictEqual( hasOwnProp( Float16Array.prototype, 'findIndex' ), true, 'has property' ); + t.strictEqual( isFunction( Float16Array.prototype.findIndex ), true, 'has method' ); + t.end(); +}); + +tape( 'the method throws an error if invoked with a `this` context which is not a floating-point number array instance', function test( t ) { + var values; + var arr; + var i; + + arr = new Float16Array( 5 ); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + {}, + [], + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + return arr.findIndex.call( value, predicate ); + }; + } + + function predicate( v ) { + return ( v === 1.0 ); + } +}); + +tape( 'the method throws an error if provided a first argument which is not a function', function test( t ) { + var values; + var arr; + var i; + + arr = new Float16Array( 10 ); + + values = [ + '5', + 3.14, + NaN, + true, + false, + null, + void 0, + {}, + [] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + return arr.findIndex( value ); + }; + } +}); + +tape( 'the method returns `-1` if operating on an empty floating-point number array', function test( t ) { + var arr; + var idx; + + arr = new Float16Array(); + idx = arr.findIndex( predicate ); + + t.strictEqual( idx, -1, 'returns expected value' ); + t.end(); + + function predicate() { + t.fail( 'should not be invoked' ); + } +}); + +tape( 'the method returns the index of the first element which passes a test', function test( t ) { + var arr; + var idx; + + arr = new Float16Array( [ 1.05, -2.05, 3.05, -4.05 ] ); + idx = arr.findIndex( predicate ); + + t.strictEqual( idx, 1, 'returns expected value' ); + t.end(); + + function predicate( v ) { + return ( v < 0.0); + } +}); + +tape( 'the method returns `-1` if all elements fail a test', function test( t ) { + var arr; + var idx; + + arr = new Float16Array( [ 1.05, 2.05, 3.05, 4.05 ] ); + idx = arr.findIndex( predicate ); + + t.strictEqual( idx, -1, 'returns expected value' ); + t.end(); + + function predicate( v ) { + return ( v < 0.0 ); + } +}); + +tape( 'the method supports providing an execution context', function test( t ) { + var ctx; + var arr; + var idx; + + ctx = { + 'count': 0 + }; + arr = new Float16Array( [ -1.05, -2.05, 3.05, 4.05, 5.05 ] ); + idx = arr.findIndex( predicate, ctx ); + + t.strictEqual( idx, 2, 'returns expected value' ); + t.strictEqual( ctx.count, 3, 'returns expected value' ); + + t.end(); + + function predicate( v ) { + this.count += 1; // eslint-disable-line no-invalid-this + return ( v > 0.0 ); + } +}); diff --git a/lib/node_modules/@stdlib/array/float16/test/test.find_last.js b/lib/node_modules/@stdlib/array/float16/test/test.find_last.js new file mode 100644 index 000000000000..58ca2946d895 --- /dev/null +++ b/lib/node_modules/@stdlib/array/float16/test/test.find_last.js @@ -0,0 +1,174 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var hasOwnProp = require( '@stdlib/assert/has-own-property' ); +var isFunction = require( '@stdlib/assert/is-function' ); +var float64ToFloat16 = require( '@stdlib/number/float64/base/to-float16' ); +var Float16Array = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof Float16Array, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'attached to the prototype of the main export is a `findLast` method', function test( t ) { + t.strictEqual( hasOwnProp( Float16Array.prototype, 'findLast' ), true, 'has property' ); + t.strictEqual( isFunction( Float16Array.prototype.findLast ), true, 'has method' ); + t.end(); +}); + +tape( 'the method throws an error if invoked with a `this` context which is not a floating-point number instance', function test( t ) { + var values; + var arr; + var i; + + arr = new Float16Array( 5 ); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + {}, + [], + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + return arr.findLast.call( value, predicate ); + }; + } + + function predicate( v ) { + return ( v === 0.0 ); + } +}); + +tape( 'the method throws an error if provided a first argument which is not a function', function test( t ) { + var values; + var arr; + var i; + + arr = new Float16Array( 10 ); + + values = [ + '5', + 3.14, + NaN, + true, + false, + null, + void 0, + {}, + [] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + return arr.findLast( value ); + }; + } +}); + +tape( 'the method returns `undefined` if operating on an empty floating-point number', function test( t ) { + var arr; + var v; + + arr = new Float16Array(); + v = arr.findLast( predicate ); + + t.strictEqual( v, void 0, 'returns expected value' ); + t.end(); + + function predicate() { + t.fail( 'should not be invoked' ); + } +}); + +tape( 'the method returns the last element which passes a test', function test( t ) { + var arr; + var v; + + arr = new Float16Array( [ 1.05, -2.05, 3.05, -4.05 ] ); + v = arr.findLast( predicate ); + + t.strictEqual( v, float64ToFloat16( -4.05 ), 'returns expected value' ); + t.end(); + + function predicate( v ) { + return ( v < 0.0 ); + } +}); + +tape( 'the method returns `undefined` if all elements fail a test', function test( t ) { + var arr; + var v; + + arr = new Float16Array( [ 1.05, 2.05, 3.05, 4.05 ] ); + v = arr.findLast( predicate ); + + t.strictEqual( v, void 0, 'returns expected value' ); + t.end(); + + function predicate( v ) { + return ( v < 0.0 ); + } +}); + +tape( 'the method supports providing an execution context', function test( t ) { + var ctx; + var arr; + var v; + + ctx = { + 'count': 0 + }; + arr = new Float16Array( [ -1.05, -2.05, 3.05, 4.05, 5.05 ] ); + v = arr.findLast( predicate, ctx ); + + t.strictEqual( v, float64ToFloat16( 5.05 ), 'returns expected value' ); + t.strictEqual( ctx.count, 1, 'returns expected value' ); + + t.end(); + + function predicate( v ) { + this.count += 1; // eslint-disable-line no-invalid-this + return ( v > 0.0 ); + } +}); diff --git a/lib/node_modules/@stdlib/array/float16/test/test.find_last_index.js b/lib/node_modules/@stdlib/array/float16/test/test.find_last_index.js new file mode 100644 index 000000000000..9757a79ee354 --- /dev/null +++ b/lib/node_modules/@stdlib/array/float16/test/test.find_last_index.js @@ -0,0 +1,173 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var hasOwnProp = require( '@stdlib/assert/has-own-property' ); +var isFunction = require( '@stdlib/assert/is-function' ); +var Float16Array = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof Float16Array, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'attached to the prototype of the main export is a `findLastIndex` method', function test( t ) { + t.strictEqual( hasOwnProp( Float16Array.prototype, 'findLastIndex' ), true, 'has property' ); + t.strictEqual( isFunction( Float16Array.prototype.findLastIndex ), true, 'has method' ); + t.end(); +}); + +tape( 'the method throws an error if invoked with a `this` context which is not a floating-point number instance', function test( t ) { + var values; + var arr; + var i; + + arr = new Float16Array( 5 ); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + {}, + [], + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + return arr.findLastIndex.call( value, predicate ); + }; + } + + function predicate( v ) { + return ( v === 1.0 ); + } +}); + +tape( 'the method throws an error if provided a first argument which is not a function', function test( t ) { + var values; + var arr; + var i; + + arr = new Float16Array( 10 ); + + values = [ + '5', + 3.14, + NaN, + true, + false, + null, + void 0, + {}, + [] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + return arr.findLastIndex( value ); + }; + } +}); + +tape( 'the method returns `-1` if operating on an empty floating-point number', function test( t ) { + var arr; + var idx; + + arr = new Float16Array(); + idx = arr.findLastIndex( predicate ); + + t.strictEqual( idx, -1, 'returns expected value' ); + t.end(); + + function predicate() { + t.fail( 'should not be invoked' ); + } +}); + +tape( 'the method returns the index of the last element which passes a test', function test( t ) { + var arr; + var idx; + + arr = new Float16Array( [ 1.05, -2.05, 3.05, -4.05 ] ); + idx = arr.findLastIndex( predicate ); + + t.strictEqual( idx, 3, 'returns expected value' ); + t.end(); + + function predicate( v ) { + return ( v < 0.0 ); + } +}); + +tape( 'the method returns `-1` if all elements fail a test', function test( t ) { + var arr; + var idx; + + arr = new Float16Array( [ 1.05, 2.05, 3.05, 4.05 ] ); + idx = arr.findLastIndex( predicate ); + + t.strictEqual( idx, -1, 'returns expected value' ); + t.end(); + + function predicate( v ) { + return ( v < 0.0 ); + } +}); + +tape( 'the method supports providing an execution context', function test( t ) { + var ctx; + var arr; + var idx; + + ctx = { + 'count': 0 + }; + arr = new Float16Array( [ -1.05, -2.05, 3.05, 4.05, 5.05 ] ); + idx = arr.findLastIndex( predicate, ctx ); + + t.strictEqual( idx, 4, 'returns expected value' ); + t.strictEqual( ctx.count, 1, 'returns expected value' ); + + t.end(); + + function predicate( v ) { + this.count += 1; // eslint-disable-line no-invalid-this + return ( v > 0.0 ); + } +}); diff --git a/lib/node_modules/@stdlib/array/float16/test/test.for_each.js b/lib/node_modules/@stdlib/array/float16/test/test.for_each.js new file mode 100644 index 000000000000..54ccbf3a587d --- /dev/null +++ b/lib/node_modules/@stdlib/array/float16/test/test.for_each.js @@ -0,0 +1,196 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var hasOwnProp = require( '@stdlib/assert/has-own-property' ); +var isFunction = require( '@stdlib/assert/is-function' ); +var isNumber = require( '@stdlib/assert/is-number' ).isPrimitive; +var float64ToFloat16 = require( '@stdlib/number/float64/base/to-float16' ); +var Float16Array = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof Float16Array, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'attached to the prototype of the main export is a `forEach` method', function test( t ) { + t.strictEqual( hasOwnProp( Float16Array.prototype, 'forEach' ), true, 'has property' ); + t.strictEqual( isFunction( Float16Array.prototype.forEach ), true, 'has method' ); + t.end(); +}); + +tape( 'the method throws an error if invoked with a `this` context which is not a floating-point number instance', function test( t ) { + var values; + var arr; + var i; + + arr = new Float16Array( 5 ); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + {}, + [], + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + return arr.forEach.call( value, fcn ); + }; + } + + function fcn( v ) { + if ( !isNumber( v ) ) { + t.fail( 'should be a number' ); + } + } +}); + +tape( 'the method throws an error if provided a first argument which is not a function', function test( t ) { + var values; + var arr; + var i; + + arr = new Float16Array( 10 ); + + values = [ + '5', + 3.14, + NaN, + true, + false, + null, + void 0, + {}, + [] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + return arr.forEach( value ); + }; + } +}); + +tape( 'the method should not invoke a provided callback function if operating on an empty floating-point number', function test( t ) { + var arr; + + arr = new Float16Array(); + arr.forEach( fcn ); + + t.end(); + + function fcn() { + t.fail( 'should not be invoked' ); + } +}); + +tape( 'the method returns `undefined`', function test( t ) { + var arr; + var out; + + arr = new Float16Array( [ 1.05, 2.05, 3.05, 4.05 ] ); + out = arr.forEach( fcn ); + + t.strictEqual( out, void 0, 'returns expected value' ); + t.end(); + + function fcn( v ) { + if ( !isNumber( v ) ) { + t.fail( 'should be a number' ); + } + } +}); + +tape( 'the method invokes a provided function for each element in an array', function test( t ) { + var expected; + var indices; + var values; + var arrays; + var arr; + + indices = []; + values = []; + arrays = []; + + arr = new Float16Array( [ 1.05, 2.05, 3.05, 4.05 ] ); + arr.forEach( fcn ); + + expected = [ + float64ToFloat16( 1.05 ), + float64ToFloat16( 2.05 ), + float64ToFloat16( 3.05 ), + float64ToFloat16( 4.05 ) + ]; + + t.deepEqual( values, expected, 'returns expected value' ); + t.deepEqual( indices, [ 0, 1, 2, 3 ], 'returns expected value' ); + t.strictEqual( arrays[ 0 ], arr, 'returns expected value' ); + t.strictEqual( arrays[ 1 ], arr, 'returns expected value' ); + t.strictEqual( arrays[ 2 ], arr, 'returns expected value' ); + t.strictEqual( arrays[ 3 ], arr, 'returns expected value' ); + + t.end(); + + function fcn( v, i, arr ) { + values.push( v ); + indices.push( i ); + arrays.push( arr ); + } +}); + +tape( 'the method supports providing an execution context', function test( t ) { + var ctx; + var arr; + + ctx = { + 'count': 0 + }; + arr = new Float16Array( [ 1.05, 2.05, 3.05, 4.05 ] ); + arr.forEach( fcn, ctx ); + + t.strictEqual( ctx.count, 4, 'returns expected value' ); + + t.end(); + + function fcn() { + this.count += 1; // eslint-disable-line no-invalid-this + } +}); diff --git a/lib/node_modules/@stdlib/array/float16/test/test.from.js b/lib/node_modules/@stdlib/array/float16/test/test.from.js new file mode 100644 index 000000000000..abe2cbee184e --- /dev/null +++ b/lib/node_modules/@stdlib/array/float16/test/test.from.js @@ -0,0 +1,654 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var proxyquire = require( 'proxyquire' ); +var Uint16Array = require( '@stdlib/array/uint16' ); +var hasOwnProp = require( '@stdlib/assert/has-own-property' ); +var isFunction = require( '@stdlib/assert/is-function' ); +var Float16Array = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof Float16Array, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'attached to the main export is a `from` method for creating a 16-bit floating-point number array from an array-like object or an iterable.', function test( t ) { + var arr; + + t.strictEqual( hasOwnProp( Float16Array, 'from' ), true, 'has property' ); + t.strictEqual( isFunction( Float16Array.from ), true, 'has method' ); + + arr = Float16Array.from( [] ); + t.strictEqual( arr instanceof Float16Array, true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the method throws an error if invoked with a `this` context which is not a constructor', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + {}, + [] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + return Float16Array.from.call( value, [] ); + }; + } +}); + +tape( 'the method throws an error if invoked with a `this` context which is not a floating-point number array constructor', function test( t ) { + var values; + var i; + + values = [ + {}, + null, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + return Float16Array.from.call( value, [] ); + }; + } +}); + +tape( 'the method throws an error if not provided an iterable or array-like object', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + return Float16Array.from( value ); + }; + } +}); + +tape( 'the method throws an error if not provided an iterable or array-like object (callback)', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + return Float16Array.from( value, clbk ); + }; + } + + function clbk() { + return 1.0; + } +}); + +tape( 'the method throws an error if not provided an iterable or array-like object (callback, thisArg)', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + return Float16Array.from( value, clbk, {} ); + }; + } + + function clbk() { + return 1.0; + } +}); + +tape( 'the method throws an error if provided a second argument which is not a function', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + {}, + [] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + return Float16Array.from( [], value ); + }; + } +}); + +tape( 'the method throws an error if provided a second argument which is not a function (thisArg)', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + {}, + [] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + return Float16Array.from( [], value, {} ); + }; + } +}); + +tape( 'the method returns a floating-point number array', function test( t ) { + var arr; + var v; + + // Generic array: + arr = Float16Array.from( [] ); + t.strictEqual( arr instanceof Float16Array, true, 'returns expected value' ); + + v = arr.length; + t.strictEqual( v, 0, 'returns expected value' ); + + arr = Float16Array.from( [ 1.0, 2.0, 3.0, 4.0 ] ); + t.strictEqual( arr instanceof Float16Array, true, 'returns expected value' ); + + v = arr.length; + t.strictEqual( v, 4, 'returns expected value' ); + + // Generic array containing non-numeric values: + arr = Float16Array.from( [ 'beep' ] ); + t.strictEqual( arr instanceof Float16Array, true, 'returns expected value' ); + + v = arr.length; + t.strictEqual( v, 1, 'returns expected value' ); + + arr = Float16Array.from( [ null, 4 ] ); + t.strictEqual( arr instanceof Float16Array, true, 'returns expected value' ); + + v = arr.length; + t.strictEqual( v, 2, 'returns expected value' ); + + arr = Float16Array.from( [ true, {}, '' ] ); + t.strictEqual( arr instanceof Float16Array, true, 'returns expected value' ); + + v = arr.length; + t.strictEqual( v, 3, 'returns expected value' ); + + // Typed array: + arr = Float16Array.from( new Uint16Array( 0 ) ); + t.strictEqual( arr instanceof Float16Array, true, 'returns expected value' ); + + v = arr.length; + t.strictEqual( v, 0, 'returns expected value' ); + + arr = Float16Array.from( new Uint16Array( [ 1, 1 ] ) ); + t.strictEqual( arr instanceof Float16Array, true, 'returns expected value' ); + + v = arr.length; + t.strictEqual( v, 2, 'returns expected value' ); + + // Floating-point number typed array: + arr = Float16Array.from( new Float16Array( 0 ) ); + t.strictEqual( arr instanceof Float16Array, true, 'returns expected value' ); + + v = arr.length; + t.strictEqual( v, 0, 'returns expected value' ); + + arr = Float16Array.from( new Float16Array( [ 1.0, 2.0 ] ) ); + t.strictEqual( arr instanceof Float16Array, true, 'returns expected value' ); + + v = arr.length; + t.strictEqual( v, 2, 'returns expected value' ); + + t.end(); +}); + +tape( 'the method returns a floating-point number array (iterable)', function test( t ) { + var Float16Array; + var iter; + var arr; + var v; + + Float16Array = proxyquire( './../lib/main.js', { + '@stdlib/assert/has-iterator-symbol-support': hasSupport, + '@stdlib/symbol/iterator': '__SYMBOL_ITERATOR__' + }); + + iter = { + 'next': next, + 'i': 0, + 'N': 4 + }; + arr = Float16Array.from( createIterable( iter ) ); + t.strictEqual( arr instanceof Float16Array, true, 'returns expected value' ); + + v = arr.length; + t.strictEqual( v, iter.N, 'returns expected value' ); + + t.end(); + + function hasSupport() { + return true; + } + + function createIterable( iterator ) { + var it = {}; + it[ '__SYMBOL_ITERATOR__' ] = iterable; + return it; + + function iterable() { + return iterator; + } + } + + function next() { + iter.i += 1; + if ( iter.i <= iter.N ) { + return { + 'value': 1.0 + }; + } + return { + 'done': true + }; + } +}); + +tape( 'the method supports providing a "map" function which is invoked for each source element', function test( t ) { + var arr; + var v; + + // Generic array: + arr = Float16Array.from( [], clbk1 ); + t.strictEqual( arr instanceof Float16Array, true, 'returns expected value' ); + + v = arr.length; + t.strictEqual( v, 0, 'returns expected value' ); + + arr = Float16Array.from( [ 1.0, 2.0, 3.0, 4.0 ], clbk1 ); + t.strictEqual( arr instanceof Float16Array, true, 'returns expected value' ); + + v = arr.length; + t.strictEqual( v, 4, 'returns expected value' ); + + // Generic array containing non-numeric values: + arr = Float16Array.from( [ 'beep' ], clbk2 ); + t.strictEqual( arr instanceof Float16Array, true, 'returns expected value' ); + + v = arr.length; + t.strictEqual( v, 1, 'returns expected value' ); + + arr = Float16Array.from( [ null ], clbk2 ); + t.strictEqual( arr instanceof Float16Array, true, 'returns expected value' ); + + v = arr.length; + t.strictEqual( v, 1, 'returns expected value' ); + + arr = Float16Array.from( [ [], {} ], clbk1 ); + t.strictEqual( arr instanceof Float16Array, true, 'returns expected value' ); + + v = arr.length; + t.strictEqual( v, 2, 'returns expected value' ); + + arr = Float16Array.from( [ 4, 5 ], clbk1 ); + t.strictEqual( arr instanceof Float16Array, true, 'returns expected value' ); + + v = arr.length; + t.strictEqual( v, 2, 'returns expected value' ); + + // Typed array: + arr = Float16Array.from( new Uint16Array( 0 ), clbk1 ); + t.strictEqual( arr instanceof Float16Array, true, 'returns expected value' ); + + v = arr.length; + t.strictEqual( v, 0, 'returns expected value' ); + + arr = Float16Array.from( new Uint16Array( [ 1, 1 ] ), clbk1 ); + t.strictEqual( arr instanceof Float16Array, true, 'returns expected value' ); + + v = arr.length; + t.strictEqual( v, 2, 'returns expected value' ); + + // Floating-point number typed array: + arr = Float16Array.from( new Float16Array( 0 ), clbk2 ); + t.strictEqual( arr instanceof Float16Array, true, 'returns expected value' ); + + v = arr.length; + t.strictEqual( v, 0, 'returns expected value' ); + + arr = Float16Array.from( new Float16Array( [ true, true ] ), clbk2 ); + t.strictEqual( arr instanceof Float16Array, true, 'returns expected value' ); + + v = arr.length; + t.strictEqual( v, 2, 'returns expected value' ); + + t.end(); + + function clbk1( v ) { + return v; + } + + function clbk2() { + return true; + } +}); + +tape( 'the method supports providing a "map" function which is invoked for each iterated value', function test( t ) { + var Float16Array; + var iter; + var arr; + var v; + + Float16Array = proxyquire( './../lib/main.js', { + '@stdlib/assert/has-iterator-symbol-support': hasSupport, + '@stdlib/symbol/iterator': '__SYMBOL_ITERATOR__' + }); + + iter = { + 'next': next, + 'i': 0, + 'N': 4 + }; + arr = Float16Array.from( createIterable( iter ), clbk ); + t.strictEqual( arr instanceof Float16Array, true, 'returns expected value' ); + + v = arr.length; + t.strictEqual( v, iter.N, 'returns expected value' ); + + t.end(); + + function hasSupport() { + return true; + } + + function createIterable( iterator ) { + var it = {}; + it[ '__SYMBOL_ITERATOR__' ] = iterable; + return it; + + function iterable() { + return iterator; + } + } + + function next() { + iter.i += 1; + if ( iter.i <= iter.N ) { + return { + 'value': 1.0 + }; + } + return { + 'done': true + }; + } + + function clbk( v ) { + return !v; + } +}); + +tape( 'the method supports providing a `this` context for a provided map function', function test( t ) { + var arr; + var ctx; + + ctx = { + 'count': 0 + }; + arr = Float16Array.from( [ 1.0, 2.0, 3.0, 4.0 ], clbk1, ctx ); + t.strictEqual( arr instanceof Float16Array, true, 'returns expected value' ); + t.strictEqual( ctx.count, 4, 'returns expected value' ); + + t.end(); + + function clbk1( v ) { + this.count += 1; // eslint-disable-line no-invalid-this + return v; + } +}); + +tape( 'the method supports providing a `this` context for a provided map function (iterable)', function test( t ) { + var Float16Array; + var iter; + var ctx; + var arr; + + Float16Array = proxyquire( './../lib/main.js', { + '@stdlib/assert/has-iterator-symbol-support': hasSupport, + '@stdlib/symbol/iterator': '__SYMBOL_ITERATOR__' + }); + + iter = { + 'next': next, + 'i': 0, + 'N': 4 + }; + ctx = { + 'count': 0 + }; + + arr = Float16Array.from( createIterable( iter ), clbk, ctx ); + t.strictEqual( arr instanceof Float16Array, true, 'returns expected value' ); + t.strictEqual( ctx.count, 4, 'returns expected value' ); + + t.end(); + + function hasSupport() { + return true; + } + + function createIterable( iterator ) { + var it = {}; + it[ '__SYMBOL_ITERATOR__' ] = iterable; + return it; + + function iterable() { + return iterator; + } + } + + function next() { + iter.i += 1; + if ( iter.i <= iter.N ) { + return { + 'value': 1.0 + }; + } + return { + 'done': true + }; + } + + function clbk( v ) { + this.count += 1; // eslint-disable-line no-invalid-this + return v; + } +}); + +tape( 'the method throws an error if provided a non-iterable object (non-ES2015+)', function test( t ) { + var Float16Array; + var values; + var i; + + Float16Array = proxyquire( './../lib/main.js', { + '@stdlib/assert/has-iterator-symbol-support': hasSupport + }); + + values = [ + {}, + { + '0': 1, + '1': 2, + '2': 3 + } + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + return Float16Array.from( value ); + }; + } + + function hasSupport() { + return false; + } +}); + +tape( 'the method throws an error if provided a non-iterable object (ES2015+)', function test( t ) { + var Float16Array; + var values; + var i; + + Float16Array = proxyquire( './../lib/main.js', { + '@stdlib/assert/has-iterator-symbol-support': hasSupport, + '@stdlib/symbol/iterator': '__SYMBOL_ITERATOR__' + }); + + values = [ + {}, + { + '0': 1, + '1': 2, + '2': 3 + }, + { + '__SYMBOL_ITERATOR__': null + }, + { + '__SYMBOL_ITERATOR__': 'beep' + }, + { + '__SYMBOL_ITERATOR__': nonIterable1 + }, + { + '__SYMBOL_ITERATOR__': nonIterable2 + } + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + return Float16Array.from( value ); + }; + } + + function hasSupport() { + return true; + } + + function nonIterable1() { + return null; + } + + function nonIterable2() { + return {}; + } +}); diff --git a/lib/node_modules/@stdlib/array/float16/test/test.get.js b/lib/node_modules/@stdlib/array/float16/test/test.get.js new file mode 100644 index 000000000000..642a370c797a --- /dev/null +++ b/lib/node_modules/@stdlib/array/float16/test/test.get.js @@ -0,0 +1,135 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var hasOwnProp = require( '@stdlib/assert/has-own-property' ); +var isFunction = require( '@stdlib/assert/is-function' ); +var float64ToFloat16 = require( '@stdlib/number/float64/base/to-float16' ); +var Float16Array = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof Float16Array, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'attached to the prototype of the main export is a `get` method for returning an array element', function test( t ) { + t.strictEqual( hasOwnProp( Float16Array.prototype, 'get' ), true, 'has property' ); + t.strictEqual( isFunction( Float16Array.prototype.get ), true, 'has method' ); + t.end(); +}); + +tape( 'the method throws an error if invoked with a `this` context which is not a floating-point number array instance', function test( t ) { + var values; + var arr; + var i; + + arr = new Float16Array( 5 ); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + {}, + [] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + return arr.get.call( value, 0 ); + }; + } +}); + +tape( 'the method throws an error if provided an index argument which is not a nonnegative integer', function test( t ) { + var values; + var arr; + var i; + + arr = new Float16Array( 10 ); + + values = [ + '5', + -5, + 3.14, + NaN, + true, + false, + null, + void 0, + {}, + [], + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + return arr.get( value ); + }; + } +}); + +tape( 'the method returns `undefined` if provided an index which exceeds array dimensions', function test( t ) { + var arr; + var v; + var i; + + arr = new Float16Array( 10 ); + for ( i = 0; i < arr.length; i++ ) { + v = arr.get( arr.length+i ); + t.strictEqual( v, void 0, 'returns expected value for index '+(arr.length+i) ); + } + t.end(); +}); + +tape( 'the method returns an array element', function test( t ) { + var arr; + var v; + var i; + + arr = []; + for ( i = 0; i < 10; i++ ) { + arr.push( i + 0.05 ); + } + arr = new Float16Array( arr ); + + for ( i = 0; i < arr.length; i++ ) { + v = arr.get( i ); + t.strictEqual( v, float64ToFloat16( i + 0.05 ), 'returns expected value for index '+i ); + } + t.end(); +}); diff --git a/lib/node_modules/@stdlib/array/float16/test/test.includes.js b/lib/node_modules/@stdlib/array/float16/test/test.includes.js new file mode 100644 index 000000000000..08b90e1c8a55 --- /dev/null +++ b/lib/node_modules/@stdlib/array/float16/test/test.includes.js @@ -0,0 +1,222 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var hasOwnProp = require( '@stdlib/assert/has-own-property' ); +var isFunction = require( '@stdlib/assert/is-function' ); +var float64ToFloat16 = require( '@stdlib/number/float64/base/to-float16' ); +var Float16Array = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof Float16Array, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'attached to the prototype of the main export is an `includes` method', function test( t ) { + t.strictEqual( hasOwnProp( Float16Array.prototype, 'includes' ), true, 'has property' ); + t.strictEqual( isFunction( Float16Array.prototype.includes ), true, 'has method' ); + t.end(); +}); + +tape( 'the method throws an error if invoked with a `this` context which is not a floating-point number array instance', function test( t ) { + var values; + var arr; + var i; + + arr = new Float16Array( 5 ); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + {}, + [], + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + return arr.includes.call( value, true ); + }; + } +}); + +tape( 'the method throws an error if provided a first argument which is not a numeric value', function test( t ) { + var values; + var arr; + var i; + + arr = new Float16Array( 5 ); + + values = [ + '5', + true, + false, + null, + void 0, + {}, + [], + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + return arr.includes( value ); + }; + } +}); + +tape( 'the method throws an error if provided a second argument which is not an integer', function test( t ) { + var values; + var arr; + var i; + + arr = new Float16Array( 10 ); + + values = [ + '5', + 3.14, + NaN, + true, + false, + null, + void 0, + {}, + [], + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + return arr.includes( true, value ); + }; + } +}); + +tape( 'the method returns `false` if operating on an empty floating-point number array', function test( t ) { + var bool; + var arr; + + arr = new Float16Array(); + bool = arr.includes( 1.0 ); + + t.strictEqual( bool, false, 'returns expected value' ); + t.end(); +}); + +tape( 'the method returns `false` if a floating-point number is not found', function test( t ) { + var bool; + var arr; + + arr = new Float16Array( 10 ); + bool = arr.includes( 1.0 ); + + t.strictEqual( bool, false, 'returns expected value' ); + t.end(); +}); + +tape( 'the method returns `true` if an array contains a specified floating-point number', function test( t ) { + var bool; + var arr; + + arr = new Float16Array( [ 1.05, 2.05, 3.05, 4.05] ); + bool = arr.includes( float64ToFloat16( 1.05 ) ); + + t.strictEqual( bool, true, 'returns expected value' ); + t.end(); +}); + +tape( 'the method returns `false` if provided a second argument which exceeds the input array length', function test( t ) { + var bool; + var arr; + + arr = new Float16Array( 10 ); + bool = arr.includes( 0.0, 20 ); + + t.strictEqual( bool, false, 'returns expected value' ); + t.end(); +}); + +tape( 'the method supports specifying a starting search index', function test( t ) { + var bool; + var arr; + + arr = new Float16Array( [ 1.05, 2.05, 1.05, 1.05, 1.05 ] ); + + bool = arr.includes( float64ToFloat16( 1.05 ), 0 ); + t.strictEqual( bool, true, 'returns expected value' ); + + bool = arr.includes( float64ToFloat16( 2.05 ), 1 ); + t.strictEqual( bool, true, 'returns expected value' ); + + bool = arr.includes( float64ToFloat16( 2.05 ), 3 ); + t.strictEqual( bool, false, 'returns expected value' ); + t.end(); +}); + +tape( 'the method supports specifying a starting search index (negative)', function test( t ) { + var bool; + var arr; + + arr = new Float16Array( [ 1.05, 2.05, 1.05, 1.05, 1.05 ] ); + + bool = arr.includes( float64ToFloat16( 1.05 ), -5 ); + t.strictEqual( bool, true, 'returns expected value' ); + + bool = arr.includes( float64ToFloat16( 2.05 ), -2 ); + t.strictEqual( bool, false, 'returns expected value' ); + t.end(); +}); + +tape( 'when provided a starting index which resolves to an index which is less than zero, the method searches from the first array element', function test( t ) { + var bool; + var arr; + + arr = new Float16Array( [ 1.05, 2.05, 1.05, 1.05, 1.05 ] ); + + bool = arr.includes( float64ToFloat16( 1.05 ), -10 ); + t.strictEqual( bool, true, 'returns expected value' ); + + bool = arr.includes( float64ToFloat16( 2.05 ), -10 ); + t.strictEqual( bool, true, 'returns expected value' ); + t.end(); +}); diff --git a/lib/node_modules/@stdlib/array/float16/test/test.index_of.js b/lib/node_modules/@stdlib/array/float16/test/test.index_of.js new file mode 100644 index 000000000000..ac611739f521 --- /dev/null +++ b/lib/node_modules/@stdlib/array/float16/test/test.index_of.js @@ -0,0 +1,222 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var hasOwnProp = require( '@stdlib/assert/has-own-property' ); +var isFunction = require( '@stdlib/assert/is-function' ); +var float64ToFloat16 = require( '@stdlib/number/float64/base/to-float16' ); +var Float16Array = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof Float16Array, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'attached to the prototype of the main export is an `indexOf` method for returning an array element', function test( t ) { + t.strictEqual( hasOwnProp( Float16Array.prototype, 'indexOf' ), true, 'has property' ); + t.strictEqual( isFunction( Float16Array.prototype.indexOf ), true, 'has method' ); + t.end(); +}); + +tape( 'the method throws an error if invoked with a `this` context which is not a floating-point number array instance', function test( t ) { + var values; + var arr; + var i; + + arr = new Float16Array( 5 ); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + {}, + [], + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + return arr.indexOf.call( value, 1.0 ); + }; + } +}); + +tape( 'the method throws an error if provided a first argument which is not a numeric value', function test( t ) { + var values; + var arr; + var i; + + arr = new Float16Array( 5 ); + + values = [ + '5', + true, + false, + null, + void 0, + {}, + [], + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + return arr.indexOf( value ); + }; + } +}); + +tape( 'the method throws an error if provided a second argument which is not an integer', function test( t ) { + var values; + var arr; + var i; + + arr = new Float16Array( 10 ); + + values = [ + '5', + 3.14, + NaN, + true, + false, + null, + void 0, + {}, + [], + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + return arr.indexOf( 1.0, value ); + }; + } +}); + +tape( 'the method returns `-1` if operating on an empty floating-point number array', function test( t ) { + var arr; + var idx; + + arr = new Float16Array(); + idx = arr.indexOf( 1.0 ); + + t.strictEqual( idx, -1, 'returns expected value' ); + t.end(); +}); + +tape( 'the method returns `-1` if a numeric value is not found', function test( t ) { + var idx; + var arr; + + arr = new Float16Array( 10 ); + idx = arr.indexOf( 1.0 ); + + t.strictEqual( idx, -1, 'returns expected value' ); + t.end(); +}); + +tape( 'the method returns the index of the first match if a numeric value is found', function test( t ) { + var idx; + var arr; + + arr = new Float16Array( [ 1.05, 2.05, 3.05, 4.05 ] ); + idx = arr.indexOf( float64ToFloat16( 2.05 ) ); + + t.strictEqual( idx, 1, 'returns expected value' ); + t.end(); +}); + +tape( 'the method returns `-1` if provided a second argument which exceeds the input array length', function test( t ) { + var idx; + var arr; + + arr = new Float16Array( 10 ); + idx = arr.indexOf( 1.0, 20 ); + + t.strictEqual( idx, -1, 'returns expected value' ); + t.end(); +}); + +tape( 'the method supports specifying a starting search index', function test( t ) { + var idx; + var arr; + + arr = new Float16Array( [ 1.05, 2.05, 3.05, 2.05, 5.05 ] ); + + idx = arr.indexOf( float64ToFloat16( 2.05 ), 0 ); + t.strictEqual( idx, 1, 'returns expected value' ); + + idx = arr.indexOf( float64ToFloat16( 2.05 ), 2 ); + t.strictEqual( idx, 3, 'returns expected value' ); + + idx = arr.indexOf( float64ToFloat16( 2.05 ), 4 ); + t.strictEqual( idx, -1, 'returns expected value' ); + t.end(); +}); + +tape( 'the method supports specifying a starting search index (negative)', function test( t ) { + var idx; + var arr; + + arr = new Float16Array( [ 1.05, 2.05, 3.05, 2.05, 5.05 ] ); + + idx = arr.indexOf( float64ToFloat16( 2.05 ), -5 ); + t.strictEqual( idx, 1, 'returns expected value' ); + + idx = arr.indexOf( float64ToFloat16( 2.05 ), -1 ); + t.strictEqual( idx, -1, 'returns expected value' ); + t.end(); +}); + +tape( 'when provided a starting index which resolves to an index which is less than zero, the method searches from the first array element', function test( t ) { + var idx; + var arr; + + arr = new Float16Array( [ 1.05, 2.05, 3.05, 2.05, 5.05 ] ); + + idx = arr.indexOf( float64ToFloat16( 2.05 ), -10 ); + t.strictEqual( idx, 1, 'returns expected value' ); + + idx = arr.indexOf( float64ToFloat16( 1.05 ), -10 ); + t.strictEqual( idx, 0, 'returns expected value' ); + t.end(); +}); diff --git a/lib/node_modules/@stdlib/array/float16/test/test.join.js b/lib/node_modules/@stdlib/array/float16/test/test.join.js new file mode 100644 index 000000000000..9d15b7035443 --- /dev/null +++ b/lib/node_modules/@stdlib/array/float16/test/test.join.js @@ -0,0 +1,173 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var hasOwnProp = require( '@stdlib/assert/has-own-property' ); +var isFunction = require( '@stdlib/assert/is-function' ); +var float64ToFloat16 = require( '@stdlib/number/float64/base/to-float16' ); +var Float16Array = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof Float16Array, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'attached to the prototype of the main export is a `join` method', function test( t ) { + t.strictEqual( hasOwnProp( Float16Array.prototype, 'join' ), true, 'has property' ); + t.strictEqual( isFunction( Float16Array.prototype.join ), true, 'has method' ); + t.end(); +}); + +tape( 'the method throws an error if invoked with a `this` context which is not a floating-point number array instance', function test( t ) { + var values; + var arr; + var i; + + arr = new Float16Array( 5 ); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + {}, + [], + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + return arr.join.call( value ); + }; + } +}); + +tape( 'the method throws an error if invoked with a `separator` argument which is not a string', function test( t ) { + var values; + var arr; + var i; + + arr = new Float16Array( 5 ); + + values = [ + 5, + NaN, + true, + false, + null, + void 0, + {}, + [], + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + return arr.join( value ); + }; + } +}); + +tape( 'the method returns an empty string if invoked on an empty array', function test( t ) { + var str; + var arr; + + arr = new Float16Array(); + str = arr.join(); + + t.strictEqual( str, '', 'returns expected value' ); + t.end(); +}); + +tape( 'the method returns a string representation of a floating-point number array with elements separated by a separator', function test( t ) { + var expected; + var str; + var arr; + var i; + + arr = new Float16Array( [ 0.05, 1.05, 2.05, 3.05, 4.05 ] ); + + expected = float64ToFloat16( 0.05 ); + for ( i = 1; i < arr.length; i++ ) { + expected += '@' + float64ToFloat16( i + 0.05 ); + } + + str = arr.join( '@' ); + + t.strictEqual( str, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the method returns a string representation of a floating-point number array with elements separated by a separator (single element)', function test( t ) { + var expected; + var str; + var arr; + + arr = new Float16Array( [ 1.05 ] ); + expected = '' + float64ToFloat16( 1.05 ); + + str = arr.join(); + + t.strictEqual( str, expected, 'returns expected value' ); + + arr = new Float16Array( [ 1.05 ] ); + expected = '' + float64ToFloat16( 1.05 ); + + str = arr.join( '@' ); + + t.strictEqual( str, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'if the method is invoked without a separator argument, the method returns a string representation of a floating-point number array with elements separated by a comma', function test( t ) { + var expected; + var str; + var arr; + var i; + + arr = new Float16Array( [ 0.05, 1.05, 2.05, 3.05, 4.05 ] ); + + expected = float64ToFloat16( 0.05 ); + for ( i = 1; i < arr.length; i++ ) { + expected += ',' + float64ToFloat16( i + 0.05 ); + } + + str = arr.join(); + + t.strictEqual( str, expected, 'returns expected value' ); + t.end(); +}); diff --git a/lib/node_modules/@stdlib/array/float16/test/test.js b/lib/node_modules/@stdlib/array/float16/test/test.js index 364340deb159..50f1fc4bb1f2 100644 --- a/lib/node_modules/@stdlib/array/float16/test/test.js +++ b/lib/node_modules/@stdlib/array/float16/test/test.js @@ -22,59 +22,719 @@ var tape = require( 'tape' ); var proxyquire = require( 'proxyquire' ); -var hasFloat16ArraySupport = require( '@stdlib/assert/has-float16array-support' ); -var polyfill = require( './../lib/polyfill.js' ); -var ctor = require( './../lib' ); - - -// VARIABLES // - -var hasFloat16Arrays = hasFloat16ArraySupport(); +var ArrayBuffer = require( '@stdlib/array/buffer' ); +var Uint16Array = require( '@stdlib/array/uint16' ); +var hasOwnProp = require( '@stdlib/assert/has-own-property' ); +var isArrayBuffer = require( '@stdlib/assert/is-arraybuffer' ); +var Float16Array = require( './../lib' ); // TESTS // tape( 'main export is a function', function test( t ) { t.ok( true, __filename ); - t.strictEqual( typeof ctor, 'function', 'main export is a function' ); + t.strictEqual( typeof Float16Array, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function is a constructor', function test( t ) { + var arr = new Float16Array( 0 ); + t.strictEqual( arr instanceof Float16Array, true, 'returns expected value' ); + t.end(); +}); + +tape( 'the constructor does not require the `new` keyword', function test( t ) { + var ctor; + var arr; + + ctor = Float16Array; + + arr = ctor( 0 ); + t.strictEqual( arr instanceof Float16Array, true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the constructor returns a 16-bit floating-point number array (no argument)', function test( t ) { + var arr = new Float16Array(); + t.strictEqual( arr instanceof Float16Array, true, 'returns expected value' ); t.end(); }); -tape( 'if an environment supports `Float16Array`, the export is an alias for `Float16Array`', function test( t ) { - var Foo; +tape( 'the constructor returns a 16-bit floating-point number array (no argument, no new)', function test( t ) { + var ctor; + var arr; + + ctor = Float16Array; + + arr = ctor(); + t.strictEqual( arr instanceof Float16Array, true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the constructor returns a 16-bit floating-point number array (length)', function test( t ) { + var arr = new Float16Array( 10 ); + t.strictEqual( arr instanceof Float16Array, true, 'returns expected value' ); + t.end(); +}); + +tape( 'the constructor returns a 16-bit floating-point number array (length, no new)', function test( t ) { + var ctor; + var arr; + + ctor = Float16Array; + + arr = ctor( 10 ); + t.strictEqual( arr instanceof Float16Array, true, 'returns expected value' ); + + t.end(); +}); - Foo = proxyquire( './../lib', { - '@stdlib/assert/has-float16array-support': isTrue, - './main.js': Mock +tape( 'the constructor returns a 16-bit floating-point number array (array)', function test( t ) { + var arr = new Float16Array( [] ); + t.strictEqual( arr instanceof Float16Array, true, 'returns expected value' ); + t.end(); +}); + +tape( 'the constructor returns a 16-bit floating-point number array (array, no new)', function test( t ) { + var ctor; + var arr; + + ctor = Float16Array; + + arr = ctor( [] ); + t.strictEqual( arr instanceof Float16Array, true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the constructor returns a 16-bit floating-point number array (typed array)', function test( t ) { + var arr = new Float16Array( new Uint16Array( 0 ) ); + t.strictEqual( arr instanceof Float16Array, true, 'returns expected value' ); + t.end(); +}); + +tape( 'the constructor returns a 16-bit floating-point number array (typed array, no new)', function test( t ) { + var ctor; + var arr; + + ctor = Float16Array; + + arr = ctor( new Uint16Array( 0 ) ); + t.strictEqual( arr instanceof Float16Array, true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the constructor returns a 16-bit floating-point number array (iterable)', function test( t ) { + var Float16Array; + var arr; + + Float16Array = proxyquire( './../lib/main.js', { + '@stdlib/assert/has-iterator-symbol-support': hasSupport, + '@stdlib/symbol/iterator': '__SYMBOL_ITERATOR__' }); - t.strictEqual( Foo, Mock, 'returns builtin' ); - if ( hasFloat16Arrays ) { - t.strictEqual( ctor, Float16Array, 'is alias' ); // eslint-disable-line stdlib/require-globals + arr = new Float16Array( createIterable() ); + t.strictEqual( arr instanceof Float16Array, true, 'returns expected value' ); + + t.end(); + + function hasSupport() { + return true; } + function createIterable() { + var it = {}; + it[ '__SYMBOL_ITERATOR__' ] = iterable; + return it; + + function iterable() { + return { + 'next': next + }; + } + + function next() { + return { + 'done': true + }; + } + } +}); + +tape( 'the constructor returns a 16-bit floating-point number array (iterable, no new)', function test( t ) { + var ctor; + var arr; + + ctor = proxyquire( './../lib/main.js', { + '@stdlib/assert/has-iterator-symbol-support': hasSupport, + '@stdlib/symbol/iterator': '__SYMBOL_ITERATOR__' + }); + + arr = ctor( createIterable() ); + t.strictEqual( arr instanceof ctor, true, 'returns expected value' ); + t.end(); - function Mock() { - return this; + function hasSupport() { + return true; + } + + function createIterable() { + var it = {}; + it[ '__SYMBOL_ITERATOR__' ] = iterable; + return it; + + function iterable() { + return { + 'next': next + }; + } + + function next() { + return { + 'done': true + }; + } } +}); + +tape( 'the constructor returns a 16-bit floating-point number array (ArrayBuffer)', function test( t ) { + var arr = new Float16Array( new ArrayBuffer( 0 ) ); + t.strictEqual( arr instanceof Float16Array, true, 'returns expected value' ); + t.end(); +}); + +tape( 'the constructor returns a 16-bit floating-point number array (ArrayBuffer, no new)', function test( t ) { + var ctor; + var arr; + + ctor = Float16Array; + + arr = ctor( new ArrayBuffer( 0 ) ); + t.strictEqual( arr instanceof Float16Array, true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the constructor returns a 16-bit floating-point number array (ArrayBuffer, byte offset)', function test( t ) { + var arr = new Float16Array( new ArrayBuffer( 8 ), 8 ); + t.strictEqual( arr instanceof Float16Array, true, 'returns expected value' ); + t.end(); +}); + +tape( 'the constructor returns a 16-bit floating-point number array (ArrayBuffer, byte offset, no new)', function test( t ) { + var ctor; + var arr; + + ctor = Float16Array; + + arr = ctor( new ArrayBuffer( 8 ), 8 ); + t.strictEqual( arr instanceof Float16Array, true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the constructor returns a 16-bit floating-point number array (ArrayBuffer, byte offset, length)', function test( t ) { + var arr = new Float16Array( new ArrayBuffer( 8 ), 8, 0 ); + t.strictEqual( arr instanceof Float16Array, true, 'returns expected value' ); + t.end(); +}); + +tape( 'the constructor returns a 16-bit floating-point number array (ArrayBuffer, byte offset, length, no new)', function test( t ) { + var ctor; + var arr; + + ctor = Float16Array; + + arr = ctor( new ArrayBuffer( 8 ), 8, 0 ); + t.strictEqual( arr instanceof Float16Array, true, 'returns expected value' ); + + t.end(); +}); + +tape( 'attached to the constructor is a property returning the number of bytes per array element', function test( t ) { + t.strictEqual( hasOwnProp( Float16Array, 'BYTES_PER_ELEMENT' ), true, 'has property' ); + t.strictEqual( Float16Array.BYTES_PER_ELEMENT, 2, 'returns expected value' ); + t.end(); +}); + +tape( 'attached to the constructor is a property returning the constructor name', function test( t ) { + t.strictEqual( hasOwnProp( Float16Array, 'name' ), true, 'has property' ); + t.strictEqual( Float16Array.name, 'Float16Array', 'returns expected value' ); + t.end(); +}); + +tape( 'the constructor returns an instance having a `BYTES_PER_ELEMENT` property returning the number of bytes per array element', function test( t ) { + var arr; + + t.strictEqual( hasOwnProp( Float16Array.prototype, 'BYTES_PER_ELEMENT' ), true, 'has property' ); + t.strictEqual( Float16Array.prototype.BYTES_PER_ELEMENT, 2, 'returns expected value' ); + + arr = new Float16Array( 0 ); + t.strictEqual( arr.BYTES_PER_ELEMENT, 2, 'returns expected value' ); + + t.end(); +}); + +tape( 'the constructor returns an instance having a `buffer` property for returning the underlying memory (i.e., ArrayBuffer)', function test( t ) { + var arr; + var buf; + + arr = new Float16Array( 0 ); + buf = arr.buffer; + t.strictEqual( isArrayBuffer( buf ), true, 'returns expected value' ); + t.end(); +}); + +tape( 'the constructor returns an instance having a `byteLength` property for returning the number of bytes belonging to the array view', function test( t ) { + var arr; + var v; + + arr = new Float16Array( 0 ); + v = arr.byteLength; + t.strictEqual( v, 0, 'returns expected value' ); + + arr = new Float16Array( new ArrayBuffer( 64 ), 8 ); + v = arr.byteLength; + t.strictEqual( v, 56, 'returns expected value' ); - function isTrue() { + arr = new Float16Array( new ArrayBuffer( 64 ), 64 ); + v = arr.byteLength; + t.strictEqual( v, 0, 'returns expected value' ); + + t.end(); +}); + +tape( 'the constructor returns an instance having a `byteOffset` property for returning the byte offset pointing to the first array element in the underlying memory', function test( t ) { + var arr; + var v; + + arr = new Float16Array( 0 ); + v = arr.byteOffset; + t.strictEqual( v, 0, 'returns expected value' ); + + arr = new Float16Array( new ArrayBuffer( 64 ), 32 ); + v = arr.byteOffset; + t.strictEqual( v, 32, 'returns expected value' ); + + arr = new Float16Array( new ArrayBuffer( 64 ), 64 ); + v = arr.byteOffset; + t.strictEqual( v, 64, 'returns expected value' ); + + t.end(); +}); + +tape( 'the constructor returns an instance having a `length` property for returning the number of array elements', function test( t ) { + var arr; + var v; + + // No arguments: + arr = new Float16Array(); + v = arr.length; + t.strictEqual( v, 0, 'returns expected value' ); + + // Explicit array length: + arr = new Float16Array( 0 ); + v = arr.length; + t.strictEqual( v, 0, 'returns expected value' ); + + arr = new Float16Array( 10 ); + v = arr.length; + t.strictEqual( v, 10, 'returns expected value' ); + + // Generic array: + arr = new Float16Array( [] ); + v = arr.length; + t.strictEqual( v, 0, 'returns expected value' ); + + arr = new Float16Array( [ 1.0, 2.0, 3.0, 4.0 ] ); + v = arr.length; + t.strictEqual( v, 4, 'returns expected value' ); + + // Typed array: + arr = new Float16Array( new Uint16Array( 0 ) ); + v = arr.length; + t.strictEqual( v, 0, 'returns expected value' ); + + arr = new Float16Array( new Uint16Array( [ 1.0, 1.0 ] ) ); + v = arr.length; + t.strictEqual( v, 2, 'returns expected value' ); + + // Float16 typed array: + arr = new Float16Array( new Float16Array( 0 ) ); + v = arr.length; + t.strictEqual( v, 0, 'returns expected value' ); + + arr = new Float16Array( new Float16Array( [ 1.0, 2.0 ] ) ); + v = arr.length; + t.strictEqual( v, 2, 'returns expected value' ); + + // ArrayBuffer: + arr = new Float16Array( new ArrayBuffer( 64 ), 32 ); + v = arr.length; + t.strictEqual( v, 16, 'returns expected value' ); + + arr = new Float16Array( new ArrayBuffer( 64 ), 64 ); + v = arr.length; + t.strictEqual( v, 0, 'returns expected value' ); + + arr = new Float16Array( new ArrayBuffer( 64 ), 32, 2 ); + v = arr.length; + t.strictEqual( v, 2, 'returns expected value' ); + + t.end(); +}); + +tape( 'the constructor returns an instance having a `length` property for returning the number of array elements (iterable)', function test( t ) { + var Float16Array; + var iter; + var arr; + var v; + + Float16Array = proxyquire( './../lib/main.js', { + '@stdlib/assert/has-iterator-symbol-support': hasSupport, + '@stdlib/symbol/iterator': '__SYMBOL_ITERATOR__' + }); + + iter = { + 'next': next, + 'i': 0, + 'N': 4 + }; + arr = new Float16Array( createIterable( iter ) ); + v = arr.length; + t.strictEqual( v, iter.N, 'returns expected value' ); + + t.end(); + + function hasSupport() { return true; } + + function createIterable( iterator ) { + var it = {}; + it[ '__SYMBOL_ITERATOR__' ] = iterable; + return it; + + function iterable() { + return iterator; + } + } + + function next() { + iter.i += 1; + if ( iter.i <= iter.N ) { + return { + 'value': 1.0 + }; + } + return { + 'done': true + }; + } +}); + +tape( 'the constructor throws an error if provided an ArrayBuffer which is not a multiple of 2', function test( t ) { + var values; + var i; + + values = [ + 1, + 3, + 5, + 7, + 9, + 11, + 101, + 1001 + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), RangeError, 'throws an error when provided an ArrayBuffer having a byte length equal to '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + return new Float16Array( new ArrayBuffer( value ) ); + }; + } }); -tape( 'if an environment does not support `Float16Array`, the export is a polyfill', function test( t ) { - var Foo; +tape( 'the constructor throws an error if provided a non-iterable object (non-ES2015+)', function test( t ) { + var Float16Array; + var values; + var i; - Foo = proxyquire( './../lib', { - '@stdlib/assert/has-float16array-support': isFalse + Float16Array = proxyquire( './../lib/main.js', { + '@stdlib/assert/has-iterator-symbol-support': hasSupport }); - t.strictEqual( Foo, polyfill, 'returns polyfill' ); + values = [ + {}, + { + '0': 1, + '1': 2, + '2': 3 + } + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } t.end(); - function isFalse() { + function badValue( value ) { + return function badValue() { + return new Float16Array( value ); + }; + } + + function hasSupport() { return false; } }); + +tape( 'the constructor throws an error if provided a non-iterable object (ES2015+)', function test( t ) { + var Float16Array; + var values; + var i; + + Float16Array = proxyquire( './../lib/main.js', { + '@stdlib/assert/has-iterator-symbol-support': hasSupport, + '@stdlib/symbol/iterator': '__SYMBOL_ITERATOR__' + }); + + values = [ + {}, + { + '0': 1, + '1': 2, + '2': 3 + }, + { + '__SYMBOL_ITERATOR__': null + }, + { + '__SYMBOL_ITERATOR__': 'beep' + }, + { + '__SYMBOL_ITERATOR__': nonIterable1 + }, + { + '__SYMBOL_ITERATOR__': nonIterable2 + } + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + return new Float16Array( value ); + }; + } + + function hasSupport() { + return true; + } + + function nonIterable1() { + return null; + } + + function nonIterable2() { + return {}; + } +}); + +tape( 'the constructor throws an error if not provided a length, iterable, array-like object, or ArrayBuffer', function test( t ) { + var values; + var i; + + values = [ + '5', + 3.14, + -1, + NaN, + true, + false, + null, + void 0, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + return new Float16Array( value ); + }; + } +}); + +tape( 'the constructor throws an error if provided more than one argument and the first argument is not an ArrayBuffer', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + return new Float16Array( value, 0 ); + }; + } +}); + +tape( 'the constructor throws an error if provided a byte offset argument which is not a nonnegative integer', function test( t ) { + var values; + var i; + + values = [ + '5', + -1, + 3.14, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + return new Float16Array( new ArrayBuffer( 64 ), value ); + }; + } +}); + +tape( 'the constructor throws an error if provided a byte offset argument which is not a multiple of 2', function test( t ) { + var values; + var i; + + values = [ + 1, + 3, + 5, + 7, + 9, + 11, + 101, + 1001 + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), RangeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + return new Float16Array( new ArrayBuffer( 1e3 ), value ); + }; + } +}); + +tape( 'the constructor throws an error if provided a byte offset argument such that the view byte length is not a multiple of 8', function test( t ) { + var values; + var i; + + values = [ + 2, + 4, + 6, + 8, + 10, + 12, + 14, + 16, + 18, + 20 + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), RangeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + return new Float16Array( new ArrayBuffer( 101 ), value ); + }; + } +}); + +tape( 'the constructor throws an error if provided a length argument which is not a nonnegative integer (ArrayBuffer)', function test( t ) { + var values; + var i; + + values = [ + '5', + -1, + 3.14, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + return new Float16Array( new ArrayBuffer( 64 ), 0, value ); + }; + } +}); + +tape( 'the constructor throws an error if provided insufficient memory to accommodate byte offset and length arguments', function test( t ) { + var values; + var i; + + values = [ + 8, + 16, + 24, + 32 + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), RangeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + return new Float16Array( new ArrayBuffer( 100 ), value, 1e3 ); + }; + } +}); diff --git a/lib/node_modules/@stdlib/array/float16/test/test.keys.js b/lib/node_modules/@stdlib/array/float16/test/test.keys.js new file mode 100644 index 000000000000..25f6e43cd488 --- /dev/null +++ b/lib/node_modules/@stdlib/array/float16/test/test.keys.js @@ -0,0 +1,261 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var proxyquire = require( 'proxyquire' ); +var hasOwnProp = require( '@stdlib/assert/has-own-property' ); +var isFunction = require( '@stdlib/assert/is-function' ); +var ITERATOR_SYMBOL = require( '@stdlib/symbol/iterator' ); +var Float16Array = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof Float16Array, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'attached to the prototype of the main export is a `keys` method', function test( t ) { + t.strictEqual( hasOwnProp( Float16Array.prototype, 'keys' ), true, 'has property' ); + t.strictEqual( isFunction( Float16Array.prototype.keys ), true, 'has method' ); + t.end(); +}); + +tape( 'the method throws an error if invoked with a `this` context which is not a floating-point number array instance', function test( t ) { + var values; + var arr; + var i; + + arr = new Float16Array( 5 ); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + {}, + [], + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + return arr.keys.call( value ); + }; + } +}); + +tape( 'the method returns an iterator protocol-compliant object', function test( t ) { + var expected; + var arr; + var it; + var i; + var r; + var e; + + arr = new Float16Array( [ 1.05, 2.05 ] ); + expected = [ + { + 'value': 0, + 'done': false + }, + { + 'value': 1, + 'done': false + }, + { + 'done': true + } + ]; + it = arr.keys(); + + t.strictEqual( typeof it, 'object', 'returns expected value' ); + t.strictEqual( typeof it.next, 'function', 'has next method' ); + + for ( i = 0; i < expected.length; i++ ) { + r = it.next(); + e = expected[ i ]; + if ( e.value === void 0 ) { + t.deepEqual( r, e, 'returns expected value' ); + } else { + t.strictEqual( r.value, e.value, 'returns expected value' ); + t.strictEqual( r.done, e.done, 'returns expected value' ); + } + } + + t.end(); +}); + +tape( 'the method returns an iterator which does not iterate over empty arrays', function test( t ) { + var expected; + var arr; + var it; + var i; + var v; + + arr = new Float16Array( [] ); + expected = [ + { + 'done': true + }, + { + 'done': true + }, + { + 'done': true + } + ]; + it = arr.keys(); + + t.strictEqual( typeof it, 'object', 'returns expected value' ); + t.strictEqual( typeof it.next, 'function', 'has next method' ); + + for ( i = 0; i < expected.length; i++ ) { + v = it.next(); + t.deepEqual( v, expected[ i ], 'returns expected value' ); + } + t.end(); +}); + +tape( 'the returned iterator has a `return` method for closing an iterator (no argument)', function test( t ) { + var arr; + var it; + var v; + + arr = new Float16Array( [ 1.05, 2.05, 3.05, 4.05 ] ); + it = arr.keys(); + + v = it.next(); + t.strictEqual( v.value, 0, 'returns expected value' ); + t.strictEqual( v.done, false, 'returns expected value' ); + + v = it.next(); + t.strictEqual( v.value, 1, 'returns expected value' ); + t.strictEqual( v.done, false, 'returns expected value' ); + + v = it.return(); + t.strictEqual( v.value, void 0, 'returns expected value' ); + t.strictEqual( v.done, true, 'returns expected value' ); + + v = it.next(); + t.strictEqual( v.value, void 0, 'returns expected value' ); + t.strictEqual( v.done, true, 'returns expected value' ); + + v = it.next(); + t.strictEqual( v.value, void 0, 'returns expected value' ); + t.strictEqual( v.done, true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the returned iterator has a `return` method for closing an iterator (argument)', function test( t ) { + var arr; + var it; + var v; + + arr = new Float16Array( [ 1.05, 2.05, 3.05, 4.05 ] ); + it = arr.keys(); + + v = it.next(); + t.strictEqual( v.value, 0, 'returns expected value' ); + t.strictEqual( v.done, false, 'returns expected value' ); + + v = it.next(); + t.strictEqual( v.value, 1, 'returns expected value' ); + t.strictEqual( v.done, false, 'returns expected value' ); + + v = it.return( 'beep' ); + t.strictEqual( v.value, 'beep', 'returns expected value' ); + t.strictEqual( v.done, true, 'returns expected value' ); + + v = it.next(); + t.strictEqual( v.value, void 0, 'returns expected value' ); + t.strictEqual( v.done, true, 'returns expected value' ); + + v = it.next(); + t.strictEqual( v.value, void 0, 'returns expected value' ); + t.strictEqual( v.done, true, 'returns expected value' ); + + t.end(); +}); + +tape( 'if an environment supports `Symbol.iterator`, the method returns an iterable', function test( t ) { + var Float16Array; + var arr; + var buf; + var it1; + var it2; + var v1; + var v2; + var i; + + Float16Array = proxyquire( './../lib/main.js', { + '@stdlib/symbol/iterator': '__ITERATOR_SYMBOL__' + }); + + buf = [ 1.05, 2.05, 3.05, 4.05 ]; + arr = new Float16Array( buf ); + + it1 = arr.keys(); + t.strictEqual( typeof it1[ '__ITERATOR_SYMBOL__' ], 'function', 'has method' ); + t.strictEqual( it1[ '__ITERATOR_SYMBOL__' ].length, 0, 'has zero arity' ); + + it2 = it1[ '__ITERATOR_SYMBOL__' ](); + t.strictEqual( typeof it2, 'object', 'returns expected value' ); + t.strictEqual( typeof it2.next, 'function', 'has `next` method' ); + t.strictEqual( typeof it2.return, 'function', 'has `return` method' ); + + for ( i = 0; i < arr.length; i++ ) { + v1 = it1.next().value; + v2 = it2.next().value; + t.strictEqual( v1, v2, 'returns expected value' ); + } + t.end(); +}); + +tape( 'if an environment does not support `Symbol.iterator`, the method does not return an "iterable"', function test( t ) { + var Float16Array; + var arr; + var buf; + var it; + + Float16Array = proxyquire( './../lib/main.js', { + '@stdlib/symbol/iterator': false + }); + + buf = [ 1.05, 2.05, 3.05, 4.05 ]; + arr = new Float16Array( buf ); + + it = arr.keys(); + t.strictEqual( it[ ITERATOR_SYMBOL ], void 0, 'does not have property' ); + + t.end(); +}); diff --git a/lib/node_modules/@stdlib/array/float16/test/test.last_index_of.js b/lib/node_modules/@stdlib/array/float16/test/test.last_index_of.js new file mode 100644 index 000000000000..7cdbebcb9aab --- /dev/null +++ b/lib/node_modules/@stdlib/array/float16/test/test.last_index_of.js @@ -0,0 +1,211 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var hasOwnProp = require( '@stdlib/assert/has-own-property' ); +var isFunction = require( '@stdlib/assert/is-function' ); +var float64ToFloat16 = require( '@stdlib/number/float64/base/to-float16' ); +var Float16Array = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof Float16Array, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'attached to the prototype of the main export is a `lastIndexOf` method for returning an array element', function test( t ) { + t.strictEqual( hasOwnProp( Float16Array.prototype, 'lastIndexOf' ), true, 'has property' ); + t.strictEqual( isFunction( Float16Array.prototype.lastIndexOf ), true, 'has method' ); + t.end(); +}); + +tape( 'the method throws an error if invoked with a `this` context which is not a floating-point number array instance', function test( t ) { + var values; + var arr; + var i; + + arr = new Float16Array( 5 ); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + {}, + [], + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + return arr.lastIndexOf.call( value, true ); + }; + } +}); + +tape( 'the method throws an error if provided a first argument which is not a numeric value', function test( t ) { + var values; + var arr; + var i; + + arr = new Float16Array( 5 ); + + values = [ + '5', + true, + false, + null, + void 0, + {}, + [], + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + return arr.lastIndexOf( value ); + }; + } +}); + +tape( 'the method throws an error if provided a second argument which is not an integer', function test( t ) { + var values; + var arr; + var i; + + arr = new Float16Array( 10 ); + + values = [ + '5', + 3.14, + NaN, + true, + false, + null, + void 0, + {}, + [], + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + return arr.lastIndexOf( true, value ); + }; + } +}); + +tape( 'the method returns `-1` if operating on an empty floating-point number array', function test( t ) { + var arr; + var idx; + + arr = new Float16Array(); + idx = arr.lastIndexOf( 0.0 ); + + t.strictEqual( idx, -1, 'returns expected value' ); + t.end(); +}); + +tape( 'the method returns `-1` if a numeric value is not found', function test( t ) { + var idx; + var arr; + + arr = new Float16Array( 10 ); + idx = arr.lastIndexOf( 1.0 ); + + t.strictEqual( idx, -1, 'returns expected value' ); + t.end(); +}); + +tape( 'the method returns the index of the first match when searching from the end of the array if a search element is found', function test( t ) { + var idx; + var arr; + + arr = new Float16Array( [ 1.05, 2.05, 1.05, 2.05 ] ); + idx = arr.lastIndexOf( float64ToFloat16( 1.05 ) ); + + t.strictEqual( idx, 2, 'returns expected value' ); + t.end(); +}); + +tape( 'the method supports specifying a starting search index', function test( t ) { + var idx; + var arr; + + arr = new Float16Array( [ 1.05, 2.05, 2.05, 1.05, 1.05 ] ); + + idx = arr.lastIndexOf( float64ToFloat16( 1.05 ), 4 ); + t.strictEqual( idx, 4, 'returns expected value' ); + + idx = arr.lastIndexOf( float64ToFloat16( 2.05 ), 2 ); + t.strictEqual( idx, 2, 'returns expected value' ); + + idx = arr.lastIndexOf( float64ToFloat16( 2.05 ), 0 ); + t.strictEqual( idx, -1, 'returns expected value' ); + t.end(); +}); + +tape( 'the method supports specifying a starting search index (negative)', function test( t ) { + var idx; + var arr; + + arr = new Float16Array( [ 1.05, 2.05, 2.05, 1.05, 1.05 ] ); + + idx = arr.lastIndexOf( float64ToFloat16( 1.05 ), -3 ); + t.strictEqual( idx, 0, 'returns expected value' ); + + idx = arr.lastIndexOf( float64ToFloat16( 2.05 ), -1 ); + t.strictEqual( idx, 2, 'returns expected value' ); + t.end(); +}); + +tape( 'when the method is provided a starting index which resolves to an index which exceeds the maximum array index, the method searches from the last array element', function test( t ) { + var idx; + var arr; + + arr = new Float16Array( [ 1.05, 2.05, 2.05, 1.05, 1.05 ] ); + + idx = arr.lastIndexOf( float64ToFloat16( 1.05 ), 10 ); + t.strictEqual( idx, 4, 'returns expected value' ); + + idx = arr.lastIndexOf( float64ToFloat16( 2.05 ), 10 ); + t.strictEqual( idx, 2, 'returns expected value' ); + t.end(); +}); diff --git a/lib/node_modules/@stdlib/array/float16/test/test.map.js b/lib/node_modules/@stdlib/array/float16/test/test.map.js new file mode 100644 index 000000000000..f919678a1e65 --- /dev/null +++ b/lib/node_modules/@stdlib/array/float16/test/test.map.js @@ -0,0 +1,178 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var hasOwnProp = require( '@stdlib/assert/has-own-property' ); +var isFunction = require( '@stdlib/assert/is-function' ); +var identity = require( '@stdlib/utils/identity-function' ); +var toWord = require( '@stdlib/number/float16/base/to-word' ); +var instanceOf = require( '@stdlib/assert/instance-of' ); +var Uint16Array = require( '@stdlib/array/uint16' ); +var Float16Array = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof Float16Array, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'attached to the prototype of the main export is a `map` method', function test( t ) { + t.strictEqual( hasOwnProp( Float16Array.prototype, 'map' ), true, 'has property' ); + t.strictEqual( isFunction( Float16Array.prototype.map ), true, 'has method' ); + t.end(); +}); + +tape( 'the method throws an error if invoked with a `this` context which is not a floating-point number array instance', function test( t ) { + var values; + var arr; + var i; + + arr = new Float16Array( 5 ); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + {}, + [], + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + return arr.map.call( value, identity ); + }; + } +}); + +tape( 'the method throws an error if provided a first argument which is not a function', function test( t ) { + var values; + var arr; + var i; + + arr = new Float16Array( 10 ); + + values = [ + '5', + 3.14, + NaN, + true, + false, + null, + void 0, + {}, + [] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + return arr.map( value ); + }; + } +}); + +tape( 'the method returns an empty array if operating on an empty floating-point number array', function test( t ) { + var arr; + var out; + + arr = new Float16Array(); + out = arr.map( identity ); + + t.strictEqual( instanceOf( out, Float16Array ), true, 'returns expected value' ); + t.strictEqual( out.length, 0, 'returns expected value' ); + t.notEqual( out, arr, 'returns a new instance' ); + t.end(); +}); + +tape( 'the method returns a new floating-point number array containing elements which are the result of a provided callback function', function test( t ) { + var expected; + var actual; + var arr; + var buf; + + arr = new Float16Array( [ 1.05, 2.05, 3.05, 4.05 ] ); + expected = new Uint16Array([ + toWord( -1.05 ), + toWord( -2.05 ), + toWord( -3.05 ), + toWord( -4.05 ) + ]); + actual = arr.map( invert ); + buf = new Uint16Array( actual.buffer ); + + t.strictEqual( instanceOf( actual, Float16Array ), true, 'returns expected value' ); + t.strictEqual( actual.length, expected.length, 'returns expected value' ); + t.notEqual( actual, arr, 'returns a new instance' ); + t.deepEqual( buf, expected, 'returns expected value' ); + t.end(); + + function invert( v ) { + return -v; + } +}); + +tape( 'the method supports providing an execution context', function test( t ) { + var expected; + var actual; + var arr; + var buf; + var ctx; + + arr = new Float16Array( [ 1.05, 2.05, 3.05, 4.05 ] ); + expected = new Uint16Array([ + toWord( -1.05 ), + toWord( -2.05 ), + toWord( -3.05 ), + toWord( -4.05 ) + ]); + ctx = { + 'count': 0 + }; + actual = arr.map( invert, ctx ); + buf = new Uint16Array( actual.buffer ); + + t.strictEqual( instanceOf( actual, Float16Array ), true, 'returns expected value' ); + t.strictEqual( actual.length, expected.length, 'returns expected value' ); + t.deepEqual( buf, expected, 'returns expected value' ); + t.strictEqual( ctx.count, 6, 'returns expected value' ); + t.end(); + + function invert( v, i ) { + this.count += i; // eslint-disable-line no-invalid-this + return -v; + } +}); diff --git a/lib/node_modules/@stdlib/array/float16/test/test.of.js b/lib/node_modules/@stdlib/array/float16/test/test.of.js new file mode 100644 index 000000000000..68c4ac69e73b --- /dev/null +++ b/lib/node_modules/@stdlib/array/float16/test/test.of.js @@ -0,0 +1,130 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var hasOwnProp = require( '@stdlib/assert/has-own-property' ); +var isFunction = require( '@stdlib/assert/is-function' ); +var Float16Array = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof Float16Array, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'attached to the main export is an `of` method for creating a 16-bit floating-point number array from a variable number of arguments', function test( t ) { + var arr; + + t.strictEqual( hasOwnProp( Float16Array, 'of' ), true, 'has property' ); + t.strictEqual( isFunction( Float16Array.of ), true, 'has method' ); + + arr = Float16Array.of(); + t.strictEqual( arr instanceof Float16Array, true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the method throws an error if invoked with a `this` context which is not a constructor', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + {}, + [] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + return Float16Array.of.call( value, true, true ); + }; + } +}); + +tape( 'the method throws an error if invoked with a `this` context which is not a floating-point array constructor', function test( t ) { + var values; + var i; + + values = [ + {}, + null, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + return Float16Array.of.call( value, true, true ); + }; + } +}); + +tape( 'the method returns a floating-point number array', function test( t ) { + var arr; + var v; + + // No arguments: + arr = Float16Array.of(); + t.strictEqual( arr instanceof Float16Array, true, 'returns expected value' ); + + v = arr.length; + t.strictEqual( v, 0, 'returns expected value' ); + + // Numeric arguments: + arr = Float16Array.of( 1.0, 2.0, 3.0, 4.0 ); + t.strictEqual( arr instanceof Float16Array, true, 'returns expected value' ); + + v = arr.length; + t.strictEqual( v, 4, 'returns expected value' ); + + // Non-numeric arguments: + arr = Float16Array.of( 1, {}, 0, null, 'beep' ); + t.strictEqual( arr instanceof Float16Array, true, 'returns expected value' ); + + v = arr.length; + t.strictEqual( v, 5, 'returns expected value' ); + + // Mixed arguments: + arr = Float16Array.of( true, 1, false, 0 ); + t.strictEqual( arr instanceof Float16Array, true, 'returns expected value' ); + + v = arr.length; + t.strictEqual( v, 4, 'returns expected value' ); + + t.end(); +}); diff --git a/lib/node_modules/@stdlib/array/float16/test/test.reduce.js b/lib/node_modules/@stdlib/array/float16/test/test.reduce.js new file mode 100644 index 000000000000..ad67307272b8 --- /dev/null +++ b/lib/node_modules/@stdlib/array/float16/test/test.reduce.js @@ -0,0 +1,189 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var hasOwnProp = require( '@stdlib/assert/has-own-property' ); +var isFunction = require( '@stdlib/assert/is-function' ); +var float64ToFloat16 = require( '@stdlib/number/float64/base/to-float16' ); +var Float16Array = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof Float16Array, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'attached to the prototype of the main export is a `reduce` method', function test( t ) { + t.strictEqual( hasOwnProp( Float16Array.prototype, 'reduce' ), true, 'has property' ); + t.strictEqual( isFunction( Float16Array.prototype.reduce ), true, 'has method' ); + t.end(); +}); + +tape( 'the method throws an error if invoked with a `this` context which is not a floating-point number array instance', function test( t ) { + var values; + var arr; + var i; + + arr = new Float16Array( 5 ); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + {}, + [], + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + return arr.reduce.call( value, reducer ); + }; + } + + function reducer( acc, value ) { + return float64ToFloat16( acc + value ); + } +}); + +tape( 'the method throws an error if provided a first argument which is not a function', function test( t ) { + var values; + var arr; + var i; + + arr = new Float16Array( 10 ); + + values = [ + '5', + 3.14, + NaN, + true, + false, + null, + void 0, + {}, + [] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + return arr.reduce( value ); + }; + } +}); + +tape( 'the method throws an error if not provided an initial value when operating on an empty floating-point number array', function test( t ) { + var arr; + + arr = new Float16Array( 0 ); + t.throws( foo, Error, 'throws an error' ); + t.end(); + + function foo() { + arr.reduce( reducer ); + } + + function reducer( acc, value ) { + return float64ToFloat16( acc + value ); + } +}); + +tape( 'the method uses the first element of the array as the initial value when an initial value is not provided', function test( t ) { + var valueArray; + var accArray; + var expected; + var actual; + var arr; + var ind; + + arr = new Float16Array( [ 1.0, 2.0, 3.0 ] ); + accArray = [ 1.0, 3.0 ]; + valueArray = [ 2.0, 3.0 ]; + expected = float64ToFloat16( 6.0 ); + actual = arr.reduce( reducer ); + + t.strictEqual( actual, expected, 'returns expected value' ); + + t.end(); + + function reducer( acc, value, index ) { + ind = index-1; + t.strictEqual( acc, float64ToFloat16( accArray[ ind ] ), 'returns expected value' ); + t.strictEqual( value, float64ToFloat16( valueArray[ ind ] ), 'returns expected value' ); + return float64ToFloat16( acc + value ); + } +}); + +tape( 'the method supports providing an initial value as the second argument', function test( t ) { + var valueArray; + var accArray; + var expected; + var actual; + var arr; + + arr = new Float16Array( [ 1.0, 2.0, 3.0 ] ); + accArray = [ 2.0, 3.0, 5.0 ]; + valueArray = [ 1.0, 2.0, 3.0 ]; + expected = 8.0; + actual = arr.reduce( reducer, 2.0 ); + + t.strictEqual( actual, expected, 'returns expected value' ); + t.end(); + + function reducer( acc, value, index ) { + t.strictEqual( acc, float64ToFloat16( accArray[ index ] ), 'returns expected value' ); + t.strictEqual( value, float64ToFloat16( valueArray[ index ] ), 'returns expected value' ); + return float64ToFloat16( acc + value ); + } +}); + +tape( 'the method returns the accumulated result', function test( t ) { + var expected; + var actual; + var arr; + + arr = new Float16Array( [ 1.05, 2.05, 3.05, 4.05 ] ); + expected = float64ToFloat16( 10.20 ); + actual = arr.reduce( reducer ); + + t.strictEqual( actual, expected, 'returns expected value' ); + t.end(); + + function reducer( acc, value ) { + return float64ToFloat16( acc + value ); + } +}); diff --git a/lib/node_modules/@stdlib/array/float16/test/test.reduce_right.js b/lib/node_modules/@stdlib/array/float16/test/test.reduce_right.js new file mode 100644 index 000000000000..167819eecb70 --- /dev/null +++ b/lib/node_modules/@stdlib/array/float16/test/test.reduce_right.js @@ -0,0 +1,192 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var hasOwnProp = require( '@stdlib/assert/has-own-property' ); +var isFunction = require( '@stdlib/assert/is-function' ); +var float64ToFloat16 = require( '@stdlib/number/float64/base/to-float16' ); +var Float16Array = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof Float16Array, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'attached to the prototype of the main export is a `reduceRight` method', function test( t ) { + t.strictEqual( hasOwnProp( Float16Array.prototype, 'reduceRight' ), true, 'has property' ); + t.strictEqual( isFunction( Float16Array.prototype.reduceRight ), true, 'has method' ); + t.end(); +}); + +tape( 'the method throws an error if invoked with a `this` context which is not a floating-point number array instance', function test( t ) { + var values; + var arr; + var i; + + arr = new Float16Array( 5 ); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + {}, + [], + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + return arr.reduceRight.call( value, reducer ); + }; + } + + function reducer( acc, value ) { + return float64ToFloat16( acc + value ); + } +}); + +tape( 'the method throws an error if provided a first argument which is not a function', function test( t ) { + var values; + var arr; + var i; + + arr = new Float16Array( 10 ); + + values = [ + '5', + 3.14, + NaN, + true, + false, + null, + void 0, + {}, + [] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + return arr.reduceRight( value ); + }; + } +}); + +tape( 'the method throws an error if not provided an initial value when operating on an empty floating-point number array', function test( t ) { + var arr; + + arr = new Float16Array( 0 ); + t.throws( foo, Error, 'throws an error' ); + t.end(); + + function foo() { + arr.reduceRight( reducer ); + } + + function reducer( acc, value ) { + return float64ToFloat16( acc + value ); + } +}); + +tape( 'the method uses the last element of the array as the initial value when an initial value is not provided', function test( t ) { + var valueArray; + var accArray; + var expected; + var actual; + var arr; + var len; + + arr = new Float16Array( [ 1.0, 2.0, 3.0 ] ); + len = arr.length; + accArray = [ 3.0, 5.0 ]; + valueArray = [ 2.0, 1.0 ]; + expected = float64ToFloat16( 6.0 ); + actual = arr.reduceRight( reducer ); + + t.strictEqual( actual, expected, 'returns expected value' ); + t.end(); + + function reducer( acc, value, index ) { + var ind = (len-index-2); + t.strictEqual( acc, float64ToFloat16( accArray[ ind ] ), 'returns expected value' ); + t.strictEqual( value, float64ToFloat16( valueArray[ ind ] ), 'returns expected value' ); + return float64ToFloat16( acc + value ); + } +}); + +tape( 'the method supports providing an initial value as the second argument', function test( t ) { + var valueArray; + var accArray; + var expected; + var actual; + var arr; + var len; + + arr = new Float16Array( [ 1.0, 2.0, 3.0 ] ); + len = arr.length; + accArray = [ 2.0, 5.0, 7.0 ]; + valueArray = [ 3.0, 2.0, 1.0 ]; + expected = 8.0; + actual = arr.reduceRight( reducer, 2.0 ); + + t.strictEqual( actual, expected, 'returns expected value' ); + t.end(); + + function reducer( acc, value, index ) { + var ind = (len-index-1); + t.strictEqual( acc, float64ToFloat16( accArray[ ind ] ), 'returns expected value' ); + t.strictEqual( value, float64ToFloat16( valueArray[ ind ] ), 'returns expected value' ); + return float64ToFloat16( acc + value ); + } +}); + +tape( 'the method returns the accumulated result', function test( t ) { + var expected; + var actual; + var arr; + + arr = new Float16Array( [ 1.05, 2.05, 3.05, 4.05 ] ); + expected = float64ToFloat16( 10.20 ); + actual = arr.reduceRight( reducer ); + + t.strictEqual( actual, expected, 'returns expected value' ); + t.end(); + + function reducer( acc, value ) { + return float64ToFloat16( acc + value ); + } +}); diff --git a/lib/node_modules/@stdlib/array/float16/test/test.reverse.js b/lib/node_modules/@stdlib/array/float16/test/test.reverse.js new file mode 100644 index 000000000000..25467af0a12c --- /dev/null +++ b/lib/node_modules/@stdlib/array/float16/test/test.reverse.js @@ -0,0 +1,122 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var hasOwnProp = require( '@stdlib/assert/has-own-property' ); +var isFunction = require( '@stdlib/assert/is-function' ); +var instanceOf = require( '@stdlib/assert/instance-of' ); +var Uint16Array = require( '@stdlib/array/uint16' ); +var toWord = require( '@stdlib/number/float16/base/to-word' ); +var Float16Array = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof Float16Array, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'attached to the prototype of the main export is a `reverse` method', function test( t ) { + t.strictEqual( hasOwnProp( Float16Array.prototype, 'reverse' ), true, 'has property' ); + t.strictEqual( isFunction( Float16Array.prototype.reverse ), true, 'has method' ); + t.end(); +}); + +tape( 'the method throws an error if invoked with a `this` context which is not a floating-point number array instance', function test( t ) { + var values; + var arr; + var i; + + arr = new Float16Array( 5 ); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + {}, + [], + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + return arr.reverse.call( value ); + }; + } +}); + +tape( 'the method returns an empty array if operating on an empty floating-point number array', function test( t ) { + var arr; + var out; + + arr = new Float16Array(); + out = arr.reverse(); + + t.strictEqual( out.buffer, arr.buffer, 'returns expected value' ); + t.strictEqual( out.length, 0, 'returns expected value' ); + t.end(); +}); + +tape( 'the method reverses elements of a floating-point number array in-place', function test( t ) { + var expected; + var arr; + var buf; + var out; + + arr = new Float16Array( [ 1.05, 2.05, 3.05, 4.05, 5.05 ] ); + expected = new Uint16Array([ + toWord( 5.05 ), + toWord( 4.05 ), + toWord( 3.05 ), + toWord( 2.05 ), + toWord( 1.05 ) + ]); + out = arr.reverse(); + buf = new Uint16Array( arr.buffer ); + + t.strictEqual( instanceOf( arr, Float16Array ), true, 'returns expected value' ); + t.strictEqual( out, arr, 'returns expected value' ); + t.strictEqual( arr.length, expected.length, 'returns expected value' ); + t.deepEqual( buf, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the method does not change the array length', function test( t ) { + var arr; + var out; + + arr = new Float16Array( 10 ); + out = arr.reverse(); + + t.strictEqual( out.length, 10, 'returns expected value' ); + t.end(); +}); diff --git a/lib/node_modules/@stdlib/array/float16/test/test.set.js b/lib/node_modules/@stdlib/array/float16/test/test.set.js new file mode 100644 index 000000000000..419705ba4930 --- /dev/null +++ b/lib/node_modules/@stdlib/array/float16/test/test.set.js @@ -0,0 +1,389 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var hasOwnProp = require( '@stdlib/assert/has-own-property' ); +var isFunction = require( '@stdlib/assert/is-function' ); +var float64ToFloat16 = require( '@stdlib/number/float64/base/to-float16' ); +var toWord = require( '@stdlib/number/float16/base/to-word' ); +var ArrayBuffer = require( '@stdlib/array/buffer' ); +var Uint16Array = require( '@stdlib/array/uint16' ); +var Float16Array = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof Float16Array, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'attached to the prototype of the main export is a `set` method for setting one or more array elements', function test( t ) { + t.strictEqual( hasOwnProp( Float16Array.prototype, 'set' ), true, 'has property' ); + t.strictEqual( isFunction( Float16Array.prototype.set ), true, 'has method' ); + t.end(); +}); + +tape( 'the method throws an error if invoked with a `this` context which is not a floating-point number array instance', function test( t ) { + var values; + var arr; + var i; + + arr = new Float16Array( 5 ); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + {}, + [] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + return arr.set.call( value, 0 ); + }; + } +}); + +tape( 'the method throws an error if provided an index argument which is not a nonnegative integer', function test( t ) { + var values; + var arr; + var i; + + arr = new Float16Array( 10 ); + + values = [ + '5', + -5, + 3.14, + NaN, + true, + false, + null, + void 0, + {}, + [], + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + return arr.set( 1.0, value ); + }; + } +}); + +tape( 'the method throws an error if provided an index argument which is out-of-bounds (non-array)', function test( t ) { + var values; + var arr; + var i; + + arr = new Float16Array( 10 ); + + values = [ + arr.length, + arr.length + 1, + arr.length + 2, + arr.length + 3 + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), RangeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + return arr.set( 1.0, value ); + }; + } +}); + +tape( 'the method throws an error if provided an index argument which is out-of-bounds (array-like object)', function test( t ) { + var values; + var arr1; + var arr2; + var i; + + arr1 = new Float16Array( 10 ); + arr2 = []; + for ( i = 0; i < arr1.length; i++ ) { + arr2.push( 1.0 ); + } + + values = [ + 1, + 2, + 3, + 4, + 5 + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), RangeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + return arr1.set( arr2, value ); + }; + } +}); + +tape( 'the method sets an array element (non-array)', function test( t ) { + var arr; + var v; + var i; + + arr = new Float16Array( 10 ); + + v = arr.get( 0 ); + t.strictEqual( v, 0.0, 'returns expected value' ); + + // No index argument: + arr.set( 1.0 ); + + v = arr.get( 0 ); + t.strictEqual( v, 1.0, 'returns expected value' ); + + arr.set( 0.0 ); + + // Index argument: + for ( i = 0; i < arr.length; i++ ) { + v = arr.get( i ); + t.strictEqual( v, 0.0, 'returns expected value' ); + + arr.set( 1.05, i ); + v = arr.get( i ); + t.strictEqual( v, float64ToFloat16( 1.05 ), 'returns expected value' ); + } + t.end(); +}); + +tape( 'the method sets an array element (array-like object)', function test( t ) { + var arr; + var v; + var i; + + arr = new Float16Array( 10 ); + + v = arr.get( 0 ); + t.strictEqual( v, 0.0, 'returns expected value' ); + + // No index argument: + arr.set( [ 1.0 ] ); + + v = arr.get( 0 ); + t.strictEqual( v, 1.0, 'returns expected value' ); + + arr.set( [ 0.0 ] ); + + // Index argument: + for ( i = 0; i < arr.length; i++ ) { + v = arr.get( i ); + t.strictEqual( v, 0.0, 'returns expected value' ); + + arr.set( [ 1.05 ], i ); + + v = arr.get( i ); + t.strictEqual( v, float64ToFloat16( 1.05 ), 'returns expected value' ); + } + + // Multiple values, no index argument: + arr.set( [ 1.05, 2.05 ] ); + + v = arr.get( 0 ); + t.strictEqual( v, float64ToFloat16( 1.05 ), 'returns expected value' ); + + v = arr.get( 1 ); + t.strictEqual( v, float64ToFloat16( 2.05 ), 'returns expected value' ); + + // Multiple values, index argument: + arr.set( [ 1.05, 2.05 ], 2 ); + + v = arr.get( 2 ); + t.strictEqual( v, float64ToFloat16( 1.05 ), 'returns expected value' ); + + v = arr.get( 3 ); + t.strictEqual( v, float64ToFloat16( 2.05 ), 'returns expected value' ); + + t.end(); +}); + +tape( 'the method sets an array element (typed array; shared buffer)', function test( t ) { + var byteOffset; + var arr; + var src; + var buf; + var ab; + var v; + var i; + + byteOffset = 112; + + ab = new ArrayBuffer( 240 ); + arr = new Float16Array( ab, byteOffset, 10 ); + + // Overlapping (requires copy), multiple values, no index argument: + buf = [ 1.05, 2.05 ]; + src = new Uint16Array( ab, byteOffset-(1*arr.BYTES_PER_ELEMENT), 2 ); + for ( i = 0; i < buf.length; i++ ) { + src[ i ] = toWord( buf[ i ] ); + } + arr.set( src ); + + v = arr.get( 0 ); + t.strictEqual( v, float64ToFloat16( buf[ 0 ] ), 'returns expected value' ); + + v = arr.get( 1 ); + t.strictEqual( v, float64ToFloat16( buf[ 1 ] ), 'returns expected value' ); + + // Overlapping (requires copy), multiple values, index argument: + buf = [ -1.05, -2.05 ]; + src = new Uint16Array( ab, byteOffset+(1*arr.BYTES_PER_ELEMENT), 2 ); + for ( i = 0; i < buf.length; i++ ) { + src[ i ] = toWord( buf[ i ] ); + } + arr.set( src, 2 ); + + v = arr.get( 2 ); + t.strictEqual( v, float64ToFloat16( buf[ 0 ] ), 'returns expected value' ); + + v = arr.get( 3 ); + t.strictEqual( v, float64ToFloat16( buf[ 1 ] ), 'returns expected value' ); + + // Overlapping (no copy), multiple values, no index argument: + buf = [ 3.05, 4.05 ]; + src = new Uint16Array( ab, byteOffset+(1*arr.BYTES_PER_ELEMENT), 2 ); + for ( i = 0; i < buf.length; i++ ) { + src[ i ] = toWord( buf[ i ] ); + } + arr.set( src ); + + v = arr.get( 0 ); + t.strictEqual( v, float64ToFloat16( buf[ 0 ] ), 'returns expected value' ); + + v = arr.get( 1 ); + t.strictEqual( v, float64ToFloat16( buf[ 1 ] ), 'returns expected value' ); + + // Overlapping (no copy), multiple values, index argument: + buf = [ -3.05, -4.05 ]; + src = new Uint16Array( ab, byteOffset+(3*arr.BYTES_PER_ELEMENT), 2 ); + for ( i = 0; i < buf.length; i++ ) { + src[ i ] = toWord( buf[ i ] ); + } + arr.set( src, 2 ); + + v = arr.get( 2 ); + t.strictEqual( v, float64ToFloat16( buf[ 0 ] ), 'returns expected value' ); + + v = arr.get( 3 ); + t.strictEqual( v, float64ToFloat16( buf[ 1 ] ), 'returns expected value' ); + + t.end(); +}); + +tape( 'the method sets an array element (floating-point number array; shared buffer)', function test( t ) { + var byteOffset; + var arr; + var src; + var buf; + var ab; + var v; + var i; + + byteOffset = 112; + + ab = new ArrayBuffer( 240 ); + arr = new Float16Array( ab, byteOffset, 10 ); + + // Overlapping (requires copy), multiple values, no index argument: + buf = [ 1.05, 2.05 ]; + src = new Float16Array( ab, byteOffset-(1*arr.BYTES_PER_ELEMENT), 2 ); + for ( i = 0; i < buf.length; i++ ) { + src.set( buf[ i ], i ); + } + arr.set( src ); + + v = arr.get( 0 ); + t.strictEqual( v, float64ToFloat16( buf[ 0 ] ), 'returns expected value' ); + + v = arr.get( 1 ); + t.strictEqual( v, float64ToFloat16( buf[ 1 ] ), 'returns expected value' ); + + // Overlapping (requires copy), multiple values, index argument: + buf = [ -1.05, -2.05 ]; + src = new Float16Array( ab, byteOffset+(1*arr.BYTES_PER_ELEMENT), 2 ); + for ( i = 0; i < buf.length; i++ ) { + src.set( buf[ i ], i ); + } + arr.set( src, 2 ); + + v = arr.get( 2 ); + t.strictEqual( v, float64ToFloat16( buf[ 0 ] ), 'returns expected value' ); + + v = arr.get( 3 ); + t.strictEqual( v, float64ToFloat16( buf[ 1 ] ), 'returns expected value' ); + + // Overlapping (no copy), multiple values, no index argument: + buf = [ 3.05, 4.05 ]; + src = new Float16Array( ab, byteOffset+(1*arr.BYTES_PER_ELEMENT), 2 ); + for ( i = 0; i < buf.length; i++ ) { + src.set( buf[ i ], i ); + } + arr.set( src ); + + v = arr.get( 0 ); + t.strictEqual( v, float64ToFloat16( buf[ 0 ] ), 'returns expected value' ); + + v = arr.get( 1 ); + t.strictEqual( v, float64ToFloat16( buf[ 1 ] ), 'returns expected value' ); + + // Overlapping (no copy), multiple values, index argument: + buf = [ -3.05, -4.05 ]; + src = new Float16Array( ab, byteOffset+(3*arr.BYTES_PER_ELEMENT), 2 ); + for ( i = 0; i < buf.length; i++ ) { + src.set( buf[ i ], i ); + } + arr.set( src, 2 ); + + v = arr.get( 2 ); + t.strictEqual( v, float64ToFloat16( buf[ 0 ] ), 'returns expected value' ); + + v = arr.get( 3 ); + t.strictEqual( v, float64ToFloat16( buf[ 1 ] ), 'returns expected value' ); + + t.end(); +}); diff --git a/lib/node_modules/@stdlib/array/float16/test/test.slice.js b/lib/node_modules/@stdlib/array/float16/test/test.slice.js new file mode 100644 index 000000000000..0b4c866c51db --- /dev/null +++ b/lib/node_modules/@stdlib/array/float16/test/test.slice.js @@ -0,0 +1,311 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var hasOwnProp = require( '@stdlib/assert/has-own-property' ); +var isFunction = require( '@stdlib/assert/is-function' ); +var instanceOf = require( '@stdlib/assert/instance-of' ); +var toWord = require( '@stdlib/number/float16/base/to-word' ); +var Uint16Array = require( '@stdlib/array/uint16' ); +var Float16Array = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof Float16Array, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'attached to the prototype of the main export is a `slice` method', function test( t ) { + t.strictEqual( hasOwnProp( Float16Array.prototype, 'slice' ), true, 'has property' ); + t.strictEqual( isFunction( Float16Array.prototype.slice ), true, 'has method' ); + t.end(); +}); + +tape( 'the method throws an error if invoked with a `this` context which is not a floating-point number array instance', function test( t ) { + var values; + var arr; + var i; + + arr = new Float16Array( 5 ); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + {}, + [], + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + return arr.slice.call( value ); + }; + } +}); + +tape( 'the method throws an error if provided a first argument which is not an integer', function test( t ) { + var values; + var arr; + var i; + + arr = new Float16Array( 10 ); + + values = [ + '5', + 3.14, + NaN, + true, + false, + null, + void 0, + {}, + [] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + return arr.slice( value ); + }; + } +}); + +tape( 'the method throws an error if provided a second argument which is not an integer', function test( t ) { + var values; + var arr; + var i; + + arr = new Float16Array( 10 ); + + values = [ + '5', + 3.14, + NaN, + true, + false, + null, + void 0, + {}, + [] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + return arr.slice( 0, value ); + }; + } +}); + +tape( 'the method returns an empty typed array if operating on an empty floating-point number array', function test( t ) { + var arr; + var out; + + arr = new Float16Array(); + out = arr.slice(); + + t.strictEqual( out.length, 0, 'returns expected value' ); + t.end(); +}); + +tape( 'if called without arguments, the method returns a typed array containing the same elements as the original array', function test( t ) { + var expected; + var actual; + var arr; + var buf; + + arr = new Float16Array( [ 1.05, 2.05, 3.05, 4.05 ] ); + expected = new Uint16Array([ + toWord( 1.05 ), + toWord( 2.05 ), + toWord( 3.05 ), + toWord( 4.05 ) + ]); + actual = arr.slice(); + buf = new Uint16Array( actual.buffer ); + + t.strictEqual( instanceOf( actual, Float16Array ), true, 'returns expected value' ); + t.notEqual( actual, arr, 'returns a new instance' ); + t.strictEqual( actual.length, expected.length, 'returns expected value' ); + t.deepEqual( buf, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'if called with one argument, the method returns a typed array containing elements starting from a specified beginning index (inclusive)', function test( t ) { + var expected; + var actual; + var arr; + var buf; + + arr = new Float16Array( [ 1.05, 2.05, 3.05, 4.05 ] ); + expected = new Uint16Array([ + toWord( 2.05 ), + toWord( 3.05 ), + toWord( 4.05 ) + ]); + actual = arr.slice( 1 ); + buf = new Uint16Array( actual.buffer ); + + t.strictEqual( instanceOf( actual, Float16Array ), true, 'returns expected value' ); + t.strictEqual( actual.length, expected.length, 'returns expected value' ); + t.deepEqual( buf, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'if provided two arguments, the method returns a typed array containing elements starting from a specified beginning index (inclusive) and ending at a specified stop index (exclusive)', function test( t ) { + var expected; + var actual; + var arr; + var buf; + + arr = new Float16Array( [ 1.05, 2.05, 3.05, 4.05 ] ); + expected = new Uint16Array([ + toWord( 2.05 ), + toWord( 3.05 ) + ]); + actual = arr.slice( 1, 3 ); + buf = new Uint16Array( actual.buffer ); + + t.strictEqual( instanceOf( actual, Float16Array ), true, 'returns expected value' ); + t.strictEqual( actual.length, expected.length, 'returns expected value' ); + t.deepEqual( buf, expected, 'returns expected value' ); + + arr = new Float16Array( [ 1.05, 2.05, 3.05, 4.05 ] ); + expected = new Uint16Array([ + toWord( 2.05 ), + toWord( 3.05 ), + toWord( 4.05 ) + ]); + actual = arr.slice( 1, 30 ); + buf = new Uint16Array( actual.buffer ); + + t.strictEqual( instanceOf( actual, Float16Array ), true, 'returns expected value' ); + t.strictEqual( actual.length, expected.length, 'returns expected value' ); + t.deepEqual( buf, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the method resolves negative indices relative to the last element', function test( t ) { + var expected; + var actual; + var arr; + var buf; + + arr = new Float16Array( [ 1.05, 2.05, 3.05, 4.05 ] ); + expected = new Uint16Array([ + toWord( 2.05 ), + toWord( 3.05 ) + ]); + actual = arr.slice( -3, -1 ); + buf = new Uint16Array( actual.buffer ); + + t.strictEqual( instanceOf( actual, Float16Array ), true, 'returns expected value' ); + t.strictEqual( actual.length, expected.length, 'returns expected value' ); + t.deepEqual( buf, expected, 'returns expected value' ); + + expected = new Uint16Array([ + toWord( 2.05 ), + toWord( 3.05 ) + ]); + actual = arr.slice( -30, -2 ); + + t.strictEqual( instanceOf( actual, Float16Array ), true, 'returns expected value' ); + t.strictEqual( actual.length, expected.length, 'returns expected value' ); + t.deepEqual( buf, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the method returns an empty typed array if a resolved beginning index exceeds a resolved ending index', function test( t ) { + var expected; + var actual; + var arr; + var buf; + + arr = new Float16Array( [ 1.05, 2.05, 3.05, 4.05 ] ); + expected = new Uint16Array( [] ); + actual = arr.slice( 2, 0 ); + buf = new Uint16Array( actual.buffer ); + + t.strictEqual( instanceOf( actual, Float16Array ), true, 'returns expected value' ); + t.strictEqual( actual.length, expected.length, 'returns expected value' ); + t.deepEqual( buf, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the method returns an empty typed array if a resolved beginning index exceeds the maximum array index', function test( t ) { + var expected; + var actual; + var arr; + var buf; + + arr = new Float16Array( [ 1.05, 2.05, 3.05, 4.05 ] ); + expected = new Uint16Array( [] ); + actual = arr.slice( 5 ); + buf = new Uint16Array( actual.buffer ); + + t.strictEqual( instanceOf( actual, Float16Array ), true, 'returns expected value' ); + t.strictEqual( actual.length, expected.length, 'returns expected value' ); + t.deepEqual( buf, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the method returns an empty typed array if a resolved ending index is less than or equal to zero', function test( t ) { + var expected; + var actual; + var arr; + var buf; + + arr = new Float16Array( [ 1.0, -1.0, 2.0, -2.0, 3.0, -3.0, 4.0, -4.0 ] ); + expected = new Uint16Array( [] ); + actual = arr.slice( 2, -8 ); + buf = new Uint16Array( actual.buffer ); + + t.strictEqual( instanceOf( actual, Float16Array ), true, 'returns expected value' ); + t.strictEqual( actual.length, expected.length/2, 'returns expected value' ); + t.deepEqual( buf, expected, 'returns expected value' ); + + actual = arr.slice( 1, 0 ); + buf = new Uint16Array( actual.buffer ); + + t.strictEqual( instanceOf( actual, Float16Array ), true, 'returns expected value' ); + t.strictEqual( actual.length, expected.length/2, 'returns expected value' ); + t.deepEqual( buf, expected, 'returns expected value' ); + t.end(); +}); diff --git a/lib/node_modules/@stdlib/array/float16/test/test.some.js b/lib/node_modules/@stdlib/array/float16/test/test.some.js new file mode 100644 index 000000000000..cd1f5b2e02ab --- /dev/null +++ b/lib/node_modules/@stdlib/array/float16/test/test.some.js @@ -0,0 +1,196 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var hasOwnProp = require( '@stdlib/assert/has-own-property' ); +var isFunction = require( '@stdlib/assert/is-function' ); +var float64ToFloat16 = require( '@stdlib/number/float64/base/to-float16' ); +var Float16Array = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof Float16Array, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'attached to the prototype of the main export is a `some` method for returning boolean indicating whether at least one element passes a test', function test( t ) { + t.strictEqual( hasOwnProp( Float16Array.prototype, 'some' ), true, 'has property' ); + t.strictEqual( isFunction( Float16Array.prototype.some ), true, 'has method' ); + t.end(); +}); + +tape( 'the method throws an error if invoked with a `this` context which is not a floating-point number array instance', function test( t ) { + var values; + var arr; + var i; + + arr = new Float16Array( 5 ); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + {}, + [], + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + return arr.some.call( value, predicate ); + }; + } + + function predicate( v ) { + return v === 1.0; + } +}); + +tape( 'the method throws an error if provided a first argument which is not a function', function test( t ) { + var values; + var arr; + var i; + + arr = new Float16Array( 10 ); + + values = [ + '5', + 3.14, + NaN, + true, + false, + null, + void 0, + {}, + [] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + return arr.some( value ); + }; + } +}); + +tape( 'the method returns `false` if operating on an empty floating-point number array', function test( t ) { + var bool; + var arr; + + arr = new Float16Array(); + bool = arr.some( predicate ); + + t.strictEqual( bool, false, 'returns expected value' ); + t.end(); + + function predicate() { + t.fail( 'should not be invoked' ); + } +}); + +tape( 'the method returns `true` if at least one element passes a test', function test( t ) { + var bool; + var arr; + + arr = new Float16Array( [ 1.05, 2.05, 3.05, 4.05 ] ); + bool = arr.some( predicate ); + + t.strictEqual( bool, true, 'returns expected value' ); + t.end(); + + function predicate( v ) { + return v === float64ToFloat16( 1.05 ); + } +}); + +tape( 'the method returns `false` if all elements fail a test', function test( t ) { + var bool; + var arr; + + arr = new Float16Array( [ 1.05, 2.05, 3.05, 4.05 ] ); + bool = arr.some( predicate ); + + t.strictEqual( bool, false, 'returns expected value' ); + t.end(); + + function predicate( v ) { + return v === float64ToFloat16( 5.05 ); + } +}); + +tape( 'the method supports providing an execution context', function test( t ) { + var bool; + var ctx; + var arr; + + ctx = { + 'count': 0 + }; + arr = new Float16Array( [ 1.05, 2.05, 3.05, 4.05 ] ); + bool = arr.some( predicate, ctx ); + + t.strictEqual( bool, true, 'returns expected value' ); + t.strictEqual( ctx.count, 4, 'returns expected value' ); + + t.end(); + + function predicate( v ) { + this.count += 1; // eslint-disable-line no-invalid-this + return v === float64ToFloat16( 4.05 ); + } +}); + +tape( 'the method stops executing upon encountering the first element which passes a test', function test( t ) { + var bool; + var ctx; + var arr; + + ctx = { + 'count': 0 + }; + arr = new Float16Array( [ 1.05, 2.05, 3.05, 4.05 ] ); + bool = arr.some( predicate, ctx ); + + t.strictEqual( bool, true, 'returns expected value' ); + t.strictEqual( ctx.count, 1, 'returns expected value' ); + + t.end(); + + function predicate( v ) { + this.count += 1; // eslint-disable-line no-invalid-this + return v === float64ToFloat16( 1.05 ); + } +}); diff --git a/lib/node_modules/@stdlib/array/float16/test/test.sort.js b/lib/node_modules/@stdlib/array/float16/test/test.sort.js new file mode 100644 index 000000000000..77912c87b8fa --- /dev/null +++ b/lib/node_modules/@stdlib/array/float16/test/test.sort.js @@ -0,0 +1,173 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var hasOwnProp = require( '@stdlib/assert/has-own-property' ); +var isFunction = require( '@stdlib/assert/is-function' ); +var instanceOf = require( '@stdlib/assert/instance-of' ); +var toWord = require( '@stdlib/number/float16/base/to-word' ); +var Uint16Array = require( '@stdlib/array/uint16' ); +var Float16Array = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Comparison function. +* +* @private +* @param {boolean} a - first value for comparison +* @param {boolean} b - second value for comparison +* @returns {number} comparison result +*/ +function compareFcn( a, b ) { + if ( a < b ) { + return -1; + } + if ( a > b ) { + return 1; + } + return 0; +} + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof Float16Array, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'attached to the prototype of the main export is a `sort` method', function test( t ) { + t.strictEqual( hasOwnProp( Float16Array.prototype, 'sort' ), true, 'has property' ); + t.strictEqual( isFunction( Float16Array.prototype.sort ), true, 'has method' ); + t.end(); +}); + +tape( 'the method throws an error if invoked with a `this` context which is not a floating-point number array instance', function test( t ) { + var values; + var arr; + var i; + + arr = new Float16Array( 5 ); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + {}, + [], + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + return arr.sort.call( value, compareFcn ); + }; + } +}); + +tape( 'the method throws an error if provided a first argument which is not a function', function test( t ) { + var values; + var arr; + var i; + + arr = new Float16Array( 10 ); + + values = [ + '5', + 3.14, + NaN, + true, + false, + null, + void 0, + {}, + [] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + return arr.sort( value ); + }; + } +}); + +tape( 'the method returns an empty array if operating on an empty floating-point number array', function test( t ) { + var arr; + var out; + + arr = new Float16Array(); + out = arr.sort( compareFcn ); + + t.strictEqual( out.buffer, arr.buffer, 'returns expected value' ); + t.strictEqual( out.length, 0, 'returns expected value' ); + t.end(); +}); + +tape( 'the method sorts elements of a floating-point number array in-place', function test( t ) { + var expected; + var arr; + var out; + var buf; + + arr = new Float16Array( [ 4.05, 3.05, 1.05, 5.05, 2.05 ] ); + expected = new Uint16Array([ + toWord( 1.05 ), + toWord( 2.05 ), + toWord( 3.05 ), + toWord( 4.05 ), + toWord( 5.05 ) + ]); + out = arr.sort( compareFcn ); + buf = new Uint16Array( out.buffer ); + + t.strictEqual( instanceOf( arr, Float16Array ), true, 'returns expected value' ); + t.strictEqual( out, arr, 'returns expected value' ); + t.strictEqual( arr.length, expected.length, 'returns expected value' ); + t.deepEqual( buf, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the method does not change the array length', function test( t ) { + var arr; + var out; + + arr = new Float16Array( [ 4.05, 3.05, 1.05, 5.05, 2.05 ] ); + out = arr.sort( compareFcn ); + + t.strictEqual( out.length, arr.length, 'returns expected value' ); + t.end(); +}); diff --git a/lib/node_modules/@stdlib/array/float16/test/test.subarray.js b/lib/node_modules/@stdlib/array/float16/test/test.subarray.js new file mode 100644 index 000000000000..1c81bcf25721 --- /dev/null +++ b/lib/node_modules/@stdlib/array/float16/test/test.subarray.js @@ -0,0 +1,308 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var hasOwnProp = require( '@stdlib/assert/has-own-property' ); +var isFunction = require( '@stdlib/assert/is-function' ); +var instanceOf = require( '@stdlib/assert/instance-of' ); +var toWord = require( '@stdlib/number/float16/base/to-word' ); +var Uint16Array = require( '@stdlib/array/uint16' ); +var Float16Array = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof Float16Array, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'attached to the prototype of the main export is a `subarray` method', function test( t ) { + t.strictEqual( hasOwnProp( Float16Array.prototype, 'subarray' ), true, 'has property' ); + t.strictEqual( isFunction( Float16Array.prototype.subarray ), true, 'has method' ); + t.end(); +}); + +tape( 'the method throws an error if invoked with a `this` context which is not a floating-point number array instance', function test( t ) { + var values; + var arr; + var i; + + arr = new Float16Array( 5 ); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + {}, + [], + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + return arr.subarray.call( value ); + }; + } +}); + +tape( 'the method throws an error if provided a first argument which is not an integer', function test( t ) { + var values; + var arr; + var i; + + arr = new Float16Array( 10 ); + + values = [ + '5', + 3.14, + NaN, + true, + false, + null, + void 0, + {}, + [] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + return arr.subarray( value ); + }; + } +}); + +tape( 'the method throws an error if provided a second argument which is not an integer', function test( t ) { + var values; + var arr; + var i; + + arr = new Float16Array( 10 ); + + values = [ + '5', + 3.14, + NaN, + true, + false, + null, + void 0, + {}, + [] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + return arr.subarray( 0, value ); + }; + } +}); + +tape( 'the method returns empty array if operating on an empty floating-point number array', function test( t ) { + var arr; + var out; + + arr = new Float16Array(); + out = arr.subarray(); + + t.strictEqual( out.buffer, arr.buffer, 'returns expected value' ); + t.strictEqual( out.length, 0, 'returns expected value' ); + t.end(); +}); + +tape( 'if called without arguments, the method returns a view containing the same elements as the original array', function test( t ) { + var expected; + var actual; + var arr; + var buf; + + arr = new Float16Array( [ 1.05, 2.05, 3.05, 4.05 ] ); + expected = new Uint16Array([ + toWord( 1.05 ), + toWord( 2.05 ), + toWord( 3.05 ), + toWord( 4.05 ) + ]); + actual = arr.subarray(); + buf = new Uint16Array( actual.buffer ); + + t.strictEqual( actual.buffer, arr.buffer, 'returns expected value' ); + t.strictEqual( instanceOf( actual, Float16Array ), true, 'returns expected value' ); + t.strictEqual( actual.length, expected.length, 'returns expected value' ); + t.deepEqual( buf, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'if called with one argument, the method returns a view containing elements starting from a specified beginning index (inclusive)', function test( t ) { + var expected; + var actual; + var arr; + var buf; + + arr = new Float16Array( [ 1.05, 2.05, 3.05, 4.05 ] ); + expected = new Uint16Array([ + toWord( 2.05 ), + toWord( 3.05 ), + toWord( 4.05 ) + ]); + actual = arr.subarray( 1 ); + buf = new Uint16Array( actual.buffer, actual.byteOffset, actual.length ); + + t.strictEqual( actual.buffer, arr.buffer, 'returns expected value' ); + t.strictEqual( instanceOf( actual, Float16Array ), true, 'returns expected value' ); + t.strictEqual( actual.length, expected.length, 'returns expected value' ); + t.deepEqual( buf, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'if provided two arguments, the method returns a view containing elements starting from a specified beginning index (inclusive) and ending at a specified stop index (exclusive)', function test( t ) { + var expected; + var actual; + var arr; + var buf; + + arr = new Float16Array( [ 1.05, 2.05, 3.05, 4.05 ] ); + expected = new Uint16Array([ + toWord( 2.05 ), + toWord( 3.05 ) + ]); + actual = arr.subarray( 1, 3 ); + buf = new Uint16Array( actual.buffer, actual.byteOffset, actual.length ); + + t.strictEqual( actual.buffer, arr.buffer, 'returns expected value' ); + t.strictEqual( instanceOf( actual, Float16Array ), true, 'returns expected value' ); + t.strictEqual( actual.length, expected.length, 'returns expected value' ); + t.deepEqual( buf, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the method resolves negative indices relative to the last element', function test( t ) { + var expected; + var actual; + var arr; + var buf; + + arr = new Float16Array( [ 1.05, 2.05, 3.05, 4.05 ] ); + expected = new Uint16Array([ + toWord( 2.05 ), + toWord( 3.05 ) + ]); + actual = arr.subarray( -3, -1 ); + buf = new Uint16Array( actual.buffer, actual.byteOffset, actual.length ); + + t.strictEqual( actual.buffer, arr.buffer, 'returns expected value' ); + t.strictEqual( instanceOf( actual, Float16Array ), true, 'returns expected value' ); + t.strictEqual( actual.length, expected.length, 'returns expected value' ); + t.deepEqual( buf, expected, 'returns expected value' ); + + expected = new Uint16Array([ + toWord( 1.05 ), + toWord( 2.05 ) + ]); + actual = arr.subarray( -30, -2 ); + buf = new Uint16Array( actual.buffer, actual.byteOffset, actual.length ); + + t.strictEqual( actual.buffer, arr.buffer, 'returns expected value' ); + t.strictEqual( instanceOf( actual, Float16Array ), true, 'returns expected value' ); + t.strictEqual( actual.length, expected.length, 'returns expected value' ); + t.deepEqual( buf, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the method returns an empty view if a resolved beginning index exceeds a resolved ending index', function test( t ) { + var expected; + var actual; + var arr; + var buf; + + arr = new Float16Array( [ 1.05, 2.05, 3.05, 4.05 ] ); + expected = new Uint16Array( [] ); + actual = arr.subarray( 2, 0 ); + buf = new Uint16Array( actual.buffer, actual.byteOffset, actual.length ); + + t.strictEqual( actual.buffer, arr.buffer, 'returns expected value' ); + t.strictEqual( instanceOf( actual, Float16Array ), true, 'returns expected value' ); + t.strictEqual( actual.length, expected.length, 'returns expected value' ); + t.deepEqual( buf, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the method returns an empty view if a resolved beginning index exceeds the maximum array index', function test( t ) { + var expected; + var actual; + var arr; + var buf; + + arr = new Float16Array( [ 1.05, 2.05, 3.05, 4.05 ] ); + expected = new Uint16Array( [] ); + actual = arr.subarray( 5 ); + buf = new Uint16Array( actual.buffer, actual.byteOffset, actual.length ); + + t.strictEqual( actual.buffer, arr.buffer, 'returns expected value' ); + t.strictEqual( instanceOf( actual, Float16Array ), true, 'returns expected value' ); + t.strictEqual( actual.length, expected.length, 'returns expected value' ); + t.deepEqual( buf, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the method returns an empty view if a resolved ending index is less than or equal to zero', function test( t ) { + var expected; + var actual; + var arr; + var buf; + + arr = new Float16Array( [ 1.05, 2.05, 3.05, 4.05 ] ); + expected = new Uint16Array( [] ); + actual = arr.subarray( 2, -8 ); + buf = new Uint16Array( actual.buffer, actual.byteOffset, actual.length ); + + t.strictEqual( actual.buffer, arr.buffer, 'returns expected value' ); + t.strictEqual( instanceOf( actual, Float16Array ), true, 'returns expected value' ); + t.strictEqual( actual.length, expected.length, 'returns expected value' ); + t.deepEqual( buf, expected, 'returns expected value' ); + + actual = arr.subarray( 1, 0 ); + buf = new Uint16Array( actual.buffer, actual.byteOffset, actual.length ); + + t.strictEqual( actual.buffer, arr.buffer, 'returns expected value' ); + t.strictEqual( instanceOf( actual, Float16Array ), true, 'returns expected value' ); + t.strictEqual( actual.length, expected.length, 'returns expected value' ); + t.deepEqual( buf, expected, 'returns expected value' ); + t.end(); +}); diff --git a/lib/node_modules/@stdlib/array/float16/test/test.to_locale_string.js b/lib/node_modules/@stdlib/array/float16/test/test.to_locale_string.js new file mode 100644 index 000000000000..dd89766d8b40 --- /dev/null +++ b/lib/node_modules/@stdlib/array/float16/test/test.to_locale_string.js @@ -0,0 +1,187 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var hasOwnProp = require( '@stdlib/assert/has-own-property' ); +var isFunction = require( '@stdlib/assert/is-function' ); +var Float16Array = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof Float16Array, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'attached to the prototype of the main export is a `toLocaleString` method', function test( t ) { + t.strictEqual( hasOwnProp( Float16Array.prototype, 'toLocaleString' ), true, 'has property' ); + t.strictEqual( isFunction( Float16Array.prototype.toLocaleString ), true, 'has method' ); + t.end(); +}); + +tape( 'the method throws an error if invoked with a `this` context which is not a floating-point number array instance', function test( t ) { + var values; + var arr; + var i; + + arr = new Float16Array( 5 ); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + {}, + [], + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + return arr.toLocaleString.call( value ); + }; + } +}); + +tape( 'the method throws an error if provided a first argument which is not a string or an array of strings', function test( t ) { + var values; + var arr; + var i; + + arr = new Float16Array(); + + values = [ + 5, + NaN, + true, + false, + null, + void 0, + {}, + [ 1, 2, 3 ], + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + return arr.toLocaleString( value ); + }; + } +}); + +tape( 'the method throws an error if provided a first argument which is not a string or an array of strings (options)', function test( t ) { + var values; + var arr; + var i; + + arr = new Float16Array(); + + values = [ + 5, + NaN, + true, + false, + null, + void 0, + {}, + [ 1, 2, 3 ], + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + return arr.toLocaleString( value, {} ); + }; + } +}); + +tape( 'the method throws an error if provided a second argument which is not an object', function test( t ) { + var values; + var arr; + var i; + + arr = new Float16Array(); + + values = [ + 5, + NaN, + true, + false, + null, + void 0, + 'beep', + [], + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + return arr.toLocaleString( 'en-GB', value ); + }; + } +}); + +tape( 'the method returns an empty string if invoked on an empty array', function test( t ) { + var str; + var arr; + + arr = new Float16Array(); + str = arr.toLocaleString(); + + t.strictEqual( str, '', 'returns expected value' ); + t.end(); +}); + +tape( 'the method returns a string representation of a floating-point number array', function test( t ) { + var expected; + var str; + var arr; + + arr = new Float16Array( [ 1.0, 2.0, 3.0 ] ); + expected = '1,2,3'; + + str = arr.toLocaleString(); + + t.strictEqual( str, expected, 'returns expected value' ); + t.end(); +}); diff --git a/lib/node_modules/@stdlib/array/float16/test/test.to_reversed.js b/lib/node_modules/@stdlib/array/float16/test/test.to_reversed.js new file mode 100644 index 000000000000..d53938eaa0f7 --- /dev/null +++ b/lib/node_modules/@stdlib/array/float16/test/test.to_reversed.js @@ -0,0 +1,121 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var hasOwnProp = require( '@stdlib/assert/has-own-property' ); +var isFunction = require( '@stdlib/assert/is-function' ); +var instanceOf = require( '@stdlib/assert/instance-of' ); +var toWord = require( '@stdlib/number/float16/base/to-word' ); +var Uint16Array = require( '@stdlib/array/uint16' ); +var Float16Array = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof Float16Array, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'attached to the prototype of the main export is a `toReversed` method', function test( t ) { + t.strictEqual( hasOwnProp( Float16Array.prototype, 'toReversed' ), true, 'has property' ); + t.strictEqual( isFunction( Float16Array.prototype.toReversed ), true, 'has method' ); + t.end(); +}); + +tape( 'the method throws an error if invoked with a `this` context which is not a floating-point number array instance', function test( t ) { + var values; + var arr; + var i; + + arr = new Float16Array( 5 ); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + {}, + [], + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + return arr.toReversed.call( value ); + }; + } +}); + +tape( 'the method returns an empty array if operating on an empty floating-point number array', function test( t ) { + var arr; + var out; + + arr = new Float16Array(); + out = arr.toReversed(); + + t.strictEqual( out.length, 0, 'returns expected value' ); + t.end(); +}); + +tape( 'the method returns a new typed array containing elements in reverse order', function test( t ) { + var expected; + var arr; + var buf; + var out; + + arr = new Float16Array( [ 1.05, 2.05, 3.05, 4.05, 5.05 ] ); + expected = new Uint16Array([ + toWord( 5.05 ), + toWord( 4.05 ), + toWord( 3.05 ), + toWord( 2.05 ), + toWord( 1.05 ) + ]); + out = arr.toReversed(); + buf = new Uint16Array( out.buffer ); + + t.strictEqual( instanceOf( out, Float16Array ), true, 'returns expected value' ); + t.notEqual( out, arr, 'returns a new instance' ); + t.strictEqual( out.length, expected.length, 'returns expected value' ); + t.deepEqual( buf, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the method does not change the array length', function test( t ) { + var arr; + var out; + + arr = new Float16Array( 10 ); + out = arr.toReversed(); + + t.strictEqual( out.length, 10, 'returns expected value' ); + t.end(); +}); diff --git a/lib/node_modules/@stdlib/array/float16/test/test.to_sorted.js b/lib/node_modules/@stdlib/array/float16/test/test.to_sorted.js new file mode 100644 index 000000000000..fa66baf75d1a --- /dev/null +++ b/lib/node_modules/@stdlib/array/float16/test/test.to_sorted.js @@ -0,0 +1,172 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var hasOwnProp = require( '@stdlib/assert/has-own-property' ); +var isFunction = require( '@stdlib/assert/is-function' ); +var instanceOf = require( '@stdlib/assert/instance-of' ); +var toWord = require( '@stdlib/number/float16/base/to-word' ); +var Uint16Array = require( '@stdlib/array/uint16' ); +var Float16Array = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Comparison function. +* +* @private +* @param {boolean} a - first value for comparison +* @param {boolean} b - second value for comparison +* @returns {number} comparison result +*/ +function compareFcn( a, b ) { + if ( a < b ) { + return -1; + } + if ( a > b ) { + return 1; + } + return 0; +} + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof Float16Array, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'attached to the prototype of the main export is a `toSorted` method', function test( t ) { + t.strictEqual( hasOwnProp( Float16Array.prototype, 'toSorted' ), true, 'has property' ); + t.strictEqual( isFunction( Float16Array.prototype.toSorted ), true, 'has method' ); + t.end(); +}); + +tape( 'the method throws an error if invoked with a `this` context which is not a floating-point number array instance', function test( t ) { + var values; + var arr; + var i; + + arr = new Float16Array( 5 ); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + {}, + [], + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + return arr.toSorted.call( value, compareFcn ); + }; + } +}); + +tape( 'the method throws an error if provided a first argument which is not a function', function test( t ) { + var values; + var arr; + var i; + + arr = new Float16Array( 10 ); + + values = [ + '5', + 3.14, + NaN, + true, + false, + null, + void 0, + {}, + [] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + return arr.toSorted( value ); + }; + } +}); + +tape( 'the method returns an empty array if operating on an empty floating-point number array', function test( t ) { + var arr; + var out; + + arr = new Float16Array(); + out = arr.toSorted( compareFcn ); + + t.strictEqual( out.length, 0, 'returns expected value' ); + t.end(); +}); + +tape( 'the method returns a new typed array containing elements in sorted order', function test( t ) { + var expected; + var arr; + var out; + var buf; + + arr = new Float16Array( [ 4.05, 3.05, 1.05, 5.05, 2.05 ] ); + expected = new Uint16Array([ + toWord( 1.05 ), + toWord( 2.05 ), + toWord( 3.05 ), + toWord( 4.05 ), + toWord( 5.05 ) + ]); + out = arr.toSorted( compareFcn ); + buf = new Uint16Array( out.buffer ); + + t.strictEqual( instanceOf( out, Float16Array ), true, 'returns expected value' ); + t.notEqual( out, arr, 'returns a new instance' ); + t.strictEqual( out.length, expected.length, 'returns expected value' ); + t.deepEqual( buf, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the method does not change the array length', function test( t ) { + var arr; + var out; + + arr = new Float16Array( [ 4.05, 3.05, 1.05, 5.05, 2.05 ] ); + out = arr.toSorted( compareFcn ); + + t.strictEqual( out.length, arr.length, 'returns expected value' ); + t.end(); +}); diff --git a/lib/node_modules/@stdlib/array/float16/test/test.to_string.js b/lib/node_modules/@stdlib/array/float16/test/test.to_string.js new file mode 100644 index 000000000000..72324825cfca --- /dev/null +++ b/lib/node_modules/@stdlib/array/float16/test/test.to_string.js @@ -0,0 +1,97 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var hasOwnProp = require( '@stdlib/assert/has-own-property' ); +var isFunction = require( '@stdlib/assert/is-function' ); +var Float16Array = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof Float16Array, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'attached to the prototype of the main export is a `toString` method', function test( t ) { + t.strictEqual( hasOwnProp( Float16Array.prototype, 'toString' ), true, 'has property' ); + t.strictEqual( isFunction( Float16Array.prototype.toString ), true, 'has method' ); + t.end(); +}); + +tape( 'the method throws an error if invoked with a `this` context which is not a floating-point number array instance', function test( t ) { + var values; + var arr; + var i; + + arr = new Float16Array( 5 ); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + {}, + [], + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + return arr.toString.call( value ); + }; + } +}); + +tape( 'the method returns an empty string if invoked on an empty array', function test( t ) { + var str; + var arr; + + arr = new Float16Array(); + str = arr.toString(); + + t.strictEqual( str, '', 'returns expected value' ); + t.end(); +}); + +tape( 'the method returns a string representation of a floating-point number array', function test( t ) { + var expected; + var str; + var arr; + + arr = new Float16Array( [ 1.0, 2.0, 3.0, 4.0 ] ); + expected = '1,2,3,4'; + + str = arr.toString(); + + t.strictEqual( str, expected, 'returns expected value' ); + t.end(); +}); diff --git a/lib/node_modules/@stdlib/array/float16/test/test.values.js b/lib/node_modules/@stdlib/array/float16/test/test.values.js new file mode 100644 index 000000000000..89533a5a7f7a --- /dev/null +++ b/lib/node_modules/@stdlib/array/float16/test/test.values.js @@ -0,0 +1,264 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var proxyquire = require( 'proxyquire' ); +var hasOwnProp = require( '@stdlib/assert/has-own-property' ); +var isFunction = require( '@stdlib/assert/is-function' ); +var float64ToFloat16 = require( '@stdlib/number/float64/base/to-float16' ); +var isNumber = require( '@stdlib/assert/is-number' ); +var ITERATOR_SYMBOL = require( '@stdlib/symbol/iterator' ); +var Float16Array = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof Float16Array, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'attached to the prototype of the main export is a `values` method', function test( t ) { + t.strictEqual( hasOwnProp( Float16Array.prototype, 'values' ), true, 'has property' ); + t.strictEqual( isFunction( Float16Array.prototype.values ), true, 'has method' ); + t.end(); +}); + +tape( 'the method throws an error if invoked with a `this` context which is not a floating-point number array instance', function test( t ) { + var values; + var arr; + var i; + + arr = new Float16Array( 5 ); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + {}, + [], + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + return arr.values.call( value ); + }; + } +}); + +tape( 'the method returns an iterator protocol-compliant object', function test( t ) { + var expected; + var arr; + var it; + var i; + var r; + var e; + + arr = new Float16Array( [ 1.05, 2.05 ] ); + expected = [ + { + 'value': float64ToFloat16( 1.05 ), + 'done': false + }, + { + 'value': float64ToFloat16( 2.05 ), + 'done': false + }, + { + 'done': true + } + ]; + it = arr.values(); + + t.strictEqual( typeof it, 'object', 'returns expected value' ); + t.strictEqual( typeof it.next, 'function', 'has next method' ); + + for ( i = 0; i < expected.length; i++ ) { + r = it.next(); + e = expected[ i ]; + if ( e.value === void 0 ) { + t.deepEqual( r, e, 'returns expected value' ); + } else { + t.strictEqual( isNumber( r.value ), true, 'returns expected value' ); + t.strictEqual( r.value, e.value, 'returns expected value' ); + t.strictEqual( r.done, e.done, 'returns expected value' ); + } + } + + t.end(); +}); + +tape( 'the method returns an iterator which does not iterate over empty arrays', function test( t ) { + var expected; + var arr; + var it; + var i; + var v; + + arr = new Float16Array( [] ); + expected = [ + { + 'done': true + }, + { + 'done': true + }, + { + 'done': true + } + ]; + it = arr.values(); + + t.strictEqual( typeof it, 'object', 'returns expected value' ); + t.strictEqual( typeof it.next, 'function', 'has next method' ); + + for ( i = 0; i < expected.length; i++ ) { + v = it.next(); + t.deepEqual( v, expected[ i ], 'returns expected value' ); + } + t.end(); +}); + +tape( 'the returned iterator has a `return` method for closing an iterator (no argument)', function test( t ) { + var arr; + var it; + var v; + + arr = new Float16Array( [ 1.05, 2.05, 3.05 ] ); + it = arr.values(); + + v = it.next(); + t.strictEqual( v.value, float64ToFloat16( 1.05 ), 'returns expected value' ); + t.strictEqual( v.done, false, 'returns expected value' ); + + v = it.next(); + t.strictEqual( v.value, float64ToFloat16( 2.05 ), 'returns expected value' ); + t.strictEqual( v.done, false, 'returns expected value' ); + + v = it.return(); + t.strictEqual( v.value, void 0, 'returns expected value' ); + t.strictEqual( v.done, true, 'returns expected value' ); + + v = it.next(); + t.strictEqual( v.value, void 0, 'returns expected value' ); + t.strictEqual( v.done, true, 'returns expected value' ); + + v = it.next(); + t.strictEqual( v.value, void 0, 'returns expected value' ); + t.strictEqual( v.done, true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the returned iterator has a `return` method for closing an iterator (argument)', function test( t ) { + var arr; + var it; + var v; + + arr = new Float16Array( [ 1.05, 2.05, 3.05 ] ); + it = arr.values(); + + v = it.next(); + t.strictEqual( v.value, float64ToFloat16( 1.05 ), 'returns expected value' ); + t.strictEqual( v.done, false, 'returns expected value' ); + + v = it.next(); + t.strictEqual( v.value, float64ToFloat16( 2.05 ), 'returns expected value' ); + t.strictEqual( v.done, false, 'returns expected value' ); + + v = it.return( 'beep' ); + t.strictEqual( v.value, 'beep', 'returns expected value' ); + t.strictEqual( v.done, true, 'returns expected value' ); + + v = it.next(); + t.strictEqual( v.value, void 0, 'returns expected value' ); + t.strictEqual( v.done, true, 'returns expected value' ); + + v = it.next(); + t.strictEqual( v.value, void 0, 'returns expected value' ); + t.strictEqual( v.done, true, 'returns expected value' ); + + t.end(); +}); + +tape( 'if an environment supports `Symbol.iterator`, the method returns an iterable', function test( t ) { + var Float16Array; + var arr; + var buf; + var it1; + var it2; + var v1; + var v2; + var i; + + Float16Array = proxyquire( './../lib/main.js', { + '@stdlib/symbol/iterator': '__ITERATOR_SYMBOL__' + }); + + buf = [ 1.05, 2.05, 3.05, 4.05 ]; + arr = new Float16Array( buf ); + + it1 = arr.values(); + t.strictEqual( typeof it1[ '__ITERATOR_SYMBOL__' ], 'function', 'has method' ); + t.strictEqual( it1[ '__ITERATOR_SYMBOL__' ].length, 0, 'has zero arity' ); + + it2 = it1[ '__ITERATOR_SYMBOL__' ](); + t.strictEqual( typeof it2, 'object', 'returns expected value' ); + t.strictEqual( typeof it2.next, 'function', 'has `next` method' ); + t.strictEqual( typeof it2.return, 'function', 'has `return` method' ); + + for ( i = 0; i < arr.length; i++ ) { + v1 = it1.next().value; + v2 = it2.next().value; + t.strictEqual( v1, v2, 'returns expected value' ); + } + t.end(); +}); + +tape( 'if an environment does not support `Symbol.iterator`, the method does not return an "iterable"', function test( t ) { + var Float16Array; + var arr; + var buf; + var it; + + Float16Array = proxyquire( './../lib/main.js', { + '@stdlib/symbol/iterator': false + }); + + buf = [ 1.05, 2.05, 3.05, 4.05 ]; + arr = new Float16Array( buf ); + + it = arr.values(); + t.strictEqual( it[ ITERATOR_SYMBOL ], void 0, 'does not have property' ); + + t.end(); +}); diff --git a/lib/node_modules/@stdlib/array/float16/test/test.with.js b/lib/node_modules/@stdlib/array/float16/test/test.with.js new file mode 100644 index 000000000000..bdcfdae8853f --- /dev/null +++ b/lib/node_modules/@stdlib/array/float16/test/test.with.js @@ -0,0 +1,213 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var hasOwnProp = require( '@stdlib/assert/has-own-property' ); +var isFunction = require( '@stdlib/assert/is-function' ); +var toWord = require( '@stdlib/number/float16/base/to-word' ); +var Uint16Array = require( '@stdlib/array/uint16' ); +var Float16Array = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof Float16Array, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'attached to the prototype of the main export is a `with` method', function test( t ) { + t.strictEqual( hasOwnProp( Float16Array.prototype, 'with' ), true, 'has property' ); + t.strictEqual( isFunction( Float16Array.prototype.with ), true, 'has method' ); + t.end(); +}); + +tape( 'the method throws an error if invoked with a `this` context which is not a floating-point number array instance', function test( t ) { + var values; + var arr; + var i; + + arr = new Float16Array( 5 ); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + {}, + [], + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + return arr.with.call( value, 0, true ); + }; + } +}); + +tape( 'the method throws an error if provided a first argument which is not an integer', function test( t ) { + var values; + var arr; + var i; + + arr = new Float16Array( 5 ); + + values = [ + '5', + 3.14, + NaN, + true, + false, + null, + void 0, + {}, + [], + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + return arr.with( value, true ); + }; + } +}); + +tape( 'the method throws an error if provided a first argument which is not in bounds', function test( t ) { + var values; + var arr; + var i; + + arr = new Float16Array( 10 ); + + values = [ + -11, + -12, + 11, + 12 + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), RangeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + return arr.with( value, 1.0 ); + }; + } +}); + +tape( 'the method throws an error if provided a second argument which is not a numeric value', function test( t ) { + var values; + var arr; + var i; + + arr = new Float16Array( 10 ); + + values = [ + '5', + true, + false, + null, + void 0, + {}, + [], + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + return arr.with( 0, value ); + }; + } +}); + +tape( 'the method does not change the array length', function test( t ) { + var arr; + var out; + + arr = new Float16Array( 10 ); + out = arr.with( 5, 1.0 ); + + t.strictEqual( out.length, 10, 'returns expected value' ); + t.end(); +}); + +tape( 'the method returns a new floating-point number array with the element at a provided index replaced with a provided value', function test( t ) { + var expected; + var arr; + var buf; + var out; + + arr = new Float16Array( [ 1.05, 1.05, 1.05, 1.05 ] ); + expected = new Uint16Array([ + toWord( 1.05 ), + toWord( 1.05 ), + toWord( 1.05 ), + toWord( 2.05 ) + ]); + out = arr.with( 3, 2.05 ); + buf = new Uint16Array( out.buffer ); + + t.strictEqual( out instanceof Float16Array, true, 'returns expected value' ); + t.deepEqual( buf, expected, 'returns expected value' ); + t.notEqual( out, arr, 'returns new instance' ); + t.end(); +}); + +tape( 'the method supports negative indices', function test( t ) { + var expected; + var arr; + var buf; + var out; + + arr = new Float16Array( [ 1.05, 1.05, 1.05, 1.05 ] ); + expected = new Uint16Array([ + toWord( 1.05 ), + toWord( 1.05 ), + toWord( 1.05 ), + toWord( 2.05 ) + ]); + out = arr.with( -1, 2.05 ); + buf = new Uint16Array( out.buffer ); + + t.strictEqual( out instanceof Float16Array, true, 'returns expected value' ); + t.deepEqual( buf, expected, 'returns expected value' ); + t.end(); +}); From 3a397095e0fa4066a921b7940ab4d9e4058e503e Mon Sep 17 00:00:00 2001 From: gururaj1512 Date: Wed, 17 Dec 2025 22:51:01 +0530 Subject: [PATCH 10/45] chore: apply suggested changes --- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: na - task: lint_package_json status: na - task: lint_repl_help status: na - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: na - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: na - task: lint_license_headers status: passed --- --- .../@stdlib/array/float16/lib/main.js | 123 ++++++++++-------- .../@stdlib/array/float16/test/test.sort.js | 16 +++ 2 files changed, 83 insertions(+), 56 deletions(-) diff --git a/lib/node_modules/@stdlib/array/float16/lib/main.js b/lib/node_modules/@stdlib/array/float16/lib/main.js index a8cf5a2226d0..9dbf883d9cad 100644 --- a/lib/node_modules/@stdlib/array/float16/lib/main.js +++ b/lib/node_modules/@stdlib/array/float16/lib/main.js @@ -38,6 +38,8 @@ var isFunction = require( '@stdlib/assert/is-function' ); var floor = require( '@stdlib/math/base/special/floor' ); var format = require( '@stdlib/string/format' ); var Uint16Array = require( '@stdlib/array/uint16' ); +var Float64Array = require( '@stdlib/array/float64' ); +var isnan = require( '@stdlib/assert/is-nan' ); var float64ToFloat16 = require( '@stdlib/number/float64/base/to-float16' ); var toWord = require( '@stdlib/number/float16/base/to-word' ); var fromWord = require( '@stdlib/number/float16/base/from-word' ); @@ -341,7 +343,7 @@ setReadOnly( Float16Array, 'from', function from( src ) { out = new this( len ); buf = out._buffer; // eslint-disable-line no-underscore-dangle for ( i = 0; i < len; i++ ) { - buf[ i ] = toWord( float64ToFloat16( clbk.call( thisArg, get( src, i ), i ) ) ); + buf[ i ] = toWord( float64ToFloat16( clbk.call( thisArg, get( src, i ), i ) ) ); // eslint-disable-line max-len } return out; } @@ -750,6 +752,7 @@ setReadOnly( Float16Array.prototype, 'fill', function fill( value, start, end ) var buf; var len; var i; + var v; if ( !isFloat16Array( this ) ) { throw new TypeError( 'invalid invocation. `this` is not a floating-point number array.' ); @@ -789,8 +792,9 @@ setReadOnly( Float16Array.prototype, 'fill', function fill( value, start, end ) start = 0; end = len; } + v = toWord( value ); for ( i = start; i < end; i++ ) { - buf[ i ] = toWord( value ); + buf[ i ] = v; } return this; }); @@ -1151,6 +1155,7 @@ setReadOnly( Float16Array.prototype, 'get', function get( idx ) { setReadOnly( Float16Array.prototype, 'includes', function includes( searchElement, fromIndex ) { var buf; var i; + var v; if ( !isFloat16Array( this ) ) { throw new TypeError( 'invalid invocation. `this` is not a floating-point number array.' ); @@ -1172,8 +1177,9 @@ setReadOnly( Float16Array.prototype, 'includes', function includes( searchElemen fromIndex = 0; } buf = this._buffer; + v = toWord( searchElement ); for ( i = fromIndex; i < this._length; i++ ) { - if ( searchElement === fromWord( buf[ i ] ) ) { + if ( v === buf[ i ] ) { return true; } } @@ -1214,6 +1220,7 @@ setReadOnly( Float16Array.prototype, 'includes', function includes( searchElemen setReadOnly( Float16Array.prototype, 'indexOf', function indexOf( searchElement, fromIndex ) { var buf; var i; + var v; if ( !isFloat16Array( this ) ) { throw new TypeError( 'invalid invocation. `this` is not a floating-point number array.' ); @@ -1235,8 +1242,9 @@ setReadOnly( Float16Array.prototype, 'indexOf', function indexOf( searchElement, fromIndex = 0; } buf = this._buffer; + v = toWord( searchElement ); for ( i = fromIndex; i < this._length; i++ ) { - if ( searchElement === fromWord( buf[ i ] ) ) { + if ( v === buf[ i ] ) { return i; } } @@ -1283,9 +1291,9 @@ setReadOnly( Float16Array.prototype, 'join', function join( separator ) { separator = ','; } buf = this._buffer; - out = []; + out = new Float64Array( this._length ); for ( i = 0; i < this._length; i++ ) { - out.push( fromWord( buf[i] ) ); + out[ i ] = fromWord( buf[ i ] ); } return out.join( separator ); }); @@ -1429,6 +1437,7 @@ setReadOnly( Float16Array.prototype, 'keys', function keys() { setReadOnly( Float16Array.prototype, 'lastIndexOf', function lastIndexOf( searchElement, fromIndex ) { var buf; var i; + var v; if ( !isFloat16Array( this ) ) { throw new TypeError( 'invalid invocation. `this` is not a floating-point number array.' ); @@ -1449,8 +1458,9 @@ setReadOnly( Float16Array.prototype, 'lastIndexOf', function lastIndexOf( search fromIndex = this._length - 1; } buf = this._buffer; + v = toWord( searchElement ); for ( i = fromIndex; i >= 0; i-- ) { - if ( searchElement === fromWord( buf[ i ] ) ) { + if ( v === buf[ i ] ) { return i; } } @@ -1994,7 +2004,7 @@ setReadOnly( Float16Array.prototype, 'sort', function sort( compareFcn ) { } buf = this._buffer; if ( arguments.length === 0 ) { - buf.sort(); + buf.sort( defaultCompare ); return this; } if ( !isFunction( compareFcn ) ) { @@ -2003,16 +2013,52 @@ setReadOnly( Float16Array.prototype, 'sort', function sort( compareFcn ) { buf.sort( compare ); return this; + /** + * Default comparison function for float16 values. + * + * @private + * @param {unsigned16} a - unsigned 16-bit integer for comparison + * @param {unsigned16} b - unsigned 16-bit integer for comparison + * @returns {number} comparison result + */ + function defaultCompare( a, b ) { // eslint-disable-line stdlib/no-unnecessary-nested-functions + var x; + var y; + + x = fromWord( a ); + y = fromWord( b ); + + // Handle NaN... + if ( isnan( x ) && isnan( y ) ) { + return 0; + } + if ( isnan( x ) ) { + return 1; + } + if ( isnan( y ) ) { + return -1; + } + + // Normal comparison + if ( x < y ) { + return -1; + } + if ( x > y ) { + return 1; + } + return 0; + } + /** * Comparison function for sorting. * * @private - * @param {float16} a - first floating-point number for comparison - * @param {float16} b - second floating-point number for comparison + * @param {unsigned16} a - unsigned 16-bit integer for comparison + * @param {unsigned16} b - unsigned 16-bit integer for comparison * @returns {number} comparison result */ function compare( a, b ) { - return compareFcn( a, b ); + return compareFcn( fromWord( a ), fromWord( b ) ); } }); @@ -2162,11 +2208,11 @@ setReadOnly( Float16Array.prototype, 'toLocaleString', function toLocaleString( throw new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) ); } buf = this._buffer; - out = []; + out = new Float64Array( this._length ); for ( i = 0; i < this._length; i++ ) { - out.push( fromWord( buf[ i ] ).toLocaleString( loc, opts ) ); + out[ i ] = fromWord( buf[ i ] ); } - return out.join( ',' ); + return out.toLocaleString( loc, opts ); }); /** @@ -2258,43 +2304,16 @@ setReadOnly( Float16Array.prototype, 'toReversed', function toReversed() { * // returns 2.0 */ setReadOnly( Float16Array.prototype, 'toSorted', function toSorted( compareFcn ) { - var outbuf; var out; - var len; - var buf; - var i; if ( !isFloat16Array( this ) ) { throw new TypeError( 'invalid invocation. `this` is not a floating-point number array.' ); } - len = this._length; - out = new this.constructor( len ); - buf = this._buffer; - outbuf = out._buffer; // eslint-disable-line no-underscore-dangle - for ( i = 0; i < len; i++ ) { - outbuf[ i ] = buf[ i ]; - } + out = new this.constructor( this ); if ( arguments.length === 0 ) { - outbuf.sort(); - return out; - } - if ( !isFunction( compareFcn ) ) { - throw new TypeError( format( 'invalid argument. First argument must be a function. Value: `%s`.', compareFcn ) ); - } - outbuf.sort( compare ); - return out; - - /** - * Comparison function for sorting. - * - * @private - * @param {boolean} a - first floating-point number for comparison - * @param {boolean} b - second floating-point number for comparison - * @returns {number} comparison result - */ - function compare( a, b ) { - return compareFcn( a, b ); + return out.sort(); } + return out.sort( compareFcn ); }); /** @@ -2323,10 +2342,10 @@ setReadOnly( Float16Array.prototype, 'toString', function toString() { if ( !isFloat16Array( this ) ) { throw new TypeError( 'invalid invocation. `this` is not a floating-point number array.' ); } - out = []; buf = this._buffer; + out = new Float64Array( this._length ); for ( i = 0; i < this._length; i++ ) { - out.push( fromWord( buf[i] ) ); + out[ i ] = fromWord( buf[ i ] ); } return out.join( ',' ); }); @@ -2463,10 +2482,8 @@ setReadOnly( Float16Array.prototype, 'values', function values() { * // returns 3.0 */ setReadOnly( Float16Array.prototype, 'with', function copyWith( index, value ) { - var buf; var out; var len; - var i; if ( !isFloat16Array( this ) ) { throw new TypeError( 'invalid invocation. `this` is not a floating-point number array.' ); @@ -2484,14 +2501,8 @@ setReadOnly( Float16Array.prototype, 'with', function copyWith( index, value ) { if ( !isNumber( value ) ) { throw new TypeError( format( 'invalid argument. Second argument must be a floating-point number. Value: `%s`.', value ) ); } - out = []; - buf = this._buffer; - for ( i = 0; i < len; i++ ) { - out[ i ] = fromWord( buf[ i ] ); - } - out = new this.constructor( out ); - buf = out._buffer; // eslint-disable-line no-underscore-dangle - buf[ index ] = toWord( value ); + out = new this.constructor( this ); + out.set( value, index ); return out; }); diff --git a/lib/node_modules/@stdlib/array/float16/test/test.sort.js b/lib/node_modules/@stdlib/array/float16/test/test.sort.js index 77912c87b8fa..cde223f1e100 100644 --- a/lib/node_modules/@stdlib/array/float16/test/test.sort.js +++ b/lib/node_modules/@stdlib/array/float16/test/test.sort.js @@ -154,6 +154,22 @@ tape( 'the method sorts elements of a floating-point number array in-place', fun out = arr.sort( compareFcn ); buf = new Uint16Array( out.buffer ); + t.strictEqual( instanceOf( arr, Float16Array ), true, 'returns expected value' ); + t.strictEqual( out, arr, 'returns expected value' ); + t.strictEqual( arr.length, expected.length, 'returns expected value' ); + t.deepEqual( buf, expected, 'returns expected value' ); + + arr = new Float16Array( [ 2.05, 1.05, -1.05, 0.05, -2.05 ] ); + expected = new Uint16Array([ + toWord( -2.05 ), + toWord( -1.05 ), + toWord( 0.05 ), + toWord( 1.05 ), + toWord( 2.05 ) + ]); + out = arr.sort( compareFcn ); + buf = new Uint16Array( out.buffer ); + t.strictEqual( instanceOf( arr, Float16Array ), true, 'returns expected value' ); t.strictEqual( out, arr, 'returns expected value' ); t.strictEqual( arr.length, expected.length, 'returns expected value' ); From 406cbbede88b674899a6864da78023bfa1d46c82 Mon Sep 17 00:00:00 2001 From: gururaj1512 Date: Thu, 18 Dec 2025 02:41:00 +0530 Subject: [PATCH 11/45] feat: enhance Float16Array constructor to handle Float16Array instances --- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: na - task: lint_package_json status: na - task: lint_repl_help status: na - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: na - task: lint_javascript_benchmarks status: na - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: na - task: lint_license_headers status: passed --- --- lib/node_modules/@stdlib/array/float16/lib/main.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/array/float16/lib/main.js b/lib/node_modules/@stdlib/array/float16/lib/main.js index 9dbf883d9cad..9719e6c63955 100644 --- a/lib/node_modules/@stdlib/array/float16/lib/main.js +++ b/lib/node_modules/@stdlib/array/float16/lib/main.js @@ -183,7 +183,11 @@ function Float16Array() { if ( isNonNegativeInteger( arg ) ) { buf = new Uint16Array( arg ); } else if ( isCollection( arg ) ) { - buf = fromArray( new Uint16Array( arg.length ), arg ); + if ( isFloat16Array( arg ) ) { + buf = new Uint16Array( arg._buffer ); // eslint-disable-line no-underscore-dangle + } else { + buf = fromArray( new Uint16Array( arg.length ), arg ); + } } else if ( isArrayBuffer( arg ) ) { if ( !isInteger( arg.byteLength/BYTES_PER_ELEMENT ) ) { throw new RangeError( format( 'invalid argument. ArrayBuffer byte length must be a multiple of %u. Byte length: `%u`.', BYTES_PER_ELEMENT, arg.byteLength ) ); From 21776822d77f4a47022cc33f1e2a356383771458 Mon Sep 17 00:00:00 2001 From: gururaj1512 Date: Thu, 18 Dec 2025 14:12:46 +0530 Subject: [PATCH 12/45] docs: add readme --- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: na - task: lint_repl_help status: na - task: lint_javascript_src status: na - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: na - task: lint_javascript_benchmarks status: na - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: na - task: lint_license_headers status: passed --- --- .../@stdlib/array/float16/README.md | 1207 +++++++++-------- 1 file changed, 658 insertions(+), 549 deletions(-) diff --git a/lib/node_modules/@stdlib/array/float16/README.md b/lib/node_modules/@stdlib/array/float16/README.md index a6131501442c..3562dbea01cc 100644 --- a/lib/node_modules/@stdlib/array/float16/README.md +++ b/lib/node_modules/@stdlib/array/float16/README.md @@ -20,7 +20,7 @@ limitations under the License. # Float16Array -> [Typed array][mdn-typed-array] constructor which returns a [typed array][mdn-typed-array] representing an array of half-precision floating-point numbers in the platform byte order. +> 16-bit half-precision floating-point number array. @@ -40,11 +40,11 @@ limitations under the License. var Float16Array = require( '@stdlib/array/float16' ); ``` -#### Float16Array() + -A [typed array][mdn-typed-array] constructor which returns a [typed array][mdn-typed-array] representing an array of half-precision floating-point numbers in the platform byte order. +#### Float16Array() - +Creates a 16-bit half-precision floating-point number array. ```javascript var arr = new Float16Array(); @@ -53,52 +53,70 @@ var arr = new Float16Array(); #### Float16Array( length ) -Returns a [typed array][mdn-typed-array] having a specified length. - - +Creates a 16-bit half-precision floating-point number array having a specified `length`. ```javascript var arr = new Float16Array( 5 ); -// returns [ 0.0, 0.0, 0.0, 0.0, 0.0 ] +// returns + +var len = arr.length; +// returns 5 ``` #### Float16Array( typedarray ) -Creates a [typed array][mdn-typed-array] from another [typed array][mdn-typed-array]. - - +Creates a 16-bit half-precision floating-point number array from a [typed array][@stdlib/array/typed]. ```javascript var Float64Array = require( '@stdlib/array/float64' ); -var arr1 = new Float64Array( [ 0.5, 0.5, 0.5 ] ); -var arr2 = new Float16Array( arr1 ); -// returns [ 0.5, 0.5, 0.5 ] +var buf = new Float64Array( [ 0.5, 0.5, 0.5 ] ); +// returns + +var arr = new Float16Array( buf ); +// returns + +var len = arr.length; +// returns 3 ``` #### Float16Array( obj ) -Creates a [typed array][mdn-typed-array] from an array-like `object` or iterable. - - +Creates a 16-bit half-precision floating-point number array from an array-like object or iterable. ```javascript var arr = new Float16Array( [ 0.5, 0.5, 0.5 ] ); -// returns [ 0.5, 0.5, 0.5 ] +// returns + +var len = arr.length; +// returns 3 ``` #### Float16Array( buffer\[, byteOffset\[, length]] ) -Returns a [typed array][mdn-typed-array] view of an [`ArrayBuffer`][@stdlib/array/buffer]. - - +Returns a 16-bit half-precision floating-point number array view of an [`ArrayBuffer`][@stdlib/array/buffer]. ```javascript var ArrayBuffer = require( '@stdlib/array/buffer' ); - var buf = new ArrayBuffer( 8 ); -var arr = new Float16Array( buf, 0, 4 ); -// returns [ 0.0, 0.0, 0.0, 0.0 ] + +var arr = new Float16Array( buf ); +// returns + +var len = arr.length; +// returns 4 + +arr = new Float16Array( buf, 2 ); +// returns + +len = arr.length; +// returns 3 + +arr = new Float16Array( buf, 2, 2 ); +// returns + +len = arr.length; +// returns 2 ``` * * * @@ -109,9 +127,7 @@ var arr = new Float16Array( buf, 0, 4 ); #### Float16Array.BYTES_PER_ELEMENT -Number of bytes per view element. - - +Static property returning the size (in bytes) of each array element. ```javascript var nbytes = Float16Array.BYTES_PER_ELEMENT; @@ -122,9 +138,7 @@ var nbytes = Float16Array.BYTES_PER_ELEMENT; #### Float16Array.name -[Typed array][mdn-typed-array] constructor name. - - +Static property returning the constructor name. ```javascript var str = Float16Array.name; @@ -135,12 +149,12 @@ var str = Float16Array.name; #### Float16Array.prototype.buffer -**Read-only** property which returns the [`ArrayBuffer`][@stdlib/array/buffer] referenced by the [typed array][mdn-typed-array]. - - +Pointer to the underlying data buffer. ```javascript var arr = new Float16Array( 5 ); +// returns + var buf = arr.buffer; // returns ``` @@ -149,12 +163,12 @@ var buf = arr.buffer; #### Float16Array.prototype.byteLength -**Read-only** property which returns the length (in bytes) of the [typed array][mdn-typed-array]. - - +Size (in bytes) of the array. ```javascript var arr = new Float16Array( 5 ); +// returns + var byteLength = arr.byteLength; // returns 10 ``` @@ -163,26 +177,35 @@ var byteLength = arr.byteLength; #### Float16Array.prototype.byteOffset -**Read-only** property which returns the offset (in bytes) of the [typed array][mdn-typed-array] from the start of its [`ArrayBuffer`][@stdlib/array/buffer]. - - +Offset (in bytes) of the array from the start of its underlying `ArrayBuffer`. ```javascript +var ArrayBuffer = require( '@stdlib/array/buffer' ); + var arr = new Float16Array( 5 ); -var byteOffset = arr.byteOffset; +// returns + +var offset = arr.byteOffset; // returns 0 + +var buf = new ArrayBuffer( 20 ); +arr = new Float16Array( buf, 10 ); +// returns + +offset = arr.byteOffset; +// returns 10 ``` #### Float16Array.prototype.BYTES_PER_ELEMENT -Number of bytes per view element. - - +Size (in bytes) of each array element. ```javascript var arr = new Float16Array( 5 ); +// returns + var nbytes = arr.BYTES_PER_ELEMENT; // returns 2 ``` @@ -191,12 +214,12 @@ var nbytes = arr.BYTES_PER_ELEMENT; #### Float16Array.prototype.length -**Read-only** property which returns the number of view elements. - - +Number of array elements. ```javascript var arr = new Float16Array( 5 ); +// returns + var len = arr.length; // returns 5 ``` @@ -207,35 +230,53 @@ var len = arr.length; -#### Float16Array.from( src\[, map\[, thisArg]] ) +#### Float16Array.from( src\[, clbk\[, thisArg]] ) -Creates a new typed array from an array-like `object` or an iterable. +Creates a new 16-bit half-precision floating-point number array from an array-like object or an iterable. ```javascript var arr = Float16Array.from( [ 1.0, 2.0 ] ); -// returns [ 1.0, 2.0 ] +// returns + +var v = arr.get( 0 ); +// returns 1.0 + +v = arr.get( 1 ); +// returns 2.0 + +var len = arr.length; +// returns 2 ``` To invoke a function for each `src` value, provide a callback function. ```javascript -function mapFcn( v ) { +var arr = Float16Array.from( [ 1.0, 2.0 ], clbk ); +// returns + +var v = arr.get( 0 ); +// returns 2.0 + +v = arr.get( 1 ); +// returns 4.0 + +var len = arr.length; +// returns 2 + +function clbk( v ) { return v * 2.0; } - -var arr = Float16Array.from( [ 1.0, 2.0 ], mapFcn ); -// returns [ 2.0, 4.0 ] ``` A callback function is provided two arguments: -- `value`: source value. -- `index`: source index. +- **value**: source value. +- **index**: source index. To set the callback execution context, provide a `thisArg`. ```javascript -function mapFcn( v ) { +function map( v ) { this.count += 1; return v * 2.0; } @@ -244,8 +285,14 @@ var ctx = { 'count': 0 }; -var arr = Float16Array.from( [ 1.0, 2.0 ], mapFcn, ctx ); -// returns [ 2.0, 4.0 ] +var arr = Float16Array.from( [ 1.0, 2.0 ], map, ctx ); +// returns + +var v = arr.get( 0 ); +// returns 2.0 + +v = arr.get( 1 ); +// returns 4.0 var n = ctx.count; // returns 2 @@ -255,11 +302,45 @@ var n = ctx.count; #### Float16Array.of( element0\[, element1\[, ...elementN]] ) -Creates a new typed array from a variable number of arguments. +Creates a new 16-bit half-precision floating-point number array from a variable number of arguments. ```javascript var arr = Float16Array.of( 1.0, 2.0 ); -// returns [ 1.0, 2.0 ] +// returns + +var v = arr.get( 0 ); +// returns 1.0 + +v = arr.get( 1 ); +// returns 2.0 +``` + + + +#### Float16Array.prototype.at( i ) + +Returns an array element located at integer position (index) `i`, with support for both nonnegative and negative integer positions. + +```javascript +var arr = new Float16Array( [ 10.0, 20.0, 30.0 ] ); + +var v = arr.at( 0 ); +// returns 10.0 + +v = arr.at( -1 ); +// returns 30.0 +``` + +If provided an out-of-bounds index, the method returns `undefined`. + +```javascript +var arr = new Float16Array( 10 ); + +var v = arr.at( 100 ); +// returns undefined + +v = arr.at( -100 ); +// returns undefined ``` @@ -268,53 +349,56 @@ var arr = Float16Array.of( 1.0, 2.0 ); Copies a sequence of elements within an array starting at `start` and ending at `end` (non-inclusive) to the position starting at `target`. - - ```javascript var arr = new Float16Array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] ); // Copy the last two elements to the first two elements: arr.copyWithin( 0, 3 ); -var v = arr[ 0 ]; +var v = arr.get( 0 ); // returns 4.0 -v = arr[ 1 ]; +v = arr.get( 1 ); // returns 5.0 + +v = arr.get( 2 ); +// returns 3.0 ``` By default, `end` equals the number of array elements (i.e., one more than the last array index). To limit the sequence length, provide an `end` argument. - - ```javascript var arr = new Float16Array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] ); // Copy the first two elements to the last two elements: arr.copyWithin( 3, 0, 2 ); -var v = arr[ 3 ]; +var v = arr.get( 3 ); // returns 1.0 -v = arr[ 4 ]; +v = arr.get( 4 ); // returns 2.0 + +v = arr.get( 0 ); +// returns 1.0 ``` When a `target`, `start`, and/or `end` index is negative, the respective index is determined relative to the last array element. The following example achieves the same behavior as the previous example: - - ```javascript var arr = new Float16Array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] ); // Copy the first two elements to the last two elements: arr.copyWithin( -2, -5, -3 ); -var v = arr[ 3 ]; +var v = arr.get( 3 ); // returns 1.0 -v = arr[ 4 ]; +v = arr.get( 4 ); // returns 2.0 + +v = arr.get( 0 ); +// returns 1.0 ``` @@ -323,8 +407,6 @@ v = arr[ 4 ]; Returns an iterator for iterating over array key-value pairs. - - ```javascript var arr = new Float16Array( [ 1.0, 2.0 ] ); @@ -342,39 +424,45 @@ var bool = it.next().done; // returns true ``` +The returned [iterator][mdn-iterator-protocol] protocol-compliant object has the following properties: + +- **next**: function which returns an [iterator][mdn-iterator-protocol] protocol-compliant object containing the next iterated value (if one exists) assigned to a `value` property and a `done` property having a `boolean` value indicating whether the [iterator][mdn-iterator-protocol] is finished. +- **return**: function which closes an [iterator][mdn-iterator-protocol] and returns a single (optional) argument in an [iterator][mdn-iterator-protocol] protocol-compliant object. + #### Float16Array.prototype.every( predicate\[, thisArg] ) -Tests whether all array elements pass a test implemented by a `predicate` function. - - +Returns a boolean indicating whether all elements pass a test. ```javascript function predicate( v ) { - return ( v <= 1.0 ); + return ( v < 0.0 ); } var arr = new Float16Array( [ 1.0, 2.0 ] ); var bool = arr.every( predicate ); // returns false + +arr = new Float16Array( [ -1.0, -2.0 ] ); + +bool = arr.every( predicate ); +// returns true ``` A `predicate` function is provided three arguments: -- `value`: array element. -- `index`: array index. -- `arr`: array on which the method is invoked. +- **value**: current array element. +- **index**: current array element index. +- **arr**: the array on which this method was called. To set the callback execution context, provide a `thisArg`. - - ```javascript function predicate( v ) { this.count += 1; - return ( v >= 1.0 ); + return ( v > 0.0 ); } var ctx = { @@ -394,9 +482,7 @@ var n = ctx.count; #### Float16Array.prototype.fill( value\[, start\[, end]] ) -Fills an array from a `start` index to an `end` index (non-inclusive) with a provided `value`. - - +Returns a modified typed array filled with a fill value. ```javascript var arr = new Float16Array( 2 ); @@ -404,45 +490,43 @@ var arr = new Float16Array( 2 ); // Set all array elements to the same value: arr.fill( 2.0 ); -var v = arr[ 0 ]; +var v = arr.get( 0 ); // returns 2.0 -v = arr[ 1 ]; +v = arr.get( 1 ); // returns 2.0 // Set all array elements starting from the first index to the same value: arr.fill( 3.0, 1 ); -v = arr[ 0 ]; +v = arr.get( 0 ); // returns 2.0 -v = arr[ 1 ]; +v = arr.get( 1 ); // returns 3.0 // Set all array elements, except the last element, to the same value: arr.fill( 4.0, 0, arr.length-1 ); -v = arr[ 0 ]; +v = arr.get( 0 ); // returns 4.0 -v = arr[ 1 ]; +v = arr.get( 1 ); // returns 3.0 ``` When a `start` and/or `end` index is negative, the respective index is determined relative to the last array element. - - ```javascript var arr = new Float16Array( 2 ); // Set all array elements, except the last element, to the same value: arr.fill( 2.0, -arr.length, -1 ); -var v = arr[ 0 ]; +var v = arr.get( 0 ); // returns 2.0 -v = arr[ 1 ]; +v = arr.get( 1 ); // returns 0.0 ``` @@ -450,112 +534,130 @@ v = arr[ 1 ]; #### Float16Array.prototype.filter( predicate\[, thisArg] ) -Creates a new array (of the same data type as the host array) which includes those elements for which a `predicate` function returns a truthy value. - - +Returns a new array containing the elements of an array which pass a test implemented by a predicate function. ```javascript function predicate( v ) { - return ( v >= 2.0 ); + return ( v > 1.0 ); } -var arr1 = new Float16Array( [ 1.0, 2.0, 3.0 ] ); - -var arr2 = arr1.filter( predicate ); -// returns [ 2.0, 3.0 ] -``` - -If a `predicate` function does not return a truthy value for any array element, the method returns an empty array. - - +var arr = new Float16Array( [ 1.0, 2.0, 3.0 ] ); -```javascript -function predicate( v ) { - return ( v >= 10.0 ); -} +var out = arr.filter( predicate ); +// returns -var arr1 = new Float16Array( [ 1.0, 2.0, 3.0 ] ); +var v = out.get( 0 ); +// returns 2.0 -var arr2 = arr1.filter( predicate ); -// returns [] +v = out.get( 1 ); +// returns 3.0 ``` -A `predicate` function is provided three arguments: - -- `value`: array element. -- `index`: array index. -- `arr`: array on which the method is invoked. +The `predicate` function is provided three arguments: -To set the callback execution context, provide a `thisArg`. +- **value**: current array element. +- **index**: current array element index. +- **arr**: the array on which this method was called. - +To set the function execution context, provide a `thisArg`. ```javascript function predicate( v ) { this.count += 1; - return ( v >= 2.0 ); + return ( v > 1.0 ); } var ctx = { 'count': 0 }; -var arr1 = new Float16Array( [ 1.0, 2.0, 3.0 ] ); +var arr = new Float16Array( [ 2.0, 3.0 ] ); +var out = arr.filter( predicate, ctx ); +// returns + +var v = out.get( 0 ); +// returns 2.0 -var arr2 = arr1.filter( predicate, ctx ); +v = out.get( 1 ); +// returns 3.0 var n = ctx.count; -// returns 3 +// returns 2 ``` #### Float16Array.prototype.find( predicate\[, thisArg] ) -Returns the first array element for which a provided `predicate` function returns a truthy value. - - +Returns the first element in an array for which a predicate function returns a truthy value. ```javascript function predicate( v ) { - return ( v > 2.0 ); + return ( v > 1.0 ); } var arr = new Float16Array( [ 1.0, 2.0, 3.0 ] ); var v = arr.find( predicate ); -// returns 3.0 +// returns 2.0 ``` -If a `predicate` function does not return a truthy value for any array element, the method returns `undefined`. +A `predicate` function is provided three arguments: - +- **value**: current array element. +- **index**: current array element index. +- **arr**: the array on which this method was called. + +To set the callback execution context, provide a `thisArg`. ```javascript function predicate( v ) { - return ( v < 1.0 ); + this.count += 1; + return ( v > 1.0 ); } +var ctx = { + 'count': 0 +}; + var arr = new Float16Array( [ 1.0, 2.0, 3.0 ] ); -var v = arr.find( predicate ); -// returns undefined +var v = arr.find( predicate, ctx ); +// returns 2.0 + +var n = ctx.count; +// returns 2 ``` -A `predicate` function is provided three arguments: + + +#### Float16Array.prototype.findIndex( predicate\[, thisArg] ) -- `value`: array element. -- `index`: array index. -- `arr`: array on which the method is invoked. +Returns the index of the first element in an array for which a predicate function returns a truthy value. -To set the callback execution context, provide a `thisArg`. +```javascript +function predicate( v ) { + return ( v > 2.0 ); +} + +var arr = new Float16Array( [ 1.0, 2.0, 3.0 ] ); + +var idx = arr.findIndex( predicate ); +// returns 2 +``` + +The `predicate` function is provided three arguments: - +- **value**: current array element. +- **index**: current array element index. +- **arr**: the array on which this method was called. + +To set the function execution context, provide a `thisArg`. ```javascript function predicate( v ) { this.count += 1; - return ( v > 2.0 ); + return ( v > 3.0 ); } var ctx = { @@ -564,61 +666,86 @@ var ctx = { var arr = new Float16Array( [ 1.0, 2.0, 3.0 ] ); -var v = arr.find( predicate, ctx ); -// returns 3.0 +var idx = arr.findIndex( predicate, ctx ); +// returns -1 var n = ctx.count; // returns 3 ``` - - -#### Float16Array.prototype.findIndex( predicate\[, thisArg] ) + -Returns the index of the first array element for which a provided `predicate` function returns a truthy value. +#### Float16Array.prototype.findLast( predicate\[, thisArg] ) - +Returns the last element in an array for which a predicate function returns a truthy value. ```javascript function predicate( v ) { - return ( v >= 3.0 ); + return v > 2.0; } var arr = new Float16Array( [ 1.0, 2.0, 3.0 ] ); -var idx = arr.findIndex( predicate ); -// returns 2 +var v = arr.findLast( predicate ); +// returns 3.0 ``` -If a `predicate` function does not return a truthy value for any array element, the method returns `-1`. +The `predicate` function is provided three arguments: + +- **value**: current array element. +- **index**: current array element index. +- **arr**: the array on which this method was called. - +To set the function execution context, provide a `thisArg`. ```javascript function predicate( v ) { - return ( v < 1.0 ); + this.count += 1; + return ( v > 2.0 ); } +var ctx = { + 'count': 0 +}; + var arr = new Float16Array( [ 1.0, 2.0, 3.0 ] ); -var idx = arr.findIndex( predicate ); -// returns -1 +var v = arr.findLast( predicate, ctx ); +// returns 3.0 + +var count = ctx.count; +// returns 1 ``` -A `predicate` function is provided three arguments: + -- `value`: array element. -- `index`: array index. -- `arr`: array on which the method is invoked. +#### Float16Array.prototype.findLastIndex( predicate\[, thisArg] ) -To set the callback execution context, provide a `thisArg`. +Returns the index of the last element in an array for which a predicate function returns a truthy value. + +```javascript +function predicate( v ) { + return v > 2.0; +} + +var arr = new Float16Array( [ 1.0, 2.0, 3.0 ] ); + +var v = arr.findLastIndex( predicate ); +// returns 2 +``` + +The `predicate` function is provided three arguments: + +- **value**: current array element. +- **index**: current array element index. +- **arr**: the array on which this method was called. - +To set the function execution context, provide a `thisArg`. ```javascript function predicate( v ) { this.count += 1; - return ( v >= 3.0 ); + return ( v > 2.0 ); } var ctx = { @@ -627,52 +754,46 @@ var ctx = { var arr = new Float16Array( [ 1.0, 2.0, 3.0 ] ); -var idx = arr.findIndex( predicate, ctx ); +var v = arr.findLastIndex( predicate, ctx ); // returns 2 -var n = ctx.count; -// returns 3 +var count = ctx.count; +// returns 1 ``` -#### Float16Array.prototype.forEach( fcn\[, thisArg] ) +#### Float16Array.prototype.forEach( callbackFn\[, thisArg] ) Invokes a callback for each array element. - - ```javascript -var arr = new Float16Array( [ 1.0, 2.0, 3.0 ] ); - -var str = ''; - -function fcn( v, i ) { - str += i + ':' + v; - if ( i < arr.length-1 ) { - str += ' '; - } +function log( v, i ) { + console.log( '%s: %s', i, v.toString() ); } -arr.forEach( fcn ); +var arr = new Float16Array( [ 1.0, 2.0, 3.0 ] ); -console.log( str ); -// => '0:1 1:2 2:3' +arr.forEach( log ); +/* => + 0: 1 + 1: 2 + 2: 3 +*/ ``` -The callback is provided three arguments: +The invoked function is provided three arguments: -- `value`: array element. -- `index`: array index. -- `arr`: array on which the method is invoked. +- **value**: current array element. +- **index**: current array element index. +- **arr**: the array on which this method was called. -To set the callback execution context, provide a `thisArg`. - - +To set the function execution context, provide a `thisArg`. ```javascript -function fcn() { +function log( v, i ) { this.count += 1; + console.log( '%s: %s', i, v.toString() ); } var ctx = { @@ -681,103 +802,89 @@ var ctx = { var arr = new Float16Array( [ 1.0, 2.0, 3.0 ] ); -arr.forEach( fcn, ctx ); +arr.forEach( log, ctx ); +/* => + 0: 1 + 1: 2 + 2: 3 +*/ -var n = ctx.count; +var count = ctx.count; // returns 3 ``` - - -#### Float16Array.prototype.includes( searchElement\[, fromIndex] ) + -Returns a `boolean` indicating whether an array includes a search element. +#### Float16Array.prototype.get( i ) - +Returns an array element located at a nonnegative integer position (index) `i`. ```javascript -var arr = new Float16Array( [ 1.0, 2.0, 3.0 ] ); +var arr = new Float16Array( 10 ); -var bool = arr.includes( 3.0 ); -// returns true +// Set the first element: +arr.set( 1.0, 0 ); -bool = arr.includes( 0.0 ); -// returns false +// Get the first element: +var v = arr.get( 0 ); +// returns 1.0 ``` -By default, the method searches the entire array (`fromIndex = 0`). To begin searching from a specific array index, provide a `fromIndex`. - - +If provided an out-of-bounds index, the method returns `undefined`. ```javascript -var arr = new Float16Array( [ 1.0, 2.0, 3.0 ] ); +var arr = new Float16Array( 10 ); -var bool = arr.includes( 1.0, 1 ); -// returns false +var v = arr.get( 100 ); +// returns undefined ``` -When a `fromIndex` is negative, the starting index is resolved relative to the last array element. + + +#### Float16Array.prototype.includes( searchElement\[, fromIndex] ) - +Returns a boolean indicating whether an array includes a provided value. ```javascript var arr = new Float16Array( [ 1.0, 2.0, 3.0 ] ); -var bool = arr.includes( 1.0, -2 ); +var bool = arr.includes( 3.0 ); +// returns true + +bool = arr.includes( 0.0 ); // returns false ``` -The method does **not** distinguish between signed and unsigned zero. - #### Float16Array.prototype.indexOf( searchElement\[, fromIndex] ) -Returns the index of the first array element strictly equal to a search element. - - +Returns the first index at which a given element can be found. ```javascript var arr = new Float16Array( [ 1.0, 2.0, 3.0 ] ); -var idx = arr.indexOf( 3.0 ); -// returns 2 - -idx = arr.indexOf( 0.0 ); -// returns -1 -``` - -By default, the method searches the entire array (`fromIndex = 0`). To begin searching from a specific array index, provide a `fromIndex`. - - - -```javascript -var arr = new Float16Array( [ 1.0, 2.0, 3.0 ] ); +var idx = arr.indexOf( 2.0 ); +// returns 1 -var idx = arr.indexOf( 1.0, 1 ); -// returns -1 +idx = arr.indexOf( 3.0 ); +// returns 2 ``` -When a `fromIndex` is negative, the starting index is resolved relative to the last array element. - - +If `searchElement` is not present in the array, the method returns `-1`. ```javascript var arr = new Float16Array( [ 1.0, 2.0, 3.0 ] ); -var idx = arr.indexOf( 1.0, -2 ); +var idx = arr.indexOf( 4.0 ); // returns -1 ``` -The method does **not** distinguish between signed and unsigned zero. - #### Float16Array.prototype.join( \[separator] ) -Serializes an array by joining all array elements as a string. - - +Returns a new string by concatenating all array elements. ```javascript var arr = new Float16Array( [ 1.0, 2.0, 3.0 ] ); @@ -786,9 +893,7 @@ var str = arr.join(); // returns '1,2,3' ``` -By default, the method delineates array elements using a comma `,`. To specify a custom separator, provide a `separator` string. - - +By default, the method separates serialized array elements with a comma. To use an alternative separator, provide a `separator` string. ```javascript var arr = new Float16Array( [ 1.0, 2.0, 3.0 ] ); @@ -801,17 +906,13 @@ var str = arr.join( '|' ); #### Float16Array.prototype.keys() -Returns an iterator for iterating over array keys. - - +Returns an iterator for iterating over each index key in a typed array. ```javascript var arr = new Float16Array( [ 1.0, 2.0 ] ); -// Create an iterator: var it = arr.keys(); -// Iterate over keys... var v = it.next().value; // returns 0 @@ -822,79 +923,78 @@ var bool = it.next().done; // returns true ``` +The returned [iterator][mdn-iterator-protocol] protocol-compliant object has the following properties: + +- **next**: function which returns an [iterator][mdn-iterator-protocol] protocol-compliant object containing the next iterated value (if one exists) assigned to a `value` property and a `done` property having a `boolean` value indicating whether the [iterator][mdn-iterator-protocol] is finished. +- **return**: function which closes an [iterator][mdn-iterator-protocol] and returns a single (optional) argument in an [iterator][mdn-iterator-protocol] protocol-compliant object. + #### Float16Array.prototype.lastIndexOf( searchElement\[, fromIndex] ) -Returns the index of the last array element strictly equal to a search element, iterating from right to left. - - +Returns the last index at which a given element can be found. ```javascript -var arr = new Float16Array( [ 1.0, 0.0, 2.0, 0.0, 1.0 ] ); +var arr = new Float16Array( [ 1.0, 2.0, 3.0, 4.0, 2.0 ] ); -var idx = arr.lastIndexOf( 0.0 ); -// returns 3 - -idx = arr.lastIndexOf( 3.0 ); -// returns -1 -``` - -By default, the method searches the entire array (`fromIndex = -1`). To begin searching from a specific array index, provide a `fromIndex`. - - +var idx = arr.lastIndexOf( 3.0 ); +// returns 2 -```javascript -var arr = new Float16Array( [ 1.0, 0.0, 2.0, 0.0, 1.0 ] ); +idx = arr.lastIndexOf( 2.0 ); +// returns 4 -var idx = arr.lastIndexOf( 0.0, 2 ); +idx = arr.lastIndexOf( 2.0, 2 ); // returns 1 ``` -When a `fromIndex` is negative, the starting index is resolved relative to the last array element. - - +If `searchElement` is not present in the array, the method returns `-1`. ```javascript -var arr = new Float16Array( [ 1.0, 0.0, 2.0, 0.0, 1.0 ] ); +var arr = new Float16Array( [ 1.0, 2.0 ] ); -var idx = arr.lastIndexOf( 0.0, -3 ); -// returns 1 -``` +var idx = arr.lastIndexOf( 3.0 ); +// returns -1 -The method does **not** distinguish between signed and unsigned zero. +idx = arr.lastIndexOf( 2.0, 0 ); +// returns -1 +``` -#### Float16Array.prototype.map( fcn\[, thisArg] ) - -Maps each array element to an element in a new array having the same data type as the host array. +#### Float16Array.prototype.map( callbackFn\[, thisArg] ) - +Returns a new array with each element being the result of a provided callback function. ```javascript -function fcn( v ) { +function scale( v ) { return v * 2.0; } -var arr1 = new Float16Array( [ 1.0, 2.0, 3.0 ] ); +var arr = new Float16Array( [ 1.0, 2.0, 3.0 ] ); -var arr2 = arr1.map( fcn ); -// returns [ 2.0, 4.0, 6.0 ] -``` +var out = arr.map( scale ); +// returns -A callback is provided three arguments: +var v = out.get( 0 ); +// returns 2.0 -- `value`: array element. -- `index`: array index. -- `arr`: array on which the method is invoked. +v = out.get( 1 ); +// returns 4.0 -To set the callback execution context, provide a `thisArg`. +v = out.get( 2 ); +// returns 6.0 +``` - +The callback function is provided three arguments: + +- **value**: current array element. +- **index**: current array element index. +- **arr**: the array on which this method was called. + +To set the function execution context, provide a `thisArg`. ```javascript -function fcn( v ) { +function scale( v ) { this.count += 1; return v * 2.0; } @@ -903,290 +1003,258 @@ var ctx = { 'count': 0 }; -var arr1 = new Float16Array( [ 1.0, 2.0, 3.0 ] ); +var arr = new Float16Array( [ 1.0, 2.0, 3.0 ] ); -var arr2 = arr1.map( fcn, ctx ); +var out = arr.map( scale, ctx ); +// returns -var n = ctx.count; +var v = out.get( 0 ); +// returns 2.0 + +v = out.get( 1 ); +// returns 4.0 + +v = out.get( 2 ); +// returns 6.0 + +var count = ctx.count; // returns 3 ``` -#### Float16Array.prototype.reduce( fcn\[, initialValue] ) - -Applies a function against an accumulator and each element in an array and returns the accumulated result. +#### Float16Array.prototype.reduce( reducerFn\[, initialValue] ) - +Applies a provided callback function to each element of the array, in order, passing in the return value from the calculation on the preceding element and returning the accumulated result upon completion. ```javascript function fcn( acc, v ) { - return acc + ( v*v ); + return acc + ( v * v ); } -var arr = new Float16Array( [ 2.0, 1.0, 3.0 ] ); +var arr = new Float16Array( [ 1.0, 2.0, 3.0 ] ); var v = arr.reduce( fcn ); -// returns 12.0 +// returns 14.0 ``` -If not provided an initial value, the method invokes a provided function with the first array element as the first argument and the second array element as the second argument. +The reducer function is provided four arguments: -If provided an initial value, the method invokes a provided function with the initial value as the first argument and the first array element as the second argument. +- **acc**: accumulated result. +- **value**: current array element. +- **index**: current array element index. +- **arr**: the array on which this method was called. - +By default, the function initializes the accumulated result to the first element in the array and passes the second array element as `value` during the first invocation of the provided callback. To begin accumulation from a different starting value and pass in the first array element as `value` during the first invocation of the provided callback, provide an `initialValue` argument. ```javascript function fcn( acc, v ) { - return acc + ( v*v ); + return acc + ( v * v ); } -var arr = new Float16Array( [ 2.0, 1.0, 3.0 ] ); +var arr = new Float16Array( [ 1.0, 2.0, 3.0 ] ); var v = arr.reduce( fcn, 0.0 ); // returns 14.0 ``` -A callback is provided four arguments: - -- `acc`: accumulated result. -- `value`: array element. -- `index`: array index. -- `arr`: array on which the method is invoked. - -#### Float16Array.prototype.reduceRight( fcn\[, initialValue] ) - -Applies a function against an accumulator and each element in an array and returns the accumulated result, iterating from right to left. +#### Float16Array.prototype.reduceRight( reducerFn\[, initialValue] ) - +Applies a provided callback function to each element of the array, in reverse order, passing in the return value from the calculation on the following element and returning the accumulated result upon completion. ```javascript function fcn( acc, v ) { - return acc + ( v*v ); + return acc + ( v * v ); } -var arr = new Float16Array( [ 2.0, 1.0, 3.0 ] ); +var arr = new Float16Array( [ 1.0, 2.0, 3.0 ] ); var v = arr.reduceRight( fcn ); // returns 8.0 ``` -If not provided an initial value, the method invokes a provided function with the last array element as the first argument and the second-to-last array element as the second argument. +The reducer function is provided four arguments: -If provided an initial value, the method invokes a provided function with the initial value as the first argument and the last array element as the second argument. +- **acc**: accumulated result. +- **value**: current array element. +- **index**: current array element index. +- **arr**: the array on which this method was called. - +By default, the function initializes the accumulated result to the last element in the array and passes the second-last array element as `value` during the first invocation of the provided callback. To begin accumulation from a different starting value and pass in the last array element as `value` during the first invocation of the provided callback, provide an `initialValue` argument. ```javascript function fcn( acc, v ) { - return acc + ( v*v ); + return acc + ( v * v ); } -var arr = new Float16Array( [ 2.0, 1.0, 3.0 ] ); +var arr = new Float16Array( [ 1.0, 2.0, 3.0 ] ); -var v = arr.reduce( fcn, 0.0 ); +var v = arr.reduceRight( fcn, 0.0 ); // returns 14.0 ``` -A callback is provided four arguments: - -- `acc`: accumulated result. -- `value`: array element. -- `index`: array index. -- `arr`: array on which the method is invoked. - #### Float16Array.prototype.reverse() -Reverses an array **in-place** (thus mutating the array on which the method is invoked). - - +Reverses an array in-place. ```javascript -var arr = new Float16Array( [ 2.0, 0.0, 3.0 ] ); +var arr = new Float16Array( [ 1.0, 2.0, 3.0 ] ); // Reverse the array: arr.reverse(); -var v = arr[ 0 ]; +var v = arr.get( 0 ); // returns 3.0 -v = arr[ 1 ]; -// returns 0.0 - -v = arr[ 2 ]; +v = arr.get( 1 ); // returns 2.0 + +v = arr.get( 2 ); +// returns 1.0 ``` #### Float16Array.prototype.set( arr\[, offset] ) -Sets array elements. - - +Sets one or more array elements. ```javascript var arr = new Float16Array( [ 1.0, 2.0, 3.0 ] ); -// returns [ 1.0, 2.0, 3.0 ] +// returns -// Set the first two array elements: arr.set( [ 4.0, 5.0 ] ); -var v = arr[ 0 ]; +var v = arr.get( 0 ); // returns 4.0 -v = arr[ 1 ]; +v = arr.get( 1 ); // returns 5.0 -``` -By default, the method starts writing values at the first array index. To specify an alternative index, provide an index `offset`. +v = arr.get( 2 ); +// returns 3.0 +``` - +By default, the method sets array elements starting at position (index) `i = 0`. To set elements starting elsewhere in the array, provide an index argument `i`. ```javascript var arr = new Float16Array( [ 1.0, 2.0, 3.0 ] ); -// returns [ 1.0, 2.0, 3.0 ] +// returns // Set the last two array elements: arr.set( [ 4.0, 5.0 ], 1 ); -var v = arr[ 1 ]; +var v = arr.get( 1 ); // returns 4.0 -v = arr[ 2 ]; +v = arr.get( 2 ); // returns 5.0 ``` +A few notes: + +- If `i` is out-of-bounds, the method throws an error. +- If a target array cannot accommodate all values (i.e., the length of source array plus `i` exceeds the target array length), the method throws an error. +- If provided a [typed array][@stdlib/array/typed] which shares an [`ArrayBuffer`][@stdlib/array/buffer] with the target array, the method will intelligently copy the source range to the destination range. + #### Float16Array.prototype.slice( \[begin\[, end]] ) -Copies array elements to a new array with the same underlying data type as the host array. - - +Copies a portion of a typed array to a new typed array. ```javascript -var arr1 = new Float16Array( [ 1.0, 2.0, 3.0 ] ); - -var arr2 = arr1.slice(); +var arr = new Float16Array( [ 1.0, 2.0, 3.0 ] ); -var bool = ( arr1 === arr2 ); -// returns false +var out = arr.slice(); -bool = ( arr1.buffer === arr2.buffer ); -// returns false +var len = out.length; +// returns 3 -var v = arr2[ 0 ]; +var v = out.get( 0 ); // returns 1.0 -v = arr2[ 1 ]; +v = out.get( 1 ); // returns 2.0 -v = arr2[ 2 ]; +v = out.get( 2 ); // returns 3.0 ``` -By default, the method copies elements beginning with the first array element. To specify an alternative array index at which to begin copying, provide a `begin` index (inclusive). - - +By default, the method returns a typed array beginning with the first array element. To specify an alternative array index at which to begin, provide a `start` index (inclusive). ```javascript -var arr1 = new Float16Array( [ 1.0, 2.0, 3.0 ] ); +var arr = new Float16Array( [ 1.0, 2.0, 3.0 ] ); -var arr2 = arr1.slice( 1 ); +var out = arr.slice( 1 ); -var len = arr2.length; +var len = out.length; // returns 2 -var v = arr2[ 0 ]; +var v = out.get( 0 ); // returns 2.0 -v = arr2[ 1 ]; +v = out.get( 1 ); // returns 3.0 ``` -By default, the method copies all array elements after `begin`. To specify an alternative array index at which to end copying, provide an `end` index (exclusive). - - +By default, the method returns a typed array which includes all array elements after `start`. To limit the number of array elements after `start`, provide an `end` index (exclusive). ```javascript -var arr1 = new Float16Array( [ 1.0, 2.0, 3.0 ] ); +var arr = new Float16Array( [ 1.0, 2.0, 3.0, 4.0 ] ); -var arr2 = arr1.slice( 0, 2 ); +var out = arr.slice( 1, -1 ); -var len = arr2.length; +var len = out.length; // returns 2 -var v = arr2[ 0 ]; -// returns 1.0 - -v = arr2[ 1 ]; +var v = out.get( 0 ); // returns 2.0 -``` - -When a `begin` and/or `end` index is negative, the respective index is determined relative to the last array element. - - - -```javascript -var arr1 = new Float16Array( [ 1.0, 2.0, 3.0 ] ); - -var arr2 = arr1.slice( -arr1.length, -1 ); - -var len = arr2.length; -// returns 2 - -var v = arr2[ 0 ]; -// returns 1.0 -v = arr2[ 1 ]; -// returns 2.0 +v = out.get( 1 ); +// returns 3.0 ``` #### Float16Array.prototype.some( predicate\[, thisArg] ) -Tests whether at least one array element passes a test implemented by a `predicate` function. - - +Returns a boolean indicating whether at least one element passes a test. ```javascript function predicate( v ) { - return ( v >= 2.0 ); + return ( v > 2.0 ); } -var arr = new Float16Array( [ 1.0, 2.0 ] ); +var arr = new Float16Array( [ 1.0, 2.0, 3.0 ] ); var bool = arr.some( predicate ); // returns true ``` -A `predicate` function is provided three arguments: +The `predicate` function is provided three arguments: -- `value`: array element. -- `index`: array index. -- `arr`: array on which the method is invoked. +- **value**: current array element. +- **index**: current array element index. +- **arr**: the array on which this method was called. -To set the callback execution context, provide a `thisArg`. - - +To set the function execution context, provide a `thisArg`. ```javascript function predicate( v ) { this.count += 1; - return ( v >= 2.0 ); + return ( v > 2.0 ); } var ctx = { 'count': 0 }; -var arr = new Float16Array( [ 1.0, 1.0 ] ); +var arr = new Float16Array( [ 1.0, 2.0 ] ); var bool = arr.some( predicate, ctx ); // returns false @@ -1197,151 +1265,195 @@ var n = ctx.count; -#### Float16Array.prototype.sort( \[compareFunction] ) +#### Float16Array.prototype.sort( \[compareFcn] ) -Sorts an array **in-place** (thus mutating the array on which the method is invoked). - - +Sorts an array in-place. ```javascript -var arr = new Float16Array( [ 2.0, 3.0, 0.0 ] ); +function compare( a, b ) { + if ( a < b ) { + return -1; + } + if ( a > b ) { + return 1; + } + return 0; +} -// Sort the array (in ascending order): -arr.sort(); +var arr = new Float16Array( [ 2.0, 3.0, 1.0 ] ); -var v = arr[ 0 ]; -// returns 0.0 +arr.sort( compare ); + +var v = arr.get( 0 ); +// returns 1.0 -v = arr[ 1 ]; +v = arr.get( 1 ); // returns 2.0 -v = arr[ 2 ]; +v = arr.get( 2 ); // returns 3.0 ``` -By default, the method sorts array elements in ascending order. To impose a custom order, provide a `compareFunction`. +The `compareFcn` determines the order of the elements. The function is called with the following arguments: - +- **a**: the first element for comparison. +- **b**: the second element for comparison. -```javascript -function descending( a, b ) { - return b - a; -} +The function should return a number where: -var arr = new Float16Array( [ 2.0, 3.0, 0.0 ] ); +- a negative value indicates that `a` should come before `b`. +- a positive value indicates that `a` should come after `b`. +- zero or `NaN` indicates that `a` and `b` are considered equal. -// Sort the array (in descending order): -arr.sort( descending ); + -var v = arr[ 0 ]; -// returns 3.0 +#### Float16Array.prototype.subarray( \[begin\[, end]] ) -v = arr[ 1 ]; -// returns 2.0 +Creates a new typed array view over the same underlying [`ArrayBuffer`][@stdlib/array/buffer] and with the same underlying data type as the host array. -v = arr[ 2 ]; -// returns 0.0 -``` +```javascript +var arr = new Float16Array( [ 1.0, 2.0, 3.0 ] ); -The comparison function is provided two array elements, `a` and `b`, per invocation, and its return value determines the sort order as follows: +var subarr = arr.subarray(); +// returns -- If the comparison function returns a value **less** than zero, then the method sorts `a` to an index lower than `b` (i.e., `a` should come **before** `b`). -- If the comparison function returns a value **greater** than zero, then the method sorts `a` to an index higher than `b` (i.e., `b` should come **before** `a`). -- If the comparison function returns **zero**, then the relative order of `a` and `b` _should_ remain unchanged. +var v = subarr.get( 0 ); +// returns 1.0 - +v = subarr.get( 1 ); +// returns 2.0 -#### Float16Array.prototype.subarray( \[begin\[, end]] ) +v = subarr.get( 2 ); +// returns 3.0 -Creates a new typed array view over the same underlying [`ArrayBuffer`][@stdlib/array/buffer] and with the same underlying data type as the host array. +var len = subarr.length; +// returns 3 +``` - +By default, the method creates a typed array view beginning with the first array element. To specify an alternative array index at which to begin, provide a `begin` index (inclusive). ```javascript -var arr1 = new Float16Array( [ 1.0, 2.0, 3.0 ] ); +var arr = new Float16Array( [ 1.0, 2.0, 3.0 ] ); -var arr2 = arr1.subarray(); -// returns [ 1.0, 2.0, 3.0 ] +var subarr = arr.subarray( 1 ); +// returns -var bool = ( arr1.buffer === arr2.buffer ); -// returns true -``` +var v = subarr.get( 0 ); +// returns 2.0 -By default, the method creates a typed array view beginning with the first array element. To specify an alternative array index at which to begin, provide a `begin` index (inclusive). +v = subarr.get( 1 ); +// returns 3.0 + +var len = subarr.length; +// returns 2 +``` - +By default, the method creates a typed array view which includes all array elements after `begin`. To limit the number of array elements after `begin`, provide an `end` index (exclusive). ```javascript -var arr1 = new Float16Array( [ 1.0, 2.0, 3.0 ] ); +var arr = new Float16Array( [ 1.0, 2.0, 3.0, 4.0 ] ); -var arr2 = arr1.subarray( 1 ); -// returns [ 2.0, 3.0 ] +var subarr = arr.subarray( 1, -1 ); +// returns -var bool = ( arr1.buffer === arr2.buffer ); -// returns true +var v = subarr.get( 0 ); +// returns 2.0 + +v = subarr.get( 1 ); +// returns 3.0 + +var len = subarr.length; +// returns 2 ``` -By default, the method creates a typed array view which includes all array elements after `begin`. To limit the number of array elements after `begin`, provide an `end` index (exclusive). + - +#### Float16Array.prototype.toLocaleString( \[locales\[, options]] ) -```javascript -var arr1 = new Float16Array( [ 1.0, 2.0, 3.0 ] ); +Serializes an array as a locale-specific string. -var arr2 = arr1.subarray( 0, 2 ); -// returns [ 1.0, 2.0 ] +```javascript +var arr = new Float16Array( [ 1.0, 2.0, 3.0 ] ); -var bool = ( arr1.buffer === arr2.buffer ); -// returns true +var str = arr.toLocaleString(); +// returns '1,2,3' ``` -When a `begin` and/or `end` index is negative, the respective index is determined relative to the last array element. +The method supports the following arguments: + +- **locales**: a string with a BCP 47 language tag or an array of such strings. +- **options**: configuration properties. - + + +#### Float16Array.prototype.toReversed() + +Returns a new typed array containing the elements in reversed order. ```javascript -var arr1 = new Float16Array( [ 1.0, 2.0, 3.0 ] ); +var arr = new Float16Array( [ 1.0, 2.0, 3.0 ] ); -var arr2 = arr1.subarray( -arr1.length, -1 ); -// returns [ 1.0, 2.0 ] +var out = arr.toReversed(); +// returns -var bool = ( arr1.buffer === arr2.buffer ); -// returns true +var v = out.get( 0 ); +// returns 3.0 + +v = out.get( 1 ); +// returns 2.0 + +v = out.get( 2 ); +// returns 1.0 ``` -If the method is unable to resolve indices to a non-empty array subsequence, the method returns an empty typed array. + + +#### Float16Array.prototype.toSorted( \[compareFcn] ) - +Returns a new typed array containing the elements in sorted order. ```javascript -var arr1 = new Float16Array( [ 1.0, 2.0, 3.0 ] ); +function compare( a, b ) { + if ( a < b ) { + return -1; + } + if ( a > b ) { + return 1; + } + return 0; +} -var arr2 = arr1.subarray( 10, -1 ); -// returns [] -``` +var arr = new Float16Array( [ 2.0, 3.0, 1.0 ] ); - +var out = arr.toSorted( compare ); +// returns -#### Float16Array.prototype.toLocaleString( \[locales\[, options]] ) +var v = out.get( 0 ); +// returns 1.0 + +v = out.get( 1 ); +// returns 2.0 + +v = out.get( 2 ); +// returns 3.0 +``` -Serializes an array as a locale-specific `string`. +The `compareFcn` determines the order of the elements. The function is called with the following arguments: - +- **a**: the first element for comparison. +- **b**: the second element for comparison. -```javascript -var arr = new Float16Array( [ 1.0, 2.0, 3.0 ] ); +The function should return a number where: -var str = arr.toLocaleString(); -// returns '1,2,3' -``` +- a negative value indicates that `a` should come before `b`. +- a positive value indicates that `a` should come after `b`. +- zero or `NaN` indicates that `a` and `b` are considered equal. #### Float16Array.prototype.toString() -Serializes an array as a `string`. - - +Serializes an array as a string. ```javascript var arr = new Float16Array( [ 1.0, 2.0, 3.0 ] ); @@ -1354,17 +1466,13 @@ var str = arr.toString(); #### Float16Array.prototype.values() -Returns an iterator for iterating over array elements. - - +Returns an iterator for iterating over each value in a typed array. ```javascript var arr = new Float16Array( [ 1.0, 2.0 ] ); -// Create an iterator: var it = arr.values(); -// Iterate over array elements... var v = it.next().value; // returns 1.0 @@ -1375,12 +1483,31 @@ var bool = it.next().done; // returns true ``` +The returned [iterator][mdn-iterator-protocol] protocol-compliant object has the following properties: + +- **next**: function which returns an [iterator][mdn-iterator-protocol] protocol-compliant object containing the next iterated value (if one exists) assigned to a `value` property and a `done` property having a `boolean` value indicating whether the [iterator][mdn-iterator-protocol] is finished. +- **return**: function which closes an [iterator][mdn-iterator-protocol] and returns a single (optional) argument in an [iterator][mdn-iterator-protocol] protocol-compliant object. + + + +#### Float16Array.prototype.with( index, value ) + +Returns a new typed array with the element at a provided index replaced with a provided value. + +```javascript +var arr = new Float16Array( [ 1.0, 2.0, 3.0 ] ); + +var out = arr.with( 0, 0.0 ); +// returns + +var v = out.get( 0 ); +// returns 0.0 +``` + -* * * -
@@ -1393,22 +1520,35 @@ var bool = it.next().done;
+* * * + ## Examples ```javascript -var randu = require( '@stdlib/random/base/randu' ); -var ctor = require( '@stdlib/array/float16' ); +var Uint16Array = require( '@stdlib/array/uint16' ); +var logEach = require( '@stdlib/console/log-each' ); +var Float16Array = require( '@stdlib/array/float16' ); -var arr; -var i; +// Create a half-precision floating-point number array by specifying a length: +var out = new Float16Array( 3 ); +logEach( '%s', out ); -arr = new ctor( 10 ); -for ( i = 0; i < arr.length; i++ ) { - arr[ i ] = randu() * 100.0; -} -console.log( arr ); +// Create a boolean array from an array of numbers: +var arr = [ 1.05, 2.05, 3.05 ]; +out = new Float16Array( arr ); +logEach( '%s', out ); + +// Create a half-precision floating-point number array from an array buffer: +arr = new Uint16Array( [ 1000, 2000, 3000, 4000 ] ); +out = new Float16Array( arr.buffer ); +logEach( '%s', out ); + +// Create a half-precision floating-point number array from an array buffer view: +arr = new Uint16Array( [ 1000, 2000, 3000, 4000 ] ); +out = new Float16Array( arr.buffer, 2, 2 ); +logEach( '%s', out ); ```
@@ -1427,21 +1567,6 @@ console.log( arr ); @@ -1450,29 +1575,13 @@ console.log( arr );