Skip to content

Commit 82982e7

Browse files
committed
Add utility to copy the elements of an array-like object to a "generic" array
1 parent 4c2c959 commit 82982e7

File tree

10 files changed

+688
-0
lines changed

10 files changed

+688
-0
lines changed
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2022 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+
# copy
22+
23+
> Copy the elements of an array-like object to a new "generic" array.
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 copy = require( '@stdlib/array/base/copy' );
41+
```
42+
43+
#### copy( x )
44+
45+
Copies the elements of an array-like object to a new "generic" array.
46+
47+
```javascript
48+
var x = [ 1, 2, 3 ];
49+
50+
var out = copy( x );
51+
// returns [ 1, 2, 3 ]
52+
53+
var bool = ( out === x );
54+
// returns false
55+
```
56+
57+
</section>
58+
59+
<!-- /.usage -->
60+
61+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
62+
63+
<section class="notes">
64+
65+
</section>
66+
67+
<!-- /.notes -->
68+
69+
<!-- Package usage examples. -->
70+
71+
<section class="examples">
72+
73+
## Examples
74+
75+
<!-- eslint no-undef: "error" -->
76+
77+
```javascript
78+
var Complex64Array = require( '@stdlib/array/complex64' );
79+
var realf = require( '@stdlib/complex/realf' );
80+
var imagf = require( '@stdlib/complex/imagf' );
81+
var copy = require( '@stdlib/array/base/copy' );
82+
83+
// Create a complex number array:
84+
var arr = new Complex64Array( 10 );
85+
86+
// Copy elements to a generic array:
87+
var out = copy( arr );
88+
89+
// Retrieve the first element:
90+
var z = out[ 0 ];
91+
// returns <Complex64>
92+
93+
var re = realf( z );
94+
// returns 0.0
95+
96+
var im = imagf( z );
97+
// returns 0.0
98+
99+
console.log( '%d + %di', re, im );
100+
// => '0 + 0i'
101+
```
102+
103+
</section>
104+
105+
<!-- /.examples -->
106+
107+
<!-- 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. -->
108+
109+
<section class="references">
110+
111+
</section>
112+
113+
<!-- /.references -->
114+
115+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
116+
117+
<section class="related">
118+
119+
</section>
120+
121+
<!-- /.related -->
122+
123+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
124+
125+
<section class="links">
126+
127+
</section>
128+
129+
<!-- /.links -->
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) 2022 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 copy = 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 = copy( x );
57+
if ( out.length !== len ) {
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+':len='+len, f );
93+
}
94+
}
95+
96+
main();
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
{{alias}}( x )
3+
Copies the elements of an array-like object to a new "generic" array.
4+
5+
Parameters
6+
----------
7+
x: ArrayLikeObject
8+
Input array.
9+
10+
Returns
11+
-------
12+
out: Array
13+
Output array.
14+
15+
Examples
16+
--------
17+
> var out = {{alias}}( [ 1, 2, 3 ] )
18+
[ 1, 2, 3 ]
19+
20+
See Also
21+
--------
22+
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2022 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+
// TypeScript Version: 2.0
20+
21+
/// <reference types="@stdlib/types"/>
22+
23+
import { Collection } from '@stdlib/types/object';
24+
25+
/**
26+
* Copies the elements of an array-like object to a new "generic" array.
27+
*
28+
* @param x - array length
29+
* @returns output array
30+
*
31+
* @example
32+
* var x = [ 1, 2, 3 ];
33+
*
34+
* var out = copy( x );
35+
* // returns [ 1, 2, 3 ]
36+
*/
37+
declare function copy( x: Collection ): Array<any>;
38+
39+
40+
// EXPORTS //
41+
42+
export = copy;
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2022 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+
import copy = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns an array...
25+
{
26+
copy( [ 1, 2, 3 ] ); // $ExpectType any[]
27+
}
28+
29+
// The compiler throws an error if the function is provided an argument which is not a collection...
30+
{
31+
copy( 5 ); // $ExpectError
32+
copy( true ); // $ExpectError
33+
copy( false ); // $ExpectError
34+
copy( null ); // $ExpectError
35+
copy( {} ); // $ExpectError
36+
}
37+
38+
// The compiler throws an error if the function is provided an unsupported number of arguments...
39+
{
40+
copy(); // $ExpectError
41+
copy( [ 1, 2, 3 ], 2 ); // $ExpectError
42+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2022 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 Complex64Array = require( '@stdlib/array/complex64' );
22+
var realf = require( '@stdlib/complex/realf' );
23+
var imagf = require( '@stdlib/complex/imagf' );
24+
var copy = require( './../lib' );
25+
26+
// Create a complex number array:
27+
var arr = new Complex64Array( 10 );
28+
29+
// Copy elements to a generic array:
30+
var out = copy( arr );
31+
32+
// Retrieve the first element:
33+
var z = out[ 0 ];
34+
// returns <Complex64>
35+
36+
var re = realf( z );
37+
// returns 0.0
38+
39+
var im = imagf( z );
40+
// returns 0.0
41+
42+
console.log( '%d + %di', re, im );
43+
// => '0 + 0i'

0 commit comments

Comments
 (0)