From f1a67c33e4d5538c8b84d980f4ba2db0ac365dfb Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Sat, 29 Jun 2024 06:54:12 +0000 Subject: [PATCH] Auto-generated commit --- CHANGELOG.md | 2 ++ base/count-same-value-zero/lib/main.js | 43 +++++++++++++++++++++++++ base/count-same-value-zero/test/test.js | 26 +++++++++++++++ 3 files changed, 71 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 33e60225..795d262d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -671,6 +671,7 @@ This release closes the following issue: ##### Features +- [`d1ef4ee`](https://github.com/stdlib-js/stdlib/commit/d1ef4ee589cb2a7a0d8bbc0b37be3cf3b7a6aad2) - add boolean dtype support to `array/base/count-same-value-zero` [(#2474)](https://github.com/stdlib-js/stdlib/pull/2474) - [`1ed81b7`](https://github.com/stdlib-js/stdlib/commit/1ed81b78fd6469d374c7b033f258e709934539b2) - add `array/base/count-same-value-zero` [(##1384)](#1384) @@ -2288,6 +2289,7 @@ A total of 13 people contributed to this release. Thank you to the following con
+- [`d1ef4ee`](https://github.com/stdlib-js/stdlib/commit/d1ef4ee589cb2a7a0d8bbc0b37be3cf3b7a6aad2) - **feat:** add boolean dtype support to `array/base/count-same-value-zero` [(#2474)](https://github.com/stdlib-js/stdlib/pull/2474) _(by Jaysukh Makvana, Athan Reines)_ - [`fd396b3`](https://github.com/stdlib-js/stdlib/commit/fd396b360cbca9c4c000d3f33eed5dfc18de6d6b) - **feat:** add boolean dtype support to `array/base/count-same-value` [(#2473)](https://github.com/stdlib-js/stdlib/pull/2473) _(by Jaysukh Makvana, Athan Reines)_ - [`29615af`](https://github.com/stdlib-js/stdlib/commit/29615af970796a43f65f4b00d29bd23a122f2208) - **feat:** add `slice` and `subarray` methods to `array/bool` [(#2472)](https://github.com/stdlib-js/stdlib/pull/2472) _(by Jaysukh Makvana, Athan Reines)_ - [`83044a1`](https://github.com/stdlib-js/stdlib/commit/83044a19f51a143c06d5bb59900d08e02f66e3c4) - **docs:** update namespace TypeScript declarations [(#2465)](https://github.com/stdlib-js/stdlib/pull/2465) _(by stdlib-bot, Athan Reines)_ diff --git a/base/count-same-value-zero/lib/main.js b/base/count-same-value-zero/lib/main.js index e131609b..bca168e5 100644 --- a/base/count-same-value-zero/lib/main.js +++ b/base/count-same-value-zero/lib/main.js @@ -21,10 +21,13 @@ // MODULES // var isComplexTypedArray = require( './../../../base/assert/is-complex-typed-array' ); +var isBooleanArray = require( './../../../base/assert/is-booleanarray' ); var isComplexLike = require( '@stdlib/assert/is-complex-like' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; var real = require( '@stdlib/complex/real' ); var imag = require( '@stdlib/complex/imag' ); var reinterpret = require( '@stdlib/strided/base/reinterpret-complex' ); +var reinterpretBoolean = require( '@stdlib/strided/base/reinterpret-boolean' ); var isAccessorArray = require( './../../../base/assert/is-accessor-array' ); var resolveGetter = require( './../../../base/resolve-getter' ); var isSameValueZero = require( '@stdlib/assert/is-same-value-zero' ); @@ -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 // @@ -160,6 +200,9 @@ function countSameValueZero( 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 ); diff --git a/base/count-same-value-zero/test/test.js b/base/count-same-value-zero/test/test.js index 66a4b66a..68565490 100644 --- a/base/count-same-value-zero/test/test.js +++ b/base/count-same-value-zero/test/test.js @@ -23,6 +23,7 @@ var tape = require( 'tape' ); var Complex128 = require( '@stdlib/complex/float64/ctor' ); var Complex128Array = require( './../../../complex128' ); +var BooleanArray = require( './../../../bool' ); var Int32Array = require( './../../../int32' ); var Float32Array = require( './../../../float32' ); var toAccessorArray = require( './../../../base/to-accessor-array' ); @@ -153,6 +154,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 = countSameValueZero( x, true ); + + t.strictEqual( actual, expected, 'returns expected value' ); + + x = new BooleanArray( [ true, false, true, false, true ] ); + expected = 2; + actual = countSameValueZero( x, false ); + + t.strictEqual( actual, expected, 'returns expected value' ); + + x = new BooleanArray( [ true, false, true, false, true ] ); + expected = 0; + actual = countSameValueZero( 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;