Skip to content

Commit 15c9b0e

Browse files
committed
feat: add support for assigning to an output array
1 parent 45bfdb6 commit 15c9b0e

File tree

12 files changed

+1565
-410
lines changed

12 files changed

+1565
-410
lines changed

lib/node_modules/@stdlib/array/base/with/README.md

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,37 @@ var out = arrayWith( x, 0, 5 );
5252

5353
out = arrayWith( x, -1, 6 );
5454
// returns [ 1, 2, 3, 6 ]
55+
```
56+
57+
The function accepts the following arguments:
58+
59+
- **x**: an input array.
60+
- **index**: element index.
61+
- **value**: replacement value.
62+
63+
### arrayWith.assign( x, index, value, out, stride, offset )
64+
65+
Copies elements from one array to another array and sets the element at the specified index to a provided value.
66+
67+
```javascript
68+
var x = [ 1, 2, 3, 4 ];
69+
70+
var out = [ 0, 0, 0, 0 ];
71+
var arr = arrayWith.assign( x, 0, 5, out, 1, 0 );
72+
// returns [ 5, 2, 3, 4 ]
5573

74+
var bool = ( arr === out );
75+
// returns true
5676
```
5777

5878
The function accepts the following arguments:
5979

6080
- **x**: an input array.
6181
- **index**: element index.
6282
- **value**: replacement value.
83+
- **out**: output array.
84+
- **stride**: output array stride.
85+
- **offset**: output array offset.
6386

6487
</section>
6588

@@ -71,13 +94,13 @@ The function accepts the following arguments:
7194

7295
## Notes
7396

74-
- If provided an array-like object having a `with` method, the function defers execution to that method and assumes that the method has the following signature:
97+
- If provided an array-like object having a `with` method, the `arrayWith` function defers execution to that method and assumes that the method has the following signature:
7598

7699
```text
77100
x.with( index, value )
78101
```
79102
80-
If provided an array-like object without a `with` method, the function shallow copies input array data to a new generic array, normalizes a provided index, and sets a specified element.
103+
If provided an array-like object without a `with` method, the `arrayWith` function shallow copies input array data to a new generic array, normalizes a provided index, and sets a specified element.
81104
82105
- Negative indices are resolved relative to the last array element, with the last element corresponding to `-1`.
83106
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) 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 isArray = require( '@stdlib/assert/is-array' );
26+
var ones = require( '@stdlib/array/base/ones' );
27+
var zeros = require( '@stdlib/array/base/zeros' );
28+
var pkg = require( './../package.json' ).name;
29+
var arrayWith = 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 out = zeros( len );
43+
var x = ones( len );
44+
return benchmark;
45+
46+
/**
47+
* Benchmark function.
48+
*
49+
* @private
50+
* @param {Benchmark} b - benchmark instance
51+
*/
52+
function benchmark( b ) {
53+
var v;
54+
var i;
55+
56+
b.tic();
57+
for ( i = 0; i < b.iterations; i++ ) {
58+
v = arrayWith.assign( x, i%len, i, out, 1, 0 );
59+
if ( typeof v !== 'object' ) {
60+
b.fail( 'should return an array' );
61+
}
62+
}
63+
b.toc();
64+
if ( !isArray( v ) ) {
65+
b.fail( 'should return an array' );
66+
}
67+
b.pass( 'benchmark finished' );
68+
b.end();
69+
}
70+
}
71+
72+
73+
// MAIN //
74+
75+
/**
76+
* Main execution sequence.
77+
*
78+
* @private
79+
*/
80+
function main() {
81+
var len;
82+
var min;
83+
var max;
84+
var f;
85+
var i;
86+
87+
min = 1; // 10^min
88+
max = 6; // 10^max
89+
90+
for ( i = min; i <= max; i++ ) {
91+
len = pow( 10, i );
92+
93+
f = createBenchmark( len );
94+
bench( pkg+':assign:dtype=generic,len='+len, f );
95+
}
96+
}
97+
98+
main();

lib/node_modules/@stdlib/array/base/with/docs/repl.txt

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,48 @@
4242
> x
4343
[ 1, 2, 3, 4 ]
4444

45+
46+
{{alias}}.assign( x, index, value, out, stride, offset )
47+
Copies elements from one array to another array and sets the element at the
48+
specified index to a provided value.
49+
50+
Negative indices are resolved relative to the last array element, with the
51+
last element corresponding to `-1`.
52+
53+
Parameters
54+
----------
55+
x: ArrayLikeObject
56+
Input array.
57+
58+
index: integer
59+
Index of the element to be replaced.
60+
61+
value: any
62+
Replacement value.
63+
64+
out: ArrayLikeObject
65+
Output array.
66+
67+
stride: integer
68+
Output array stride.
69+
70+
offset: integer
71+
Output array offset.
72+
73+
Returns
74+
-------
75+
out: ArrayLikeObject
76+
Output array.
77+
78+
Examples
79+
--------
80+
> var x = [ 1, 2, 3, 4 ];
81+
> var out = [ 0, 0, 0, 0 ];
82+
> var arr = {{alias}}.assign( x, 0, 5, out, 1, 0 )
83+
[ 5, 2, 3, 4 ]
84+
> var bool = ( arr === out )
85+
true
86+
4587
See Also
4688
--------
4789

0 commit comments

Comments
 (0)