Skip to content

Commit 1d217ba

Browse files
aayush0325kgryte
andauthored
feat: add map method to array/fixed-endian-factory
PR-URL: #3270 Closes: #3150 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com>
1 parent 3b3d051 commit 1d217ba

File tree

5 files changed

+459
-1
lines changed

5 files changed

+459
-1
lines changed

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

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,68 @@ var v = arr.get( 100 );
483483
// returns undefined
484484
```
485485

486+
<a name="method-map"></a>
487+
488+
#### TypedArray.prototype.map( callbackFn\[, thisArg] )
489+
490+
Returns a new array with each element being the result of a provided callback function.
491+
492+
```javascript
493+
function fcn( v ) {
494+
return -v;
495+
}
496+
497+
var Float64ArrayFE = fixedEndianFactory( 'float64' );
498+
499+
var arr = new Float64ArrayFE( 'little-endian', [ 1.0, 2.0, 3.0 ] );
500+
// returns <Float64ArrayFE>
501+
502+
var out = arr.map( fcn );
503+
// returns <Float64ArrayFE>
504+
505+
var z = out.get( 0 );
506+
// returns -1.0
507+
508+
z = out.get( 1 );
509+
// returns -2.0
510+
511+
z = out.get( 2 );
512+
// returns -3.0
513+
```
514+
515+
The callback function is provided three arguments:
516+
517+
- **value**: current array element.
518+
- **index**: current array element index.
519+
- **arr**: the array on which this method was called.
520+
521+
To set the function execution context, provide a `thisArg`.
522+
523+
```javascript
524+
function fcn( v, i ) {
525+
this.count += i;
526+
return -v;
527+
}
528+
529+
var Float64ArrayFE = fixedEndianFactory( 'float64' );
530+
531+
var arr = new Float64ArrayFE( 'little-endian', 3 );
532+
533+
arr.set( -1.0, 0 );
534+
arr.set( 1.0, 1 );
535+
arr.set( -1.0, 2 );
536+
537+
var context = {
538+
'count': 0
539+
};
540+
541+
var out = arr.map( fcn, context );
542+
// returns <Float64ArrayFE>
543+
544+
var count = context.count;
545+
// returns 3;
546+
```
547+
486548
<a name="method-set"></a>
487549

488550
#### TypedArrayFE.prototype.set( arr\[, offset] )
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+':map', 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.map( identity );
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 identity( v ) {
57+
return v;
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+
* Identity 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 {number} input array element
45+
*/
46+
function identity( value ) {
47+
return value;
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.map( identity );
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 TypedArray' );
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+':map:len='+len, f );
109+
}
110+
}
111+
112+
main();

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

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ var fromIteratorMap = require( './from_iterator_map.js' );
5151
// VARIABLES //
5252

5353
var HAS_ITERATOR_SYMBOL = hasIteratorSymbolSupport();
54+
var LITTLE_ENDIAN = 'little-endian';
55+
var BIG_ENDIAN = 'big-endian';
5456
var DTYPES = [ 'float64', 'float32', 'int32', 'int16', 'uint32', 'uint16' ];
5557
var DTYPE2SET = {
5658
'float64': 'setFloat64',
@@ -99,7 +101,18 @@ function byteOrder( value ) {
99101
* @returns {boolean} boolean indicating whether a byte order is little-endian byte order
100102
*/
101103
function isLittleEndian( value ) {
102-
return ( value === 'little-endian' );
104+
return ( value === LITTLE_ENDIAN );
105+
}
106+
107+
/**
108+
* Resolves a byte order string from a boolean flag.
109+
*
110+
* @private
111+
* @param {boolean} isLE - flag indicating whether an array is little-endian
112+
* @returns {string} resolved byte order
113+
*/
114+
function flag2byteOrder( isLE ) {
115+
return ( isLE ) ? LITTLE_ENDIAN : BIG_ENDIAN;
103116
}
104117

105118
/**
@@ -634,6 +647,41 @@ function factory( dtype ) { // eslint-disable-line max-lines-per-function, stdli
634647
return this._length;
635648
});
636649

650+
/**
651+
* Returns a new array with each element being the result of a provided callback function.
652+
*
653+
* @private
654+
* @name map
655+
* @memberof TypedArray.prototype
656+
* @type {Function}
657+
* @param {Function} fcn - function to invoke
658+
* @param {*} [thisArg] - function invocation context
659+
* @throws {TypeError} `this` must be a typed array instance
660+
* @throws {TypeError} first argument must be a function
661+
* @returns {TypedArray} new typed array
662+
*/
663+
setReadOnly( TypedArray.prototype, 'map', function map( fcn, thisArg ) {
664+
var obuf;
665+
var out;
666+
var buf;
667+
var i;
668+
var v;
669+
if ( !isTypedArray( this ) ) {
670+
throw new TypeError( format( 'invalid invocation. `this` is not %s %s.', CHAR2ARTICLE[ dtype[0] ], CTOR_NAME ) );
671+
}
672+
if ( !isFunction( fcn ) ) {
673+
throw new TypeError( format( 'invalid argument. First argument must be a function. Value: `%s`.', fcn ) );
674+
}
675+
buf = this._buffer;
676+
out = new this.constructor( flag2byteOrder( this._isLE ), this._length );
677+
obuf = out._buffer; // eslint-disable-line no-underscore-dangle
678+
for ( i = 0; i < this._length; i++ ) {
679+
v = fcn.call( thisArg, buf[ GETTER ]( i * BYTES_PER_ELEMENT, this._isLE ), i, this );
680+
obuf[ SETTER ]( i * BYTES_PER_ELEMENT, v, this._isLE );
681+
}
682+
return out;
683+
});
684+
637685
/**
638686
* Sets an array element.
639687
*

0 commit comments

Comments
 (0)