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 c92a71895e80..0c01b39c0a3f 100644 --- a/lib/node_modules/@stdlib/array/fixed-endian-factory/README.md +++ b/lib/node_modules/@stdlib/array/fixed-endian-factory/README.md @@ -511,6 +511,47 @@ var count = context.count; // returns 3 ``` + + +### TypedArrayFE.prototype.values() + +Returns an iterator for the values of the typed array. + +```javascript +var Float64ArrayFE = fixedEndianFactory( 'float64' ); + +var arr = new Float64ArrayFE( 'little-endian', [ 1.0, 2.0, 3.0 ] ); +// returns + +var iter = arr.values(); + +var result = iter.next(); +// returns { 'value': 1.0, 'done': false } + +result = iter.next(); +// returns { 'value': 2.0, 'done': false } + +result = iter.next(); +// returns { 'value': 3.0, 'done': false } + +result = iter.next(); +// returns { 'done': true } +``` + +use the iterator with a `for...of` loop. + +```javascript +var value; +for ( value of arr.values() ) { + console.log( value ); +} +// print each value of array +// 1.0 +// 2.0 +// 3.0 +``` + + #### TypedArrayFE.prototype.get( i ) diff --git a/lib/node_modules/@stdlib/array/fixed-endian-factory/benchmark/benchmark.values.js b/lib/node_modules/@stdlib/array/fixed-endian-factory/benchmark/benchmark.values.js new file mode 100644 index 000000000000..77e19b62e1cf --- /dev/null +++ b/lib/node_modules/@stdlib/array/fixed-endian-factory/benchmark/benchmark.values.js @@ -0,0 +1,57 @@ +/** + * @license Apache-2.0 + * + * Copyright (c) 2025 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 pkg = require( './../package.json' ).name; +var factory = require( './../lib' ); + + +// VARIABLES // + +var Float64ArrayFE = factory( 'float64' ); + + +// MAIN // + +bench( pkg+':values', function benchmark( b ) { + var value; + var iter; + var arr; + var i; + + arr = new Float64ArrayFE( 'little-endian', [ 1.0, 2.0, 2.0, 1.0 ] ); + iter = arr.values(); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + iter = arr.values(); + for ( value of iter ) { + if ( isnan( value ) ) { + b.fail( 'should not return NaN' ); + } + } + } + b.toc(); + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/lib/node_modules/@stdlib/array/fixed-endian-factory/benchmark/benchmark.values.length.js b/lib/node_modules/@stdlib/array/fixed-endian-factory/benchmark/benchmark.values.length.js new file mode 100644 index 000000000000..f2e8dd328cea --- /dev/null +++ b/lib/node_modules/@stdlib/array/fixed-endian-factory/benchmark/benchmark.values.length.js @@ -0,0 +1,109 @@ +/** + * @license Apache-2.0 + * + * Copyright (c) 2025 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 zeroTo = require( '@stdlib/utils/zero-to' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var pkg = require( './../package.json' ).name; +var factory = require( './../lib' ); + + +// VARIABLES // + +var Float64ArrayFE = factory( 'float64' ); + + +// FUNCTIONS // + +/** + * Creates a benchmark function for `values` iterator. + * + * @private + * @param {PositiveInteger} len - array length + * @returns {Function} benchmark function + */ +function createBenchmark( len ) { + var arr = new Float64ArrayFE( 'little-endian', zeroTo( len ) ); + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + * @throws {Error} throws an error if NaN value is encountered + */ + function benchmark( b ) { + var value; + var iter; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + iter = arr.values(); // Get the iterator for values + + // Use for...of loop to iterate over the values + for ( value of iter ) { + if ( isnan( value ) ) { + throw new Error( 'something went wrong' ); + } + } + if ( arr.length !== len ) { + b.fail( 'should not change an array length' ); + } + } + b.toc(); + if ( arr.length !== len ) { + b.fail( 'should not change an array length' ); + } + 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+':values: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 414280f7f2ae..93faabc2b53f 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 @@ -1095,6 +1095,47 @@ function factory( dtype ) { // eslint-disable-line max-lines-per-function, stdli return false; }); + /** + * Returns an iterator for array elements. + * + * @private + * @name values + * @memberof TypedArray.prototype + * @type {Function} + * @throws {TypeError} `this` must be a typed array instance + * @returns {Object} iterator for array elements + */ + setReadOnly( TypedArray.prototype, 'values', function values() { + var iterator; + var index = 0; + var value; + var isLE = this._isLE; + var buf = this._buffer; + var len = this._length; + if ( !isTypedArray( this ) ) { + throw new TypeError( format( 'invalid invocation. `this` is not %s %s.', CHAR2ARTICLE[ dtype[0] ], CTOR_NAME ) ); + } + iterator = { + 'next': function next() { + if ( index < len ) { + value = buf[ GETTER ]( index * BYTES_PER_ELEMENT, isLE ); + index += 1; + return { + 'value': value, + 'done': false + }; + } + return { + 'done': true + }; + } + }; + iterator[ Symbol.iterator ] = function iterator() { + return this; + }; + return iterator; + }); + /** * Serializes an array as a string. * diff --git a/lib/node_modules/@stdlib/array/fixed-endian-factory/test/test.values.js b/lib/node_modules/@stdlib/array/fixed-endian-factory/test/test.values.js new file mode 100644 index 000000000000..4b9ca1e9d40f --- /dev/null +++ b/lib/node_modules/@stdlib/array/fixed-endian-factory/test/test.values.js @@ -0,0 +1,223 @@ +/** + * @license Apache-2.0 + * + * Copyright (c) 2025 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 factory = require( './../lib' ); + + +// 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( 'the `values` method returns an iterator function', function test( t ) { + var iterator; + var ctor = factory( 'float64' ); + var arr = new ctor( 'little-endian', [1.0, 2.0, 3.0, 4.0, 5.0] ); + iterator = arr.values(); + + t.strictEqual( isFunction( iterator.next ), true, 'returns expected iterator' ); + t.end(); +}); + +tape( 'attached to the prototype of the returned function is a `values` method', function test( t ) { + var ctor = factory( 'float64' ); + t.strictEqual( hasOwnProp( ctor.prototype, 'values' ), true, 'has `values` method on the prototype' ); + t.strictEqual( isFunction( ctor.prototype.values ), true, '`values` is a function' ); + t.end(); +}); + +tape( 'the `values` iterator stops when all elements are iterated', function test( t ) { + var iterator; + var result; + var ctor = factory( 'float64' ); + var arr = new ctor( 'little-endian', [ 1.0, 2.0, 3.0, 4.0, 5.0 ] ); + + iterator = arr.values(); + + result = iterator.next(); + t.deepEqual( result, { + 'value': 1.0, + 'done': false + }, 'returns expected value' ); + + result = iterator.next(); + t.deepEqual( result, { + 'value': 2.0, + 'done': false + }, 'returns expected value' ); + + result = iterator.next(); + t.deepEqual( result, { + 'value': 3.0, + 'done': false + }, 'returns expected value' ); + + result = iterator.next(); + t.deepEqual( result, { + 'value': 4.0, + 'done': false + }, 'returns expected value' ); + + result = iterator.next(); + t.deepEqual( result, { + 'value': 5.0, + 'done': false + }, 'returns expected value' ); + + result = iterator.next(); + t.deepEqual( result, { + 'done': true + }, 'iteration is done' ); + + t.end(); +}); + +tape( 'the `values` iterator works on empty arrays', function test( t ) { + var iterator; + var result; + var ctor = factory( 'float64' ); + var arr = new ctor( 'little-endian' ); + + iterator = arr.values(); + result = iterator.next(); + t.deepEqual( result, { + 'done': true + }, 'returns expected result' ); + + t.end(); +}); + +tape( 'the `values` iterator returns the correct value for typed arrays with different data types', function test( t ) { + var iterator; + var result; + var ctor = factory( 'float64' ); + var arr = new ctor( 'little-endian', [ 10.5, 20.5, 30.5 ] ); + + iterator = arr.values(); + + result = iterator.next(); + t.deepEqual( result, { + 'value': 10.5, + 'done': false + }, 'returns correct value' ); + + result = iterator.next(); + t.deepEqual( result, { + 'value': 20.5, + 'done': false + }, 'returns correct value' ); + + result = iterator.next(); + t.deepEqual( result, { + 'value': 30.5, + 'done': false + }, 'returns correct value' ); + + result = iterator.next(); + t.deepEqual( result, { + 'done': true + }, 'iteration is done' ); + + t.end(); +}); + +tape( 'the `values` iterator works with `for...of` loop', function test( t ) { + var values = []; + var value; + var ctor = factory( 'float64' ); + var arr = new ctor( 'little-endian', [ 1.0, 2.0, 3.0, 4.0, 5.0 ] ); + + for ( value of arr.values() ) { + values.push( value ); + } + + t.deepEqual( values, [ 1.0, 2.0, 3.0, 4.0, 5.0 ], 'correctly iterates over values' ); + t.end(); +}); + +tape( 'the `values` iterator supports providing an execution context', function test( t ) { + var iterator; + var result; + var ctor = factory( 'float64' ); + var arr = new ctor( 'little-endian', [ 1.0, 2.0, 3.0, 4.0, 5.0 ] ); + var ctx = { + 'threshold': 3 + }; + iterator = arr.values(); + + result = iterator.next.call( ctx ); + t.deepEqual( result, { + 'value': 1.0, + 'done': false + }, 'returns correct value' ); + + result = iterator.next.call( ctx ); + t.deepEqual( result, { + 'value': 2.0, + 'done': false + }, 'returns correct value' ); + + result = iterator.next.call( ctx ); + t.deepEqual( result, { + 'value': 3.0, + 'done': false + }, 'returns correct value' ); + + t.end(); +}); + +tape( 'the `values` iterator throws an error when `this` is not a typed array', function test( t ) { + var values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + {}, + [], + function noop() {} + ]; + + var ctor = factory( 'float64' ); + var arr = new ctor( 'little-endian', [ 1.0, 2.0, 3.0, 4.0 ] ); + var i; + + 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.values.call( value ); + }; + } +});