Skip to content

feat: add fill method to array/fixed-endian-factory #3284

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: develop
Choose a base branch
from
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
59 changes: 59 additions & 0 deletions lib/node_modules/@stdlib/array/fixed-endian-factory/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -958,6 +958,65 @@ var v = out.get( 0 );

* * *

<a name="method-some"></a>

#### TypedArrayFE.prototype.fill( value\[, start\[, end]] )

Returns a modified typed array filled with a fill value.

```javascript
var Float64ArrayFE = fixedEndianFactory( 'float64' );

var arr = new Float64ArrayFE( 'little-endian', [ 1.0, 2.0, 3.0 ] );

// Set all elements to the same value:
arr.fill( 6.0 );

var z = arr.get( 0 );
// returns 6.0

z = arr.get( 1 );
// returns 6.0

z = arr.get( 2 );
// returns 6.0

// Fill all elements starting from the second element:
arr.fill( 7.0, 1 );

z = arr.get( 1 );
// returns 7.0

z = arr.get( 2 );
// returns 7.0

// Fill all elements from first element until the second-to-last element:
arr.fill( 8.0, 0, 2 );

z = arr.get( 0 );
// returns 8.0

z = arr.get( 1 );
// returns 8.0
```

When a `start` and/or `end` index is negative, the respective index is determined relative to the last array element.

```javascript
var Float64ArrayFE = fixedEndianFactory( 'float64' );

var arr = new Float64ArrayFE( 'little-endian', [ 1.0, 2.0, 3.0 ] );

// Set all array elements, except the last element, to the same value:
arr.fill( 6.0, 0, -1 );

var z = arr.get( 0 );
// returns 6.0

z = arr.get( arr.length - 1 );
// returns 3.0
```

## Notes

- A returned constructor supports the following byte orders:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2024 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 pkg = require( './../package.json' ).name;
var factory = require( './../lib' );


// VARIABLES //

var Float64ArrayFE = factory( 'float64' );


// MAIN //

bench( pkg+':fill', function benchmark( b ) {
var values;
var arr;
var out;
var i;

arr = new Float64ArrayFE( 'little-endian', 5);
values = [ 6.0, 7.0, 10.0 ];

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
out = arr.fill( values[ i%values.length ] );
if ( typeof out !== 'object' ) {
b.fail( 'should return an object' );
}
}
b.toc();
if ( !( out instanceof Float64ArrayFE ) ) {
b.fail( 'should return a TypedArray' );
}
b.pass( 'benchmark finished' );
b.end();
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2024 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 pkg = require( './../package.json' ).name;
var factory = require( './../lib' );


// VARIABLES //

var Float64ArrayFE = factory( 'float64' );


// FUNCTIONS //

/**
* Creates a benchmark function.
*
* @private
* @param {PositiveInteger} len - array length
* @returns {Function} benchmark function
*/
function createBenchmark( len ) {
var arr = new Float64ArrayFE( 'little-endian', len );
return benchmark;

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

values = [ 6.0, 7.0, 10.2 ];

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
out = arr.fill( values[ i%values.length ] );
if ( typeof out !== 'object' ) {
b.fail( 'should return an object' );
}
}
b.toc();
if ( !( out instanceof Float64ArrayFE ) ) {
b.fail( 'should return a TypedArray' );
}
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( pkg+':fill:len='+len, f );
}
}

main();
88 changes: 88 additions & 0 deletions lib/node_modules/@stdlib/array/fixed-endian-factory/lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -1153,6 +1153,94 @@ function factory( dtype ) { // eslint-disable-line max-lines-per-function, stdli
return out;
});

/**
* Returns a modified typed array filled with a fill value.
*
* @name fill
* @memberof Complex128Array.prototype
* @type {Function}
* @param {ComplexLike} value - fill value
* @param {integer} [start=0] - starting index (inclusive)
* @param {integer} [end] - ending index (exclusive)
* @throws {TypeError} `this` must be a complex number array
* @throws {TypeError} first argument must be a complex number
* @throws {TypeError} second argument must be an integer
* @throws {TypeError} third argument must be an integer
* @returns {Complex128Array} modified array
*
* @example
* var real = require( '@stdlib/complex/float64/real' );
* var imag = require( '@stdlib/complex/float64/imag' );
*
* var arr = new Complex128Array( 3 );
*
* arr.fill( new Complex128( 1.0, 1.0 ), 1 );
*
* var z = arr.get( 1 );
* // returns <Complex128>
*
* var re = real( z );
* // returns 1.0
*
* var im = imag( z );
* // returns 1.0
*
* z = arr.get( 2 );
* // returns <Complex128>
*
* re = real( z );
* // returns 1.0
*
* im = imag( z );
* // returns 1.0
*/
setReadOnly( TypedArray.prototype, 'fill', function fill( value, start, end ) {
var isLE;
var buf;
var len;
var i;
if ( !isTypedArray( this ) ) {
throw new TypeError( 'invalid invocation. `this` is not a TypedArray array.' );
}
buf = this._buffer;
len = this._length;
isLE = this._isLE;
if ( arguments.length > 1 ) {
if ( !isInteger( start ) ) {
throw new TypeError( format( 'invalid argument. Second argument must be an integer. Value: `%s`.', start ) );
}
if ( start < 0 ) {
start += len;
if ( start < 0 ) {
start = 0;
}
}
if ( arguments.length > 2 ) {
if ( !isInteger( end ) ) {
throw new TypeError( format( 'invalid argument. Third argument must be an integer. Value: `%s`.', end ) );
}
if ( end < 0 ) {
end += len;
if ( end < 0 ) {
end = 0;
}
}
if ( end > len ) {
end = len;
}
} else {
end = len;
}
} else {
start = 0;
end = len;
}
for ( i = start; i < end; i++ ) {
buf[ SETTER ]( i*BYTES_PER_ELEMENT, value, isLE );
}
return this;
});

return TypedArray;

/**
Expand Down
Loading
Loading