Skip to content

Commit 7cc8638

Browse files
authored
feat(typed-array): add values() method to iterate over array values
The `values()` method was added to the typed array prototype to allow iteration over the array's values. This method returns an iterator, which can be used in a `for...of` loop or with `next()` calls to access each value in the array sequentially. This change simplifies the process of iterating over typed arrays, ensuring that code interacting with them is more readable and easier to maintain. Fixes: #123
1 parent 9818fa6 commit 7cc8638

File tree

5 files changed

+468
-0
lines changed

5 files changed

+468
-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
@@ -451,6 +451,47 @@ var count = context.count;
451451
// returns 3
452452
```
453453

454+
<a name="values"></a>
455+
456+
#### TypedArrayFE.prototype.values()
457+
458+
Returns an iterator for the values of the typed array.
459+
460+
```javascript
461+
var Float64ArrayFE = fixedEndianFactory( 'float64' );
462+
463+
var arr = new Float64ArrayFE( 'little-endian', [ 1.0, 2.0, 3.0 ] );
464+
// returns <Float64ArrayFE>
465+
466+
var iter = arr.values();
467+
468+
var result = iter.next();
469+
// returns { 'value': 1.0, 'done': false }
470+
471+
result = iter.next();
472+
// returns { 'value': 2.0, 'done': false }
473+
474+
result = iter.next();
475+
// returns { 'value': 3.0, 'done': false }
476+
477+
result = iter.next();
478+
// returns { 'done': true }
479+
```
480+
481+
use the iterator with a `for...of` loop.
482+
483+
```javascript
484+
var value;
485+
for ( value of arr.values() ) {
486+
console.log( value );
487+
}
488+
// print each value of array
489+
// 1.0
490+
// 2.0
491+
// 3.0
492+
```
493+
494+
454495
<a name="method-get"></a>
455496

456497
#### TypedArrayFE.prototype.get( i )
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 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+
49+
for ( value of iter ) {
50+
if ( isnan( value ) ) {
51+
b.fail( 'should not return NaN' );
52+
}
53+
}
54+
}
55+
b.toc();
56+
b.pass( 'benchmark finished' );
57+
b.end();
58+
});
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) 2024 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/array/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: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -850,6 +850,51 @@ function factory( dtype ) { // eslint-disable-line max-lines-per-function, stdli
850850
return false;
851851
});
852852

853+
/**
854+
* Returns an iterator for array elements.
855+
*
856+
* @private
857+
* @name values
858+
* @memberof TypedArray.prototype
859+
* @type {Function}
860+
* @throws {TypeError} `this` must be a typed array instance
861+
* @returns {Object} iterator for array elements
862+
*/
863+
setReadOnly(TypedArray.prototype, 'values', function values() {
864+
var iterator;
865+
var index = 0;
866+
var value;
867+
var isLE = this._isLE;
868+
var buf = this._buffer;
869+
var len = this._length;
870+
871+
if (!isTypedArray(this)) {
872+
throw new TypeError(format('invalid invocation. `this` is not %s %s.', CHAR2ARTICLE[dtype[0]], CTOR_NAME));
873+
}
874+
875+
iterator = {
876+
'next': function next() {
877+
if (index < len) {
878+
value = buf[GETTER](index * BYTES_PER_ELEMENT, isLE);
879+
index += 1;
880+
return {
881+
'value': value,
882+
'done': false
883+
};
884+
}
885+
return {
886+
'done': true
887+
};
888+
}
889+
};
890+
891+
iterator[Symbol.iterator] = function iterator() {
892+
return this;
893+
};
894+
895+
return iterator;
896+
});
897+
853898
/**
854899
* Serializes an array as a string.
855900
*

0 commit comments

Comments
 (0)