Skip to content

Commit

Permalink
Auto-generated commit
Browse files Browse the repository at this point in the history
  • Loading branch information
stdlib-bot committed May 1, 2024
1 parent 9913cb6 commit 09595f9
Show file tree
Hide file tree
Showing 7 changed files with 625 additions and 0 deletions.
1 change: 1 addition & 0 deletions CONTRIBUTORS
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ Shivam <[email protected]>
Shraddheya Shendre <[email protected]>
Shubh Mehta <[email protected]>
Shubham Mishra <[email protected]>
Sivam Das <[email protected]>
Snehil Shah <[email protected]>
Soumajit Chatterjee <[email protected]>
Spandan Barve <[email protected]>
Expand Down
43 changes: 43 additions & 0 deletions complex128/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2249,6 +2249,49 @@ var str = arr.toString();
// returns '1 + 1i,2 - 2i,3 + 3i'
```

<a name="method-values"></a>

#### Complex128Array.prototype.values()

Returns an iterator for iterating over each value in a typed array.

```javascript
var real = require( '@stdlib/complex/real' );
var imag = require( '@stdlib/complex/imag' );
var arr = new Complex128Array( 2 );

arr.set( [ 1.0, -1.0 ], 0 );
arr.set( [ 2.0, -2.0 ], 1 );

var iter = arr.values();

var v = iter.next().value;
// returns <Complex128>

var re = real( v );
// returns 1.0

var im = imag( v );
// returns -1.0

v = iter.next().value;
// returns <Complex128>

re = real( v );
// returns 2.0

im = imag( v );
// returns -2.0

var bool = iter.next().done;
// returns true
```

The returned [iterator][mdn-iterator-protocol] protocol-compliant object has the following properties:

- **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.
- **return**: function which closes an [iterator][mdn-iterator-protocol] and returns a single (optional) argument in an [iterator][mdn-iterator-protocol] protocol-compliant object.

<a name="method-with"></a>

#### Complex128Array.prototype.with( index, value )
Expand Down
51 changes: 51 additions & 0 deletions complex128/benchmark/benchmark.values.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2024 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

// MODULES //

var bench = require( '@stdlib/bench' );
var isIteratorLike = require( '@stdlib/assert/is-iterator-like' );
var pkg = require( './../package.json' ).name;
var Complex128Array = require( './../lib' );


// MAIN //

bench( pkg+':values:len=2', function benchmark( b ) {
var iter;
var arr;
var i;

arr = new Complex128Array( [ 1.0, -1.0, 2.0, -2.0 ] );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
iter = arr.values();
if ( typeof iter !== 'object' ) {
b.fail( 'should return an object' );
}
}
b.toc();
if ( !isIteratorLike( iter ) ) {
b.fail( 'should return an iterator protocol-compliant object' );
}
b.pass( 'benchmark finished' );
b.end();
});
103 changes: 103 additions & 0 deletions complex128/benchmark/benchmark.values.length.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2024 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

// MODULES //

var bench = require( '@stdlib/bench' );
var pow = require( '@stdlib/math/base/special/pow' );
var Complex128 = require( '@stdlib/complex/float64' );
var isIteratorLike = require('@stdlib/assert/is-iterator-like');
var pkg = require( './../package.json' ).name;
var Complex128Array = require( './../lib' );


// FUNCTIONS //

/**
* Creates a benchmark function.
*
* @private
* @param {PositiveInteger} len - array length
* @returns {Function} benchmark function
*/
function createBenchmark( len ) {
var arr;
var i;

arr = [];
for ( i = 0; i < len; i++ ) {
arr.push( new Complex128( i, i ) );
}
arr = new Complex128Array( arr );

return benchmark;

/**
* Benchmark function.
*
* @private
* @param {Benchmark} b - benchmark instance
*/
function benchmark( b ) {
var iter;
var i;

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
iter = arr.values();
if ( typeof iter !== 'object' ) {
b.fail( 'should return an object' );
}
}
b.toc();
if ( !isIteratorLike( iter ) ) {
b.fail( 'should return an iterator protocol-compliant object' );
}
b.pass( 'benchmark finished' );
b.end();
}
}


// MAIN //

/**
* Main execution sequence.
*
* @private
*/
function main() {
var len;
var min;
var max;
var f;
var i;

min = 1; // 10^min
max = 6; // 10^max

for ( i = min; i <= max; i++ ) {
len = pow( 10, i );
f = createBenchmark( len );
bench( pkg+':values:len='+len, f );
}
}

main();
38 changes: 38 additions & 0 deletions complex128/docs/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1282,6 +1282,44 @@ declare class Complex128Array implements Complex128ArrayInterface {
*/
toString(): string;

/**
* Returns an iterator for iterating over each value in a typed array.
*
* @returns iterator
*
* @example
* var real = require( '@stdlib/complex/real' );
* var imag = require( '@stdlib/complex/imag' );
* var arr = new Complex128Array( 2 );
*
* arr.set( [ 1.0, -1.0 ], 0 );
* arr.set( [ 2.0, -2.0 ], 1 );
*
* var iter = arr.values();
*
* var v = iter.next().value;
* // returns <Complex128>
*
* var re = real( v );
* // returns 1.0
*
* var im = imag( v );
* // returns -1.0
*
* v = iter.next().value;
* // returns <Complex128>
*
* re = real( v );
* // returns 2.0
*
* im = imag( v );
* // returns -2.0
*
* var bool = iter.next().done;
* // returns true
*/
values(): TypedIterator<Complex128>;

/**
* Returns a new typed array with the element at a provided index replaced with a provided value.
*
Expand Down
117 changes: 117 additions & 0 deletions complex128/lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -2597,6 +2597,123 @@ setReadOnly( Complex128Array.prototype, 'toString', function toString() {
return out.join( ',' );
});

/**
* Returns an iterator for iterating over each value in a typed array.
*
* @name values
* @memberof Complex128Array.prototype
* @type {Function}
* @throws {TypeError} `this` must be a complex number array
* @returns {Iterator} iterator
*
* @example
* var real = require( '@stdlib/complex/real' );
* var imag = require( '@stdlib/complex/imag' );
* var arr = new Complex128Array( 2 );
*
* arr.set( [ 1.0, -1.0 ], 0 );
* arr.set( [ 2.0, -2.0 ], 1 );
*
* var iter = arr.values();
*
* var v = iter.next().value;
* // returns <Complex128>
*
* var re = real( v );
* // returns 1.0
*
* var im = imag( v );
* // returns -1.0
*
* v = iter.next().value;
* // returns <Complex128>
*
* re = real( v );
* // returns 2.0
*
* im = imag( v );
* // returns -2.0
*
* var bool = iter.next().done;
* // returns true
*/
setReadOnly( Complex128Array.prototype, 'values', function values() {
var iter;
var self;
var len;
var FLG;
var buf;
var i;
if ( !isComplexArray( this ) ) {
throw new TypeError( 'invalid invocation. `this` is not a complex number array.' );
}
self = this;
buf = this._buffer;
len = this._length;

// Initialize an iteration index:
i = -1;

// Create an iterator protocol-compliant object:
iter = {};
setReadOnly( iter, 'next', next );
setReadOnly( iter, 'return', end );

if ( ITERATOR_SYMBOL ) {
setReadOnly( iter, ITERATOR_SYMBOL, factory );
}
return iter;

/**
* Returns an iterator protocol-compliant object containing the next iterated value.
*
* @private
* @returns {Object} iterator protocol-compliant object
*/
function next() {
i += 1;
if ( FLG || i >= len ) {
return {
'done': true
};
}
return {
'value': getComplex128( buf, i ),
'done': false
};
}

/**
* Finishes an iterator.
*
* @private
* @param {*} [value] - value to return
* @returns {Object} iterator protocol-compliant object
*/
function end( value ) {
FLG = true;
if ( arguments.length ) {
return {
'value': value,
'done': true
};
}
return {
'done': true
};
}

/**
* Returns a new iterator.
*
* @private
* @returns {Iterator} iterator
*/
function factory() {
return self.values();
}
});

/**
* Returns a new typed array with the element at a provided index replaced with a provided value.
*
Expand Down
Loading

0 comments on commit 09595f9

Please sign in to comment.