From 0723366eaf259e750351fe6e956f987b33cf35ba Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Thu, 22 Feb 2024 23:08:08 +0000 Subject: [PATCH] Auto-generated commit --- base/count-same-value/README.md | 111 ++++++++++ .../benchmark/benchmark.length.js | 96 +++++++++ base/count-same-value/docs/repl.txt | 25 +++ base/count-same-value/docs/types/index.d.ts | 43 ++++ base/count-same-value/docs/types/test.ts | 43 ++++ base/count-same-value/examples/index.js | 28 +++ base/count-same-value/lib/index.js | 42 ++++ base/count-same-value/lib/main.js | 165 +++++++++++++++ base/count-same-value/package.json | 65 ++++++ base/count-same-value/test/test.js | 194 ++++++++++++++++++ 10 files changed, 812 insertions(+) create mode 100644 base/count-same-value/README.md create mode 100644 base/count-same-value/benchmark/benchmark.length.js create mode 100644 base/count-same-value/docs/repl.txt create mode 100644 base/count-same-value/docs/types/index.d.ts create mode 100644 base/count-same-value/docs/types/test.ts create mode 100644 base/count-same-value/examples/index.js create mode 100644 base/count-same-value/lib/index.js create mode 100644 base/count-same-value/lib/main.js create mode 100644 base/count-same-value/package.json create mode 100644 base/count-same-value/test/test.js diff --git a/base/count-same-value/README.md b/base/count-same-value/README.md new file mode 100644 index 00000000..92b701e2 --- /dev/null +++ b/base/count-same-value/README.md @@ -0,0 +1,111 @@ + + +# countSameValue + +> Count the number of elements that are equal to a given value in an array. + + + +
+ +
+ + + + + +
+ +## Usage + +```javascript +var countSameValue = require( '@stdlib/array/base/count-same-value' ); +``` + +#### countSameValue( x, value ) + +Counts the number of elements that are equal to a given value in an array. + +```javascript +var x = [ 0, 1, 0, 1, 2 ]; + +var out = countSameValue( x, 1 ); +// returns 2 +``` + +
+ + + + + +
+ +
+ + + + + +
+ +## Examples + + + +```javascript +var sample = require( '@stdlib/random/sample' ); +var countSameValue = require( '@stdlib/array/base/count-same-value' ); + +var x = sample( [ 1, 2, 3, 4, 5 ] ); +console.log( x ); + +var n = countSameValue( x, 1 ); +console.log( n ); +``` + +
+ + + + + +
+ +
+ + + + + + + + + + + + + + diff --git a/base/count-same-value/benchmark/benchmark.length.js b/base/count-same-value/benchmark/benchmark.length.js new file mode 100644 index 00000000..8cc24d23 --- /dev/null +++ b/base/count-same-value/benchmark/benchmark.length.js @@ -0,0 +1,96 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var isNonNegativeInteger = require( '@stdlib/assert/is-nonnegative-integer' ).isPrimitive; +var ones = require( './../../../base/ones' ); +var pkg = require( './../package.json' ).name; +var countSameValue = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var x = ones( len ); + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var out; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + out = countSameValue( x, 1 ); + if ( typeof out !== 'number' ) { + b.fail( 'should return a number' ); + } + } + b.toc(); + if ( !isNonNegativeInteger( out ) ) { + b.fail( 'should return a nonnegative integer' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var len; + var min; + var max; + var f; + var i; + + min = 1; // 10^min + max = 6; // 10^max + + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + + f = createBenchmark( len ); + bench( pkg+':dtype=generic,len='+len, f ); + } +} + +main(); diff --git a/base/count-same-value/docs/repl.txt b/base/count-same-value/docs/repl.txt new file mode 100644 index 00000000..dd1d1fc1 --- /dev/null +++ b/base/count-same-value/docs/repl.txt @@ -0,0 +1,25 @@ + +{{alias}}( x, value ) + Counts the number of elements that are equal to a given value in an array. + + Parameters + ---------- + x: ArrayLikeObject + Input array. + + value: any + Input value. + + Returns + ------- + out: integer + Number of elements that are equal to the given value. + + Examples + -------- + > var out = {{alias}}( [ 0, 1, 1 ], 1 ) + 2 + + See Also + -------- + diff --git a/base/count-same-value/docs/types/index.d.ts b/base/count-same-value/docs/types/index.d.ts new file mode 100644 index 00000000..3a656bd8 --- /dev/null +++ b/base/count-same-value/docs/types/index.d.ts @@ -0,0 +1,43 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 4.1 + +/// + +import { Collection } from '@stdlib/types/array'; + +/** +* Counts the number of elements that are equal to a given value in an array. +* +* @param x - input array +* @param value - given value +* @returns number of elements that are equal to the given value +* +* @example +* var x = [ 0, 1, 0, 1, 1 ]; +* +* var out = countSameValue( x, 1 ); +* // returns 3 +*/ +declare function countSameValue( x: Collection, value: any ): number; + + +// EXPORTS // + +export = countSameValue; diff --git a/base/count-same-value/docs/types/test.ts b/base/count-same-value/docs/types/test.ts new file mode 100644 index 00000000..0e132945 --- /dev/null +++ b/base/count-same-value/docs/types/test.ts @@ -0,0 +1,43 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import countSameValue = require( './index' ); + + +// TESTS // + +// The function returns a number... +{ + countSameValue( [ 1, 2, 3 ], 1 ); // $ExpectType number +} + +// The compiler throws an error if the first argument is not a collection... +{ + countSameValue( 5, 1 ); // $ExpectError + countSameValue( true, 1 ); // $ExpectError + countSameValue( false, 1 ); // $ExpectError + countSameValue( null, 1 ); // $ExpectError + countSameValue( {}, 1 ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + countSameValue(); // $ExpectError + countSameValue( [ 1, 2, 3 ] ); // $ExpectError + countSameValue( [ 1, 2, 3 ], 2, 3 ); // $ExpectError +} diff --git a/base/count-same-value/examples/index.js b/base/count-same-value/examples/index.js new file mode 100644 index 00000000..f76a9ff3 --- /dev/null +++ b/base/count-same-value/examples/index.js @@ -0,0 +1,28 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var sample = require( '@stdlib/random/sample' ); +var countSameValue = require( './../lib' ); + +var x = sample( [ 1, 2, 3, 4, 5 ] ); +console.log( x ); + +var n = countSameValue( x, 1 ); +console.log( n ); diff --git a/base/count-same-value/lib/index.js b/base/count-same-value/lib/index.js new file mode 100644 index 00000000..bccf8d32 --- /dev/null +++ b/base/count-same-value/lib/index.js @@ -0,0 +1,42 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Count the number of elements that are equal to a given value in an array. +* +* @module @stdlib/array/base/count-same-value +* +* @example +* var countSameValue = require( '@stdlib/array/base/count-same-value' ); +* +* var x = [ 0, 1, 0, 1, 1 ]; +* +* var n = countSameValue( x, 1 ); +* // returns 3 +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/base/count-same-value/lib/main.js b/base/count-same-value/lib/main.js new file mode 100644 index 00000000..4fa14503 --- /dev/null +++ b/base/count-same-value/lib/main.js @@ -0,0 +1,165 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isComplexLike = require( '@stdlib/assert/is-complex-like' ); +var real = require( '@stdlib/complex/real' ); +var imag = require( '@stdlib/complex/imag' ); +var reinterpret = require( '@stdlib/strided/base/reinterpret-complex' ); +var isComplexTypedArray = require( './../../../base/assert/is-complex-typed-array' ); +var isAccessorArray = require( './../../../base/assert/is-accessor-array' ); +var resolveGetter = require( './../../../base/resolve-getter' ); +var isSameValue = require( '@stdlib/assert/is-same-value' ); + + +// FUNCTIONS // + +/** +* Counts the number of elements that are equal to a given value in an indexed array. +* +* @private +* @param {Collection} x - input array +* @param {*} value - given value +* @returns {NonNegativeInteger} number of elements that are equal to the given value +* +* @example +* var x = [ 0, 1, 0, 1 ]; +* +* var n = indexed( x, 1 ); +* // returns 2 +*/ +function indexed( x, value ) { + var n; + var i; + + n = 0; + for ( i = 0; i < x.length; i++ ) { + if ( isSameValue( x[ i ], value ) ) { + n += 1; + } + } + return n; +} + +/** +* Counts the number of elements that are equal to a given value in an accessor array. +* +* @private +* @param {Collection} x - input array +* @param {*} value - given value +* @returns {NonNegativeInteger} number of elements that are equal to the given value +* +* @example +* var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); +* +* var x = toAccessorArray( [ 0, 1, 0, 1 ] ); +* +* var n = accessors( x, 1 ); +* // returns 2 +*/ +function accessors( x, value ) { + var get; + var n; + var i; + + get = resolveGetter( x ); + + n = 0; + for ( i = 0; i < x.length; i++ ) { + if ( isSameValue( get( x, i ), value ) ) { + n += 1; + } + } + return n; +} + +/** +* Counts the number of elements that are equal to a given value in a complex array. +* +* @private +* @param {Collection} x - input array +* @param {*} value - given value +* @returns {NonNegativeInteger} number of elements that are equal to the given value +* +* @example +* var Complex128 = require( '@stdlib/complex/float64' ); +* var Complex128Array = require( '@stdlib/array/complex128' ); +* +* var x = new Complex128Array( [ 1.0, 2.0, 0.0, 0.0, 3.0, 4.0, 0.0, 0.0 ] ); +* +* var n = complex( x, new Complex128( 1.0, 2.0 ) ); +* // returns 1 +*/ +function complex( x, value ) { + var view; + var re; + var im; + var n; + var i; + + 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 ( isSameValue( view[ i ], re ) && isSameValue( view[ i + 1 ], im ) ) { + n += 1; + } + } + return n; +} + + +// MAIN // + +/** +* Counts the number of elements that are equal to a given value in an array. +* +* @param {Collection} x - input array +* @param {*} value - given value +* @returns {NonNegativeInteger} number of elements that are equal to the given value +* +* @example +* var x = [ 0, 1, 0, 1, 1 ]; +* +* var n = countSameValue( x, 1 ); +* // returns 3 +*/ +function countSameValue( x, value ) { + if ( isAccessorArray( x, value ) ) { + if ( isComplexTypedArray( x, value ) ) { + return complex( x, value ); + } + return accessors( x, value ); + } + return indexed( x, value ); +} + + +// EXPORTS // + +module.exports = countSameValue; diff --git a/base/count-same-value/package.json b/base/count-same-value/package.json new file mode 100644 index 00000000..17a9abe3 --- /dev/null +++ b/base/count-same-value/package.json @@ -0,0 +1,65 @@ +{ + "name": "@stdlib/array/base/count-same-value", + "version": "0.0.0", + "description": "Count the number of elements that are equal to a given value in an array.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdtypes", + "types", + "data", + "structure", + "array", + "count", + "sum", + "summation", + "countif", + "total", + "same" + ] +} diff --git a/base/count-same-value/test/test.js b/base/count-same-value/test/test.js new file mode 100644 index 00000000..a5e43557 --- /dev/null +++ b/base/count-same-value/test/test.js @@ -0,0 +1,194 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Complex128 = require( '@stdlib/complex/float64' ); +var Complex128Array = require( './../../../complex128' ); +var Int32Array = require( './../../../int32' ); +var Float32Array = require( './../../../float32' ); +var toAccessorArray = require( './../../../base/to-accessor-array' ); +var countSameValue = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof countSameValue, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function counts the number of same values (generic)', function test( t ) { + var expected; + var actual; + var x; + + x = [ 0, 1, 0, 1, 2 ]; + expected = 2; + actual = countSameValue( x, 1 ); + + t.strictEqual( actual, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function distinguishes between positive and negative zeros (generic)', function test( t ) { + var expected; + var actual; + var x; + + x = [ 0.0, -0.0, 0.0, -0.0, 0.0, NaN, 1.0 ]; + expected = 3; + actual = countSameValue( x, 0.0 ); + + t.strictEqual( actual, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function considers all NaN values to be identical (generic)', function test( t ) { + var expected; + var actual; + var x; + + x = [ NaN, 0, NaN, 2, NaN, 9, NaN ]; + expected = 4; + actual = countSameValue( x, NaN ); + + t.strictEqual( actual, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function counts the number of same values (accessors)', function test( t ) { + var expected; + var actual; + var x; + + x = toAccessorArray( [ 0, 1, 0, 1, 2 ] ); + expected = 2; + actual = countSameValue( x, 1 ); + + t.strictEqual( actual, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function distinguishes between positive and negative zeros (accessors)', function test( t ) { + var expected; + var actual; + var x; + + x = toAccessorArray( [ 0.0, -0.0, 0.0, -0.0, 0.0, NaN, 1.0 ] ); + expected = 3; + actual = countSameValue( x, 0.0 ); + + t.strictEqual( actual, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function considers all NaN values to be identical (accessors)', function test( t ) { + var expected; + var actual; + var x; + + x = toAccessorArray( [ NaN, 0, NaN, 2, NaN, 9, NaN ] ); + expected = 4; + actual = countSameValue( x, NaN ); + + t.strictEqual( actual, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function counts the number of same values (real typed array)', function test( t ) { + var expected; + var actual; + var x; + + x = new Int32Array( [ 0, 1, 0, 1, 2 ] ); + expected = 2; + actual = countSameValue( x, 1 ); + + t.strictEqual( actual, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function distinguishes between positive and negative zeros (real typed array)', function test( t ) { + var expected; + var actual; + var x; + + x = new Float32Array( [ 0.0, -0.0, 0.0, -0.0, 0.0, NaN, 1.0 ] ); + expected = 3; + actual = countSameValue( x, 0.0 ); + + t.strictEqual( actual, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function considers all NaN values to be identical (real typed array)', function test( t ) { + var expected; + var actual; + var x; + + x = new Float32Array( [ NaN, 0, NaN, 2, NaN, 9, NaN ] ); + expected = 4; + actual = countSameValue( x, NaN ); + + 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; + var x; + + x = new Complex128Array( [ 0.0, 0.0, 1.0, 0.0, 3.0, 4.0, 0.0, 5.0 ] ); + expected = 1; + actual = countSameValue( x, new Complex128( 3.0, 4.0 ) ); + + t.strictEqual( actual, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function distinguishes between positive and negative zeros (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 ] ); + expected = 2; + actual = countSameValue( x, new Complex128( 0.0, -0.0 ) ); + + t.strictEqual( actual, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function considers all NaN values to be identical (complex typed array)', function test( t ) { + var expected; + var actual; + var x; + + x = new Complex128Array( [ NaN, NaN, NaN, 2.0, NaN, 9.0, NaN, NaN ] ); + expected = 2; + actual = countSameValue( x, new Complex128( NaN, NaN ) ); + + t.strictEqual( actual, expected, 'returns expected value' ); + t.end(); +});