Skip to content

Commit bcc4673

Browse files
committed
Auto-generated commit
1 parent 3e4d535 commit bcc4673

File tree

8 files changed

+579
-2
lines changed

8 files changed

+579
-2
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -1212,6 +1212,7 @@ This release closes the following issue:
12121212

12131213
##### Features
12141214

1215+
- [`0b3db04`](https://github.com/stdlib-js/stdlib/commit/0b3db0401bd16df7aeccb500d8280c280a394762) - add `toSorted` method to `array/bool` [(#2413)](https://github.com/stdlib-js/stdlib/pull/2413)
12151216
- [`de50d0a`](https://github.com/stdlib-js/stdlib/commit/de50d0af5f4b9a466a87be81da737fdbed48dbf3) - add `reverse` and `toReversed` methods to `array/bool` [(#2390)](https://github.com/stdlib-js/stdlib/pull/2390)
12161217
- [`5cd4a70`](https://github.com/stdlib-js/stdlib/commit/5cd4a70beaa7663d2a822b0922b3fb3cc6ec539f) - add `findIndex` and `findLastIndex` methods to `array/bool` [(#2384)](https://github.com/stdlib-js/stdlib/pull/2384)
12171218
- [`cba0d92`](https://github.com/stdlib-js/stdlib/commit/cba0d9249a25664a0b52f3ea2fe65eeddedd1e59) - add `find` and `findLast` methods to `array/bool` [(#2376)](https://github.com/stdlib-js/stdlib/pull/2376)
@@ -2114,6 +2115,7 @@ A total of 13 people contributed to this release. Thank you to the following con
21142115

21152116
<details>
21162117

2118+
- [`0b3db04`](https://github.com/stdlib-js/stdlib/commit/0b3db0401bd16df7aeccb500d8280c280a394762) - **feat:** add `toSorted` method to `array/bool` [(#2413)](https://github.com/stdlib-js/stdlib/pull/2413) _(by Jaysukh Makvana)_
21172119
- [`e57ccb2`](https://github.com/stdlib-js/stdlib/commit/e57ccb234687de4087aa12348d266ea448f3f241) - **refactor:** update boolean array indexing implementation and add tests _(by Athan Reines)_
21182120
- [`d903fc9`](https://github.com/stdlib-js/stdlib/commit/d903fc907ddaacc144a97edaaed0f05abadc470c) - **fix:** require broadcast compatibility _(by Athan Reines)_
21192121
- [`2769ce9`](https://github.com/stdlib-js/stdlib/commit/2769ce9e876c2a6de5735ffdbc4c79b3d26347b6) - **fix:** require broadcast compatibility _(by Athan Reines)_

bool/README.md

+51-1
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ var count = context.count;
440440

441441
<a name="method-find-last"></a>
442442

443-
#### Complex64Array.prototype.findLast( predicate\[, thisArg] )
443+
#### BooleanArray.prototype.findLast( predicate\[, thisArg] )
444444

445445
Returns the last element in an array for which a predicate function returns a truthy value.
446446

@@ -790,6 +790,56 @@ v = out.get( 2 );
790790
// returns true
791791
```
792792

793+
<a name="method-to-sorted"></a>
794+
795+
#### BooleanArray.prototype.toSorted( \[compareFcn] )
796+
797+
Returns a new typed array containing the elements in sorted order.
798+
799+
```javascript
800+
function compare( a, b ) {
801+
if ( a === false ) {
802+
if ( b === false ) {
803+
return 0;
804+
}
805+
return 1;
806+
}
807+
if ( b === true ) {
808+
return 0;
809+
}
810+
return -1;
811+
}
812+
813+
var arr = new BooleanArray( 3 );
814+
815+
arr.set( true, 0 );
816+
arr.set( false, 1 );
817+
arr.set( true, 2 );
818+
819+
var out = arr.sort( compare );
820+
// returns <BooleanArray>
821+
822+
var v = out.get( 0 );
823+
// returns true
824+
825+
v = out.get( 1 );
826+
// returns true
827+
828+
v = out.get( 2 );
829+
// returns false
830+
```
831+
832+
The `compareFcn` determines the order of the elements. The function is called with the following arguments:
833+
834+
- **a**: the first boolean value for comparison.
835+
- **b**: the second boolean value for comparison.
836+
837+
The function should return a number where:
838+
839+
- a negative value indicates that `a` should come before `b`.
840+
- a positive value indicates that `a` should come after `b`.
841+
- zero or `NaN` indicates that `a` and `b` are considered equal.
842+
793843
</section>
794844

795845
<!-- /.usage -->

bool/benchmark/benchmark.to_sorted.js

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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+
// FUNCTIONS //
30+
31+
/**
32+
* Comparison function.
33+
*
34+
* @private
35+
* @param {boolean} a - first boolean value for comparison
36+
* @param {boolean} b - second boolean value for comparison
37+
* @returns {number} comparison result
38+
*/
39+
function compareFcn( a, b ) {
40+
if ( a === true ) {
41+
if ( b === true ) {
42+
return 0;
43+
}
44+
return 1;
45+
}
46+
if ( b === false ) {
47+
return 0;
48+
}
49+
return -1;
50+
}
51+
52+
53+
// MAIN //
54+
55+
bench( pkg+':toSorted', function benchmark( b ) {
56+
var out;
57+
var arr;
58+
var i;
59+
60+
arr = new BooleanArray( [ true, false, false, true, true, false ] );
61+
62+
b.tic();
63+
for ( i = 0; i < b.iterations; i++ ) {
64+
out = arr.toSorted( compareFcn );
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+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
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+
* Comparison function.
35+
*
36+
* @private
37+
* @param {boolean} a - first boolean value for comparison
38+
* @param {boolean} b - second boolean value for comparison
39+
* @returns {number} comparison result
40+
*/
41+
function compareFcn( a, b ) {
42+
if ( a === true ) {
43+
if ( b === true ) {
44+
return 0;
45+
}
46+
return 1;
47+
}
48+
if ( b === false ) {
49+
return 0;
50+
}
51+
return -1;
52+
}
53+
54+
/**
55+
* Creates a benchmark function.
56+
*
57+
* @private
58+
* @param {PositiveInteger} len - array length
59+
* @returns {Function} benchmark function
60+
*/
61+
function createBenchmark( len ) {
62+
var arr;
63+
var i;
64+
65+
arr = [];
66+
for ( i = 0; i < len; i++ ) {
67+
arr.push( Boolean( i%2 ) );
68+
}
69+
arr = new BooleanArray( arr );
70+
71+
return benchmark;
72+
73+
/**
74+
* Benchmark function.
75+
*
76+
* @private
77+
* @param {Benchmark} b - benchmark instance
78+
*/
79+
function benchmark( b ) {
80+
var out;
81+
var i;
82+
83+
b.tic();
84+
for ( i = 0; i < b.iterations; i++ ) {
85+
out = arr.toSorted( compareFcn );
86+
if ( typeof out !== 'object' ) {
87+
b.fail( 'should return an object' );
88+
}
89+
}
90+
b.toc();
91+
if ( !isBooleanArray( out ) ) {
92+
b.fail( 'should return a BooleanArray' );
93+
}
94+
b.pass( 'benchmark finished' );
95+
b.end();
96+
}
97+
}
98+
99+
100+
// MAIN //
101+
102+
/**
103+
* Main execution sequence.
104+
*
105+
* @private
106+
*/
107+
function main() {
108+
var len;
109+
var min;
110+
var max;
111+
var f;
112+
var i;
113+
114+
min = 1; // 10^min
115+
max = 6; // 10^max
116+
117+
for ( i = min; i <= max; i++ ) {
118+
len = pow( 10, i );
119+
f = createBenchmark( len );
120+
bench( pkg+':toSorted:len='+len, f );
121+
}
122+
}
123+
124+
main();

bool/docs/repl.txt

+37
Original file line numberDiff line numberDiff line change
@@ -603,5 +603,42 @@
603603
true
604604

605605

606+
{{alias}}.prototype.toSorted( [compareFunction] )
607+
Returns a new typed array containing the elements in sorted order.
608+
609+
A comparison function determines the order of the array elements. The
610+
function is provided two arguments:
611+
612+
- a: first boolean value for comparison.
613+
- b: second boolean value for comparison.
614+
615+
The function should return a value less than zero if `a` comes before `b`,
616+
a value greater than zero if `a` comes after `b`, and zero if `a` and `b`
617+
are equivalent.
618+
619+
Parameters
620+
----------
621+
compareFunction: Function (optional)
622+
Comparison function.
623+
624+
Returns
625+
-------
626+
out: BooleanArray
627+
New typed array.
628+
629+
Examples
630+
--------
631+
> function compare( a, b ) { return a === true ? -1 : 1; }
632+
> var arr = new {{alias}}( [ true, false, true ] )
633+
<BooleanArray>
634+
> var out = arr.toSorted( compare );
635+
> var v = out.get( 0 )
636+
true
637+
> v = out.get( 1 )
638+
true
639+
> v = out.get( 2 )
640+
false
641+
642+
606643
See Also
607644
--------

bool/docs/types/index.d.ts

+40
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,46 @@ declare class BooleanArray implements BooleanArrayInterface {
548548
* // returns true
549549
*/
550550
toReversed(): BooleanArray;
551+
552+
/**
553+
* Returns a new typed array containing the elements in sorted order.
554+
*
555+
* @param compareFcn - comparison function
556+
* @returns sorted array
557+
*
558+
* @example
559+
* function compare( a, b ) {
560+
* if ( a === false ) {
561+
* if ( b === false ) {
562+
* return 0;
563+
* }
564+
* return 1;
565+
* }
566+
* if ( b === true ) {
567+
* return 0;
568+
* }
569+
* return -1;
570+
* }
571+
*
572+
* var arr = new BooleanArray( 3 );
573+
*
574+
* arr.set( true, 0 );
575+
* arr.set( false, 1 );
576+
* arr.set( true, 2 );
577+
*
578+
* var out = arr.sort( compare );
579+
* // returns <BooleanArray>
580+
*
581+
* var v = out.get( 0 );
582+
* // returns true
583+
*
584+
* v = out.get( 1 );
585+
* // returns true
586+
*
587+
* v = out.get( 2 );
588+
* // returns false
589+
*/
590+
toSorted( compareFcn: CompareFcn ): BooleanArray;
551591
}
552592

553593
/**

0 commit comments

Comments
 (0)