|
21 | 21 | // MODULES //
|
22 | 22 |
|
23 | 23 | var isComplexTypedArray = require( '@stdlib/array/base/assert/is-complex-typed-array' );
|
| 24 | +var isBooleanArray = require( '@stdlib/array/base/assert/is-booleanarray' ); |
24 | 25 | var isComplexLike = require( '@stdlib/assert/is-complex-like' );
|
| 26 | +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; |
25 | 27 | var real = require( '@stdlib/complex/real' );
|
26 | 28 | var imag = require( '@stdlib/complex/imag' );
|
27 | 29 | var reinterpret = require( '@stdlib/strided/base/reinterpret-complex' );
|
| 30 | +var reinterpretBoolean = require( '@stdlib/strided/base/reinterpret-boolean' ); |
28 | 31 | var isAccessorArray = require( '@stdlib/array/base/assert/is-accessor-array' );
|
29 | 32 | var resolveGetter = require( '@stdlib/array/base/resolve-getter' );
|
30 | 33 | var isSameValueZero = require( '@stdlib/assert/is-same-value-zero' );
|
@@ -132,6 +135,43 @@ function complex( x, value ) {
|
132 | 135 | return n;
|
133 | 136 | }
|
134 | 137 |
|
| 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 | + |
135 | 175 |
|
136 | 176 | // MAIN //
|
137 | 177 |
|
@@ -160,6 +200,9 @@ function countSameValueZero( x, value ) {
|
160 | 200 | if ( isComplexTypedArray( x, value ) ) {
|
161 | 201 | return complex( x, value );
|
162 | 202 | }
|
| 203 | + if ( isBooleanArray( x, value ) ) { |
| 204 | + return boolean( x, value ); |
| 205 | + } |
163 | 206 | return accessors( x, value );
|
164 | 207 | }
|
165 | 208 | return indexed( x, value );
|
|
0 commit comments