Skip to content

Commit f627a3e

Browse files
feat: adding 'slice' prototype method in fixed-endian-factory
Added necessary changes in test ,benchmark and README.md files Fixes : #3157
1 parent d8792cd commit f627a3e

File tree

5 files changed

+606
-0
lines changed

5 files changed

+606
-0
lines changed

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

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,97 @@ A few notes:
535535
- If a target array cannot accommodate all values (i.e., the length of source array plus `i` exceeds the target array length), the method throws an error.
536536
- If provided a typed array which shares an [`ArrayBuffer`][@stdlib/array/buffer] with the target array, the method will intelligently copy the source range to the destination range.
537537

538+
<a name="method-subarray"></a>
539+
540+
#### TypedArrayFE.prototype.subarray( \[start\[, end]] )
541+
542+
Copies a portion of a typed array to a new typed array.
543+
544+
```javascript
545+
546+
var fixedEndianFactory = require( '@stdlib/array/fixed-endian-factory' );
547+
548+
var Float64ArrayFE = fixedEndianFactory( 'float64' );
549+
550+
var arr = new Float64ArrayFE( 'little-endian', 5 );
551+
// returns <Float64ArrayFE>
552+
553+
arr.set( 0, 0 );
554+
arr.set( 1, 1 );
555+
arr.set( 2, 2 );
556+
arr.set( 3, 3 );
557+
arr.set( 4, 4 );
558+
559+
var out = arr.subarray();
560+
// returns <Float64ArrayFE>
561+
562+
var len = out.length;
563+
// returns 5
564+
565+
var bool = out.get( 0 );
566+
// returns 0
567+
568+
bool = out.get( len-1 );
569+
// returns 4
570+
```
571+
572+
By default, the method returns a typed array beginning with the first array element. To specify an alternative array index at which to begin, provide a `start` index (inclusive).
573+
574+
```javascript
575+
var fixedEndianFactory = require( '@stdlib/array/fixed-endian-factory' );
576+
577+
var Float64ArrayFE = fixedEndianFactory( 'float64' );
578+
579+
var arr = new Float64ArrayFE( 'little-endian', 5 );
580+
// returns <Float64ArrayFE>
581+
582+
arr.set( 0, 0 );
583+
arr.set( 1, 1 );
584+
arr.set( 2, 2 );
585+
arr.set( 3, 3 );
586+
arr.set( 4, 4 );
587+
588+
var out = arr.subarray( 1 );
589+
// returns <Float64ArrayFE>
590+
591+
var len = out.length;
592+
// returns 4
593+
594+
var bool = out.get( 0 );
595+
// returns 1
596+
597+
bool = out.get( len-1 );
598+
// returns 4
599+
```
600+
601+
By default, the method returns a typed array which includes all array elements after `start`. To limit the number of array elements after `start`, provide an `end` index (exclusive).
602+
603+
```javascript
604+
var fixedEndianFactory = require( '@stdlib/array/fixed-endian-factory' );
605+
606+
var Float64ArrayFE = fixedEndianFactory( 'float64' );
607+
608+
var arr = new Float64ArrayFE( 'little-endian', 5 );
609+
610+
arr.set( 0, 0 );
611+
arr.set( 1, 1 );
612+
arr.set( 2, 2 );
613+
arr.set( 3, 3 );
614+
arr.set( 4, 4 );
615+
616+
var out = arr.subarray( 1, -2 );
617+
// returns <Float64ArrayFE>
618+
619+
var len = out.length;
620+
// returns 2
621+
622+
var bool = out.get( 0 );
623+
// returns 1
624+
625+
bool = out.get( len-1 );
626+
// returns 2
627+
```
628+
538629
<a name="method-some"></a>
539630

540631
#### TypedArrayFE.prototype.some( predicate\[, thisArg] )
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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+':subarray', 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.subarray(1, 2);
45+
if ( out instanceof Float64ArrayFE === false ) {
46+
b.fail( 'should return a TypedArray' );
47+
}
48+
}
49+
b.toc();
50+
if ( out instanceof Float64ArrayFE === false ) {
51+
b.fail( 'should return a TypedArray' );
52+
}
53+
b.pass( 'benchmark finished' );
54+
b.end();
55+
});
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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 pow = require( '@stdlib/math/base/special/pow' );
25+
var pkg = require( './../package.json' ).name;
26+
var factory = require( './../lib' );
27+
28+
29+
// FUNCTIONS //
30+
31+
/**
32+
* Creates a benchmark function.
33+
*
34+
* @private
35+
* @param {PositiveInteger} len - array length
36+
* @returns {Function} benchmark function
37+
*/
38+
function createBenchmark( len ) {
39+
var Float64Array;
40+
var arr;
41+
var i;
42+
Float64Array = factory( 'float64' );
43+
arr = [];
44+
for ( i = 0; i < len; i++ ) {
45+
arr.push( i );
46+
}
47+
arr = new Float64Array( 'little-endian', arr );
48+
49+
return benchmark;
50+
51+
/**
52+
* Benchmark function.
53+
*
54+
* @private
55+
* @param {Benchmark} b - benchmark instance
56+
*/
57+
function benchmark( b ) {
58+
var out;
59+
var i;
60+
61+
b.tic();
62+
for ( i = 0; i < b.iterations; i++ ) {
63+
out = arr.subarray();
64+
if ( typeof out !== 'object' ) {
65+
b.fail( 'should return an object' );
66+
}
67+
}
68+
b.toc();
69+
if ( out instanceof Float64Array === false ) {
70+
b.fail( 'should return a TypedArray' );
71+
}
72+
b.pass( 'benchmark finished' );
73+
b.end();
74+
}
75+
}
76+
77+
78+
// MAIN //
79+
80+
/**
81+
* Main execution sequence.
82+
*
83+
* @private
84+
*/
85+
function main() {
86+
var len;
87+
var min;
88+
var max;
89+
var f;
90+
var i;
91+
92+
min = 1; // 10^min
93+
max = 6; // 10^max
94+
95+
for ( i = min; i <= max; i++ ) {
96+
len = pow( 10, i );
97+
f = createBenchmark( len );
98+
bench( pkg+':subarray:len='+len, f );
99+
}
100+
}
101+
102+
main();

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

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -729,6 +729,101 @@ function factory( dtype ) { // eslint-disable-line max-lines-per-function, stdli
729729
buf[ SETTER ]( idx*BYTES_PER_ELEMENT, value, this._isLE );
730730
});
731731

732+
/**
733+
* Copies a portion of a typed array to a new typed array.
734+
* @name subarray
735+
* @memberof TypedArray.prototype
736+
* @type {Function}
737+
* @param {integer} [begin] - start index (inclusive)
738+
* @param {integer} [end] - end index (exclusive)
739+
* @throws {TypeError} `this` must be a typed array instance
740+
* @throws {RangeError} first argument must be in the range of `[0, this.length -1]` while second argument must be in the range of `[0, this.length]`
741+
* @throws {TypeError} first argument must be integer, and second argument must be integer
742+
* @returns {TypedArray} TypedArray array
743+
*
744+
* @example
745+
* var Float64Array = factory('float64');
746+
*
747+
* var arr = new Float64Array( 'little-endian' , [0.0, 1.0, 2.0, 3.0, 4.0] );
748+
*
749+
* var out = arr.subarray();
750+
* // returns <Float64Array>
751+
*
752+
* var len = out.length;
753+
* // returns 5
754+
*
755+
* var bool = out.get( 0 );
756+
* // returns 0.0
757+
*
758+
* bool = out.get( len-1 );
759+
* // returns 4.0
760+
*
761+
* out = arr.subarray( 1, -2 );
762+
* // returns <Float64Array>
763+
*
764+
* len = out.length;
765+
* // returns 2
766+
*
767+
* bool = out.get( 0 );
768+
* // returns 1.0
769+
*
770+
* bool = out.get( len-1 );
771+
* // returns 2.0
772+
*/
773+
setReadOnly( TypedArray.prototype, 'subarray', function subarray( begin, end) {
774+
var outbuf;
775+
var out;
776+
var buf;
777+
var len;
778+
var i;
779+
outbuf = [];
780+
if ( !isTypedArray( this ) ) {
781+
throw new TypeError( 'invalid invocation. `this` is not a typed array.' );
782+
}
783+
buf = this._buffer;
784+
len = this._length;
785+
if ( arguments.length === 0 ) {
786+
begin = 0;
787+
end = len;
788+
} else {
789+
if ( !isInteger( begin ) ) {
790+
throw new TypeError( format( 'invalid argument. First argument must be an integer. Value: `%s`.', begin ) );
791+
}
792+
if ( begin < 0 ) {
793+
begin += len;
794+
if ( begin < 0 ) {
795+
begin = 0;
796+
}
797+
}
798+
if ( arguments.length === 1 ) {
799+
end = len;
800+
} else {
801+
if ( !isInteger( end ) ) {
802+
throw new TypeError( format( 'invalid argument. Second argument must be an integer. Value: `%s`.', end ) );
803+
}
804+
if ( end < 0 ) {
805+
end += len;
806+
if ( end < 0 ) {
807+
end = 0;
808+
}
809+
} else if ( end > len ) {
810+
end = len;
811+
}
812+
}
813+
}
814+
815+
for ( i = begin; i < end; i++ ) {
816+
outbuf[ i - begin ] = buf[ GETTER ]( i * BYTES_PER_ELEMENT, this._isLE );
817+
}
818+
819+
if (this._isLE) {
820+
out = new this.constructor( 'little-endian', outbuf );
821+
} else {
822+
out = new this.constructor( 'big-endian', outbuf );
823+
}
824+
return out;
825+
});
826+
732827
/**
733828
* Tests whether at least one element in the typed array passes a test implemented by a predicate function.
734829
*
@@ -741,6 +836,7 @@ function factory( dtype ) { // eslint-disable-line max-lines-per-function, stdli
741836
* @throws {TypeError} first argument must be a function
742837
* @returns {boolean} boolean indicating whether at least one element passes a test
743838
*/
839+
744840
setReadOnly( TypedArray.prototype, 'some', function some( predicate, thisArg ) {
745841
var buf;
746842
var i;

0 commit comments

Comments
 (0)