Skip to content

feat: add boolean dtype support to array/base/count-same-value #2473

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 5 commits into from
Jun 29, 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
43 changes: 43 additions & 0 deletions lib/node_modules/@stdlib/array/base/count-same-value/lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@
// MODULES //

var isComplexLike = require( '@stdlib/assert/is-complex-like' );
var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive;
var reinterpret = require( '@stdlib/strided/base/reinterpret-complex' );
var reinterpretBoolean = require( '@stdlib/strided/base/reinterpret-boolean' );
var isComplexTypedArray = require( '@stdlib/array/base/assert/is-complex-typed-array' );
var isBooleanArray = require( '@stdlib/array/base/assert/is-booleanarray' );
var isAccessorArray = require( '@stdlib/array/base/assert/is-accessor-array' );
var resolveGetter = require( '@stdlib/array/base/resolve-getter' );
var isSameValue = require( '@stdlib/assert/is-same-value' );
Expand Down Expand Up @@ -132,6 +135,43 @@ function complex( x, value ) {
return n;
}

/**
* Counts the number of elements in a boolean array that are equal to a specified value.
*
* @private
* @param {Collection} x - input array
* @param {*} value - search value
* @returns {NonNegativeInteger} number of elements that are equal to a specified value
*
* @example
* var BooleanArray = require( '@stdlib/array/bool' );
*
* var x = new BooleanArray( [ true, false, true, false, true ] );
*
* var n = boolean( x, true );
* // returns 3
*/
function boolean( x, value ) {
var view;
var n;
var v;
var i;

if ( !isBoolean( value ) ) {
return 0;
}
view = reinterpretBoolean( x, 0 );

v = ( value ) ? 1 : 0;
n = 0;
for ( i = 0; i < view.length; i++ ) {
if ( view[ i ] === v ) {
n += 1;
}
}
return n;
}


// MAIN //

Expand Down Expand Up @@ -160,6 +200,9 @@ function countSameValue( x, value ) {
if ( isComplexTypedArray( x, value ) ) {
return complex( x, value );
}
if ( isBooleanArray( x, value ) ) {
return boolean( x, value );
}
return accessors( x, value );
}
return indexed( x, value );
Expand Down
26 changes: 26 additions & 0 deletions lib/node_modules/@stdlib/array/base/count-same-value/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
var tape = require( 'tape' );
var Complex128 = require( '@stdlib/complex/float64/ctor' );
var Complex128Array = require( '@stdlib/array/complex128' );
var BooleanArray = require( '@stdlib/array/bool' );
var Int32Array = require( '@stdlib/array/int32' );
var Float32Array = require( '@stdlib/array/float32' );
var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' );
Expand Down Expand Up @@ -169,6 +170,31 @@ tape( 'the function considers all NaN values to be identical (real typed array)'
t.end();
});

tape( 'the function counts the number of same values (boolean array)', function test( t ) {
var expected;
var actual;
var x;

x = new BooleanArray( [ true, false, true, false, true ] );
expected = 3;
actual = countSameValue( x, true );

t.strictEqual( actual, expected, 'returns expected value' );

x = new BooleanArray( [ true, false, true, false, true ] );
expected = 2;
actual = countSameValue( x, false );

t.strictEqual( actual, expected, 'returns expected value' );

x = new BooleanArray( [ true, false, true, false, true ] );
expected = 0;
actual = countSameValue( x, 'beep' );

t.strictEqual( actual, expected, 'returns expected value' );
t.end();
});

tape( 'the function counts the number of same values (complex typed array)', function test( t ) {
var expected;
var actual;
Expand Down
Loading