Skip to content

Commit 5a3d80b

Browse files
committed
feat: add array/base/scatter-filled
--- 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 4f2384c commit 5a3d80b

File tree

11 files changed

+915
-0
lines changed

11 files changed

+915
-0
lines changed
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
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+
# scatterFilled
22+
23+
> Scatter a list of provided values to specified indices in a new filled "generic" array.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var scatterFilled = require( '@stdlib/array/base/scatter-filled' );
31+
```
32+
33+
#### scatterFilled( fill, len, indices, values, mode )
34+
35+
Scatters a list of provided values to specified indices in a new filled "generic" array.
36+
37+
```javascript
38+
var out = scatterFilled( null, 4, [ 1, 3 ], [ 20, 40 ], 'throw' );
39+
// returns [ null, 20, null, 40 ]
40+
```
41+
42+
The function supports the following parameters:
43+
44+
- **fill**: fill value.
45+
- **len**: output array length.
46+
- **indices**: list of indices.
47+
- **values**: values to scatter. When `indices` contains one or more elements, `values` must be broadcast [compatible][@stdlib/ndarray/base/broadcast-shapes] with `indices` (i.e., must have either one element or the same number of elements as `indices`).
48+
- **mode**: index [mode][@stdlib/ndarray/base/ind].
49+
50+
If `indices` is an empty array, the function returns a filled array.
51+
52+
```javascript
53+
var out = scatterFilled( null, 4, [], [ 20, 40 ], 'throw' );
54+
// returns [ null, null, null, null ]
55+
```
56+
57+
The function supports broadcasting a `values` array containing a single element against an `indices` array containing one or more elements.
58+
59+
```javascript
60+
var out = scatterFilled( null, 4, [ 1, 3 ], [ 20 ], 'throw' );
61+
// returns [ null, 20, null, 20 ]
62+
```
63+
64+
</section>
65+
66+
<!-- /.usage -->
67+
68+
<section class="notes">
69+
70+
</section>
71+
72+
<!-- /.notes -->
73+
74+
<section class="examples">
75+
76+
## Examples
77+
78+
<!-- eslint no-undef: "error" -->
79+
80+
```javascript
81+
var filledBy = require( '@stdlib/array/base/filled-by' );
82+
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' );
83+
var scatterFilled = require( '@stdlib/array/base/scatter-filled' );
84+
85+
// Generate an array of random indices:
86+
var N = discreteUniform( 5, 15 );
87+
var indices = filledBy( N, discreteUniform.factory( 0, 10 ) );
88+
console.log( indices );
89+
90+
// Generate an array of random values:
91+
var values = filledBy( N, discreteUniform.factory( 1000, 2025 ) );
92+
console.log( values );
93+
94+
// Scatter a random sample of elements to a new array:
95+
var out = scatterFilled( null, 11, indices, values, 'throw' );
96+
console.log( out );
97+
```
98+
99+
</section>
100+
101+
<!-- /.examples -->
102+
103+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
104+
105+
<section class="related">
106+
107+
</section>
108+
109+
<!-- /.related -->
110+
111+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
112+
113+
<section class="links">
114+
115+
[@stdlib/ndarray/base/ind]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/ind
116+
117+
[@stdlib/ndarray/base/broadcast-shapes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/broadcast-shapes
118+
119+
</section>
120+
121+
<!-- /.links -->
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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 isArray = require( '@stdlib/assert/is-array' );
25+
var zeroTo = require( '@stdlib/array/base/zero-to' );
26+
var pkg = require( './../package.json' ).name;
27+
var scatterFilled = require( './../lib' );
28+
29+
30+
// MAIN //
31+
32+
bench( pkg+'::no_broadcasting:len=100', function benchmark( b ) {
33+
var idx;
34+
var x;
35+
var i;
36+
var v;
37+
38+
x = zeroTo( 100 );
39+
idx = zeroTo( 100 );
40+
41+
b.tic();
42+
for ( i = 0; i < b.iterations; i++ ) {
43+
v = scatterFilled( -1, x.length, idx, x, 'throw' );
44+
if ( typeof v !== 'object' ) {
45+
b.fail( 'should return an array' );
46+
}
47+
}
48+
b.toc();
49+
if ( !isArray( v ) ) {
50+
b.fail( 'should return an array' );
51+
}
52+
b.pass( 'benchmark finished' );
53+
b.end();
54+
});
55+
56+
bench( pkg+'::broadcasting:len=100', function benchmark( b ) {
57+
var idx;
58+
var i;
59+
var v;
60+
61+
idx = zeroTo( 100 );
62+
63+
b.tic();
64+
for ( i = 0; i < b.iterations; i++ ) {
65+
v = scatterFilled( -1, 100, idx, [ i ], 'throw' );
66+
if ( typeof v !== 'object' ) {
67+
b.fail( 'should return an array' );
68+
}
69+
}
70+
b.toc();
71+
if ( !isArray( v ) ) {
72+
b.fail( 'should return an array' );
73+
}
74+
b.pass( 'benchmark finished' );
75+
b.end();
76+
});
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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 discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
26+
var zeroTo = require( '@stdlib/array/base/zero-to' );
27+
var isArray = require( '@stdlib/assert/is-array' );
28+
var pkg = require( './../package.json' ).name;
29+
var scatterFilled = 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 idx;
51+
52+
idx = zeroTo( len );
53+
values = [
54+
discreteUniform( len, -10, 0, opts ),
55+
discreteUniform( len, 0, 10, opts )
56+
];
57+
58+
return benchmark;
59+
60+
/**
61+
* Benchmark function.
62+
*
63+
* @private
64+
* @param {Benchmark} b - benchmark instance
65+
*/
66+
function benchmark( b ) {
67+
var v;
68+
var i;
69+
70+
b.tic();
71+
for ( i = 0; i < b.iterations; i++ ) {
72+
v = scatterFilled( -999, len, idx, values[ i%values.length ], 'throw' );
73+
if ( typeof v !== 'object' ) {
74+
b.fail( 'should return an array' );
75+
}
76+
}
77+
b.toc();
78+
if ( !isArray( v ) ) {
79+
b.fail( 'should return an array' );
80+
}
81+
b.pass( 'benchmark finished' );
82+
b.end();
83+
}
84+
}
85+
86+
87+
// MAIN //
88+
89+
/**
90+
* Main execution sequence.
91+
*
92+
* @private
93+
*/
94+
function main() {
95+
var len;
96+
var min;
97+
var max;
98+
var f;
99+
var i;
100+
101+
min = 1; // 10^min
102+
max = 6; // 10^max
103+
104+
for ( i = min; i <= max; i++ ) {
105+
len = pow( 10, i );
106+
f = createBenchmark( len );
107+
bench( pkg+':len='+len, f );
108+
}
109+
}
110+
111+
main();
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
2+
{{alias}}( fill, len, indices, values, mode )
3+
Scatters a list of provided values to specified indices in a new filled
4+
"generic" array.
5+
6+
The function supports broadcasting a `values` array containing a single
7+
element against an `indices` array containing one or more elements.
8+
9+
If `indices` is an empty array, the function returns a filled array.
10+
11+
Parameters
12+
----------
13+
fill: any
14+
Fill value.
15+
16+
len: integer
17+
Output array length.
18+
19+
indices: ArrayLikeObject<integer>
20+
List of element indices.
21+
22+
values: ArrayLikeObject
23+
Values to scatter. When `indices` contains one or more elements,
24+
`values` must be broadcast compatible with `indices` (i.e., must have
25+
either one element or the same number of elements as `indices`).
26+
27+
mode: string
28+
Specifies how to handle an index outside the interval [0, max], where
29+
`max` is the maximum possible array index. If equal to 'throw', the
30+
function throws an error. If equal to 'normalize', the function throws
31+
an error if provided an out-of-bounds normalized index. If equal to
32+
'wrap', the function wraps around an index using modulo arithmetic. If
33+
equal to 'clamp', the function sets an index to either 0 (minimum index)
34+
or the maximum index.
35+
36+
Returns
37+
-------
38+
out: Array
39+
Output array.
40+
41+
Examples
42+
--------
43+
> var out = {{alias}}( null, 4, [ 1, 3 ], [ 20, 40 ], 'throw' )
44+
[ null, 20, null, 40 ]
45+
46+
See Also
47+
--------
48+

0 commit comments

Comments
 (0)