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..fb42266e1813 100644 --- a/lib/node_modules/@stdlib/array/fixed-endian-factory/README.md +++ b/lib/node_modules/@stdlib/array/fixed-endian-factory/README.md @@ -958,6 +958,60 @@ var v = out.get( 0 ); * * * + + +#### TypedArrayFE.prototype.entries() + +Return an iterator for iterating over array as key-value pairs. + +```javascript +var factory = require( '@stdlib/array/fixed-endian-factory' ); +var int32arr = factory( 'int32' ); +var arr = int32arr( 'little-endian', [ 1, 2, 3, 4, 5 ] ); + +// Creat an iterator: +var itr = arr.entries(); + +// Iterate over the key-value pairs... +var v = itr.next().value; +// returns [ 0, 1 ]; + +var i = v[ 1 ]; +// returns 1; + +v = itr.next().value; +// returns [ 1, 2 ]; + +i = v[ 1 ]; +// returns 2; + +v = itr.next().value; +// returns [ 2, 3 ]; + +i = v[ 1 ]; +// returns 3; + +v = itr.next().value; +// returns [ 3, 4 ] + +i = v[ 1 ]; +// returns 4 + +v = itr.next().value; +// returns [ 4, 5 ]; + +i = v[ 1 ]; +// returns 5 + +var bool = itr.next().done; +// returns true +``` + +The returned [iterator][mdn-iterator-protocol] protocol-compliant object has the following properties: + +- **next**: function which returns an [iterator][mdn-iterator-protocol] protocol-compliant object containing the next iterated value (if one exists) assigned to a `value` property and a `done` property having a `boolean` value indicating whether the [iterator][mdn-iterator-protocol] is finished. +- **return**: function which closes an [iterator][mdn-iterator-protocol] and returns a single (optional) argument in an [iterator][mdn-iterator-protocol] protocol-compliant object. + ## Notes - A returned constructor supports the following byte orders: @@ -1035,6 +1089,8 @@ logEach( '%s', out ); + diff --git a/lib/node_modules/@stdlib/array/fixed-endian-factory/benchmark/benchmark.entries.js b/lib/node_modules/@stdlib/array/fixed-endian-factory/benchmark/benchmark.entries.js new file mode 100644 index 000000000000..f3c6a3e3a2ad --- /dev/null +++ b/lib/node_modules/@stdlib/array/fixed-endian-factory/benchmark/benchmark.entries.js @@ -0,0 +1,53 @@ +/** +* @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' ); + + +// MAIN // + +bench( pkg+':entries:len=10', function benchmark( b ) { + var iter; + var arr; + var i; + + arr = new factory( 'int32' )( 'little-endian', 10 ); + for ( i = 0; i < 10; i++ ) { + arr.set( i, i ); + } + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + iter = arr.entries(); + if ( typeof iter !== 'object' ) { + b.fail( 'should return an object' ); + } + } + b.toc(); + if ( typeof iter !== 'object' || typeof iter.next !== 'function' ) { + b.fail( 'should return an iterator protocol-compliant object' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/lib/node_modules/@stdlib/array/fixed-endian-factory/benchmark/benchmark.entries.length.js b/lib/node_modules/@stdlib/array/fixed-endian-factory/benchmark/benchmark.entries.length.js new file mode 100644 index 000000000000..91df88196dc5 --- /dev/null +++ b/lib/node_modules/@stdlib/array/fixed-endian-factory/benchmark/benchmark.entries.length.js @@ -0,0 +1,99 @@ +/** +* @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 ) { + var arr = new Float64ArrayFE( 'little-endian', len ); + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var itr; + var i; + + itr = arr.entries(); + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + itr.next(); + 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+':every: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..3db0b54b2855 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 @@ -1153,6 +1153,114 @@ function factory( dtype ) { // eslint-disable-line max-lines-per-function, stdli return out; }); + /** Returns an iterator for iterating over array key-value pairs. + * + * @name entries + * @memberof TypedArray.prototype + * @type {Function} + * @throws {TypeError} `this` must be a complex number array + * @returns {Iterator} iterator + * + * @example + * var factory = require( '@stdlib/array/fixed-endian-factory' ); + * var int32arr = factory( 'int32' ); + * + * var arr = int32arr( 'little-endian', [ 1, 2, 3 ] ); + + * // Create an iterator: + * var it = arr.entries(); + * + * // Iterate over the key-value pairs... + * var v = it.next().value; + * // returns [ 0, 1 ] + * + * v = it.next().value; + * // returns [ 1, 2 ] + * + * v = it.next().value; + * // returns [ 2, 3 ] + * + * var bool = it.next().done; + * // returns true + */ + setReadOnly( TypedArray.prototype, 'entries', function entries() { + var length; + var self; + var isLe; + var FLAG; + var itr; + var buf; + var i; + if ( !isTypedArray( this ) ) { + throw new TypeError( format( 'invalid invocation. `this` is not %s %s.', CHAR2ARTICLE[ dtype[0] ], CTOR_NAME ) ); + } + self = this; + buf = this._buffer; + length = this._length; + isLe = this._isLE; + i = -1; + + itr = {}; + setReadOnly( itr, 'next', next ); + setReadOnly( itr, 'return', end ); + + if ( ITERATOR_SYMBOL ) { + setReadOnly( itr, ITERATOR_SYMBOL, factory); + } + return itr; + + /** + * Returns an iterator protocol-compliant object containing the next iterated value. + * + * @private + * @returns {Object} iterator protocol-compliant object + */ + function next() { + var val; + i += 1; + if ( FLAG || i >= length ) { + return { + 'done': true + }; + } + val = buf[ GETTER ]( i*BYTES_PER_ELEMENT, isLe ); + return { + 'value': [ i, val ], + 'done': false + }; + } + + /** + * Finishes an iterator. + * + * @private + * @param {*} [value] - value to return + * @returns {Object} iterator protocol-compliant object + */ + function end( value ) { + FLAG = true; + if ( arguments.length ) { + return { + 'value': value, + 'done': true + }; + } + return { + 'done': true + }; + } + + /** + * Returns a new iterator. + * + * @private + * @returns {Iterator} iterator + */ + function factory() { + return self.entries(); + } + }); + return TypedArray; /** diff --git a/lib/node_modules/@stdlib/array/fixed-endian-factory/test/test.entries.js b/lib/node_modules/@stdlib/array/fixed-endian-factory/test/test.entries.js new file mode 100644 index 000000000000..046e38f79df0 --- /dev/null +++ b/lib/node_modules/@stdlib/array/fixed-endian-factory/test/test.entries.js @@ -0,0 +1,241 @@ +/** +* @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 proxyquire = require( 'proxyquire' ); +var hasOwnProp = require( '@stdlib/assert/has-own-property' ); +var ITERATOR_SYMBOL = require( '@stdlib/symbol/iterator' ); +var isFunction = require( '@stdlib/assert/is-function' ); +var isArray = require( '@stdlib/assert/is-array' ); +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( 'attached to the prototype of the returned function is an `entries` method', function test( t ) { + var ctor = factory( 'float64' ); + t.strictEqual( hasOwnProp( ctor.prototype, 'entries' ), true, 'returns expected value' ); + t.strictEqual( isFunction( ctor.prototype.at ), true, 'returns expected value' ); + 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 ctor; + var arr; + var i; + + ctor = factory( 'float64' ); + arr = new ctor( '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.entries.call( value, 0 ); + }; + } +}); + +tape( 'the method returns an iterator protocol-compliant object', function test( t ) { + var ctor; + var arr; + var buf; + var it; + var v; + var i; + + buf = [ 1.0, 2.0, 3.0, 4.0, 5.0]; + ctor = factory( 'float64' ); + arr = new ctor( 'little-endian', buf); + + it = arr.entries(); + t.strictEqual( it.next.length, 0, 'has zero arity' ); + + for ( i = 0; i < arr.length; i++ ) { + v = it.next(); + t.strictEqual( isArray( v.value ), true, 'returns an array' ); + t.strictEqual( v.value[ 0 ], i, 'returns an index' ); + t.strictEqual( v.value[ 1 ], buf[ i ], 'returns expected value' ); + t.strictEqual( typeof v.done, 'boolean', 'returns a boolean' ); + } + t.end(); +}); + +tape( 'the method returns an iterator which has a `return` method for closing an iterator (no argument)', function test( t ) { + var ctor; + var arr; + var buf; + var it; + var v; + + buf = [ 1.0, 2.0, 3.0, 4.0, 5.0]; + ctor = factory( 'float64' ); + arr = new ctor( 'little-endian', buf); + + it = arr.entries(); + + v = it.next(); + t.strictEqual( v.value[ 1 ], buf[ 0 ], 'returns expected value' ); + t.strictEqual( v.done, false, 'returns expected value' ); + + v = it.next(); + t.strictEqual( v.value[ 1 ], buf[ 1 ], 'returns expected value' ); + t.strictEqual( v.done, false, 'returns expected value' ); + + v = it.return(); + t.strictEqual( v.value, void 0, 'returns expected value' ); + t.strictEqual( v.done, true, 'returns expected value' ); + + v = it.next(); + t.strictEqual( v.value, void 0, 'returns expected value' ); + t.strictEqual( v.done, true, 'returns expected value' ); + + v = it.next(); + t.strictEqual( v.value, void 0, 'returns expected value' ); + t.strictEqual( v.done, true, 'returns expected value' ); + + v = it.next(); + t.strictEqual( v.value, void 0, 'returns expected value' ); + t.strictEqual( v.done, true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the method returns an iterator which has a `return` method for closing an iterator (argument)', function test( t ) { + var ctor; + var buf; + var arr; + var it; + var v; + + buf = [ 1.0, 2.0, 3.0, 4.0, 5.0]; + ctor = factory( 'float64' ); + arr = new ctor( 'little-endian', buf); + + it = arr.entries(); + + v = it.next(); + t.strictEqual( v.value[ 1 ], buf[ 0 ], 'returns expected value' ); + t.strictEqual( v.done, false, 'returns expected value' ); + + v = it.next(); + t.strictEqual( v.value[ 1 ], buf[ 1 ], 'returns expected value' ); + t.strictEqual( v.done, false, 'returns expected value' ); + + v = it.return( 'finished' ); + t.strictEqual( v.value, 'finished', 'returns expected value' ); + t.strictEqual( v.done, true, 'returns expected value' ); + + v = it.next(); + t.strictEqual( v.value, void 0, 'returns expected value' ); + t.strictEqual( v.done, true, 'returns expected value' ); + + v = it.next(); + t.strictEqual( v.value, void 0, 'returns expected value' ); + t.strictEqual( v.done, true, 'returns expected value' ); + + v = it.next(); + t.strictEqual( v.value, void 0, 'returns expected value' ); + t.strictEqual( v.done, true, 'returns expected value' ); + + t.end(); +}); + +tape( 'if an environment supports `Symbol.iterator`, the method returns an iterable', function test( t ) { + var factory; + var ctor; + var arr; + var buf; + var it1; + var it2; + var v1; + var v2; + var i; + + factory = proxyquire( './../lib/main.js', { + '@stdlib/symbol/iterator': '__ITERATOR_SYMBOL__' + }); + + buf = [ 1.0, 2.0, 3.0, 4.0, 5.0]; + ctor = factory( 'float64' ); + arr = new ctor( 'little-endian', buf); + + it1 = arr.entries(); + t.strictEqual( typeof it1[ '__ITERATOR_SYMBOL__' ], 'function', 'has method' ); + t.strictEqual( it1[ '__ITERATOR_SYMBOL__' ].length, 0, 'has zero arity' ); + + it2 = it1[ '__ITERATOR_SYMBOL__' ](); + t.strictEqual( typeof it2, 'object', 'returns an object' ); + t.strictEqual( typeof it2.next, 'function', 'has `next` method' ); + t.strictEqual( typeof it2.return, 'function', 'has `return` method' ); + + for ( i = 0; i < arr.length; i++ ) { + v1 = it1.next().value; + v2 = it2.next().value; + t.strictEqual( v1[ 0 ], v2[ 0 ], 'returns expected value' ); + t.strictEqual( v1[ 1 ], v2[ 1 ], 'returns expected value' ); + } + t.end(); +}); + +tape( 'if an environment does not support `Symbol.iterator`, the method does not return an "iterable"', function test( t ) { + var factory; + var ctor; + var arr; + var buf; + var it; + + factory = proxyquire( './../lib/main.js', { + '@stdlib/symbol/iterator': false + }); + + buf = [ 1.0, 2.0, 3.0, 4.0, 5.0 ]; + ctor = factory( 'float64' ); + arr = new ctor( 'little-endian', buf ); + + it = arr.entries(); + t.strictEqual( it[ ITERATOR_SYMBOL ], void 0, 'does not have property' ); + + t.end(); +});