Skip to content

Commit 1242bbf

Browse files
feat: add filter method to array/fixed-endian-factory
PR-URL: #3278 Closes: #3140 Co-authored-by: Philipp Burckhardt <[email protected]> Reviewed-by: Philipp Burckhardt <[email protected]> Signed-off-by: Philipp Burckhardt <[email protected]>
1 parent 41af546 commit 1242bbf

File tree

5 files changed

+491
-0
lines changed

5 files changed

+491
-0
lines changed

lib/node_modules/@stdlib/array/fixed-endian-factory/README.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,66 @@ var count = context.count;
451451
// returns 3
452452
```
453453

454+
<a name="method-filter"></a>
455+
456+
#### TypedArrayFE.prototype.filter( predicate\[, thisArg] )
457+
458+
Returns a new array containing the elements of an array which pass a test implemented by a predicate function.
459+
460+
```javascript
461+
function predicate( v ) {
462+
return ( v % 2 === 0 );
463+
}
464+
465+
var Float64ArrayFE = fixedEndianFactory( 'float64' );
466+
467+
var arr = new Float64ArrayFE( 'little-endian', [ 1.0, 2.0, 3.0, 4.0 ] );
468+
469+
var out = arr.filter( predicate );
470+
// returns <Float64ArrayFE>
471+
472+
var len = out.length;
473+
// returns 2
474+
475+
var v = out.get( 0 );
476+
// returns 2.0
477+
478+
v = out.get( 1 );
479+
// return 4.0
480+
```
481+
482+
The `predicate` function is provided three arguments:
483+
484+
- **value**: current array element.
485+
- **index**: current array element index.
486+
- **arr**: the array on which this method was called.
487+
488+
To set the function execution context, provide a `thisArg`.
489+
490+
```javascript
491+
function predicate( v, i ) {
492+
this.count += 1;
493+
return ( v % 2 === 0 );
494+
}
495+
496+
var context = {
497+
'count': 0
498+
};
499+
500+
var Float64ArrayFE = fixedEndianFactory( 'float64' );
501+
502+
var arr = new Float64ArrayFE( 'little-endian', [ 1.0, 2.0, 3.0, 4.0 ] );
503+
504+
var out = arr.filter( predicate, context );
505+
// returns <Float64ArrayFE>
506+
507+
var len = out.length;
508+
// returns 2
509+
510+
var count = context.count;
511+
// returns 4
512+
```
513+
454514
<a name="method-get"></a>
455515

456516
#### TypedArrayFE.prototype.get( i )
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 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 pkg = require( './../package.json' ).name;
25+
var factory = require( './../lib' );
26+
27+
28+
// VARIABLES //
29+
30+
var Float64ArrayFE = factory( 'float64' );
31+
32+
33+
// MAIN //
34+
35+
bench( pkg+':filter', function benchmark( b ) {
36+
var out;
37+
var arr;
38+
var i;
39+
40+
arr = new Float64ArrayFE( 'little-endian', [ 1.0, 2.0, 2.0, 1.0 ] );
41+
42+
b.tic();
43+
for ( i = 0; i < b.iterations; i++ ) {
44+
out = arr.filter( predicate );
45+
if ( typeof out !== 'object' ) {
46+
b.fail( 'should return an object' );
47+
}
48+
}
49+
b.toc();
50+
if ( !( out instanceof Float64ArrayFE ) ) {
51+
b.fail( 'should return a typed array' );
52+
}
53+
b.pass( 'benchmark finished' );
54+
b.end();
55+
56+
function predicate( v ) {
57+
return v % 2 === 0;
58+
}
59+
});
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 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 pow = require( '@stdlib/math/base/special/pow' );
25+
var zeroTo = require( '@stdlib/array/zero-to' );
26+
var pkg = require( './../package.json' ).name;
27+
var factory = require( './../lib' );
28+
29+
30+
// VARIABLES //
31+
32+
var Float64ArrayFE = factory( 'float64' );
33+
34+
35+
// FUNCTIONS //
36+
37+
/**
38+
* Predicate function.
39+
*
40+
* @private
41+
* @param {number} value - array element
42+
* @param {NonNegativeInteger} idx - array element index
43+
* @param {TypedArray} arr - array instance
44+
* @returns {boolean} boolean indicating whether a value passes a test
45+
*/
46+
function predicate( value ) {
47+
return value % 2 === 0;
48+
}
49+
50+
/**
51+
* Creates a benchmark function.
52+
*
53+
* @private
54+
* @param {PositiveInteger} len - array length
55+
* @returns {Function} benchmark function
56+
*/
57+
function createBenchmark( len ) {
58+
var arr = new Float64ArrayFE( 'little-endian', zeroTo( len ) );
59+
return benchmark;
60+
61+
/**
62+
* Benchmark function.
63+
*
64+
* @private
65+
* @param {Benchmark} b - benchmark instance
66+
*/
67+
function benchmark( b ) {
68+
var out;
69+
var i;
70+
71+
b.tic();
72+
for ( i = 0; i < b.iterations; i++ ) {
73+
out = arr.filter( predicate );
74+
if ( typeof out !== 'object' ) {
75+
b.fail( 'should return an object' );
76+
}
77+
}
78+
b.toc();
79+
if ( !( out instanceof Float64ArrayFE ) ) {
80+
b.fail( 'should return a typed array' );
81+
}
82+
b.pass( 'benchmark finished' );
83+
b.end();
84+
}
85+
}
86+
87+
88+
// MAIN //
89+
90+
/**
91+
* Main execution sequence.
92+
*
93+
* @private
94+
*/
95+
function main() {
96+
var len;
97+
var min;
98+
var max;
99+
var f;
100+
var i;
101+
102+
min = 1; // 10^min
103+
max = 6; // 10^max
104+
105+
for ( i = min; i <= max; i++ ) {
106+
len = pow( 10, i );
107+
f = createBenchmark( len );
108+
bench( pkg+':filter:len='+len, f );
109+
}
110+
}
111+
112+
main();

lib/node_modules/@stdlib/array/fixed-endian-factory/lib/main.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -609,6 +609,41 @@ function factory( dtype ) { // eslint-disable-line max-lines-per-function, stdli
609609
}
610610
});
611611

612+
/**
613+
* Returns a new array containing the elements of an array which pass a test implemented by a predicate function.
614+
*
615+
* @name filter
616+
* @memberof TypedArray.prototype
617+
* @type {Function}
618+
* @param {Function} predicate - test function
619+
* @param {*} [thisArg] - predicate function execution context
620+
* @throws {TypeError} `this` must be a typed array instance
621+
* @throws {TypeError} first argument must be a function
622+
* @returns {TypedArray} typed array
623+
*/
624+
setReadOnly( TypedArray.prototype, 'filter', function filter( predicate, thisArg ) {
625+
var buf;
626+
var out;
627+
var i;
628+
var v;
629+
630+
if ( !isTypedArray( this ) ) {
631+
throw new TypeError( format( 'invalid invocation. `this` is not %s %s.', CHAR2ARTICLE[ dtype[0] ], CTOR_NAME ) );
632+
}
633+
if ( !isFunction( predicate ) ) {
634+
throw new TypeError( format( 'invalid argument. First argument must be a function. Value: `%s`.', predicate ) );
635+
}
636+
buf = this._buffer;
637+
out = [];
638+
for ( i = 0; i < this._length; i++) {
639+
v = buf[ GETTER ]( i*BYTES_PER_ELEMENT, this._isLE );
640+
if ( predicate.call( thisArg, v, i, this ) ) {
641+
out.push( v );
642+
}
643+
}
644+
return new this.constructor( flag2byteOrder( this._isLE ), out );
645+
});
646+
612647
/**
613648
* Returns an array element.
614649
*

0 commit comments

Comments
 (0)