diff --git a/lib/node_modules/@stdlib/array/fixed-endian-factory/README.md b/lib/node_modules/@stdlib/array/fixed-endian-factory/README.md index ab659bbc55ba..058444457d44 100644 --- a/lib/node_modules/@stdlib/array/fixed-endian-factory/README.md +++ b/lib/node_modules/@stdlib/array/fixed-endian-factory/README.md @@ -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. + + +#### 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 + +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 + +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 + +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 + +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 + +var len = out.length; +// returns 2 + +var bool = out.get( 0 ); +// returns 1 + +bool = out.get( len-1 ); +// returns 2 +``` + #### TypedArrayFE.prototype.some( predicate\[, thisArg] ) diff --git a/lib/node_modules/@stdlib/array/fixed-endian-factory/benchmark/benchmark.subarray.js b/lib/node_modules/@stdlib/array/fixed-endian-factory/benchmark/benchmark.subarray.js new file mode 100644 index 000000000000..2f39580087af --- /dev/null +++ b/lib/node_modules/@stdlib/array/fixed-endian-factory/benchmark/benchmark.subarray.js @@ -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(); +}); diff --git a/lib/node_modules/@stdlib/array/fixed-endian-factory/benchmark/benchmark.subarray.length.js b/lib/node_modules/@stdlib/array/fixed-endian-factory/benchmark/benchmark.subarray.length.js new file mode 100644 index 000000000000..9c832518b6d3 --- /dev/null +++ b/lib/node_modules/@stdlib/array/fixed-endian-factory/benchmark/benchmark.subarray.length.js @@ -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(); diff --git a/lib/node_modules/@stdlib/array/fixed-endian-factory/lib/main.js b/lib/node_modules/@stdlib/array/fixed-endian-factory/lib/main.js index 079902a6c333..3a3dbe4b1039 100644 --- a/lib/node_modules/@stdlib/array/fixed-endian-factory/lib/main.js +++ b/lib/node_modules/@stdlib/array/fixed-endian-factory/lib/main.js @@ -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 + * + * 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 + * + * 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. * @@ -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; diff --git a/lib/node_modules/@stdlib/array/fixed-endian-factory/test/test.subarray.js b/lib/node_modules/@stdlib/array/fixed-endian-factory/test/test.subarray.js new file mode 100644 index 000000000000..37d1a7b3311d --- /dev/null +++ b/lib/node_modules/@stdlib/array/fixed-endian-factory/test/test.subarray.js @@ -0,0 +1,262 @@ +/** +* @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 tape = require( 'tape' ); +var hasOwnProp = require( '@stdlib/assert/has-own-property' ); +var isFunction = require( '@stdlib/assert/is-function' ); +var instanceOf = require( '@stdlib/assert/instance-of' ); +var factory = require( './../lib' ); +var Float64Array = factory( 'float64' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof factory, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'attached to the prototype of the main export is a `subarray` method', function test( t ) { + t.strictEqual( hasOwnProp( Float64Array.prototype, 'subarray' ), true, 'has property' ); + t.strictEqual( isFunction( Float64Array.prototype.subarray ), true, 'has method' ); + t.end(); +}); + +tape( 'the method throws an error if invoked with a `this` context which is not a typed array instance', function test( t ) { + var values; + var arr; + var i; + + arr = new Float64Array( 'little-endian', 5 ); + + 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() { + return arr.subarray.call( value ); + }; + } +}); + +tape( 'the method throws an error if provided a first argument which is not an integer', function test( t ) { + var values; + var arr; + var i; + + arr = new Float64Array( 'little-endian', 10 ); + + values = [ + '5', + 3.14, + NaN, + true, + false, + null, + void 0, + {}, + [] + ]; + 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() { + return arr.subarray( value ); + }; + } +}); + +tape( 'the method throws an error if provided a second argument which is not an integer', function test( t ) { + var values; + var arr; + var i; + + arr = new Float64Array( 'little-endian', 10 ); + + values = [ + '5', + 3.14, + NaN, + true, + false, + null, + void 0, + {}, + [] + ]; + 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() { + return arr.subarray( 0, value ); + }; + } +}); + +tape( 'the method returns empty array if operating on an empty typed array', function test( t ) { + var arr; + var out; + + arr = new Float64Array( 'little-endian' ); + out = arr.subarray(); + + t.strictEqual( out.length, 0, 'returns expected value' ); + t.end(); +}); + +tape( 'if called without arguments, the method returns a view containing the same elements as the original array', function test( t ) { + var expected; + var actual; + var arr; + + arr = new Float64Array( 'little-endian', [ 0, 1, 2, 3, 4 ] ); + expected = new Float64Array( 'little-endian', [ 0, 1, 2, 3, 4 ] ); + actual = arr.subarray(); + + t.strictEqual( instanceOf( actual, Float64Array ), true, 'returns expected value' ); + t.strictEqual( actual.length, expected.length, 'returns expected value' ); + t.deepEqual( arr, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'if called with one argument, the method returns a view containing elements starting from a specified beginning index (inclusive)', function test( t ) { + var expected; + var actual; + var arr; + + arr = new Float64Array( 'little-endian', [ 0, 1, 2, 3, 4 ] ); + expected = new Float64Array( 'little-endian', [ 1, 2, 3, 4 ] ); + actual = arr.subarray( 1 ); + + t.strictEqual( instanceOf( actual, Float64Array ), true, 'returns expected value' ); + t.strictEqual( actual.length, expected.length, 'returns expected value' ); + t.deepEqual( arr, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'if provided two arguments, the method returns a view containing elements starting from a specified beginning index (inclusive) and ending at a specified stop index (exclusive)', function test( t ) { + var expected; + var actual; + var arr; + + arr = new Float64Array( 'little-endian', [ 0, 1, 2, 3, 4 ] ); + expected = new Float64Array( 'little-endian', [ 1, 2 ] ); + actual = arr.subarray( 1, 3 ); + + t.strictEqual( instanceOf( actual, Float64Array ), true, 'returns expected value' ); + t.strictEqual( actual.length, expected.length, 'returns expected value' ); + t.deepEqual( arr, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the method resolves negative indices relative to the last element', function test( t ) { + var expected; + var actual; + var arr; + + arr = new Float64Array( 'little-endian', [ 0, 1, 2, 3, 4 ] ); + + expected = new Float64Array( 'little-endian', [2, 3] ); + + actual = arr.subarray( -3, -1 ); + t.strictEqual( instanceOf( actual, Float64Array ), true, 'returns expected value' ); + t.strictEqual( actual.length, expected.length, 'returns expected value' ); + t.deepEqual( arr, expected, 'returns expected value' ); + + expected = new Float64Array( 'little-endian', [ 0, 1, 2 ] ); + actual = arr.subarray( -30, -2 ); + t.strictEqual( instanceOf( actual, Float64Array ), true, 'returns expected value' ); + t.strictEqual( actual.length, expected.length, 'returns expected value' ); + t.deepEqual( arr, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the method returns an empty view if a resolved beginning index exceeds a resolved ending index', function test( t ) { + var expected; + var actual; + var arr; + + arr = new Float64Array( 'little-endian', [ 0, 1, 2, 3, 4 ] ); + expected = new Float64Array( 'little-endian' ); + actual = arr.subarray( 2, 0 ); + + t.strictEqual( instanceOf( actual, Float64Array ), true, 'returns expected value' ); + t.strictEqual( actual.length, expected.length, 'returns expected value' ); + t.deepEqual( arr, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the method returns an empty view if a resolved beginning index exceeds the maximum array index', function test( t ) { + var expected; + var actual; + var arr; + + arr = new Float64Array( 'little-endian', [ 0, 1, 2, 3, 4 ] ); + expected = new Float64Array( 'little-endian' ); + actual = arr.subarray( 5 ); + + t.strictEqual( instanceOf( actual, Float64Array ), true, 'returns expected value' ); + t.strictEqual( actual.length, expected.length, 'returns expected value' ); + t.deepEqual( arr, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the method returns an empty view if a resolved ending index is less than or equal to zero', function test( t ) { + var expected; + var actual; + var arr; + + arr = new Float64Array( 'little-endian', [ 0, 1, 2, 3, 4 ] ); + expected = new Float64Array( 'little-endian' ); + + actual = arr.subarray( 2, -8 ); + t.strictEqual( instanceOf( actual, Float64Array ), true, 'returns expected value' ); + t.strictEqual( actual.length, expected.length, 'returns expected value' ); + t.deepEqual( arr, expected, 'returns expected value' ); + + actual = arr.subarray( 1, 0 ); + t.strictEqual( instanceOf( actual, Float64Array ), true, 'returns expected value' ); + t.strictEqual( actual.length, expected.length, 'returns expected value' ); + t.deepEqual( arr, expected, 'returns expected value' ); + t.end(); +});