From a561bffc90a32d32ddb4781f288864d354bd13c5 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Sat, 18 May 2024 01:50:54 +0000 Subject: [PATCH] Auto-generated commit --- base/mskfilter-map/README.md | 171 +++++++++ .../benchmark/benchmark.assign.length.js | 106 ++++++ base/mskfilter-map/benchmark/benchmark.js | 56 +++ .../benchmark/benchmark.length.js | 98 ++++++ base/mskfilter-map/docs/repl.txt | 84 +++++ base/mskfilter-map/docs/types/index.d.ts | 166 +++++++++ base/mskfilter-map/docs/types/test.ts | 259 ++++++++++++++ base/mskfilter-map/examples/index.js | 38 ++ base/mskfilter-map/lib/assign.js | 182 ++++++++++ base/mskfilter-map/lib/index.js | 71 ++++ base/mskfilter-map/lib/main.js | 71 ++++ base/mskfilter-map/package.json | 66 ++++ base/mskfilter-map/test/test.assign.js | 325 ++++++++++++++++++ base/mskfilter-map/test/test.js | 41 +++ base/mskfilter-map/test/test.main.js | 158 +++++++++ 15 files changed, 1892 insertions(+) create mode 100644 base/mskfilter-map/README.md create mode 100644 base/mskfilter-map/benchmark/benchmark.assign.length.js create mode 100644 base/mskfilter-map/benchmark/benchmark.js create mode 100644 base/mskfilter-map/benchmark/benchmark.length.js create mode 100644 base/mskfilter-map/docs/repl.txt create mode 100644 base/mskfilter-map/docs/types/index.d.ts create mode 100644 base/mskfilter-map/docs/types/test.ts create mode 100644 base/mskfilter-map/examples/index.js create mode 100644 base/mskfilter-map/lib/assign.js create mode 100644 base/mskfilter-map/lib/index.js create mode 100644 base/mskfilter-map/lib/main.js create mode 100644 base/mskfilter-map/package.json create mode 100644 base/mskfilter-map/test/test.assign.js create mode 100644 base/mskfilter-map/test/test.js create mode 100644 base/mskfilter-map/test/test.main.js diff --git a/base/mskfilter-map/README.md b/base/mskfilter-map/README.md new file mode 100644 index 00000000..07368993 --- /dev/null +++ b/base/mskfilter-map/README.md @@ -0,0 +1,171 @@ + + +# mskfilterMap + +> 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] ) + +Applies a mask to a provided input array and returns a new array after applying a mapping function. + +```javascript +function clbk( value ) { + return value * 2; +} + +var x = [ 1, 2, 3, 4 ]; +var m = [ 0, 1, 0, 1 ]; + +var y = mskfilterMap( x, m, clbk ); +// 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`. + + + +```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] ) + +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 m = [ 0, 1, 0, 1 ]; + +var out = [ 0, 0, 0, 0 ]; +var arr = mskfilterMap.assign( x, m, 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 abs2 = require( '@stdlib/math/base/special/abs2' ); +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 ); + +// Filter an array using the mask: +var y = mskfilterMap( x, mask, abs2 ); +console.log( y ); +``` + +
+ + + + + + + + + + + + + + diff --git a/base/mskfilter-map/benchmark/benchmark.assign.length.js b/base/mskfilter-map/benchmark/benchmark.assign.length.js new file mode 100644 index 00000000..9f771198 --- /dev/null +++ b/base/mskfilter-map/benchmark/benchmark.assign.length.js @@ -0,0 +1,106 @@ +/** +* @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( './../../../base/zero-to' ); +var zeros = require( './../../../zeros' ); +var ones = require( './../../../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' ); + + +// 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; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = mskfilterMap.assign( x, mask, out, 1, 0, identity ); + 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/base/mskfilter-map/benchmark/benchmark.js b/base/mskfilter-map/benchmark/benchmark.js new file mode 100644 index 00000000..099400c8 --- /dev/null +++ b/base/mskfilter-map/benchmark/benchmark.js @@ -0,0 +1,56 @@ +/** +* @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( './../../../base/zero-to' ); +var ones = require( './../../../base/ones' ); +var identity = require( '@stdlib/math/base/special/identity' ); +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 ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = mskfilterMap( x, y, identity ); + 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/base/mskfilter-map/benchmark/benchmark.length.js b/base/mskfilter-map/benchmark/benchmark.length.js new file mode 100644 index 00000000..2bf970ff --- /dev/null +++ b/base/mskfilter-map/benchmark/benchmark.length.js @@ -0,0 +1,98 @@ +/** +* @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( './../../../base/zero-to' ); +var ones = require( './../../../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' ); + + +// 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; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = mskfilterMap( x, y, identity ); + 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/base/mskfilter-map/docs/repl.txt b/base/mskfilter-map/docs/repl.txt new file mode 100644 index 00000000..5ab7835f --- /dev/null +++ b/base/mskfilter-map/docs/repl.txt @@ -0,0 +1,84 @@ + +{{alias}}( x, mask, fcn[, thisArg] ) + 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` + 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 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` + 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/base/mskfilter-map/docs/types/index.d.ts b/base/mskfilter-map/docs/types/index.d.ts new file mode 100644 index 00000000..bbf5f503 --- /dev/null +++ b/base/mskfilter-map/docs/types/index.d.ts @@ -0,0 +1,166 @@ +/* +* @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 { Array2D, Collection } from '@stdlib/types/array'; + +/** +* 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, index: number ) => 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, index: number, 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 input array and assigns results to elements in an output array. + * + * @param x - input 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> ): Collection; +} + +/** +* 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 ] +* +* @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 var mskfilterMap: Routine; + + +// EXPORTS // + +export = mskfilterMap; + diff --git a/base/mskfilter-map/docs/types/test.ts b/base/mskfilter-map/docs/types/test.ts new file mode 100644 index 00000000..38f6761d --- /dev/null +++ b/base/mskfilter-map/docs/types/test.ts @@ -0,0 +1,259 @@ +/* +* @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' ); + +/** +* Unary function. +* +* @param value - input value +* @returns result +*/ +function clbk( value: number ): number { + return value; +} + +// TESTS // + +// The function returns an 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 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... +{ + 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, [ 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... +{ + const x = [ 1.0, 2.0 ]; + + mskfilterMap(); // $ExpectError + mskfilterMap( x ); // $ExpectError + mskfilterMap( x, [ 0, 1 ] ); // $ExpectError + mskfilterMap( x, [ 0, 1 ], clbk, {}, {} ); // $ExpectError +} + +// 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 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... +{ + 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 an array-like object... +{ + const x = [ 1.0, 2.0, 3.0, 4.0 ]; + + 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, {}, [ 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, 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, {}, [ 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-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, 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, 0, clbk ); // $ExpectError + mskfilterMap.assign( x, mask, ( x: number ): number => x, 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, 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 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 ], 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 ], 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/base/mskfilter-map/examples/index.js b/base/mskfilter-map/examples/index.js new file mode 100644 index 00000000..a4beb442 --- /dev/null +++ b/base/mskfilter-map/examples/index.js @@ -0,0 +1,38 @@ +/** +* @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( './../../../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: +var x = zeroTo( 20 ); +console.log( x ); + +// Generate a random mask: +var mask = bernoulli( x.length, 0.5, { + 'dtype': 'generic' +}); +console.log( mask ); + +// Filter an array using the mask: +var y = mskfilterMap( x, mask, abs2 ); +console.log( y ); diff --git a/base/mskfilter-map/lib/assign.js b/base/mskfilter-map/lib/assign.js new file mode 100644 index 00000000..18ebd155 --- /dev/null +++ b/base/mskfilter-map/lib/assign.js @@ -0,0 +1,182 @@ +/** +* @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 arraylike2object = require( './../../../base/arraylike2object' ); + + +// FUNCTIONS // + +/** +* 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 +* @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 scale( x ) { +* return x * 10; +* } +* +* var arr = indexed( x, mask, out, 1, 0, scale ); +* // 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 ], i, x ); + io += stride; + } + } + return out; +} + +/** +* 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 +* @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 scale( x ) { +* return x * 10; +* } +* +* var out = toAccessorArray( [ 0, 0, 0, 0 ] ); +* var arr = accessors( arraylike2object( x ), arraylike2object( mask ), arraylike2object( out ), 1, 0, scale ); +* +* 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 ), i, xdata ) ); + io += stride; + } + } + return odata; +} + + +// MAIN // + +/** +* 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 +* @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 scale( x ) { +* return x * 10; +* } +* +* var out = [ 0, 0 ]; +* var arr = assign( x, mask, out, 1, 0, scale ); +* // 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 + ) { + 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/base/mskfilter-map/lib/index.js b/base/mskfilter-map/lib/index.js new file mode 100644 index 00000000..c131c816 --- /dev/null +++ b/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 and return a new array after applying a mapping function. +* +* @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 scale( val ) { +* return 10 * val; +* } +* +* var y = mskfilterMap( x, mask, scale ); +* // returns [ 20, 40 ] +* +* @example +* var mskfilterMap = require( '@stdlib/array/base/mskfilter-map' ); +* +* var x = [ 1, 2, 3, 4 ]; +* var mask = [ 0, 1, 0, 1 ]; +* +* function scale( val ) { +* return 10 * val; +* } +* +* var out = [ 0, 0 ]; +* var arr = mskfilterMap.assign( x, mask, out, 1, 0, scale ); +* // returns [ 20, 40 ] +* +* 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/base/mskfilter-map/lib/main.js b/base/mskfilter-map/lib/main.js new file mode 100644 index 00000000..71bc7aef --- /dev/null +++ b/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( './../../../base/resolve-getter' ); + + +// MAIN // + +/** +* 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 +* @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 scale( val ) { +* return 10 * val; +* } +* +* var y = mskfilterMap( x, mask, scale ); +* // returns [ 20, 40 ] +*/ +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 ), i, x ) ); // use `Array#push` to ensure "fast" elements + } + } + return out; +} + + +// EXPORTS // + +module.exports = mskfilterMap; diff --git a/base/mskfilter-map/package.json b/base/mskfilter-map/package.json new file mode 100644 index 00000000..c0b047cd --- /dev/null +++ b/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/base/mskfilter-map/test/test.assign.js b/base/mskfilter-map/test/test.assign.js new file mode 100644 index 00000000..5c75d535 --- /dev/null +++ b/base/mskfilter-map/test/test.assign.js @@ -0,0 +1,325 @@ +/** +* @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( './../../../complex64' ); +var toAccessorArray = require( './../../../base/to-accessor-array' ); +var reinterpret = require( '@stdlib/strided/base/reinterpret-complex' ); +var Int32Array = require( './../../../int32' ); +var zeros = require( './../../../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 = reinterpret(new Complex64Array( [ 1.0, 2.0, 3.0, 4.0 ] ) ); + + mask = [ 1, 0 ]; + out = reinterpret( zeros( 1, 'complex64' ) ); + actual = mskfilterMap( x, mask, out, 1, 0, clbk ); + expected = reinterpret( new Complex64Array( [ 1.0, 4.0, 25.0, 36.0 ] ) ); + + t.strictEqual( actual, out, 'returns expected value' ); + isEqual( actual, expected ); + + mask = [ 0, 0 ]; + out = reinterpret( zeros( 0, 'complex64' ) ); + actual = mskfilterMap( x, mask, out, 1, 0, clbk ); + expected = reinterpret( new Complex64Array( [] ) ); + + t.strictEqual( actual, out, 'returns expected value' ); + isEqual( actual, expected ); + + mask = [ 0, 1 ]; + out = reinterpret( zeros( 1, 'complex64' ) ); + actual = mskfilterMap( x, mask, out, 1, 0, clbk ); + expected = reinterpret( new Complex64Array( [ 9.0, 16.0 ] ) ); + + t.strictEqual( actual, out, 'returns expected value' ); + isEqual( actual, expected ); + + mask = [ 1, 1 ]; + out = reinterpret( zeros( 3, 'complex64' ) ); + actual = mskfilterMap( x, mask, out, 1, 0, clbk ); + expected = reinterpret( new Complex64Array( [ 1.0, 4.0, 9.0, 16.0 ] ) ); + + t.strictEqual( actual, out, 'returns expected value' ); + isEqual( actual, expected ); + + mask = [ 1, 0 ]; + out = reinterpret( zeros( 3, 'complex64' ) ); + actual = mskfilterMap( x, mask, out, -2, out.length-1, clbk ); + expected = reinterpret( new Complex64Array( [ 0.0, 0.0, 1.0, 4.0 ] ) ); + + 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 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 having all element equal to zero ', 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' ); + + 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 ); + 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; + } +}); diff --git a/base/mskfilter-map/test/test.js b/base/mskfilter-map/test/test.js new file mode 100644 index 00000000..028b9f7e --- /dev/null +++ b/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/base/mskfilter-map/test/test.main.js b/base/mskfilter-map/test/test.main.js new file mode 100644 index 00000000..18c18371 --- /dev/null +++ b/base/mskfilter-map/test/test.main.js @@ -0,0 +1,158 @@ +/** +* @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( './../../../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 = [ 4, 16 ]; + 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 = [ 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, 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' ); + + 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 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 * 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; + } +});