Skip to content

Commit 23e158b

Browse files
AuenKrkgrytePlaneshifter
authored
feat: add array/base/mskfilter-map
PR-URL: #1705 Closes: #1322 --------- Signed-off-by: Golden <[email protected]> Signed-off-by: Athan Reines <[email protected]> Signed-off-by: Philipp Burckhardt <[email protected]> Co-authored-by: Athan Reines <[email protected]> Co-authored-by: Philipp Burckhardt <[email protected]> Reviewed-by: Athan Reines <[email protected]> Reviewed-by: Philipp Burckhardt <[email protected]>
1 parent 09618b7 commit 23e158b

File tree

15 files changed

+1892
-0
lines changed

15 files changed

+1892
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2024 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+
# mskfilterMap
22+
23+
> Apply a mask to a provided input array and return a new array after applying a mapping function.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var mskfilterMap = require( '@stdlib/array/base/mskfilter-map' );
31+
```
32+
33+
#### mskfilterMap( x, mask, clbk\[, thisArg] )
34+
35+
Applies a mask to a provided input array and returns a new array after applying a mapping function.
36+
37+
```javascript
38+
function clbk( value ) {
39+
return value * 2;
40+
}
41+
42+
var x = [ 1, 2, 3, 4 ];
43+
var m = [ 0, 1, 0, 1 ];
44+
45+
var y = mskfilterMap( x, m, clbk );
46+
// returns [ 4, 8 ]
47+
```
48+
49+
The function supports the following parameters:
50+
51+
- **x**: input array.
52+
- **mask**: mask array.
53+
- **clbk**: function to apply.
54+
- **thisArg**: applied function execution context (_optional_).
55+
56+
To set the applied function's execution context, provide a `thisArg`.
57+
58+
<!-- eslint-disable no-invalid-this -->
59+
60+
```javascript
61+
function clbk( x ) {
62+
this.count += 1;
63+
return x;
64+
}
65+
66+
var x = [ 1, 2, 3, 4 ];
67+
var m = [ 1, 1, 0, 1 ];
68+
69+
var ctx = {
70+
'count': 0
71+
};
72+
73+
var y = mskfilterMap( x, m, clbk, ctx );
74+
// returns [ 1, 2, 4 ]
75+
76+
var v = ctx.count;
77+
// returns 3
78+
```
79+
80+
The function **always** returns a new "generic" array.
81+
82+
#### mskfilterMap.assign( x, mask, out, stride, offset, clbk\[, thisArg] )
83+
84+
Applies a mask and mapping function to a provided input array and assigns results to elements in a provided output array.
85+
86+
```javascript
87+
function clbk( value ) {
88+
return value * 2;
89+
}
90+
91+
var x = [ 1, 2, 3, 4 ];
92+
var m = [ 0, 1, 0, 1 ];
93+
94+
var out = [ 0, 0, 0, 0 ];
95+
var arr = mskfilterMap.assign( x, m, out, -2, out.length-1, clbk );
96+
// returns [ 0, 8, 0, 4 ]
97+
98+
var bool = ( arr === out );
99+
// returns true
100+
```
101+
102+
The function supports the following parameters:
103+
104+
- **x**: input array.
105+
- **mask**: mask array.
106+
- **out**: output array.
107+
- **stride**: output array stride.
108+
- **offset**: output array offset.
109+
- **clbk**: function to apply.
110+
- **thisArg**: applied function execution context (_optional_).
111+
112+
</section>
113+
114+
<!-- /.usage -->
115+
116+
<section class="notes">
117+
118+
## Notes
119+
120+
- 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.
121+
122+
</section>
123+
124+
<!-- /.notes -->
125+
126+
<section class="examples">
127+
128+
## Examples
129+
130+
<!-- eslint no-undef: "error" -->
131+
132+
```javascript
133+
var zeroTo = require( '@stdlib/array/base/zero-to' );
134+
var bernoulli = require( '@stdlib/random/array/bernoulli' );
135+
var abs2 = require( '@stdlib/math/base/special/abs2' );
136+
var mskfilterMap = require( '@stdlib/array/base/mskfilter-map' );
137+
138+
// Generate a linearly spaced array:
139+
var x = zeroTo( 20 );
140+
console.log( x );
141+
142+
// Generate a random mask:
143+
var mask = bernoulli( x.length, 0.5, {
144+
'dtype': 'generic'
145+
});
146+
console.log( mask );
147+
148+
// Filter an array using the mask:
149+
var y = mskfilterMap( x, mask, abs2 );
150+
console.log( y );
151+
```
152+
153+
</section>
154+
155+
<!-- /.examples -->
156+
157+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
158+
159+
<section class="related">
160+
161+
</section>
162+
163+
<!-- /.related -->
164+
165+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
166+
167+
<section class="links">
168+
169+
</section>
170+
171+
<!-- /.links -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 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 zeros = require( '@stdlib/array/zeros' );
27+
var ones = require( '@stdlib/array/ones' );
28+
var isArray = require( '@stdlib/assert/is-array' );
29+
var isnan = require( '@stdlib/assert/is-nan' ).isPrimitive;
30+
var identity = require( '@stdlib/math/base/special/identity' );
31+
var pkg = require( './../package.json' ).name;
32+
var mskfilterMap = require( './../lib' );
33+
34+
35+
// FUNCTIONS //
36+
37+
/**
38+
* Creates a benchmark function.
39+
*
40+
* @private
41+
* @param {PositiveInteger} len - array length
42+
* @returns {Function} benchmark function
43+
*/
44+
function createBenchmark( len ) {
45+
var mask;
46+
var out;
47+
var x;
48+
49+
x = zeroTo( len, 'generic' );
50+
mask = ones( len, 'generic' );
51+
out = zeros( len, 'generic' );
52+
53+
return benchmark;
54+
55+
/**
56+
* Benchmark function.
57+
*
58+
* @private
59+
* @param {Benchmark} b - benchmark instance
60+
*/
61+
function benchmark( b ) {
62+
var v;
63+
var i;
64+
65+
b.tic();
66+
for ( i = 0; i < b.iterations; i++ ) {
67+
v = mskfilterMap.assign( x, mask, out, 1, 0, identity );
68+
if ( typeof v !== 'object' ) {
69+
b.fail( 'should return an array' );
70+
}
71+
}
72+
b.toc();
73+
if ( !isArray( v ) || isnan( v[ i%len ] ) ) {
74+
b.fail( 'should return an array' );
75+
}
76+
b.pass( 'benchmark finished' );
77+
b.end();
78+
}
79+
}
80+
81+
82+
// MAIN //
83+
84+
/**
85+
* Main execution sequence.
86+
*
87+
* @private
88+
*/
89+
function main() {
90+
var len;
91+
var min;
92+
var max;
93+
var f;
94+
var i;
95+
96+
min = 1; // 10^min
97+
max = 6; // 10^max
98+
99+
for ( i = min; i <= max; i++ ) {
100+
len = pow( 10, i );
101+
f = createBenchmark( len );
102+
bench( pkg+':assign:len='+len, f );
103+
}
104+
}
105+
106+
main();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 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 isArray = require( '@stdlib/assert/is-array' );
25+
var zeroTo = require( '@stdlib/array/base/zero-to' );
26+
var ones = require( '@stdlib/array/base/ones' );
27+
var identity = require( '@stdlib/math/base/special/identity' );
28+
var pkg = require( './../package.json' ).name;
29+
var mskfilterMap = require( './../lib' );
30+
31+
32+
// MAIN //
33+
34+
bench( pkg+'::copy:len=100', function benchmark( b ) {
35+
var x;
36+
var y;
37+
var i;
38+
var v;
39+
40+
x = zeroTo( 100 );
41+
y = ones( x.length );
42+
43+
b.tic();
44+
for ( i = 0; i < b.iterations; i++ ) {
45+
v = mskfilterMap( x, y, identity );
46+
if ( typeof v !== 'object' ) {
47+
b.fail( 'should return an array' );
48+
}
49+
}
50+
b.toc();
51+
if ( !isArray( v ) ) {
52+
b.fail( 'should return an array' );
53+
}
54+
b.pass( 'benchmark finished' );
55+
b.end();
56+
});

0 commit comments

Comments
 (0)