Skip to content

Commit a0ba090

Browse files
feat: add with method to array/fixed-endian-factory
PR-URL: #3291 Closes: #3162 Co-authored-by: Philipp Burckhardt <pburckhardt@outlook.com> Reviewed-by: Philipp Burckhardt <pburckhardt@outlook.com> Signed-off-by: Philipp Burckhardt <pburckhardt@outlook.com>
1 parent c0d083d commit a0ba090

File tree

5 files changed

+450
-3
lines changed

5 files changed

+450
-3
lines changed

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -908,6 +908,25 @@ var str = arr.join( 0 );
908908
// returns '10203'
909909
```
910910

911+
<a name="method-with"></a>
912+
913+
#### TypedArrayFE.prototype.with( index, value )
914+
915+
Returns a new typed array with the element at a provided index replaced with a provided value.
916+
917+
```javascript
918+
var Float64ArrayFE = fixedEndianFactory( 'float64' );
919+
920+
var arr = new Float64ArrayFE( 'little-endian', [ 1.0, 2.0, 3.0 ] );
921+
// returns <Float64ArrayFE>
922+
923+
var out = arr.with( 0, 0.0 );
924+
// returns <Float64ArrayFE>
925+
926+
var v = out.get( 0 );
927+
// returns 0.0
928+
```
929+
911930
</section>
912931

913932
<!-- /.usage -->
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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+':with', function benchmark( b ) {
36+
var values;
37+
var out;
38+
var arr;
39+
var i;
40+
41+
values = [
42+
1.0,
43+
2.0,
44+
3.0
45+
];
46+
arr = new Float64ArrayFE( 'little-endian', [ 1.0, 2.0, 2.0, 1.0 ] );
47+
48+
b.tic();
49+
for ( i = 0; i < b.iterations; i++ ) {
50+
out = arr.with( i % arr.length, values[ i % values.length ] );
51+
if ( typeof out !== 'object' ) {
52+
b.fail( 'should return an object' );
53+
}
54+
}
55+
b.toc();
56+
if ( !( out instanceof Float64ArrayFE ) ) {
57+
b.fail( 'should return a typed array' );
58+
}
59+
b.pass( 'benchmark finished' );
60+
b.end();
61+
});
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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 pow = require( '@stdlib/math/base/special/pow' );
25+
var zeroTo = require( '@stdlib/array/zero-to' );
26+
var pkg = require( './../package.json' ).name;
27+
var factory = require( './../lib' );
28+
29+
30+
// VARIABLES //
31+
32+
var Float64ArrayFE = factory( 'float64' );
33+
34+
35+
// FUNCTIONS //
36+
37+
/**
38+
* Creates a benchmark function.
39+
*
40+
* @private
41+
* @param {PositiveInteger} len - array length
42+
* @returns {Function} benchmark function
43+
*/
44+
function createBenchmark( len ) {
45+
var arr = new Float64ArrayFE( 'little-endian', zeroTo( len ) );
46+
return benchmark;
47+
48+
/**
49+
* Benchmark function.
50+
*
51+
* @private
52+
* @param {Benchmark} b - benchmark instance
53+
*/
54+
function benchmark( b ) {
55+
var values;
56+
var out;
57+
var i;
58+
59+
values = [
60+
1.0,
61+
2.0,
62+
3.0
63+
];
64+
65+
b.tic();
66+
for ( i = 0; i < b.iterations; i++ ) {
67+
out = arr.with( i % arr.length, values[ i % values.length ] );
68+
if ( typeof out !== 'object' ) {
69+
b.fail( 'should return an object' );
70+
}
71+
}
72+
b.toc();
73+
if ( !( out instanceof Float64ArrayFE ) ) {
74+
b.fail( 'should return a typed array' );
75+
}
76+
b.pass( 'benchmark finished' );
77+
b.end();
78+
}
79+
}
80+
81+
82+
// MAIN //
83+
84+
/**
85+
* Main execution sequence.
86+
*
87+
* @private
88+
*/
89+
function main() {
90+
var len;
91+
var min;
92+
var max;
93+
var f;
94+
var i;
95+
96+
min = 1; // 10^min
97+
max = 6; // 10^max
98+
99+
for ( i = min; i <= max; i++ ) {
100+
len = pow( 10, i );
101+
f = createBenchmark( len );
102+
bench( pkg+':with:len='+len, f );
103+
}
104+
}
105+
106+
main();

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

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
var isNonNegativeInteger = require( '@stdlib/assert/is-nonnegative-integer' ).isPrimitive;
2626
var isInteger = require( '@stdlib/assert/is-integer' ).isPrimitive;
27+
var isNumber = require( '@stdlib/assert/is-number' ).isPrimitive;
2728
var isCollection = require( '@stdlib/assert/is-collection' );
2829
var isArrayBuffer = require( '@stdlib/assert/is-arraybuffer' );
2930
var isObject = require( '@stdlib/assert/is-object' );
@@ -1059,22 +1060,62 @@ function factory( dtype ) { // eslint-disable-line max-lines-per-function, stdli
10591060
if ( !isTypedArray( this ) ) {
10601061
throw new TypeError( format( 'invalid invocation. `this` is not %s %s.', CHAR2ARTICLE[ dtype[0] ], CTOR_NAME ) );
10611062
}
1062-
10631063
if ( arguments.length > 0 ) {
10641064
sep = String( separator );
10651065
} else {
10661066
sep = ',';
10671067
}
1068-
10691068
out = [];
10701069
buf = this._buffer;
10711070
for ( i = 0; i < this._length; i++ ) {
10721071
out.push( buf[ GETTER ]( i * BYTES_PER_ELEMENT, this._isLE ) );
10731072
}
1074-
10751073
return out.join( sep );
10761074
});
10771075

1076+
/**
1077+
* Returns a new typed array with the element at a provided index replaced with a provided value.
1078+
*
1079+
* @name with
1080+
* @memberof TypedArray.prototype
1081+
* @type {Function}
1082+
* @param {integer} index - element index
1083+
* @param {number} value - new value
1084+
* @throws {TypeError} `this` must be a typed array instance
1085+
* @throws {TypeError} first argument must be an integer
1086+
* @throws {RangeError} index argument is out-of-bounds
1087+
* @throws {TypeError} second argument must be a number
1088+
* @returns {TypedArray} new typed array
1089+
*/
1090+
setReadOnly( TypedArray.prototype, 'with', function copyWith( index, value ) {
1091+
var outbuf;
1092+
var buf;
1093+
var out;
1094+
var len;
1095+
1096+
if ( !isTypedArray( this ) ) {
1097+
throw new TypeError( format( 'invalid invocation. `this` is not %s %s.', CHAR2ARTICLE[ dtype[0] ], CTOR_NAME ) );
1098+
}
1099+
if ( !isInteger( index ) ) {
1100+
throw new TypeError( format( 'invalid argument. First argument must be an integer. Value: `%s`.', index ) );
1101+
}
1102+
len = this._length;
1103+
buf = this._buffer;
1104+
if ( index < 0 ) {
1105+
index += len;
1106+
}
1107+
if ( index < 0 || index >= len ) {
1108+
throw new RangeError( format( 'invalid argument. Index argument is out-of-bounds. Value: `%s`.', index ) );
1109+
}
1110+
if ( !isNumber( value ) ) {
1111+
throw new TypeError( format( 'invalid argument. Second argument must be a number. Value: `%s`.', value ) );
1112+
}
1113+
out = new this.constructor( flag2byteOrder( this._isLE ), buf.buffer );
1114+
outbuf = out._buffer; // eslint-disable-line no-underscore-dangle
1115+
outbuf[ SETTER ]( index * BYTES_PER_ELEMENT, value, this._isLE );
1116+
return out;
1117+
});
1118+
10781119
return TypedArray;
10791120

10801121
/**

0 commit comments

Comments
 (0)