Skip to content

Commit 2a10592

Browse files
committed
feat: add copywithin logic
1 parent 28db4f2 commit 2a10592

File tree

4 files changed

+557
-0
lines changed

4 files changed

+557
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var factory = require( './../lib' );
25+
var pkg = require( './../package.json' ).name;
26+
27+
28+
// VARIABLES //
29+
30+
var Float64ArrayFE = factory( 'float64' );
31+
32+
33+
// MAIN //
34+
35+
bench( pkg+':copyWithin', function benchmark( b ) {
36+
var arr;
37+
var i;
38+
39+
arr = new Float64ArrayFE( 'little-endian', [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0 ] );
40+
41+
b.tic();
42+
for ( i = 0; i < b.iterations; i++ ) {
43+
arr.copyWithin( 0, 6 );
44+
if ( arr.get( 0 ) !== 7.0 ) {
45+
b.fail( 'unexpected result' );
46+
}
47+
}
48+
b.toc();
49+
if ( arr.at( 0 ) !== arr.at( 0 ) ) {
50+
b.fail( 'should not be NaN' );
51+
}
52+
b.pass( 'benchmark finished' );
53+
b.end();
54+
});
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var pow = require( '@stdlib/math/base/special/pow' );
25+
var zeroTo = require( '@stdlib/array/zero-to' );
26+
var pkg = require( './../package.json' ).name;
27+
var factory = require( './../lib' );
28+
29+
30+
// VARIABLES //
31+
32+
var Float64ArrayFE = factory( 'float64' );
33+
34+
35+
// FUNCTIONS //
36+
37+
/**
38+
* Creates a benchmark function.
39+
*
40+
* @private
41+
* @param {PositiveInteger} len - array length
42+
* @returns {Function} benchmark function
43+
*/
44+
function createBenchmark( len ) {
45+
return function benchmark( b ) {
46+
var arr;
47+
var i;
48+
49+
arr = new Float64ArrayFE( 'little-endian', zeroTo( len ) );
50+
51+
b.tic();
52+
for ( i = 0; i < b.iterations; i++ ) {
53+
arr.copyWithin( 0, len - 1 );
54+
if ( arr.get( 0 ) !== len ) {
55+
b.fail( 'unexpected result' );
56+
}
57+
}
58+
b.toc();
59+
if ( arr.at( 0 ) !== arr.at( 0 ) ) {
60+
b.fail( 'should not be NaN' );
61+
}
62+
63+
b.pass( 'benchmark finished' );
64+
b.end();
65+
};
66+
}
67+
68+
69+
// MAIN //
70+
71+
/**
72+
* Main execution sequence.
73+
*
74+
* @private
75+
*/
76+
function main() {
77+
var len;
78+
var min;
79+
var max;
80+
var f;
81+
var i;
82+
83+
min = 1; // 10^min
84+
max = 6; // 10^max
85+
86+
for ( i = min; i <= max; i++ ) {
87+
len = pow( 10, i );
88+
f = createBenchmark( len );
89+
bench( pkg + ':copyWithin:len=' + len, f );
90+
}
91+
}
92+
93+
main();

lib/node_modules/@stdlib/array/fixed-endian-factory/lib/main.js

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ var contains = require( '@stdlib/array/base/assert/contains' ).factory;
4444
var bytesPerElement = require( '@stdlib/ndarray/base/bytes-per-element' );
4545
var capitalize = require( '@stdlib/string/base/capitalize' );
4646
var format = require( '@stdlib/string/format' );
47+
var max = require( '@stdlib/math/base/special/max' );
48+
var min = require( '@stdlib/math/base/special/min' );
4749
var fromIterator = require( './from_iterator.js' );
4850
var fromIteratorMap = require( './from_iterator_map.js' );
4951

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

493+
/**
494+
* Copies a sequence of array elements from `start` to `end` within the array to the position starting at `target`.
495+
*
496+
* @private
497+
* @name copyWithin
498+
* @memberof TypedArray.prototype
499+
* @type {Function}
500+
* @param {integer} target - index at which to copy the sequence to
501+
* @param {integer} start - index at which to start copying the sequence from
502+
* @param {integer} end - index at which to end copying the sequence (optional)
503+
* @throws {TypeError} `this` is not a typed array
504+
* @throws {TypeError} `target` must be an integer
505+
* @throws {TypeError} `start` must be an integer
506+
* @throws {TypeError} `end` must be an integer
507+
* @returns {void}
508+
*/
509+
setReadOnly( TypedArray.prototype, 'copyWithin', function copyWithin( target, start ) {
510+
var copyDirection;
511+
var len;
512+
var end;
513+
var cnt;
514+
var buf;
515+
516+
if ( !isTypedArray( this ) ) {
517+
throw new TypeError( format( 'invalid invocation. `this` is not %s %s.', CHAR2ARTICLE[ dtype[0] ], CTOR_NAME ) );
518+
}
519+
if ( arguments.length < 2 || arguments.length > 3 ) {
520+
throw new TypeError( format( 'invalid invocation. Unsupported number of arguments. Value: `%s`.', arguments.length ) );
521+
}
522+
if ( !isInteger( target ) ) {
523+
throw new TypeError( format( 'invalid argument. Must provide an integer. Value: `%s`.', target ) );
524+
}
525+
if ( !isInteger( start ) ) {
526+
throw new TypeError( format( 'invalid argument. Must provide an integer. Value: `%s`.', start ) );
527+
}
528+
529+
len = this._length;
530+
531+
if ( target >= len || start >= len ) {
532+
return;
533+
}
534+
535+
if ( arguments.length === 3 ) {
536+
end = arguments[ 2 ];
537+
if ( !isInteger( end ) ) {
538+
throw new TypeError( format( 'invalid argument. Must provide an integer. Value: `%s`.', end ) );
539+
}
540+
if ( end < 0 ) {
541+
end = max(len + end, 0);
542+
}
543+
end = min( end, len );
544+
} else {
545+
end = len;
546+
}
547+
548+
if ( target < 0 ) {
549+
target = max(len + target, 0);
550+
}
551+
if ( start < 0 ) {
552+
start = max(len + start, 0);
553+
}
554+
555+
cnt = min( end - start, len - target );
556+
557+
if ( cnt <= 0 ) {
558+
return;
559+
}
560+
561+
buf = this._buffer;
562+
563+
if ( start < target && target < ( start + cnt ) ) {
564+
copyDirection = -1;
565+
start = start + cnt - 1;
566+
target = target + cnt - 1;
567+
} else {
568+
copyDirection = 1;
569+
}
570+
571+
while ( cnt > 0 ) {
572+
buf[ SETTER ]( target*BYTES_PER_ELEMENT, buf[ GETTER ]( start*BYTES_PER_ELEMENT, this._isLE ), this._isLE );
573+
target += copyDirection;
574+
start += copyDirection;
575+
cnt -= 1;
576+
}
577+
});
578+
491579
/**
492580
* Pointer to the underlying data buffer.
493581
*

0 commit comments

Comments
 (0)