Skip to content

Commit 814fb4c

Browse files
Jaysukh-409kgryte
andauthored
feat: add every method to array/complex128
PR-URL: #1207 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]> Signed-off-by: Athan Reines <[email protected]>
1 parent 1464d5b commit 814fb4c

File tree

6 files changed

+524
-0
lines changed

6 files changed

+524
-0
lines changed

Diff for: lib/node_modules/@stdlib/array/complex128/README.md

+58
Original file line numberDiff line numberDiff line change
@@ -798,6 +798,64 @@ var bool = it.next().done;
798798
// returns true
799799
```
800800

801+
<a name="method-every"></a>
802+
803+
#### Complex128Array.prototype.every( predicate\[, thisArg] )
804+
805+
Returns a boolean indicating whether all elements pass a test.
806+
807+
```javascript
808+
var real = require( '@stdlib/complex/real' );
809+
var imag = require( '@stdlib/complex/imag' );
810+
811+
function predicate( v ) {
812+
return ( real( v ) === imag( v ) );
813+
}
814+
815+
var arr = new Complex128Array( 3 );
816+
817+
// Set the first three elements:
818+
arr.set( [ 1.0, 1.0 ], 0 );
819+
arr.set( [ 2.0, 2.0 ], 1 );
820+
arr.set( [ 3.0, 3.0 ], 2 );
821+
822+
// Check whether all elements pass a test:
823+
var bool = arr.every( predicate );
824+
// returns true
825+
```
826+
827+
The `predicate` function is provided three arguments:
828+
829+
- **value**: current array element.
830+
- **index**: current array element index.
831+
- **arr**: the array on which this method was called.
832+
833+
To set the function execution context, provide a `thisArg`.
834+
835+
```javascript
836+
function predicate( v, i ) {
837+
this.count += 1;
838+
return ( i >= 0 );
839+
}
840+
841+
var arr = new Complex128Array( 3 );
842+
843+
var context = {
844+
'count': 0
845+
};
846+
847+
// Set the first three elements:
848+
arr.set( [ 1.0, 1.0 ], 0 );
849+
arr.set( [ 2.0, 2.0 ], 1 );
850+
arr.set( [ 3.0, 3.0 ], 2 );
851+
852+
var bool = arr.every( predicate, context );
853+
// returns true
854+
855+
var count = context.count;
856+
// returns 3
857+
```
858+
801859
<a name="method-get"></a>
802860

803861
#### Complex128Array.prototype.get( i )
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 isComplex128 = require( '@stdlib/assert/is-complex128' );
25+
var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive;
26+
var pkg = require( './../package.json' ).name;
27+
var Complex128Array = 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 Complex128Array( [ 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 isComplex128( v );
55+
}
56+
});
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 Complex128 = require( '@stdlib/complex/float64' );
27+
var real = require( '@stdlib/complex/real' );
28+
var imag = require( '@stdlib/complex/imag' );
29+
var pkg = require( './../package.json' ).name;
30+
var Complex128Array = require( './../lib' );
31+
32+
33+
// FUNCTIONS //
34+
35+
/**
36+
* Predicate function.
37+
*
38+
* @private
39+
* @param {Complex128} value - array element
40+
* @param {NonNegativeInteger} idx - array element index
41+
* @param {Complex128Array} arr - array instance
42+
* @returns {boolean} boolean indicating whether a value passes a test
43+
*/
44+
function predicate( value ) {
45+
return ( real( value ) === imag( 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 Complex128( i, i ) );
62+
}
63+
arr = new Complex128Array( 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();

Diff for: lib/node_modules/@stdlib/array/complex128/docs/types/index.d.ts

+70
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,50 @@ import ArrayBuffer = require( '@stdlib/array/buffer' );
2828
// Define a union type representing both iterable and non-iterable iterators:
2929
type Iterator = Iter | IterableIterator;
3030

31+
/**
32+
* Checks whether an element in an array passes a test.
33+
*
34+
* @returns boolean indicating whether an element in an array passes a test
35+
*/
36+
type NullaryPredicate<U> = ( this: U ) => boolean;
37+
38+
/**
39+
* Checks whether an element in an array passes a test.
40+
*
41+
* @param value - current array element
42+
* @returns boolean indicating whether an element in an array passes a test
43+
*/
44+
type UnaryPredicate<U> = ( this: U, value: Complex64 ) => boolean;
45+
46+
/**
47+
* Checks whether an element in an array passes a test.
48+
*
49+
* @param value - current array element
50+
* @param index - current array element index
51+
* @returns boolean indicating whether an element in an array passes a test
52+
*/
53+
type BinaryPredicate<U> = ( this: U, value: Complex64, index: number ) => boolean;
54+
55+
/**
56+
* Checks whether an element in an array passes a test.
57+
*
58+
* @param value - current array element
59+
* @param index - current array element index
60+
* @param arr - array on which the method was called
61+
* @returns boolean indicating whether an element in an array passes a test
62+
*/
63+
type TernaryPredicate<U> = ( this: U, value: Complex64, index: number, arr: Complex64Array ) => boolean;
64+
65+
/**
66+
* Checks whether an element in an array passes a test.
67+
*
68+
* @param value - current array element
69+
* @param index - current array element index
70+
* @param arr - array on which the method was called
71+
* @returns boolean indicating whether an element in an array passes a test
72+
*/
73+
type Predicate<U> = NullaryPredicate<U> | UnaryPredicate<U> | BinaryPredicate<U> | TernaryPredicate<U>;
74+
3175
/**
3276
* Class for creating a 128-bit complex number array.
3377
*/
@@ -240,6 +284,32 @@ declare class Complex128Array implements Complex128ArrayInterface {
240284
*/
241285
entries(): Iterator;
242286

287+
/**
288+
* Tests whether all elements in an array pass a test implemented by a predicate function.
289+
*
290+
* @param predicate - test function
291+
* @param thisArg - execution context
292+
* @returns boolean indicating whether all elements pass a test
293+
*
294+
* @example
295+
* var real = require( '@stdlib/complex/real' );
296+
* var imag = require( '@stdlib/complex/imag' );
297+
*
298+
* function predicate( v ) {
299+
* return ( real( v ) === imag( v ) );
300+
* }
301+
*
302+
* var arr = new Complex128Array( 3 );
303+
*
304+
* arr.set( [ 1.0 , 1.0 ], 0 );
305+
* arr.set( [ 2.0 , 2.0 ], 1 );
306+
* arr.set( [ 3.0 , 3.0 ], 2 );
307+
*
308+
* var bool = arr.every( predicate );
309+
* // returns true
310+
*/
311+
every<U = unknown>( predicate: Predicate, thisArg?: ThisParameterType<Predicate<U>> ): boolean;
312+
243313
/**
244314
* Returns an array element.
245315
*

Diff for: lib/node_modules/@stdlib/array/complex128/lib/main.js

+47
Original file line numberDiff line numberDiff line change
@@ -873,6 +873,53 @@ setReadOnly( Complex128Array.prototype, 'entries', function entries() {
873873
}
874874
});
875875

876+
/**
877+
* Tests whether all elements in an array pass a test implemented by a predicate function.
878+
*
879+
* @name every
880+
* @memberof Complex128Array.prototype
881+
* @type {Function}
882+
* @param {Function} predicate - test function
883+
* @param {*} [thisArg] - predicate function execution context
884+
* @throws {TypeError} `this` must be a complex number array
885+
* @throws {TypeError} first argument must be a function
886+
* @returns {boolean} boolean indicating whether all elements pass a test
887+
*
888+
* @example
889+
* var real = require( '@stdlib/complex/real' );
890+
* var imag = require( '@stdlib/complex/imag' );
891+
*
892+
* function predicate( v ) {
893+
* return ( real( v ) === imag( v ) );
894+
* }
895+
*
896+
* var arr = new Complex128Array( 3 );
897+
*
898+
* arr.set( [ 1.0, 1.0 ], 0 );
899+
* arr.set( [ 2.0, 2.0 ], 1 );
900+
* arr.set( [ 3.0, 3.0 ], 2 );
901+
*
902+
* var bool = arr.every( predicate );
903+
* // returns true
904+
*/
905+
setReadOnly( Complex128Array.prototype, 'every', function every( predicate, thisArg ) {
906+
var buf;
907+
var i;
908+
if ( !isComplexArray( this ) ) {
909+
throw new TypeError( 'invalid invocation. `this` is not a complex number array.' );
910+
}
911+
if ( !isFunction( predicate ) ) {
912+
throw new TypeError( format( 'invalid argument. First argument must be a function. Value: `%s`.', predicate ) );
913+
}
914+
buf = this._buffer;
915+
for ( i = 0; i < this._length; i++ ) {
916+
if ( !predicate.call( thisArg, getComplex128( buf, i ), i, this ) ) {
917+
return false;
918+
}
919+
}
920+
return true;
921+
});
922+
876923
/**
877924
* Returns an array element.
878925
*

0 commit comments

Comments
 (0)