Skip to content

Commit 14b3434

Browse files
authored
feat: add keys method to array/complex128
PR-URL: #2174 Reviewed-by: Athan Reines <[email protected]>
1 parent 67e80eb commit 14b3434

File tree

6 files changed

+574
-3
lines changed

6 files changed

+574
-3
lines changed

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

+32-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
@license Apache-2.0
44
5-
Copyright (c) 2018 The Stdlib Authors.
5+
Copyright (c) 2024 The Stdlib Authors.
66
77
Licensed under the Apache License, Version 2.0 (the "License");
88
you may not use this file except in compliance with the License.
@@ -1505,6 +1505,35 @@ var str = arr.join( '/' );
15051505
// returns '1 + 1i/2 - 2i/3 + 3i'
15061506
```
15071507

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

15101539
#### Complex128Array.prototype.lastIndexOf( searchElement\[, fromIndex] )
@@ -2350,6 +2379,8 @@ logEach( '%s', out );
23502379

23512380
<section class="links">
23522381

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

23552386
[@stdlib/array/buffer]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/buffer
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 Complex128Array = 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 Complex128Array( [ 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+
});
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 Complex128 = require( '@stdlib/complex/float64' );
26+
var isIteratorLike = require('@stdlib/assert/is-iterator-like');
27+
var pkg = require( './../package.json' ).name;
28+
var Complex128Array = 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 Complex128( i, i ) );
47+
}
48+
arr = new Complex128Array( 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/complex128/docs/types/index.d.ts

+25-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
/*
44
* @license Apache-2.0
55
*
6-
* Copyright (c) 2021 The Stdlib Authors.
6+
* Copyright (c) 2024 The Stdlib Authors.
77
*
88
* Licensed under the Apache License, Version 2.0 (the "License");
99
* you may not use this file except in compliance with the License.
@@ -800,6 +800,30 @@ declare class Complex128Array implements Complex128ArrayInterface {
800800
*/
801801
join( separator?: string ): string;
802802

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

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

+102-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
/**
44
* @license Apache-2.0
55
*
6-
* Copyright (c) 2018 The Stdlib Authors.
6+
* Copyright (c) 2024 The Stdlib Authors.
77
*
88
* Licensed under the Apache License, Version 2.0 (the "License");
99
* you may not use this file except in compliance with the License.
@@ -1555,6 +1555,107 @@ setReadOnly( Complex128Array.prototype, 'join', function join( separator ) {
15551555
return out.join( sep );
15561556
});
15571557

1558+
/**
1559+
* Returns an iterator for iterating over each index key in a typed array.
1560+
*
1561+
* @name keys
1562+
* @memberof Complex128Array.prototype
1563+
* @type {Function}
1564+
* @throws {TypeError} `this` must be a complex number array
1565+
* @returns {Iterator} iterator
1566+
*
1567+
* @example
1568+
* var arr = new Complex128Array( 2 );
1569+
*
1570+
* arr.set( [ 1.0, 1.0 ], 0 );
1571+
* arr.set( [ 2.0, 2.0 ], 1 );
1572+
*
1573+
* var iter = arr.keys();
1574+
*
1575+
* var v = iter.next().value;
1576+
* // returns 0
1577+
*
1578+
* v = iter.next().value;
1579+
* // returns 1
1580+
*
1581+
* var bool = iter.next().done;
1582+
* // returns true
1583+
*/
1584+
setReadOnly( Complex128Array.prototype, 'keys', function keys() {
1585+
var self;
1586+
var iter;
1587+
var len;
1588+
var FLG;
1589+
var i;
1590+
if ( !isComplexArray( this ) ) {
1591+
throw new TypeError( 'invalid invocation. `this` is not a complex number array.' );
1592+
}
1593+
self = this;
1594+
len = this._length;
1595+
1596+
// Initialize an iteration index:
1597+
i = -1;
1598+
1599+
// Create an iterator protocol-compliant object:
1600+
iter = {};
1601+
setReadOnly( iter, 'next', next );
1602+
setReadOnly( iter, 'return', end );
1603+
1604+
if ( ITERATOR_SYMBOL ) {
1605+
setReadOnly( iter, ITERATOR_SYMBOL, factory );
1606+
}
1607+
return iter;
1608+
1609+
/**
1610+
* Returns an iterator protocol-compliant object containing the next iterated value.
1611+
*
1612+
* @private
1613+
* @returns {Object} iterator protocol-compliant object
1614+
*/
1615+
function next() {
1616+
i += 1;
1617+
if ( FLG || i >= len ) {
1618+
return {
1619+
'done': true
1620+
};
1621+
}
1622+
return {
1623+
'value': i,
1624+
'done': false
1625+
};
1626+
}
1627+
1628+
/**
1629+
* Finishes an iterator.
1630+
*
1631+
* @private
1632+
* @param {*} [value] - value to return
1633+
* @returns {Object} iterator protocol-compliant object
1634+
*/
1635+
function end( value ) {
1636+
FLG = true;
1637+
if ( arguments.length ) {
1638+
return {
1639+
'value': value,
1640+
'done': true
1641+
};
1642+
}
1643+
return {
1644+
'done': true
1645+
};
1646+
}
1647+
1648+
/**
1649+
* Returns a new iterator.
1650+
*
1651+
* @private
1652+
* @returns {Iterator} iterator
1653+
*/
1654+
function factory() {
1655+
return self.keys();
1656+
}
1657+
});
1658+
15581659
/**
15591660
* Returns the last index at which a given element can be found.
15601661
*

0 commit comments

Comments
 (0)