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 copyWithin method to array/fixed-endian-factory #3271

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
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
21 changes: 21 additions & 0 deletions lib/node_modules/@stdlib/array/fixed-endian-factory/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,27 @@ var len = arr.length;

### Typed Array Methods

<a name="method-copy-within"></a>

#### TypedArrayFE.prototype.copyWithin( target, start\[, end] )

Copies a sequence of elements within the array starting at `start` and ending at `end` (non-inclusive) to the position starting at `target`.

```javascript
var Float64ArrayFE = fixedEndianFactory( 'float64' );

var arr = new Float64ArrayFE( 'little-endian', [ 1.0, 2.0, 3.0, 4.0, 5.0 ] );

arr.copyWithin( 0, 3 );

var out = arr.get( 0 );
// returns 4.0

out = arr.get( 1 );
// returns 5.0
```


<a name="static-method-from"></a>

#### TypedArrayFE.from( endianness, src\[, map\[, thisArg]] )
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2024 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 factory = require( './../lib' );
var pkg = require( './../package.json' ).name;


// VARIABLES //

var Float64ArrayFE = factory( 'float64' );


// MAIN //

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

arr = new Float64ArrayFE( 'little-endian', [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0 ] );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
arr.copyWithin( 0, 6 );
if ( arr.get( 0 ) !== 7.0 ) {
b.fail( 'unexpected result' );
}
}
b.toc();
if ( arr.at( 0 ) !== arr.at( 0 ) ) {
b.fail( 'should not be NaN' );
}
b.pass( 'benchmark finished' );
b.end();
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2024 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 pkg = require( './../package.json' ).name;
var factory = require( './../lib' );


// VARIABLES //

var Float64ArrayFE = factory( 'float64' );


// FUNCTIONS //

/**
* Creates a benchmark function.
*
* @private
* @param {PositiveInteger} len - array length
* @returns {Function} benchmark function
*/
function createBenchmark( len ) {
return function benchmark( b ) {
var arr;
var i;

function generateArray( len ) {
return new Array( len ).fill( 0 ).map(function mapFn( _, idx ) {
return idx + 1;
});
}

arr = new Float64ArrayFE( 'little-endian', generateArray( len ) );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
arr.copyWithin( 0, len - 1 );
if ( arr.get( 0 ) !== len ) {
b.fail( 'unexpected result' );
}
}
b.toc();
if ( arr.at( 0 ) !== arr.at( 0 ) ) {
b.fail( 'should not be 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( pkg + ':copyWithin:len=' + len, f );
}
}

main();
88 changes: 88 additions & 0 deletions lib/node_modules/@stdlib/array/fixed-endian-factory/lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ var contains = require( '@stdlib/array/base/assert/contains' ).factory;
var bytesPerElement = require( '@stdlib/ndarray/base/bytes-per-element' );
var capitalize = require( '@stdlib/string/base/capitalize' );
var format = require( '@stdlib/string/format' );
var max = require( '@stdlib/math/base/special/max' );
var min = require( '@stdlib/math/base/special/min' );
var fromIterator = require( './from_iterator.js' );
var fromIteratorMap = require( './from_iterator_map.js' );

Expand Down Expand Up @@ -488,6 +490,92 @@ function factory( dtype ) { // eslint-disable-line max-lines-per-function, stdli
return this._buffer[ GETTER ]( idx * BYTES_PER_ELEMENT, this._isLE );
});

/**
* Copies a sequence of array elements from `start` to `end` within the array to the position starting at `target`.
*
* @private
* @name copyWithin
* @memberof TypedArray.prototype
* @type {Function}
* @param {integer} target - index at which to copy the sequence to
* @param {integer} start - index at which to start copying the sequence from
* @param {integer} end - index at which to end copying the sequence (optional)
* @throws {TypeError} `this` is not a typed array
* @throws {TypeError} `target` must be an integer
* @throws {TypeError} `start` must be an integer
* @throws {TypeError} `end` must be an integer
* @returns {void}
*/
setReadOnly( TypedArray.prototype, 'copyWithin', function copyWithin( target, start ) {
var copyDirection;
var len;
var end;
var cnt;
var buf;

if ( !isTypedArray( this ) ) {
throw new TypeError( format( 'invalid invocation. `this` is not %s %s.', CHAR2ARTICLE[ dtype[0] ], CTOR_NAME ) );
}
if ( arguments.length < 2 || arguments.length > 3 ) {
throw new TypeError( format( 'invalid invocation. Unsupported number of arguments. Value: `%s`.', arguments.length ) );
}
if ( !isInteger( target ) ) {
throw new TypeError( format( 'invalid argument. Must provide an integer. Value: `%s`.', target ) );
}
if ( !isInteger( start ) ) {
throw new TypeError( format( 'invalid argument. Must provide an integer. Value: `%s`.', start ) );
}

len = this._length;

if ( target >= len || start >= len ) {
return;
}

if ( arguments.length === 3 ) {
end = arguments[ 2 ];
if ( !isInteger( end ) ) {
throw new TypeError( format( 'invalid argument. Must provide an integer. Value: `%s`.', end ) );
}
if ( end < 0 ) {
end = max(len + end, 0);
}
end = min( end, len );
} else {
end = len;
}

if ( target < 0 ) {
target = max(len + target, 0);
}
if ( start < 0 ) {
start = max(len + start, 0);
}

cnt = min( end - start, len - target );

if ( cnt <= 0 ) {
return;
}

buf = this._buffer;

if ( start < target && target < ( start + cnt ) ) {
copyDirection = -1;
start = start + cnt - 1;
target = target + cnt - 1;
} else {
copyDirection = 1;
}

while ( cnt > 0 ) {
buf[ SETTER ]( target*BYTES_PER_ELEMENT, buf[ GETTER ]( start*BYTES_PER_ELEMENT, this._isLE ), this._isLE );
target += copyDirection;
start += copyDirection;
cnt -= 1;
}
});

/**
* Pointer to the underlying data buffer.
*
Expand Down
Loading