Skip to content

Commit 7b99916

Browse files
committed
add values() method to iterate over array values
Signed-off-by: hemantmm <[email protected]>
1 parent 3ef117f commit 7b99916

File tree

5 files changed

+471
-0
lines changed

5 files changed

+471
-0
lines changed

lib/node_modules/@stdlib/array/fixed-endian-factory/README.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,47 @@ var count = context.count;
511511
// returns 3
512512
```
513513

514+
<a name="values"></a>
515+
516+
### TypedArrayFE.prototype.values()
517+
518+
Returns an iterator for the values of the typed array.
519+
520+
```javascript
521+
var Float64ArrayFE = fixedEndianFactory( 'float64' );
522+
523+
var arr = new Float64ArrayFE( 'little-endian', [ 1.0, 2.0, 3.0 ] );
524+
// returns <Float64ArrayFE>
525+
526+
var iter = arr.values();
527+
528+
var result = iter.next();
529+
// returns { 'value': 1.0, 'done': false }
530+
531+
result = iter.next();
532+
// returns { 'value': 2.0, 'done': false }
533+
534+
result = iter.next();
535+
// returns { 'value': 3.0, 'done': false }
536+
537+
result = iter.next();
538+
// returns { 'done': true }
539+
```
540+
541+
use the iterator with a `for...of` loop.
542+
543+
```javascript
544+
var value;
545+
for( value of arr.values() ) {
546+
console.log( value );
547+
}
548+
// print each value of array
549+
// 1.0
550+
// 2.0
551+
// 3.0
552+
```
553+
554+
514555
<a name="method-get"></a>
515556

516557
#### TypedArrayFE.prototype.get( i )
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
25+
var pkg = require( './../package.json' ).name;
26+
var factory = require( './../lib' );
27+
28+
29+
// VARIABLES //
30+
31+
var Float64ArrayFE = factory( 'float64' );
32+
33+
34+
// MAIN //
35+
36+
bench( pkg+':values', function benchmark( b ) {
37+
var value;
38+
var iter;
39+
var arr;
40+
var i;
41+
42+
arr = new Float64ArrayFE( 'little-endian', [ 1.0, 2.0, 2.0, 1.0 ] );
43+
iter = arr.values();
44+
45+
b.tic();
46+
for ( i = 0; i < b.iterations; i++ ) {
47+
iter = arr.values();
48+
for ( value of iter ) {
49+
if ( isnan( value ) ) {
50+
b.fail( 'should not return NaN' );
51+
}
52+
}
53+
}
54+
b.toc();
55+
b.pass( 'benchmark finished' );
56+
b.end();
57+
});
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var pow = require( '@stdlib/math/base/special/pow' );
25+
var zeroTo = require( '@stdlib/utils/zero-to' );
26+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
27+
var pkg = require( './../package.json' ).name;
28+
var factory = require( './../lib' );
29+
30+
31+
// VARIABLES //
32+
33+
var Float64ArrayFE = factory( 'float64' );
34+
35+
36+
// FUNCTIONS //
37+
38+
/**
39+
* Creates a benchmark function for `values` iterator.
40+
*
41+
* @private
42+
* @param {PositiveInteger} len - array length
43+
* @returns {Function} benchmark function
44+
*/
45+
function createBenchmark( len ) {
46+
var arr = new Float64ArrayFE( 'little-endian', zeroTo( len ) );
47+
return benchmark;
48+
49+
/**
50+
* Benchmark function.
51+
*
52+
* @private
53+
* @param {Benchmark} b - benchmark instance
54+
* @throws {Error} throws an error if NaN value is encountered
55+
*/
56+
function benchmark( b ) {
57+
var value;
58+
var iter;
59+
var i;
60+
61+
b.tic();
62+
for ( i = 0; i < b.iterations; i++ ) {
63+
iter = arr.values(); // Get the iterator for values
64+
65+
// Use for...of loop to iterate over the values
66+
for ( value of iter ) {
67+
if ( isnan( value ) ) {
68+
throw new Error( 'something went wrong' );
69+
}
70+
}
71+
if ( arr.length !== len ) {
72+
b.fail( 'should not change an array length' );
73+
}
74+
}
75+
b.toc();
76+
if ( arr.length !== len ) {
77+
b.fail( 'should not change an array length' );
78+
}
79+
b.pass( 'benchmark finished' );
80+
b.end();
81+
}
82+
}
83+
84+
85+
// MAIN //
86+
87+
/**
88+
* Main execution sequence.
89+
*
90+
* @private
91+
*/
92+
function main() {
93+
var len;
94+
var min;
95+
var max;
96+
var f;
97+
var i;
98+
99+
min = 1; // 10^min
100+
max = 6; // 10^max
101+
102+
for( i = min; i <= max; i++ ) {
103+
len = pow( 10, i );
104+
f = createBenchmark( len );
105+
bench( pkg+':values:len='+len, f );
106+
}
107+
}
108+
109+
main();

lib/node_modules/@stdlib/array/fixed-endian-factory/lib/main.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1095,6 +1095,47 @@ function factory( dtype ) { // eslint-disable-line max-lines-per-function, stdli
10951095
return false;
10961096
});
10971097

1098+
/**
1099+
* Returns an iterator for array elements.
1100+
*
1101+
* @private
1102+
* @name values
1103+
* @memberof TypedArray.prototype
1104+
* @type {Function}
1105+
* @throws {TypeError} `this` must be a typed array instance
1106+
* @returns {Object} iterator for array elements
1107+
*/
1108+
setReadOnly( TypedArray.prototype, 'values', function values() {
1109+
var iterator;
1110+
var index = 0;
1111+
var value;
1112+
var isLE = this._isLE;
1113+
var buf = this._buffer;
1114+
var len = this._length;
1115+
if ( !isTypedArray( this ) ) {
1116+
throw new TypeError( format( 'invalid invocation. `this` is not %s %s.', CHAR2ARTICLE[ dtype[0] ], CTOR_NAME ) );
1117+
}
1118+
iterator = {
1119+
'next': function next() {
1120+
if( index < len ) {
1121+
value = buf[ GETTER ]( index * BYTES_PER_ELEMENT, isLE );
1122+
index += 1;
1123+
return {
1124+
'value': value,
1125+
'done': false
1126+
};
1127+
}
1128+
return {
1129+
'done': true
1130+
};
1131+
}
1132+
};
1133+
iterator[ Symbol.iterator ] = function iterator() {
1134+
return this;
1135+
};
1136+
return iterator;
1137+
});
1138+
10981139
/**
10991140
* Serializes an array as a string.
11001141
*

0 commit comments

Comments
 (0)