Skip to content

Commit 7fb88d4

Browse files
Jaysukh-409kgryte
andauthored
feat: add filter method to array/complex64
PR-URL: #1206 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]> Signed-off-by: Athan Reines <[email protected]>
1 parent 814fb4c commit 7fb88d4

File tree

6 files changed

+540
-0
lines changed

6 files changed

+540
-0
lines changed

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

+75
Original file line numberDiff line numberDiff line change
@@ -856,6 +856,81 @@ var count = context.count;
856856
// returns 3
857857
```
858858

859+
<a name="method-filter"></a>
860+
861+
#### Complex64Array.prototype.filter( predicate\[, thisArg] )
862+
863+
Returns a new array containing the elements of an array which pass a test implemented by a predicate function.
864+
865+
```javascript
866+
var realf = require( '@stdlib/complex/realf' );
867+
var imagf = require( '@stdlib/complex/imagf' );
868+
869+
function predicate( v ) {
870+
return ( realf( v ) === imagf( v ) );
871+
}
872+
873+
var arr = new Complex64Array( 3 );
874+
875+
// Set the first three elements:
876+
arr.set( [ 1.0, -1.0 ], 0 );
877+
arr.set( [ 2.0, 2.0 ], 1 );
878+
arr.set( [ 3.0, -3.0 ], 2 );
879+
880+
var out = arr.filter( predicate );
881+
// returns <Complex64Array>
882+
883+
var len = out.length;
884+
// returns 1
885+
886+
var z = out.get( 0 );
887+
// returns <Complex64>
888+
889+
var re = realf( z );
890+
// returns 2.0
891+
892+
var im = imagf( z );
893+
// returns 2.0
894+
```
895+
896+
The `predicate` function is provided three arguments:
897+
898+
- **value**: current array element.
899+
- **index**: current array element index.
900+
- **arr**: the array on which this method was called.
901+
902+
To set the function execution context, provide a `thisArg`.
903+
904+
```javascript
905+
var realf = require( '@stdlib/complex/realf' );
906+
var imagf = require( '@stdlib/complex/imagf' );
907+
908+
function predicate( v, i ) {
909+
this.count += 1;
910+
return ( i >= 0 && realf( v ) === imagf( v ) );
911+
}
912+
913+
var arr = new Complex64Array( 3 );
914+
915+
var context = {
916+
'count': 0
917+
};
918+
919+
// Set the first three elements:
920+
arr.set( [ 1.0, -1.0 ], 0 );
921+
arr.set( [ 2.0, 2.0 ], 1 );
922+
arr.set( [ 3.0, 3.0 ], 2 );
923+
924+
var out = arr.filter( predicate, context );
925+
// returns <Complex64Array>
926+
927+
var len = out.length;
928+
// returns 2
929+
930+
var count = context.count;
931+
// returns 3
932+
```
933+
859934
<a name="method-find"></a>
860935

861936
#### Complex64Array.prototype.find( predicate\[, thisArg] )
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 isComplex64Array = require( '@stdlib/assert/is-complex64array' );
26+
var pkg = require( './../package.json' ).name;
27+
var Complex64Array = require( './../lib' );
28+
29+
30+
// MAIN //
31+
32+
bench( pkg+':filter', function benchmark( b ) {
33+
var out;
34+
var arr;
35+
var i;
36+
37+
arr = new Complex64Array( [ 1, 2, 3, 4, 5, 6 ] );
38+
39+
b.tic();
40+
for ( i = 0; i < b.iterations; i++ ) {
41+
out = arr.filter( predicate );
42+
if ( typeof out !== 'object' ) {
43+
b.fail( 'should return an object' );
44+
}
45+
}
46+
b.toc();
47+
if ( !isComplex64Array( out ) ) {
48+
b.fail( 'should return a Complex64Array' );
49+
}
50+
b.pass( 'benchmark finished' );
51+
b.end();
52+
53+
function predicate( v ) {
54+
return isComplex64( 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 isComplex64Array = require( '@stdlib/assert/is-complex64array' );
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 out;
75+
var i;
76+
77+
b.tic();
78+
for ( i = 0; i < b.iterations; i++ ) {
79+
out = arr.filter( predicate );
80+
if ( typeof out !== 'object' ) {
81+
b.fail( 'should return an object' );
82+
}
83+
}
84+
b.toc();
85+
if ( !isComplex64Array( out ) ) {
86+
b.fail( 'should return a Complex64Array' );
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+':filter:len='+len, f );
115+
}
116+
}
117+
118+
main();

lib/node_modules/@stdlib/array/complex64/docs/types/index.d.ts

+38
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,44 @@ declare class Complex64Array implements Complex64ArrayInterface {
343343
*/
344344
every<U = unknown>( predicate: Predicate, thisArg?: ThisParameterType<Predicate<U>> ): boolean;
345345

346+
/**
347+
* Returns a new array containing the elements of an array which pass a test implemented by a predicate function.
348+
*
349+
* @param predicate - test function
350+
* @param thisArg - execution context
351+
* @returns new array containing elements which pass a test implemented by a predicate function
352+
*
353+
* @example
354+
* var realf = require( '@stdlib/complex/realf' );
355+
* var imagf = require( '@stdlib/complex/imagf' );
356+
*
357+
* function predicate( v ) {
358+
* return ( realf( v ) === imagf( v ) );
359+
* }
360+
*
361+
* var arr = new Complex64Array( 3 );
362+
*
363+
* arr.set( [ 1.0, -1.0 ], 0 );
364+
* arr.set( [ 2.0, 2.0 ], 1 );
365+
* arr.set( [ 3.0, -3.0 ], 2 );
366+
*
367+
* var out = arr.filter( predicate );
368+
* // returns <Complex64Array>
369+
*
370+
* var len = out.length;
371+
* // returns 1
372+
*
373+
* var z = out.get( 0 );
374+
* // returns <Complex64>
375+
*
376+
* var re = realf( z );
377+
* // returns 2.0
378+
*
379+
* var im = imagf( z );
380+
* // returns 2.0
381+
*/
382+
filter<U = unknown>( predicate: Predicate, thisArg?: ThisParameterType<Predicate<U>> ): Complex64Array;
383+
346384
/**
347385
* Returns the first element in an array for which a predicate function returns a truthy value.
348386
*

lib/node_modules/@stdlib/array/complex64/lib/main.js

+63
Original file line numberDiff line numberDiff line change
@@ -919,6 +919,69 @@ setReadOnly( Complex64Array.prototype, 'every', function every( predicate, thisA
919919
return true;
920920
});
921921

922+
/**
923+
* Returns a new array containing the elements of an array which pass a test implemented by a predicate function.
924+
*
925+
* @name filter
926+
* @memberof Complex64Array.prototype
927+
* @type {Function}
928+
* @param {Function} predicate - test function
929+
* @param {*} [thisArg] - predicate function execution context
930+
* @throws {TypeError} `this` must be a complex number array
931+
* @throws {TypeError} first argument must be a function
932+
* @returns {Complex64Array} complex number array
933+
*
934+
* @example
935+
* var realf = require( '@stdlib/complex/realf' );
936+
* var imagf = require( '@stdlib/complex/imagf' );
937+
*
938+
* function predicate( v ) {
939+
* return ( realf( v ) === imagf( v ) );
940+
* }
941+
*
942+
* var arr = new Complex64Array( 3 );
943+
*
944+
* arr.set( [ 1.0, -1.0 ], 0 );
945+
* arr.set( [ 2.0, 2.0 ], 1 );
946+
* arr.set( [ 3.0, -3.0 ], 2 );
947+
*
948+
* var out = arr.filter( predicate );
949+
* // returns <Complex64Array>
950+
*
951+
* var len = out.length;
952+
* // returns 1
953+
*
954+
* var z = out.get( 0 );
955+
* // returns <Complex64>
956+
*
957+
* var re = realf( z );
958+
* // returns 2.0
959+
*
960+
* var im = imagf( z );
961+
* // returns 2.0
962+
*/
963+
setReadOnly( Complex64Array.prototype, 'filter', function filter( predicate, thisArg ) {
964+
var buf;
965+
var out;
966+
var i;
967+
var z;
968+
if ( !isComplexArray( this ) ) {
969+
throw new TypeError( 'invalid invocation. `this` is not a complex number array.' );
970+
}
971+
if ( !isFunction( predicate ) ) {
972+
throw new TypeError( format( 'invalid argument. First argument must be a function. Value: `%s`.', predicate ) );
973+
}
974+
buf = this._buffer;
975+
out = [];
976+
for ( i = 0; i < this._length; i++ ) {
977+
z = getComplex64( buf, i );
978+
if ( predicate.call( thisArg, z, i, this ) ) {
979+
out.push( z );
980+
}
981+
}
982+
return new this.constructor( out );
983+
});
984+
922985
/**
923986
* Returns the first element in an array for which a predicate function returns a truthy value.
924987
*

0 commit comments

Comments
 (0)