From 7e6adcd97e42f4dee23a29048177356cb2b7f6ed Mon Sep 17 00:00:00 2001 From: skoriop Date: Thu, 22 Feb 2024 01:26:25 +0530 Subject: [PATCH 01/13] feat: add `count-same-value` --- .../array/base/count-same-value/README.md | 112 +++++++++++++ .../benchmark/benchmark.length.js | 96 +++++++++++ .../array/base/count-same-value/docs/repl.txt | 24 +++ .../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 ++++ .../array/base/count-same-value/lib/index.js | 42 +++++ .../array/base/count-same-value/lib/main.js | 149 ++++++++++++++++++ .../array/base/count-same-value/package.json | 65 ++++++++ .../array/base/count-same-value/test/test.js | 89 +++++++++++ 10 files changed, 691 insertions(+) create mode 100644 lib/node_modules/@stdlib/array/base/count-same-value/README.md create mode 100644 lib/node_modules/@stdlib/array/base/count-same-value/benchmark/benchmark.length.js create mode 100644 lib/node_modules/@stdlib/array/base/count-same-value/docs/repl.txt create mode 100644 lib/node_modules/@stdlib/array/base/count-same-value/docs/types/index.d.ts create mode 100644 lib/node_modules/@stdlib/array/base/count-same-value/docs/types/test.ts create mode 100644 lib/node_modules/@stdlib/array/base/count-same-value/examples/index.js create mode 100644 lib/node_modules/@stdlib/array/base/count-same-value/lib/index.js create mode 100644 lib/node_modules/@stdlib/array/base/count-same-value/lib/main.js create mode 100644 lib/node_modules/@stdlib/array/base/count-same-value/package.json create mode 100644 lib/node_modules/@stdlib/array/base/count-same-value/test/test.js diff --git a/lib/node_modules/@stdlib/array/base/count-same-value/README.md b/lib/node_modules/@stdlib/array/base/count-same-value/README.md new file mode 100644 index 000000000000..8da0cf14d9b0 --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/count-same-value/README.md @@ -0,0 +1,112 @@ + + +# 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 value = 1; + +var out = countSameValue( x, value ); +// 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/lib/node_modules/@stdlib/array/base/count-same-value/benchmark/benchmark.length.js b/lib/node_modules/@stdlib/array/base/count-same-value/benchmark/benchmark.length.js new file mode 100644 index 000000000000..a86b593940ec --- /dev/null +++ b/lib/node_modules/@stdlib/array/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( '@stdlib/array/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/lib/node_modules/@stdlib/array/base/count-same-value/docs/repl.txt b/lib/node_modules/@stdlib/array/base/count-same-value/docs/repl.txt new file mode 100644 index 000000000000..40c8e4516ed1 --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/count-same-value/docs/repl.txt @@ -0,0 +1,24 @@ + +{{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 + given 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/lib/node_modules/@stdlib/array/base/count-same-value/docs/types/index.d.ts b/lib/node_modules/@stdlib/array/base/count-same-value/docs/types/index.d.ts new file mode 100644 index 000000000000..11dfb79c1402 --- /dev/null +++ b/lib/node_modules/@stdlib/array/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'; + +/** +* Count 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/lib/node_modules/@stdlib/array/base/count-same-value/docs/types/test.ts b/lib/node_modules/@stdlib/array/base/count-same-value/docs/types/test.ts new file mode 100644 index 000000000000..0e13294551f4 --- /dev/null +++ b/lib/node_modules/@stdlib/array/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/lib/node_modules/@stdlib/array/base/count-same-value/examples/index.js b/lib/node_modules/@stdlib/array/base/count-same-value/examples/index.js new file mode 100644 index 000000000000..62a33163d7fa --- /dev/null +++ b/lib/node_modules/@stdlib/array/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/lib/node_modules/@stdlib/array/base/count-same-value/lib/index.js b/lib/node_modules/@stdlib/array/base/count-same-value/lib/index.js new file mode 100644 index 000000000000..bccf8d32d85f --- /dev/null +++ b/lib/node_modules/@stdlib/array/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/lib/node_modules/@stdlib/array/base/count-same-value/lib/main.js b/lib/node_modules/@stdlib/array/base/count-same-value/lib/main.js new file mode 100644 index 000000000000..ec1aed313b3a --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/count-same-value/lib/main.js @@ -0,0 +1,149 @@ +/** +* @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 isComplexTypedArray = require( '@stdlib/array/base/assert/is-complex-typed-array' ); +var isAccessorArray = require( '@stdlib/array/base/assert/is-accessor-array' ); +var resolveGetter = require( '@stdlib/array/base/resolve-getter' ); +var isSameValue = require( '@stdlib/assert/is-same-value' ); + + +// FUNCTIONS // + +/** +* Count 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; +} + +/** +* Count 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; +} + +/** +* Count 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 n; + var i; + + n = 0; + for ( i = 0; i < x.length; i++ ) { + if ( isSameValue(x.get(i), value) ) { + n += 1; + } + } + return n; +} + + +// MAIN // + +/** +* Count 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/lib/node_modules/@stdlib/array/base/count-same-value/package.json b/lib/node_modules/@stdlib/array/base/count-same-value/package.json new file mode 100644 index 000000000000..17a9abe39c3e --- /dev/null +++ b/lib/node_modules/@stdlib/array/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/lib/node_modules/@stdlib/array/base/count-same-value/test/test.js b/lib/node_modules/@stdlib/array/base/count-same-value/test/test.js new file mode 100644 index 000000000000..7561c956a5a8 --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/count-same-value/test/test.js @@ -0,0 +1,89 @@ +/** +* @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( '@stdlib/array/complex128' ); +var Int32Array = require( '@stdlib/array/int32' ); +var toAccessorArray = require( '@stdlib/array/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 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 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 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(); +}); From 2fe96a5493fe6e98adf01407f425e993c1dd50e1 Mon Sep 17 00:00:00 2001 From: Athan Date: Wed, 21 Feb 2024 12:10:02 -0800 Subject: [PATCH 02/13] Apply suggestions from code review Signed-off-by: Athan --- lib/node_modules/@stdlib/array/base/count-same-value/README.md | 2 +- .../@stdlib/array/base/count-same-value/docs/repl.txt | 1 + .../@stdlib/array/base/count-same-value/docs/types/index.d.ts | 2 +- .../@stdlib/array/base/count-same-value/examples/index.js | 2 +- 4 files changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/node_modules/@stdlib/array/base/count-same-value/README.md b/lib/node_modules/@stdlib/array/base/count-same-value/README.md index 8da0cf14d9b0..6e051e25f903 100644 --- a/lib/node_modules/@stdlib/array/base/count-same-value/README.md +++ b/lib/node_modules/@stdlib/array/base/count-same-value/README.md @@ -76,7 +76,7 @@ var out = countSameValue( x, value ); var sample = require( '@stdlib/random/sample' ); var countSameValue = require( '@stdlib/array/base/count-same-value' ); -var x = sample( [1, 2, 3, 4, 5] ); +var x = sample( [ 1, 2, 3, 4, 5 ] ); console.log( x ); var n = countSameValue( x, 1 ); diff --git a/lib/node_modules/@stdlib/array/base/count-same-value/docs/repl.txt b/lib/node_modules/@stdlib/array/base/count-same-value/docs/repl.txt index 40c8e4516ed1..527d177bec83 100644 --- a/lib/node_modules/@stdlib/array/base/count-same-value/docs/repl.txt +++ b/lib/node_modules/@stdlib/array/base/count-same-value/docs/repl.txt @@ -6,6 +6,7 @@ ---------- x: ArrayLikeObject Input array. + value: any given value. diff --git a/lib/node_modules/@stdlib/array/base/count-same-value/docs/types/index.d.ts b/lib/node_modules/@stdlib/array/base/count-same-value/docs/types/index.d.ts index 11dfb79c1402..3a656bd87735 100644 --- a/lib/node_modules/@stdlib/array/base/count-same-value/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/array/base/count-same-value/docs/types/index.d.ts @@ -23,7 +23,7 @@ import { Collection } from '@stdlib/types/array'; /** -* Count the number of elements that are equal to a given value in an array. +* Counts the number of elements that are equal to a given value in an array. * * @param x - input array * @param value - given value diff --git a/lib/node_modules/@stdlib/array/base/count-same-value/examples/index.js b/lib/node_modules/@stdlib/array/base/count-same-value/examples/index.js index 62a33163d7fa..f76a9ff3642c 100644 --- a/lib/node_modules/@stdlib/array/base/count-same-value/examples/index.js +++ b/lib/node_modules/@stdlib/array/base/count-same-value/examples/index.js @@ -21,7 +21,7 @@ var sample = require( '@stdlib/random/sample' ); var countSameValue = require( './../lib' ); -var x = sample( [1, 2, 3, 4, 5] ); +var x = sample( [ 1, 2, 3, 4, 5 ] ); console.log( x ); var n = countSameValue( x, 1 ); From 9062cce1b19384c8f1109f0cd5dbc4b4c3c04563 Mon Sep 17 00:00:00 2001 From: Athan Date: Wed, 21 Feb 2024 12:10:35 -0800 Subject: [PATCH 03/13] Apply suggestions from code review Signed-off-by: Athan --- lib/node_modules/@stdlib/array/base/count-same-value/README.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/node_modules/@stdlib/array/base/count-same-value/README.md b/lib/node_modules/@stdlib/array/base/count-same-value/README.md index 6e051e25f903..92b701e2cae4 100644 --- a/lib/node_modules/@stdlib/array/base/count-same-value/README.md +++ b/lib/node_modules/@stdlib/array/base/count-same-value/README.md @@ -46,9 +46,8 @@ Counts the number of elements that are equal to a given value in an array. ```javascript var x = [ 0, 1, 0, 1, 2 ]; -var value = 1; -var out = countSameValue( x, value ); +var out = countSameValue( x, 1 ); // returns 2 ``` From 2570bd2906d7e3ad3973fe467184687578f019bf Mon Sep 17 00:00:00 2001 From: Athan Date: Wed, 21 Feb 2024 12:11:55 -0800 Subject: [PATCH 04/13] Apply suggestions from code review Signed-off-by: Athan --- .../@stdlib/array/base/count-same-value/docs/repl.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/array/base/count-same-value/docs/repl.txt b/lib/node_modules/@stdlib/array/base/count-same-value/docs/repl.txt index 527d177bec83..dd1d1fc13c78 100644 --- a/lib/node_modules/@stdlib/array/base/count-same-value/docs/repl.txt +++ b/lib/node_modules/@stdlib/array/base/count-same-value/docs/repl.txt @@ -8,7 +8,7 @@ Input array. value: any - given value. + Input value. Returns ------- From b93bb76ee6a5a1212851e02f5c72f78b3b8afd9a Mon Sep 17 00:00:00 2001 From: Athan Date: Wed, 21 Feb 2024 12:25:15 -0800 Subject: [PATCH 05/13] Apply suggestions from code review Signed-off-by: Athan --- .../@stdlib/array/base/count-same-value/lib/main.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/node_modules/@stdlib/array/base/count-same-value/lib/main.js b/lib/node_modules/@stdlib/array/base/count-same-value/lib/main.js index ec1aed313b3a..d2b986424c17 100644 --- a/lib/node_modules/@stdlib/array/base/count-same-value/lib/main.js +++ b/lib/node_modules/@stdlib/array/base/count-same-value/lib/main.js @@ -29,7 +29,7 @@ var isSameValue = require( '@stdlib/assert/is-same-value' ); // FUNCTIONS // /** -* Count the number of elements that are equal to a given value in an indexed array. +* Counts the number of elements that are equal to a given value in an indexed array. * * @private * @param {Collection} x - input array @@ -48,7 +48,7 @@ function indexed( x, value ) { n = 0; for ( i = 0; i < x.length; i++ ) { - if ( isSameValue(x[i], value) ) { + if ( isSameValue( x[ i ], value ) ) { n += 1; } } @@ -56,7 +56,7 @@ function indexed( x, value ) { } /** -* Count the number of elements that are equal to a given value in an accessor array. +* Counts the number of elements that are equal to a given value in an accessor array. * * @private * @param {Collection} x - input array @@ -80,7 +80,7 @@ function accessors( x, value ) { n = 0; for ( i = 0; i < x.length; i++ ) { - if ( isSameValue(get( x, i ), value) ) { + if ( isSameValue( get( x, i ), value ) ) { n += 1; } } @@ -88,7 +88,7 @@ function accessors( x, value ) { } /** -* Count the number of elements that are equal to a given value in a complex array. +* Counts the number of elements that are equal to a given value in a complex array. * * @private * @param {Collection} x - input array @@ -121,7 +121,7 @@ function complex( x, value ) { // MAIN // /** -* Count the number of elements that are equal to a given value in an array. +* Counts the number of elements that are equal to a given value in an array. * * @param {Collection} x - input array * @param {*} value - given value From 728628e10b67625bc1c409714bbe95b43d6d01de Mon Sep 17 00:00:00 2001 From: skoriop Date: Thu, 22 Feb 2024 12:26:17 +0530 Subject: [PATCH 06/13] refactor: avoid complex number instance creation --- .../array/base/count-same-value/lib/main.js | 20 +++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/lib/node_modules/@stdlib/array/base/count-same-value/lib/main.js b/lib/node_modules/@stdlib/array/base/count-same-value/lib/main.js index d2b986424c17..4ebef4d6b341 100644 --- a/lib/node_modules/@stdlib/array/base/count-same-value/lib/main.js +++ b/lib/node_modules/@stdlib/array/base/count-same-value/lib/main.js @@ -20,6 +20,10 @@ // 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( '@stdlib/array/base/assert/is-complex-typed-array' ); var isAccessorArray = require( '@stdlib/array/base/assert/is-accessor-array' ); var resolveGetter = require( '@stdlib/array/base/resolve-getter' ); @@ -105,12 +109,24 @@ function accessors( x, value ) { * // 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 < x.length; i++ ) { - if ( isSameValue(x.get(i), value) ) { + for ( i = 0; i < view.length; i += 2 ) { + if ( isSameValue( view[ i ], re ) && isSameValue( view[ i + 1 ], im ) ) { n += 1; } } From 8a17c658d701cfbc59bd0dc359c067db1a0fec4f Mon Sep 17 00:00:00 2001 From: skoriop Date: Thu, 22 Feb 2024 18:50:04 +0530 Subject: [PATCH 07/13] test: add tests involving `NaN` and signed zeroes --- .../array/base/count-same-value/test/test.js | 107 +++++++++++++++++- 1 file changed, 106 insertions(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/array/base/count-same-value/test/test.js b/lib/node_modules/@stdlib/array/base/count-same-value/test/test.js index 7561c956a5a8..d5abb1ba03aa 100644 --- a/lib/node_modules/@stdlib/array/base/count-same-value/test/test.js +++ b/lib/node_modules/@stdlib/array/base/count-same-value/test/test.js @@ -24,6 +24,7 @@ var tape = require( 'tape' ); var Complex128 = require( '@stdlib/complex/float64' ); var Complex128Array = require( '@stdlib/array/complex128' ); var Int32Array = require( '@stdlib/array/int32' ); +var Float32Array = require( '@stdlib/array/float32' ); var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); var countSameValue = require( './../lib' ); @@ -41,7 +42,7 @@ tape( 'the function counts the number of same values (generic)', function test( var actual; var x; - x = [ 0, 1, 0, 1, 2 ]; + x = [ 0, 1, -0, 1, 2 ]; expected = 2; actual = countSameValue( x, 1 ); @@ -49,6 +50,32 @@ tape( 'the function counts the number of same values (generic)', function test( 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, NaN, 1 ]; + expected = 3; + actual = countSameValue( x, 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; @@ -62,6 +89,32 @@ tape( 'the function counts the number of same values (accessors)', function test 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, NaN, 1 ] ); + expected = 3; + actual = countSameValue( x, 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; @@ -75,6 +128,32 @@ tape( 'the function counts the number of same values (real typed array)', functi 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, NaN, 1 ] ); + expected = 3; + actual = countSameValue( x, 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; @@ -87,3 +166,29 @@ tape( 'the function counts the number of same values (complex typed array)', fun 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 ] ); + 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, NaN, 9, NaN, NaN ] ); + expected = 2; + actual = countSameValue( x, new Complex128( NaN, NaN ) ); + + t.strictEqual( actual, expected, 'returns expected value' ); + t.end(); +}); From e99407e81ad31e65382d6817baa175b1cd67f07a Mon Sep 17 00:00:00 2001 From: Athan Date: Thu, 22 Feb 2024 13:28:03 -0800 Subject: [PATCH 08/13] Apply suggestions from code review Signed-off-by: Athan --- .../@stdlib/array/base/count-same-value/lib/main.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/array/base/count-same-value/lib/main.js b/lib/node_modules/@stdlib/array/base/count-same-value/lib/main.js index 4ebef4d6b341..72de05892f6a 100644 --- a/lib/node_modules/@stdlib/array/base/count-same-value/lib/main.js +++ b/lib/node_modules/@stdlib/array/base/count-same-value/lib/main.js @@ -115,7 +115,7 @@ function complex( x, value ) { var n; var i; - if (!isComplexLike(value)) { + if ( !isComplexLike( value ) ) { return 0; } From 72070f13e117d4a25626e9816e53024f51efd5b8 Mon Sep 17 00:00:00 2001 From: Athan Date: Thu, 22 Feb 2024 13:30:10 -0800 Subject: [PATCH 09/13] Apply suggestions from code review Signed-off-by: Athan --- .../@stdlib/array/base/count-same-value/test/test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/array/base/count-same-value/test/test.js b/lib/node_modules/@stdlib/array/base/count-same-value/test/test.js index d5abb1ba03aa..631f8b6d93b1 100644 --- a/lib/node_modules/@stdlib/array/base/count-same-value/test/test.js +++ b/lib/node_modules/@stdlib/array/base/count-same-value/test/test.js @@ -42,7 +42,7 @@ tape( 'the function counts the number of same values (generic)', function test( var actual; var x; - x = [ 0, 1, -0, 1, 2 ]; + x = [ 0, 1, 0, 1, 2 ]; expected = 2; actual = countSameValue( x, 1 ); From f49b7a73037ae3c2345cafce274fd389b60f927f Mon Sep 17 00:00:00 2001 From: Athan Date: Thu, 22 Feb 2024 13:30:50 -0800 Subject: [PATCH 10/13] Apply suggestions from code review Signed-off-by: Athan --- .../@stdlib/array/base/count-same-value/test/test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/array/base/count-same-value/test/test.js b/lib/node_modules/@stdlib/array/base/count-same-value/test/test.js index 631f8b6d93b1..fbde6abeff39 100644 --- a/lib/node_modules/@stdlib/array/base/count-same-value/test/test.js +++ b/lib/node_modules/@stdlib/array/base/count-same-value/test/test.js @@ -55,7 +55,7 @@ tape( 'the function distinguishes between positive and negative zeros (generic)' var actual; var x; - x = [ 0, -0, 0.0, -0.0, 0, NaN, 1 ]; + x = [ 0.0, -0.0, 0.0, -0.0, 0.0, NaN, 1.0 ]; expected = 3; actual = countSameValue( x, 0 ); From 19de0e56ec4646f401497e9dd4645443c5c112b1 Mon Sep 17 00:00:00 2001 From: Athan Date: Thu, 22 Feb 2024 13:31:18 -0800 Subject: [PATCH 11/13] Apply suggestions from code review Signed-off-by: Athan --- .../@stdlib/array/base/count-same-value/test/test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/array/base/count-same-value/test/test.js b/lib/node_modules/@stdlib/array/base/count-same-value/test/test.js index fbde6abeff39..ffb55ee2c3eb 100644 --- a/lib/node_modules/@stdlib/array/base/count-same-value/test/test.js +++ b/lib/node_modules/@stdlib/array/base/count-same-value/test/test.js @@ -57,7 +57,7 @@ tape( 'the function distinguishes between positive and negative zeros (generic)' x = [ 0.0, -0.0, 0.0, -0.0, 0.0, NaN, 1.0 ]; expected = 3; - actual = countSameValue( x, 0 ); + actual = countSameValue( x, 0.0 ); t.strictEqual( actual, expected, 'returns expected value' ); t.end(); From 0f3f5d0453f3fe5b2ec37a955eb4bd7b96bb5837 Mon Sep 17 00:00:00 2001 From: Athan Date: Thu, 22 Feb 2024 13:32:10 -0800 Subject: [PATCH 12/13] Apply suggestions from code review Signed-off-by: Athan --- .../@stdlib/array/base/count-same-value/test/test.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/node_modules/@stdlib/array/base/count-same-value/test/test.js b/lib/node_modules/@stdlib/array/base/count-same-value/test/test.js index ffb55ee2c3eb..afe0bdcf1d8d 100644 --- a/lib/node_modules/@stdlib/array/base/count-same-value/test/test.js +++ b/lib/node_modules/@stdlib/array/base/count-same-value/test/test.js @@ -94,9 +94,9 @@ tape( 'the function distinguishes between positive and negative zeros (accessors var actual; var x; - x = toAccessorArray( [ 0, -0, 0.0, -0.0, 0, NaN, 1 ] ); + x = toAccessorArray( [ 0.0, -0.0, 0.0, -0.0, 0.0, NaN, 1.0 ] ); expected = 3; - actual = countSameValue( x, 0 ); + actual = countSameValue( x, 0.0 ); t.strictEqual( actual, expected, 'returns expected value' ); t.end(); From 30b30218d85ece62ba50da636e0bc8cc45bb357b Mon Sep 17 00:00:00 2001 From: Athan Date: Thu, 22 Feb 2024 13:34:08 -0800 Subject: [PATCH 13/13] Apply suggestions from code review Signed-off-by: Athan --- .../@stdlib/array/base/count-same-value/test/test.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/node_modules/@stdlib/array/base/count-same-value/test/test.js b/lib/node_modules/@stdlib/array/base/count-same-value/test/test.js index afe0bdcf1d8d..0d56293b2337 100644 --- a/lib/node_modules/@stdlib/array/base/count-same-value/test/test.js +++ b/lib/node_modules/@stdlib/array/base/count-same-value/test/test.js @@ -133,9 +133,9 @@ tape( 'the function distinguishes between positive and negative zeros (real type var actual; var x; - x = new Float32Array( [ 0, -0, 0.0, -0.0, 0, NaN, 1 ] ); + x = new Float32Array( [ 0.0, -0.0, 0.0, -0.0, 0.0, NaN, 1.0 ] ); expected = 3; - actual = countSameValue( x, 0 ); + actual = countSameValue( x, 0.0 ); t.strictEqual( actual, expected, 'returns expected value' ); t.end(); @@ -172,7 +172,7 @@ tape( 'the function distinguishes between positive and negative zeros (complex t var actual; var x; - x = new Complex128Array( [ 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 ] ); expected = 2; actual = countSameValue( x, new Complex128( 0.0, -0.0 ) ); @@ -185,7 +185,7 @@ tape( 'the function considers all NaN values to be identical (complex typed arra var actual; var x; - x = new Complex128Array( [ NaN, NaN, NaN, 2, NaN, 9, NaN, NaN ] ); + x = new Complex128Array( [ NaN, NaN, NaN, 2.0, NaN, 9.0, NaN, NaN ] ); expected = 2; actual = countSameValue( x, new Complex128( NaN, NaN ) );