Skip to content

feat: add boolean dtype support to array/base/assert/has-same-values #2404

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

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

var isComplex128Array = require( '@stdlib/array/base/assert/is-complex128array' );
var isComplex64Array = require( '@stdlib/array/base/assert/is-complex64array' );
var isBooleanArray = require( '@stdlib/array/base/assert/is-booleanarray' );
var arraylike2object = require( '@stdlib/array/base/arraylike2object' );
var reinterpret128 = require( '@stdlib/strided/base/reinterpret-complex128' );
var reinterpret64 = require( '@stdlib/strided/base/reinterpret-complex64' );
var reinterpretBoolean = require( '@stdlib/strided/base/reinterpret-boolean' );
var isSameValue = require( '@stdlib/assert/is-same-value' );


Expand Down Expand Up @@ -150,6 +152,13 @@ function hasSameValues( x, y ) {
if ( xo.accessorProtocol || yo.accessorProtocol ) {
FLG = 2;

// If provided boolean arrays, reinterpret the arrays to avoid using accessors to access array elements...
if ( isBooleanArray( x ) ) {
if ( isBooleanArray( y ) ) {
return internal( reinterpretBoolean( x, 0 ), reinterpretBoolean( y, 0 ) );
}
return accessors( xo, yo );
}
// If provided a complex number array, reinterpret as a real typed array and test interleaved real and imaginary components...
if ( isComplex128Array( x ) ) {
xr = reinterpret128( x, 0 );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ var AccessorArray = require( '@stdlib/array/base/accessor' );
var Float64Array = require( '@stdlib/array/float64' );
var Complex64Array = require( '@stdlib/array/complex64' );
var Complex128Array = require( '@stdlib/array/complex128' );
var BooleanArray = require( '@stdlib/array/bool' );
var hasSameValues = require( './../lib' );


Expand Down Expand Up @@ -111,6 +112,23 @@ tape( 'if provided empty collections, the function returns `true` (mixed)', func
t.end();
});

tape( 'if provided empty collections, the function returns `true` (boolean array)', function test( t ) {
var out;
var x;
var y;

x = new BooleanArray( [] );
out = hasSameValues( x, x );
t.strictEqual( out, true, 'returns expected value' );

x = new BooleanArray( [] );
y = new BooleanArray( [] );
out = hasSameValues( x, y );
t.strictEqual( out, true, 'returns expected value' );

t.end();
});

tape( 'if provided empty collections, the function returns `true` (complex typed array)', function test( t ) {
var out;
var x;
Expand Down Expand Up @@ -206,6 +224,23 @@ tape( 'the function returns `true` if both arrays have the same values (mixed)',
t.end();
});

tape( 'the function returns `true` if both arrays have the same values (boolean array)', function test( t ) {
var out;
var x;
var y;

x = new BooleanArray( [ true, false, true ] );
out = hasSameValues( x, x );
t.strictEqual( out, true, 'returns expected value' );

x = new BooleanArray( [ true, false, true ] );
y = new BooleanArray( [ true, false, true ] );
out = hasSameValues( x, y );
t.strictEqual( out, true, 'returns expected value' );

t.end();
});

tape( 'the function returns `true` if both arrays have the same values (real typed array)', function test( t ) {
var out;
var x;
Expand Down Expand Up @@ -330,6 +365,19 @@ tape( 'the function returns `false` if both arrays do not have the same values (
t.end();
});

tape( 'the function returns `false` if both arrays do not have the same values (boolean array)', function test( t ) {
var out;
var x;
var y;

x = new BooleanArray( [ true, false, false, true ] );
y = new BooleanArray( [ true, true, false, false ] );
out = hasSameValues( x, y );
t.strictEqual( out, false, 'returns expected value' );

t.end();
});

tape( 'the function returns `false` if both arrays do not have the same values (complex typed array)', function test( t ) {
var out;
var x;
Expand Down