diff --git a/lib/node_modules/@stdlib/blas/ext/copy-within/README.md b/lib/node_modules/@stdlib/blas/ext/copy-within/README.md new file mode 100644 index 000000000000..a02680e95a37 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/copy-within/README.md @@ -0,0 +1,152 @@ + + +# copyWithin + +> Perform an in-place copy of elements within an [ndarray][@stdlib/ndarray/ctor] along an [ndarray][@stdlib/ndarray/ctor] dimension. + +
+ +## Usage + +```javascript +var copyWithin = require( '@stdlib/blas/ext/copy-within' ); +``` + +#### copyWithin( x, target, start\[, end]\[, options] ) + +Performs an in-place copy of elements within an [ndarray][@stdlib/ndarray/ctor] along an [ndarray][@stdlib/ndarray/ctor] dimension. + +```javascript +var array = require( '@stdlib/ndarray/array' ); + +// Create an input ndarray: +var x = array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); +// returns [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] + +// Perform operation: +var y = copyWithin( x, 3, 1, 4 ); +// returns [ 1.0, 2.0, 3.0, 2.0, 3.0, 4.0 ] + +var bool = ( x === y ); +// returns true +``` + +The function has the following parameters: + +- **x**: input [ndarray][@stdlib/ndarray/ctor]. +- **target**: target index. May be either an integer or an [ndarray][@stdlib/ndarray/ctor] having an integer index or "generic" [data type][@stdlib/ndarray/dtypes]. If provided an [ndarray][@stdlib/ndarray/ctor], the value must have a shape which is [broadcast-compatible][@stdlib/ndarray/base/broadcast-shapes] with the complement of the shape defined by `options.dim`. For example, given the input shape `[2, 3, 4]` and `options.dim=0`, an [ndarray][@stdlib/ndarray/ctor] target index must have a shape which is [broadcast-compatible][@stdlib/ndarray/base/broadcast-shapes] with the shape `[3, 4]`. +- **start**: source start index (inclusive). May be either an integer or an [ndarray][@stdlib/ndarray/ctor] having an integer index or "generic" [data type][@stdlib/ndarray/dtypes] and following the same broadcasting semantics as the `target` argument. +- **end**: source end index (exclusive) (_optional_). May be either an integer or an [ndarray][@stdlib/ndarray/ctor] having an integer index or "generic" [data type][@stdlib/ndarray/dtypes] and following the same broadcasting semantics as the `target` argument. By default, the source end index is `N + 1`, where `N` is the number of elements along the dimension specified by `options.dim`. +- **options**: function options (_optional_). + +The function accepts the following options: + +- **dim**: dimension over which to perform operation. If provided a negative integer, the dimension along which to perform the operation is determined by counting backward from the last dimension (where `-1` refers to the last dimension). Default: `-1`. + +When a `target`, `start`, and/or `end` index is negative, the respective index is determined relative to the last indexed element, with out-of-bounds indices clamped to index bounds. + +```javascript +var array = require( '@stdlib/ndarray/array' ); + +var x = array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); + +var y = copyWithin( x, 0, -2 ); +// returns [ 5.0, 6.0, 3.0, 4.0, 5.0, 6.0 ] +``` + +By default, the function performs the operation over elements along the last dimension. To perform the operation over a different dimension, provide a `dim` option. + +```javascript +var array = require( '@stdlib/ndarray/array' ); + +var x = array( [ [ 1.0, 2.0, 3.0, 4.0 ], [ 5.0, 6.0, 7.0, 8.0 ] ] ); + +var y = copyWithin( x, 1, 0, { + 'dim': 0 +}); +// returns [ [ 1.0, 2.0, 3.0, 4.0 ], [ 1.0, 2.0, 3.0, 4.0 ] ] +``` + +
+ + + +
+ +## Notes + +- The input [ndarray][@stdlib/ndarray/ctor] is copied **in-place** (i.e., the input [ndarray][@stdlib/ndarray/ctor] is **mutated**). + +
+ + + +
+ +## Examples + + + +```javascript +var discreteUniform = require( '@stdlib/random/discrete-uniform' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var copyWithin = require( '@stdlib/blas/ext/copy-within' ); + +// Generate an ndarray of random numbers: +var x = discreteUniform( [ 5, 5 ], 0, 20, { + 'dtype': 'generic' +}); +console.log( ndarray2array( x ) ); + +// Perform operation: +copyWithin( x, -2, 0, 2, { + 'dim': 0 +}); + +// Print the results: +console.log( ndarray2array( x ) ); +``` + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/blas/ext/copy-within/benchmark/benchmark.js b/lib/node_modules/@stdlib/blas/ext/copy-within/benchmark/benchmark.js new file mode 100644 index 000000000000..c30d34baae44 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/copy-within/benchmark/benchmark.js @@ -0,0 +1,103 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 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 isnan = require( '@stdlib/math/base/assert/is-nan' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var uniform = require( '@stdlib/random/uniform' ); +var format = require( '@stdlib/string/format' ); +var pkg = require( './../package.json' ).name; +var copyWithin = require( './../lib' ); + + +// VARIABLES // + +var options = { + 'dtype': 'float64' +}; + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var x = uniform( [ len ], -50.0, 50.0, options ); + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var o; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + o = copyWithin( x, 1, 0, len-1 ); + if ( typeof o !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( isnan( o.get( i%len ) ) ) { + b.fail( 'should not return NaN' ); + } + 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( format( '%s:dtype=%s,len=%d', pkg, options.dtype, len ), f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/blas/ext/copy-within/docs/repl.txt b/lib/node_modules/@stdlib/blas/ext/copy-within/docs/repl.txt new file mode 100644 index 000000000000..f53736859b2e --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/copy-within/docs/repl.txt @@ -0,0 +1,61 @@ + +{{alias}}( x, target, start[, end][, options] ) + Performs an in-place copy of elements within an ndarray along an ndarray + dimension. + + When a `target`, `start`, and/or `end` index is negative, the respective + index is determined relative to the last indexed element, with out-of-bounds + indices clamped to index bounds. + + The function copies elements within an input ndarray in-place and thus + mutates an input ndarray. + + Parameters + ---------- + x: ndarray + Input array. + + target: ndarray|integer + Target index. May be either an integer or an ndarray having an integer + index or "generic" data type. If provided an ndarray, the value must + have a shape which is broadcast compatible with the complement of the + shape defined by `options.dim`. For example, given the input shape + `[2, 3, 4]` and `options.dim=0`, an ndarray target index must have a + shape which is broadcast compatible with the shape `[3, 4]`. + + start: ndarray|integer + Source start index (inclusive). May be either an integer or an ndarray + having an integer index or "generic" data type and following the same + broadcasting semantics as the `target` argument. + + end: ndarray|integer (optional) + Source end index (exclusive). May be either an integer or an ndarray + having an integer index or "generic" data type and following the same + broadcasting semantics as the `target` argument. Default: `N + 1` where + `N` is the number of elements along the operation dimension. + + options: Object (optional) + Function options. + + options.dim: integer (optional) + Dimension over which to perform operation. If provided a negative + integer, the dimension along which to perform the operation is + determined by counting backward from the last dimension (where `-1` + refers to the last dimension). Default: `-1`. + + Returns + ------- + out: ndarray + Input array. + + Examples + -------- + > var x = {{alias:@stdlib/ndarray/array}}( [ 1.0, 2.0, 3.0, 4.0 ] ); + > var y = {{alias}}( x, 2, 0 ) + [ 1.0, 2.0, 1.0, 2.0 ] + > var bool = ( x === y ) + true + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/blas/ext/copy-within/docs/types/index.d.ts b/lib/node_modules/@stdlib/blas/ext/copy-within/docs/types/index.d.ts new file mode 100644 index 000000000000..12929fd10acb --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/copy-within/docs/types/index.d.ts @@ -0,0 +1,135 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2026 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. +*/ + +// TypeScript Version: 4.1 + +/// + +import { typedndarray } from '@stdlib/types/ndarray'; + +/** +* Index argument. +*/ +type IndexArgument = typedndarray | number; + +/** +* Interface defining options. +*/ +interface Options { + /** + * Dimension over which to perform operation. Default: `-1`. + * + * ## Notes + * + * - If provided a negative integer, the dimension along which to perform the operation is determined by counting backward from the last dimension (where `-1` refers to the last dimension). + */ + dim?: number; +} + +/** +* Interface for performing an in-place copy of elements within an ndarray. +*/ +interface CopyWithin { + /** + * Performs an in-place copy of elements within an ndarray along an ndarray dimension. + * + * ## Notes + * + * - The input ndarray is copied **in-place** (i.e., the input ndarray is **mutated**). + * - When a `target`, `start`, and/or `end` index is negative, the respective index is determined relative to the last indexed element, with out-of-bounds indices clamped to index bounds. + * + * @param x - input ndarray + * @param target - target index + * @param start - source start index (inclusive) + * @param options - function options + * @returns input ndarray + * + * @example + * var array = require( '@stdlib/ndarray/array' ); + * + * var x = array( [ 1.0, 2.0, 3.0, 4.0 ] ); + * + * var y = copyWithin( x, 2, 0 ); + * // returns [ 1.0, 2.0, 1.0, 2.0 ] + * + * var bool = ( x === y ); + * // returns true + */ + >( x: T, target: IndexArgument, start: IndexArgument, options?: Options ): T; + + /** + * Performs an in-place copy of elements within an ndarray along an ndarray dimension. + * + * ## Notes + * + * - The input ndarray is copied **in-place** (i.e., the input ndarray is **mutated**). + * - When a `target`, `start`, and/or `end` index is negative, the respective index is determined relative to the last indexed element, with out-of-bounds indices clamped to index bounds. + * + * @param x - input ndarray + * @param target - target index + * @param start - source start index (inclusive) + * @param end - source end index (exclusive) + * @param options - function options + * @returns input ndarray + * + * @example + * var array = require( '@stdlib/ndarray/array' ); + * + * var x = array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); + * + * var y = copyWithin( x, 3, 1, 4 ); + * // returns [ 1.0, 2.0, 3.0, 2.0, 3.0, 4.0 ] + * + * var bool = ( x === y ); + * // returns true + */ + >( x: T, target: IndexArgument, start: IndexArgument, end: IndexArgument, options?: Options ): T; +} + +/** +* Performs an in-place copy of elements within an ndarray along an ndarray dimension. +* +* ## Notes +* +* - The input ndarray is copied **in-place** (i.e., the input ndarray is **mutated**). +* - When a `target`, `start`, and/or `end` index is negative, the respective index is determined relative to the last indexed element, with out-of-bounds indices clamped to index bounds. +* +* @param x - input ndarray +* @param target - target index +* @param start - source start index (inclusive) +* @param end - source end index (exclusive) +* @param options - function options +* @returns input ndarray +* +* @example +* var array = require( '@stdlib/ndarray/array' ); +* +* var x = array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); +* +* var y = copyWithin( x, 3, 1, 4 ); +* // returns [ 1.0, 2.0, 3.0, 2.0, 3.0, 4.0 ] +* +* var bool = ( x === y ); +* // returns true +*/ +declare const copyWithin: CopyWithin; + + +// EXPORTS // + +export = copyWithin; diff --git a/lib/node_modules/@stdlib/blas/ext/copy-within/docs/types/test.ts b/lib/node_modules/@stdlib/blas/ext/copy-within/docs/types/test.ts new file mode 100644 index 000000000000..ac6d3f8d11e8 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/copy-within/docs/types/test.ts @@ -0,0 +1,190 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2026 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. +*/ + +/* eslint-disable space-in-parens */ + +/// + +import zeros = require( '@stdlib/ndarray/zeros' ); +import copyWithin = require( './index' ); + + +// TESTS // + +// The function returns an ndarray... +{ + copyWithin( zeros( [ 2, 2 ], { 'dtype': 'float64' } ), 2, 0 ); // $ExpectType float64ndarray + copyWithin( zeros( [ 2, 2 ], { 'dtype': 'float64' } ), 2, 0, {} ); // $ExpectType float64ndarray + copyWithin( zeros( [ 2, 2 ], { 'dtype': 'float64' } ), 2, 0, 4 ); // $ExpectType float64ndarray + copyWithin( zeros( [ 2, 2 ], { 'dtype': 'float64' } ), 2, 0, 4, {} ); // $ExpectType float64ndarray + copyWithin( zeros( [ 2, 2 ], { 'dtype': 'float32' } ), 2, 0 ); // $ExpectType float32ndarray + copyWithin( zeros( [ 2, 2 ], { 'dtype': 'float32' } ), 2, 0, {} ); // $ExpectType float32ndarray + copyWithin( zeros( [ 2, 2 ], { 'dtype': 'int32' } ), 2, 0 ); // $ExpectType int32ndarray + copyWithin( zeros( [ 2, 2 ], { 'dtype': 'int32' } ), 2, 0, {} ); // $ExpectType int32ndarray + copyWithin( zeros( [ 2, 2 ], { 'dtype': 'complex128' } ), 2, 0 ); // $ExpectType complex128ndarray + copyWithin( zeros( [ 2, 2 ], { 'dtype': 'complex128' } ), 2, 0, {} ); // $ExpectType complex128ndarray + copyWithin( zeros( [ 2, 2 ], { 'dtype': 'complex64' } ), 2, 0 ); // $ExpectType complex64ndarray + copyWithin( zeros( [ 2, 2 ], { 'dtype': 'complex64' } ), 2, 0, {} ); // $ExpectType complex64ndarray + copyWithin( zeros( [ 2, 2 ], { 'dtype': 'generic' } ), 2, 0 ); // $ExpectType genericndarray + copyWithin( zeros( [ 2, 2 ], { 'dtype': 'generic' } ), 2, 0, {} ); // $ExpectType genericndarray +} + +// The function returns an ndarray when provided ndarray index arguments... +{ + const x = zeros( [ 2, 2 ], { + 'dtype': 'float64' + }); + + copyWithin( x, x, x ); // $ExpectType float64ndarray + copyWithin( x, x, x, {} ); // $ExpectType float64ndarray + copyWithin( x, x, x, x ); // $ExpectType float64ndarray + copyWithin( x, x, x, x, {} ); // $ExpectType float64ndarray +} + +// The compiler throws an error if the function is provided a first argument which is not an ndarray... +{ + copyWithin( '5', 2, 0 ); // $ExpectError + copyWithin( 5, 2, 0 ); // $ExpectError + copyWithin( true, 2, 0 ); // $ExpectError + copyWithin( false, 2, 0 ); // $ExpectError + copyWithin( null, 2, 0 ); // $ExpectError + copyWithin( void 0, 2, 0 ); // $ExpectError + copyWithin( {}, 2, 0 ); // $ExpectError + copyWithin( ( x: number ): number => x, 2, 0 ); // $ExpectError + + copyWithin( '5', 2, 0, {} ); // $ExpectError + copyWithin( 5, 2, 0, {} ); // $ExpectError + copyWithin( true, 2, 0, {} ); // $ExpectError + copyWithin( false, 2, 0, {} ); // $ExpectError + copyWithin( null, 2, 0, {} ); // $ExpectError + copyWithin( void 0, 2, 0, {} ); // $ExpectError + copyWithin( {}, 2, 0, {} ); // $ExpectError + copyWithin( ( x: number ): number => x, 2, 0, {} ); // $ExpectError +} + +// The compiler throws an error if the function is provided a target index argument which is not an ndarray or integer... +{ + const x = zeros( [ 2, 2 ], { + 'dtype': 'float64' + }); + + copyWithin( x, '5', 0 ); // $ExpectError + copyWithin( x, true, 0 ); // $ExpectError + copyWithin( x, false, 0 ); // $ExpectError + copyWithin( x, null, 0 ); // $ExpectError + copyWithin( x, [], 0 ); // $ExpectError + copyWithin( x, ( x: number ): number => x, 0 ); // $ExpectError + + copyWithin( x, '5', 0, {} ); // $ExpectError + copyWithin( x, true, 0, {} ); // $ExpectError + copyWithin( x, false, 0, {} ); // $ExpectError + copyWithin( x, null, 0, {} ); // $ExpectError + copyWithin( x, [], 0, {} ); // $ExpectError + copyWithin( x, ( x: number ): number => x, 0, {} ); // $ExpectError +} + +// The compiler throws an error if the function is provided a start index argument which is not an ndarray or integer... +{ + const x = zeros( [ 2, 2 ], { + 'dtype': 'float64' + }); + + copyWithin( x, 2, '5' ); // $ExpectError + copyWithin( x, 2, true ); // $ExpectError + copyWithin( x, 2, false ); // $ExpectError + copyWithin( x, 2, null ); // $ExpectError + copyWithin( x, 2, [] ); // $ExpectError + copyWithin( x, 2, ( x: number ): number => x ); // $ExpectError + + copyWithin( x, 2, '5', {} ); // $ExpectError + copyWithin( x, 2, true, {} ); // $ExpectError + copyWithin( x, 2, false, {} ); // $ExpectError + copyWithin( x, 2, null, {} ); // $ExpectError + copyWithin( x, 2, [], {} ); // $ExpectError + copyWithin( x, 2, ( x: number ): number => x, {} ); // $ExpectError +} + +// The compiler throws an error if the function is provided an end index argument which is not an ndarray or integer... +{ + const x = zeros( [ 2, 2 ], { + 'dtype': 'float64' + }); + + copyWithin( x, 2, 0, '5', {} ); // $ExpectError + copyWithin( x, 2, 0, true, {} ); // $ExpectError + copyWithin( x, 2, 0, false, {} ); // $ExpectError + copyWithin( x, 2, 0, null, {} ); // $ExpectError + copyWithin( x, 2, 0, [], {} ); // $ExpectError + copyWithin( x, 2, 0, ( x: number ): number => x, {} ); // $ExpectError +} + +// The compiler throws an error if the function is provided an options argument which is not an object... +{ + const x = zeros( [ 2, 2 ], { + 'dtype': 'float64' + }); + + copyWithin( x, 2, 0, '5' ); // $ExpectError + copyWithin( x, 2, 0, true ); // $ExpectError + copyWithin( x, 2, 0, false ); // $ExpectError + copyWithin( x, 2, 0, null ); // $ExpectError + copyWithin( x, 2, 0, [] ); // $ExpectError + copyWithin( x, 2, 0, ( x: number ): number => x ); // $ExpectError + + copyWithin( x, 2, 0, 4, '5' ); // $ExpectError + copyWithin( x, 2, 0, 4, true ); // $ExpectError + copyWithin( x, 2, 0, 4, false ); // $ExpectError + copyWithin( x, 2, 0, 4, null ); // $ExpectError + copyWithin( x, 2, 0, 4, [] ); // $ExpectError + copyWithin( x, 2, 0, 4, ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided an invalid `dim` option... +{ + const x = zeros( [ 2, 2 ], { + 'dtype': 'float64' + }); + + copyWithin( x, 2, 0, { 'dim': '5' } ); // $ExpectError + copyWithin( x, 2, 0, { 'dim': true } ); // $ExpectError + copyWithin( x, 2, 0, { 'dim': false } ); // $ExpectError + copyWithin( x, 2, 0, { 'dim': null } ); // $ExpectError + copyWithin( x, 2, 0, { 'dim': [] } ); // $ExpectError + copyWithin( x, 2, 0, { 'dim': {} } ); // $ExpectError + copyWithin( x, 2, 0, { 'dim': ( x: number ): number => x } ); // $ExpectError + + copyWithin( x, 2, 0, 4, { 'dim': '5' } ); // $ExpectError + copyWithin( x, 2, 0, 4, { 'dim': true } ); // $ExpectError + copyWithin( x, 2, 0, 4, { 'dim': false } ); // $ExpectError + copyWithin( x, 2, 0, 4, { 'dim': null } ); // $ExpectError + copyWithin( x, 2, 0, 4, { 'dim': [] } ); // $ExpectError + copyWithin( x, 2, 0, 4, { 'dim': {} } ); // $ExpectError + copyWithin( x, 2, 0, 4, { 'dim': ( x: number ): number => x } ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + const x = zeros( [ 2, 2 ], { + 'dtype': 'float64' + }); + + copyWithin(); // $ExpectError + copyWithin( x ); // $ExpectError + copyWithin( x, 2 ); // $ExpectError + copyWithin( x, 2, 0, 4, {}, {} ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/blas/ext/copy-within/examples/index.js b/lib/node_modules/@stdlib/blas/ext/copy-within/examples/index.js new file mode 100644 index 000000000000..85cb042df2a8 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/copy-within/examples/index.js @@ -0,0 +1,37 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 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'; + +var discreteUniform = require( '@stdlib/random/discrete-uniform' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var copyWithin = require( './../lib' ); + +// Generate an ndarray of random numbers: +var x = discreteUniform( [ 5, 5 ], 0, 20, { + 'dtype': 'generic' +}); +console.log( ndarray2array( x ) ); + +// Perform operation: +copyWithin( x, -2, 0, 2, { + 'dim': 0 +}); + +// Print the results: +console.log( ndarray2array( x ) ); diff --git a/lib/node_modules/@stdlib/blas/ext/copy-within/lib/base.js b/lib/node_modules/@stdlib/blas/ext/copy-within/lib/base.js new file mode 100644 index 000000000000..74f613196580 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/copy-within/lib/base.js @@ -0,0 +1,128 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 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 dtypes = require( '@stdlib/ndarray/dtypes' ); +var gcopyWithin = require( '@stdlib/blas/ext/base/ndarray/gcopy-within' ); +var dcopyWithin = require( '@stdlib/blas/ext/base/ndarray/dcopy-within' ); +var scopyWithin = require( '@stdlib/blas/ext/base/ndarray/scopy-within' ); +var zcopyWithin = require( '@stdlib/blas/ext/base/ndarray/zcopy-within' ); +var ccopyWithin = require( '@stdlib/blas/ext/base/ndarray/ccopy-within' ); +var factory = require( '@stdlib/ndarray/base/nullary-strided1d-dispatch-factory' ); + + +// VARIABLES // + +var idtypes0 = dtypes( 'integer_index_and_generic' ); // target/start/end ndarrays +var idtypes1 = dtypes( 'all' ); // workspace ndarray +var odtypes = dtypes( 'all' ); +var table = { + 'types': [ + 'float64', // input/output + 'float32', // input/output + 'complex128', // input/output + 'complex64' // input/output + ], + 'fcns': [ + dcopyWithin, + scopyWithin, + zcopyWithin, + ccopyWithin + ], + 'default': gcopyWithin +}; +var options = { + 'strictTraversalOrder': true +}; + + +// MAIN // + +/** +* Performs an in-place copy of elements within an ndarray along an ndarray dimension. +* +* @private +* @name copyWithin +* @type {Function} +* @param {ndarray} x - input ndarray +* @param {ndarray} target - ndarray containing the target index +* @param {ndarray} start - ndarray containing the source start index (inclusive) +* @param {ndarray} end - ndarray containing the source end index (exclusive) +* @param {ndarray} workspace - workspace ndarray +* @param {Options} [options] - function options +* @param {IntegerArray} [options.dims] - list of dimensions over which to perform operation +* @throws {TypeError} first argument must be an ndarray-like object +* @throws {TypeError} index arguments must be ndarray-like objects +* @throws {TypeError} workspace argument must be an ndarray-like object +* @throws {TypeError} options argument must be an object +* @throws {RangeError} dimension indices must not exceed input ndarray bounds +* @throws {Error} must provide valid options +* @returns {ndarray} input ndarray +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +* var zeros = require( '@stdlib/ndarray/zeros' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* +* // Create a data buffer: +* var xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); +* +* // Define the shape of the input array: +* var sh = [ 6 ]; +* +* // Define the array strides: +* var sx = [ 1 ]; +* +* // Define the index offset: +* var ox = 0; +* +* // Create an input ndarray: +* var x = new ndarray( 'float64', xbuf, sh, sx, ox, 'row-major' ); +* +* // Create ndarrays containing the target, start, and end indices: +* var opts = { +* 'dtype': 'int32' +* }; +* var target = scalar2ndarray( 3, opts ); +* var start = scalar2ndarray( 1, opts ); +* var end = scalar2ndarray( 4, opts ); +* +* // Create a workspace ndarray: +* var w = zeros( [ 6 ], { +* 'dtype': 'float64' +* }); +* +* // Perform operation: +* var out = copyWithin( x, target, start, end, w, { +* 'dims': [ 0 ] +* }); +* // returns [ 1.0, 2.0, 3.0, 2.0, 3.0, 4.0 ] +* +* var bool = ( out === x ); +* // returns true +*/ +var copyWithin = factory( table, [ idtypes0, idtypes0, idtypes0, idtypes1 ], odtypes, options ); // eslint-disable-line max-len + + +// EXPORTS // + +module.exports = copyWithin; diff --git a/lib/node_modules/@stdlib/blas/ext/copy-within/lib/broadcast_index.js b/lib/node_modules/@stdlib/blas/ext/copy-within/lib/broadcast_index.js new file mode 100644 index 000000000000..1bdbb12ce481 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/copy-within/lib/broadcast_index.js @@ -0,0 +1,55 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 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 isInteger = require( '@stdlib/assert/is-integer' ).isPrimitive; +var broadcastScalar = require( '@stdlib/ndarray/base/broadcast-scalar' ); +var maybeBroadcastArray = require( '@stdlib/ndarray/base/maybe-broadcast-array' ); +var getDefault = require( '@stdlib/ndarray/defaults' ).get; + + +// VARIABLES // + +var DEFAULT_DTYPE = getDefault( 'dtypes.integer_index' ); + + +// MAIN // + +/** +* Broadcasts an index argument to match the shape of the non-core dimensions. +* +* @private +* @param {(ndarray|NonNegativeInteger)} idx - index argument +* @param {NonNegativeIntegerArray} shape - shape of the non-core dimensions +* @param {string} order - memory layout order +* @returns {ndarray} ndarray containing index values +*/ +function broadcastIndex( idx, shape, order ) { + if ( isInteger( idx ) ) { + return broadcastScalar( idx, DEFAULT_DTYPE, shape, order ); + } + return maybeBroadcastArray( idx, shape ); +} + + +// EXPORTS // + +module.exports = broadcastIndex; diff --git a/lib/node_modules/@stdlib/blas/ext/copy-within/lib/index.js b/lib/node_modules/@stdlib/blas/ext/copy-within/lib/index.js new file mode 100644 index 000000000000..a2aba9c0ca06 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/copy-within/lib/index.js @@ -0,0 +1,61 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 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'; + +/** +* Perform an in-place copy of elements within an ndarray along an ndarray dimension. +* +* @module @stdlib/blas/ext/copy-within +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var copyWithin = require( '@stdlib/blas/ext/copy-within' ); +* +* // Create a data buffer: +* var xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); +* +* // Define the shape of the input array: +* var sh = [ 6 ]; +* +* // Define the array strides: +* var sx = [ 1 ]; +* +* // Define the index offset: +* var ox = 0; +* +* // Create an input ndarray: +* var x = new ndarray( 'float64', xbuf, sh, sx, ox, 'row-major' ); +* +* // Perform operation: +* var out = copyWithin( x, 3, 1, 4 ); +* // returns [ 1.0, 2.0, 3.0, 2.0, 3.0, 4.0 ] +* +* var bool = ( out === x ); +* // returns true +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/blas/ext/copy-within/lib/main.js b/lib/node_modules/@stdlib/blas/ext/copy-within/lib/main.js new file mode 100644 index 000000000000..6c98d63fed4c --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/copy-within/lib/main.js @@ -0,0 +1,194 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 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 hasOwnProp = require( '@stdlib/assert/has-own-property' ); +var isPlainObject = require( '@stdlib/assert/is-plain-object' ); +var isInteger = require( '@stdlib/assert/is-integer' ).isPrimitive; +var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); +var broadcastArray = require( '@stdlib/ndarray/base/broadcast-array' ); +var nonCoreShape = require( '@stdlib/ndarray/base/complement-shape' ); +var normalizeIndex = require( '@stdlib/ndarray/base/normalize-index' ); +var empty = require( '@stdlib/ndarray/base/empty' ); +var getDType = require( '@stdlib/ndarray/dtype' ); +var getShape = require( '@stdlib/ndarray/shape' ); +var getOrder = require( '@stdlib/ndarray/order' ); +var format = require( '@stdlib/string/format' ); +var normalizeIndices = require( './normalize_indices.js' ); +var broadcastIndex = require( './broadcast_index.js' ); +var base = require( './base.js' ); + + +// MAIN // + +/** +* Performs an in-place copy of elements within an ndarray along an ndarray dimension. +* +* @param {ndarrayLike} x - input ndarray +* @param {(ndarrayLike|integer)} target - target index +* @param {(ndarrayLike|integer)} start - source start index (inclusive) +* @param {(ndarrayLike|integer)} [end] - source end index (exclusive) +* @param {Options} [options] - function options +* @param {integer} [options.dim=-1] - dimension over which to perform operation +* @throws {TypeError} first argument must be an ndarray-like object +* @throws {TypeError} index arguments must be either ndarray-like objects or integers +* @throws {TypeError} options argument must be an object +* @throws {RangeError} dimension index must not exceed input ndarray bounds +* @throws {RangeError} first argument must have at least one dimension +* @throws {TypeError} must provide valid options +* @returns {ndarray} input ndarray +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* +* // Create a data buffer: +* var xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); +* +* // Define the shape of the input array: +* var sh = [ 6 ]; +* +* // Define the array strides: +* var sx = [ 1 ]; +* +* // Define the index offset: +* var ox = 0; +* +* // Create an input ndarray: +* var x = new ndarray( 'float64', xbuf, sh, sx, ox, 'row-major' ); +* +* // Perform operation: +* var out = copyWithin( x, 3, 1, 4 ); +* // returns [ 1.0, 2.0, 3.0, 2.0, 3.0, 4.0 ] +* +* var bool = ( out === x ); +* // returns true +*/ +function copyWithin( x, target, start ) { + var hasOptions; + var options; + var hasEnd; + var nargs; + var opts; + var end; + var lsh; + var len; + var ord; + var sh; + var dt; + var d; + var e; + var s; + var t; + var w; + + nargs = arguments.length; + if ( !isndarrayLike( x ) ) { + throw new TypeError( format( 'invalid argument. First argument must be an ndarray. Value: `%s`.', x ) ); + } + if ( !isInteger( target ) && !isndarrayLike( target ) ) { + throw new TypeError( format( 'invalid argument. Second argument must be either an ndarray or an integer. Value: `%s`.', target ) ); + } + if ( !isInteger( start ) && !isndarrayLike( start ) ) { + throw new TypeError( format( 'invalid argument. Third argument must be either an ndarray or an integer. Value: `%s`.', start ) ); + } + // Resolve input ndarray meta data: + dt = getDType( x ); + ord = getOrder( x ); + + // Initialize an options object: + opts = { + 'dims': [ -1 ] // default behavior is to perform the operation over the last dimension + }; + + // Initialize flags indicating whether `end` and `options` arguments were provided: + hasOptions = false; + hasEnd = false; + + // Case: copyWithin( x, target, start, end, options ) + if ( nargs > 4 ) { + end = arguments[ 3 ]; + if ( !isInteger( end ) && !isndarrayLike( end ) ) { + throw new TypeError( format( 'invalid argument. Fourth argument must be either an ndarray or an integer. Value: `%s`.', end ) ); + } + hasEnd = true; + options = arguments[ 4 ]; + hasOptions = true; + } + // Case: copyWithin( x, target, start, ??? ) + else if ( nargs === 4 ) { + // Case: copyWithin( x, target, start, end ) + if ( isInteger( arguments[ 3 ] ) || isndarrayLike( arguments[ 3 ] ) ) { + end = arguments[ 3 ]; + hasEnd = true; + } + // Case: copyWithin( x, target, start, options ) + else { + options = arguments[ 3 ]; + hasOptions = true; + } + } + if ( hasOptions ) { + if ( !isPlainObject( options ) ) { + throw new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) ); + } + // Resolve provided options... + if ( hasOwnProp( options, 'dim' ) ) { + if ( !isInteger( options.dim ) ) { + throw new TypeError( format( 'invalid option. `%s` option must be an integer. Option: `%s`.', 'dim', options.dim ) ); + } + opts.dims[ 0 ] = options.dim; + } + } + // Resolve the shape of the non-core dimensions and the number of indexed elements... + sh = getShape( x ); + if ( sh.length < 1 ) { + throw new RangeError( 'invalid argument. First argument must have at least one dimension.' ); + } + d = normalizeIndex( opts.dims[ 0 ], sh.length-1 ); + if ( d === -1 ) { + throw new RangeError( format( 'invalid option. Dimension index exceeds the number of dimensions. Number of dimensions: %d. Value: `%d`.', sh.length, opts.dims[ 0 ] ) ); + } + opts.dims[ 0 ] = d; + lsh = nonCoreShape( sh, opts.dims ); + len = sh[ d ]; + + // Normalize the provided indices... + t = normalizeIndices( target, len ); + s = normalizeIndices( start, len ); + e = ( hasEnd ) ? normalizeIndices( end, len ) : len; + + // Broadcast the indices to match the shape of the non-core dimensions... + t = broadcastIndex( t, lsh, ord ); + s = broadcastIndex( s, lsh, ord ); + e = broadcastIndex( e, lsh, ord ); + + // Allocate a workspace ndarray which is broadcast across the non-core dimensions, thus allowing each one-dimensional slice to reuse the same workspace buffer: + w = broadcastArray( empty( dt, [ len ], ord ), lsh.concat( [ len ] ) ); + + // Perform operation: + return base( x, t, s, e, w, opts ); +} + + +// EXPORTS // + +module.exports = copyWithin; diff --git a/lib/node_modules/@stdlib/blas/ext/copy-within/lib/normalize_indices.js b/lib/node_modules/@stdlib/blas/ext/copy-within/lib/normalize_indices.js new file mode 100644 index 000000000000..be1e9669cba7 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/copy-within/lib/normalize_indices.js @@ -0,0 +1,96 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 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 isInteger = require( '@stdlib/assert/is-integer' ).isPrimitive; +var unary = require( '@stdlib/ndarray/base/unary' ); +var baseEmpty = require( '@stdlib/ndarray/base/empty' ); +var getDType = require( '@stdlib/ndarray/base/dtype' ); +var getShape = require( '@stdlib/ndarray/base/shape' ); +var getOrder = require( '@stdlib/ndarray/base/order' ); + + +// FUNCTIONS // + +/** +* Normalizes a relative index. +* +* @private +* @param {integer} idx - relative index +* @param {NonNegativeInteger} len - number of indexed elements +* @returns {NonNegativeInteger} normalized index +*/ +function normalize( idx, len ) { + if ( idx < 0 ) { + idx += len; + if ( idx < 0 ) { + return 0; + } + return idx; + } + if ( idx > len ) { + return len; + } + return idx; +} + + +// MAIN // + +/** +* Normalizes an index argument. +* +* ## Notes +* +* - When a `target`, `start`, and/or `end` index is negative, the respective index is determined relative to the last indexed element, with out-of-bounds indices clamped to index bounds. +* - If provided an ndarray, the function normalizes each element and returns a new ndarray having the same shape and data type as the provided ndarray. +* +* @private +* @param {(ndarray|integer)} idx - index argument +* @param {NonNegativeInteger} len - number of indexed elements +* @returns {(ndarray|NonNegativeInteger)} normalized index argument +*/ +function normalizeIndices( idx, len ) { + var out; + + if ( isInteger( idx ) ) { + return normalize( idx, len ); + } + out = baseEmpty( getDType( idx ), getShape( idx, false ), getOrder( idx ) ); + unary( [ idx, out ], clbk ); + return out; + + /** + * Normalizes a relative index. + * + * @private + * @param {integer} v - relative index + * @returns {NonNegativeInteger} normalized index + */ + function clbk( v ) { + return normalize( v, len ); + } +} + + +// EXPORTS // + +module.exports = normalizeIndices; diff --git a/lib/node_modules/@stdlib/blas/ext/copy-within/package.json b/lib/node_modules/@stdlib/blas/ext/copy-within/package.json new file mode 100644 index 000000000000..cc06182be036 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/copy-within/package.json @@ -0,0 +1,65 @@ +{ + "name": "@stdlib/blas/ext/copy-within", + "version": "0.0.0", + "description": "Perform an in-place copy of elements within an ndarray along an ndarray dimension.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdmath", + "mathematics", + "math", + "blas", + "extended", + "copy", + "copywithin", + "within", + "move", + "ndarray" + ], + "__stdlib__": {} +} diff --git a/lib/node_modules/@stdlib/blas/ext/copy-within/test/test.js b/lib/node_modules/@stdlib/blas/ext/copy-within/test/test.js new file mode 100644 index 000000000000..d3ec150a0b8f --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/copy-within/test/test.js @@ -0,0 +1,1167 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 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 tape = require( 'tape' ); +var isSameArray = require( '@stdlib/assert/is-same-array' ); +var Int32Array = require( '@stdlib/array/int32' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); +var zeros = require( '@stdlib/ndarray/zeros' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +var getDType = require( '@stdlib/ndarray/dtype' ); +var getShape = require( '@stdlib/ndarray/shape' ); +var getOrder = require( '@stdlib/ndarray/order' ); +var getData = require( '@stdlib/ndarray/data-buffer' ); +var copyWithin = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof copyWithin, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function throws an error if provided a first argument which is not an ndarray-like object', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + copyWithin( value, 1, 0 ); + }; + } +}); + +tape( 'the function throws an error if provided a first argument which is not an ndarray-like object (end)', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + copyWithin( value, 1, 0, 2 ); + }; + } +}); + +tape( 'the function throws an error if provided a first argument which is not an ndarray-like object (options)', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + copyWithin( value, 1, 0, {} ); + }; + } +}); + +tape( 'the function throws an error if provided a first argument which is not an ndarray-like object (end, options)', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + copyWithin( value, 1, 0, 2, {} ); + }; + } +}); + +tape( 'the function throws an error if provided a first argument which is a zero-dimensional ndarray', function test( t ) { + var values; + var i; + + values = [ + scalar2ndarray( 10.0 ), + scalar2ndarray( -3.0 ), + scalar2ndarray( 0.0 ) + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), RangeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + copyWithin( value, 1, 0 ); + }; + } +}); + +tape( 'the function throws an error if provided a `target` argument which is not an ndarray-like object or an integer', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + '5', + 3.14, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + copyWithin( x, value, 0 ); + }; + } +}); + +tape( 'the function throws an error if provided a `target` argument which is not an ndarray-like object or an integer (end)', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + '5', + 3.14, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + copyWithin( x, value, 0, 2 ); + }; + } +}); + +tape( 'the function throws an error if provided a `target` argument which is not an ndarray-like object or an integer (options)', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + '5', + 3.14, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + copyWithin( x, value, 0, {} ); + }; + } +}); + +tape( 'the function throws an error if provided a `target` argument which is not an ndarray-like object or an integer (end, options)', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + '5', + 3.14, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + copyWithin( x, value, 0, 2, {} ); + }; + } +}); + +tape( 'the function throws an error if provided a `start` argument which is not an ndarray-like object or an integer', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + '5', + 3.14, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + copyWithin( x, 1, value ); + }; + } +}); + +tape( 'the function throws an error if provided a `start` argument which is not an ndarray-like object or an integer (end)', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + '5', + 3.14, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + copyWithin( x, 1, value, 2 ); + }; + } +}); + +tape( 'the function throws an error if provided a `start` argument which is not an ndarray-like object or an integer (options)', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + '5', + 3.14, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + copyWithin( x, 1, value, {} ); + }; + } +}); + +tape( 'the function throws an error if provided a `start` argument which is not an ndarray-like object or an integer (end, options)', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + '5', + 3.14, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + copyWithin( x, 1, value, 2, {} ); + }; + } +}); + +tape( 'the function throws an error if provided an `end` argument which is not an ndarray-like object or an integer (options)', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + '5', + 3.14, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + copyWithin( x, 1, 0, value, {} ); + }; + } +}); + +tape( 'the function throws an error if provided a `target` argument which is not broadcast-compatible', function test( t ) { + var values; + var opts; + var x; + var i; + + opts = { + 'dtype': 'generic' + }; + x = zeros( [ 2, 2 ], opts ); + + values = [ + zeros( [ 4 ], opts ), + zeros( [ 2, 2, 2 ], opts ), + zeros( [ 0 ], opts ) + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), Error, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + copyWithin( x, value, 0 ); + }; + } +}); + +tape( 'the function throws an error if provided a `target` argument which is not broadcast-compatible (options)', function test( t ) { + var values; + var opts; + var x; + var i; + + opts = { + 'dtype': 'generic' + }; + x = zeros( [ 2, 2 ], opts ); + + values = [ + zeros( [ 4 ], opts ), + zeros( [ 2, 2, 2 ], opts ), + zeros( [ 0 ], opts ) + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), Error, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + copyWithin( x, value, 0, {} ); + }; + } +}); + +tape( 'the function throws an error if provided a `target` argument which is not broadcast-compatible (dim)', function test( t ) { + var values; + var opts; + var x; + var i; + + opts = { + 'dtype': 'generic' + }; + x = zeros( [ 2, 2 ], opts ); + + values = [ + zeros( [ 4 ], opts ), + zeros( [ 2, 2, 2 ], opts ), + zeros( [ 0 ], opts ) + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), Error, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + copyWithin( x, value, 0, { + 'dim': 0 + }); + }; + } +}); + +tape( 'the function throws an error if provided a `start` argument which is not broadcast-compatible', function test( t ) { + var values; + var opts; + var x; + var i; + + opts = { + 'dtype': 'generic' + }; + x = zeros( [ 2, 2 ], opts ); + + values = [ + zeros( [ 4 ], opts ), + zeros( [ 2, 2, 2 ], opts ), + zeros( [ 0 ], opts ) + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), Error, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + copyWithin( x, 1, value ); + }; + } +}); + +tape( 'the function throws an error if provided a `start` argument which is not broadcast-compatible (options)', function test( t ) { + var values; + var opts; + var x; + var i; + + opts = { + 'dtype': 'generic' + }; + x = zeros( [ 2, 2 ], opts ); + + values = [ + zeros( [ 4 ], opts ), + zeros( [ 2, 2, 2 ], opts ), + zeros( [ 0 ], opts ) + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), Error, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + copyWithin( x, 1, value, {} ); + }; + } +}); + +tape( 'the function throws an error if provided an `end` argument which is not broadcast-compatible', function test( t ) { + var values; + var opts; + var x; + var i; + + opts = { + 'dtype': 'generic' + }; + x = zeros( [ 2, 2 ], opts ); + + values = [ + zeros( [ 4 ], opts ), + zeros( [ 2, 2, 2 ], opts ), + zeros( [ 0 ], opts ) + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), Error, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + copyWithin( x, 1, 0, value ); + }; + } +}); + +tape( 'the function throws an error if provided an `end` argument which is not broadcast-compatible (options)', function test( t ) { + var values; + var opts; + var x; + var i; + + opts = { + 'dtype': 'generic' + }; + x = zeros( [ 2, 2 ], opts ); + + values = [ + zeros( [ 4 ], opts ), + zeros( [ 2, 2, 2 ], opts ), + zeros( [ 0 ], opts ) + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), Error, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + copyWithin( x, 1, 0, value, {} ); + }; + } +}); + +tape( 'the function throws an error if provided an options argument which is not an object', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + '5', + 3.14, + NaN, + true, + false, + null, + void 0, + [], + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + copyWithin( x, 1, 0, value ); + }; + } +}); + +tape( 'the function throws an error if provided an options argument which is not an object (end)', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + copyWithin( x, 1, 0, 2, value ); + }; + } +}); + +tape( 'the function throws an error if provided a `dim` option which is not an integer', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + '5', + NaN, + true, + false, + null, + void 0, + [ 'a' ], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + copyWithin( x, 1, 0, { + 'dim': value + }); + }; + } +}); + +tape( 'the function throws an error if provided a `dim` option which is not an integer (end)', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + '5', + NaN, + true, + false, + null, + void 0, + [ 'a' ], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + copyWithin( x, 1, 0, 2, { + 'dim': value + }); + }; + } +}); + +tape( 'the function throws an error if provided a `dim` option which is out-of-bounds', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + -10, + 20 + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), RangeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + copyWithin( x, 1, 0, { + 'dim': value + }); + }; + } +}); + +tape( 'the function throws an error if provided a `dim` option which is out-of-bounds (end)', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + -10, + 20 + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), RangeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + copyWithin( x, 1, 0, 2, { + 'dim': value + }); + }; + } +}); + +tape( 'the function performs an in-place copy of elements within an ndarray (default, row-major)', function test( t ) { + var expected; + var actual; + var x; + + x = new ndarray( 'generic', [ 1.0, 2.0, 3.0, 4.0 ], [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + actual = copyWithin( x, 1, 0 ); + expected = [ 1.0, 1.0, 3.0, 3.0 ]; + + t.strictEqual( actual, x, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), getShape( x ), 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.strictEqual( isSameArray( getData( actual ), expected ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function performs an in-place copy of elements within an ndarray (default, column-major)', function test( t ) { + var expected; + var actual; + var x; + + x = new ndarray( 'generic', [ 1.0, 2.0, 3.0, 4.0 ], [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + + actual = copyWithin( x, 1, 0 ); + expected = [ 1.0, 2.0, 1.0, 2.0 ]; + + t.strictEqual( actual, x, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), getShape( x ), 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.strictEqual( isSameArray( getData( actual ), expected ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports specifying the operation dimension (row-major)', function test( t ) { + var expected; + var actual; + var x; + + x = new ndarray( 'generic', [ 1.0, 2.0, 3.0, 4.0 ], [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + actual = copyWithin( x, 1, 0, { + 'dim': 0 + }); + expected = [ [ 1.0, 2.0 ], [ 1.0, 2.0 ] ]; + + t.strictEqual( actual, x, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), getShape( x ), 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + x = new ndarray( 'generic', [ 1.0, 2.0, 3.0, 4.0 ], [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + actual = copyWithin( x, 1, 0, { + 'dim': 1 + }); + expected = [ [ 1.0, 1.0 ], [ 3.0, 3.0 ] ]; + + t.strictEqual( actual, x, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), getShape( x ), 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + x = new ndarray( 'generic', [ 1.0, 2.0, 3.0, 4.0 ], [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + actual = copyWithin( x, 1, 0, { + 'dim': -2 + }); + expected = [ [ 1.0, 2.0 ], [ 1.0, 2.0 ] ]; + + t.strictEqual( actual, x, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), getShape( x ), 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports specifying the operation dimension (column-major)', function test( t ) { + var expected; + var actual; + var x; + + x = new ndarray( 'generic', [ 1.0, 2.0, 3.0, 4.0 ], [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + + actual = copyWithin( x, 1, 0, { + 'dim': 0 + }); + expected = [ [ 1.0, 3.0 ], [ 1.0, 3.0 ] ]; + + t.strictEqual( actual, x, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), getShape( x ), 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + x = new ndarray( 'generic', [ 1.0, 2.0, 3.0, 4.0 ], [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + + actual = copyWithin( x, 1, 0, { + 'dim': 1 + }); + expected = [ [ 1.0, 1.0 ], [ 2.0, 2.0 ] ]; + + t.strictEqual( actual, x, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), getShape( x ), 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + x = new ndarray( 'generic', [ 1.0, 2.0, 3.0, 4.0 ], [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + + actual = copyWithin( x, 1, 0, { + 'dim': -2 + }); + expected = [ [ 1.0, 3.0 ], [ 1.0, 3.0 ] ]; + + t.strictEqual( actual, x, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), getShape( x ), 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports providing an ending index', function test( t ) { + var expected; + var actual; + var x; + + x = new ndarray( 'generic', [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ], [ 6 ], [ 1 ], 0, 'row-major' ); + + actual = copyWithin( x, 3, 1, 4 ); + expected = [ 1.0, 2.0, 3.0, 2.0, 3.0, 4.0 ]; + + t.strictEqual( actual, x, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), getShape( x ), 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.strictEqual( isSameArray( getData( actual ), expected ), true, 'returns expected value' ); + + x = new ndarray( 'generic', [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ], [ 6 ], [ 1 ], 0, 'column-major' ); + + actual = copyWithin( x, 3, 1, 4 ); + expected = [ 1.0, 2.0, 3.0, 2.0, 3.0, 4.0 ]; + + t.strictEqual( actual, x, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), getShape( x ), 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.strictEqual( isSameArray( getData( actual ), expected ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports providing negative indices', function test( t ) { + var expected; + var actual; + var x; + + x = new ndarray( 'generic', [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ], [ 6 ], [ 1 ], 0, 'row-major' ); + + actual = copyWithin( x, 0, -2 ); + expected = [ 5.0, 6.0, 3.0, 4.0, 5.0, 6.0 ]; + + t.strictEqual( actual, x, 'returns expected value' ); + t.strictEqual( isSameArray( getData( actual ), expected ), true, 'returns expected value' ); + + x = new ndarray( 'generic', [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ], [ 6 ], [ 1 ], 0, 'row-major' ); + + actual = copyWithin( x, -2, 0, 2 ); + expected = [ 1.0, 2.0, 3.0, 4.0, 1.0, 2.0 ]; + + t.strictEqual( actual, x, 'returns expected value' ); + t.strictEqual( isSameArray( getData( actual ), expected ), true, 'returns expected value' ); + + x = new ndarray( 'generic', [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ], [ 6 ], [ 1 ], 0, 'row-major' ); + + actual = copyWithin( x, 0, -3, -1 ); + expected = [ 4.0, 5.0, 3.0, 4.0, 5.0, 6.0 ]; + + t.strictEqual( actual, x, 'returns expected value' ); + t.strictEqual( isSameArray( getData( actual ), expected ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function clamps out-of-bounds indices', function test( t ) { + var expected; + var actual; + var x; + + x = new ndarray( 'generic', [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ], [ 6 ], [ 1 ], 0, 'row-major' ); + + actual = copyWithin( x, 10, 0, 3 ); + expected = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + + t.strictEqual( actual, x, 'returns expected value' ); + t.strictEqual( isSameArray( getData( actual ), expected ), true, 'returns expected value' ); + + x = new ndarray( 'generic', [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ], [ 6 ], [ 1 ], 0, 'row-major' ); + + actual = copyWithin( x, 2, 0, 100 ); + expected = [ 1.0, 2.0, 1.0, 2.0, 3.0, 4.0 ]; + + t.strictEqual( actual, x, 'returns expected value' ); + t.strictEqual( isSameArray( getData( actual ), expected ), true, 'returns expected value' ); + + x = new ndarray( 'generic', [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ], [ 6 ], [ 1 ], 0, 'row-major' ); + + actual = copyWithin( x, -100, 2, 4 ); + expected = [ 3.0, 4.0, 3.0, 4.0, 5.0, 6.0 ]; + + t.strictEqual( actual, x, 'returns expected value' ); + t.strictEqual( isSameArray( getData( actual ), expected ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports providing index arguments as zero-dimensional ndarrays', function test( t ) { + var expected; + var actual; + var opts; + var x; + + opts = { + 'dtype': 'int32' + }; + x = new ndarray( 'generic', [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ], [ 6 ], [ 1 ], 0, 'row-major' ); + + actual = copyWithin( x, scalar2ndarray( 3, opts ), scalar2ndarray( 1, opts ), scalar2ndarray( 4, opts ) ); // eslint-disable-line max-len + expected = [ 1.0, 2.0, 3.0, 2.0, 3.0, 4.0 ]; + + t.strictEqual( actual, x, 'returns expected value' ); + t.strictEqual( isSameArray( getData( actual ), expected ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports providing index arguments as zero-dimensional ndarrays (broadcasted)', function test( t ) { + var expected; + var actual; + var opts; + var x; + + opts = { + 'dtype': 'int32' + }; + x = new ndarray( 'generic', [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ], [ 2, 4 ], [ 4, 1 ], 0, 'row-major' ); + + actual = copyWithin( x, scalar2ndarray( 2, opts ), 0, { + 'dim': 1 + }); + expected = [ [ 1.0, 2.0, 1.0, 2.0 ], [ 5.0, 6.0, 5.0, 6.0 ] ]; + + t.strictEqual( actual, x, 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports providing index arguments as ndarrays', function test( t ) { + var expected; + var target; + var actual; + var x; + + x = new ndarray( 'generic', [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ], [ 2, 4 ], [ 4, 1 ], 0, 'row-major' ); + + target = new ndarray( 'int32', new Int32Array( [ 0, 2 ] ), [ 2 ], [ 1 ], 0, 'row-major' ); + + actual = copyWithin( x, target, 2, 4, { + 'dim': 1 + }); + expected = [ [ 3.0, 4.0, 3.0, 4.0 ], [ 5.0, 6.0, 7.0, 8.0 ] ]; + + t.strictEqual( actual, x, 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports providing index arguments as ndarrays containing negative indices', function test( t ) { + var expected; + var target; + var actual; + var x; + + x = new ndarray( 'generic', [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ], [ 2, 4 ], [ 4, 1 ], 0, 'row-major' ); + + target = new ndarray( 'int32', new Int32Array( [ -2, 0 ] ), [ 2 ], [ 1 ], 0, 'row-major' ); + + actual = copyWithin( x, target, 0, 2, { + 'dim': 1 + }); + expected = [ [ 1.0, 2.0, 1.0, 2.0 ], [ 5.0, 6.0, 7.0, 8.0 ] ]; + + t.strictEqual( actual, x, 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +});