From ebf0d8d88d9501416a129c5bd6503893109612cc Mon Sep 17 00:00:00 2001 From: AuenKr Date: Tue, 5 Mar 2024 14:43:56 +0530 Subject: [PATCH 01/18] feat: add array/base/mskfilter-map --- .../array/base/mskfilter-map/README.md | 153 +++++++++ .../benchmark/benchmark.assign.length.js | 109 ++++++ .../base/mskfilter-map/benchmark/benchmark.js | 59 ++++ .../benchmark/benchmark.length.js | 101 ++++++ .../array/base/mskfilter-map/docs/repl.txt | 84 +++++ .../base/mskfilter-map/docs/types/index.d.ts | 50 +++ .../base/mskfilter-map/docs/types/test.ts | 102 ++++++ .../base/mskfilter-map/examples/index.js | 42 +++ .../array/base/mskfilter-map/lib/assign.js | 245 ++++++++++++++ .../array/base/mskfilter-map/lib/index.js | 71 ++++ .../array/base/mskfilter-map/lib/main.js | 71 ++++ .../array/base/mskfilter-map/package.json | 66 ++++ .../base/mskfilter-map/test/test.assign.js | 309 ++++++++++++++++++ .../array/base/mskfilter-map/test/test.js | 41 +++ .../base/mskfilter-map/test/test.main.js | 112 +++++++ 15 files changed, 1615 insertions(+) create mode 100644 lib/node_modules/@stdlib/array/base/mskfilter-map/README.md create mode 100644 lib/node_modules/@stdlib/array/base/mskfilter-map/benchmark/benchmark.assign.length.js create mode 100644 lib/node_modules/@stdlib/array/base/mskfilter-map/benchmark/benchmark.js create mode 100644 lib/node_modules/@stdlib/array/base/mskfilter-map/benchmark/benchmark.length.js create mode 100644 lib/node_modules/@stdlib/array/base/mskfilter-map/docs/repl.txt create mode 100644 lib/node_modules/@stdlib/array/base/mskfilter-map/docs/types/index.d.ts create mode 100644 lib/node_modules/@stdlib/array/base/mskfilter-map/docs/types/test.ts create mode 100644 lib/node_modules/@stdlib/array/base/mskfilter-map/examples/index.js create mode 100644 lib/node_modules/@stdlib/array/base/mskfilter-map/lib/assign.js create mode 100644 lib/node_modules/@stdlib/array/base/mskfilter-map/lib/index.js create mode 100644 lib/node_modules/@stdlib/array/base/mskfilter-map/lib/main.js create mode 100644 lib/node_modules/@stdlib/array/base/mskfilter-map/package.json create mode 100644 lib/node_modules/@stdlib/array/base/mskfilter-map/test/test.assign.js create mode 100644 lib/node_modules/@stdlib/array/base/mskfilter-map/test/test.js create mode 100644 lib/node_modules/@stdlib/array/base/mskfilter-map/test/test.main.js diff --git a/lib/node_modules/@stdlib/array/base/mskfilter-map/README.md b/lib/node_modules/@stdlib/array/base/mskfilter-map/README.md new file mode 100644 index 000000000000..8ca5d9bbbf4b --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/mskfilter-map/README.md @@ -0,0 +1,153 @@ + + +# mskfilter-map + +> Apply a mask to a provided input array and return a new array after applying a mapping function. + +
+ +## Usage + +```javascript +var mskfilterMap = require( '@stdlib/array/base/mskfilter-map' ); +``` + +#### mskfilterMap( x, mask, clbk\[, thisArg] ) + +Returns a new array by applying a mask to a provided input array after applying mapping function. + +```javascript +var x = [ 1, 2, 3, 4 ]; + +function customMapping( value ) { + return value * 2; +} + +var y = mskfilterMap( x, [ 0, 1, 0, 1 ], customMapping ); +// returns [ 4, 8 ] +``` + +The function supports the following parameters: + +- **x**: input array. +- **mask**: mask array. +- **clbk**: function to apply. +- **thisArg**: applied function execution context (_optional_). + +To set the applied function's execution context, provide a `thisArg`. + +The function **always** returns a new "generic" array. + +#### mskfilterMap.assign( x, mask, out, stride, offset ) + +Applies a mask & mapping function to a provided input array and assigns unmasked values to elements in a provided output array. + +```javascript +var x = [ 1, 2, 3, 4 ]; +var mask = [ 0, 1, 0, 1 ]; + +var out = [ 0, 0, 0, 0 ]; + +function clbk( val ) { + return val * 2; +} + +var arr = mskfilterMap.assign( x, mask, out, -2, out.length-1, clbk ); +// returns [ 0, 8, 0, 4 ] + +var bool = ( arr === out ); +// returns true +``` + +The function supports the following parameters: + +- **x**: input array. +- **mask**: mask array. +- **out**: output array. +- **stride**: output array stride. +- **offset**: output array offset. +- **clbk**: function to apply. +- **thisArg**: applied function execution context (_optional_). + +
+ + + +
+ +## Notes + +- If a `mask` array element is truthy, the corresponding element in `x` is **included** in the output array; otherwise, the corresponding element in `x` is "masked" and thus **excluded** from the output array. + +
+ + + +
+ +## Examples + + + +```javascript +var zeroTo = require( '@stdlib/array/base/zero-to' ); +var bernoulli = require( '@stdlib/random/array/bernoulli' ); +var mskfilterMap = require( '@stdlib/array/base/mskfilter-map' ); + +// Generate a linearly spaced array: +var x = zeroTo( 20 ); +console.log( x ); + +// Generate a random mask: +var mask = bernoulli( x.length, 0.5, { + 'dtype': 'generic' +}); +console.log( mask ); + +// Mapping functions +function square( val ) { + return val * val; +} + +// Filter an array using the mask: +var y = mskfilterMap( x, mask, square ); +console.log( y ); +``` + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/array/base/mskfilter-map/benchmark/benchmark.assign.length.js b/lib/node_modules/@stdlib/array/base/mskfilter-map/benchmark/benchmark.assign.length.js new file mode 100644 index 000000000000..aacdae56bf81 --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/mskfilter-map/benchmark/benchmark.assign.length.js @@ -0,0 +1,109 @@ +/** +* @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 zeroTo = require( '@stdlib/array/base/zero-to' ); +var zeros = require( '@stdlib/array/zeros' ); +var ones = require( '@stdlib/array/ones' ); +var isArray = require( '@stdlib/assert/is-array' ); +var isnan = require( '@stdlib/assert/is-nan' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var mskfilterMap = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var mask; + var out; + var x; + + x = zeroTo( len, 'generic' ); + mask = ones( len, 'generic' ); + out = zeros( len, 'generic' ); + + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var v; + var i; + + function clbk( val ) { + return val; + } + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = mskfilterMap.assign( x, mask, out, 1, 0, clbk ); + if ( typeof v !== 'object' ) { + b.fail( 'should return an array' ); + } + } + b.toc(); + if ( !isArray( v ) || isnan( v[ i%len ] ) ) { + b.fail( 'should return an array' ); + } + 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+':assign:len='+len, f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/array/base/mskfilter-map/benchmark/benchmark.js b/lib/node_modules/@stdlib/array/base/mskfilter-map/benchmark/benchmark.js new file mode 100644 index 000000000000..1acc3e96cdba --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/mskfilter-map/benchmark/benchmark.js @@ -0,0 +1,59 @@ +/** +* @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 isArray = require( '@stdlib/assert/is-array' ); +var zeroTo = require( '@stdlib/array/base/zero-to' ); +var ones = require( '@stdlib/array/base/ones' ); +var pkg = require( './../package.json' ).name; +var mskfilterMap = require( './../lib' ); + + +// MAIN // + +bench( pkg+'::copy:len=100', function benchmark( b ) { + var x; + var y; + var i; + var v; + + x = zeroTo( 100 ); + y = ones( x.length ); + + function clbk( val ) { + return val; + } + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = mskfilterMap( x, y, clbk ); + if ( typeof v !== 'object' ) { + b.fail( 'should return an array' ); + } + } + b.toc(); + if ( !isArray( v ) ) { + b.fail( 'should return an array' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/lib/node_modules/@stdlib/array/base/mskfilter-map/benchmark/benchmark.length.js b/lib/node_modules/@stdlib/array/base/mskfilter-map/benchmark/benchmark.length.js new file mode 100644 index 000000000000..fe9ec108182b --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/mskfilter-map/benchmark/benchmark.length.js @@ -0,0 +1,101 @@ +/** +* @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 zeroTo = require( '@stdlib/array/base/zero-to' ); +var ones = require( '@stdlib/array/base/ones' ); +var isArray = require( '@stdlib/assert/is-array' ); +var pkg = require( './../package.json' ).name; +var mskfiltermap = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var x = zeroTo( len ); + var y = ones( len ); + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var v; + var i; + + function clbk( val ) { + return val; + } + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = mskfiltermap( x, y, clbk ); + if ( typeof v !== 'object' ) { + b.fail( 'should return an array' ); + } + } + b.toc(); + if ( !isArray( v ) ) { + b.fail( 'should return an array' ); + } + 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+':len='+len, f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/array/base/mskfilter-map/docs/repl.txt b/lib/node_modules/@stdlib/array/base/mskfilter-map/docs/repl.txt new file mode 100644 index 000000000000..e2ad49f66cd2 --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/mskfilter-map/docs/repl.txt @@ -0,0 +1,84 @@ + +{{alias}}( x, mask, fcn[, thisArg] ) + Returns a new array by applying a mask & mapping function to a provided + input array. + + If a mask array element is truthy, the corresponding element in `x` is + included in the output array; otherwise, the corresponding element in `x` + is "masked" and thus excluded from the output array. + + Parameters + ---------- + x: ArrayLikeObject + Input array. + + mask: ArrayLikeObject + Mask array. + + fcn: Function + Function to apply. + + thisArg: any (optional) + Function execution context. + + Returns + ------- + out: Array + Output array. + + Examples + -------- + > var x = [ 1, -2, -3, 4 ]; + > var y = {{alias}}( x, [ 0, 1, 0, 1 ], {{alias:@stdlib/math/base/special/abs}} ) + [ 2, 4 ] + + +{{alias}}.assign( x, mask, out, stride, offset, fcn[, thisArg] ) + Applies mask to a provided input array & mapping function and assigns + unmasked values to elements in a provided output array. + + If a mask array element is truthy, the corresponding element in `x` is + included in the output array; otherwise, the corresponding element in `x` + is "masked" and thus excluded from the output array. + + Parameters + ---------- + x: ArrayLikeObject + Input array. + + mask: ArrayLikeObject + Mask array. + + out: ArrayLikeObject + Output array. + + stride: integer + Output array stride. + + offset: integer + Output array offset. + + fcn: Function + Function to apply. + + thisArg: any (optional) + Function execution context. + + Returns + ------- + out: ArrayLikeObject + Output array. + + Examples + -------- + > var x = [ -1, -2, -3, -4 ]; + > var m = [ 0, 1, 0, 1 ]; + > var out = [ 0, 0, 0, 0 ]; + > var arr = {{alias}}.assign( x, m, out, 2, 0, {{alias:@stdlib/math/base/special/abs}} ) + [ 2, 0, 4, 0 ] + > var bool = ( arr === out ) + true + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/array/base/mskfilter-map/docs/types/index.d.ts b/lib/node_modules/@stdlib/array/base/mskfilter-map/docs/types/index.d.ts new file mode 100644 index 000000000000..66786b2746a6 --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/mskfilter-map/docs/types/index.d.ts @@ -0,0 +1,50 @@ +/* +* @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'; + +/** +* Returns a new array by applying a mask to a provided input array. +* +* @param x - input array +* @param mask - mask array +* @param clbk - callback to invoke +* @param thisArg - execution context +* @returns output array +* +* @example +* var x = [ 1, 2, 3, 4 ]; +* +* function clbk( val ) { +* return 2 * val; +* } +* +* var y = mskfilterMap( x, [ 0, 1, 0, 1 ], clbk ); +* // returns [ 4, 8 ] +*/ +declare function mskfilterMap( x: Collection, mask: Collection, clbk: Function, thisArg?: ThisParameterType ): Array; + + +// EXPORTS // + +export = mskfilterMap; + diff --git a/lib/node_modules/@stdlib/array/base/mskfilter-map/docs/types/test.ts b/lib/node_modules/@stdlib/array/base/mskfilter-map/docs/types/test.ts new file mode 100644 index 000000000000..09009400ef0c --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/mskfilter-map/docs/types/test.ts @@ -0,0 +1,102 @@ +/* +* @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 mskfilterMap = require( './index' ); + +function clbk( value: number ): number { + return value; +} + +// TESTS // + +// The function returns an array... +{ + mskfilterMap( [ 1, 2, 3, 4 ], [ 0, 0 ], clbk ); // $ExpectType number[] + mskfilterMap( [ 1, 2, 3, 4 ], [ 0, 0 ], clbk, {} ); // $ExpectType number[] + mskfilterMap( [ 1, 2, 3, 4 ], [ 0, 0 ], clbk ); // $ExpectType any[] + mskfilterMap( [ 1, 2, 3, 4 ], [ 0, 0 ], clbk, {} ); // $ExpectType any[] + mskfilterMap( [ 1, 2, 3, 4 ], [ 0, 0 ], clbk ); // $ExpectType number[] + mskfilterMap( [ 1, 2, 3, 4 ], [ 0, 0 ], clbk, {} ); // $ExpectType number[] + mskfilterMap( [ '1', '2', '3', '4' ], [ 0, 0 ], clbk ); // $ExpectType string[] + mskfilterMap( [ '1', '2', '3', '4' ], [ 0, 0 ], clbk, {} ); // $ExpectType string[] +} + +// The compiler throws an error if the function is provided a first argument which is not an array-like object... +{ + mskfilterMap( 1, [ 0, 0 ], clbk ); // $ExpectError + mskfilterMap( true, [ 0, 0 ], clbk ); // $ExpectError + mskfilterMap( false, [ 0, 0 ], clbk ); // $ExpectError + mskfilterMap( null, [ 0, 0 ], clbk ); // $ExpectError + mskfilterMap( void 0, [ 0, 0 ], clbk ); // $ExpectError + mskfilterMap( {}, [ 0, 0 ], clbk ); // $ExpectError + + mskfilterMap( 1, [ 0, 0 ], clbk, {} ); // $ExpectError + mskfilterMap( true, [ 0, 0 ], clbk, {} ); // $ExpectError + mskfilterMap( false, [ 0, 0 ], clbk, {} ); // $ExpectError + mskfilterMap( null, [ 0, 0 ], clbk, {} ); // $ExpectError + mskfilterMap( void 0, [ 0, 0 ], clbk, {} ); // $ExpectError + mskfilterMap( {}, [ 0, 0 ], clbk, {} ); // $ExpectError +} + +// The compiler throws an error if the function is provided a second argument which is not an array-like object containing numbers... +{ + mskfilterMap( [], 1, clbk ); // $ExpectError + mskfilterMap( [], true, clbk ); // $ExpectError + mskfilterMap( [], false, clbk ); // $ExpectError + mskfilterMap( [], null, clbk ); // $ExpectError + mskfilterMap( [], void 0, clbk ); // $ExpectError + mskfilterMap( [], {}, clbk ); // $ExpectError + + mskfilterMap( [], 1, clbk, {} ); // $ExpectError + mskfilterMap( [], true, clbk, {} ); // $ExpectError + mskfilterMap( [], false, clbk, {} ); // $ExpectError + mskfilterMap( [], null, clbk, {} ); // $ExpectError + mskfilterMap( [], void 0, clbk, {} ); // $ExpectError + mskfilterMap( [], {}, clbk, {} ); // $ExpectError +} + +// The compiler throws an error if the function is provided a third argument which is not a valid callback... +{ + const x = [1, 2]; + + mskfilterMap( x, [ 2, 2 ], 'abc' ); // $ExpectError + mskfilterMap( x, [ 2, 2 ], 3.14 ); // $ExpectError + mskfilterMap( x, [ 2, 2 ], true ); // $ExpectError + mskfilterMap( x, [ 2, 2 ], false ); // $ExpectError + mskfilterMap( x, [ 2, 2 ], null ); // $ExpectError + mskfilterMap( x, [ 2, 2 ], [ '1' ] ); // $ExpectError + mskfilterMap( x, [ 2, 2 ], {} ); // $ExpectError + + mskfilterMap( x, [ 2, 2 ], 'abc', {} ); // $ExpectError + mskfilterMap( x, [ 2, 2 ], 3.14, {} ); // $ExpectError + mskfilterMap( x, [ 2, 2 ], true, {} ); // $ExpectError + mskfilterMap( x, [ 2, 2 ], false, {} ); // $ExpectError + mskfilterMap( x, [ 2, 2 ], null, {} ); // $ExpectError + mskfilterMap( x, [ 2, 2 ], [ '1' ], {} ); // $ExpectError + mskfilterMap( x, [ 2, 2 ], {}, {} ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + const x = [ 1.0, 2.0 ]; + + mskfilterMap(); // $ExpectError + mskfilterMap( x ); // $ExpectError + mskfilterMap( x, [ 2, 2 ] ); // $ExpectError + mskfilterMap( x, [ 2, 2 ], clbk, {}, {} ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/array/base/mskfilter-map/examples/index.js b/lib/node_modules/@stdlib/array/base/mskfilter-map/examples/index.js new file mode 100644 index 000000000000..e16390753a24 --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/mskfilter-map/examples/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'; + +var zeroTo = require( '@stdlib/array/base/zero-to' ); +var bernoulli = require( '@stdlib/random/array/bernoulli' ); +var mskfilterMap = require( './../lib' ); + +// Generate a linearly spaced array: +var x = zeroTo( 20 ); +console.log( x ); + +// Generate a random mask: +var mask = bernoulli( x.length, 0.5, { + 'dtype': 'generic' +}); +console.log( mask ); + +// Mapping callback function: +function clbk( x ) { + return 2 * x; +} + +// Filter an array using the mask & apply mapping function: +var y = mskfilterMap( x, mask, clbk ); +console.log( y ); diff --git a/lib/node_modules/@stdlib/array/base/mskfilter-map/lib/assign.js b/lib/node_modules/@stdlib/array/base/mskfilter-map/lib/assign.js new file mode 100644 index 000000000000..b89c019b8bf2 --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/mskfilter-map/lib/assign.js @@ -0,0 +1,245 @@ +/** +* @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 isComplexDataType = require( '@stdlib/array/base/assert/is-complex-floating-point-data-type' ); +var arraylike2object = require( '@stdlib/array/base/arraylike2object' ); +var reinterpret = require( '@stdlib/strided/base/reinterpret-complex' ); + + +// FUNCTIONS // + +/** +* Applies a mask to an indexed array & apply mapping function and assigns unmasked values to elements in an indexed output array. +* +* @private +* @param {Collection} x - input array +* @param {IntegerArray} mask - mask array +* @param {Collection} out - output array +* @param {integer} stride - output array stride +* @param {NonNegativeInteger} offset - output array offset +* @param {Function} clbk - function to apply +* @param {*} [thisArg] - function execution context +* @returns {Collection} output array +* +* @example +* var x = [ 1, 2, 3, 4 ]; +* var mask = [ 0, 1, 0, 1 ]; +* +* var out = [ 0, 0, 0, 0 ]; +* +* function mappingFunction( x ) { +* return x * 10; +* } +* +* var arr = indexed( x, mask, out, 1, 0, mappingFunction ); +* // returns [ 20, 40, 0, 0 ] +*/ +function indexed( x, mask, out, stride, offset, clbk, thisArg ) { + var io; + var i; + + io = offset; + for ( i = 0; i < x.length; i++ ) { + if ( mask[ i ] ) { + out[ io ] = clbk.call( thisArg, x[ i ] ); + io += stride; + } + } + return out; +} + +/** +* Applies a mask to an accessor array & apply mapping function and assigns unmasked values to elements in an accessor output array. +* +* @private +* @param {Object} x - input array object +* @param {Object} mask - mask array object +* @param {Object} out - output array object +* @param {integer} stride - output array stride +* @param {NonNegativeInteger} offset - output array offset +* @param {Function} clbk - function to apply +* @param {*} [thisArg] - function execution context +* @returns {Collection} output array +* +* @example +* var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); +* var arraylike2object = require( '@stdlib/array/base/arraylike2object' ); +* +* var x = toAccessorArray( [ 1, 2, 3, 4 ] ); +* var mask = toAccessorArray( [ 0, 1, 0, 1 ] ); +* +* function mappingFunction( x ) { +* return x * 10; +* } +* +* var out = toAccessorArray( [ 0, 0, 0, 0 ] ); +* var arr = accessors( arraylike2object( x ), arraylike2object( mask ), arraylike2object( out ), 1, 0, mappingFunction ); +* +* var v = arr.get( 0 ); +* // returns 20 +* +* v = arr.get( 1 ); +* // returns 40 +*/ +function accessors( x, mask, out, stride, offset, clbk, thisArg ) { + var xdata; + var mdata; + var odata; + var xget; + var mget; + var oset; + var io; + var i; + + xdata = x.data; + mdata = mask.data; + odata = out.data; + + xget = x.accessors[ 0 ]; + mget = mask.accessors[ 0 ]; + oset = out.accessors[ 1 ]; + + io = offset; + for ( i = 0; i < xdata.length; i++ ) { + if ( mget( mdata, i ) ) { + oset( odata, io, clbk.call( thisArg, xget( xdata, i ) ) ); + io += stride; + } + } + return odata; +} + +/** +* Applies a mask to a complex array & apply mapping function and assigns unmasked values to elements in a complex output array. +* +* @private +* @param {Collection} x - real-valued floating-point input array view +* @param {Object} mask - mask array object +* @param {Collection} out - real-valued floating-point output array view +* @param {integer} stride - output array stride +* @param {NonNegativeInteger} offset - output array offset +* @param {Function} clbk - function to apply +* @param {*} [thisArg] - function execution context +* @returns {Collection} output array view +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var arraylike2object = require( '@stdlib/array/base/arraylike2object' ); +* +* var x = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); +* var mask = [ 0, 1, 0, 1 ]; +* +* var out = new Float64Array( 8 ); +* +* function mappingFunction( x ) { +* return x * 10.0; +* } +* +* var arr = complex( x, arraylike2object( mask ), out, 1, 0, mappingFunction ); +* // returns [ 30.0, 40.0, 70.0, 80.0, 0.0, 0.0, 0.0, 0.0 ] +*/ +function complex( x, mask, out, stride, offset, clbk, thisArg ) { + var mdata; + var mget; + var io; + var so; + var i; + var j; + + mdata = mask.data; + mget = mask.accessors[ 0 ]; + + so = stride * 2; // note: multiply by 2, as real-valued array consists of interleaved real and imaginary components + io = offset * 2; + for ( i = 0; i < mdata.length; i++ ) { + if ( mget( mdata, i ) ) { + j = i * 2; + out[ io ] = clbk.call( thisArg, x[ j ] ); + out[ io+1 ] = clbk.call( thisArg, x[ j+1 ] ); + io += so; + } + } + return out; +} + + +// MAIN // + +/** +* Applies a mask to a provided input array & apply mapping function and assigns unmasked values to elements in a provided output array. +* +* @param {Collection} x - input array +* @param {Collection} mask - mask array +* @param {Collection} out - output array +* @param {integer} stride - output array stride +* @param {NonNegativeInteger} offset - output array offset +* @param {Function} clbk - function to apply +* @param {*} [thisArg] - function execution context +* @returns {Collection} output array +* +* @example +* var x = [ 1, 2, 3, 4 ]; +* var mask = [ 0, 1, 0, 1 ]; +* +* function mappingFunction( x ) { +* return x * 10; +* } +* +* var out = [ 0, 0 ]; +* var arr = assign( x, mask, out, 1, 0, mappingFunction ); +* // returns [ 20, 40 ] +* +* var bool = ( arr === out ); +* // returns true +*/ +function assign( x, mask, out, stride, offset, clbk, thisArg ) { + var xo; + var mo; + var oo; + + xo = arraylike2object( x ); + mo = arraylike2object( mask ); + oo = arraylike2object( out ); + if ( + xo.accessorProtocol || + mo.accessorProtocol || + oo.accessorProtocol + ) { + // Note: we only explicitly support complex-to-complex, as this function should not be concerned with casting rules, etc. That is left to userland... + if ( + isComplexDataType( xo.dtype ) && + isComplexDataType( oo.dtype ) + ) { + complex( reinterpret( x, 0 ), mo, reinterpret( out, 0 ), stride, offset, clbk, thisArg ); // eslint-disable-line max-len + return out; + } + accessors( xo, mo, oo, stride, offset, clbk, thisArg ); + return out; + } + indexed( x, mask, out, stride, offset, clbk, thisArg ); + return out; +} + + +// EXPORTS // + +module.exports = assign; diff --git a/lib/node_modules/@stdlib/array/base/mskfilter-map/lib/index.js b/lib/node_modules/@stdlib/array/base/mskfilter-map/lib/index.js new file mode 100644 index 000000000000..be0d823fc6c5 --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/mskfilter-map/lib/index.js @@ -0,0 +1,71 @@ +/** +* @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'; + +/** +* Apply a mask to a provided input array. +* +* @module @stdlib/array/base/mskfilter-map +* +* @example +* var mskfilterMap = require( '@stdlib/array/base/mskfilter-map' ); +* +* var x = [ 1, 2, 3, 4 ]; +* var mask = [ 0, 1, 0, 1 ]; +* +* function clbk( val ) { +* return 2 * val; +* } +* +* var y = mskfilterMap( x, mask, clbk ); +* // returns [ 4, 8 ] +* +* @example +* var mskfilterMap = require( '@stdlib/array/base/mskfilter-map' ); +* +* var x = [ 1, 2, 3, 4 ]; +* var mask = [ 0, 1, 0, 1 ]; +* +* function clbk( val ) { +* return 2 * val; +* } +* +* var out = [ 0, 0 ]; +* var arr = mskfilterMap.assign( x, mask, out, 1, 0, clbk ); +* // returns [ 4, 8 ] +* +* var bool = ( arr === out ); +* // returns true +*/ + +// MODULES // + +var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); +var main = require( './main.js' ); +var assign = require( './assign.js' ); + + +// MAIN // + +setReadOnly( main, 'assign', assign ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/array/base/mskfilter-map/lib/main.js b/lib/node_modules/@stdlib/array/base/mskfilter-map/lib/main.js new file mode 100644 index 000000000000..59ac51528a4c --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/mskfilter-map/lib/main.js @@ -0,0 +1,71 @@ +/** +* @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 resolveGetter = require( '@stdlib/array/base/resolve-getter' ); + + +// MAIN // + +/** +* Returns a new array by applying a mask to a provided input array. +* +* @param {Collection} x - input array +* @param {Collection} mask - mask array +* @param {Function} clbk - callback to invoke +* @param {*} [thisArg] - execution context +* @returns {Array} output array +* +* @example +* var x = [ 1, 2, 3, 4 ]; +* var mask = [ 0, 1, 0, 1 ]; +* +* function clbk( val ) { +* return 2 * val; +* } +* +* var y = mskfilterMap( x, mask, clbk ); +* // returns [ 4, 8 ] +*/ +function mskfilterMap( x, mask, clbk, thisArg ) { + var xget; + var mget; + var out; + var i; + + // Resolve accessors for retrieving array elements: + xget = resolveGetter( x ); + mget = resolveGetter( mask ); + + // Extract each desired element from the provided array... + out = []; + for ( i = 0; i < x.length; i++ ) { + if ( mget( mask, i ) ) { + out.push( clbk.call( thisArg, xget( x, i ) ) ); // use `Array#push` to ensure "fast" elements + } + } + return out; +} + + +// EXPORTS // + +module.exports = mskfilterMap; diff --git a/lib/node_modules/@stdlib/array/base/mskfilter-map/package.json b/lib/node_modules/@stdlib/array/base/mskfilter-map/package.json new file mode 100644 index 000000000000..c0b047cd7bc1 --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/mskfilter-map/package.json @@ -0,0 +1,66 @@ +{ + "name": "@stdlib/array/base/mskfilter-map", + "version": "0.0.0", + "description": "Apply a mask to a provided input 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", + "utilities", + "utils", + "generic", + "array", + "take", + "extract", + "copy", + "index", + "mask", + "map", + "filter", + "reject" + ] +} diff --git a/lib/node_modules/@stdlib/array/base/mskfilter-map/test/test.assign.js b/lib/node_modules/@stdlib/array/base/mskfilter-map/test/test.assign.js new file mode 100644 index 000000000000..aa4b1ac36f83 --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/mskfilter-map/test/test.assign.js @@ -0,0 +1,309 @@ +/** +* @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 Complex64Array = require( '@stdlib/array/complex64' ); +var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); +var Int32Array = require( '@stdlib/array/int32' ); +var isSameComplex64Array = require( '@stdlib/assert/is-same-complex64array' ); +var zeros = require( '@stdlib/array/zeros' ); +var mskfilterMap = require( './../lib' ).assign; + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof mskfilterMap, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function filters array elements (generic)', function test( t ) { + var expected; + var actual; + var mask; + var out; + var x; + + x = [ 1, 2, 3, 4 ]; + + mask = [ 0, 1, 0, 1 ]; + out = zeros( 2, 'generic' ); + actual = mskfilterMap( x, mask, out, 1, 0, clbk ); + expected = [ 4, 16 ]; + + t.strictEqual( actual, out, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + mask = [ 0, 0, 0, 0 ]; + out = zeros( 0, 'generic' ); + actual = mskfilterMap( x, mask, out, 1, 0, clbk ); + expected = []; + + t.strictEqual( actual, out, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + mask = [ 0, 0, 0, 1 ]; + out = zeros( 1, 'generic' ); + actual = mskfilterMap( x, mask, out, 1, 0, clbk ); + expected = [ 16 ]; + + t.strictEqual( actual, out, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + mask = [ 1, 1, 1, 1 ]; + out = zeros( 4, 'generic' ); + actual = mskfilterMap( x, mask, out, 1, 0, clbk ); + expected = [ 1, 4, 9, 16 ]; + + t.strictEqual( actual, out, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + mask = [ 0, 1, 0, 1 ]; + out = zeros( 4, 'generic' ); + actual = mskfilterMap( x, mask, out, -2, out.length-1, clbk ); + expected = [ 0, 16, 0, 4 ]; + + t.strictEqual( actual, out, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + t.end(); + + function clbk( val ) { + return val * val; + } +}); + +tape( 'the function filters array elements (real typed array)', function test( t ) { + var expected; + var actual; + var mask; + var out; + var x; + + x = new Int32Array( [ 1, 2, 3, 4 ] ); + + mask = [ 0, 1, 0, 1 ]; + out = zeros( 2, 'int32' ); + actual = mskfilterMap( x, mask, out, 1, 0, clbk ); + expected = new Int32Array( [ 4, 16 ] ); + + t.strictEqual( actual, out, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + mask = [ 0, 0, 0, 0 ]; + out = zeros( 0, 'int32' ); + actual = mskfilterMap( x, mask, out, 1, 0, clbk ); + expected = new Int32Array( [] ); + + t.strictEqual( actual, out, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + mask = [ 0, 0, 0, 1 ]; + out = zeros( 1, 'int32' ); + actual = mskfilterMap( x, mask, out, 1, 0, clbk ); + expected = new Int32Array( [ 16 ] ); + + t.strictEqual( actual, out, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + mask = [ 1, 1, 1, 1 ]; + out = zeros( 4, 'int32' ); + actual = mskfilterMap( x, mask, out, 1, 0, clbk ); + expected = new Int32Array( [ 1, 4, 9, 16 ] ); + + t.strictEqual( actual, out, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + mask = [ 0, 1, 0, 1 ]; + out = zeros( 4, 'int32' ); + actual = mskfilterMap( x, mask, out, -2, out.length-1, clbk ); + expected = new Int32Array( [ 0, 16, 0, 4 ] ); + + t.strictEqual( actual, out, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + t.end(); + + function clbk( val ) { + return val * val; + } +}); + +tape( 'the function filters array elements (complex typed array)', function test( t ) { + var expected; + var actual; + var mask; + var out; + var x; + + x = new Complex64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); + + mask = [ 1, 0, 1 ]; + out = zeros( 2, 'complex64' ); + actual = mskfilterMap( x, mask, out, 1, 0, clbk ); + expected = new Complex64Array( [ 1.0, 4.0, 25.0, 36.0 ] ); + + t.strictEqual( actual, out, 'returns expected value' ); + t.strictEqual( isSameComplex64Array( actual, expected ), true, 'returns expected value' ); + + mask = [ 0, 0, 0 ]; + out = zeros( 0, 'complex64' ); + actual = mskfilterMap( x, mask, out, 1, 0, clbk ); + expected = new Complex64Array( [] ); + + t.strictEqual( actual, out, 'returns expected value' ); + t.strictEqual( isSameComplex64Array( actual, expected ), true, 'returns expected value' ); + + mask = [ 0, 0, 1 ]; + out = zeros( 1, 'complex64' ); + actual = mskfilterMap( x, mask, out, 1, 0, clbk ); + expected = new Complex64Array( [ 25.0, 36.0 ] ); + + t.strictEqual( actual, out, 'returns expected value' ); + t.strictEqual( isSameComplex64Array( actual, expected ), true, 'returns expected value' ); + + mask = [ 1, 1, 1 ]; + out = zeros( 3, 'complex64' ); + actual = mskfilterMap( x, mask, out, 1, 0, clbk ); + expected = new Complex64Array( [ 1.0, 4.0, 9.0, 16.0, 25.0, 36.0 ] ); + + t.strictEqual( actual, out, 'returns expected value' ); + t.strictEqual( isSameComplex64Array( actual, expected ), true, 'returns expected value' ); + + mask = [ 1, 0, 0 ]; + out = zeros( 3, 'complex64' ); + actual = mskfilterMap( x, mask, out, -2, out.length-1, clbk ); + expected = new Complex64Array( [ 0.0, 0.0, 0.0, 0.0, 1.0, 4.0 ] ); + + t.strictEqual( actual, out, 'returns expected value' ); + t.strictEqual( isSameComplex64Array( actual, expected ), true, 'returns expected value' ); + + t.end(); + + function clbk( val ) { + return val * val; + } +}); + +tape( 'the function filters array elements (accessors)', function test( t ) { + var expected; + var actual; + var mask; + var out; + var x; + + x = toAccessorArray( [ 1, 2, 3, 4 ] ); + + mask = toAccessorArray( [ 0, 1, 0, 1 ] ); + out = toAccessorArray( zeros( 2, 'generic' ) ); + actual = mskfilterMap( x, mask, out, 1, 0, clbk ); + expected = [ 4, 16 ]; + + t.strictEqual( actual, out, 'returns expected value' ); + isEqual( actual, expected ); + + mask = toAccessorArray( [ 0, 0, 0, 0 ] ); + out = toAccessorArray( zeros( 0, 'generic' ) ); + actual = mskfilterMap( x, mask, out, 1, 0, clbk ); + expected = []; + + t.strictEqual( actual, out, 'returns expected value' ); + isEqual( actual, expected ); + + mask = toAccessorArray( [ 0, 0, 0, 1 ] ); + out = toAccessorArray( zeros( 1, 'generic' ) ); + actual = mskfilterMap( x, mask, out, 1, 0, clbk ); + expected = [ 16 ]; + + t.strictEqual( actual, out, 'returns expected value' ); + isEqual( actual, expected ); + + mask = toAccessorArray( [ 1, 1, 1, 1 ] ); + out = toAccessorArray( zeros( 4, 'generic' ) ); + actual = mskfilterMap( x, mask, out, 1, 0, clbk ); + expected = [ 1, 4, 9, 16 ]; + + t.strictEqual( actual, out, 'returns expected value' ); + isEqual( actual, expected ); + + mask = toAccessorArray( [ 0, 1, 0, 1 ] ); + out = toAccessorArray( zeros( 4, 'generic' ) ); + actual = mskfilterMap( x, mask, out, -2, out.length-1, clbk ); + expected = [ 0, 16, 0, 4 ]; + + t.strictEqual( actual, out, 'returns expected value' ); + isEqual( actual, expected ); + + t.end(); + + function isEqual( actual, expected ) { + var i; + for ( i = 0; i < expected.length; i++ ) { + t.strictEqual( actual.get( i ), expected[ i ], 'returns expected value' ); + } + } + + function clbk( val ) { + return val * val; + } +}); + +tape( 'the function returns leaves an output array unchanged if provided a second argument which masks all input array values', function test( t ) { + var expected; + var actual; + var mask; + var out; + var x; + + mask = [ 0, 0, 0, 0 ]; + + x = [ 1, 2, 3, 4 ]; + out = [ 0, 0, 0, 0 ]; + expected = [ 0, 0, 0, 0 ]; + actual = mskfilterMap( x, mask, out, 1, 0, clbk ); + t.deepEqual( actual, expected, 'returns expected value' ); + + x = toAccessorArray( [ 1, 2, 3, 4 ] ); + out = [ 0, 0, 0, 0 ]; + expected = [ 0, 0, 0, 0 ]; + actual = mskfilterMap( x, mask, out, 1, 0, clbk ); + t.deepEqual( actual, expected, 'returns expected value' ); + + x = new Int32Array( [ 1.0, 2.0, 3.0, 4.0 ] ); + out = [ 0, 0, 0, 0 ]; + expected = [ 0, 0, 0, 0 ]; + actual = mskfilterMap( x, mask, out, 1, 0, clbk ); + t.deepEqual( actual, expected, 'returns expected value' ); + + x = new Complex64Array( [ 1.0, 2.0, 3.0, 4.0 ] ); + out = new Complex64Array( [ 0.0, 0.0, 0.0, 0.0 ] ); + expected = new Complex64Array( [ 0.0, 0.0, 0.0, 0.0 ] ); + actual = mskfilterMap( x, mask, out, 1, 0, clbk ); + t.strictEqual( isSameComplex64Array( actual, expected ), true, 'returns expected value' ); + + t.end(); + + function clbk( val ) { + return val * val; + } +}); diff --git a/lib/node_modules/@stdlib/array/base/mskfilter-map/test/test.js b/lib/node_modules/@stdlib/array/base/mskfilter-map/test/test.js new file mode 100644 index 000000000000..028b9f7e5120 --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/mskfilter-map/test/test.js @@ -0,0 +1,41 @@ +/** +* @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 hasOwnProp = require( '@stdlib/assert/has-own-property' ); +var hasMethod = require( '@stdlib/assert/is-method' ); +var mskfilterMap = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof mskfilterMap, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'attached to the main export is an `assign` method', function test( t ) { + t.strictEqual( hasOwnProp( mskfilterMap, 'assign' ), true, 'returns expected value' ); + t.strictEqual( hasMethod( mskfilterMap, 'assign' ), true, 'returns expected value' ); + t.end(); +}); diff --git a/lib/node_modules/@stdlib/array/base/mskfilter-map/test/test.main.js b/lib/node_modules/@stdlib/array/base/mskfilter-map/test/test.main.js new file mode 100644 index 000000000000..d70c880334ca --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/mskfilter-map/test/test.main.js @@ -0,0 +1,112 @@ +/** +* @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 Complex64Array = require( '@stdlib/array/complex64' ); +var isSameComplex64 = require( '@stdlib/assert/is-same-complex64' ); +var isArray = require( '@stdlib/assert/is-array' ); +var mskfilterMap = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof mskfilterMap, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function filters array elements', function test( t ) { + var expected; + var actual; + var mask; + var x; + + x = [ 1, 2, 3, 4 ]; + + mask = [ 0, 1, 0, 1 ]; + actual = mskfilterMap( x, mask, clbk ); + expected = [ 2, 4 ]; + t.strictEqual( isArray( actual ), true, 'returns expected value' ); + t.notEqual( actual, x, 'returns new array' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + mask = [ 0, 0, 0, 0 ]; + actual = mskfilterMap( x, mask, clbk ); + expected = []; + t.strictEqual( isArray( actual ), true, 'returns expected value' ); + t.notEqual( actual, x, 'returns new array' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + mask = [ 0, 0, 0, 1 ]; + actual = mskfilterMap( x, mask, clbk ); + expected = [ 4 ]; + t.strictEqual( isArray( actual ), true, 'returns expected value' ); + t.notEqual( actual, x, 'returns new array' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + mask = [ 1, 1, 1, 1 ]; + actual = mskfilterMap( x, mask, clbk ); + expected = [ 1, 2, 3, 4 ]; + t.strictEqual( isArray( actual ), true, 'returns expected value' ); + t.notEqual( actual, x, 'returns new array' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + t.end(); + + function clbk( val ) { + return val; + } +}); + +tape( 'the function filters array elements (accessors)', function test( t ) { + var expected; + var actual; + var mask; + var x; + var i; + + x = new Complex64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); + mask = [ 0, 1, 0, 1 ]; + actual = mskfilterMap( x, mask, clbk ); + expected = [ x.get( 1 ), x.get( 3 ) ]; + + t.strictEqual( isArray( actual ), true, 'returns expected value' ); + t.notEqual( actual, x, 'returns different reference' ); + for ( i = 0; i < expected.length; i++ ) { + t.strictEqual( isSameComplex64( actual[ i ], expected[ i ] ), true, 'returns expected value' ); + } + t.end(); + + function clbk( val ) { + return val; + } +}); + +tape( 'the function returns an empty array if provided empty arrays', function test( t ) { + t.deepEqual( mskfilterMap( [], [], clbk ), [], 'returns expected value' ); + t.end(); + + function clbk( val ) { + return val; + } +}); From 351cb37d1779363bfe0522188c779786dd3adbea Mon Sep 17 00:00:00 2001 From: Golden <103646877+AuenKr@users.noreply.github.com> Date: Sat, 9 Mar 2024 15:01:23 +0530 Subject: [PATCH 02/18] Update lib/node_modules/@stdlib/array/base/mskfilter-map/README.md Co-authored-by: Athan Signed-off-by: Golden <103646877+AuenKr@users.noreply.github.com> --- lib/node_modules/@stdlib/array/base/mskfilter-map/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/array/base/mskfilter-map/README.md b/lib/node_modules/@stdlib/array/base/mskfilter-map/README.md index 8ca5d9bbbf4b..02af1b947afa 100644 --- a/lib/node_modules/@stdlib/array/base/mskfilter-map/README.md +++ b/lib/node_modules/@stdlib/array/base/mskfilter-map/README.md @@ -18,7 +18,7 @@ limitations under the License. --> -# mskfilter-map +# mskfilterMap > Apply a mask to a provided input array and return a new array after applying a mapping function. From 96e62040925c01db37779189a8060cd0b590569e Mon Sep 17 00:00:00 2001 From: Golden <103646877+AuenKr@users.noreply.github.com> Date: Sat, 9 Mar 2024 15:01:36 +0530 Subject: [PATCH 03/18] Update lib/node_modules/@stdlib/array/base/mskfilter-map/README.md Co-authored-by: Athan Signed-off-by: Golden <103646877+AuenKr@users.noreply.github.com> --- lib/node_modules/@stdlib/array/base/mskfilter-map/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/array/base/mskfilter-map/README.md b/lib/node_modules/@stdlib/array/base/mskfilter-map/README.md index 02af1b947afa..7213777d2321 100644 --- a/lib/node_modules/@stdlib/array/base/mskfilter-map/README.md +++ b/lib/node_modules/@stdlib/array/base/mskfilter-map/README.md @@ -32,7 +32,7 @@ var mskfilterMap = require( '@stdlib/array/base/mskfilter-map' ); #### mskfilterMap( x, mask, clbk\[, thisArg] ) -Returns a new array by applying a mask to a provided input array after applying mapping function. +Applies a mask to a provided input array and returns a new array after applying a mapping function. ```javascript var x = [ 1, 2, 3, 4 ]; From 1a307f248c87ddd731cf61aca2da60978564d709 Mon Sep 17 00:00:00 2001 From: Golden <103646877+AuenKr@users.noreply.github.com> Date: Sat, 9 Mar 2024 15:01:58 +0530 Subject: [PATCH 04/18] Update lib/node_modules/@stdlib/array/base/mskfilter-map/README.md Co-authored-by: Athan Signed-off-by: Golden <103646877+AuenKr@users.noreply.github.com> --- lib/node_modules/@stdlib/array/base/mskfilter-map/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/array/base/mskfilter-map/README.md b/lib/node_modules/@stdlib/array/base/mskfilter-map/README.md index 7213777d2321..c722ad3e8ce1 100644 --- a/lib/node_modules/@stdlib/array/base/mskfilter-map/README.md +++ b/lib/node_modules/@stdlib/array/base/mskfilter-map/README.md @@ -58,7 +58,7 @@ The function **always** returns a new "generic" array. #### mskfilterMap.assign( x, mask, out, stride, offset ) -Applies a mask & mapping function to a provided input array and assigns unmasked values to elements in a provided output array. +Applies a mask and mapping function to a provided input array and assigns results to elements in a provided output array. ```javascript var x = [ 1, 2, 3, 4 ]; From 77e3fc09b0ff2107d45bfd74240652a0e98996a4 Mon Sep 17 00:00:00 2001 From: Golden <103646877+AuenKr@users.noreply.github.com> Date: Sat, 9 Mar 2024 15:02:11 +0530 Subject: [PATCH 05/18] Update lib/node_modules/@stdlib/array/base/mskfilter-map/README.md Co-authored-by: Athan Signed-off-by: Golden <103646877+AuenKr@users.noreply.github.com> --- lib/node_modules/@stdlib/array/base/mskfilter-map/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/array/base/mskfilter-map/README.md b/lib/node_modules/@stdlib/array/base/mskfilter-map/README.md index c722ad3e8ce1..01e5df19d51b 100644 --- a/lib/node_modules/@stdlib/array/base/mskfilter-map/README.md +++ b/lib/node_modules/@stdlib/array/base/mskfilter-map/README.md @@ -56,7 +56,7 @@ To set the applied function's execution context, provide a `thisArg`. The function **always** returns a new "generic" array. -#### mskfilterMap.assign( x, mask, out, stride, offset ) +#### mskfilterMap.assign( x, mask, out, stride, offset, clbk\[, thisArg] ) Applies a mask and mapping function to a provided input array and assigns results to elements in a provided output array. From b1c66e1107afd96af79ed4e8885e03cfad7b6a69 Mon Sep 17 00:00:00 2001 From: Golden <103646877+AuenKr@users.noreply.github.com> Date: Sat, 9 Mar 2024 15:02:51 +0530 Subject: [PATCH 06/18] Update lib/node_modules/@stdlib/array/base/mskfilter-map/README.md Co-authored-by: Athan Signed-off-by: Golden <103646877+AuenKr@users.noreply.github.com> --- lib/node_modules/@stdlib/array/base/mskfilter-map/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/node_modules/@stdlib/array/base/mskfilter-map/README.md b/lib/node_modules/@stdlib/array/base/mskfilter-map/README.md index 01e5df19d51b..d96e23e2eb51 100644 --- a/lib/node_modules/@stdlib/array/base/mskfilter-map/README.md +++ b/lib/node_modules/@stdlib/array/base/mskfilter-map/README.md @@ -110,6 +110,7 @@ The function supports the following parameters: ```javascript var zeroTo = require( '@stdlib/array/base/zero-to' ); var bernoulli = require( '@stdlib/random/array/bernoulli' ); +var abs2 = require( '@stdlib/math/base/special/abs2' ); var mskfilterMap = require( '@stdlib/array/base/mskfilter-map' ); // Generate a linearly spaced array: From e419fd520f74e3abb21350db6b6c4855aa0c2a8b Mon Sep 17 00:00:00 2001 From: Golden <103646877+AuenKr@users.noreply.github.com> Date: Sat, 9 Mar 2024 15:03:03 +0530 Subject: [PATCH 07/18] Update lib/node_modules/@stdlib/array/base/mskfilter-map/README.md Co-authored-by: Athan Signed-off-by: Golden <103646877+AuenKr@users.noreply.github.com> --- lib/node_modules/@stdlib/array/base/mskfilter-map/README.md | 5 ----- 1 file changed, 5 deletions(-) diff --git a/lib/node_modules/@stdlib/array/base/mskfilter-map/README.md b/lib/node_modules/@stdlib/array/base/mskfilter-map/README.md index d96e23e2eb51..367abffe2096 100644 --- a/lib/node_modules/@stdlib/array/base/mskfilter-map/README.md +++ b/lib/node_modules/@stdlib/array/base/mskfilter-map/README.md @@ -123,11 +123,6 @@ var mask = bernoulli( x.length, 0.5, { }); console.log( mask ); -// Mapping functions -function square( val ) { - return val * val; -} - // Filter an array using the mask: var y = mskfilterMap( x, mask, square ); console.log( y ); From 529c822f5c1b71cdf70d5c01d6df1f727bff0426 Mon Sep 17 00:00:00 2001 From: Golden <103646877+AuenKr@users.noreply.github.com> Date: Sat, 9 Mar 2024 15:03:13 +0530 Subject: [PATCH 08/18] Update lib/node_modules/@stdlib/array/base/mskfilter-map/README.md Co-authored-by: Athan Signed-off-by: Golden <103646877+AuenKr@users.noreply.github.com> --- lib/node_modules/@stdlib/array/base/mskfilter-map/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/array/base/mskfilter-map/README.md b/lib/node_modules/@stdlib/array/base/mskfilter-map/README.md index 367abffe2096..802db722dcd0 100644 --- a/lib/node_modules/@stdlib/array/base/mskfilter-map/README.md +++ b/lib/node_modules/@stdlib/array/base/mskfilter-map/README.md @@ -124,7 +124,7 @@ var mask = bernoulli( x.length, 0.5, { console.log( mask ); // Filter an array using the mask: -var y = mskfilterMap( x, mask, square ); +var y = mskfilterMap( x, mask, abs2 ); console.log( y ); ``` From c4a6735050b716e38730c4677e78d5bc468f0af8 Mon Sep 17 00:00:00 2001 From: Golden <103646877+AuenKr@users.noreply.github.com> Date: Sat, 9 Mar 2024 15:04:26 +0530 Subject: [PATCH 09/18] Update lib/node_modules/@stdlib/array/base/mskfilter-map/lib/main.js Co-authored-by: Athan Signed-off-by: Golden <103646877+AuenKr@users.noreply.github.com> --- lib/node_modules/@stdlib/array/base/mskfilter-map/lib/main.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/array/base/mskfilter-map/lib/main.js b/lib/node_modules/@stdlib/array/base/mskfilter-map/lib/main.js index 59ac51528a4c..db6cc6135294 100644 --- a/lib/node_modules/@stdlib/array/base/mskfilter-map/lib/main.js +++ b/lib/node_modules/@stdlib/array/base/mskfilter-map/lib/main.js @@ -59,7 +59,7 @@ function mskfilterMap( x, mask, clbk, thisArg ) { out = []; for ( i = 0; i < x.length; i++ ) { if ( mget( mask, i ) ) { - out.push( clbk.call( thisArg, xget( x, i ) ) ); // use `Array#push` to ensure "fast" elements + out.push( clbk.call( thisArg, xget( x, i ), i, x ) ); // use `Array#push` to ensure "fast" elements } } return out; From 8cd599936e63daea7bc2318a28884bfd94afe3f7 Mon Sep 17 00:00:00 2001 From: AuenKr Date: Tue, 12 Mar 2024 00:34:18 +0530 Subject: [PATCH 10/18] fix: applied feedback to mskfilter-map --- .../array/base/mskfilter-map/README.md | 24 ++ .../benchmark/benchmark.assign.length.js | 7 +- .../base/mskfilter-map/benchmark/benchmark.js | 7 +- .../benchmark/benchmark.length.js | 7 +- .../array/base/mskfilter-map/docs/repl.txt | 8 +- .../base/mskfilter-map/docs/types/index.d.ts | 135 ++++++++++- .../base/mskfilter-map/docs/types/test.ts | 219 ++++++++++++++++-- .../base/mskfilter-map/examples/index.js | 8 +- .../array/base/mskfilter-map/lib/assign.js | 34 +-- .../array/base/mskfilter-map/lib/index.js | 18 +- .../array/base/mskfilter-map/lib/main.js | 10 +- .../base/mskfilter-map/test/test.assign.js | 2 +- .../base/mskfilter-map/test/test.main.js | 56 ++++- 13 files changed, 441 insertions(+), 94 deletions(-) diff --git a/lib/node_modules/@stdlib/array/base/mskfilter-map/README.md b/lib/node_modules/@stdlib/array/base/mskfilter-map/README.md index 802db722dcd0..840925c12ffb 100644 --- a/lib/node_modules/@stdlib/array/base/mskfilter-map/README.md +++ b/lib/node_modules/@stdlib/array/base/mskfilter-map/README.md @@ -87,6 +87,30 @@ The function supports the following parameters: - **clbk**: function to apply. - **thisArg**: applied function execution context (_optional_). +To set the applied function's execution context, provide a `thisArg`. + + + +```javascript +function clbk( x ) { + this.count += 1; + return x; +} + +var x = [ 1.0, 2.0, 3.0, 4.0 ]; +var mask = [ 1, 1, 0, 1 ]; + +var ctx = { + 'count': 0 +}; + +var y = mskfilterMap( x, mask, clbk, ctx ); +// returns [ 1.0, 2.0, 4.0 ] + +var v = ctx.count; +// returns 3 +``` + diff --git a/lib/node_modules/@stdlib/array/base/mskfilter-map/benchmark/benchmark.assign.length.js b/lib/node_modules/@stdlib/array/base/mskfilter-map/benchmark/benchmark.assign.length.js index aacdae56bf81..3a6e2bd75bc5 100644 --- a/lib/node_modules/@stdlib/array/base/mskfilter-map/benchmark/benchmark.assign.length.js +++ b/lib/node_modules/@stdlib/array/base/mskfilter-map/benchmark/benchmark.assign.length.js @@ -27,6 +27,7 @@ var zeros = require( '@stdlib/array/zeros' ); var ones = require( '@stdlib/array/ones' ); var isArray = require( '@stdlib/assert/is-array' ); var isnan = require( '@stdlib/assert/is-nan' ).isPrimitive; +var identity = require( '@stdlib/math/base/special/identity' ); var pkg = require( './../package.json' ).name; var mskfilterMap = require( './../lib' ); @@ -61,13 +62,9 @@ function createBenchmark( len ) { var v; var i; - function clbk( val ) { - return val; - } - b.tic(); for ( i = 0; i < b.iterations; i++ ) { - v = mskfilterMap.assign( x, mask, out, 1, 0, clbk ); + v = mskfilterMap.assign( x, mask, out, 1, 0, identity ); if ( typeof v !== 'object' ) { b.fail( 'should return an array' ); } diff --git a/lib/node_modules/@stdlib/array/base/mskfilter-map/benchmark/benchmark.js b/lib/node_modules/@stdlib/array/base/mskfilter-map/benchmark/benchmark.js index 1acc3e96cdba..8a65993f2f60 100644 --- a/lib/node_modules/@stdlib/array/base/mskfilter-map/benchmark/benchmark.js +++ b/lib/node_modules/@stdlib/array/base/mskfilter-map/benchmark/benchmark.js @@ -24,6 +24,7 @@ var bench = require( '@stdlib/bench' ); var isArray = require( '@stdlib/assert/is-array' ); var zeroTo = require( '@stdlib/array/base/zero-to' ); var ones = require( '@stdlib/array/base/ones' ); +var identity = require( '@stdlib/math/base/special/identity' ); var pkg = require( './../package.json' ).name; var mskfilterMap = require( './../lib' ); @@ -39,13 +40,9 @@ bench( pkg+'::copy:len=100', function benchmark( b ) { x = zeroTo( 100 ); y = ones( x.length ); - function clbk( val ) { - return val; - } - b.tic(); for ( i = 0; i < b.iterations; i++ ) { - v = mskfilterMap( x, y, clbk ); + v = mskfilterMap( x, y, identity ); if ( typeof v !== 'object' ) { b.fail( 'should return an array' ); } diff --git a/lib/node_modules/@stdlib/array/base/mskfilter-map/benchmark/benchmark.length.js b/lib/node_modules/@stdlib/array/base/mskfilter-map/benchmark/benchmark.length.js index fe9ec108182b..9cb4e523aa42 100644 --- a/lib/node_modules/@stdlib/array/base/mskfilter-map/benchmark/benchmark.length.js +++ b/lib/node_modules/@stdlib/array/base/mskfilter-map/benchmark/benchmark.length.js @@ -25,6 +25,7 @@ var pow = require( '@stdlib/math/base/special/pow' ); var zeroTo = require( '@stdlib/array/base/zero-to' ); var ones = require( '@stdlib/array/base/ones' ); var isArray = require( '@stdlib/assert/is-array' ); +var identity = require( '@stdlib/math/base/special/identity' ); var pkg = require( './../package.json' ).name; var mskfiltermap = require( './../lib' ); @@ -53,13 +54,9 @@ function createBenchmark( len ) { var v; var i; - function clbk( val ) { - return val; - } - b.tic(); for ( i = 0; i < b.iterations; i++ ) { - v = mskfiltermap( x, y, clbk ); + v = mskfiltermap( x, y, identity ); if ( typeof v !== 'object' ) { b.fail( 'should return an array' ); } diff --git a/lib/node_modules/@stdlib/array/base/mskfilter-map/docs/repl.txt b/lib/node_modules/@stdlib/array/base/mskfilter-map/docs/repl.txt index e2ad49f66cd2..5ab7835f7c17 100644 --- a/lib/node_modules/@stdlib/array/base/mskfilter-map/docs/repl.txt +++ b/lib/node_modules/@stdlib/array/base/mskfilter-map/docs/repl.txt @@ -1,7 +1,7 @@ {{alias}}( x, mask, fcn[, thisArg] ) - Returns a new array by applying a mask & mapping function to a provided - input array. + Applies a mask to a provided input array and returns a new array after + applying a mapping function. If a mask array element is truthy, the corresponding element in `x` is included in the output array; otherwise, the corresponding element in `x` @@ -34,8 +34,8 @@ {{alias}}.assign( x, mask, out, stride, offset, fcn[, thisArg] ) - Applies mask to a provided input array & mapping function and assigns - unmasked values to elements in a provided output array. + Applies a mask and mapping function to a provided input array and assigns + results to elements in a provided output array. If a mask array element is truthy, the corresponding element in `x` is included in the output array; otherwise, the corresponding element in `x` diff --git a/lib/node_modules/@stdlib/array/base/mskfilter-map/docs/types/index.d.ts b/lib/node_modules/@stdlib/array/base/mskfilter-map/docs/types/index.d.ts index 66786b2746a6..14eea089b891 100644 --- a/lib/node_modules/@stdlib/array/base/mskfilter-map/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/array/base/mskfilter-map/docs/types/index.d.ts @@ -20,10 +20,115 @@ /// -import { Collection } from '@stdlib/types/array'; +import { Array2D, Collection } from '@stdlib/types/array'; /** -* Returns a new array by applying a mask to a provided input array. +* Array element indices. +*/ +type Indices = [ number, number ]; + +/** +* Callback invoked for each array element. +* +* @returns result +*/ +type Nullary = ( this: V ) => U; + +/** +* Callback invoked for each array element. +* +* @param value - array element +* @returns result +*/ +type Unary = ( this: V, value: T ) => U; + +/** +* Callback invoked for each array element. +* +* @param value - array element +* @param indices - current array element indices +* @returns result +*/ +type Binary = ( this: V, value: T, indices: Indices ) => U; + +/** +* Callback invoked for each array element. +* +* @param value - array element +* @param indices - current array element indices +* @param array - input array +* @returns result +*/ +type Ternary = ( this: V, value: T, indices: Indices, array: Array2D ) => U; + +/** +* Callback invoked for each array element. +* +* @param value - array element +* @param indices - current array element indices +* @param array - input array +* @returns result +*/ +type Callback = Nullary | Unary | Binary | Ternary; + +/** +* Interface describing the main export. +*/ +interface Routine { + /** + * Applies a mask to a provided input array and returns a new array after applying a mapping function. + * + * @param x - input array + * @param mask - mask array + * @param clbk - callback to invoke + * @param thisArg - execution context + * @returns output array + * + * @example + * var x = [ 1, 2, 3, 4 ]; + * + * function scale( val ) { + * return 10 * val; + * } + * + * var y = mskfilterMap( x, [ 0, 1, 0, 1 ], scale ); + * // returns [ 20, 40 ] + */ + ( x: Collection, mask: Collection, clbk: Callback, thisArg?: ThisParameterType> ): Array; + + /** + * Applies a mask and mapping function to an indexed array and assigns results to elements in an indexed output array. + * + * @param x - input nested array + * @param mask - mask array + * @param out - output array + * @param stride - output array stride + * @param offset - output array offset + * @param clbk - callback to invoke + * @param thisArg - execution context + * @returns output array + * + * @example + * var x = [ 1, 2, 3, 4 ]; + * var mask = [ 0, 1, 0, 1 ]; + * + * var out = [ 0, 0, 0, 0 ]; + * + * function scale( x ) { + * return x * 10; + * } + * + * var arr = mskfilterMap.assign( x, mask, out, 1, 0, scale ); + * // returns [ 20, 40, 0, 0 ] + * + * var bool = ( arr === out ); + * // returns true + */ + assign( x: Collection, mask: Collection, out: Collection, stride: number, offset: number, clbk: Callback, thisArg?: ThisParameterType> ): Array; +} + +/** +* Applies a mask to a provided input array and returns a new array after applying a mapping function. * * @param x - input array * @param mask - mask array @@ -34,14 +139,30 @@ import { Collection } from '@stdlib/types/array'; * @example * var x = [ 1, 2, 3, 4 ]; * -* function clbk( val ) { -* return 2 * val; +* function scale( val ) { +* return 10 * val; * } * -* var y = mskfilterMap( x, [ 0, 1, 0, 1 ], clbk ); -* // returns [ 4, 8 ] +* var y = mskfilterMap( x, [ 0, 1, 0, 1 ], scale ); +* // returns [ 20, 40 ] +* +* @example +* var x = [ 1, 2, 3, 4 ]; +* var mask = [ 0, 1, 0, 1 ]; +* +* var out = [ 0, 0, 0, 0 ]; +* +* function scale( x ) { +* return x * 10; +* } +* +* var arr = mskfilterMap.assign( x, mask, out, 1, 0, scale ); +* // returns [ 20, 40, 0, 0 ] +* +* var bool = ( arr === out ); +* // returns true */ -declare function mskfilterMap( x: Collection, mask: Collection, clbk: Function, thisArg?: ThisParameterType ): Array; +declare var mskfilterMap: Routine; // EXPORTS // diff --git a/lib/node_modules/@stdlib/array/base/mskfilter-map/docs/types/test.ts b/lib/node_modules/@stdlib/array/base/mskfilter-map/docs/types/test.ts index 09009400ef0c..5a37bf2a54dc 100644 --- a/lib/node_modules/@stdlib/array/base/mskfilter-map/docs/types/test.ts +++ b/lib/node_modules/@stdlib/array/base/mskfilter-map/docs/types/test.ts @@ -18,22 +18,30 @@ import mskfilterMap = require( './index' ); +/** +* Unary function. +* +* @param value - input value +* @returns result +*/ function clbk( value: number ): number { return value; } // TESTS // +// The function returns a nested array... +{ + const x = [ 1.0, 2.0, 3.0, 4.0 ]; + + mskfilterMap( x, [ 1, 1, 1, 1 ], clbk ); // $ExpectType number[] + mskfilterMap( x, [ 1, 1, 1, 1 ], clbk, {} ); // $ExpectType number[] +} + // The function returns an array... { - mskfilterMap( [ 1, 2, 3, 4 ], [ 0, 0 ], clbk ); // $ExpectType number[] - mskfilterMap( [ 1, 2, 3, 4 ], [ 0, 0 ], clbk, {} ); // $ExpectType number[] - mskfilterMap( [ 1, 2, 3, 4 ], [ 0, 0 ], clbk ); // $ExpectType any[] - mskfilterMap( [ 1, 2, 3, 4 ], [ 0, 0 ], clbk, {} ); // $ExpectType any[] - mskfilterMap( [ 1, 2, 3, 4 ], [ 0, 0 ], clbk ); // $ExpectType number[] - mskfilterMap( [ 1, 2, 3, 4 ], [ 0, 0 ], clbk, {} ); // $ExpectType number[] - mskfilterMap( [ '1', '2', '3', '4' ], [ 0, 0 ], clbk ); // $ExpectType string[] - mskfilterMap( [ '1', '2', '3', '4' ], [ 0, 0 ], clbk, {} ); // $ExpectType string[] + mskfilterMap( [ 1, 2, 3, 4 ], [ 1, 1, 1, 1 ], clbk ); // $ExpectType number[] + mskfilterMap( [ 1, 2, 3, 4 ], [ 1, 1, 1, 1 ], clbk, {} ); // $ExpectType number[] } // The compiler throws an error if the function is provided a first argument which is not an array-like object... @@ -74,21 +82,21 @@ function clbk( value: number ): number { { const x = [1, 2]; - mskfilterMap( x, [ 2, 2 ], 'abc' ); // $ExpectError - mskfilterMap( x, [ 2, 2 ], 3.14 ); // $ExpectError - mskfilterMap( x, [ 2, 2 ], true ); // $ExpectError - mskfilterMap( x, [ 2, 2 ], false ); // $ExpectError - mskfilterMap( x, [ 2, 2 ], null ); // $ExpectError - mskfilterMap( x, [ 2, 2 ], [ '1' ] ); // $ExpectError - mskfilterMap( x, [ 2, 2 ], {} ); // $ExpectError - - mskfilterMap( x, [ 2, 2 ], 'abc', {} ); // $ExpectError - mskfilterMap( x, [ 2, 2 ], 3.14, {} ); // $ExpectError - mskfilterMap( x, [ 2, 2 ], true, {} ); // $ExpectError - mskfilterMap( x, [ 2, 2 ], false, {} ); // $ExpectError - mskfilterMap( x, [ 2, 2 ], null, {} ); // $ExpectError - mskfilterMap( x, [ 2, 2 ], [ '1' ], {} ); // $ExpectError - mskfilterMap( x, [ 2, 2 ], {}, {} ); // $ExpectError + mskfilterMap( x, [ 1, 1 ], 'abc' ); // $ExpectError + mskfilterMap( x, [ 1, 1 ], 3.14 ); // $ExpectError + mskfilterMap( x, [ 1, 1 ], true ); // $ExpectError + mskfilterMap( x, [ 1, 1 ], false ); // $ExpectError + mskfilterMap( x, [ 1, 1 ], null ); // $ExpectError + mskfilterMap( x, [ 1, 1 ], [ '1' ] ); // $ExpectError + mskfilterMap( x, [ 1, 1 ], {} ); // $ExpectError + + mskfilterMap( x, [ 1, 1 ], 'abc', {} ); // $ExpectError + mskfilterMap( x, [ 1, 1 ], 3.14, {} ); // $ExpectError + mskfilterMap( x, [ 1, 1 ], true, {} ); // $ExpectError + mskfilterMap( x, [ 1, 1 ], false, {} ); // $ExpectError + mskfilterMap( x, [ 1, 1 ], null, {} ); // $ExpectError + mskfilterMap( x, [ 1, 1 ], [ '1' ], {} ); // $ExpectError + mskfilterMap( x, [ 1, 1 ], {}, {} ); // $ExpectError } // The compiler throws an error if the function is provided an unsupported number of arguments... @@ -97,6 +105,167 @@ function clbk( value: number ): number { mskfilterMap(); // $ExpectError mskfilterMap( x ); // $ExpectError - mskfilterMap( x, [ 2, 2 ] ); // $ExpectError - mskfilterMap( x, [ 2, 2 ], clbk, {}, {} ); // $ExpectError + mskfilterMap( x, [ 0, 1 ] ); // $ExpectError + mskfilterMap( x, [ 0, 1 ], clbk, {}, {} ); // $ExpectError +} + +// Attached to the main export is an `assign` method which returns a nested array... +{ + const x = [ 1.0, 2.0, 3.0, 4.0 ]; + const out = [ 0, 0, 0, 0 ]; + + mskfilterMap.assign( x, [1, 0, 1, 0], out, 1, 0, clbk ); // $ExpectType number[] + mskfilterMap.assign( x, [1, 0, 1, 0], out, 1, 0, clbk, {} ); // $ExpectType number[] +} + +// The compiler throws an error if the `assign` method is provided a first argument which is not a nested array... +{ + const mask = [ 0, 0, 0, 0 ]; + + mskfilterMap.assign( 'abc', mask, [ 0, 0, 0, 0 ], 1, 0, clbk ); // $ExpectError + mskfilterMap.assign( 3.14, mask, [ 0, 0, 0, 0 ], 1, 0, clbk ); // $ExpectError + mskfilterMap.assign( true, mask, [ 0, 0, 0, 0 ], 1, 0, clbk ); // $ExpectError + mskfilterMap.assign( false, mask, [ 0, 0, 0, 0 ], 1, 0, clbk ); // $ExpectError + mskfilterMap.assign( null, mask, [ 0, 0, 0, 0 ], 1, 0, clbk ); // $ExpectError + mskfilterMap.assign( [ '1' ], mask, [ 0, 0, 0, 0 ], 1, 0, clbk ); // $ExpectError + mskfilterMap.assign( {}, mask, [ 0, 0, 0, 0 ], 1, 0, clbk ); // $ExpectError + mskfilterMap.assign( ( x: number ): number => x, mask, [ 0, 0, 0, 0 ], 1, 0, clbk ); // $ExpectError + + mskfilterMap.assign( 'abc', mask, [ 0, 0, 0, 0 ], 1, 0, clbk, {} ); // $ExpectError + mskfilterMap.assign( 3.14, mask, [ 0, 0, 0, 0 ], 1, 0, clbk, {} ); // $ExpectError + mskfilterMap.assign( true, mask, [ 0, 0, 0, 0 ], 1, 0, clbk, {} ); // $ExpectError + mskfilterMap.assign( false, mask, [ 0, 0, 0, 0 ], 1, 0, clbk, {} ); // $ExpectError + mskfilterMap.assign( null, mask, [ 0, 0, 0, 0 ], 1, 0, clbk, {} ); // $ExpectError + mskfilterMap.assign( [ '1' ], mask, [ 0, 0, 0, 0 ], 1, 0, clbk, {} ); // $ExpectError + mskfilterMap.assign( {}, mask, [ 0, 0, 0, 0 ], 1, 0, clbk, {} ); // $ExpectError + mskfilterMap.assign( ( x: number ): number => x, mask, [ 0, 0, 0, 0 ], 1, 0, clbk, {} ); // $ExpectError +} + +// The compiler throws an error if the `assign` method is provided a second argument which is not a nested array... +{ + const x = [ 1.0, 2.0, 3.0, 4.0 ]; + + mskfilterMap.assign( x, 'abc', [ 0, 0, 0, 0 ], 1, 0, clbk ); // $ExpectError + mskfilterMap.assign( x, 3.14, [ 0, 0, 0, 0 ], 1, 0, clbk ); // $ExpectError + mskfilterMap.assign( x, true, [ 0, 0, 0, 0 ], 1, 0, clbk ); // $ExpectError + mskfilterMap.assign( x, false, [ 0, 0, 0, 0 ], 1, 0, clbk ); // $ExpectError + mskfilterMap.assign( x, null, [ 0, 0, 0, 0 ], 1, 0, clbk ); // $ExpectError + mskfilterMap.assign( x, [ '1' ], [ 0, 0, 0, 0 ], 1, 0, clbk ); // $ExpectError + mskfilterMap.assign( x, {}, [ 0, 0, 0, 0 ], 1, 0, clbk ); // $ExpectError + mskfilterMap.assign( x, ( x: number ): number => x, [ 0, 0, 0, 0 ], 1, 0, clbk ); // $ExpectError + + mskfilterMap.assign( x, 'abc', [ 0, 0, 0, 0 ], 1, 0, clbk, {} ); // $ExpectError + mskfilterMap.assign( x, 3.14, [ 0, 0, 0, 0 ], 1, 0, clbk, {} ); // $ExpectError + mskfilterMap.assign( x, true, [ 0, 0, 0, 0 ], 1, 0, clbk, {} ); // $ExpectError + mskfilterMap.assign( x, false, [ 0, 0, 0, 0 ], 1, 0, clbk, {} ); // $ExpectError + mskfilterMap.assign( x, null, [ 0, 0, 0, 0 ], 1, 0, clbk, {} ); // $ExpectError + mskfilterMap.assign( x, [ '1' ], [ 0, 0, 0, 0 ], 1, 0, clbk, {} ); // $ExpectError + mskfilterMap.assign( x, {}, [ 0, 0, 0, 0 ], 1, 0, clbk, {} ); // $ExpectError + mskfilterMap.assign( x, ( x: number ): number => x, [ 0, 0, 0, 0 ], 1, 0, clbk, {} ); // $ExpectError +} + +// The compiler throws an error if the `assign` method is provided a third argument which is not an array of numbers... +{ + const x = [ 1.0, 2.0, 3.0, 4.0 ]; + const mask = [ 0.0, 0.0, 0.0, 0.0 ]; + + mskfilterMap.assign( x, mask, 'abc', 1, 0, clbk ); // $ExpectError + mskfilterMap.assign( x, mask, 3.14, 1, 0, clbk ); // $ExpectError + mskfilterMap.assign( x, mask, true, 1, 0, clbk ); // $ExpectError + mskfilterMap.assign( x, mask, false, 1, 0, clbk ); // $ExpectError + mskfilterMap.assign( x, mask, null, 1, 0, clbk ); // $ExpectError + mskfilterMap.assign( x, mask, [ '1' ], 1, 0, clbk ); // $ExpectError + mskfilterMap.assign( x, mask, {}, 1, 0, clbk ); // $ExpectError + mskfilterMap.assign( x, mask, ( x: number ): number => x, 1, 0, clbk ); // $ExpectError + + mskfilterMap.assign( x, mask, 'abc', 1, 0, clbk, {} ); // $ExpectError + mskfilterMap.assign( x, mask, 3.14, 1, 0, clbk, {} ); // $ExpectError + mskfilterMap.assign( x, mask, true, 1, 0, clbk, {} ); // $ExpectError + mskfilterMap.assign( x, mask, false, 1, 0, clbk, {} ); // $ExpectError + mskfilterMap.assign( x, mask, null, 1, 0, clbk, {} ); // $ExpectError + mskfilterMap.assign( x, mask, [ '1' ], 1, 0, clbk, {} ); // $ExpectError + mskfilterMap.assign( x, mask, {}, 1, 0, clbk, {} ); // $ExpectError + mskfilterMap.assign( x, mask, ( x: number ): number => x, 1, 0, clbk, {} ); // $ExpectError +} + +// The compiler throws an error if the `assign` method is provided a fourth argument which is not a number... +{ + const x = [ 1.0, 2.0, 3.0, 4.0 ]; + const mask = [ 0.0, 0.0, 0.0, 0.0 ]; + + mskfilterMap.assign( x, mask, [ 0, 0, 0, 0 ], 'abc', 0, clbk ); // $ExpectError + mskfilterMap.assign( x, mask, [ 0, 0, 0, 0 ], [ 1 ], 0, clbk ); // $ExpectError + mskfilterMap.assign( x, mask, [ 0, 0, 0, 0 ], true, 0, clbk ); // $ExpectError + mskfilterMap.assign( x, mask, [ 0, 0, 0, 0 ], false, 0, clbk ); // $ExpectError + mskfilterMap.assign( x, mask, [ 0, 0, 0, 0 ], null, 0, clbk ); // $ExpectError + mskfilterMap.assign( x, mask, [ 0, 0, 0, 0 ], [ '1' ], 0, clbk ); // $ExpectError + mskfilterMap.assign( x, mask, [ 0, 0, 0, 0 ], {}, 0, clbk ); // $ExpectError + mskfilterMap.assign( x, mask, [ 0, 0, 0, 0 ], ( x: number ): number => x, 0, clbk ); // $ExpectError + + mskfilterMap.assign( x, mask, [ 0, 0, 0, 0 ], 'abc', 0, clbk, {} ); // $ExpectError + mskfilterMap.assign( x, mask, [ 0, 0, 0, 0 ], [ 1 ], 0, clbk, {} ); // $ExpectError + mskfilterMap.assign( x, mask, [ 0, 0, 0, 0 ], true, 0, clbk, {} ); // $ExpectError + mskfilterMap.assign( x, mask, [ 0, 0, 0, 0 ], false, 0, clbk, {} ); // $ExpectError + mskfilterMap.assign( x, mask, [ 0, 0, 0, 0 ], null, 0, clbk, {} ); // $ExpectError + mskfilterMap.assign( x, mask, [ 0, 0, 0, 0 ], [ '1' ], 0, clbk, {} ); // $ExpectError + mskfilterMap.assign( x, mask, [ 0, 0, 0, 0 ], {}, 0, clbk, {} ); // $ExpectError + mskfilterMap.assign( x, mask, [ 0, 0, 0, 0 ], ( x: number ): number => x, 0, clbk, {} ); // $ExpectError } + +// The compiler throws an error if the `assign` method is provided a fifth argument which is not an array of numbers... +{ + const x = [ 1.0, 2.0, 3.0, 4.0 ]; + const mask = [ 0.0, 0.0, 0.0, 0.0 ]; + + mskfilterMap.assign( x, mask, [ 0, 0, 0, 0 ], 1, 'abc', clbk ); // $ExpectError + mskfilterMap.assign( x, mask, [ 0, 0, 0, 0 ], 1, [ 1 ], clbk ); // $ExpectError + mskfilterMap.assign( x, mask, [ 0, 0, 0, 0 ], 1, true, clbk ); // $ExpectError + mskfilterMap.assign( x, mask, [ 0, 0, 0, 0 ], 1, false, clbk ); // $ExpectError + mskfilterMap.assign( x, mask, [ 0, 0, 0, 0 ], 1, null, clbk ); // $ExpectError + mskfilterMap.assign( x, mask, [ 0, 0, 0, 0 ], 1, [ '1' ], clbk ); // $ExpectError + mskfilterMap.assign( x, mask, [ 0, 0, 0, 0 ], 1, {}, clbk ); // $ExpectError + mskfilterMap.assign( x, mask, [ 0, 0, 0, 0 ], 1, ( x: number ): number => x, clbk ); // $ExpectError + + mskfilterMap.assign( x, mask, [ 0, 0, 0, 0 ], 1, 'abc', clbk, {} ); // $ExpectError + mskfilterMap.assign( x, mask, [ 0, 0, 0, 0 ], 1, [ 1 ], clbk, {} ); // $ExpectError + mskfilterMap.assign( x, mask, [ 0, 0, 0, 0 ], 1, true, clbk, {} ); // $ExpectError + mskfilterMap.assign( x, mask, [ 0, 0, 0, 0 ], 1, false, clbk, {} ); // $ExpectError + mskfilterMap.assign( x, mask, [ 0, 0, 0, 0 ], 1, null, clbk, {} ); // $ExpectError + mskfilterMap.assign( x, mask, [ 0, 0, 0, 0 ], 1, [ '1' ], clbk, {} ); // $ExpectError + mskfilterMap.assign( x, mask, [ 0, 0, 0, 0 ], 1, {}, clbk, {} ); // $ExpectError + mskfilterMap.assign( x, mask, [ 0, 0, 0, 0 ], 1, ( x: number ): number => x, clbk, {} ); // $ExpectError +} + +// The compiler throws an error if the `assign` method is provided a sixth argument which is not a valid callback... +{ + const x = [ 1.0, 2.0, 3.0, 4.0 ]; + const mask = [ 0.0, 0.0, 0.0, 0.0 ]; + + mskfilterMap.assign( x, mask, [ 0, 0, 0, 0 ], 1, 0, 'abc' ); // $ExpectError + mskfilterMap.assign( x, mask, [ 0, 0, 0, 0 ], 1, 0, 3.14 ); // $ExpectError + mskfilterMap.assign( x, mask, [ 0, 0, 0, 0 ], 1, 0, true ); // $ExpectError + mskfilterMap.assign( x, mask, [ 0, 0, 0, 0 ], 1, 0, false ); // $ExpectError + mskfilterMap.assign( x, mask, [ 0, 0, 0, 0 ], 1, 0, null ); // $ExpectError + mskfilterMap.assign( x, mask, [ 0, 0, 0, 0 ], 1, 0, [ '1' ] ); // $ExpectError + mskfilterMap.assign( x, mask, [ 0, 0, 0, 0 ], 1, 0, {} ); // $ExpectError + + mskfilterMap.assign( x, mask, [ 0, 0, 0, 0 ], 1, 0, 'abc', {} ); // $ExpectError + mskfilterMap.assign( x, mask, [ 0, 0, 0, 0 ], 1, 0, 3.14, {} ); // $ExpectError + mskfilterMap.assign( x, mask, [ 0, 0, 0, 0 ], 1, 0, true, {} ); // $ExpectError + mskfilterMap.assign( x, mask, [ 0, 0, 0, 0 ], 1, 0, false, {} ); // $ExpectError + mskfilterMap.assign( x, mask, [ 0, 0, 0, 0 ], 1, 0, null, {} ); // $ExpectError + mskfilterMap.assign( x, mask, [ 0, 0, 0, 0 ], 1, 0, [ '1' ], {} ); // $ExpectError + mskfilterMap.assign( x, mask, [ 0, 0, 0, 0 ], 1, 0, {}, {} ); // $ExpectError +} + +// The compiler throws an error if the `assign` method is provided an unsupported number of arguments... +{ + const x = [ 1.0, 2.0, 3.0, 4.0 ]; + const mask = [ 0.0, 0.0, 0.0, 0.0 ]; + + mskfilterMap.assign(); // $ExpectError + mskfilterMap.assign( x, mask ); // $ExpectError + mskfilterMap.assign( x, mask, [ 0, 0, 0, 0 ] ); // $ExpectError + mskfilterMap.assign( x, mask, [ 0, 0, 0, 0 ], clbk, {}, {} ); // $ExpectError +} + diff --git a/lib/node_modules/@stdlib/array/base/mskfilter-map/examples/index.js b/lib/node_modules/@stdlib/array/base/mskfilter-map/examples/index.js index e16390753a24..e110d6169309 100644 --- a/lib/node_modules/@stdlib/array/base/mskfilter-map/examples/index.js +++ b/lib/node_modules/@stdlib/array/base/mskfilter-map/examples/index.js @@ -20,6 +20,7 @@ var zeroTo = require( '@stdlib/array/base/zero-to' ); var bernoulli = require( '@stdlib/random/array/bernoulli' ); +var abs2 = require( '@stdlib/math/base/special/abs2' ); var mskfilterMap = require( './../lib' ); // Generate a linearly spaced array: @@ -32,11 +33,6 @@ var mask = bernoulli( x.length, 0.5, { }); console.log( mask ); -// Mapping callback function: -function clbk( x ) { - return 2 * x; -} - // Filter an array using the mask & apply mapping function: -var y = mskfilterMap( x, mask, clbk ); +var y = mskfilterMap( x, mask, abs2 ); console.log( y ); diff --git a/lib/node_modules/@stdlib/array/base/mskfilter-map/lib/assign.js b/lib/node_modules/@stdlib/array/base/mskfilter-map/lib/assign.js index b89c019b8bf2..f8f69c6d6065 100644 --- a/lib/node_modules/@stdlib/array/base/mskfilter-map/lib/assign.js +++ b/lib/node_modules/@stdlib/array/base/mskfilter-map/lib/assign.js @@ -28,7 +28,7 @@ var reinterpret = require( '@stdlib/strided/base/reinterpret-complex' ); // FUNCTIONS // /** -* Applies a mask to an indexed array & apply mapping function and assigns unmasked values to elements in an indexed output array. +* Applies a mask and mapping function to an indexed array and assigns results to elements in an indexed output array. * * @private * @param {Collection} x - input array @@ -46,11 +46,11 @@ var reinterpret = require( '@stdlib/strided/base/reinterpret-complex' ); * * var out = [ 0, 0, 0, 0 ]; * -* function mappingFunction( x ) { +* function scale( x ) { * return x * 10; * } * -* var arr = indexed( x, mask, out, 1, 0, mappingFunction ); +* var arr = indexed( x, mask, out, 1, 0, scale ); * // returns [ 20, 40, 0, 0 ] */ function indexed( x, mask, out, stride, offset, clbk, thisArg ) { @@ -60,7 +60,7 @@ function indexed( x, mask, out, stride, offset, clbk, thisArg ) { io = offset; for ( i = 0; i < x.length; i++ ) { if ( mask[ i ] ) { - out[ io ] = clbk.call( thisArg, x[ i ] ); + out[ io ] = clbk.call( thisArg, x[ i ], i, x ); io += stride; } } @@ -68,7 +68,7 @@ function indexed( x, mask, out, stride, offset, clbk, thisArg ) { } /** -* Applies a mask to an accessor array & apply mapping function and assigns unmasked values to elements in an accessor output array. +* Applies a mask and mapping function to an input accessor array and assigns results to elements in an output accessor array. * * @private * @param {Object} x - input array object @@ -87,12 +87,12 @@ function indexed( x, mask, out, stride, offset, clbk, thisArg ) { * var x = toAccessorArray( [ 1, 2, 3, 4 ] ); * var mask = toAccessorArray( [ 0, 1, 0, 1 ] ); * -* function mappingFunction( x ) { +* function scale( x ) { * return x * 10; * } * * var out = toAccessorArray( [ 0, 0, 0, 0 ] ); -* var arr = accessors( arraylike2object( x ), arraylike2object( mask ), arraylike2object( out ), 1, 0, mappingFunction ); +* var arr = accessors( arraylike2object( x ), arraylike2object( mask ), arraylike2object( out ), 1, 0, scale ); * * var v = arr.get( 0 ); * // returns 20 @@ -121,7 +121,7 @@ function accessors( x, mask, out, stride, offset, clbk, thisArg ) { io = offset; for ( i = 0; i < xdata.length; i++ ) { if ( mget( mdata, i ) ) { - oset( odata, io, clbk.call( thisArg, xget( xdata, i ) ) ); + oset( odata, io, clbk.call( thisArg, xget( xdata, i ), i, xdata ) ); io += stride; } } @@ -129,7 +129,7 @@ function accessors( x, mask, out, stride, offset, clbk, thisArg ) { } /** -* Applies a mask to a complex array & apply mapping function and assigns unmasked values to elements in a complex output array. +* Applies a mask and mapping function to a complex array and assigns results to elements in an output complex array. * * @private * @param {Collection} x - real-valued floating-point input array view @@ -150,11 +150,11 @@ function accessors( x, mask, out, stride, offset, clbk, thisArg ) { * * var out = new Float64Array( 8 ); * -* function mappingFunction( x ) { +* function scale( x ) { * return x * 10.0; * } * -* var arr = complex( x, arraylike2object( mask ), out, 1, 0, mappingFunction ); +* var arr = complex( x, arraylike2object( mask ), out, 1, 0, scale ); * // returns [ 30.0, 40.0, 70.0, 80.0, 0.0, 0.0, 0.0, 0.0 ] */ function complex( x, mask, out, stride, offset, clbk, thisArg ) { @@ -168,13 +168,13 @@ function complex( x, mask, out, stride, offset, clbk, thisArg ) { mdata = mask.data; mget = mask.accessors[ 0 ]; - so = stride * 2; // note: multiply by 2, as real-valued array consists of interleaved real and imaginary components + so = stride * 2; // note: multiply by 2, as a real-valued array consists of interleaved real and imaginary components io = offset * 2; for ( i = 0; i < mdata.length; i++ ) { if ( mget( mdata, i ) ) { j = i * 2; - out[ io ] = clbk.call( thisArg, x[ j ] ); - out[ io+1 ] = clbk.call( thisArg, x[ j+1 ] ); + out[ io ] = clbk.call( thisArg, x[ j ], j, x ); + out[ io+1 ] = clbk.call( thisArg, x[ j+1 ], j+1, x ); io += so; } } @@ -185,7 +185,7 @@ function complex( x, mask, out, stride, offset, clbk, thisArg ) { // MAIN // /** -* Applies a mask to a provided input array & apply mapping function and assigns unmasked values to elements in a provided output array. +* Applies a mask and mapping function to a provided input array and assigns results to elements in a provided output array. * * @param {Collection} x - input array * @param {Collection} mask - mask array @@ -200,12 +200,12 @@ function complex( x, mask, out, stride, offset, clbk, thisArg ) { * var x = [ 1, 2, 3, 4 ]; * var mask = [ 0, 1, 0, 1 ]; * -* function mappingFunction( x ) { +* function scale( x ) { * return x * 10; * } * * var out = [ 0, 0 ]; -* var arr = assign( x, mask, out, 1, 0, mappingFunction ); +* var arr = assign( x, mask, out, 1, 0, scale ); * // returns [ 20, 40 ] * * var bool = ( arr === out ); diff --git a/lib/node_modules/@stdlib/array/base/mskfilter-map/lib/index.js b/lib/node_modules/@stdlib/array/base/mskfilter-map/lib/index.js index be0d823fc6c5..a0c687f1a083 100644 --- a/lib/node_modules/@stdlib/array/base/mskfilter-map/lib/index.js +++ b/lib/node_modules/@stdlib/array/base/mskfilter-map/lib/index.js @@ -19,7 +19,7 @@ 'use strict'; /** -* Apply a mask to a provided input array. +* Apply a mask to a provided input array and returns a new array after applying a mapping function. * * @module @stdlib/array/base/mskfilter-map * @@ -29,12 +29,12 @@ * var x = [ 1, 2, 3, 4 ]; * var mask = [ 0, 1, 0, 1 ]; * -* function clbk( val ) { -* return 2 * val; +* function scale( val ) { +* return 10 * val; * } * -* var y = mskfilterMap( x, mask, clbk ); -* // returns [ 4, 8 ] +* var y = mskfilterMap( x, mask, scale ); +* // returns [ 20, 40 ] * * @example * var mskfilterMap = require( '@stdlib/array/base/mskfilter-map' ); @@ -42,13 +42,13 @@ * var x = [ 1, 2, 3, 4 ]; * var mask = [ 0, 1, 0, 1 ]; * -* function clbk( val ) { -* return 2 * val; +* function scale( val ) { +* return 10 * val; * } * * var out = [ 0, 0 ]; -* var arr = mskfilterMap.assign( x, mask, out, 1, 0, clbk ); -* // returns [ 4, 8 ] +* var arr = mskfilterMap.assign( x, mask, out, 1, 0, scale ); +* // returns [ 20, 40 ] * * var bool = ( arr === out ); * // returns true diff --git a/lib/node_modules/@stdlib/array/base/mskfilter-map/lib/main.js b/lib/node_modules/@stdlib/array/base/mskfilter-map/lib/main.js index db6cc6135294..671eca3a4071 100644 --- a/lib/node_modules/@stdlib/array/base/mskfilter-map/lib/main.js +++ b/lib/node_modules/@stdlib/array/base/mskfilter-map/lib/main.js @@ -26,7 +26,7 @@ var resolveGetter = require( '@stdlib/array/base/resolve-getter' ); // MAIN // /** -* Returns a new array by applying a mask to a provided input array. +* Apply a mask to a provided input array and returns a new array after applying a mapping function. * * @param {Collection} x - input array * @param {Collection} mask - mask array @@ -38,12 +38,12 @@ var resolveGetter = require( '@stdlib/array/base/resolve-getter' ); * var x = [ 1, 2, 3, 4 ]; * var mask = [ 0, 1, 0, 1 ]; * -* function clbk( val ) { -* return 2 * val; +* function scale( val ) { +* return 10 * val; * } * -* var y = mskfilterMap( x, mask, clbk ); -* // returns [ 4, 8 ] +* var y = mskfilterMap( x, mask, scale ); +* // returns [ 20, 40 ] */ function mskfilterMap( x, mask, clbk, thisArg ) { var xget; diff --git a/lib/node_modules/@stdlib/array/base/mskfilter-map/test/test.assign.js b/lib/node_modules/@stdlib/array/base/mskfilter-map/test/test.assign.js index aa4b1ac36f83..161dd38bfffd 100644 --- a/lib/node_modules/@stdlib/array/base/mskfilter-map/test/test.assign.js +++ b/lib/node_modules/@stdlib/array/base/mskfilter-map/test/test.assign.js @@ -268,7 +268,7 @@ tape( 'the function filters array elements (accessors)', function test( t ) { } }); -tape( 'the function returns leaves an output array unchanged if provided a second argument which masks all input array values', function test( t ) { +tape( 'the function returns leaves an output array unchanged if provided a second argument having all element equal to zero ', function test( t ) { var expected; var actual; var mask; diff --git a/lib/node_modules/@stdlib/array/base/mskfilter-map/test/test.main.js b/lib/node_modules/@stdlib/array/base/mskfilter-map/test/test.main.js index d70c880334ca..7429251591e4 100644 --- a/lib/node_modules/@stdlib/array/base/mskfilter-map/test/test.main.js +++ b/lib/node_modules/@stdlib/array/base/mskfilter-map/test/test.main.js @@ -45,7 +45,7 @@ tape( 'the function filters array elements', function test( t ) { mask = [ 0, 1, 0, 1 ]; actual = mskfilterMap( x, mask, clbk ); - expected = [ 2, 4 ]; + expected = [ 4, 16 ]; t.strictEqual( isArray( actual ), true, 'returns expected value' ); t.notEqual( actual, x, 'returns new array' ); t.deepEqual( actual, expected, 'returns expected value' ); @@ -59,14 +59,14 @@ tape( 'the function filters array elements', function test( t ) { mask = [ 0, 0, 0, 1 ]; actual = mskfilterMap( x, mask, clbk ); - expected = [ 4 ]; + expected = [ 16 ]; t.strictEqual( isArray( actual ), true, 'returns expected value' ); t.notEqual( actual, x, 'returns new array' ); t.deepEqual( actual, expected, 'returns expected value' ); mask = [ 1, 1, 1, 1 ]; actual = mskfilterMap( x, mask, clbk ); - expected = [ 1, 2, 3, 4 ]; + expected = [ 1, 4, 9, 16 ]; t.strictEqual( isArray( actual ), true, 'returns expected value' ); t.notEqual( actual, x, 'returns new array' ); t.deepEqual( actual, expected, 'returns expected value' ); @@ -74,7 +74,7 @@ tape( 'the function filters array elements', function test( t ) { t.end(); function clbk( val ) { - return val; + return val * val; } }); @@ -107,6 +107,52 @@ tape( 'the function returns an empty array if provided empty arrays', function t t.end(); function clbk( val ) { - return val; + return val * val; + } +}); + +tape( 'the function supports providing a function execution context', function test( t ) { + var expected; + var indices; + var values; + var arrays; + var mask; + var out; + var ctx; + var x; + + values = []; + indices = []; + arrays = []; + + x = [ 1.0, 2.0, 3.0, 4.0 ]; + mask = [ 1, 1, 1, 1 ]; + + ctx = { + 'count': 0 + }; + out = mskfilterMap( x, mask, clbk, ctx ); + + expected = [ 1.0, 2.0, 3.0, 4.0]; + t.deepEqual( out, expected, 'returns expected value' ); + t.strictEqual( ctx.count, 4, 'returns expected value' ); + + expected = [ 1.0, 2.0, 3.0, 4.0 ]; + t.deepEqual( values, expected, 'returns expected value' ); + + expected = [ 0, 1, 2, 3 ]; + t.deepEqual( indices, expected, 'returns expected value' ); + + expected = [ x, x, x, x ]; + t.deepEqual( arrays, expected, 'returns expected value' ); + + t.end(); + + function clbk( value, idx, array ) { + values.push( value ); + indices.push( idx ); + arrays.push( array ); + ctx.count += 1; + return value; } }); From 5cf2b698c7c0e07785ce1caff7e9318b0e6973a1 Mon Sep 17 00:00:00 2001 From: AuenKr Date: Tue, 12 Mar 2024 20:03:27 +0530 Subject: [PATCH 11/18] fix: applied all suggestions --- .../array/base/mskfilter-map/lib/assign.js | 63 ----------------- .../base/mskfilter-map/test/test.assign.js | 68 ++++++++++++------- 2 files changed, 42 insertions(+), 89 deletions(-) diff --git a/lib/node_modules/@stdlib/array/base/mskfilter-map/lib/assign.js b/lib/node_modules/@stdlib/array/base/mskfilter-map/lib/assign.js index f8f69c6d6065..49189a7425cc 100644 --- a/lib/node_modules/@stdlib/array/base/mskfilter-map/lib/assign.js +++ b/lib/node_modules/@stdlib/array/base/mskfilter-map/lib/assign.js @@ -20,9 +20,7 @@ // MODULES // -var isComplexDataType = require( '@stdlib/array/base/assert/is-complex-floating-point-data-type' ); var arraylike2object = require( '@stdlib/array/base/arraylike2object' ); -var reinterpret = require( '@stdlib/strided/base/reinterpret-complex' ); // FUNCTIONS // @@ -128,59 +126,6 @@ function accessors( x, mask, out, stride, offset, clbk, thisArg ) { return odata; } -/** -* Applies a mask and mapping function to a complex array and assigns results to elements in an output complex array. -* -* @private -* @param {Collection} x - real-valued floating-point input array view -* @param {Object} mask - mask array object -* @param {Collection} out - real-valued floating-point output array view -* @param {integer} stride - output array stride -* @param {NonNegativeInteger} offset - output array offset -* @param {Function} clbk - function to apply -* @param {*} [thisArg] - function execution context -* @returns {Collection} output array view -* -* @example -* var Float64Array = require( '@stdlib/array/float64' ); -* var arraylike2object = require( '@stdlib/array/base/arraylike2object' ); -* -* var x = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); -* var mask = [ 0, 1, 0, 1 ]; -* -* var out = new Float64Array( 8 ); -* -* function scale( x ) { -* return x * 10.0; -* } -* -* var arr = complex( x, arraylike2object( mask ), out, 1, 0, scale ); -* // returns [ 30.0, 40.0, 70.0, 80.0, 0.0, 0.0, 0.0, 0.0 ] -*/ -function complex( x, mask, out, stride, offset, clbk, thisArg ) { - var mdata; - var mget; - var io; - var so; - var i; - var j; - - mdata = mask.data; - mget = mask.accessors[ 0 ]; - - so = stride * 2; // note: multiply by 2, as a real-valued array consists of interleaved real and imaginary components - io = offset * 2; - for ( i = 0; i < mdata.length; i++ ) { - if ( mget( mdata, i ) ) { - j = i * 2; - out[ io ] = clbk.call( thisArg, x[ j ], j, x ); - out[ io+1 ] = clbk.call( thisArg, x[ j+1 ], j+1, x ); - io += so; - } - } - return out; -} - // MAIN // @@ -224,14 +169,6 @@ function assign( x, mask, out, stride, offset, clbk, thisArg ) { mo.accessorProtocol || oo.accessorProtocol ) { - // Note: we only explicitly support complex-to-complex, as this function should not be concerned with casting rules, etc. That is left to userland... - if ( - isComplexDataType( xo.dtype ) && - isComplexDataType( oo.dtype ) - ) { - complex( reinterpret( x, 0 ), mo, reinterpret( out, 0 ), stride, offset, clbk, thisArg ); // eslint-disable-line max-len - return out; - } accessors( xo, mo, oo, stride, offset, clbk, thisArg ); return out; } diff --git a/lib/node_modules/@stdlib/array/base/mskfilter-map/test/test.assign.js b/lib/node_modules/@stdlib/array/base/mskfilter-map/test/test.assign.js index 161dd38bfffd..a70f3c13ee05 100644 --- a/lib/node_modules/@stdlib/array/base/mskfilter-map/test/test.assign.js +++ b/lib/node_modules/@stdlib/array/base/mskfilter-map/test/test.assign.js @@ -23,8 +23,8 @@ var tape = require( 'tape' ); var Complex64Array = require( '@stdlib/array/complex64' ); var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); +var reinterpret = require( '@stdlib/strided/base/reinterpret-complex' ); var Int32Array = require( '@stdlib/array/int32' ); -var isSameComplex64Array = require( '@stdlib/assert/is-same-complex64array' ); var zeros = require( '@stdlib/array/zeros' ); var mskfilterMap = require( './../lib' ).assign; @@ -156,50 +156,57 @@ tape( 'the function filters array elements (complex typed array)', function test var out; var x; - x = new Complex64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); + x = reinterpret(new Complex64Array( [ 1.0, 2.0, 3.0, 4.0 ] ) ); - mask = [ 1, 0, 1 ]; - out = zeros( 2, 'complex64' ); + mask = [ 1, 0 ]; + out = reinterpret( zeros( 1, 'complex64' ) ); actual = mskfilterMap( x, mask, out, 1, 0, clbk ); - expected = new Complex64Array( [ 1.0, 4.0, 25.0, 36.0 ] ); + expected = reinterpret( new Complex64Array( [ 1.0, 4.0, 25.0, 36.0 ] ) ); t.strictEqual( actual, out, 'returns expected value' ); - t.strictEqual( isSameComplex64Array( actual, expected ), true, 'returns expected value' ); + isEqual( actual, expected ); - mask = [ 0, 0, 0 ]; - out = zeros( 0, 'complex64' ); + mask = [ 0, 0 ]; + out = reinterpret( zeros( 0, 'complex64' ) ); actual = mskfilterMap( x, mask, out, 1, 0, clbk ); - expected = new Complex64Array( [] ); + expected = reinterpret( new Complex64Array( [] ) ); t.strictEqual( actual, out, 'returns expected value' ); - t.strictEqual( isSameComplex64Array( actual, expected ), true, 'returns expected value' ); + isEqual( actual, expected ); - mask = [ 0, 0, 1 ]; - out = zeros( 1, 'complex64' ); + mask = [ 0, 1 ]; + out = reinterpret( zeros( 1, 'complex64' ) ); actual = mskfilterMap( x, mask, out, 1, 0, clbk ); - expected = new Complex64Array( [ 25.0, 36.0 ] ); + expected = reinterpret( new Complex64Array( [ 9.0, 16.0 ] ) ); t.strictEqual( actual, out, 'returns expected value' ); - t.strictEqual( isSameComplex64Array( actual, expected ), true, 'returns expected value' ); + isEqual( actual, expected ); - mask = [ 1, 1, 1 ]; - out = zeros( 3, 'complex64' ); + mask = [ 1, 1 ]; + out = reinterpret( zeros( 3, 'complex64' ) ); actual = mskfilterMap( x, mask, out, 1, 0, clbk ); - expected = new Complex64Array( [ 1.0, 4.0, 9.0, 16.0, 25.0, 36.0 ] ); + expected = reinterpret( new Complex64Array( [ 1.0, 4.0, 9.0, 16.0 ] ) ); t.strictEqual( actual, out, 'returns expected value' ); - t.strictEqual( isSameComplex64Array( actual, expected ), true, 'returns expected value' ); + isEqual( actual, expected ); - mask = [ 1, 0, 0 ]; - out = zeros( 3, 'complex64' ); + mask = [ 1, 0 ]; + out = reinterpret( zeros( 3, 'complex64' ) ); actual = mskfilterMap( x, mask, out, -2, out.length-1, clbk ); - expected = new Complex64Array( [ 0.0, 0.0, 0.0, 0.0, 1.0, 4.0 ] ); + expected = reinterpret( new Complex64Array( [ 0.0, 0.0, 1.0, 4.0 ] ) ); t.strictEqual( actual, out, 'returns expected value' ); - t.strictEqual( isSameComplex64Array( actual, expected ), true, 'returns expected value' ); + isEqual( actual, expected ); t.end(); + function isEqual( actual, expected ) { + var i; + for ( i = 0; i < expected.length; i++ ) { + t.strictEqual( actual.get( i ), expected[ i ], 'returns expected value' ); + } + } + function clbk( val ) { return val * val; } @@ -295,14 +302,23 @@ tape( 'the function returns leaves an output array unchanged if provided a secon actual = mskfilterMap( x, mask, out, 1, 0, clbk ); t.deepEqual( actual, expected, 'returns expected value' ); - x = new Complex64Array( [ 1.0, 2.0, 3.0, 4.0 ] ); - out = new Complex64Array( [ 0.0, 0.0, 0.0, 0.0 ] ); - expected = new Complex64Array( [ 0.0, 0.0, 0.0, 0.0 ] ); + mask = [ 0, 0 ]; + + x = reinterpret( new Complex64Array( [ 1.0, 2.0, 3.0, 4.0 ] ) ); + out = reinterpret( new Complex64Array( [ 0.0, 0.0, 0.0, 0.0 ] ) ); + expected = reinterpret( new Complex64Array( [ 0.0, 0.0, 0.0, 0.0 ] ) ); actual = mskfilterMap( x, mask, out, 1, 0, clbk ); - t.strictEqual( isSameComplex64Array( actual, expected ), true, 'returns expected value' ); + isEqual( actual, expected ); t.end(); + function isEqual( actual, expected ) { + var i; + for ( i = 0; i < expected.length; i++ ) { + t.strictEqual( actual.get( i ), expected[ i ], 'returns expected value' ); + } + } + function clbk( val ) { return val * val; } From 2ad6713d62a29728d70497a259881be70d492bbb Mon Sep 17 00:00:00 2001 From: Athan Date: Thu, 14 Mar 2024 03:24:01 -0700 Subject: [PATCH 12/18] Apply suggestions from code review Signed-off-by: Athan --- .../@stdlib/array/base/mskfilter-map/README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/node_modules/@stdlib/array/base/mskfilter-map/README.md b/lib/node_modules/@stdlib/array/base/mskfilter-map/README.md index 840925c12ffb..124c14a7fd53 100644 --- a/lib/node_modules/@stdlib/array/base/mskfilter-map/README.md +++ b/lib/node_modules/@stdlib/array/base/mskfilter-map/README.md @@ -35,13 +35,13 @@ var mskfilterMap = require( '@stdlib/array/base/mskfilter-map' ); Applies a mask to a provided input array and returns a new array after applying a mapping function. ```javascript -var x = [ 1, 2, 3, 4 ]; - -function customMapping( value ) { +function clbk( value ) { return value * 2; } -var y = mskfilterMap( x, [ 0, 1, 0, 1 ], customMapping ); +var x = [ 1, 2, 3, 4 ]; + +var y = mskfilterMap( x, [ 0, 1, 0, 1 ], clbk ); // returns [ 4, 8 ] ``` From 43c7b76255ab5ae7dc19fc802abd87d419c49e8f Mon Sep 17 00:00:00 2001 From: Athan Date: Thu, 14 Mar 2024 03:25:26 -0700 Subject: [PATCH 13/18] Apply suggestions from code review Signed-off-by: Athan --- lib/node_modules/@stdlib/array/base/mskfilter-map/README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/array/base/mskfilter-map/README.md b/lib/node_modules/@stdlib/array/base/mskfilter-map/README.md index 124c14a7fd53..38468e173f7c 100644 --- a/lib/node_modules/@stdlib/array/base/mskfilter-map/README.md +++ b/lib/node_modules/@stdlib/array/base/mskfilter-map/README.md @@ -40,8 +40,9 @@ function clbk( value ) { } var x = [ 1, 2, 3, 4 ]; +var m = [ 0, 1, 0, 1 ]; -var y = mskfilterMap( x, [ 0, 1, 0, 1 ], clbk ); +var y = mskfilterMap( x, m, clbk ); // returns [ 4, 8 ] ``` From b02c3f234bbf0659bb6428f5e139ec5bd69a263b Mon Sep 17 00:00:00 2001 From: Athan Date: Thu, 14 Mar 2024 03:27:10 -0700 Subject: [PATCH 14/18] Apply suggestions from code review Signed-off-by: Athan --- .../@stdlib/array/base/mskfilter-map/README.md | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/lib/node_modules/@stdlib/array/base/mskfilter-map/README.md b/lib/node_modules/@stdlib/array/base/mskfilter-map/README.md index 38468e173f7c..4e99f61bb413 100644 --- a/lib/node_modules/@stdlib/array/base/mskfilter-map/README.md +++ b/lib/node_modules/@stdlib/array/base/mskfilter-map/README.md @@ -62,16 +62,15 @@ The function **always** returns a new "generic" array. Applies a mask and mapping function to a provided input array and assigns results to elements in a provided output array. ```javascript +function clbk( value ) { + return value * 2; +} + var x = [ 1, 2, 3, 4 ]; -var mask = [ 0, 1, 0, 1 ]; +var m = [ 0, 1, 0, 1 ]; var out = [ 0, 0, 0, 0 ]; - -function clbk( val ) { - return val * 2; -} - -var arr = mskfilterMap.assign( x, mask, out, -2, out.length-1, clbk ); +var arr = mskfilterMap.assign( x, m, out, -2, out.length-1, clbk ); // returns [ 0, 8, 0, 4 ] var bool = ( arr === out ); From a280a9ba4d9361acaf5b86166024b3d77aae115f Mon Sep 17 00:00:00 2001 From: Athan Date: Thu, 14 Mar 2024 03:29:40 -0700 Subject: [PATCH 15/18] Apply suggestions from code review Signed-off-by: Athan --- .../@stdlib/array/base/mskfilter-map/README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/node_modules/@stdlib/array/base/mskfilter-map/README.md b/lib/node_modules/@stdlib/array/base/mskfilter-map/README.md index 4e99f61bb413..9c0bf3bc52e5 100644 --- a/lib/node_modules/@stdlib/array/base/mskfilter-map/README.md +++ b/lib/node_modules/@stdlib/array/base/mskfilter-map/README.md @@ -97,15 +97,15 @@ function clbk( x ) { return x; } -var x = [ 1.0, 2.0, 3.0, 4.0 ]; -var mask = [ 1, 1, 0, 1 ]; +var x = [ 1, 2, 3, 4 ]; +var m = [ 1, 1, 0, 1 ]; var ctx = { 'count': 0 }; -var y = mskfilterMap( x, mask, clbk, ctx ); -// returns [ 1.0, 2.0, 4.0 ] +var y = mskfilterMap( x, m, clbk, ctx ); +// returns [ 1, 2, 4 ] var v = ctx.count; // returns 3 From f53589aa1b8d8810a13da896537fab7425f21179 Mon Sep 17 00:00:00 2001 From: Athan Date: Thu, 14 Mar 2024 03:37:59 -0700 Subject: [PATCH 16/18] Apply suggestions from code review Signed-off-by: Athan --- .../array/base/mskfilter-map/benchmark/benchmark.length.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/node_modules/@stdlib/array/base/mskfilter-map/benchmark/benchmark.length.js b/lib/node_modules/@stdlib/array/base/mskfilter-map/benchmark/benchmark.length.js index 9cb4e523aa42..ba417028e9df 100644 --- a/lib/node_modules/@stdlib/array/base/mskfilter-map/benchmark/benchmark.length.js +++ b/lib/node_modules/@stdlib/array/base/mskfilter-map/benchmark/benchmark.length.js @@ -27,7 +27,7 @@ var ones = require( '@stdlib/array/base/ones' ); var isArray = require( '@stdlib/assert/is-array' ); var identity = require( '@stdlib/math/base/special/identity' ); var pkg = require( './../package.json' ).name; -var mskfiltermap = require( './../lib' ); +var mskfilterMap = require( './../lib' ); // FUNCTIONS // @@ -56,7 +56,7 @@ function createBenchmark( len ) { b.tic(); for ( i = 0; i < b.iterations; i++ ) { - v = mskfiltermap( x, y, identity ); + v = mskfilterMap( x, y, identity ); if ( typeof v !== 'object' ) { b.fail( 'should return an array' ); } From 1e00b97112b9ea312c73d13cb59ce981bb2d6362 Mon Sep 17 00:00:00 2001 From: Philipp Burckhardt Date: Fri, 17 May 2024 21:21:44 -0400 Subject: [PATCH 17/18] Apply suggestions from code review Co-authored-by: Athan Signed-off-by: Philipp Burckhardt --- .../base/mskfilter-map/docs/types/index.d.ts | 15 ++++-------- .../base/mskfilter-map/docs/types/test.ts | 24 ++++++++----------- .../base/mskfilter-map/examples/index.js | 2 +- .../array/base/mskfilter-map/lib/index.js | 2 +- .../array/base/mskfilter-map/lib/main.js | 2 +- 5 files changed, 18 insertions(+), 27 deletions(-) diff --git a/lib/node_modules/@stdlib/array/base/mskfilter-map/docs/types/index.d.ts b/lib/node_modules/@stdlib/array/base/mskfilter-map/docs/types/index.d.ts index 14eea089b891..7380b961237a 100644 --- a/lib/node_modules/@stdlib/array/base/mskfilter-map/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/array/base/mskfilter-map/docs/types/index.d.ts @@ -22,11 +22,6 @@ import { Array2D, Collection } from '@stdlib/types/array'; -/** -* Array element indices. -*/ -type Indices = [ number, number ]; - /** * Callback invoked for each array element. * @@ -49,7 +44,7 @@ type Unary = ( this: V, value: T ) => U; * @param indices - current array element indices * @returns result */ -type Binary = ( this: V, value: T, indices: Indices ) => U; +type Binary = ( this: V, value: T, index: number ) => U; /** * Callback invoked for each array element. @@ -59,7 +54,7 @@ type Binary = ( this: V, value: T, indices: Indices ) => U; * @param array - input array * @returns result */ -type Ternary = ( this: V, value: T, indices: Indices, array: Array2D ) => U; +type Ternary = ( this: V, value: T, index: number, array: Array2D ) => U; /** * Callback invoked for each array element. @@ -97,9 +92,9 @@ interface Routine { ( x: Collection, mask: Collection, clbk: Callback, thisArg?: ThisParameterType> ): Array; /** - * Applies a mask and mapping function to an indexed array and assigns results to elements in an indexed output array. + * Applies a mask and mapping function to an input array and assigns results to elements in an output array. * - * @param x - input nested array + * @param x - input array * @param mask - mask array * @param out - output array * @param stride - output array stride @@ -140,7 +135,7 @@ interface Routine { * var x = [ 1, 2, 3, 4 ]; * * function scale( val ) { -* return 10 * val; +* return 10 * val; * } * * var y = mskfilterMap( x, [ 0, 1, 0, 1 ], scale ); diff --git a/lib/node_modules/@stdlib/array/base/mskfilter-map/docs/types/test.ts b/lib/node_modules/@stdlib/array/base/mskfilter-map/docs/types/test.ts index 5a37bf2a54dc..c73506fd7012 100644 --- a/lib/node_modules/@stdlib/array/base/mskfilter-map/docs/types/test.ts +++ b/lib/node_modules/@stdlib/array/base/mskfilter-map/docs/types/test.ts @@ -30,7 +30,7 @@ function clbk( value: number ): number { // TESTS // -// The function returns a nested array... +// The function returns an array... { const x = [ 1.0, 2.0, 3.0, 4.0 ]; @@ -38,12 +38,6 @@ function clbk( value: number ): number { mskfilterMap( x, [ 1, 1, 1, 1 ], clbk, {} ); // $ExpectType number[] } -// The function returns an array... -{ - mskfilterMap( [ 1, 2, 3, 4 ], [ 1, 1, 1, 1 ], clbk ); // $ExpectType number[] - mskfilterMap( [ 1, 2, 3, 4 ], [ 1, 1, 1, 1 ], clbk, {} ); // $ExpectType number[] -} - // The compiler throws an error if the function is provided a first argument which is not an array-like object... { mskfilterMap( 1, [ 0, 0 ], clbk ); // $ExpectError @@ -61,7 +55,7 @@ function clbk( value: number ): number { mskfilterMap( {}, [ 0, 0 ], clbk, {} ); // $ExpectError } -// The compiler throws an error if the function is provided a second argument which is not an array-like object containing numbers... +// The compiler throws an error if the function is provided a second argument which is not an array-like object... { mskfilterMap( [], 1, clbk ); // $ExpectError mskfilterMap( [], true, clbk ); // $ExpectError @@ -109,16 +103,16 @@ function clbk( value: number ): number { mskfilterMap( x, [ 0, 1 ], clbk, {}, {} ); // $ExpectError } -// Attached to the main export is an `assign` method which returns a nested array... +// Attached to the main export is an `assign` method which returns an array... { const x = [ 1.0, 2.0, 3.0, 4.0 ]; const out = [ 0, 0, 0, 0 ]; - mskfilterMap.assign( x, [1, 0, 1, 0], out, 1, 0, clbk ); // $ExpectType number[] - mskfilterMap.assign( x, [1, 0, 1, 0], out, 1, 0, clbk, {} ); // $ExpectType number[] + mskfilterMap.assign( x, [ 1, 0, 1, 0 ], out, 1, 0, clbk ); // $ExpectType number[] + mskfilterMap.assign( x, [ 1, 0, 1, 0 ], out, 1, 0, clbk, {} ); // $ExpectType number[] } -// The compiler throws an error if the `assign` method is provided a first argument which is not a nested array... +// The compiler throws an error if the `assign` method is provided a first argument which is not an array-like object... { const mask = [ 0, 0, 0, 0 ]; @@ -141,7 +135,7 @@ function clbk( value: number ): number { mskfilterMap.assign( ( x: number ): number => x, mask, [ 0, 0, 0, 0 ], 1, 0, clbk, {} ); // $ExpectError } -// The compiler throws an error if the `assign` method is provided a second argument which is not a nested array... +// The compiler throws an error if the `assign` method is provided a second argument which is not an array-like object... { const x = [ 1.0, 2.0, 3.0, 4.0 ]; @@ -266,6 +260,8 @@ function clbk( value: number ): number { mskfilterMap.assign(); // $ExpectError mskfilterMap.assign( x, mask ); // $ExpectError mskfilterMap.assign( x, mask, [ 0, 0, 0, 0 ] ); // $ExpectError - mskfilterMap.assign( x, mask, [ 0, 0, 0, 0 ], clbk, {}, {} ); // $ExpectError + mskfilterMap.assign( x, mask, [ 0, 0, 0, 0 ], 1 ); // $ExpectError + mskfilterMap.assign( x, mask, [ 0, 0, 0, 0 ], 1, 0 ); // $ExpectError + mskfilterMap.assign( x, mask, [ 0, 0, 0, 0 ], 1, 0, clbk, {}, {} ); // $ExpectError } diff --git a/lib/node_modules/@stdlib/array/base/mskfilter-map/examples/index.js b/lib/node_modules/@stdlib/array/base/mskfilter-map/examples/index.js index e110d6169309..adf09533422d 100644 --- a/lib/node_modules/@stdlib/array/base/mskfilter-map/examples/index.js +++ b/lib/node_modules/@stdlib/array/base/mskfilter-map/examples/index.js @@ -33,6 +33,6 @@ var mask = bernoulli( x.length, 0.5, { }); console.log( mask ); -// Filter an array using the mask & apply mapping function: +// Filter an array using the mask: var y = mskfilterMap( x, mask, abs2 ); console.log( y ); diff --git a/lib/node_modules/@stdlib/array/base/mskfilter-map/lib/index.js b/lib/node_modules/@stdlib/array/base/mskfilter-map/lib/index.js index a0c687f1a083..0f1d88aa5e59 100644 --- a/lib/node_modules/@stdlib/array/base/mskfilter-map/lib/index.js +++ b/lib/node_modules/@stdlib/array/base/mskfilter-map/lib/index.js @@ -19,7 +19,7 @@ 'use strict'; /** -* Apply a mask to a provided input array and returns a new array after applying a mapping function. +* Apply a mask to a provided input array and return a new array after applying a mapping function. * * @module @stdlib/array/base/mskfilter-map * diff --git a/lib/node_modules/@stdlib/array/base/mskfilter-map/lib/main.js b/lib/node_modules/@stdlib/array/base/mskfilter-map/lib/main.js index 671eca3a4071..9f860bbaddd2 100644 --- a/lib/node_modules/@stdlib/array/base/mskfilter-map/lib/main.js +++ b/lib/node_modules/@stdlib/array/base/mskfilter-map/lib/main.js @@ -39,7 +39,7 @@ var resolveGetter = require( '@stdlib/array/base/resolve-getter' ); * var mask = [ 0, 1, 0, 1 ]; * * function scale( val ) { -* return 10 * val; +* return 10 * val; * } * * var y = mskfilterMap( x, mask, scale ); From c2aeb93b3bb566e1e392bc51b4d62ce9a5afc05e Mon Sep 17 00:00:00 2001 From: Philipp Burckhardt Date: Fri, 17 May 2024 21:44:12 -0400 Subject: [PATCH 18/18] chore: apply suggestions from code review --- .../array/base/mskfilter-map/README.md | 46 +++++++++---------- .../base/mskfilter-map/docs/types/index.d.ts | 6 +-- .../base/mskfilter-map/docs/types/test.ts | 16 ++----- .../array/base/mskfilter-map/lib/index.js | 4 +- 4 files changed, 31 insertions(+), 41 deletions(-) diff --git a/lib/node_modules/@stdlib/array/base/mskfilter-map/README.md b/lib/node_modules/@stdlib/array/base/mskfilter-map/README.md index 9c0bf3bc52e5..07368993beef 100644 --- a/lib/node_modules/@stdlib/array/base/mskfilter-map/README.md +++ b/lib/node_modules/@stdlib/array/base/mskfilter-map/README.md @@ -55,6 +55,28 @@ The function supports the following parameters: To set the applied function's execution context, provide a `thisArg`. + + +```javascript +function clbk( x ) { + this.count += 1; + return x; +} + +var x = [ 1, 2, 3, 4 ]; +var m = [ 1, 1, 0, 1 ]; + +var ctx = { + 'count': 0 +}; + +var y = mskfilterMap( x, m, clbk, ctx ); +// returns [ 1, 2, 4 ] + +var v = ctx.count; +// returns 3 +``` + The function **always** returns a new "generic" array. #### mskfilterMap.assign( x, mask, out, stride, offset, clbk\[, thisArg] ) @@ -87,30 +109,6 @@ The function supports the following parameters: - **clbk**: function to apply. - **thisArg**: applied function execution context (_optional_). -To set the applied function's execution context, provide a `thisArg`. - - - -```javascript -function clbk( x ) { - this.count += 1; - return x; -} - -var x = [ 1, 2, 3, 4 ]; -var m = [ 1, 1, 0, 1 ]; - -var ctx = { - 'count': 0 -}; - -var y = mskfilterMap( x, m, clbk, ctx ); -// returns [ 1, 2, 4 ] - -var v = ctx.count; -// returns 3 -``` - diff --git a/lib/node_modules/@stdlib/array/base/mskfilter-map/docs/types/index.d.ts b/lib/node_modules/@stdlib/array/base/mskfilter-map/docs/types/index.d.ts index 7380b961237a..bbf5f503d4d4 100644 --- a/lib/node_modules/@stdlib/array/base/mskfilter-map/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/array/base/mskfilter-map/docs/types/index.d.ts @@ -83,13 +83,13 @@ interface Routine { * var x = [ 1, 2, 3, 4 ]; * * function scale( val ) { - * return 10 * val; + * return 10 * val; * } * * var y = mskfilterMap( x, [ 0, 1, 0, 1 ], scale ); * // returns [ 20, 40 ] */ - ( x: Collection, mask: Collection, clbk: Callback, thisArg?: ThisParameterType> ): Array; + ( x: Collection, mask: Collection, clbk: Callback, thisArg?: ThisParameterType> ): Array; /** * Applies a mask and mapping function to an input array and assigns results to elements in an output array. @@ -119,7 +119,7 @@ interface Routine { * var bool = ( arr === out ); * // returns true */ - assign( x: Collection, mask: Collection, out: Collection, stride: number, offset: number, clbk: Callback, thisArg?: ThisParameterType> ): Array; + assign( x: Collection, mask: Collection, out: Collection, stride: number, offset: number, clbk: Callback, thisArg?: ThisParameterType> ): Collection; } /** diff --git a/lib/node_modules/@stdlib/array/base/mskfilter-map/docs/types/test.ts b/lib/node_modules/@stdlib/array/base/mskfilter-map/docs/types/test.ts index c73506fd7012..38f6761d45d0 100644 --- a/lib/node_modules/@stdlib/array/base/mskfilter-map/docs/types/test.ts +++ b/lib/node_modules/@stdlib/array/base/mskfilter-map/docs/types/test.ts @@ -108,8 +108,8 @@ function clbk( value: number ): number { const x = [ 1.0, 2.0, 3.0, 4.0 ]; const out = [ 0, 0, 0, 0 ]; - mskfilterMap.assign( x, [ 1, 0, 1, 0 ], out, 1, 0, clbk ); // $ExpectType number[] - mskfilterMap.assign( x, [ 1, 0, 1, 0 ], out, 1, 0, clbk, {} ); // $ExpectType number[] + mskfilterMap.assign( x, [ 1, 0, 1, 0 ], out, 1, 0, clbk ); // $ExpectType Collection + mskfilterMap.assign( x, [ 1, 0, 1, 0 ], out, 1, 0, clbk, {} ); // $ExpectType Collection } // The compiler throws an error if the `assign` method is provided a first argument which is not an array-like object... @@ -139,45 +139,37 @@ function clbk( value: number ): number { { const x = [ 1.0, 2.0, 3.0, 4.0 ]; - mskfilterMap.assign( x, 'abc', [ 0, 0, 0, 0 ], 1, 0, clbk ); // $ExpectError mskfilterMap.assign( x, 3.14, [ 0, 0, 0, 0 ], 1, 0, clbk ); // $ExpectError mskfilterMap.assign( x, true, [ 0, 0, 0, 0 ], 1, 0, clbk ); // $ExpectError mskfilterMap.assign( x, false, [ 0, 0, 0, 0 ], 1, 0, clbk ); // $ExpectError mskfilterMap.assign( x, null, [ 0, 0, 0, 0 ], 1, 0, clbk ); // $ExpectError - mskfilterMap.assign( x, [ '1' ], [ 0, 0, 0, 0 ], 1, 0, clbk ); // $ExpectError mskfilterMap.assign( x, {}, [ 0, 0, 0, 0 ], 1, 0, clbk ); // $ExpectError mskfilterMap.assign( x, ( x: number ): number => x, [ 0, 0, 0, 0 ], 1, 0, clbk ); // $ExpectError - mskfilterMap.assign( x, 'abc', [ 0, 0, 0, 0 ], 1, 0, clbk, {} ); // $ExpectError mskfilterMap.assign( x, 3.14, [ 0, 0, 0, 0 ], 1, 0, clbk, {} ); // $ExpectError mskfilterMap.assign( x, true, [ 0, 0, 0, 0 ], 1, 0, clbk, {} ); // $ExpectError mskfilterMap.assign( x, false, [ 0, 0, 0, 0 ], 1, 0, clbk, {} ); // $ExpectError mskfilterMap.assign( x, null, [ 0, 0, 0, 0 ], 1, 0, clbk, {} ); // $ExpectError - mskfilterMap.assign( x, [ '1' ], [ 0, 0, 0, 0 ], 1, 0, clbk, {} ); // $ExpectError mskfilterMap.assign( x, {}, [ 0, 0, 0, 0 ], 1, 0, clbk, {} ); // $ExpectError mskfilterMap.assign( x, ( x: number ): number => x, [ 0, 0, 0, 0 ], 1, 0, clbk, {} ); // $ExpectError } -// The compiler throws an error if the `assign` method is provided a third argument which is not an array of numbers... +// The compiler throws an error if the `assign` method is provided a third argument which is not an array-like object... { const x = [ 1.0, 2.0, 3.0, 4.0 ]; const mask = [ 0.0, 0.0, 0.0, 0.0 ]; - mskfilterMap.assign( x, mask, 'abc', 1, 0, clbk ); // $ExpectError mskfilterMap.assign( x, mask, 3.14, 1, 0, clbk ); // $ExpectError mskfilterMap.assign( x, mask, true, 1, 0, clbk ); // $ExpectError mskfilterMap.assign( x, mask, false, 1, 0, clbk ); // $ExpectError mskfilterMap.assign( x, mask, null, 1, 0, clbk ); // $ExpectError - mskfilterMap.assign( x, mask, [ '1' ], 1, 0, clbk ); // $ExpectError mskfilterMap.assign( x, mask, {}, 1, 0, clbk ); // $ExpectError mskfilterMap.assign( x, mask, ( x: number ): number => x, 1, 0, clbk ); // $ExpectError - mskfilterMap.assign( x, mask, 'abc', 1, 0, clbk, {} ); // $ExpectError mskfilterMap.assign( x, mask, 3.14, 1, 0, clbk, {} ); // $ExpectError mskfilterMap.assign( x, mask, true, 1, 0, clbk, {} ); // $ExpectError mskfilterMap.assign( x, mask, false, 1, 0, clbk, {} ); // $ExpectError mskfilterMap.assign( x, mask, null, 1, 0, clbk, {} ); // $ExpectError - mskfilterMap.assign( x, mask, [ '1' ], 1, 0, clbk, {} ); // $ExpectError mskfilterMap.assign( x, mask, {}, 1, 0, clbk, {} ); // $ExpectError mskfilterMap.assign( x, mask, ( x: number ): number => x, 1, 0, clbk, {} ); // $ExpectError } @@ -206,7 +198,7 @@ function clbk( value: number ): number { mskfilterMap.assign( x, mask, [ 0, 0, 0, 0 ], ( x: number ): number => x, 0, clbk, {} ); // $ExpectError } -// The compiler throws an error if the `assign` method is provided a fifth argument which is not an array of numbers... +// The compiler throws an error if the `assign` method is provided a fifth argument which is not a number... { const x = [ 1.0, 2.0, 3.0, 4.0 ]; const mask = [ 0.0, 0.0, 0.0, 0.0 ]; diff --git a/lib/node_modules/@stdlib/array/base/mskfilter-map/lib/index.js b/lib/node_modules/@stdlib/array/base/mskfilter-map/lib/index.js index 0f1d88aa5e59..c131c816d6e9 100644 --- a/lib/node_modules/@stdlib/array/base/mskfilter-map/lib/index.js +++ b/lib/node_modules/@stdlib/array/base/mskfilter-map/lib/index.js @@ -30,7 +30,7 @@ * var mask = [ 0, 1, 0, 1 ]; * * function scale( val ) { -* return 10 * val; +* return 10 * val; * } * * var y = mskfilterMap( x, mask, scale ); @@ -43,7 +43,7 @@ * var mask = [ 0, 1, 0, 1 ]; * * function scale( val ) { -* return 10 * val; +* return 10 * val; * } * * var out = [ 0, 0 ];