Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
163 changes: 163 additions & 0 deletions lib/node_modules/@stdlib/array/base/to-filled/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
<!--

@license Apache-2.0

Copyright (c) 2026 The Stdlib Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

-->

# toFilled

> Return a new array with all elements within a specified range replaced with a provided value.

<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->

<section class="intro">

</section>

<!-- /.intro -->

<!-- Package usage documentation. -->

<section class="usage">

## Usage

```javascript
var toFilled = require( '@stdlib/array/base/to-filled' );
```

#### toFilled( x, value, start, end )

Returns a new array with all elements within a specified range replaced with a provided value.

```javascript
var x = [ 1, 2, 3, 4 ];

var out = toFilled( x, 5, 1, 3 );
// returns [ 1, 5, 5, 4 ]

out = toFilled( x, 6, -3, -1 );
// returns [ 1, 6, 6, 4 ]
```

The function accepts the following arguments:

- **x**: an input array.
- **value**: fill value.
- **start**: starting index (inclusive).
- **end**: ending index (exclusive).

#### toFilled.assign( x, value, start, end, out, stride, offset )

Copies elements from one array to another array and replaces all elements within a specified range with a provided value.

```javascript
var x = [ 1, 2, 3, 4 ];

var out = [ 0, 0, 0, 0 ];
var arr = toFilled.assign( x, 5, 1, 3, out, 1, 0 );
// returns [ 1, 5, 5, 4 ]

var bool = ( arr === out );
// returns true
```

The function accepts the following arguments:

- **x**: an input array.
- **value**: fill value.
- **start**: starting index (inclusive).
- **end**: ending index (exclusive).
- **out**: output array.
- **stride**: output array stride.
- **offset**: output array offset.

</section>

<!-- /.usage -->

<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="notes">

## Notes

- Negative indices are resolved relative to the last array element, with the last element corresponding to `-1`.

</section>

<!-- /.notes -->

<!-- Package usage examples. -->

<section class="examples">

## Examples

<!-- eslint no-undef: "error" -->

```javascript
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
var logEachMap = require( '@stdlib/console/log-each-map' );
var naryFunction = require( '@stdlib/utils/nary-function' );
var papply = require( '@stdlib/utils/papply' );
var toFilled = require( '@stdlib/array/base/to-filled' );

// Define an array:
var opts = {
'dtype': 'generic'
};
var x = discreteUniform( 10, -100, 100, opts );

// Define an array containing random fill values:
var values = discreteUniform( 100, -10000, 10000, opts );

// Define parallel arrays containing random fill ranges:
var starts = discreteUniform( values.length, 0, x.length-1, opts );
var ends = discreteUniform( values.length, 1, x.length, opts );

// Randomly fill ranges of the input array:
logEachMap( 'fill with %d in [%d, %d) => x = [%s]', values, starts, ends, naryFunction( papply( toFilled, x ), 3 ) );
```

</section>

<!-- /.examples -->

<!-- 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. -->

<section class="references">

</section>

<!-- /.references -->

<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->

<section class="related">

</section>

<!-- /.related -->

<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="links">

</section>

<!-- /.links -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2026 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

// MODULES //

var bench = require( '@stdlib/bench' );
var pow = require( '@stdlib/math/base/special/pow' );
var isArray = require( '@stdlib/assert/is-array' );
var ones = require( '@stdlib/array/base/ones' );
var zeros = require( '@stdlib/array/base/zeros' );
var format = require( '@stdlib/string/format' );
var pkg = require( './../package.json' ).name;
var toFilled = require( './../lib' );


// FUNCTIONS //

/**
* Creates a benchmark function.
*
* @private
* @param {PositiveInteger} len - array length
* @returns {Function} benchmark function
*/
function createBenchmark( len ) {
var out = zeros( len );
var x = ones( len );
return benchmark;

/**
* Benchmark function.
*
* @private
* @param {Benchmark} b - benchmark instance
*/
function benchmark( b ) {
var v;
var i;

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
v = toFilled.assign( x, i, 0, len, out, 1, 0 );
if ( typeof v !== 'object' ) {
b.fail( 'should return an array' );
}
}
b.toc();
if ( !isArray( v ) ) {
b.fail( 'should return an array' );
}
b.pass( 'benchmark finished' );
b.end();
}
}


// MAIN //

/**
* Main execution sequence.
*
* @private
*/
function main() {
var len;
var min;
var max;
var f;
var i;

min = 1; // 10^min
max = 6; // 10^max

for ( i = min; i <= max; i++ ) {
len = pow( 10, i );

f = createBenchmark( len );
bench( format( '%s:assign:dtype=generic,len=%d', pkg, len ), f );
}
}

main();
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2026 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

// MODULES //

var bench = require( '@stdlib/bench' );
var pow = require( '@stdlib/math/base/special/pow' );
var isArray = require( '@stdlib/assert/is-array' );
var ones = require( '@stdlib/array/base/ones' );
var format = require( '@stdlib/string/format' );
var pkg = require( './../package.json' ).name;
var toFilled = require( './../lib' );


// FUNCTIONS //

/**
* Creates a benchmark function.
*
* @private
* @param {PositiveInteger} len - array length
* @returns {Function} benchmark function
*/
function createBenchmark( len ) {
var x = ones( len );
return benchmark;

/**
* Benchmark function.
*
* @private
* @param {Benchmark} b - benchmark instance
*/
function benchmark( b ) {
var out;
var i;

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
out = toFilled( x, i, 0, len );
if ( out.length !== len ) {
b.fail( 'unexpected length' );
}
}
b.toc();
if ( !isArray( out ) ) {
b.fail( 'should return an array' );
}
b.pass( 'benchmark finished' );
b.end();
}
}


// MAIN //

/**
* Main execution sequence.
*
* @private
*/
function main() {
var len;
var min;
var max;
var f;
var i;

min = 1; // 10^min
max = 6; // 10^max

for ( i = min; i <= max; i++ ) {
len = pow( 10, i );

f = createBenchmark( len );
bench( format( '%s:dtype=generic,len=%d', pkg, len ), f );
}
}

main();
Loading