Skip to content
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

feat: add entries method to array/fixed-endian-factory #3280

Open
wants to merge 8 commits into
base: develop
Choose a base branch
from
57 changes: 57 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 @@ -958,6 +958,60 @@ var v = out.get( 0 );

* * *

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

#### 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:
Expand Down Expand Up @@ -1035,6 +1089,8 @@ logEach( '%s', out );

<section class="links">

[mdn-iterator-protocol]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterator_protocol

[@stdlib/array/typed]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/typed

[@stdlib/array/buffer]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/buffer
Expand All @@ -1046,3 +1102,4 @@ logEach( '%s', out );
</section>

<!-- /.links -->

Original file line number Diff line number Diff line change
@@ -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();
});
Original file line number Diff line number Diff line change
@@ -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();
108 changes: 108 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 @@ -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;

/**
Expand Down
Loading
Loading