Skip to content

Commit e8a8a6e

Browse files
committed
feat: add array/base/place
1 parent 566c39c commit e8a8a6e

File tree

11 files changed

+1537
-0
lines changed

11 files changed

+1537
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
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+
# place
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 place = require( '@stdlib/array/base/place' );
31+
```
32+
33+
#### place( x, mask, values, mode )
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 = place( x, [ 0, 1, 0, 1 ], [ 20, 40 ], 'throw' );
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+
- **mode**: string specifying whether to raise an exception when the number of `values` is less than the number of truthy `mask` values.
53+
54+
The function supports the following modes:
55+
56+
- `'throw'`: specifies that the function must raise an exception when the function is provided insufficient `values` to satisfy the `mask` array.
57+
- `'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.
58+
- `'repeat'`: specifies that the function must reuse provided `values` when replacing elements in `x` in order to satisfy the `mask` array.
59+
60+
When `mode` is equal to `'broadcast`', the function supports broadcasting a `values` array containing a single element against the number of truthy values in the `mask` array.
61+
62+
```javascript
63+
var x = [ 1, 2, 3, 4 ];
64+
65+
var out = place( x, [ 0, 1, 0, 1 ], [ 20 ], 'broadcast' );
66+
// returns [ 1, 20, 3, 20 ]
67+
68+
var bool = ( out === x );
69+
// returns true
70+
```
71+
72+
When `mode` is equal to `repeat`, the function supports recycling elements in a `values` array to satisfy the number of truthy values in the `mask` array.
73+
74+
```javascript
75+
var x = [ 1, 2, 3, 4 ];
76+
77+
var out = place( x, [ 1, 1, 0, 1 ], [ 20, 40 ], 'repeat' );
78+
// returns [ 20, 40, 3, 20 ]
79+
80+
var bool = ( out === x );
81+
// returns true
82+
```
83+
84+
</section>
85+
86+
<!-- /.usage -->
87+
88+
<section class="notes">
89+
90+
## Notes
91+
92+
- The function mutates the input array `x`.
93+
- If a `mask` array element is truthy, the corresponding element in `x` is **replaced**; otherwise, the corresponding element in `x` is "masked" and thus left unchanged.
94+
95+
</section>
96+
97+
<!-- /.notes -->
98+
99+
<section class="examples">
100+
101+
## Examples
102+
103+
<!-- eslint no-undef: "error" -->
104+
105+
```javascript
106+
var filledBy = require( '@stdlib/array/base/filled-by' );
107+
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' );
108+
var bernoulli = require( '@stdlib/random/base/bernoulli' );
109+
var linspace = require( '@stdlib/array/base/linspace' );
110+
var place = require( '@stdlib/array/base/place' );
111+
112+
// Generate a linearly spaced array:
113+
var x = linspace( 0, 100, 11 );
114+
console.log( x );
115+
116+
// Generate a random mask array:
117+
var N = discreteUniform( 5, 15 );
118+
var mask = filledBy( N, bernoulli.factory( 0.3 ) );
119+
console.log( mask );
120+
121+
// Generate an array of random values:
122+
var values = filledBy( N, discreteUniform.factory( 1000, 2000 ) );
123+
console.log( values );
124+
125+
// Update a random sample of elements in `x`:
126+
var out = place( x, mask, values, 'throw' );
127+
console.log( out );
128+
```
129+
130+
</section>
131+
132+
<!-- /.examples -->
133+
134+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
135+
136+
<section class="related">
137+
138+
</section>
139+
140+
<!-- /.related -->
141+
142+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
143+
144+
<section class="links">
145+
146+
</section>
147+
148+
<!-- /.links -->
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 ones = require( '@stdlib/array/base/ones' );
27+
var pkg = require( './../package.json' ).name;
28+
var place = 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 = ones( 100 );
41+
42+
b.tic();
43+
for ( i = 0; i < b.iterations; i++ ) {
44+
v = place( x, mask, x, 'throw' );
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 = ones( 100 );
65+
66+
b.tic();
67+
for ( i = 0; i < b.iterations; i++ ) {
68+
v = place( x, mask, [ i ], 'broadcast' );
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+
});
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 ones = require( '@stdlib/array/base/ones' );
27+
var isArray = require( '@stdlib/assert/is-array' );
28+
var pkg = require( './../package.json' ).name;
29+
var place = 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 = ones( 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 = place( x, mask, values[ i%values.length ], 'throw' );
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();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
2+
{{alias}}( x, mask, values, mode )
3+
Replaces elements of an array with provided values according to a provided
4+
mask array.
5+
6+
When the `mode` is `'broadcast'`, the function supports broadcasting a
7+
`values` array containing a single element against the number of truthy
8+
values in the `mask` array.
9+
10+
When the `mode` is `'repeat'`, the function supports recycling elements in a
11+
`values` array to satisfy the number of truthy values in the `mask` array.
12+
13+
The function mutates the input array.
14+
15+
Parameters
16+
----------
17+
x: ArrayLikeObject
18+
Input array.
19+
20+
mask: ArrayLikeObject
21+
Mask array. If a mask array element is truthy, the corresponding element
22+
in `x` is *replaced*; otherwise, the corresponding element in `x` is
23+
"masked" and thus left unchanged.
24+
25+
values: ArrayLikeObject
26+
Values to set.
27+
28+
mode: string
29+
String specifying behavior when the number of values to set is less than
30+
the number of truthy mask values. The function supports the following
31+
modes:
32+
33+
- 'throw': specifies that the function must raise an exception when the
34+
function is provided insufficient values to satisfy the mask array.
35+
- 'broadcast': specifies that the function must broadcast a single-
36+
element values array and otherwise raise an exception when the function
37+
is provided insufficient values to satisfy the mask array.
38+
- 'repeat': specifies that the function must reuse provided values when
39+
replacing elements in `x` in order to satisfy the mask array.
40+
41+
Returns
42+
-------
43+
out: ArrayLikeObject
44+
Input array.
45+
46+
Examples
47+
--------
48+
> var x = [ 1, 2, 3, 4 ];
49+
> var out = {{alias}}( x, [ 0, 1, 0, 1 ], [ 20, 40 ], 'throw' )
50+
[ 1, 20, 3, 40 ]
51+
> var bool = ( out === x )
52+
true
53+
54+
See Also
55+
--------
56+

0 commit comments

Comments
 (0)