Skip to content

Commit 58ffc76

Browse files
committed
feat: add array/put
1 parent ca3bd75 commit 58ffc76

File tree

14 files changed

+2070
-0
lines changed

14 files changed

+2070
-0
lines changed
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2024 The Stdlib Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
21+
# put
22+
23+
> Replace specified elements of an array with provided values.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var put = require( '@stdlib/array/put' );
31+
```
32+
33+
#### put( x, indices, values\[, options] )
34+
35+
Replaces specified elements of an array with provided values.
36+
37+
```javascript
38+
var x = [ 1, 2, 3, 4 ];
39+
40+
var out = put( x, [ 1, 3 ], [ 20, 40 ] );
41+
// returns [ 1, 20, 3, 40 ]
42+
43+
var bool = ( out === x );
44+
// returns true
45+
```
46+
47+
The function supports the following parameters:
48+
49+
- **x**: input array.
50+
- **indices**: list of indices.
51+
- **values**: values to set. When `indices` contains one or more elements, `values` must be broadcast [compatible][@stdlib/ndarray/base/broadcast-shapes] with `indices` (i.e., must have either one element or the same number of elements as `indices`).
52+
- **options**: function options.
53+
54+
The function supports the following options:
55+
56+
- **mode**: index [mode][@stdlib/ndarray/base/ind]. Default: `'normalize'`.
57+
58+
If `indices` is an empty array, the function returns the input array unchanged.
59+
60+
```javascript
61+
var x = [ 1, 2, 3, 4 ];
62+
63+
var out = put( x, [], [ 20, 40 ] );
64+
// returns [ 1, 2, 3, 4 ]
65+
```
66+
67+
The function supports broadcasting a `values` array containing a single element against an `indices` array containing one or more elements.
68+
69+
```javascript
70+
var x = [ 1, 2, 3, 4 ];
71+
72+
var out = put( x, [ 1, 3 ], [ 20 ] );
73+
// returns [ 1, 20, 3, 20 ]
74+
```
75+
76+
By default, the function normalizes negative integer indices to positive integer index equivalents.
77+
78+
```javascript
79+
var x = [ 1, 2, 3, 4 ];
80+
81+
var out = put( x, [ -3, -1 ], [ 20, 40 ] );
82+
// returns [ 1, 20, 3, 40 ]
83+
```
84+
85+
To specify an alternative index [mode][@stdlib/ndarray/base/ind], provide a `mode` option.
86+
87+
```javascript
88+
var x = [ 1, 2, 3, 4 ];
89+
90+
var out = put( x, [ -10, 10 ], [ 20, 40 ], {
91+
'mode': 'clamp'
92+
});
93+
// returns [ 20, 2, 3, 40 ]
94+
```
95+
96+
</section>
97+
98+
<!-- /.usage -->
99+
100+
<section class="notes">
101+
102+
## Notes
103+
104+
- The function mutates the input array `x`.
105+
- The `values` array must have a [data type][@stdlib/array/dtypes] which can be [safely cast][@stdlib/array/safe-casts] to the input array data type. Floating-point data types (both real and complex) are allowed to downcast to a lower precision data type of the [same kind][@stdlib/array/same-kind-casts] (e.g., element values from a `'float64'` values array can be assigned to corresponding elements in a `'float32'` input array).
106+
107+
</section>
108+
109+
<!-- /.notes -->
110+
111+
<section class="examples">
112+
113+
## Examples
114+
115+
<!-- eslint no-undef: "error" -->
116+
117+
```javascript
118+
var filledBy = require( '@stdlib/array/base/filled-by' );
119+
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' );
120+
var linspace = require( '@stdlib/array/base/linspace' );
121+
var put = require( '@stdlib/array/put' );
122+
123+
// Generate a linearly spaced array:
124+
var x = linspace( 0, 100, 11 );
125+
console.log( x );
126+
127+
// Generate an array of random indices:
128+
var N = discreteUniform( 5, 15 );
129+
var indices = filledBy( N, discreteUniform.factory( 0, x.length-1 ) );
130+
console.log( indices );
131+
132+
// Generate an array of random values:
133+
var values = filledBy( N, discreteUniform.factory( 1000, 2000 ) );
134+
console.log( values );
135+
136+
// Update a random sample of elements in `x`:
137+
var out = put( x, indices, values );
138+
console.log( out );
139+
```
140+
141+
</section>
142+
143+
<!-- /.examples -->
144+
145+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
146+
147+
<section class="related">
148+
149+
</section>
150+
151+
<!-- /.related -->
152+
153+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
154+
155+
<section class="links">
156+
157+
[@stdlib/ndarray/base/ind]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/ind
158+
159+
[@stdlib/ndarray/base/broadcast-shapes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/broadcast-shapes
160+
161+
[@stdlib/array/dtypes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/dtypes
162+
163+
[@stdlib/array/safe-casts]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/safe-casts
164+
165+
[@stdlib/array/same-kind-casts]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/same-kind-casts
166+
167+
</section>
168+
169+
<!-- /.links -->
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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 isArray = require( '@stdlib/assert/is-array' );
25+
var zeroTo = require( '@stdlib/array/base/zero-to' );
26+
var pkg = require( './../package.json' ).name;
27+
var put = require( './../lib' );
28+
29+
30+
// MAIN //
31+
32+
bench( pkg+'::no_broadcasting:len=100', function benchmark( b ) {
33+
var idx;
34+
var x;
35+
var i;
36+
var v;
37+
38+
x = zeroTo( 100 );
39+
idx = zeroTo( 100 );
40+
41+
b.tic();
42+
for ( i = 0; i < b.iterations; i++ ) {
43+
v = put( x, idx, x );
44+
if ( typeof v !== 'object' ) {
45+
b.fail( 'should return an array' );
46+
}
47+
}
48+
b.toc();
49+
if ( !isArray( v ) ) {
50+
b.fail( 'should return an array' );
51+
}
52+
b.pass( 'benchmark finished' );
53+
b.end();
54+
});
55+
56+
bench( pkg+'::broadcasting:len=100', function benchmark( b ) {
57+
var idx;
58+
var x;
59+
var i;
60+
var v;
61+
62+
x = zeroTo( 100 );
63+
idx = zeroTo( 100 );
64+
65+
b.tic();
66+
for ( i = 0; i < b.iterations; i++ ) {
67+
v = put( x, idx, [ i ] );
68+
if ( typeof v !== 'object' ) {
69+
b.fail( 'should return an array' );
70+
}
71+
}
72+
b.toc();
73+
if ( !isArray( v ) ) {
74+
b.fail( 'should return an array' );
75+
}
76+
b.pass( 'benchmark finished' );
77+
b.end();
78+
});
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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 discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
26+
var zeroTo = require( '@stdlib/array/base/zero-to' );
27+
var isArray = require( '@stdlib/assert/is-array' );
28+
var pkg = require( './../package.json' ).name;
29+
var put = require( './../lib' );
30+
31+
32+
// VARIABLES //
33+
34+
var opts = {
35+
'dtype': 'generic'
36+
};
37+
38+
39+
// FUNCTIONS //
40+
41+
/**
42+
* Creates a benchmark function.
43+
*
44+
* @private
45+
* @param {PositiveInteger} len - array length
46+
* @returns {Function} benchmark function
47+
*/
48+
function createBenchmark( len ) {
49+
var values;
50+
var idx;
51+
var x;
52+
53+
x = discreteUniform( len, 0, 10, opts );
54+
idx = zeroTo( len );
55+
values = [
56+
discreteUniform( len, -10, 0, opts ),
57+
discreteUniform( len, 0, 10, opts )
58+
];
59+
60+
return benchmark;
61+
62+
/**
63+
* Benchmark function.
64+
*
65+
* @private
66+
* @param {Benchmark} b - benchmark instance
67+
*/
68+
function benchmark( b ) {
69+
var v;
70+
var i;
71+
72+
b.tic();
73+
for ( i = 0; i < b.iterations; i++ ) {
74+
v = put( x, idx, values[ i%values.length ] );
75+
if ( typeof v !== 'object' ) {
76+
b.fail( 'should return an array' );
77+
}
78+
}
79+
b.toc();
80+
if ( !isArray( v ) ) {
81+
b.fail( 'should return an array' );
82+
}
83+
b.pass( 'benchmark finished' );
84+
b.end();
85+
}
86+
}
87+
88+
89+
// MAIN //
90+
91+
/**
92+
* Main execution sequence.
93+
*
94+
* @private
95+
*/
96+
function main() {
97+
var len;
98+
var min;
99+
var max;
100+
var f;
101+
var i;
102+
103+
min = 1; // 10^min
104+
max = 6; // 10^max
105+
106+
for ( i = min; i <= max; i++ ) {
107+
len = pow( 10, i );
108+
f = createBenchmark( len );
109+
bench( pkg+':len='+len, f );
110+
}
111+
}
112+
113+
main();

0 commit comments

Comments
 (0)