Skip to content

Commit 3a39709

Browse files
committed
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 ---
1 parent 96f487a commit 3a39709

2 files changed

Lines changed: 83 additions & 56 deletions

File tree

lib/node_modules/@stdlib/array/float16/lib/main.js

Lines changed: 67 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ var isFunction = require( '@stdlib/assert/is-function' );
3838
var floor = require( '@stdlib/math/base/special/floor' );
3939
var format = require( '@stdlib/string/format' );
4040
var Uint16Array = require( '@stdlib/array/uint16' );
41+
var Float64Array = require( '@stdlib/array/float64' );
42+
var isnan = require( '@stdlib/assert/is-nan' );
4143
var float64ToFloat16 = require( '@stdlib/number/float64/base/to-float16' );
4244
var toWord = require( '@stdlib/number/float16/base/to-word' );
4345
var fromWord = require( '@stdlib/number/float16/base/from-word' );
@@ -341,7 +343,7 @@ setReadOnly( Float16Array, 'from', function from( src ) {
341343
out = new this( len );
342344
buf = out._buffer; // eslint-disable-line no-underscore-dangle
343345
for ( i = 0; i < len; i++ ) {
344-
buf[ i ] = toWord( float64ToFloat16( clbk.call( thisArg, get( src, i ), i ) ) );
346+
buf[ i ] = toWord( float64ToFloat16( clbk.call( thisArg, get( src, i ), i ) ) ); // eslint-disable-line max-len
345347
}
346348
return out;
347349
}
@@ -750,6 +752,7 @@ setReadOnly( Float16Array.prototype, 'fill', function fill( value, start, end )
750752
var buf;
751753
var len;
752754
var i;
755+
var v;
753756

754757
if ( !isFloat16Array( this ) ) {
755758
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 )
789792
start = 0;
790793
end = len;
791794
}
795+
v = toWord( value );
792796
for ( i = start; i < end; i++ ) {
793-
buf[ i ] = toWord( value );
797+
buf[ i ] = v;
794798
}
795799
return this;
796800
});
@@ -1151,6 +1155,7 @@ setReadOnly( Float16Array.prototype, 'get', function get( idx ) {
11511155
setReadOnly( Float16Array.prototype, 'includes', function includes( searchElement, fromIndex ) {
11521156
var buf;
11531157
var i;
1158+
var v;
11541159

11551160
if ( !isFloat16Array( this ) ) {
11561161
throw new TypeError( 'invalid invocation. `this` is not a floating-point number array.' );
@@ -1172,8 +1177,9 @@ setReadOnly( Float16Array.prototype, 'includes', function includes( searchElemen
11721177
fromIndex = 0;
11731178
}
11741179
buf = this._buffer;
1180+
v = toWord( searchElement );
11751181
for ( i = fromIndex; i < this._length; i++ ) {
1176-
if ( searchElement === fromWord( buf[ i ] ) ) {
1182+
if ( v === buf[ i ] ) {
11771183
return true;
11781184
}
11791185
}
@@ -1214,6 +1220,7 @@ setReadOnly( Float16Array.prototype, 'includes', function includes( searchElemen
12141220
setReadOnly( Float16Array.prototype, 'indexOf', function indexOf( searchElement, fromIndex ) {
12151221
var buf;
12161222
var i;
1223+
var v;
12171224

12181225
if ( !isFloat16Array( this ) ) {
12191226
throw new TypeError( 'invalid invocation. `this` is not a floating-point number array.' );
@@ -1235,8 +1242,9 @@ setReadOnly( Float16Array.prototype, 'indexOf', function indexOf( searchElement,
12351242
fromIndex = 0;
12361243
}
12371244
buf = this._buffer;
1245+
v = toWord( searchElement );
12381246
for ( i = fromIndex; i < this._length; i++ ) {
1239-
if ( searchElement === fromWord( buf[ i ] ) ) {
1247+
if ( v === buf[ i ] ) {
12401248
return i;
12411249
}
12421250
}
@@ -1283,9 +1291,9 @@ setReadOnly( Float16Array.prototype, 'join', function join( separator ) {
12831291
separator = ',';
12841292
}
12851293
buf = this._buffer;
1286-
out = [];
1294+
out = new Float64Array( this._length );
12871295
for ( i = 0; i < this._length; i++ ) {
1288-
out.push( fromWord( buf[i] ) );
1296+
out[ i ] = fromWord( buf[ i ] );
12891297
}
12901298
return out.join( separator );
12911299
});
@@ -1429,6 +1437,7 @@ setReadOnly( Float16Array.prototype, 'keys', function keys() {
14291437
setReadOnly( Float16Array.prototype, 'lastIndexOf', function lastIndexOf( searchElement, fromIndex ) {
14301438
var buf;
14311439
var i;
1440+
var v;
14321441

14331442
if ( !isFloat16Array( this ) ) {
14341443
throw new TypeError( 'invalid invocation. `this` is not a floating-point number array.' );
@@ -1449,8 +1458,9 @@ setReadOnly( Float16Array.prototype, 'lastIndexOf', function lastIndexOf( search
14491458
fromIndex = this._length - 1;
14501459
}
14511460
buf = this._buffer;
1461+
v = toWord( searchElement );
14521462
for ( i = fromIndex; i >= 0; i-- ) {
1453-
if ( searchElement === fromWord( buf[ i ] ) ) {
1463+
if ( v === buf[ i ] ) {
14541464
return i;
14551465
}
14561466
}
@@ -1994,7 +2004,7 @@ setReadOnly( Float16Array.prototype, 'sort', function sort( compareFcn ) {
19942004
}
19952005
buf = this._buffer;
19962006
if ( arguments.length === 0 ) {
1997-
buf.sort();
2007+
buf.sort( defaultCompare );
19982008
return this;
19992009
}
20002010
if ( !isFunction( compareFcn ) ) {
@@ -2003,16 +2013,52 @@ setReadOnly( Float16Array.prototype, 'sort', function sort( compareFcn ) {
20032013
buf.sort( compare );
20042014
return this;
20052015

2016+
/**
2017+
* Default comparison function for float16 values.
2018+
*
2019+
* @private
2020+
* @param {unsigned16} a - unsigned 16-bit integer for comparison
2021+
* @param {unsigned16} b - unsigned 16-bit integer for comparison
2022+
* @returns {number} comparison result
2023+
*/
2024+
function defaultCompare( a, b ) { // eslint-disable-line stdlib/no-unnecessary-nested-functions
2025+
var x;
2026+
var y;
2027+
2028+
x = fromWord( a );
2029+
y = fromWord( b );
2030+
2031+
// Handle NaN...
2032+
if ( isnan( x ) && isnan( y ) ) {
2033+
return 0;
2034+
}
2035+
if ( isnan( x ) ) {
2036+
return 1;
2037+
}
2038+
if ( isnan( y ) ) {
2039+
return -1;
2040+
}
2041+
2042+
// Normal comparison
2043+
if ( x < y ) {
2044+
return -1;
2045+
}
2046+
if ( x > y ) {
2047+
return 1;
2048+
}
2049+
return 0;
2050+
}
2051+
20062052
/**
20072053
* Comparison function for sorting.
20082054
*
20092055
* @private
2010-
* @param {float16} a - first floating-point number for comparison
2011-
* @param {float16} b - second floating-point number for comparison
2056+
* @param {unsigned16} a - unsigned 16-bit integer for comparison
2057+
* @param {unsigned16} b - unsigned 16-bit integer for comparison
20122058
* @returns {number} comparison result
20132059
*/
20142060
function compare( a, b ) {
2015-
return compareFcn( a, b );
2061+
return compareFcn( fromWord( a ), fromWord( b ) );
20162062
}
20172063
});
20182064

@@ -2162,11 +2208,11 @@ setReadOnly( Float16Array.prototype, 'toLocaleString', function toLocaleString(
21622208
throw new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) );
21632209
}
21642210
buf = this._buffer;
2165-
out = [];
2211+
out = new Float64Array( this._length );
21662212
for ( i = 0; i < this._length; i++ ) {
2167-
out.push( fromWord( buf[ i ] ).toLocaleString( loc, opts ) );
2213+
out[ i ] = fromWord( buf[ i ] );
21682214
}
2169-
return out.join( ',' );
2215+
return out.toLocaleString( loc, opts );
21702216
});
21712217

21722218
/**
@@ -2258,43 +2304,16 @@ setReadOnly( Float16Array.prototype, 'toReversed', function toReversed() {
22582304
* // returns 2.0
22592305
*/
22602306
setReadOnly( Float16Array.prototype, 'toSorted', function toSorted( compareFcn ) {
2261-
var outbuf;
22622307
var out;
2263-
var len;
2264-
var buf;
2265-
var i;
22662308

22672309
if ( !isFloat16Array( this ) ) {
22682310
throw new TypeError( 'invalid invocation. `this` is not a floating-point number array.' );
22692311
}
2270-
len = this._length;
2271-
out = new this.constructor( len );
2272-
buf = this._buffer;
2273-
outbuf = out._buffer; // eslint-disable-line no-underscore-dangle
2274-
for ( i = 0; i < len; i++ ) {
2275-
outbuf[ i ] = buf[ i ];
2276-
}
2312+
out = new this.constructor( this );
22772313
if ( arguments.length === 0 ) {
2278-
outbuf.sort();
2279-
return out;
2280-
}
2281-
if ( !isFunction( compareFcn ) ) {
2282-
throw new TypeError( format( 'invalid argument. First argument must be a function. Value: `%s`.', compareFcn ) );
2283-
}
2284-
outbuf.sort( compare );
2285-
return out;
2286-
2287-
/**
2288-
* Comparison function for sorting.
2289-
*
2290-
* @private
2291-
* @param {boolean} a - first floating-point number for comparison
2292-
* @param {boolean} b - second floating-point number for comparison
2293-
* @returns {number} comparison result
2294-
*/
2295-
function compare( a, b ) {
2296-
return compareFcn( a, b );
2314+
return out.sort();
22972315
}
2316+
return out.sort( compareFcn );
22982317
});
22992318

23002319
/**
@@ -2323,10 +2342,10 @@ setReadOnly( Float16Array.prototype, 'toString', function toString() {
23232342
if ( !isFloat16Array( this ) ) {
23242343
throw new TypeError( 'invalid invocation. `this` is not a floating-point number array.' );
23252344
}
2326-
out = [];
23272345
buf = this._buffer;
2346+
out = new Float64Array( this._length );
23282347
for ( i = 0; i < this._length; i++ ) {
2329-
out.push( fromWord( buf[i] ) );
2348+
out[ i ] = fromWord( buf[ i ] );
23302349
}
23312350
return out.join( ',' );
23322351
});
@@ -2463,10 +2482,8 @@ setReadOnly( Float16Array.prototype, 'values', function values() {
24632482
* // returns 3.0
24642483
*/
24652484
setReadOnly( Float16Array.prototype, 'with', function copyWith( index, value ) {
2466-
var buf;
24672485
var out;
24682486
var len;
2469-
var i;
24702487

24712488
if ( !isFloat16Array( this ) ) {
24722489
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 ) {
24842501
if ( !isNumber( value ) ) {
24852502
throw new TypeError( format( 'invalid argument. Second argument must be a floating-point number. Value: `%s`.', value ) );
24862503
}
2487-
out = [];
2488-
buf = this._buffer;
2489-
for ( i = 0; i < len; i++ ) {
2490-
out[ i ] = fromWord( buf[ i ] );
2491-
}
2492-
out = new this.constructor( out );
2493-
buf = out._buffer; // eslint-disable-line no-underscore-dangle
2494-
buf[ index ] = toWord( value );
2504+
out = new this.constructor( this );
2505+
out.set( value, index );
24952506
return out;
24962507
});
24972508

lib/node_modules/@stdlib/array/float16/test/test.sort.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,22 @@ tape( 'the method sorts elements of a floating-point number array in-place', fun
154154
out = arr.sort( compareFcn );
155155
buf = new Uint16Array( out.buffer );
156156

157+
t.strictEqual( instanceOf( arr, Float16Array ), true, 'returns expected value' );
158+
t.strictEqual( out, arr, 'returns expected value' );
159+
t.strictEqual( arr.length, expected.length, 'returns expected value' );
160+
t.deepEqual( buf, expected, 'returns expected value' );
161+
162+
arr = new Float16Array( [ 2.05, 1.05, -1.05, 0.05, -2.05 ] );
163+
expected = new Uint16Array([
164+
toWord( -2.05 ),
165+
toWord( -1.05 ),
166+
toWord( 0.05 ),
167+
toWord( 1.05 ),
168+
toWord( 2.05 )
169+
]);
170+
out = arr.sort( compareFcn );
171+
buf = new Uint16Array( out.buffer );
172+
157173
t.strictEqual( instanceOf( arr, Float16Array ), true, 'returns expected value' );
158174
t.strictEqual( out, arr, 'returns expected value' );
159175
t.strictEqual( arr.length, expected.length, 'returns expected value' );

0 commit comments

Comments
 (0)