Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
41 changes: 41 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 @@ -451,6 +451,47 @@ var count = context.count;
// returns 3
```

<a name="values"></a>

#### 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 <Float64ArrayFE>

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
```


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

#### TypedArrayFE.prototype.get( i )
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/**
* @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 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();

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

for ( value of iter ) {
if ( isnan( value ) ) {
b.fail( 'should not return NaN' );
}
}
}
b.toc();
b.pass( 'benchmark finished' );
b.end();
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/**
* @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 zeroTo = require( '@stdlib/array/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();
45 changes: 45 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 @@ -850,6 +850,51 @@ 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() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
setReadOnly(TypedArray.prototype, 'values', function values() {
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)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (!isTypedArray(this)) {
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;
});

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add the spaces under the every parans.. same way i have suggested

/**
* Serializes an array as a string.
*
Expand Down
Loading