Skip to content

Commit 77e0108

Browse files
ShabiShett07kgrytestdlib-bot
authored
feat: add array/base/to-inserted-at
PR-URL: #6807 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]> Co-authored-by: stdlib-bot <[email protected]>
1 parent 3cae960 commit 77e0108

File tree

14 files changed

+2316
-0
lines changed

14 files changed

+2316
-0
lines changed
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2025 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+
# toInsertedAt
22+
23+
> Return a new array containing every element from an input array and with a provided value inserted at a specified index.
24+
25+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
26+
27+
<section class="intro">
28+
29+
</section>
30+
31+
<!-- /.intro -->
32+
33+
<!-- Package usage documentation. -->
34+
35+
<section class="usage">
36+
37+
## Usage
38+
39+
```javascript
40+
var toInsertedAt = require( '@stdlib/array/base/to-inserted-at' );
41+
```
42+
43+
#### toInsertedAt( x, index, value )
44+
45+
Returns a new array containing every element from an input array and with a provided value inserted at a specified index.
46+
47+
```javascript
48+
var x = [ 1, 2, 3, 4 ];
49+
50+
var out = toInsertedAt( x, 0, 5 );
51+
// returns [ 5, 1, 2, 3, 4 ]
52+
53+
out = toInsertedAt( x, -1, 6 );
54+
// returns [ 1, 2, 3, 4, 6 ]
55+
```
56+
57+
The function accepts the following arguments:
58+
59+
- **x**: an input array.
60+
- **index**: element index.
61+
- **value**: value to insert.
62+
63+
### toInsertedAt.assign( x, index, value, out, stride, offset )
64+
65+
Copies elements from one array to another array and inserts a provided value at a specified index.
66+
67+
```javascript
68+
var x = [ 1, 2, 3, 4 ];
69+
70+
var out = [ 0, 0, 0, 0, 0 ];
71+
var arr = toInsertedAt.assign( x, 0, 5, out, 1, 0 );
72+
// returns [ 5, 1, 2, 3, 4 ]
73+
74+
var bool = ( arr === out );
75+
// returns true
76+
```
77+
78+
The function accepts the following arguments:
79+
80+
- **x**: an input array.
81+
- **index**: element index.
82+
- **value**: value to insert.
83+
- **out**: output array.
84+
- **stride**: output array stride.
85+
- **offset**: output array offset.
86+
87+
</section>
88+
89+
<!-- /.usage -->
90+
91+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
92+
93+
<section class="notes">
94+
95+
## Notes
96+
97+
- Negative indices are resolved relative to the last array element, with the last element corresponding to `-1`.
98+
99+
</section>
100+
101+
<!-- /.notes -->
102+
103+
<!-- Package usage examples. -->
104+
105+
<section class="examples">
106+
107+
## Examples
108+
109+
<!-- eslint no-undef: "error" -->
110+
111+
```javascript
112+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
113+
var toInsertedAt = require( '@stdlib/array/base/to-inserted-at' );
114+
115+
// Define an array:
116+
var opts = {
117+
'dtype': 'generic'
118+
};
119+
var x = discreteUniform( 5, -100, 100, opts );
120+
121+
// Define an array containing random index values:
122+
var indices = discreteUniform( 100, -x.length, x.length, opts );
123+
124+
// Define an array with random values to insert:
125+
var values = discreteUniform( indices.length, -10000, 10000, opts );
126+
127+
// Randomly insert elements in the input array:
128+
var i;
129+
for ( i = 0; i < indices.length; i++ ) {
130+
console.log( 'x = [%s]', toInsertedAt( x, indices[ i ], values[ i ] ).join( ',' ) );
131+
}
132+
```
133+
134+
</section>
135+
136+
<!-- /.examples -->
137+
138+
<!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
139+
140+
<section class="references">
141+
142+
</section>
143+
144+
<!-- /.references -->
145+
146+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
147+
148+
<section class="related">
149+
150+
</section>
151+
152+
<!-- /.related -->
153+
154+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
155+
156+
<section class="links">
157+
158+
</section>
159+
160+
<!-- /.links -->
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) 2025 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 toInsertedAt = 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+1 );
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 = toInsertedAt.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();
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 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 pkg = require( './../package.json' ).name;
28+
var toInsertedAt = require( './../lib' );
29+
30+
31+
// FUNCTIONS //
32+
33+
/**
34+
* Creates a benchmark function.
35+
*
36+
* @private
37+
* @param {PositiveInteger} len - array length
38+
* @returns {Function} benchmark function
39+
*/
40+
function createBenchmark( len ) {
41+
var x = ones( len );
42+
return benchmark;
43+
44+
/**
45+
* Benchmark function.
46+
*
47+
* @private
48+
* @param {Benchmark} b - benchmark instance
49+
*/
50+
function benchmark( b ) {
51+
var out;
52+
var i;
53+
54+
b.tic();
55+
for ( i = 0; i < b.iterations; i++ ) {
56+
out = toInsertedAt( x, i%len, i );
57+
if ( out.length !== len+1 ) {
58+
b.fail( 'unexpected length' );
59+
}
60+
}
61+
b.toc();
62+
if ( !isArray( out ) ) {
63+
b.fail( 'should return an array' );
64+
}
65+
b.pass( 'benchmark finished' );
66+
b.end();
67+
}
68+
}
69+
70+
71+
// MAIN //
72+
73+
/**
74+
* Main execution sequence.
75+
*
76+
* @private
77+
*/
78+
function main() {
79+
var len;
80+
var min;
81+
var max;
82+
var f;
83+
var i;
84+
85+
min = 1; // 10^min
86+
max = 6; // 10^max
87+
88+
for ( i = min; i <= max; i++ ) {
89+
len = pow( 10, i );
90+
91+
f = createBenchmark( len );
92+
bench( pkg+':dtype=generic,len='+len, f );
93+
}
94+
}
95+
96+
main();

0 commit comments

Comments
 (0)