Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add values method to array/complex64 #1706

Merged
merged 15 commits into from
Apr 14, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions lib/node_modules/@stdlib/array/complex64/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2222,6 +2222,48 @@ 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 realf = require( '@stdlib/complex/realf' );
var imagf = require( '@stdlib/complex/imagf' );
var arr = new Complex64Array( 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 <Complex64>

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

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

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

re = realf( v );
// returns 2.0

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

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

The returned iterator protocol-compliant object has the following property:

- **next**: function which returns an iterator 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 is finished.

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

#### Complex128Array.prototype.with( index, value )
Expand Down
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 Complex64Array = require( './../lib' );


// MAIN //

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

arr = new Complex64Array( [ 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();
});
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 Complex64 = require( '@stdlib/complex/float32' );
var isIteratorLike = require('@stdlib/assert/is-iterator-like');
var pkg = require( './../package.json' ).name;
var Complex64Array = 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 Complex64( i, i ) );
}
arr = new Complex64Array( 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();
40 changes: 39 additions & 1 deletion lib/node_modules/@stdlib/array/complex64/docs/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

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

import { Iterator as Iter, IterableIterator } from '@stdlib/types/iter';
import { Iterator as Iter, IterableIterator, TypedIterator } from '@stdlib/types/iter';
import { ArrayLike, RealOrComplexTypedArray, Complex64Array as Complex64ArrayInterface } from '@stdlib/types/array';
import { ComplexLike, Complex64 } from '@stdlib/types/complex';
import ArrayBuffer = require( '@stdlib/array/buffer' );
Expand Down Expand Up @@ -1260,6 +1260,44 @@ declare class Complex64Array implements Complex64ArrayInterface {
*/
toString(): string;

/**
* Returns an iterator for iterating over each value in a typed array.
*
* @returns iterator
*
* @example
* var realf = require( '@stdlib/complex/realf' );
* var imagf = require( '@stdlib/complex/imagf' );
* var arr = new Complex64Array( 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 <Complex64>
*
* var re = realf( v );
* // returns 1.0
*
* var im = imagf( v );
* // returns -1.0
*
* v = iter.next().value;
* // returns <Complex64>
*
* re = realf( v );
* // returns 2.0
*
* im = imagf( v );
* // returns -2.0
*
* var bool = iter.next().done;
* // returns true
*/
values(): TypedIterator<Complex64>;

/**
* Returns a new typed array with the element at a provided index replaced with a provided value.
*
Expand Down
96 changes: 96 additions & 0 deletions lib/node_modules/@stdlib/array/complex64/lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@
}
buf = buf[ ITERATOR_SYMBOL ]();
if ( !isFunction( buf.next ) ) {
throw new TypeError( format( 'invalid argument. Must provide a length, ArrayBuffer, typed array, array-like object, or an iterable. Value: `%s`.', buf ) ); // FIXME: `buf` is what is returned from above, NOT the original value

Check warning on line 256 in lib/node_modules/@stdlib/array/complex64/lib/main.js

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Unexpected 'fixme' comment: 'FIXME: `buf` is what is returned from...'
}
buf = fromIterator( buf );
if ( buf instanceof Error ) {
Expand Down Expand Up @@ -718,7 +718,7 @@
if ( !isComplexArray( this ) ) {
throw new TypeError( 'invalid invocation. `this` is not a complex number array.' );
}
// FIXME: prefer a functional `copyWithin` implementation which addresses lack of universal browser support (e.g., IE11 and Safari) or ensure that typed arrays are polyfilled

Check warning on line 721 in lib/node_modules/@stdlib/array/complex64/lib/main.js

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Unexpected 'fixme' comment: 'FIXME: prefer a functional `copyWithin`...'
if ( arguments.length === 2 ) {
this._buffer.copyWithin( target*2, start*2 );
} else {
Expand Down Expand Up @@ -2061,7 +2061,7 @@
// We need to copy source values...
tmp = new Float32Array( N );
for ( i = 0; i < N; i++ ) {
tmp[ i ] = sbuf[ i ]; // TODO: handle accessor arrays

Check warning on line 2064 in lib/node_modules/@stdlib/array/complex64/lib/main.js

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Unexpected 'todo' comment: 'TODO: handle accessor arrays'
}
sbuf = tmp;
}
Expand Down Expand Up @@ -2499,6 +2499,102 @@
return out.join( ',' );
});

/**
* Returns an iterator for iterating over each value in a typed array.
*
* @name values
* @memberof Complex64Array.prototype
* @type {Function}
* @throws {TypeError} `this` must be a complex number array
* @returns {Iterator} iterator
*
* @example
* var realf = require( '@stdlib/complex/realf' );
* var imagf = require( '@stdlib/complex/imagf' );
* var arr = new Complex64Array( 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 <Complex64>
*
* var re = realf( v );
* // returns 1.0
*
* var im = imagf( v );
* // returns -1.0
*
* v = iter.next().value;
* // returns <Complex64>
*
* re = realf( v );
* // returns 2.0
*
* im = imagf( v );
* // returns -2.0
*
* var bool = iter.next().done;
* // returns true
*/
setReadOnly( Complex64Array.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.' );
}

buf = this._buffer;
len = this._length;
FLG = true;
self = this;
i = -1;
iter = {};
setReadOnly( iter, 'next', next );
kgryte marked this conversation as resolved.
Show resolved Hide resolved
kgryte marked this conversation as resolved.
Show resolved Hide resolved

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 ) {
kgryte marked this conversation as resolved.
Show resolved Hide resolved
return {
'value': getComplex64( buf, i ),
'done': false
};
}
FLG = false;
kgryte marked this conversation as resolved.
Show resolved Hide resolved
return {
'value': void 0,
kgryte marked this conversation as resolved.
Show resolved Hide resolved
'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
Loading