Skip to content

Commit 7a03202

Browse files
committed
docs: complete implementations
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: passed - task: lint_repl_help status: na - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: na - task: lint_typescript_tests status: na - task: lint_license_headers status: passed ---
1 parent 5775b3b commit 7a03202

File tree

10 files changed

+1213
-30
lines changed

10 files changed

+1213
-30
lines changed
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
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+
# array2InsertedAt
22+
23+
> Return a new array with the element at the specified index inserted with a provided value.
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 array2InsertedAt = require( '@stdlib/array/base/to-inserted-at' );
41+
```
42+
43+
#### array2InsertedAt( x, index, value )
44+
45+
Returns a new array with the element at the specified index inserted with a provided value.
46+
47+
```javascript
48+
var x = [ 1, 2, 3, 4 ];
49+
50+
var out = array2InsertedAt( x, 0, 5 );
51+
// returns [ 5, 1, 2, 3, 4 ]
52+
53+
out = array2InsertedAt( x, -1, 6 );
54+
// returns [ 1, 2, 3, 6, 4 ]
55+
```
56+
57+
The function accepts the following arguments:
58+
59+
- **x**: an input array.
60+
- **index**: element index.
61+
- **value**: inserting value.
62+
63+
### array2InsertedAt.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 = array2InsertedAt.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**: inserting value.
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+
100+
</section>
101+
102+
<!-- /.notes -->
103+
104+
<!-- Package usage examples. -->
105+
106+
<section class="examples">
107+
108+
## Examples
109+
110+
<!-- eslint no-undef: "error" -->
111+
112+
```javascript
113+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
114+
var array2InsertedAt = require( '@stdlib/array/base/to-inserted-at' );
115+
116+
// Define an array:
117+
var opts = {
118+
'dtype': 'generic'
119+
};
120+
var x = discreteUniform( 5, -100, 100, opts );
121+
122+
// Define an array containing random index values:
123+
var indices = discreteUniform( 100, -x.length, x.length-1, opts );
124+
125+
// Define an array with random values to set:
126+
var values = discreteUniform( indices.length, -10000, 10000, opts );
127+
128+
// Randomly set elements in the input array:
129+
var i;
130+
for ( i = 0; i < indices.length; i++ ) {
131+
console.log( 'x = [%s]', array2InsertedAt( x, indices[ i ], values[ i ] ).join( ',' ) );
132+
}
133+
```
134+
135+
</section>
136+
137+
<!-- /.examples -->
138+
139+
<!-- 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. -->
140+
141+
<section class="references">
142+
143+
</section>
144+
145+
<!-- /.references -->
146+
147+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
148+
149+
<section class="related">
150+
151+
</section>
152+
153+
<!-- /.related -->
154+
155+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
156+
157+
<section class="links">
158+
159+
</section>
160+
161+
<!-- /.links -->

lib/node_modules/@stdlib/array/base/to-inserted-at/benchmark/benchmark.assign.length.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ var isArray = require( '@stdlib/assert/is-array' );
2626
var ones = require( '@stdlib/array/base/ones' );
2727
var zeros = require( '@stdlib/array/base/zeros' );
2828
var pkg = require( './../package.json' ).name;
29-
var array2insertedAt = require( './../lib' );
29+
var array2InsertedAt = require( './../lib' );
3030

3131

3232
// FUNCTIONS //
@@ -55,7 +55,7 @@ function createBenchmark( len ) {
5555

5656
b.tic();
5757
for ( i = 0; i < b.iterations; i++ ) {
58-
v = array2insertedAt.assign( x, i%len, i, out, 1, 0 );
58+
v = array2InsertedAt.assign( x, i%len, i, out, 1, 0 );
5959
if ( typeof v !== 'object' ) {
6060
b.fail( 'should return an array' );
6161
}

lib/node_modules/@stdlib/array/base/to-inserted-at/benchmark/benchmark.length.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ var pow = require( '@stdlib/math/base/special/pow' );
2525
var isArray = require( '@stdlib/assert/is-array' );
2626
var ones = require( '@stdlib/array/base/ones' );
2727
var pkg = require( './../package.json' ).name;
28-
var array2insertedAt = require( './../lib' );
28+
var array2InsertedAt = require( './../lib' );
2929

3030

3131
// FUNCTIONS //
@@ -53,7 +53,7 @@ function createBenchmark( len ) {
5353

5454
b.tic();
5555
for ( i = 0; i < b.iterations; i++ ) {
56-
out = array2insertedAt( x, i%len, i );
56+
out = array2InsertedAt( x, i%len, i );
5757
if ( out.length !== len ) {
5858
b.fail( 'unexpected length' );
5959
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
22+
var array2InsertedAt = require( './../lib' );
23+
24+
// Define an array:
25+
var opts = {
26+
'dtype': 'generic'
27+
};
28+
var x = discreteUniform( 5, -100, 100, opts );
29+
30+
// Define an array containing random index values:
31+
var indices = discreteUniform( 100, -x.length, x.length-1, opts );
32+
33+
// Define an array with random values to set:
34+
var values = discreteUniform( indices.length, -10000, 10000, opts );
35+
36+
// Randomly set elements in the input array:
37+
var i;
38+
for ( i = 0; i < indices.length; i++ ) {
39+
console.log( 'x = [%s]', array2InsertedAt( x, indices[ i ], values[ i ] ).join( ',' ) );
40+
}

lib/node_modules/@stdlib/array/base/to-inserted-at/lib/assign.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ function accessors( x, index, value, out, stride, offset ) {
102102
var xdata;
103103
var odata;
104104
var xget;
105+
var oget;
105106
var oset;
106107
var io;
107108
var i;
@@ -110,6 +111,7 @@ function accessors( x, index, value, out, stride, offset ) {
110111
odata = out.data;
111112

112113
xget = x.accessors[ 0 ];
114+
oget = out.accessors[ 0 ];
113115
oset = out.accessors[ 1 ];
114116

115117
io = offset;
@@ -118,7 +120,7 @@ function accessors( x, index, value, out, stride, offset ) {
118120
io += stride;
119121
}
120122
for ( i = xdata.length - 1; i >= index; i-- ) {
121-
oset( odata, offset + ((i + 1) * stride), xget( odata, offset + (i * stride) ) );
123+
oset( odata, offset + ((i + 1) * stride), oget( odata, offset + (i * stride) ) );
122124
}
123125

124126
oset( odata, offset + (index * stride), value );

lib/node_modules/@stdlib/array/base/to-inserted-at/lib/main.js

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -27,29 +27,6 @@ var format = require( '@stdlib/string/format' );
2727
var assign = require( './assign.js' );
2828

2929

30-
// FUNCTIONS //
31-
32-
/**
33-
* Tests whether an object has a specified method.
34-
*
35-
* @private
36-
* @param {Object} obj - input object
37-
* @param {string} method - method name
38-
* @returns {boolean} boolean indicating whether an object has a specified method
39-
*
40-
* @example
41-
* var bool = hasMethod( [], 'map' );
42-
* // returns true
43-
*
44-
* @example
45-
* var bool = hasMethod( [], 'beep' );
46-
* // returns false
47-
*/
48-
function hasMethod( obj, method ) {
49-
return ( typeof obj[ method ] === 'function' );
50-
}
51-
52-
5330
// MAIN //
5431

5532
/**
@@ -71,7 +48,7 @@ function hasMethod( obj, method ) {
7148
* // returns [ 1, 6, 2, 3, 4 ]
7249
*
7350
* v = array2InsertedAt( x, -2, 7 );
74-
* // returns [ 1, 2, 3, 7, 4 ]
51+
* // returns [ 1, 2, 7, 3, 4 ]
7552
*/
7653
function array2InsertedAt( x, index, value ) {
7754
var out;
@@ -80,7 +57,7 @@ function array2InsertedAt( x, index, value ) {
8057
if ( index < 0 ) {
8158
throw new RangeError( format( 'invalid argument. Index argument is out-of-bounds. Value: `%d`.', index ) );
8259
}
83-
out = zeros( x.length-1, dtype( x ) || 'generic' );
60+
out = zeros( x.length+1, dtype( x ) || 'generic' );
8461
assign( x, index, value, out, 1, 0 );
8562
return out;
8663
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
{
2+
"name": "@stdlib/array/base/to-inserted-at",
3+
"version": "0.0.0",
4+
"description": "Return a new array with the element at the specified index inserted with a provided value.",
5+
"license": "Apache-2.0",
6+
"author": {
7+
"name": "The Stdlib Authors",
8+
"url": "https://github.com/stdlib-js/stdlib/graphs/contributors"
9+
},
10+
"contributors": [
11+
{
12+
"name": "The Stdlib Authors",
13+
"url": "https://github.com/stdlib-js/stdlib/graphs/contributors"
14+
}
15+
],
16+
"main": "./lib",
17+
"directories": {
18+
"benchmark": "./benchmark",
19+
"doc": "./docs",
20+
"example": "./examples",
21+
"lib": "./lib",
22+
"test": "./test"
23+
},
24+
"types": "./docs/types",
25+
"scripts": {},
26+
"homepage": "https://github.com/stdlib-js/stdlib",
27+
"repository": {
28+
"type": "git",
29+
"url": "git://github.com/stdlib-js/stdlib.git"
30+
},
31+
"bugs": {
32+
"url": "https://github.com/stdlib-js/stdlib/issues"
33+
},
34+
"dependencies": {},
35+
"devDependencies": {},
36+
"engines": {
37+
"node": ">=0.10.0",
38+
"npm": ">2.7.0"
39+
},
40+
"os": [
41+
"aix",
42+
"darwin",
43+
"freebsd",
44+
"linux",
45+
"macos",
46+
"openbsd",
47+
"sunos",
48+
"win32",
49+
"windows"
50+
],
51+
"keywords": [
52+
"stdlib",
53+
"base",
54+
"array",
55+
"typed",
56+
"collection",
57+
"vector",
58+
"inserted",
59+
"insert",
60+
"inserter",
61+
"copy"
62+
],
63+
"__stdlib__": {}
64+
}

0 commit comments

Comments
 (0)