Skip to content

feat: add slice and subarray methods to array/bool #2472

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
152 changes: 152 additions & 0 deletions lib/node_modules/@stdlib/array/bool/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -865,6 +865,82 @@ A few notes:
- 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.
- If provided a [typed array][@stdlib/array/typed] which shares an [`ArrayBuffer`][@stdlib/array/buffer] with the target array, the method will intelligently copy the source range to the destination range.

<a name="method-slice"></a>

#### BooleanArray.prototype.slice( \[start\[, end]] )

Copies a portion of a typed array to a new typed array.

```javascript
var arr = new BooleanArray( 5 );

arr.set( true, 0 );
arr.set( false, 1 );
arr.set( true, 2 );
arr.set( false, 3 );
arr.set( true, 4 );

var out = arr.slice();
// returns <BooleanArray>

var len = out.length;
// returns 5

var bool = out.get( 0 );
// returns true

bool = out.get( len-1 );
// returns true
```

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).

```javascript
var arr = new BooleanArray( 5 );

arr.set( true, 0 );
arr.set( false, 1 );
arr.set( true, 2 );
arr.set( false, 3 );
arr.set( true, 4 );

var out = arr.slice( 1 );
// returns <BooleanArray>

var len = out.length;
// returns 4

var bool = out.get( 0 );
// returns false

bool = out.get( len-1 );
// returns true
```

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).

```javascript
var arr = new BooleanArray( 5 );

arr.set( true, 0 );
arr.set( false, 1 );
arr.set( true, 2 );
arr.set( false, 3 );
arr.set( true, 4 );

var out = arr.slice( 1, -2 );
// returns <BooleanArray>

var len = out.length;
// returns 2

var bool = out.get( 0 );
// returns false

bool = out.get( len-1 );
// returns true
```

<a name="method-some"></a>

#### BooleanArray.prototype.some( predicate\[, thisArg] )
Expand Down Expand Up @@ -968,6 +1044,82 @@ The function should return a number where:

<a name="method-to-reversed"></a>

<a name="method-subarray"></a>

#### BooleanArray.prototype.subarray( \[begin\[, end]] )

Creates a new typed array view over the same underlying [`ArrayBuffer`][@stdlib/array/buffer] and with the same underlying data type as the host array.

```javascript
var arr = new BooleanArray( 5 );

arr.set( true, 0 );
arr.set( false, 1 );
arr.set( true, 2 );
arr.set( false, 3 );
arr.set( true, 4 );

var subarr = arr.subarray();
// returns <BooleanArray>

var len = subarr.length;
// returns 5

var bool = subarr.get( 0 );
// returns true

bool = subarr.get( len-1 );
// returns true
```

By default, the method creates a typed array view beginning with the first array element. To specify an alternative array index at which to begin, provide a `begin` index (inclusive).

```javascript
var arr = new BooleanArray( 5 );

arr.set( true, 0 );
arr.set( false, 1 );
arr.set( true, 2 );
arr.set( false, 3 );
arr.set( true, 4 );

var subarr = arr.subarray( 1 );
// returns <BooleanArray>

var len = subarr.length;
// returns 4

var bool = subarr.get( 0 );
// returns false

bool = subarr.get( len-1 );
// returns true
```

By default, the method creates a typed array view which includes all array elements after `begin`. To limit the number of array elements after `begin`, provide an `end` index (exclusive).

```javascript
var arr = new BooleanArray( 5 );

arr.set( true, 0 );
arr.set( false, 1 );
arr.set( true, 2 );
arr.set( false, 3 );
arr.set( true, 4 );

var subarr = arr.subarray( 1, -2 );
// returns <BooleanArray>

var len = subarr.length;
// returns 2

var bool = subarr.get( 0 );
// returns false

bool = subarr.get( len-1 );
// returns true
```

#### BooleanArray.prototype.toReversed()

Returns a new typed array containing the elements in reversed order.
Expand Down
51 changes: 51 additions & 0 deletions lib/node_modules/@stdlib/array/bool/benchmark/benchmark.slice.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2024 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

// MODULES //

var bench = require( '@stdlib/bench' );
var isBooleanArray = require( '@stdlib/assert/is-booleanarray' );
var pkg = require( './../package.json' ).name;
var BooleanArray = require( './../lib' );


// MAIN //

bench( pkg+':slice', function benchmark( b ) {
var out;
var arr;
var i;

arr = new BooleanArray( [ true, false, false, true ] );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
out = arr.slice();
if ( typeof out !== 'object' ) {
b.fail( 'should return an object' );
}
}
b.toc();
if ( !isBooleanArray( out ) ) {
b.fail( 'should return a BooleanArray' );
}
b.pass( 'benchmark finished' );
b.end();
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2023 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

// MODULES //

var bench = require( '@stdlib/bench' );
var isBooleanArray = require( '@stdlib/assert/is-booleanarray' );
var Boolean = require( '@stdlib/boolean/ctor' );
var pow = require( '@stdlib/math/base/special/pow' );
var pkg = require( './../package.json' ).name;
var BooleanArray = require( './../lib' );


// FUNCTIONS //

/**
* Creates a benchmark function.
*
* @private
* @param {PositiveInteger} len - array length
* @returns {Function} benchmark function
*/
function createBenchmark( len ) {
var arr;
var i;

arr = [];
for ( i = 0; i < len; i++ ) {
arr.push( Boolean( i%2 ) );
}
arr = new BooleanArray( arr );

return benchmark;

/**
* Benchmark function.
*
* @private
* @param {Benchmark} b - benchmark instance
*/
function benchmark( b ) {
var out;
var i;

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
out = arr.slice();
if ( typeof out !== 'object' ) {
b.fail( 'should return an object' );
}
}
b.toc();
if ( !isBooleanArray( out ) ) {
b.fail( 'should return a BooleanArray' );
}
b.pass( 'benchmark finished' );
b.end();
}
}


// MAIN //

/**
* Main execution sequence.
*
* @private
*/
function main() {
var len;
var min;
var max;
var f;
var i;

min = 1; // 10^min
max = 6; // 10^max

for ( i = min; i <= max; i++ ) {
len = pow( 10, i );
f = createBenchmark( len );
bench( pkg+':slice:len='+len, f );
}
}

main();
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2024 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

// MODULES //

var bench = require( '@stdlib/bench' );
var isBooleanArray = require( '@stdlib/assert/is-booleanarray' );
var pkg = require( './../package.json' ).name;
var BooleanArray = require( './../lib' );


// MAIN //

bench( pkg+':subarray', function benchmark( b ) {
var out;
var arr;
var i;

arr = new BooleanArray( [ true, false, false, true ] );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
out = arr.subarray();
if ( typeof out !== 'object' ) {
b.fail( 'should return an object' );
}
}
b.toc();
if ( !isBooleanArray( out ) ) {
b.fail( 'should return a BooleanArray' );
}
b.pass( 'benchmark finished' );
b.end();
});
Loading
Loading