Skip to content

Commit de50d0a

Browse files
Jaysukh-409kgryte
andauthored
feat: add reverse and toReversed methods to array/bool
PR-URL: stdlib-js#2390 Ref: stdlib-js#2304 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com>
1 parent 95c263b commit de50d0a

File tree

10 files changed

+784
-0
lines changed

10 files changed

+784
-0
lines changed

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

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -629,6 +629,32 @@ var count = context.count;
629629
// returns 3;
630630
```
631631

632+
<a name="method-reverse"></a>
633+
634+
#### BooleanArray.prototype.reverse()
635+
636+
Reverses an array in-place.
637+
638+
```javascript
639+
var arr = new BooleanArray( 3 );
640+
641+
arr.set( true, 0 );
642+
arr.set( false, 1 );
643+
arr.set( false, 2 );
644+
645+
var out = arr.reverse();
646+
// returns <BooleanArray>
647+
648+
var v = out.get( 0 );
649+
// returns false
650+
651+
v = out.get( 1 );
652+
// returns false
653+
654+
v = out.get( 2 );
655+
// returns true
656+
```
657+
632658
<a name="method-set"></a>
633659

634660
#### BooleanArray.prototype.set( v\[, i] )
@@ -738,6 +764,32 @@ The function should return a number where:
738764
- a positive value indicates that `a` should come after `b`.
739765
- zero or `NaN` indicates that `a` and `b` are considered equal.
740766

767+
<a name="method-to-reversed"></a>
768+
769+
#### BooleanArray.prototype.toReversed()
770+
771+
Returns a new typed array containing the elements in reversed order.
772+
773+
```javascript
774+
var arr = new BooleanArray( 3 );
775+
776+
arr.set( true, 0 );
777+
arr.set( false, 1 );
778+
arr.set( false, 2 );
779+
780+
var out = arr.toReversed();
781+
// returns <BooleanArray>
782+
783+
var v = out.get( 0 );
784+
// returns false
785+
786+
v = out.get( 1 );
787+
// returns false
788+
789+
v = out.get( 2 );
790+
// returns true
791+
```
792+
741793
</section>
742794

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

0 commit comments

Comments
 (0)