Skip to content

Commit

Permalink
Auto-generated commit
Browse files Browse the repository at this point in the history
  • Loading branch information
stdlib-bot committed Mar 16, 2024
1 parent 45326af commit 98df9fa
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 43 deletions.
30 changes: 26 additions & 4 deletions base/count-same-value-zero/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->

Expand All @@ -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 ];
Expand All @@ -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
```

</section>

<!-- /.usage -->
Expand All @@ -72,10 +90,12 @@ var out = countSameValueZero( x, 1 );
<!-- eslint no-undef: "error" -->

```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 );
Expand Down Expand Up @@ -106,6 +126,8 @@ console.log( n );

<section class="links">

[@stdlib/array/base/count-same-value]: https://github.com/stdlib-js/array/tree/main/base/count-same-value

</section>

<!-- /.links -->
7 changes: 5 additions & 2 deletions base/count-same-value-zero/docs/repl.txt
Original file line number Diff line number Diff line change
@@ -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
----------
Expand All @@ -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
--------
Expand Down
10 changes: 7 additions & 3 deletions base/count-same-value-zero/docs/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 ];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
2 changes: 1 addition & 1 deletion base/count-same-value-zero/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
37 changes: 19 additions & 18 deletions base/count-same-value-zero/lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ];
Expand All @@ -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' );
Expand All @@ -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 ] );
*
Expand All @@ -118,18 +118,14 @@ function complex( x, value ) {
if ( !isComplexLike( value ) ) {
return 0;
}

re = real( value );
im = imag( value );

view = reinterpret( x, 0 );

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;
}
}
Expand All @@ -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' );
Expand Down
6 changes: 4 additions & 2 deletions base/count-same-value-zero/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -17,6 +17,7 @@
"directories": {
"benchmark": "./benchmark",
"doc": "./docs",
"example": "./examples",
"lib": "./lib",
"test": "./test"
},
Expand Down Expand Up @@ -59,6 +60,7 @@
"summation",
"countif",
"total",
"same"
"same",
"equal"
]
}
24 changes: 16 additions & 8 deletions base/count-same-value-zero/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 );

Expand All @@ -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;
Expand All @@ -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 );

Expand All @@ -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;
Expand All @@ -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 );

Expand All @@ -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 ) );

Expand Down
9 changes: 9 additions & 0 deletions base/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 98df9fa

Please sign in to comment.