Skip to content

Commit dd4f94d

Browse files
committed
feat: add array/base/to-filled
1 parent 86e9aee commit dd4f94d

14 files changed

Lines changed: 2035 additions & 0 deletions

File tree

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2026 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+
# toFilled
22+
23+
> Return a new array with all elements within a specified range replaced with a provided value.
24+
25+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
26+
27+
<section class="intro">
28+
29+
</section>
30+
31+
<!-- /.intro -->
32+
33+
<!-- Package usage documentation. -->
34+
35+
<section class="usage">
36+
37+
## Usage
38+
39+
```javascript
40+
var toFilled = require( '@stdlib/array/base/to-filled' );
41+
```
42+
43+
#### toFilled( x, value, start, end )
44+
45+
Returns a new array with all elements within a specified range replaced with a provided value.
46+
47+
```javascript
48+
var x = [ 1, 2, 3, 4 ];
49+
50+
var out = toFilled( x, 5, 1, 3 );
51+
// returns [ 1, 5, 5, 4 ]
52+
53+
out = toFilled( x, 6, -3, -1 );
54+
// returns [ 1, 6, 6, 4 ]
55+
```
56+
57+
The function accepts the following arguments:
58+
59+
- **x**: an input array.
60+
- **value**: fill value.
61+
- **start**: starting index (inclusive).
62+
- **end**: ending index (exclusive).
63+
64+
#### toFilled.assign( x, value, start, end, out, stride, offset )
65+
66+
Copies elements from one array to another array and replaces all elements within a specified range with a provided value.
67+
68+
```javascript
69+
var x = [ 1, 2, 3, 4 ];
70+
71+
var out = [ 0, 0, 0, 0 ];
72+
var arr = toFilled.assign( x, 5, 1, 3, out, 1, 0 );
73+
// returns [ 1, 5, 5, 4 ]
74+
75+
var bool = ( arr === out );
76+
// returns true
77+
```
78+
79+
The function accepts the following arguments:
80+
81+
- **x**: an input array.
82+
- **value**: fill value.
83+
- **start**: starting index (inclusive).
84+
- **end**: ending index (exclusive).
85+
- **out**: output array.
86+
- **stride**: output array stride.
87+
- **offset**: output array offset.
88+
89+
</section>
90+
91+
<!-- /.usage -->
92+
93+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
94+
95+
<section class="notes">
96+
97+
## Notes
98+
99+
- Negative indices are resolved relative to the last array element, with the last element corresponding to `-1`.
100+
101+
</section>
102+
103+
<!-- /.notes -->
104+
105+
<!-- Package usage examples. -->
106+
107+
<section class="examples">
108+
109+
## Examples
110+
111+
<!-- eslint no-undef: "error" -->
112+
113+
```javascript
114+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
115+
var logEachMap = require( '@stdlib/console/log-each-map' );
116+
var naryFunction = require( '@stdlib/utils/nary-function' );
117+
var papply = require( '@stdlib/utils/papply' );
118+
var toFilled = require( '@stdlib/array/base/to-filled' );
119+
120+
// Define an array:
121+
var opts = {
122+
'dtype': 'generic'
123+
};
124+
var x = discreteUniform( 10, -100, 100, opts );
125+
126+
// Define an array containing random fill values:
127+
var values = discreteUniform( 100, -10000, 10000, opts );
128+
129+
// Define parallel arrays containing random fill ranges:
130+
var starts = discreteUniform( values.length, 0, x.length-1, opts );
131+
var ends = discreteUniform( values.length, 1, x.length, opts );
132+
133+
// Randomly fill ranges of the input array:
134+
logEachMap( 'fill with %d in [%d, %d) => x = [%s]', values, starts, ends, naryFunction( papply( toFilled, x ), 3 ) );
135+
```
136+
137+
</section>
138+
139+
<!-- /.examples -->
140+
141+
<!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
142+
143+
<section class="references">
144+
145+
</section>
146+
147+
<!-- /.references -->
148+
149+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
150+
151+
<section class="related">
152+
153+
</section>
154+
155+
<!-- /.related -->
156+
157+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
158+
159+
<section class="links">
160+
161+
</section>
162+
163+
<!-- /.links -->
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 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 isArray = require( '@stdlib/assert/is-array' );
26+
var ones = require( '@stdlib/array/base/ones' );
27+
var zeros = require( '@stdlib/array/base/zeros' );
28+
var format = require( '@stdlib/string/format' );
29+
var pkg = require( './../package.json' ).name;
30+
var toFilled = require( './../lib' );
31+
32+
33+
// FUNCTIONS //
34+
35+
/**
36+
* Creates a benchmark function.
37+
*
38+
* @private
39+
* @param {PositiveInteger} len - array length
40+
* @returns {Function} benchmark function
41+
*/
42+
function createBenchmark( len ) {
43+
var out = zeros( len );
44+
var x = ones( len );
45+
return benchmark;
46+
47+
/**
48+
* Benchmark function.
49+
*
50+
* @private
51+
* @param {Benchmark} b - benchmark instance
52+
*/
53+
function benchmark( b ) {
54+
var v;
55+
var i;
56+
57+
b.tic();
58+
for ( i = 0; i < b.iterations; i++ ) {
59+
v = toFilled.assign( x, i, 0, len, out, 1, 0 );
60+
if ( typeof v !== 'object' ) {
61+
b.fail( 'should return an array' );
62+
}
63+
}
64+
b.toc();
65+
if ( !isArray( v ) ) {
66+
b.fail( 'should return an array' );
67+
}
68+
b.pass( 'benchmark finished' );
69+
b.end();
70+
}
71+
}
72+
73+
74+
// MAIN //
75+
76+
/**
77+
* Main execution sequence.
78+
*
79+
* @private
80+
*/
81+
function main() {
82+
var len;
83+
var min;
84+
var max;
85+
var f;
86+
var i;
87+
88+
min = 1; // 10^min
89+
max = 6; // 10^max
90+
91+
for ( i = min; i <= max; i++ ) {
92+
len = pow( 10, i );
93+
94+
f = createBenchmark( len );
95+
bench( format( '%s:assign:dtype=generic,len=%d', pkg, len ), f );
96+
}
97+
}
98+
99+
main();
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 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 isArray = require( '@stdlib/assert/is-array' );
26+
var ones = require( '@stdlib/array/base/ones' );
27+
var format = require( '@stdlib/string/format' );
28+
var pkg = require( './../package.json' ).name;
29+
var toFilled = 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 = ones( len );
43+
return benchmark;
44+
45+
/**
46+
* Benchmark function.
47+
*
48+
* @private
49+
* @param {Benchmark} b - benchmark instance
50+
*/
51+
function benchmark( b ) {
52+
var out;
53+
var i;
54+
55+
b.tic();
56+
for ( i = 0; i < b.iterations; i++ ) {
57+
out = toFilled( x, i, 0, len );
58+
if ( out.length !== len ) {
59+
b.fail( 'unexpected length' );
60+
}
61+
}
62+
b.toc();
63+
if ( !isArray( out ) ) {
64+
b.fail( 'should return an array' );
65+
}
66+
b.pass( 'benchmark finished' );
67+
b.end();
68+
}
69+
}
70+
71+
72+
// MAIN //
73+
74+
/**
75+
* Main execution sequence.
76+
*
77+
* @private
78+
*/
79+
function main() {
80+
var len;
81+
var min;
82+
var max;
83+
var f;
84+
var i;
85+
86+
min = 1; // 10^min
87+
max = 6; // 10^max
88+
89+
for ( i = min; i <= max; i++ ) {
90+
len = pow( 10, i );
91+
92+
f = createBenchmark( len );
93+
bench( format( '%s:dtype=generic,len=%d', pkg, len ), f );
94+
}
95+
}
96+
97+
main();

0 commit comments

Comments
 (0)