From 98df9fa8b3423b211f1b279372ee973ba67385b2 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Sat, 16 Mar 2024 01:10:26 +0000 Subject: [PATCH] Auto-generated commit --- base/count-same-value-zero/README.md | 30 +++++++++++++-- base/count-same-value-zero/docs/repl.txt | 7 +++- .../docs/types/index.d.ts | 10 +++-- .../{example => examples}/index.js | 10 ++--- base/count-same-value-zero/lib/index.js | 2 +- base/count-same-value-zero/lib/main.js | 37 ++++++++++--------- base/count-same-value-zero/package.json | 6 ++- base/count-same-value-zero/test/test.js | 24 ++++++++---- base/lib/index.js | 9 +++++ 9 files changed, 92 insertions(+), 43 deletions(-) rename base/count-same-value-zero/{example => examples}/index.js (82%) diff --git a/base/count-same-value-zero/README.md b/base/count-same-value-zero/README.md index cb75ff0c..9a388b17 100644 --- a/base/count-same-value-zero/README.md +++ b/base/count-same-value-zero/README.md @@ -20,7 +20,7 @@ limitations under the License. # countSameValueZero -> Count the number of elements that are equal to a given value in an array. +> Count the number of elements in an array that are equal to a specified value. @@ -42,7 +42,7 @@ var countSameValueZero = require( '@stdlib/array/base/count-same-value-zero' ); #### countSameValueZero( x, value ) -Counts the number of elements that are equal to a given value in an array. +Counts the number of elements in an array that are equal to a specified value. ```javascript var x = [ 0, 1, 0, 1, 2 ]; @@ -51,6 +51,24 @@ var out = countSameValueZero( x, 1 ); // returns 2 ``` +In contrast to an implementation using the strict equality operator `===`, the function distinguishes treats `NaNs` as the same value. + +```javascript +var x = [ NaN, NaN, NaN ]; + +var out = countSameValueZero( x, NaN ); +// returns 3 +``` + +In contrast to an implementation using the [SameValue Algorithm][@stdlib/array/base/count-same-value] (as specified in ECMAScript 5), the function does not distinguish between `+0` and `-0`. + +```javascript +var x = [ 0.0, -0.0, 0.0 ]; + +var out = countSameValueZero( x, 0.0 ); +// returns 3 +``` + @@ -72,10 +90,12 @@ var out = countSameValueZero( x, 1 ); ```javascript -var sample = require( '@stdlib/random/sample' ); +var bernoulli = require( '@stdlib/random/array/bernoulli' ); var countSameValueZero = require( '@stdlib/array/base/count-same-value-zero' ); -var x = sample( [ 1, 2, 3, 4, 5 ] ); +var x = bernoulli( 10, 0.5, { + 'dtype': 'generic' +}); console.log( x ); var n = countSameValueZero( x, 1 ); @@ -106,6 +126,8 @@ console.log( n ); diff --git a/base/count-same-value-zero/docs/repl.txt b/base/count-same-value-zero/docs/repl.txt index dd1d1fc1..f74b77f6 100644 --- a/base/count-same-value-zero/docs/repl.txt +++ b/base/count-same-value-zero/docs/repl.txt @@ -1,6 +1,9 @@ {{alias}}( x, value ) - Counts the number of elements that are equal to a given value in an array. + Counts the number of elements in an array that are equal to a specified + value. + + The function treats `NaN` values as the same value. Parameters ---------- @@ -13,7 +16,7 @@ Returns ------- out: integer - Number of elements that are equal to the given value. + Number of elements that are equal to a specified value. Examples -------- diff --git a/base/count-same-value-zero/docs/types/index.d.ts b/base/count-same-value-zero/docs/types/index.d.ts index fdc41966..4ba49596 100644 --- a/base/count-same-value-zero/docs/types/index.d.ts +++ b/base/count-same-value-zero/docs/types/index.d.ts @@ -23,11 +23,15 @@ import { Collection } from '@stdlib/types/array'; /** -* Counts the number of elements that are equal to a given value in an array. +* Counts the number of elements in an array that are equal to a specified value. +* +* ## Notes +* +* - In contrast to an implementation based on the strict equality operator `===`, the function treats `NaNs` as the same value. * * @param x - input array -* @param value - given value -* @returns number of elements that are equal to the given value +* @param value - search value +* @returns number of elements that are equal to a specified value * * @example * var x = [ 0, 1, 0, 1, 1 ]; diff --git a/base/count-same-value-zero/example/index.js b/base/count-same-value-zero/examples/index.js similarity index 82% rename from base/count-same-value-zero/example/index.js rename to base/count-same-value-zero/examples/index.js index c85282e9..27e165dc 100644 --- a/base/count-same-value-zero/example/index.js +++ b/base/count-same-value-zero/examples/index.js @@ -18,13 +18,13 @@ 'use strict'; -var sample = require( '@stdlib/random/sample' ); +var bernoulli = require( '@stdlib/random/array/bernoulli' ); var countSameValueZero = require( './../lib' ); -var x; -var n; -x = sample( [ 1, 2, 3, 4, 5 ] ); +var x = bernoulli( 10, 0.5, { + 'dtype': 'generic' +}); console.log( x ); -n = countSameValueZero( x, 1 ); +var n = countSameValueZero( x, 1 ); console.log( n ); diff --git a/base/count-same-value-zero/lib/index.js b/base/count-same-value-zero/lib/index.js index 75abf6a6..a115d773 100644 --- a/base/count-same-value-zero/lib/index.js +++ b/base/count-same-value-zero/lib/index.js @@ -19,7 +19,7 @@ 'use strict'; /** -* Count the number of elements that are equal to a given value in an array. +* Count the number of elements in an array that are equal to a specified value. * * @module @stdlib/array/base/count-same-value-zero * diff --git a/base/count-same-value-zero/lib/main.js b/base/count-same-value-zero/lib/main.js index 37ebb8a0..23efa38d 100644 --- a/base/count-same-value-zero/lib/main.js +++ b/base/count-same-value-zero/lib/main.js @@ -33,12 +33,12 @@ var isSameValueZero = require( '@stdlib/assert/is-same-value-zero' ); // FUNCTIONS // /** -* Counts the number of elements that are equal to a given value in an indexed array. +* Counts the number of elements in an indexed array that are equal to a specified value. * * @private * @param {Collection} x - input array -* @param {*} value - given value -* @returns {NonNegativeInteger} number of elements that are equal to the given value +* @param {*} value - search value +* @returns {NonNegativeInteger} number of elements that are equal to a specified value * * @example * var x = [ 0, 0, 1, 0, 1 ]; @@ -60,12 +60,12 @@ function indexed( x, value ) { } /** -* Counts the number of elements that are equal to a given value in an accessor array. +* Counts the number of elements in an accessor array that are equal to a specified value. * * @private * @param {Collection} x - input array -* @param {*} value - given value -* @returns {NonNegativeInteger} number of elements that are equal to the given value +* @param {*} value - search value +* @returns {NonNegativeInteger} number of elements that are equal to a specified value * * @example * var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); @@ -92,16 +92,16 @@ function accessors( x, value ) { } /** -* Counts the number of elements that are equal to a given value in a complex array. +* Counts the number of elements in a complex array that are equal to a specified value. * * @private * @param {Collection} x - input array -* @param {*} value - given value -* @returns {NonNegativeInteger} number of elements that are equal to the given value +* @param {*} value - search value +* @returns {NonNegativeInteger} number of elements that are equal to a specified value * * @example -* var Complex128 = require( '@stdlib/complex/float64' ); * var Complex128Array = require( '@stdlib/array/complex128' ); +* var Complex128 = require( '@stdlib/complex/float64' ); * * var x = new Complex128Array( [ 1.0, 2.0, 0.0, 0.0, 3.0, 4.0, 0.0, 0.0 ] ); * @@ -118,7 +118,6 @@ function complex( x, value ) { if ( !isComplexLike( value ) ) { return 0; } - re = real( value ); im = imag( value ); @@ -126,10 +125,7 @@ function complex( x, value ) { n = 0; for ( i = 0; i < view.length; i += 2 ) { - if ( - isSameValueZero( view[ i ], re ) && - isSameValueZero( view[ i + 1 ], im ) - ) { + if ( isSameValueZero( view[ i ], re ) && isSameValueZero( view[ i+1 ], im ) ) { // eslint-disable-line max-len n += 1; } } @@ -140,11 +136,16 @@ function complex( x, value ) { // MAIN // /** -* Counts the number of elements that are equal to a given value in an array. +* Counts the number of elements in an array that are equal to a specified value. +* +* ## Notes +* +* - The function uses the SameValueZero Algorithm used by `TypedArray` and `ArrayBuffer` constructors, `Map` and `Set` operations, `String.prototype.includes`, and `Array.prototype.includes` since ES2016. +* - In contrast to an implementation based on the strict equality operator `===`, the function treats `NaNs` as the same value. * * @param {Collection} x - input array -* @param {*} value - given value -* @returns {NonNegativeInteger} number of elements that are equal to the given value +* @param {*} value - search value +* @returns {NonNegativeInteger} number of elements that are equal to a specified value * * @example * var countSameValueZero = require( '@stdlib/array/base/count-same-value-zero' ); diff --git a/base/count-same-value-zero/package.json b/base/count-same-value-zero/package.json index 130c9f6c..7ac01393 100644 --- a/base/count-same-value-zero/package.json +++ b/base/count-same-value-zero/package.json @@ -1,7 +1,7 @@ { "name": "@stdlib/array/base/count-same-value-zero", "version": "0.0.0", - "description": "Count the number of elements that are equal to a given value in an array.", + "description": "Count the number of elements in an array that are equal to a specified value.", "license": "Apache-2.0", "author": { "name": "The Stdlib Authors", @@ -17,6 +17,7 @@ "directories": { "benchmark": "./benchmark", "doc": "./docs", + "example": "./examples", "lib": "./lib", "test": "./test" }, @@ -59,6 +60,7 @@ "summation", "countif", "total", - "same" + "same", + "equal" ] } diff --git a/base/count-same-value-zero/test/test.js b/base/count-same-value-zero/test/test.js index ef9ca8e8..edaa0ccd 100644 --- a/base/count-same-value-zero/test/test.js +++ b/base/count-same-value-zero/test/test.js @@ -45,11 +45,12 @@ tape( 'the function counts the number of same values (generic)', function test( x = [ 0, 1, 0, 1, 2 ]; expected = 2; actual = countSameValueZero( x, 1 ); + t.strictEqual( actual, expected, 'returns expected value' ); t.end(); }); -tape( 'the function consider positive and negative zeros to be indentical (generic)', function test( t ) { +tape( 'the function considers positive and negative zeros to be identical (generic)', function test( t ) { var expected; var actual; var x; @@ -67,7 +68,7 @@ tape( 'the function considers all NaN values to be identical (generic)', functio var actual; var x; - x = [ NaN, 0, NaN, 2, NaN, 9, NaN ]; + x = [ NaN, 0.0, NaN, 2.0, NaN, 9.0, NaN ]; expected = 4; actual = countSameValueZero( x, NaN ); @@ -87,7 +88,7 @@ tape( 'the function counts the number of same values (accessors)', function test t.end(); }); -tape( 'the function consider positive and negative zeros to be indentical (accessors)', function test( t ) { +tape( 'the function considers positive and negative zeros to be identical (accessors)', function test( t ) { var expected; var actual; var x; @@ -105,7 +106,7 @@ tape( 'the function considers all NaN values to be identical (accessors)', funct var actual; var x; - x = toAccessorArray( [ NaN, 0, NaN, 2, NaN, 9, NaN ] ); + x = toAccessorArray( [ NaN, 0.0, NaN, 2.0, NaN, 9.0, NaN ] ); expected = 4; actual = countSameValueZero( x, NaN ); @@ -126,7 +127,7 @@ tape( 'the function counts the number of same values (real typed array)', functi t.end(); }); -tape( 'the function consider positive and negative zeros to be indentical (real typed array)', function test( t ) { +tape( 'the function considers positive and negative zeros to be identical (real typed array)', function test( t ) { var expected; var actual; var x; @@ -144,7 +145,7 @@ tape( 'the function considers all NaN values to be identical (real typed array)' var actual; var x; - x = new Float32Array( [ NaN, 0, NaN, 2, NaN, 9, NaN ] ); + x = new Float32Array( [ NaN, 0.0, NaN, 2.0, NaN, 9.0, NaN ] ); expected = 4; actual = countSameValueZero( x, NaN ); @@ -160,16 +161,23 @@ tape( 'the function counts the number of same values (complex typed array)', fun x = new Complex128Array( [ 0.0, 0.0, 1.0, 0.0, 3.0, 4.0, 0.0, 5.0 ] ); expected = 1; actual = countSameValueZero( x, new Complex128( 3.0, 4.0 ) ); + + t.strictEqual( actual, expected, 'returns expected value' ); + + x = new Complex128Array( [ 0.0, 0.0, 1.0, 0.0, 3.0, 4.0, 0.0, 5.0 ] ); + expected = 0; + actual = countSameValueZero( x, 0.0 ); + t.strictEqual( actual, expected, 'returns expected value' ); t.end(); }); -tape( 'the function consider positive and negative zeros to be indentical (complex typed array)', function test( t ) { +tape( 'the function considers positive and negative zeros to be identical (complex typed array)', function test( t ) { var expected; var actual; var x; - x = new Complex128Array( [ 0.0, -0.0, 0.0, -0.0, 0.0, 0.0, -0.0, -0.0, -0.0, 0.0 ] ); + x = new Complex128Array( [ 0.0, -0.0, 0.0, -0.0, 0.0, 0.0, -0.0, -0.0, -0.0, 0.0 ] ); // eslint-disable-line max-len expected = 5; actual = countSameValueZero( x, new Complex128( 0.0, -0.0 ) ); diff --git a/base/lib/index.js b/base/lib/index.js index 5a8a4e79..7e0bd4e7 100644 --- a/base/lib/index.js +++ b/base/lib/index.js @@ -441,6 +441,15 @@ setReadOnly( ns, 'countFalsy', require( './../../base/count-falsy' ) ); */ setReadOnly( ns, 'countSameValue', require( './../../base/count-same-value' ) ); +/** +* @name countSameValueZero +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/array/base/count-same-value-zero} +*/ +setReadOnly( ns, 'countSameValueZero', require( './../../base/count-same-value-zero' ) ); + /** * @name countTruthy * @memberof ns