Skip to content

Commit d1ef4ee

Browse files
Jaysukh-409kgryte
andauthored
feat: add boolean dtype support to array/base/count-same-value-zero
PR-URL: #2474 Ref: #2304 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com>
1 parent fd396b3 commit d1ef4ee

File tree

2 files changed

+69
-0
lines changed
  • lib/node_modules/@stdlib/array/base/count-same-value-zero

2 files changed

+69
-0
lines changed

lib/node_modules/@stdlib/array/base/count-same-value-zero/lib/main.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,13 @@
2121
// MODULES //
2222

2323
var isComplexTypedArray = require( '@stdlib/array/base/assert/is-complex-typed-array' );
24+
var isBooleanArray = require( '@stdlib/array/base/assert/is-booleanarray' );
2425
var isComplexLike = require( '@stdlib/assert/is-complex-like' );
26+
var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive;
2527
var real = require( '@stdlib/complex/real' );
2628
var imag = require( '@stdlib/complex/imag' );
2729
var reinterpret = require( '@stdlib/strided/base/reinterpret-complex' );
30+
var reinterpretBoolean = require( '@stdlib/strided/base/reinterpret-boolean' );
2831
var isAccessorArray = require( '@stdlib/array/base/assert/is-accessor-array' );
2932
var resolveGetter = require( '@stdlib/array/base/resolve-getter' );
3033
var isSameValueZero = require( '@stdlib/assert/is-same-value-zero' );
@@ -132,6 +135,43 @@ function complex( x, value ) {
132135
return n;
133136
}
134137

138+
/**
139+
* Counts the number of elements in a boolean array that are equal to a specified value.
140+
*
141+
* @private
142+
* @param {Collection} x - input array
143+
* @param {*} value - search value
144+
* @returns {NonNegativeInteger} number of elements that are equal to a specified value
145+
*
146+
* @example
147+
* var BooleanArray = require( '@stdlib/array/bool' );
148+
*
149+
* var x = new BooleanArray( [ true, false, true, false, true ] );
150+
*
151+
* var n = boolean( x, true );
152+
* // returns 3
153+
*/
154+
function boolean( x, value ) {
155+
var view;
156+
var n;
157+
var v;
158+
var i;
159+
160+
if ( !isBoolean( value ) ) {
161+
return 0;
162+
}
163+
view = reinterpretBoolean( x, 0 );
164+
165+
v = ( value ) ? 1 : 0;
166+
n = 0;
167+
for ( i = 0; i < view.length; i++ ) {
168+
if ( view[ i ] === v ) {
169+
n += 1;
170+
}
171+
}
172+
return n;
173+
}
174+
135175

136176
// MAIN //
137177

@@ -160,6 +200,9 @@ function countSameValueZero( x, value ) {
160200
if ( isComplexTypedArray( x, value ) ) {
161201
return complex( x, value );
162202
}
203+
if ( isBooleanArray( x, value ) ) {
204+
return boolean( x, value );
205+
}
163206
return accessors( x, value );
164207
}
165208
return indexed( x, value );

lib/node_modules/@stdlib/array/base/count-same-value-zero/test/test.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
var tape = require( 'tape' );
2424
var Complex128 = require( '@stdlib/complex/float64/ctor' );
2525
var Complex128Array = require( '@stdlib/array/complex128' );
26+
var BooleanArray = require( '@stdlib/array/bool' );
2627
var Int32Array = require( '@stdlib/array/int32' );
2728
var Float32Array = require( '@stdlib/array/float32' );
2829
var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' );
@@ -153,6 +154,31 @@ tape( 'the function considers all NaN values to be identical (real typed array)'
153154
t.end();
154155
});
155156

157+
tape( 'the function counts the number of same values (boolean array)', function test( t ) {
158+
var expected;
159+
var actual;
160+
var x;
161+
162+
x = new BooleanArray( [ true, false, true, false, true ] );
163+
expected = 3;
164+
actual = countSameValueZero( x, true );
165+
166+
t.strictEqual( actual, expected, 'returns expected value' );
167+
168+
x = new BooleanArray( [ true, false, true, false, true ] );
169+
expected = 2;
170+
actual = countSameValueZero( x, false );
171+
172+
t.strictEqual( actual, expected, 'returns expected value' );
173+
174+
x = new BooleanArray( [ true, false, true, false, true ] );
175+
expected = 0;
176+
actual = countSameValueZero( x, 'beep' );
177+
178+
t.strictEqual( actual, expected, 'returns expected value' );
179+
t.end();
180+
});
181+
156182
tape( 'the function counts the number of same values (complex typed array)', function test( t ) {
157183
var expected;
158184
var actual;

0 commit comments

Comments
 (0)