Skip to content

Commit 6c18ce2

Browse files
refactor: update toSorted method
- update method logic and insert it into correct place in both main.js and readme - update type checking in benchmarks - use hasSameValues in test file
1 parent 872f8b2 commit 6c18ce2

File tree

5 files changed

+106
-125
lines changed

5 files changed

+106
-125
lines changed

lib/node_modules/@stdlib/array/fixed-endian-factory/README.md

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -607,39 +607,6 @@ var idx = arr.lastIndexOf( 5.0 );
607607
// returns -1
608608
```
609609

610-
<a name="method-toSorted"></a>
611-
612-
#### TypedArrayFE.prototype.toSorted( compareFcn )
613-
614-
Returns a new typed array containing the elements in sorted order.
615-
616-
```javascript
617-
function compareFcn( a, b ) {
618-
if ( a > b ) {
619-
return 1;
620-
}
621-
if ( a < b ) {
622-
return -1;
623-
}
624-
return 0;
625-
}
626-
var Float64ArrayFE = fixedEndianFactory( 'float64' );
627-
var arr = new Float64ArrayFE( 'little-endian', [ 3.0, 1.0, 2.0 ] );
628-
// returns <Float64ArrayFE>
629-
630-
var out=arr.toSorted( compareFcn );
631-
// returns <Float64ArrayFE>
632-
633-
var v=out.get(0);
634-
// returns 1.0
635-
636-
v=out.get(1);
637-
// returns 2.0
638-
639-
v=out.get(2);
640-
// returns 3.0
641-
```
642-
643610
<a name="method-map"></a>
644611

645612
#### TypedArray.prototype.map( callbackFn\[, thisArg] )
@@ -896,6 +863,39 @@ var count = context.count;
896863
// returns 3
897864
```
898865

866+
<a name="method-to-sorted"></a>
867+
868+
#### TypedArrayFE.prototype.toSorted( \[compareFcn] )
869+
870+
Returns a new typed array containing the elements in sorted order.
871+
872+
```javascript
873+
function compareFcn( a, b ) {
874+
if ( a > b ) {
875+
return 1;
876+
}
877+
if ( a < b ) {
878+
return -1;
879+
}
880+
return 0;
881+
}
882+
var Float64ArrayFE = fixedEndianFactory( 'float64' );
883+
var arr = new Float64ArrayFE( 'little-endian', [ 3.0, 1.0, 2.0 ] );
884+
// returns <Float64ArrayFE>
885+
886+
var out = arr.toSorted( compareFcn );
887+
// returns <Float64ArrayFE>
888+
889+
var v = out.get( 0 );
890+
// returns 1.0
891+
892+
v = out.get( 1 );
893+
// returns 2.0
894+
895+
v = out.get( 2 );
896+
// returns 3.0
897+
```
898+
899899
<a name="method-to-string"></a>
900900

901901
#### TypedArrayFE.prototype.toString()

lib/node_modules/@stdlib/array/fixed-endian-factory/benchmark/benchmark.to_sorted.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
// MODULES //
2222

2323
var bench = require( '@stdlib/bench' );
24-
var isTypedArray = require( '@stdlib/assert/is-typed-array' );
2524
var pkg = require( './../package.json' ).name;
2625
var factory = require( './../lib' );
2726

@@ -48,7 +47,7 @@ bench( pkg+':toSorted', function benchmark( b ) {
4847
}
4948
}
5049
b.toc();
51-
if ( !isTypedArray( out ) ) {
50+
if ( !( out instanceof Float64ArrayFE ) ) {
5251
b.fail( 'should return a typed array' );
5352
}
5453
b.pass( 'benchmark finished' );

lib/node_modules/@stdlib/array/fixed-endian-factory/benchmark/benchmark.to_sorted.length.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
var bench = require( '@stdlib/bench' );
2424
var pow = require( '@stdlib/math/base/special/pow' );
2525
var zeroTo = require( '@stdlib/array/zero-to' );
26-
var isTypedArray = require( '@stdlib/assert/is-typed-array' );
2726
var pkg = require( './../package.json' ).name;
2827
var factory = require( './../lib' );
2928

@@ -47,7 +46,7 @@ function compareFcn( a, b ) {
4746
if ( a > b ) {
4847
return 1;
4948
}
50-
if ( a < b) {
49+
if ( a < b ) {
5150
return -1;
5251
}
5352
return 0;
@@ -82,7 +81,7 @@ function createBenchmark( len ) {
8281
}
8382
}
8483
b.toc();
85-
if ( !isTypedArray( out ) ) {
84+
if ( !( out instanceof Float64ArrayFE ) ) {
8685
b.fail( 'should return a typed array' );
8786
}
8887
b.pass( 'benchmark finished' );

lib/node_modules/@stdlib/array/fixed-endian-factory/lib/main.js

Lines changed: 63 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -669,70 +669,6 @@ function factory( dtype ) { // eslint-disable-line max-lines-per-function, stdli
669669
return this._buffer[ GETTER ]( idx*BYTES_PER_ELEMENT, this._isLE );
670670
});
671671

672-
/**
673-
* Returns a new typed array containing the elements in sorted order.
674-
*
675-
* @private
676-
* @name toSorted
677-
* @memberof TypedArray.prototype
678-
* @type {Function}
679-
* @param {Function} compareFcn - comparison function
680-
* @throws {TypeError} `this` must be a typed array
681-
* @throws {TypeError} first argument must be a function
682-
* @returns {TypedArray} sorted array
683-
*/
684-
setReadOnly( TypedArray.prototype, 'toSorted', function sort( compareFcn ) {
685-
var minIdx;
686-
var minVal;
687-
var obuf;
688-
var buf;
689-
var len;
690-
var out;
691-
var val;
692-
var i;
693-
var j;
694-
695-
if ( !isTypedArray( this ) ) {
696-
throw new TypeError( format( 'invalid invocation. `this` is not %s %s.', CHAR2ARTICLE[ dtype[0] ], CTOR_NAME ) );
697-
}
698-
if ( arguments.length === 0 ) {
699-
compareFcn = function defaultCompareFcn( a, b ) {
700-
if ( a > b ) {
701-
return 1;
702-
}
703-
if ( a < b ) {
704-
return -1;
705-
}
706-
return 0;
707-
};
708-
} else if ( !isFunction( compareFcn ) ) {
709-
throw new TypeError( format( 'invalid argument. First argument must be a function. Value: `%s`.', compareFcn ) );
710-
}
711-
len = this._length;
712-
out = new this.constructor( flag2byteOrder( this._isLE ), len );
713-
buf = this._buffer;
714-
obuf = out._buffer; // eslint-disable-line no-underscore-dangle
715-
for ( i = 0; i < len; i++ ) {
716-
obuf[ SETTER ]( i * BYTES_PER_ELEMENT, buf[ GETTER ]( i * BYTES_PER_ELEMENT, this._isLE ), this._isLE );
717-
}
718-
for ( i = 0; i < len - 1; i++ ) {
719-
minIdx = i;
720-
minVal = obuf[ GETTER ]( i * BYTES_PER_ELEMENT, this._isLE );
721-
for ( j = i+1; j < len; j++ ) {
722-
val = obuf[ GETTER ]( j * BYTES_PER_ELEMENT, this._isLE );
723-
if ( compareFcn( minVal, val ) > 0 ) {
724-
minIdx = j;
725-
minVal = val;
726-
}
727-
}
728-
if ( i !== minIdx ) {
729-
obuf[ SETTER ]( minIdx * BYTES_PER_ELEMENT, obuf[ GETTER ]( i * BYTES_PER_ELEMENT, this._isLE ), this._isLE );
730-
obuf[ SETTER ]( i * BYTES_PER_ELEMENT, minVal, this._isLE );
731-
}
732-
}
733-
return out;
734-
});
735-
736672
/**
737673
* Returns the index of the first occurrence of a given element.
738674
*
@@ -1078,6 +1014,69 @@ function factory( dtype ) { // eslint-disable-line max-lines-per-function, stdli
10781014
return false;
10791015
});
10801016

1017+
/**
1018+
* Returns a new typed array containing the elements in sorted order.
1019+
*
1020+
* @private
1021+
* @name toSorted
1022+
* @memberof TypedArray.prototype
1023+
* @type {Function}
1024+
* @param {Function} [compareFcn] - comparison function
1025+
* @throws {TypeError} `this` must be a typed array
1026+
* @throws {TypeError} first argument must be a function
1027+
* @returns {TypedArray} sorted array
1028+
*/
1029+
setReadOnly( TypedArray.prototype, 'toSorted', function toSorted( compareFcn ) {
1030+
var obuf;
1031+
var buf;
1032+
var tmp;
1033+
var len;
1034+
var out;
1035+
var i;
1036+
1037+
if ( !isTypedArray( this ) ) {
1038+
throw new TypeError( format( 'invalid invocation. `this` is not %s %s.', CHAR2ARTICLE[ dtype[0] ], CTOR_NAME ) );
1039+
}
1040+
if ( arguments.length === 0 ) {
1041+
compareFcn = defaultCompareFcn;
1042+
} else if ( !isFunction( compareFcn ) ) {
1043+
throw new TypeError( format( 'invalid argument. First argument must be a function. Value: `%s`.', compareFcn ) );
1044+
}
1045+
1046+
len = this._length;
1047+
out = new this.constructor( flag2byteOrder( this._isLE ), len );
1048+
buf = this._buffer;
1049+
obuf = out._buffer; // eslint-disable-line no-underscore-dangle
1050+
tmp = [];
1051+
1052+
for ( i = 0; i < len; i++ ) {
1053+
tmp.push( buf[ GETTER ]( i * BYTES_PER_ELEMENT, this._isLE ) );
1054+
}
1055+
tmp.sort( compareFcn );
1056+
1057+
for ( i = 0; i < len; i++ ) {
1058+
obuf[ SETTER ]( i * BYTES_PER_ELEMENT, tmp[ i ], this._isLE );
1059+
}
1060+
return out;
1061+
1062+
/**
1063+
* Comparison function.
1064+
*
1065+
* @private
1066+
* @param {number} a - first value for comparison
1067+
* @param {number} b - second value for comparison
1068+
* @returns {number} comparison result
1069+
*/
1070+
function defaultCompareFcn( a, b ) {
1071+
if ( a > b ) {
1072+
return 1;
1073+
}
1074+
if ( a < b ) {
1075+
return -1;
1076+
}
1077+
return 0;
1078+
}
1079+
});
10811080
/**
10821081
* Serializes an array as a string.
10831082
*

lib/node_modules/@stdlib/array/fixed-endian-factory/test/test.to_sorted.js

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
var tape = require( 'tape' );
2424
var hasOwnProp = require( '@stdlib/assert/has-own-property' );
25+
var hasSameValues = require( '@stdlib/array/base/assert/has-same-values' );
2526
var isFunction = require( '@stdlib/assert/is-function' );
2627
var factory = require( '@stdlib/array/fixed-endian-factory/lib' );
2728

@@ -148,52 +149,35 @@ tape( 'the method returns an empty array if operating on an empty array', functi
148149

149150
tape( 'the method returns a new typed array containing elements in ascending order', function test( t ) {
150151
var expected;
151-
var values;
152152
var ctor;
153153
var arr;
154154
var out;
155-
var i;
156155

157156
ctor = factory( 'float64' );
158157
arr = new ctor( 'little-endian', [ 5.0, 4.0, 1.0, 2.0, 3.0 ] );
159-
expected = [ 1.0, 2.0, 3.0, 4.0, 5.0 ];
158+
expected = new ctor( 'little-endian', [ 1.0, 2.0, 3.0, 4.0, 5.0 ] );
160159
out = arr.toSorted( compareFcn );
161-
values = [];
162-
fcn();
160+
163161
t.strictEqual( out instanceof ctor, true, 'returns expected value' );
164162
t.notEqual( out, arr, 'returns a new instance' );
165163
t.strictEqual( out.length, expected.length, 'returns expected value' );
166-
t.deepEqual( values, expected, 'returns expected value' );
167-
function fcn() {
168-
for ( i = 0; i < out.length; i++ ) {
169-
values.push( out.at( i ) );
170-
}
171-
}
164+
t.strictEqual( hasSameValues( out, expected ), true, 'returns expected value' );
172165
t.end();
173166
});
174167

175168
tape( 'the method returns a new typed array containing elements in ascending order without passing a comparison function', function test( t ) {
176169
var expected;
177-
var values;
178170
var ctor;
179171
var arr;
180172
var out;
181-
var i;
182173

183174
ctor = factory( 'float64' );
184175
arr = new ctor( 'little-endian', [ 5.0, 4.0, 1.0, 2.0, 3.0 ] );
185-
expected = [ 1.0, 2.0, 3.0, 4.0, 5.0 ];
176+
expected = new ctor( 'little-endian', [ 1.0, 2.0, 3.0, 4.0, 5.0 ] );
186177
out = arr.toSorted();
187-
values = [];
188-
fcn();
189-
t.strictEqual( out.length, expected.length, 'returns expected value' );
190-
t.deepEqual( values, expected, 'returns expected value' );
191178

192-
function fcn() {
193-
for ( i = 0; i < out.length; i++ ) {
194-
values.push( out.at( i ) );
195-
}
196-
}
179+
t.strictEqual( out.length, expected.length, 'returns expected value' );
180+
t.strictEqual( hasSameValues( out, expected ), true, 'returns expected value' );
197181
t.end();
198182
});
199183

0 commit comments

Comments
 (0)