|
3 | 3 | /**
|
4 | 4 | * @license Apache-2.0
|
5 | 5 | *
|
6 |
| -* Copyright (c) 2018 The Stdlib Authors. |
| 6 | +* Copyright (c) 2024 The Stdlib Authors. |
7 | 7 | *
|
8 | 8 | * Licensed under the Apache License, Version 2.0 (the "License");
|
9 | 9 | * you may not use this file except in compliance with the License.
|
@@ -1555,6 +1555,107 @@ setReadOnly( Complex128Array.prototype, 'join', function join( separator ) {
|
1555 | 1555 | return out.join( sep );
|
1556 | 1556 | });
|
1557 | 1557 |
|
| 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 | + |
1558 | 1659 | /**
|
1559 | 1660 | * Returns the last index at which a given element can be found.
|
1560 | 1661 | *
|
|
0 commit comments