-
-
Notifications
You must be signed in to change notification settings - Fork 828
feat: add array/base/to-inserted-at
#6807
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ShabiShett07
wants to merge
18
commits into
stdlib-js:develop
Choose a base branch
from
ShabiShett07:feature/to-inserted-at
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 7 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
af9eb61
feat: add array/base/to-inserted-at
ShabiShett07 5775b3b
docs: add implementations
ShabiShett07 7a03202
docs: complete implementations
ShabiShett07 45fecc3
chore: update benchmark
ShabiShett07 7205464
chore: update package.json
ShabiShett07 ec6dcd1
chore: update copyright years
stdlib-bot 2153a1f
chore: update repl.txt
ShabiShett07 341eb66
chore: add suggestions
ShabiShett07 c22c0f5
chore: add suggestions
ShabiShett07 128a273
chore: add suggestions
ShabiShett07 ae6c33f
chore: add suggestions
ShabiShett07 641df0f
chore: add suggestions
ShabiShett07 f9bd4d6
chore: add suggestions
ShabiShett07 94fb9ce
chore: add suggestions
ShabiShett07 609d9e9
fix: indexing
ShabiShett07 d830862
chore: update jsdoc
ShabiShett07 d3d06cf
chore: update examples
ShabiShett07 4e27cee
chore: update examples
ShabiShett07 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
161 changes: 161 additions & 0 deletions
161
lib/node_modules/@stdlib/array/base/to-inserted-at/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
<!-- | ||
|
||
@license Apache-2.0 | ||
|
||
Copyright (c) 2025 The Stdlib Authors. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
|
||
--> | ||
|
||
# array2InsertedAt | ||
|
||
> Return a new array with the element at the specified index inserted with a provided value. | ||
ShabiShett07 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. --> | ||
|
||
<section class="intro"> | ||
|
||
</section> | ||
|
||
<!-- /.intro --> | ||
|
||
<!-- Package usage documentation. --> | ||
|
||
<section class="usage"> | ||
|
||
## Usage | ||
|
||
```javascript | ||
var array2InsertedAt = require( '@stdlib/array/base/to-inserted-at' ); | ||
``` | ||
|
||
#### array2InsertedAt( x, index, value ) | ||
|
||
Returns a new array with the element at the specified index inserted with a provided value. | ||
|
||
```javascript | ||
var x = [ 1, 2, 3, 4 ]; | ||
|
||
var out = array2InsertedAt( x, 0, 5 ); | ||
// returns [ 5, 1, 2, 3, 4 ] | ||
|
||
out = array2InsertedAt( x, -1, 6 ); | ||
// returns [ 1, 2, 3, 6, 4 ] | ||
``` | ||
|
||
The function accepts the following arguments: | ||
|
||
- **x**: an input array. | ||
- **index**: element index. | ||
- **value**: inserting value. | ||
|
||
### array2InsertedAt.assign( x, index, value, out, stride, offset ) | ||
|
||
Copies elements from one array to another array and sets the element at the specified index to a provided value. | ||
|
||
```javascript | ||
var x = [ 1, 2, 3, 4 ]; | ||
|
||
var out = [ 0, 0, 0, 0 ]; | ||
ShabiShett07 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
var arr = array2InsertedAt.assign( x, 0, 5, out, 1, 0 ); | ||
// returns [ 5, 1, 2, 3, 4 ] | ||
|
||
var bool = ( arr === out ); | ||
// returns true | ||
``` | ||
|
||
The function accepts the following arguments: | ||
|
||
- **x**: an input array. | ||
- **index**: element index. | ||
- **value**: inserting value. | ||
- **out**: output array. | ||
- **stride**: output array stride. | ||
- **offset**: output array offset. | ||
|
||
</section> | ||
|
||
<!-- /.usage --> | ||
|
||
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> | ||
|
||
<section class="notes"> | ||
|
||
## Notes | ||
|
||
- Negative indices are resolved relative to the last array element, with the last element corresponding to `-1`. | ||
|
||
|
||
ShabiShett07 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
</section> | ||
|
||
<!-- /.notes --> | ||
|
||
<!-- Package usage examples. --> | ||
|
||
<section class="examples"> | ||
|
||
## Examples | ||
|
||
<!-- eslint no-undef: "error" --> | ||
|
||
```javascript | ||
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); | ||
var array2InsertedAt = require( '@stdlib/array/base/to-inserted-at' ); | ||
|
||
// Define an array: | ||
var opts = { | ||
'dtype': 'generic' | ||
}; | ||
var x = discreteUniform( 5, -100, 100, opts ); | ||
|
||
// Define an array containing random index values: | ||
var indices = discreteUniform( 100, -x.length, x.length-1, opts ); | ||
|
||
// Define an array with random values to set: | ||
var values = discreteUniform( indices.length, -10000, 10000, opts ); | ||
|
||
// Randomly set elements in the input array: | ||
var i; | ||
for ( i = 0; i < indices.length; i++ ) { | ||
console.log( 'x = [%s]', array2InsertedAt( x, indices[ i ], values[ i ] ).join( ',' ) ); | ||
} | ||
``` | ||
|
||
</section> | ||
|
||
<!-- /.examples --> | ||
|
||
<!-- 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. --> | ||
|
||
<section class="references"> | ||
|
||
</section> | ||
|
||
<!-- /.references --> | ||
|
||
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> | ||
|
||
<section class="related"> | ||
|
||
</section> | ||
|
||
<!-- /.related --> | ||
|
||
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> | ||
|
||
<section class="links"> | ||
|
||
</section> | ||
|
||
<!-- /.links --> |
98 changes: 98 additions & 0 deletions
98
lib/node_modules/@stdlib/array/base/to-inserted-at/benchmark/benchmark.assign.length.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/** | ||
* @license Apache-2.0 | ||
* | ||
* Copyright (c) 2025 The Stdlib Authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
// MODULES // | ||
|
||
var bench = require( '@stdlib/bench' ); | ||
var pow = require( '@stdlib/math/base/special/pow' ); | ||
var isArray = require( '@stdlib/assert/is-array' ); | ||
var ones = require( '@stdlib/array/base/ones' ); | ||
var zeros = require( '@stdlib/array/base/zeros' ); | ||
var pkg = require( './../package.json' ).name; | ||
var array2InsertedAt = require( './../lib' ); | ||
|
||
|
||
// FUNCTIONS // | ||
|
||
/** | ||
* Creates a benchmark function. | ||
* | ||
* @private | ||
* @param {PositiveInteger} len - array length | ||
* @returns {Function} benchmark function | ||
*/ | ||
function createBenchmark( len ) { | ||
var out = zeros( len ); | ||
var x = ones( len ); | ||
return benchmark; | ||
|
||
/** | ||
* Benchmark function. | ||
* | ||
* @private | ||
* @param {Benchmark} b - benchmark instance | ||
*/ | ||
function benchmark( b ) { | ||
var v; | ||
var i; | ||
|
||
b.tic(); | ||
for ( i = 0; i < b.iterations; i++ ) { | ||
v = array2InsertedAt.assign( x, i%len, i, out, 1, 0 ); | ||
if ( typeof v !== 'object' ) { | ||
b.fail( 'should return an array' ); | ||
} | ||
} | ||
b.toc(); | ||
if ( !isArray( v ) ) { | ||
b.fail( 'should return an array' ); | ||
} | ||
b.pass( 'benchmark finished' ); | ||
b.end(); | ||
} | ||
} | ||
|
||
|
||
// MAIN // | ||
|
||
/** | ||
* Main execution sequence. | ||
* | ||
* @private | ||
*/ | ||
function main() { | ||
var len; | ||
var min; | ||
var max; | ||
var f; | ||
var i; | ||
|
||
min = 1; // 10^min | ||
max = 6; // 10^max | ||
|
||
for ( i = min; i <= max; i++ ) { | ||
len = pow( 10, i ); | ||
|
||
f = createBenchmark( len ); | ||
bench( pkg+':assign:dtype=generic,len='+len, f ); | ||
} | ||
} | ||
|
||
main(); |
96 changes: 96 additions & 0 deletions
96
lib/node_modules/@stdlib/array/base/to-inserted-at/benchmark/benchmark.length.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/** | ||
* @license Apache-2.0 | ||
* | ||
* Copyright (c) 2025 The Stdlib Authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
// MODULES // | ||
|
||
var bench = require( '@stdlib/bench' ); | ||
var pow = require( '@stdlib/math/base/special/pow' ); | ||
var isArray = require( '@stdlib/assert/is-array' ); | ||
var ones = require( '@stdlib/array/base/ones' ); | ||
var pkg = require( './../package.json' ).name; | ||
var array2InsertedAt = require( './../lib' ); | ||
|
||
|
||
// FUNCTIONS // | ||
|
||
/** | ||
* Creates a benchmark function. | ||
* | ||
* @private | ||
* @param {PositiveInteger} len - array length | ||
* @returns {Function} benchmark function | ||
*/ | ||
function createBenchmark( len ) { | ||
var x = ones( len ); | ||
return benchmark; | ||
|
||
/** | ||
* Benchmark function. | ||
* | ||
* @private | ||
* @param {Benchmark} b - benchmark instance | ||
*/ | ||
function benchmark( b ) { | ||
var out; | ||
var i; | ||
|
||
b.tic(); | ||
for ( i = 0; i < b.iterations; i++ ) { | ||
out = array2InsertedAt( x, i%len, i ); | ||
ShabiShett07 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if ( out.length !== len + 1 ) { | ||
b.fail( 'unexpected length' ); | ||
} | ||
} | ||
b.toc(); | ||
if ( !isArray( out ) ) { | ||
b.fail( 'should return an array' ); | ||
} | ||
b.pass( 'benchmark finished' ); | ||
b.end(); | ||
} | ||
} | ||
|
||
|
||
// MAIN // | ||
|
||
/** | ||
* Main execution sequence. | ||
* | ||
* @private | ||
*/ | ||
function main() { | ||
var len; | ||
var min; | ||
var max; | ||
var f; | ||
var i; | ||
|
||
min = 1; // 10^min | ||
max = 6; // 10^max | ||
|
||
for ( i = min; i <= max; i++ ) { | ||
len = pow( 10, i ); | ||
|
||
f = createBenchmark( len ); | ||
bench( pkg+':dtype=generic,len='+len, f ); | ||
} | ||
} | ||
|
||
main(); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.