Skip to content

Commit 50327dc

Browse files
authored
feat: add filter method to array/complex128
PR-URL: #1225 Reviewed-by: Athan Reines <kgryte@gmail.com>
1 parent fc39aa9 commit 50327dc

File tree

6 files changed

+540
-0
lines changed

6 files changed

+540
-0
lines changed

lib/node_modules/@stdlib/array/complex128/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+
#### Complex128Array.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 real = require( '@stdlib/complex/real' );
867+
var imag = require( '@stdlib/complex/imag' );
868+
869+
function predicate( v ) {
870+
return ( real( v ) === imag( v ) );
871+
}
872+
873+
var arr = new Complex128Array( 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 <Complex128Array>
882+
883+
var len = out.length;
884+
// returns 1
885+
886+
var z = out.get( 0 );
887+
// returns <Complex128>
888+
889+
var re = real( z );
890+
// returns 2.0
891+
892+
var im = imag( 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 real = require( '@stdlib/complex/real' );
906+
var imag = require( '@stdlib/complex/imag' );
907+
908+
function predicate( v, i ) {
909+
this.count += 1;
910+
return ( i >= 0 && real( v ) === imag( v ) );
911+
}
912+
913+
var arr = new Complex128Array( 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 <Complex128Array>
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
#### Complex128Array.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 isComplex128 = require( '@stdlib/assert/is-complex128' );
25+
var isComplex128Array = require( '@stdlib/assert/is-complex128array' );
26+
var pkg = require( './../package.json' ).name;
27+
var Complex128Array = 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 Complex128Array( [ 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 ( !isComplex128Array( out ) ) {
48+
b.fail( 'should return a Complex128Array' );
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 isComplex128Array = require( '@stdlib/assert/is-complex128array' );
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 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 ( !isComplex128Array( out ) ) {
86+
b.fail( 'should return a Complex128Array' );
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/complex128/docs/types/index.d.ts

+38
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,44 @@ declare class Complex128Array implements Complex128ArrayInterface {
343343
*/
344344
every<U = unknown>( predicate: Predicate<U>, 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 real = require( '@stdlib/complex/real' );
355+
* var imag = require( '@stdlib/complex/imag' );
356+
*
357+
* function predicate( v ) {
358+
* return ( real( v ) === imag( v ) );
359+
* }
360+
*
361+
* var arr = new Complex128Array( 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 <Complex128Array>
369+
*
370+
* var len = out.length;
371+
* // returns 1
372+
*
373+
* var z = out.get( 0 );
374+
* // returns <Complex128>
375+
*
376+
* var re = real( z );
377+
* // returns 2.0
378+
*
379+
* var im = imag( z );
380+
* // returns 2.0
381+
*/
382+
filter<U = unknown>( predicate: Predicate<U>, thisArg?: ThisParameterType<Predicate<U>> ): Complex128Array;
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/complex128/lib/main.js

+63
Original file line numberDiff line numberDiff line change
@@ -920,6 +920,69 @@ setReadOnly( Complex128Array.prototype, 'every', function every( predicate, this
920920
return true;
921921
});
922922

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

0 commit comments

Comments
 (0)