Skip to content

Commit 9272073

Browse files
Jaysukh-409kgryte
andauthored
feat: add every method to array/complex64
PR-URL: #1184 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com>
1 parent 4f80f99 commit 9272073

File tree

8 files changed

+600
-36
lines changed

8 files changed

+600
-36
lines changed

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

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -833,6 +833,64 @@ z = arr.at( -100 );
833833
// returns undefined
834834
```
835835

836+
<a name="method-every"></a>
837+
838+
#### Complex64Array.prototype.every( predicate\[, thisArg] )
839+
840+
Returns a boolean indicating whether all elements pass a test.
841+
842+
```javascript
843+
var realf = require( '@stdlib/complex/realf' );
844+
var imagf = require( '@stdlib/complex/imagf' );
845+
846+
function predicate( v ) {
847+
return ( realf( v ) === imagf( v ) );
848+
}
849+
850+
var arr = new Complex64Array( 3 );
851+
852+
// Set the first three elements:
853+
arr.set( [ 1.0, 1.0 ], 0 );
854+
arr.set( [ 2.0, 2.0 ], 1 );
855+
arr.set( [ 3.0, 3.0 ], 2 );
856+
857+
// Check whether all elements pass a test:
858+
var z = arr.every( predicate );
859+
// returns true
860+
```
861+
862+
The `predicate` function is provided four arguments:
863+
864+
- **value**: current array element.
865+
- **index**: current array element index.
866+
- **arr**: the array on which this method was called.
867+
868+
To set the function execution context, provide a `thisArg`.
869+
870+
```javascript
871+
function predicate( v, i ) {
872+
this.count += 1;
873+
return ( i >= 0 );
874+
}
875+
876+
var arr = new Complex64Array( 3 );
877+
878+
var context = {
879+
'count': 0
880+
};
881+
882+
// Set the first three elements:
883+
arr.set( [ 1.0, 1.0 ], 0 );
884+
arr.set( [ 2.0, 2.0 ], 1 );
885+
arr.set( [ 3.0, 3.0 ], 2 );
886+
887+
var z = arr.every( predicate, context );
888+
// returns true
889+
890+
var count = context.count;
891+
// returns 3
892+
```
893+
836894
<a name="method-set"></a>
837895

838896
#### Complex64Array.prototype.set( z\[, i] )

lib/node_modules/@stdlib/array/complex64/benchmark/benchmark.copy_within.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ var Complex64Array = require( './../lib' );
2828

2929
// MAIN //
3030

31-
bench( pkg+':copyWithin', function benchmark( b ) {
31+
bench( pkg+':copyWithin:len=5', function benchmark( b ) {
3232
var arr;
3333
var buf;
3434
var i;

lib/node_modules/@stdlib/array/complex64/benchmark/benchmark.entries.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ var Complex64Array = require( './../lib' );
2828

2929
// MAIN //
3030

31-
bench( pkg+':entries', function benchmark( b ) {
31+
bench( pkg+':entries:len=10', function benchmark( b ) {
3232
var iter;
3333
var arr;
3434
var i;
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2023 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 isComplex64 = require( '@stdlib/assert/is-complex64' );
25+
var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive;
26+
var pkg = require( './../package.json' ).name;
27+
var Complex64Array = require( './../lib' );
28+
29+
30+
// MAIN //
31+
32+
bench( pkg+':every', function benchmark( b ) {
33+
var bool;
34+
var arr;
35+
var i;
36+
37+
arr = new Complex64Array( [ 1, 2, 3, 4 ] );
38+
39+
b.tic();
40+
for ( i = 0; i < b.iterations; i++ ) {
41+
bool = arr.every( predicate );
42+
if ( typeof bool !== 'boolean' ) {
43+
b.fail( 'should return a boolean' );
44+
}
45+
}
46+
b.toc();
47+
if ( !isBoolean( bool ) ) {
48+
b.fail( 'should return a boolean' );
49+
}
50+
b.pass( 'benchmark finished' );
51+
b.end();
52+
53+
function predicate( v ) {
54+
return isComplex64( v );
55+
}
56+
});
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2023 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 isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive;
25+
var pow = require( '@stdlib/math/base/special/pow' );
26+
var Complex64 = require( '@stdlib/complex/float32' );
27+
var realf = require( '@stdlib/complex/realf' );
28+
var imagf = require( '@stdlib/complex/imagf' );
29+
var pkg = require( './../package.json' ).name;
30+
var Complex64Array = require( './../lib' );
31+
32+
33+
// FUNCTIONS //
34+
35+
/**
36+
* Predicate function.
37+
*
38+
* @private
39+
* @param {Complex64} value - array element
40+
* @param {NonNegativeInteger} idx - array element index
41+
* @param {Complex64Array} arr - array instance
42+
* @returns {boolean} boolean indicating whether a value passes a test
43+
*/
44+
function predicate( value ) {
45+
return ( realf( value ) === imagf( value ) );
46+
}
47+
48+
/**
49+
* Creates a benchmark function.
50+
*
51+
* @private
52+
* @param {PositiveInteger} len - array length
53+
* @returns {Function} benchmark function
54+
*/
55+
function createBenchmark( len ) {
56+
var arr;
57+
var i;
58+
59+
arr = [];
60+
for ( i = 0; i < len; i++ ) {
61+
arr.push( new Complex64( i, i ) );
62+
}
63+
arr = new Complex64Array( arr );
64+
65+
return benchmark;
66+
67+
/**
68+
* Benchmark function.
69+
*
70+
* @private
71+
* @param {Benchmark} b - benchmark instance
72+
*/
73+
function benchmark( b ) {
74+
var bool;
75+
var i;
76+
77+
b.tic();
78+
for ( i = 0; i < b.iterations; i++ ) {
79+
bool = arr.every( predicate );
80+
if ( typeof bool !== 'boolean' ) {
81+
b.fail( 'should return a boolean' );
82+
}
83+
}
84+
b.toc();
85+
if ( !isBoolean( bool ) ) {
86+
b.fail( 'should return a boolean' );
87+
}
88+
b.pass( 'benchmark finished' );
89+
b.end();
90+
}
91+
}
92+
93+
94+
// MAIN //
95+
96+
/**
97+
* Main execution sequence.
98+
*
99+
* @private
100+
*/
101+
function main() {
102+
var len;
103+
var min;
104+
var max;
105+
var f;
106+
var i;
107+
108+
min = 1; // 10^min
109+
max = 6; // 10^max
110+
111+
for ( i = min; i <= max; i++ ) {
112+
len = pow( 10, i );
113+
f = createBenchmark( len );
114+
bench( pkg+':every:len='+len, f );
115+
}
116+
}
117+
118+
main();

0 commit comments

Comments
 (0)