Skip to content

Commit 8dfa8c0

Browse files
committed
feat: add array/base/mskfiltern
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: passed - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed ---
1 parent 19c85a5 commit 8dfa8c0

File tree

12 files changed

+1032
-0
lines changed

12 files changed

+1032
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2025 The Stdlib Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
21+
# mskfiltern
22+
23+
> Apply a mask to one or more provided input arrays in a single pass.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var mskfiltern = require( '@stdlib/array/base/mskfiltern' );
31+
```
32+
33+
#### mskfiltern( x, \[...arrays,] mask )
34+
35+
Returns new arrays by applying a mask to one or more provided input arrays in a single pass.
36+
37+
```javascript
38+
var x = [ 1, 2, 3, 4 ];
39+
var idx = [ 0, 1, 2, 3 ];
40+
41+
var out = mskfiltern( x, idx, [ 0, 1, 0, 1 ] );
42+
// returns [ [ 2, 4 ], [ 1, 3 ] ]
43+
```
44+
45+
The function supports the following parameters:
46+
47+
- **x**: first input array.
48+
- **...arrays**: additional input arrays.
49+
- **mask**: mask array.
50+
51+
The function **always** returns new "generic" arrays.
52+
53+
</section>
54+
55+
<!-- /.usage -->
56+
57+
<section class="notes">
58+
59+
## Notes
60+
61+
- If a `mask` array element is truthy, the corresponding elements in the input arrays are **included** in the respective output arrays; otherwise, the corresponding elements in the input arrays are "masked" and thus **excluded** from the respective output arrays.
62+
63+
</section>
64+
65+
<!-- /.notes -->
66+
67+
<section class="examples">
68+
69+
## Examples
70+
71+
<!-- eslint no-undef: "error" -->
72+
73+
```javascript
74+
var zeroTo = require( '@stdlib/array/base/zero-to' );
75+
var bernoulli = require( '@stdlib/random/array/bernoulli' );
76+
var mskfiltern = require( '@stdlib/array/base/mskfiltern' );
77+
78+
// Generate linearly spaced arrays:
79+
var x = zeroTo( 20 );
80+
console.log( x );
81+
82+
var idx = zeroTo( x.length );
83+
console.log( idx );
84+
85+
// Generate a random mask:
86+
var mask = bernoulli( x.length, 0.5, {
87+
'dtype': 'generic'
88+
});
89+
console.log( mask );
90+
91+
// Filter both arrays using the mask:
92+
var out = mskfiltern( x, idx, mask );
93+
console.log( out );
94+
```
95+
96+
</section>
97+
98+
<!-- /.examples -->
99+
100+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
101+
102+
<section class="related">
103+
104+
</section>
105+
106+
<!-- /.related -->
107+
108+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
109+
110+
<section class="links">
111+
112+
</section>
113+
114+
<!-- /.links -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var isArrayArray = require( '@stdlib/assert/is-array-array' );
25+
var zeroTo = require( '@stdlib/array/base/zero-to' );
26+
var ones = require( '@stdlib/array/base/ones' );
27+
var pkg = require( './../package.json' ).name;
28+
var mskfiltern = require( './../lib' );
29+
30+
31+
// MAIN //
32+
33+
bench( pkg+'::copy:num_arrays=2,len=100', function benchmark( b ) {
34+
var x;
35+
var y;
36+
var i;
37+
var v;
38+
39+
x = zeroTo( 100 );
40+
y = ones( x.length );
41+
42+
b.tic();
43+
for ( i = 0; i < b.iterations; i++ ) {
44+
v = mskfiltern( x, x, y );
45+
if ( typeof v !== 'object' ) {
46+
b.fail( 'should return an array' );
47+
}
48+
}
49+
b.toc();
50+
if ( !isArrayArray( v ) ) {
51+
b.fail( 'should return an array of arrays' );
52+
}
53+
b.pass( 'benchmark finished' );
54+
b.end();
55+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var pow = require( '@stdlib/math/base/special/pow' );
25+
var zeroTo = require( '@stdlib/array/base/zero-to' );
26+
var ones = require( '@stdlib/array/base/ones' );
27+
var isArrayArray = require( '@stdlib/assert/is-array-array' );
28+
var pkg = require( './../package.json' ).name;
29+
var mskfiltern = require( './../lib' );
30+
31+
32+
// FUNCTIONS //
33+
34+
/**
35+
* Creates a benchmark function.
36+
*
37+
* @private
38+
* @param {PositiveInteger} len - array length
39+
* @returns {Function} benchmark function
40+
*/
41+
function createBenchmark( len ) {
42+
var x = zeroTo( len );
43+
var y = ones( len );
44+
return benchmark;
45+
46+
/**
47+
* Benchmark function.
48+
*
49+
* @private
50+
* @param {Benchmark} b - benchmark instance
51+
*/
52+
function benchmark( b ) {
53+
var v;
54+
var i;
55+
56+
b.tic();
57+
for ( i = 0; i < b.iterations; i++ ) {
58+
v = mskfiltern( x, x, y );
59+
if ( typeof v !== 'object' ) {
60+
b.fail( 'should return an array' );
61+
}
62+
}
63+
b.toc();
64+
if ( !isArrayArray( v ) ) {
65+
b.fail( 'should return an array of arrays' );
66+
}
67+
b.pass( 'benchmark finished' );
68+
b.end();
69+
}
70+
}
71+
72+
73+
// MAIN //
74+
75+
/**
76+
* Main execution sequence.
77+
*
78+
* @private
79+
*/
80+
function main() {
81+
var len;
82+
var min;
83+
var max;
84+
var f;
85+
var i;
86+
87+
min = 1; // 10^min
88+
max = 6; // 10^max
89+
90+
for ( i = min; i <= max; i++ ) {
91+
len = pow( 10, i );
92+
f = createBenchmark( len );
93+
bench( pkg+':num_arrays=2,len='+len, f );
94+
}
95+
}
96+
97+
main();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
2+
{{alias}}( x[, ...arrays], mask )
3+
Returns new arrays by applying a mask to one or more provided input arrays
4+
in a single pass.
5+
6+
If a mask array element is truthy, the corresponding elements in the input
7+
arrays are included in the respective output arrays; otherwise, the
8+
corresponding elements in the input arrays are "masked" and thus excluded
9+
from the output arrays.
10+
11+
Parameters
12+
----------
13+
x: ArrayLikeObject
14+
First input array.
15+
16+
arrays: ...ArrayLikeObject (optional)
17+
Additional input arrays.
18+
19+
mask: ArrayLikeObject
20+
Mask array.
21+
22+
Returns
23+
-------
24+
out: Array<Array>
25+
Output arrays.
26+
27+
Examples
28+
--------
29+
> var x = [ 1, 2, 3, 4 ];
30+
> var idx = [ 0, 1, 2, 3 ];
31+
> var out = {{alias}}( x, idx, [ 0, 1, 0, 1 ] )
32+
[ [ 2, 4 ], [ 1, 3 ] ]
33+
34+
See Also
35+
--------
36+

0 commit comments

Comments
 (0)