Skip to content

Commit d9359b6

Browse files
Jaysukh-409kgryte
andauthored
feat: add keys method to array/complex64
PR-URL: #2160 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com>
1 parent 7b77a10 commit d9359b6

File tree

6 files changed

+569
-0
lines changed

6 files changed

+569
-0
lines changed

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1512,6 +1512,35 @@ var str = arr.join( '/' );
15121512
// returns '1 + 1i/2 - 2i/3 + 3i'
15131513
```
15141514

1515+
<a name="method-keys"></a>
1516+
1517+
#### Complex64Array.prototype.keys()
1518+
1519+
Returns an iterator for iterating over each index key in a typed array.
1520+
1521+
```javascript
1522+
var arr = new Complex64Array( 2 );
1523+
1524+
arr.set( [ 1.0, -1.0 ], 0 );
1525+
arr.set( [ 2.0, -2.0 ], 1 );
1526+
1527+
var iter = arr.keys();
1528+
1529+
var v = iter.next().value;
1530+
// returns 0
1531+
1532+
v = iter.next().value;
1533+
// returns 1
1534+
1535+
var bool = iter.next().done;
1536+
// returns true
1537+
```
1538+
1539+
The returned [iterator][mdn-iterator-protocol] protocol-compliant object has the following properties:
1540+
1541+
- **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.
1542+
- **return**: function which closes an [iterator][mdn-iterator-protocol] and returns a single (optional) argument in an [iterator][mdn-iterator-protocol] protocol-compliant object.
1543+
15151544
<a name="method-last-index-of"></a>
15161545

15171546
#### Complex64Array.prototype.lastIndexOf( searchElement\[, fromIndex] )
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+':keys: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.keys();
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.keys();
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+':keys:len='+len, f );
100+
}
101+
}
102+
103+
main();

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -802,6 +802,30 @@ declare class Complex64Array implements Complex64ArrayInterface {
802802
*/
803803
join( separator?: string ): string;
804804

805+
/**
806+
* Returns an iterator for iterating over each index key in a typed array.
807+
*
808+
* @returns iterator
809+
*
810+
* @example
811+
* var arr = new Complex64Array( 2 );
812+
*
813+
* arr.set( [ 1.0, 1.0 ], 0 );
814+
* arr.set( [ 2.0, 2.0 ], 1 );
815+
*
816+
* var iter = arr.keys();
817+
*
818+
* var v = iter.next().value;
819+
* // returns 0
820+
*
821+
* v = iter.next().value;
822+
* // returns 1
823+
*
824+
* var bool = iter.next().done;
825+
* // returns true
826+
*/
827+
keys(): TypedIterator<number>;
828+
805829
/**
806830
* Returns the last index at which a given element can be found.
807831
*

lib/node_modules/@stdlib/array/complex64/lib/main.js

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1535,6 +1535,107 @@ setReadOnly( Complex64Array.prototype, 'join', function join( separator ) {
15351535
return out.join( sep );
15361536
});
15371537

1538+
/**
1539+
* Returns an iterator for iterating over each index key in a typed array.
1540+
*
1541+
* @name keys
1542+
* @memberof Complex64Array.prototype
1543+
* @type {Function}
1544+
* @throws {TypeError} `this` must be a complex number array
1545+
* @returns {Iterator} iterator
1546+
*
1547+
* @example
1548+
* var arr = new Complex64Array( 2 );
1549+
*
1550+
* arr.set( [ 1.0, 1.0 ], 0 );
1551+
* arr.set( [ 2.0, 2.0 ], 1 );
1552+
*
1553+
* var iter = arr.keys();
1554+
*
1555+
* var v = iter.next().value;
1556+
* // returns 0
1557+
*
1558+
* v = iter.next().value;
1559+
* // returns 1
1560+
*
1561+
* var bool = iter.next().done;
1562+
* // returns true
1563+
*/
1564+
setReadOnly( Complex64Array.prototype, 'keys', function keys() {
1565+
var self;
1566+
var iter;
1567+
var len;
1568+
var FLG;
1569+
var i;
1570+
if ( !isComplexArray( this ) ) {
1571+
throw new TypeError( 'invalid invocation. `this` is not a complex number array.' );
1572+
}
1573+
self = this;
1574+
len = this._length;
1575+
1576+
// Initialize an iteration index:
1577+
i = -1;
1578+
1579+
// Create an iterator protocol-compliant object:
1580+
iter = {};
1581+
setReadOnly( iter, 'next', next );
1582+
setReadOnly( iter, 'return', end );
1583+
1584+
if ( ITERATOR_SYMBOL ) {
1585+
setReadOnly( iter, ITERATOR_SYMBOL, factory );
1586+
}
1587+
return iter;
1588+
1589+
/**
1590+
* Returns an iterator protocol-compliant object containing the next iterated value.
1591+
*
1592+
* @private
1593+
* @returns {Object} iterator protocol-compliant object
1594+
*/
1595+
function next() {
1596+
i += 1;
1597+
if ( FLG || i >= len ) {
1598+
return {
1599+
'done': true
1600+
};
1601+
}
1602+
return {
1603+
'value': i,
1604+
'done': false
1605+
};
1606+
}
1607+
1608+
/**
1609+
* Finishes an iterator.
1610+
*
1611+
* @private
1612+
* @param {*} [value] - value to return
1613+
* @returns {Object} iterator protocol-compliant object
1614+
*/
1615+
function end( value ) {
1616+
FLG = true;
1617+
if ( arguments.length ) {
1618+
return {
1619+
'value': value,
1620+
'done': true
1621+
};
1622+
}
1623+
return {
1624+
'done': true
1625+
};
1626+
}
1627+
1628+
/**
1629+
* Returns a new iterator.
1630+
*
1631+
* @private
1632+
* @returns {Iterator} iterator
1633+
*/
1634+
function factory() {
1635+
return self.keys();
1636+
}
1637+
});
1638+
15381639
/**
15391640
* Returns the last index at which a given element can be found.
15401641
*

0 commit comments

Comments
 (0)