Skip to content

Commit c58e9e4

Browse files
Jaysukh-409kgryte
andauthored
feat: add indexOf and lastIndexOf methods to array/bool
PR-URL: #2432 Ref: #2304 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com> Signed-off-by: Athan Reines <kgryte@gmail.com>
1 parent 61c5609 commit c58e9e4

File tree

10 files changed

+1073
-10
lines changed

10 files changed

+1073
-10
lines changed

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

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -620,6 +620,82 @@ var v = arr.get( 100 );
620620
// returns undefined
621621
```
622622

623+
<a name="method-index-of"></a>
624+
625+
#### BooleanArray.prototype.indexOf( searchElement\[, fromIndex] )
626+
627+
Returns the first index at which a given element can be found.
628+
629+
```javascript
630+
var arr = new BooleanArray( 5 );
631+
632+
arr.set( true, 0 );
633+
arr.set( false, 1 );
634+
arr.set( true, 2 );
635+
arr.set( true, 3 );
636+
arr.set( true, 4 );
637+
638+
var idx = arr.indexOf( true );
639+
// returns 0
640+
641+
idx = arr.indexOf( false, 1 );
642+
// returns 1
643+
644+
idx = arr.indexOf( true, -3 );
645+
// returns 2
646+
```
647+
648+
If `searchElement` is not present in the array, the method returns `-1`.
649+
650+
```javascript
651+
var arr = new BooleanArray( 3 );
652+
653+
arr.set( true, 0 );
654+
arr.set( true, 1 );
655+
arr.set( true, 2 );
656+
657+
var idx = arr.indexOf( false );
658+
// returns -1
659+
```
660+
661+
<a name="method-last-index-of"></a>
662+
663+
#### BooleanArray.prototype.lastIndexOf( searchElement\[, fromIndex] )
664+
665+
Returns the last index at which a given element can be found.
666+
667+
```javascript
668+
var arr = new BooleanArray( 5 );
669+
670+
arr.set( true, 0 );
671+
arr.set( true, 1 );
672+
arr.set( true, 2 );
673+
arr.set( false, 3 );
674+
arr.set( true, 4 );
675+
676+
var idx = arr.lastIndexOf( true );
677+
// returns 4
678+
679+
idx = arr.lastIndexOf( false, 3 );
680+
// returns 3
681+
682+
idx = arr.lastIndexOf( true, -3 );
683+
// returns 2
684+
```
685+
686+
If `searchElement` is not present in the array, the method returns `-1`.
687+
688+
```javascript
689+
var arr = new BooleanArray( 3 );
690+
691+
arr.set( true, 0 );
692+
arr.set( true, 1 );
693+
arr.set( true, 2 );
694+
695+
var idx = arr.lastIndexOf( false );
696+
// returns -1
697+
```
698+
623699
<a name="method-map"></a>
624700

625701
#### BooleanArray.prototype.map( callbackFn\[, 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 isInteger = require('@stdlib/assert/is-integer' ).isPrimitive;
25+
var pkg = require( './../package.json' ).name;
26+
var BooleanArray = require( './../lib' );
27+
28+
29+
// MAIN //
30+
31+
bench( pkg+':indexOf', function benchmark( b ) {
32+
var arr;
33+
var idx;
34+
var i;
35+
36+
arr = [];
37+
for ( i = 0; i < 10; i++ ) {
38+
arr.push( true );
39+
}
40+
arr = new BooleanArray( arr );
41+
42+
b.tic();
43+
for ( i = 0; i < b.iterations; i++ ) {
44+
idx = arr.indexOf( false, 0 );
45+
if ( typeof idx !== 'number' ) {
46+
b.fail( 'should return an integer' );
47+
}
48+
}
49+
b.toc();
50+
if ( !isInteger( idx ) ) {
51+
b.fail( 'should return an integer' );
52+
}
53+
b.pass( 'benchmark finished' );
54+
b.end();
55+
});
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 pow = require( '@stdlib/math/base/special/pow' );
25+
var isInteger = require('@stdlib/assert/is-integer' ).isPrimitive;
26+
var pkg = require( './../package.json' ).name;
27+
var BooleanArray = require( './../lib' );
28+
29+
30+
// FUNCTIONS //
31+
32+
/**
33+
* Creates a benchmark function.
34+
*
35+
* @private
36+
* @param {PositiveInteger} len - array length
37+
* @returns {Function} benchmark function
38+
*/
39+
function createBenchmark( len ) {
40+
var arr;
41+
var i;
42+
43+
arr = [];
44+
for ( i = 0; i < len-1; i++ ) {
45+
arr.push( false );
46+
}
47+
arr.push( true );
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 idx;
60+
var i;
61+
62+
b.tic();
63+
for ( i = 0; i < b.iterations; i++ ) {
64+
idx = arr.indexOf( true );
65+
if ( typeof idx !== 'number' ) {
66+
b.fail( 'should return an integer' );
67+
}
68+
}
69+
b.toc();
70+
if ( !isInteger( idx ) ) {
71+
b.fail( 'should return an integer' );
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+':indexOf:len='+len, f );
100+
}
101+
}
102+
103+
main();
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 isInteger = require('@stdlib/assert/is-integer' ).isPrimitive;
25+
var pkg = require( './../package.json' ).name;
26+
var BooleanArray = require( './../lib' );
27+
28+
29+
// MAIN //
30+
31+
bench( pkg+':lastIndexOf', function benchmark( b ) {
32+
var arr;
33+
var idx;
34+
var i;
35+
36+
arr = [];
37+
for ( i = 0; i < 10; i++ ) {
38+
arr.push( false );
39+
}
40+
arr = new BooleanArray( arr );
41+
42+
b.tic();
43+
for ( i = 0; i < b.iterations; i++ ) {
44+
idx = arr.lastIndexOf( true, 0 );
45+
if ( typeof idx !== 'number' ) {
46+
b.fail( 'should return an integer' );
47+
}
48+
}
49+
b.toc();
50+
if ( !isInteger( idx ) ) {
51+
b.fail( 'should return an integer' );
52+
}
53+
b.pass( 'benchmark finished' );
54+
b.end();
55+
});

0 commit comments

Comments
 (0)