Skip to content
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

feat: add functional equivalent of Array.prototype.copyWithin and TypedArray.prototype.copyWithin #367

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 131 additions & 0 deletions lib/node_modules/@stdlib/utils/copy-within/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
<!--

@license Apache-2.0

Copyright (c) 2020 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.

-->

# copyWithin

> Copy a part of a collection to another location in the same collection.

<!-- 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 copyWithin = require( '@stdlib/utils/copy-within' );
```

#### copyWithin( collection, target\[, start\[, end\]\] )

Copies a part of a `collection` to another location in the same `collection`. A `collection` may be either an [`Array`][mdn-array], [`Typed Array`][mdn-typed-array], or an array-like [`Object`][mdn-object] (i.e., an [`Object`][mdn-object] having a valid writable `length` property).

```javascript
var arr = [ 1, 2, 3, 4, 5 ];

var out = copyWithin( arr, 0, 2, 4 );
// returns [ 3, 4, 3, 4, 5 ]

var bool = ( out === arr );
// returns true
```

</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">


</section>

<!-- /.notes -->

<!-- Package usage examples. -->

<section class="examples">

## Examples

<!-- eslint no-undef: "error" -->

```javascript
var Float64Array= require( '@stdlib/array/float64' );
var copyWithin = require( '@stdlib/utils/copy-within' );

var bool;
var arr;
var out;

arr = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] );
out = copyWithin( arr, 0, 2, 4 );
// returns <Float64Array>[ 3.0, 4.0, 3.0, 4.0, 5.0 ]

bool = ( out === arr );
// returns true

arr = [ 1, 2, 3, 4, 5 ];
out = copyWithin( arr, 0, 3, 4 );
// returns [ 4, 2, 3, 4, 5 ]

bool = ( out === arr );
// returns true

out = copyWithin( arr, 1, 3 );
// returns [ 4, 4, 5, 4, 5 ]

bool = ( out === arr );
// returns true
```

</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 all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="links">

[mdn-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array

[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Typed_arrays

[mdn-object]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object

</section>

<!-- /.links -->
53 changes: 53 additions & 0 deletions lib/node_modules/@stdlib/utils/copy-within/benchmark/benchmark.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2020 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 isCollection = require( '@stdlib/assert/is-collection' );
var pkg = require( './../package.json' ).name;
var copyWithin = require( './../lib' );


// MAIN //

bench( pkg+'::array', function benchmark( b ) {
var arr;
var i;

arr = [];
for ( i = 0; i < 10; i++ ) {
arr.push( i );
}

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
arr = copyWithin( arr, i % 10 );
if ( !isCollection( arr ) ) {
b.fail( 'should return an array-like object' );
}
}
b.toc();
if ( !isCollection( arr ) ) {
b.fail( 'should return an array-like object' );
}
b.pass( 'benchmark finished' );
b.end();
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2020 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 isCollection = require( '@stdlib/assert/is-collection' );
var typedarray = require( '@stdlib/array/typed' );
var pkg = require( './../package.json' ).name;
var copyWithin = require( './../lib' );


// FUNCTIONS //

/**
* Creates a benchmark function.
*
* @private
* @param {PositiveInteger} len - array length
* @param {string} dtype - data type
* @returns {Function} benchmark function
*/
function createBenchmark( len, dtype ) {
var arr;
var i;

arr = [];
for ( i = 0; i < len; i++ ) {
arr[ i ] = i;
}
arr = typedarray( arr, dtype );
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 = copyWithin( arr, i%len );
if ( !isCollection( out ) ) {
b.fail( 'should return an array-like object' );
}
}
b.toc();
if ( !isCollection( out ) ) {
b.fail( 'should return an array-like object' );
}
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, 'float64' );
bench( pkg+':len='+len+',dtype=float64', f );

f = createBenchmark( len, 'float32' );
bench( pkg+':len='+len+',dtype=float32', f );

f = createBenchmark( len, 'int32' );
bench( pkg+':len='+len+',dtype=int32', f );

f = createBenchmark( len, 'int16' );
bench( pkg+':len='+len+',dtype=int16', f );

f = createBenchmark( len, 'int8' );
bench( pkg+':len='+len+',dtype=int8', f );

f = createBenchmark( len, 'uint32' );
bench( pkg+':len='+len+',dtype=uint32', f );

f = createBenchmark( len, 'uint16' );
bench( pkg+':len='+len+',dtype=uint16', f );

f = createBenchmark( len, 'uint8' );
bench( pkg+':len='+len+',dtype=uint8', f );

f = createBenchmark( len, 'uint8c' );
bench( pkg+':len='+len+',dtype=uint8c', f );
}
}

main();
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2020 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 isCollection = require( '@stdlib/assert/is-collection' );
var pkg = require( './../package.json' ).name;
var copyWithin = require( './../lib' );


// MAIN //

bench( pkg+'::object', function benchmark( b ) {
var arr;
var i;

arr = {
'length': 10,
'0': 0,
'1': 1,
'2': 2,
'3': 3,
'4': 4,
'5': 5,
'6': 6,
'7': 7,
'8': 8,
'9': 9
};

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
arr = copyWithin( arr, i % 10 );
if ( !isCollection( arr ) ) {
b.fail( 'should return an array-like object' );
}
}
b.toc();
if ( !isCollection( arr ) ) {
b.fail( 'should return an array-like object' );
}
b.pass( 'benchmark finished' );
b.end();
});
Loading
Oops, something went wrong.