Skip to content

Commit 832504a

Browse files
Jaysukh-409kgryte
andauthored
feat: add values method to array/complex64
PR-URL: #1706 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com> Signed-off-by: Athan Reines <kgryte@gmail.com>
1 parent dc2cd24 commit 832504a

File tree

6 files changed

+638
-12
lines changed

6 files changed

+638
-12
lines changed

lib/node_modules/@stdlib/array/complex64/README.md

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -798,6 +798,11 @@ var bool = it.next().done;
798798
// returns true
799799
```
800800

801+
The returned [iterator][mdn-iterator-protocol] protocol-compliant object has the following properties:
802+
803+
- **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.
804+
- **return**: function which closes an [iterator][mdn-iterator-protocol] and returns a single (optional) argument in an [iterator][mdn-iterator-protocol] protocol-compliant object.
805+
801806
<a name="method-every"></a>
802807

803808
#### Complex64Array.prototype.every( predicate\[, thisArg] )
@@ -2222,9 +2227,52 @@ var str = arr.toString();
22222227
// returns '1 + 1i,2 - 2i,3 + 3i'
22232228
```
22242229

2230+
<a name="method-values"></a>
2231+
2232+
#### Complex64Array.prototype.values()
2233+
2234+
Returns an iterator for iterating over each value in a typed array.
2235+
2236+
```javascript
2237+
var realf = require( '@stdlib/complex/realf' );
2238+
var imagf = require( '@stdlib/complex/imagf' );
2239+
var arr = new Complex64Array( 2 );
2240+
2241+
arr.set( [ 1.0, -1.0 ], 0 );
2242+
arr.set( [ 2.0, -2.0 ], 1 );
2243+
2244+
var iter = arr.values();
2245+
2246+
var v = iter.next().value;
2247+
// returns <Complex64>
2248+
2249+
var re = realf( v );
2250+
// returns 1.0
2251+
2252+
var im = imagf( v );
2253+
// returns -1.0
2254+
2255+
v = iter.next().value;
2256+
// returns <Complex64>
2257+
2258+
re = realf( v );
2259+
// returns 2.0
2260+
2261+
im = imagf( v );
2262+
// returns -2.0
2263+
2264+
var bool = iter.next().done;
2265+
// returns true
2266+
```
2267+
2268+
The returned [iterator][mdn-iterator-protocol] protocol-compliant object has the following properties:
2269+
2270+
- **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.
2271+
- **return**: function which closes an [iterator][mdn-iterator-protocol] and returns a single (optional) argument in an [iterator][mdn-iterator-protocol] protocol-compliant object.
2272+
22252273
<a name="method-with"></a>
22262274

2227-
#### Complex128Array.prototype.with( index, value )
2275+
#### Complex64Array.prototype.with( index, value )
22282276

22292277
Returns a new typed array with the element at a provided index replaced with a provided value.
22302278

@@ -2352,6 +2400,8 @@ logEach( '%s', out );
23522400

23532401
<section class="links">
23542402

2403+
[mdn-iterator-protocol]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterator_protocol
2404+
23552405
[@stdlib/array/typed]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/typed
23562406

23572407
[@stdlib/array/buffer]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/buffer
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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 isIteratorLike = require( '@stdlib/assert/is-iterator-like' );
25+
var pkg = require( './../package.json' ).name;
26+
var Complex64Array = require( './../lib' );
27+
28+
29+
// MAIN //
30+
31+
bench( pkg+':values:len=2', function benchmark( b ) {
32+
var iter;
33+
var arr;
34+
var i;
35+
36+
arr = new Complex64Array( [ 1.0, -1.0, 2.0, -2.0 ] );
37+
38+
b.tic();
39+
for ( i = 0; i < b.iterations; i++ ) {
40+
iter = arr.values();
41+
if ( typeof iter !== 'object' ) {
42+
b.fail( 'should return an object' );
43+
}
44+
}
45+
b.toc();
46+
if ( !isIteratorLike( iter ) ) {
47+
b.fail( 'should return an iterator protocol-compliant object' );
48+
}
49+
b.pass( 'benchmark finished' );
50+
b.end();
51+
});
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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 Complex64 = require( '@stdlib/complex/float32' );
26+
var isIteratorLike = require('@stdlib/assert/is-iterator-like');
27+
var pkg = require( './../package.json' ).name;
28+
var Complex64Array = require( './../lib' );
29+
30+
31+
// FUNCTIONS //
32+
33+
/**
34+
* Creates a benchmark function.
35+
*
36+
* @private
37+
* @param {PositiveInteger} len - array length
38+
* @returns {Function} benchmark function
39+
*/
40+
function createBenchmark( len ) {
41+
var arr;
42+
var i;
43+
44+
arr = [];
45+
for ( i = 0; i < len; i++ ) {
46+
arr.push( new Complex64( i, i ) );
47+
}
48+
arr = new Complex64Array( arr );
49+
50+
return benchmark;
51+
52+
/**
53+
* Benchmark function.
54+
*
55+
* @private
56+
* @param {Benchmark} b - benchmark instance
57+
*/
58+
function benchmark( b ) {
59+
var iter;
60+
var i;
61+
62+
b.tic();
63+
for ( i = 0; i < b.iterations; i++ ) {
64+
iter = arr.values();
65+
if ( typeof iter !== 'object' ) {
66+
b.fail( 'should return an object' );
67+
}
68+
}
69+
b.toc();
70+
if ( !isIteratorLike( iter ) ) {
71+
b.fail( 'should return an iterator protocol-compliant object' );
72+
}
73+
b.pass( 'benchmark finished' );
74+
b.end();
75+
}
76+
}
77+
78+
79+
// MAIN //
80+
81+
/**
82+
* Main execution sequence.
83+
*
84+
* @private
85+
*/
86+
function main() {
87+
var len;
88+
var min;
89+
var max;
90+
var f;
91+
var i;
92+
93+
min = 1; // 10^min
94+
max = 6; // 10^max
95+
96+
for ( i = min; i <= max; i++ ) {
97+
len = pow( 10, i );
98+
f = createBenchmark( len );
99+
bench( pkg+':values:len='+len, f );
100+
}
101+
}
102+
103+
main();

lib/node_modules/@stdlib/array/complex64/docs/types/index.d.ts

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
/// <reference types="@stdlib/types"/>
2424

25-
import { Iterator as Iter, IterableIterator } from '@stdlib/types/iter';
25+
import { Iterator as Iter, IterableIterator, TypedIterator } from '@stdlib/types/iter';
2626
import { ArrayLike, RealOrComplexTypedArray, Complex64Array as Complex64ArrayInterface } from '@stdlib/types/array';
2727
import { ComplexLike, Complex64 } from '@stdlib/types/complex';
2828
import ArrayBuffer = require( '@stdlib/array/buffer' );
@@ -459,7 +459,7 @@ declare class Complex64Array implements Complex64ArrayInterface {
459459
* var bool = it.next().done;
460460
* // returns true
461461
*/
462-
entries(): Iterator;
462+
entries(): TypedIterator<[number, Complex64]>;
463463

464464
/**
465465
* Tests whether all elements in an array pass a test implemented by a predicate function.
@@ -1260,6 +1260,44 @@ declare class Complex64Array implements Complex64ArrayInterface {
12601260
*/
12611261
toString(): string;
12621262

1263+
/**
1264+
* Returns an iterator for iterating over each value in a typed array.
1265+
*
1266+
* @returns iterator
1267+
*
1268+
* @example
1269+
* var realf = require( '@stdlib/complex/realf' );
1270+
* var imagf = require( '@stdlib/complex/imagf' );
1271+
* var arr = new Complex64Array( 2 );
1272+
*
1273+
* arr.set( [ 1.0, -1.0 ], 0 );
1274+
* arr.set( [ 2.0, -2.0 ], 1 );
1275+
*
1276+
* var iter = arr.values();
1277+
*
1278+
* var v = iter.next().value;
1279+
* // returns <Complex64>
1280+
*
1281+
* var re = realf( v );
1282+
* // returns 1.0
1283+
*
1284+
* var im = imagf( v );
1285+
* // returns -1.0
1286+
*
1287+
* v = iter.next().value;
1288+
* // returns <Complex64>
1289+
*
1290+
* re = realf( v );
1291+
* // returns 2.0
1292+
*
1293+
* im = imagf( v );
1294+
* // returns -2.0
1295+
*
1296+
* var bool = iter.next().done;
1297+
* // returns true
1298+
*/
1299+
values(): TypedIterator<Complex64>;
1300+
12631301
/**
12641302
* Returns a new typed array with the element at a provided index replaced with a provided value.
12651303
*

0 commit comments

Comments
 (0)