Skip to content

feat: add subarray prototype method in fixed-endian-factory #3272

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

Open
wants to merge 3 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
91 changes: 91 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 @@ -837,6 +837,97 @@ A few notes:
- If a target array cannot accommodate all values (i.e., the length of source array plus `i` exceeds the target array length), the method throws an error.
- If provided a typed array which shares an [`ArrayBuffer`][@stdlib/array/buffer] with the target array, the method will intelligently copy the source range to the destination range.

<a name="method-subarray"></a>

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

Copies a portion of a typed array to a new typed array.

```javascript

var fixedEndianFactory = require( '@stdlib/array/fixed-endian-factory' );

var Float64ArrayFE = fixedEndianFactory( 'float64' );

var arr = new Float64ArrayFE( 'little-endian', 5 );
// returns <Float64ArrayFE>

arr.set( 0, 0 );
arr.set( 1, 1 );
arr.set( 2, 2 );
arr.set( 3, 3 );
arr.set( 4, 4 );

var out = arr.subarray();
// returns <Float64ArrayFE>

var len = out.length;
// returns 5

var bool = out.get( 0 );
// returns 0

bool = out.get( len-1 );
// returns 4
```

By default, the method returns a typed array beginning with the first array element. To specify an alternative array index at which to begin, provide a `start` index (inclusive).

```javascript
var fixedEndianFactory = require( '@stdlib/array/fixed-endian-factory' );

var Float64ArrayFE = fixedEndianFactory( 'float64' );

var arr = new Float64ArrayFE( 'little-endian', 5 );
// returns <Float64ArrayFE>

arr.set( 0, 0 );
arr.set( 1, 1 );
arr.set( 2, 2 );
arr.set( 3, 3 );
arr.set( 4, 4 );

var out = arr.subarray( 1 );
// returns <Float64ArrayFE>

var len = out.length;
// returns 4

var bool = out.get( 0 );
// returns 1

bool = out.get( len-1 );
// returns 4
```

By default, the method returns a typed array which includes all array elements after `start`. To limit the number of array elements after `start`, provide an `end` index (exclusive).

```javascript
var fixedEndianFactory = require( '@stdlib/array/fixed-endian-factory' );

var Float64ArrayFE = fixedEndianFactory( 'float64' );

var arr = new Float64ArrayFE( 'little-endian', 5 );

arr.set( 0, 0 );
arr.set( 1, 1 );
arr.set( 2, 2 );
arr.set( 3, 3 );
arr.set( 4, 4 );

var out = arr.subarray( 1, -2 );
// returns <Float64ArrayFE>

var len = out.length;
// returns 2

var bool = out.get( 0 );
// returns 1

bool = out.get( len-1 );
// returns 2
```

<a name="method-some"></a>

#### TypedArrayFE.prototype.some( predicate\[, thisArg] )
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
* @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 pkg = require( './../package.json' ).name;
var factory = require( './../lib' );


// VARIABLES //

var Float64ArrayFE = factory( 'float64' );


// MAIN //

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

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

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
out = arr.subarray(1, 2);
if ( out instanceof Float64ArrayFE === false ) {
b.fail( 'should return a TypedArray' );
}
}
b.toc();
if ( out instanceof Float64ArrayFE === false ) {
b.fail( 'should return a TypedArray' );
}
b.pass( 'benchmark finished' );
b.end();
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2023 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' );


// FUNCTIONS //

/**
* Creates a benchmark function.
*
* @private
* @param {PositiveInteger} len - array length
* @returns {Function} benchmark function
*/
function createBenchmark( len ) {
var Float64Array;
var arr;
var i;
Float64Array = factory( 'float64' );
arr = [];
for ( i = 0; i < len; i++ ) {
arr.push( i );
}
arr = new Float64Array( 'little-endian', arr );

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 = arr.subarray();
if ( typeof out !== 'object' ) {
b.fail( 'should return an object' );
}
}
b.toc();
if ( out instanceof Float64Array === false ) {
b.fail( 'should return a TypedArray' );
}
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+':subarray:len='+len, f );
}
}

main();
96 changes: 96 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 @@ -1058,6 +1058,101 @@ function factory( dtype ) { // eslint-disable-line max-lines-per-function, stdli
buf[ SETTER ]( idx*BYTES_PER_ELEMENT, value, this._isLE );
});

/**
* Copies a portion of a typed array to a new typed array.
* @name subarray
* @memberof TypedArray.prototype
* @type {Function}
* @param {integer} [begin] - start index (inclusive)
* @param {integer} [end] - end index (exclusive)
* @throws {TypeError} `this` must be a typed array instance
* @throws {RangeError} first argument must be in the range of `[0, this.length -1]` while second argument must be in the range of `[0, this.length]`
* @throws {TypeError} first argument must be integer, and second argument must be integer
* @returns {TypedArray} TypedArray array
*
* @example
* var Float64Array = factory('float64');
*
* var arr = new Float64Array( 'little-endian' , [0.0, 1.0, 2.0, 3.0, 4.0] );
*
* var out = arr.subarray();
* // returns <Float64Array>
*
* var len = out.length;
* // returns 5
*
* var bool = out.get( 0 );
* // returns 0.0
*
* bool = out.get( len-1 );
* // returns 4.0
*
* out = arr.subarray( 1, -2 );
* // returns <Float64Array>
*
* len = out.length;
* // returns 2
*
* bool = out.get( 0 );
* // returns 1.0
*
* bool = out.get( len-1 );
* // returns 2.0
*/
setReadOnly( TypedArray.prototype, 'subarray', function subarray( begin, end ) {
var outbuf;
var out;
var buf;
var len;
var i;
outbuf = [];
if ( !isTypedArray( this ) ) {
throw new TypeError( 'invalid invocation. `this` is not a typed array.' );
}
buf = this._buffer;
len = this._length;
if ( arguments.length === 0 ) {
begin = 0;
end = len;
} else {
if ( !isInteger( begin ) ) {
throw new TypeError( format( 'invalid argument. First argument must be an integer. Value: `%s`.', begin ) );
}
if ( begin < 0 ) {
begin += len;
if ( begin < 0 ) {
begin = 0;
}
}
if ( arguments.length === 1 ) {
end = len;
} else {
if ( !isInteger( end ) ) {
throw new TypeError( format( 'invalid argument. Second argument must be an integer. Value: `%s`.', end ) );
}
if ( end < 0 ) {
end += len;
if ( end < 0 ) {
end = 0;
}
} else if ( end > len ) {
end = len;
}
}
}

for ( i = begin; i < end; i++ ) {
outbuf[ i - begin ] = buf[ GETTER ]( i * BYTES_PER_ELEMENT, this._isLE );
}

if (this._isLE) {
out = new this.constructor( 'little-endian', outbuf );
} else {
out = new this.constructor( 'big-endian', outbuf );
}
return out;
});

/**
* Tests whether at least one element in the typed array passes a test implemented by a predicate function.
*
Expand All @@ -1070,6 +1165,7 @@ function factory( dtype ) { // eslint-disable-line max-lines-per-function, stdli
* @throws {TypeError} first argument must be a function
* @returns {boolean} boolean indicating whether at least one element passes a test
*/

setReadOnly( TypedArray.prototype, 'some', function some( predicate, thisArg ) {
var buf;
var i;
Expand Down
Loading
Loading