Skip to content

Commit 349b96f

Browse files
committed
feat: add fill method to array/fixed-endian-factory
1 parent 9db1815 commit 349b96f

File tree

5 files changed

+599
-0
lines changed

5 files changed

+599
-0
lines changed

lib/node_modules/@stdlib/array/fixed-endian-factory/README.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -705,6 +705,65 @@ var str = arr.toString();
705705

706706
* * *
707707

708+
<a name="method-some"></a>
709+
710+
#### TypedArrayFE.prototype.fill( value\[, start\[, end]] )
711+
712+
Returns a modified typed array filled with a fill value.
713+
714+
```javascript
715+
var Float64ArrayFE = fixedEndianFactory( 'float64' );
716+
717+
var arr = new Float64ArrayFE( 'little-endian', [ 1.0, 2.0, 3.0 ] );
718+
719+
// Set all elements to the same value:
720+
arr.fill( 6.0 );
721+
722+
var z = arr.get( 0 );
723+
// returns 6.0
724+
725+
z = arr.get( 1 );
726+
// returns 6.0
727+
728+
z = arr.get( 2 );
729+
// returns 6.0
730+
731+
// Fill all elements starting from the second element:
732+
arr.fill( 7.0, 1 );
733+
734+
z = arr.get( 1 );
735+
// returns 7.0
736+
737+
z = arr.get( 2 );
738+
// returns 7.0
739+
740+
// Fill all elements from first element until the second-to-last element:
741+
arr.fill( 8.0, 0, 2 );
742+
743+
z = arr.get( 0 );
744+
// returns 8.0
745+
746+
z = arr.get( 1 );
747+
// returns 8.0
748+
```
749+
750+
When a `start` and/or `end` index is negative, the respective index is determined relative to the last array element.
751+
752+
```javascript
753+
var Float64ArrayFE = fixedEndianFactory( 'float64' );
754+
755+
var arr = new Float64ArrayFE( 'little-endian', [ 1.0, 2.0, 3.0 ] );
756+
757+
// Set all array elements, except the last element, to the same value:
758+
arr.fill( 6.0, 0, -1 );
759+
760+
var z = arr.get( 0 );
761+
// returns 6.0
762+
763+
z = arr.get( arr.length - 1 );
764+
// returns 3.0
765+
```
766+
708767
## Notes
709768

710769
- A returned constructor supports the following byte orders:
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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 pkg = require( './../package.json' ).name;
25+
var factory = require( './../lib' );
26+
27+
28+
// VARIABLES //
29+
30+
var float64ArrayFE = factory( 'float64' );
31+
32+
33+
// MAIN //
34+
35+
bench( pkg+':fill', function benchmark( b ) {
36+
var values;
37+
var arr;
38+
var out;
39+
var i;
40+
41+
arr = float64ArrayFE( 'little-endian', 5);
42+
values = [ 6.0, 7.0, 10.0];
43+
44+
b.tic();
45+
for ( i = 0; i < b.iterations; i++ ) {
46+
out = arr.fill( values[ i%values.length ] );
47+
if ( typeof out !== 'object' ) {
48+
b.fail( 'should return an object' );
49+
}
50+
}
51+
b.toc();
52+
b.pass( 'benchmark finished' );
53+
b.end();
54+
});
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2023 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 pkg = require( './../package.json' ).name;
26+
var factory = require( './../lib' );
27+
28+
29+
// VARIABLES //
30+
31+
var Float64ArrayFE = factory( 'float64' );
32+
33+
34+
// FUNCTIONS //
35+
36+
/**
37+
* Creates a benchmark function.
38+
*
39+
* @private
40+
* @param {PositiveInteger} len - array length
41+
* @returns {Function} benchmark function
42+
*/
43+
function createBenchmark( len ) {
44+
var arr = new Float64ArrayFE( 'little-endian', 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 values;
55+
var out;
56+
var i;
57+
58+
values = [ 6.0, 7.0, 10.2];
59+
60+
b.tic();
61+
for ( i = 0; i < b.iterations; i++ ) {
62+
out = arr.fill( values[ i%values.length ] );
63+
if ( typeof out !== 'object' ) {
64+
b.fail( 'should return an object' );
65+
}
66+
}
67+
b.toc();
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+
f = createBenchmark( len );
94+
bench( pkg+':fill:len='+len, f );
95+
}
96+
}
97+
98+
main();

lib/node_modules/@stdlib/array/fixed-endian-factory/lib/main.js

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -875,6 +875,94 @@ function factory( dtype ) { // eslint-disable-line max-lines-per-function, stdli
875875
return out.join( ',' );
876876
});
877877

878+
/**
879+
* Returns a modified typed array filled with a fill value.
880+
*
881+
* @name fill
882+
* @memberof Complex128Array.prototype
883+
* @type {Function}
884+
* @param {ComplexLike} value - fill value
885+
* @param {integer} [start=0] - starting index (inclusive)
886+
* @param {integer} [end] - ending index (exclusive)
887+
* @throws {TypeError} `this` must be a complex number array
888+
* @throws {TypeError} first argument must be a complex number
889+
* @throws {TypeError} second argument must be an integer
890+
* @throws {TypeError} third argument must be an integer
891+
* @returns {Complex128Array} modified array
892+
*
893+
* @example
894+
* var real = require( '@stdlib/complex/float64/real' );
895+
* var imag = require( '@stdlib/complex/float64/imag' );
896+
*
897+
* var arr = new Complex128Array( 3 );
898+
*
899+
* arr.fill( new Complex128( 1.0, 1.0 ), 1 );
900+
*
901+
* var z = arr.get( 1 );
902+
* // returns <Complex128>
903+
*
904+
* var re = real( z );
905+
* // returns 1.0
906+
*
907+
* var im = imag( z );
908+
* // returns 1.0
909+
*
910+
* z = arr.get( 2 );
911+
* // returns <Complex128>
912+
*
913+
* re = real( z );
914+
* // returns 1.0
915+
*
916+
* im = imag( z );
917+
* // returns 1.0
918+
*/
919+
setReadOnly( TypedArray.prototype, 'fill', function fill( value, start, end ) {
920+
var isLE;
921+
var buf;
922+
var len;
923+
var i;
924+
if ( !isTypedArray( this ) ) {
925+
throw new TypeError( 'invalid invocation. `this` is not a TypedArray array.' );
926+
}
927+
buf = this._buffer;
928+
len = this._length;
929+
isLE = this._isLE;
930+
if ( arguments.length > 1 ) {
931+
if ( !isInteger( start ) ) {
932+
throw new TypeError( format( 'invalid argument. Second argument must be an integer. Value: `%s`.', start ) );
933+
}
934+
if ( start < 0 ) {
935+
start += len;
936+
if ( start < 0 ) {
937+
start = 0;
938+
}
939+
}
940+
if ( arguments.length > 2 ) {
941+
if ( !isInteger( end ) ) {
942+
throw new TypeError( format( 'invalid argument. Third argument must be an integer. Value: `%s`.', end ) );
943+
}
944+
if ( end < 0 ) {
945+
end += len;
946+
if ( end < 0 ) {
947+
end = 0;
948+
}
949+
}
950+
if ( end > len ) {
951+
end = len;
952+
}
953+
} else {
954+
end = len;
955+
}
956+
} else {
957+
start = 0;
958+
end = len;
959+
}
960+
for ( i = start; i < end; i++ ) {
961+
buf[ SETTER ]( i*BYTES_PER_ELEMENT, value, isLE );
962+
}
963+
return this;
964+
});
965+
878966
return TypedArray;
879967

880968
/**

0 commit comments

Comments
 (0)