Skip to content

Commit 201ce11

Browse files
committedJun 21, 2024
feat: add array/mskput
1 parent 3eb5c20 commit 201ce11

File tree

14 files changed

+1982
-0
lines changed

14 files changed

+1982
-0
lines changed
 
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
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+
# mskput
22+
23+
> Replace elements of an array with provided values according to a provided mask array.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var mskput = require( '@stdlib/array/mskput' );
31+
```
32+
33+
#### mskput( x, mask, values\[, options] )
34+
35+
Replaces elements of an array with provided values according to a provided mask array.
36+
37+
```javascript
38+
var x = [ 1, 2, 3, 4 ];
39+
40+
var out = mskput( x, [ 1, 0, 1, 0 ], [ 20, 40 ] );
41+
// returns [ 1, 20, 3, 40 ]
42+
43+
var bool = ( out === x );
44+
// returns true
45+
```
46+
47+
The function supports the following parameters:
48+
49+
- **x**: input array.
50+
- **mask**: mask array.
51+
- **values**: values to set.
52+
- **options**: function options.
53+
54+
The function supports the following options:
55+
56+
- **mode**: string specifying behavior when the number of `values` does not equal the number of falsy `mask` values. Default: `'repeat'`.
57+
58+
The function supports the following modes:
59+
60+
- `'strict'`: specifies that the function must raise an exception when the number of `values` does not **exactly** equal the number of falsy `mask` values.
61+
- `'non_strict'`: specifies that the function must raise an exception when the function is provided insufficient `values` to satisfy the `mask` array.
62+
- `'strict_broadcast'`: specifies that the function must broadcast a single-element `values` array and otherwise raise an exception when the number of `values` does not **exactly** equal the number of falsy `mask` values.
63+
- `'broadcast'`: specifies that the function must broadcast a single-element `values` array and otherwise raise an exception when the function is provided insufficient `values` to satisfy the `mask` array.
64+
- `'repeat'`: specifies that the function must reuse provided `values` when replacing elements in `x` in order to satisfy the `mask` array.
65+
66+
In broadcasting modes, the function supports broadcasting a `values` array containing a single element against the number of falsy values in the `mask` array.
67+
68+
```javascript
69+
var x = [ 1, 2, 3, 4 ];
70+
71+
var out = mskput( x, [ 1, 0, 1, 0 ], [ 20 ], {
72+
'mode': 'strict_broadcast'
73+
});
74+
// returns [ 1, 20, 3, 20 ]
75+
76+
var bool = ( out === x );
77+
// returns true
78+
```
79+
80+
In repeat mode, the function supports recycling elements in a `values` array to satisfy the number of falsy values in the `mask` array.
81+
82+
```javascript
83+
var x = [ 1, 2, 3, 4 ];
84+
85+
var out = mskput( x, [ 0, 0, 1, 0 ], [ 20, 40 ], {
86+
'mode': 'repeat'
87+
});
88+
// returns [ 20, 40, 3, 20 ]
89+
90+
var bool = ( out === x );
91+
// returns true
92+
```
93+
94+
</section>
95+
96+
<!-- /.usage -->
97+
98+
<section class="notes">
99+
100+
## Notes
101+
102+
- The function mutates the input array `x`.
103+
- If a `mask` array element is falsy, the corresponding element in `x` is **replaced**; otherwise, the corresponding element in `x` is "masked" and thus left unchanged.
104+
- The `values` array must have a [data type][@stdlib/array/dtypes] which can be [safely cast][@stdlib/array/safe-casts] to the input array data type. Floating-point data types (both real and complex) are allowed to downcast to a lower precision data type of the [same kind][@stdlib/array/same-kind-casts] (e.g., element values from a `'float64'` values array can be assigned to corresponding elements in a `'float32'` input array).
105+
106+
</section>
107+
108+
<!-- /.notes -->
109+
110+
<section class="examples">
111+
112+
## Examples
113+
114+
<!-- eslint no-undef: "error" -->
115+
116+
```javascript
117+
var filledBy = require( '@stdlib/array/base/filled-by' );
118+
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' );
119+
var bernoulli = require( '@stdlib/random/base/bernoulli' );
120+
var linspace = require( '@stdlib/array/base/linspace' );
121+
var mskput = require( '@stdlib/array/mskput' );
122+
123+
// Generate a linearly spaced array:
124+
var x = linspace( 0, 100, 11 );
125+
console.log( x );
126+
127+
// Generate a random mask array:
128+
var N = discreteUniform( 5, 15 );
129+
var mask = filledBy( N, bernoulli.factory( 0.3 ) );
130+
console.log( mask );
131+
132+
// Generate an array of random values:
133+
var values = filledBy( N, discreteUniform.factory( 1000, 2000 ) );
134+
console.log( values );
135+
136+
// Update a random sample of elements in `x`:
137+
var out = mskput( x, mask, values );
138+
console.log( out );
139+
```
140+
141+
</section>
142+
143+
<!-- /.examples -->
144+
145+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
146+
147+
<section class="related">
148+
149+
</section>
150+
151+
<!-- /.related -->
152+
153+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
154+
155+
<section class="links">
156+
157+
[@stdlib/array/dtypes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/dtypes
158+
159+
[@stdlib/array/safe-casts]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/safe-casts
160+
161+
[@stdlib/array/same-kind-casts]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/same-kind-casts
162+
163+
</section>
164+
165+
<!-- /.links -->
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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 zeros = require( '@stdlib/array/base/zeros' );
27+
var pkg = require( './../package.json' ).name;
28+
var mskput = require( './../lib' );
29+
30+
31+
// MAIN //
32+
33+
bench( pkg+'::no_broadcasting:len=100', function benchmark( b ) {
34+
var mask;
35+
var x;
36+
var i;
37+
var v;
38+
39+
x = zeroTo( 100 );
40+
mask = zeros( 100 );
41+
42+
b.tic();
43+
for ( i = 0; i < b.iterations; i++ ) {
44+
v = mskput( x, mask, x );
45+
if ( typeof v !== 'object' ) {
46+
b.fail( 'should return an array' );
47+
}
48+
}
49+
b.toc();
50+
if ( !isArray( v ) ) {
51+
b.fail( 'should return an array' );
52+
}
53+
b.pass( 'benchmark finished' );
54+
b.end();
55+
});
56+
57+
bench( pkg+'::broadcasting:len=100', function benchmark( b ) {
58+
var mask;
59+
var x;
60+
var i;
61+
var v;
62+
63+
x = zeroTo( 100 );
64+
mask = zeros( 100 );
65+
66+
b.tic();
67+
for ( i = 0; i < b.iterations; i++ ) {
68+
v = mskput( x, mask, [ i ] );
69+
if ( typeof v !== 'object' ) {
70+
b.fail( 'should return an array' );
71+
}
72+
}
73+
b.toc();
74+
if ( !isArray( v ) ) {
75+
b.fail( 'should return an array' );
76+
}
77+
b.pass( 'benchmark finished' );
78+
b.end();
79+
});
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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 discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
26+
var zeros = require( '@stdlib/array/base/zeros' );
27+
var isArray = require( '@stdlib/assert/is-array' );
28+
var pkg = require( './../package.json' ).name;
29+
var mskput = require( './../lib' );
30+
31+
32+
// VARIABLES //
33+
34+
var opts = {
35+
'dtype': 'generic'
36+
};
37+
38+
39+
// FUNCTIONS //
40+
41+
/**
42+
* Creates a benchmark function.
43+
*
44+
* @private
45+
* @param {PositiveInteger} len - array length
46+
* @returns {Function} benchmark function
47+
*/
48+
function createBenchmark( len ) {
49+
var values;
50+
var mask;
51+
var x;
52+
53+
x = discreteUniform( len, 0, 10, opts );
54+
mask = zeros( len );
55+
values = [
56+
discreteUniform( len, -10, 0, opts ),
57+
discreteUniform( len, 0, 10, opts )
58+
];
59+
60+
return benchmark;
61+
62+
/**
63+
* Benchmark function.
64+
*
65+
* @private
66+
* @param {Benchmark} b - benchmark instance
67+
*/
68+
function benchmark( b ) {
69+
var v;
70+
var i;
71+
72+
b.tic();
73+
for ( i = 0; i < b.iterations; i++ ) {
74+
v = mskput( x, mask, values[ i%values.length ] );
75+
if ( typeof v !== 'object' ) {
76+
b.fail( 'should return an array' );
77+
}
78+
}
79+
b.toc();
80+
if ( !isArray( v ) ) {
81+
b.fail( 'should return an array' );
82+
}
83+
b.pass( 'benchmark finished' );
84+
b.end();
85+
}
86+
}
87+
88+
89+
// MAIN //
90+
91+
/**
92+
* Main execution sequence.
93+
*
94+
* @private
95+
*/
96+
function main() {
97+
var len;
98+
var min;
99+
var max;
100+
var f;
101+
var i;
102+
103+
min = 1; // 10^min
104+
max = 6; // 10^max
105+
106+
for ( i = min; i <= max; i++ ) {
107+
len = pow( 10, i );
108+
f = createBenchmark( len );
109+
bench( pkg+':len='+len, f );
110+
}
111+
}
112+
113+
main();

‎lib/node_modules/@stdlib/array/mskput/docs/repl.txt

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change

‎lib/node_modules/@stdlib/array/mskput/docs/types/index.d.ts

Lines changed: 232 additions & 0 deletions
Original file line numberDiff line numberDiff line change

‎lib/node_modules/@stdlib/array/mskput/docs/types/test.ts

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change

‎lib/node_modules/@stdlib/array/mskput/examples/index.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change

‎lib/node_modules/@stdlib/array/mskput/lib/defaults.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change

‎lib/node_modules/@stdlib/array/mskput/lib/index.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change

‎lib/node_modules/@stdlib/array/mskput/lib/main.js

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change

‎lib/node_modules/@stdlib/array/mskput/lib/validate.js

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change

‎lib/node_modules/@stdlib/array/mskput/package.json

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change

‎lib/node_modules/@stdlib/array/mskput/test/test.js

Lines changed: 696 additions & 0 deletions
Large diffs are not rendered by default.

‎lib/node_modules/@stdlib/array/mskput/test/test.validate.js

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change

0 commit comments

Comments
 (0)
Please sign in to comment.