Skip to content

Commit

Permalink
feat: add copyWithin, entries, forEach to array/bool
Browse files Browse the repository at this point in the history
PR-URL: #2611
Ref: #2304
Co-authored-by: Athan Reines <[email protected]>
Reviewed-by: Athan Reines <[email protected]> 
Signed-off-by: Athan Reines <[email protected]>
  • Loading branch information
Jaysukh-409 and kgryte authored Jul 19, 2024
1 parent e4cd553 commit cb20ecc
Show file tree
Hide file tree
Showing 12 changed files with 1,597 additions and 1 deletion.
175 changes: 175 additions & 0 deletions lib/node_modules/@stdlib/array/bool/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,121 @@ v = arr.at( -100 );
// returns undefined
```

<a name="method-copy-within"></a>

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

Copies a sequence of elements within the array starting at `start` and ending at `end` (non-inclusive) to the position starting at `target`.

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

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

var v = arr.get( 0 );
// returns true

v = arr.get( 1 );
// returns false

// Copy the last two elements to the first two elements:
arr.copyWithin( 0, 2 );

v = arr.get( 0 );
// returns false

v = arr.get( 1 );
// returns true
```

By default, `end` equals the number of array elements (i.e., one more than the last array index). To limit the sequence length, provide an `end` argument.

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

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

var v = arr.get( 2 );
// returns false

v = arr.get( 3 );
// returns true

// Copy the first two elements to the last two elements:
arr.copyWithin( 2, 0, 2 );

v = arr.get( 2 );
// returns true

v = arr.get( 3 );
// returns false
```

When a `target`, `start`, and/or `end` index is negative, the respective index is determined relative to the last array element. The following example achieves the same behavior as the previous example:

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

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

var v = arr.get( 2 );
// returns false

v = arr.get( 3 );
// returns true

// Copy the first two elements to the last two elements using negative indices:
arr.copyWithin( -2, -4, -2 );

v = arr.get( 2 );
// returns true

v = arr.get( 3 );
// returns false
```

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

#### BooleanArray.prototype.entries()

Returns an iterator for iterating over array key-value pairs.

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

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

var it = arr.entries();

var v = it.next().value;
// returns [ 0, true ]

v = it.next().value;
// returns [ 1, false ]

v = it.next().value;
// returns [ 2, true ]

var bool = it.next().done;
// returns true
```

The returned [iterator][mdn-iterator-protocol] protocol-compliant object has the following properties:

- **next**: function which returns an [iterator][mdn-iterator-protocol] protocol-compliant object containing the next iterated value (if one exists) assigned to a `value` property and a `done` property having a `boolean` value indicating whether the [iterator][mdn-iterator-protocol] is finished.
- **return**: function which closes an [iterator][mdn-iterator-protocol] and returns a single (optional) argument in an [iterator][mdn-iterator-protocol] protocol-compliant object.

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

#### BooleanArray.prototype.every( predicate\[, thisArg] )
Expand Down Expand Up @@ -746,6 +861,66 @@ var count = context.count;
// returns 3
```

<a name="method-for-each"></a>

#### BooleanArray.prototype.forEach( callbackFn\[, thisArg] )

Invokes a function once for each array element.

```javascript
function log( v, i ) {
console.log( '%s: %s', i, v.toString() );
}

var arr = new BooleanArray( 3 );

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

arr.forEach( log );
/* =>
0: true
1: false
2: true
*/
```

The invoked function is provided three arguments:

- **value**: current array element.
- **index**: current array element index.
- **arr**: the array on which this method was called.

To set the function execution context, provide a `thisArg`.

```javascript
function fcn( v, i ) {
this.count += 1;
console.log( '%s: %s', i, v.toString() );
}

var arr = new BooleanArray( 3 );

var context = {
'count': 0
};

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

arr.forEach( fcn, context );
/* =>
0: 1 + 1i
1: 2 + 2i
2: 3 + 3i
*/

var count = context.count;
// returns 3
```

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

#### BooleanArray.prototype.get( i )
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/**
* @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 pkg = require( './../package.json' ).name;
var BooleanArray = require( './../lib' );


// MAIN //

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

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

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
buf[ 0 ] = i%2;
arr = arr.copyWithin( 1, 0 );
if ( buf[ 0 ] !== i%2 ) {
b.fail( 'unexpected result' );
}
}
b.toc();
if ( buf[ 0 ] !== buf[ 0 ] ) {
b.fail( 'should not be NaN' );
}
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) 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 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 buf;
var i;

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

return benchmark;

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

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
buf[ 0 ] = i%2;
arr.copyWithin( 1, 0 );
if ( buf[ 0 ] !== i%2 ) {
b.fail( 'unexpected result' );
}
}
b.toc();
if ( buf[ 0 ] !== buf[ 0 ] ) {
b.fail( 'should not be NaN' );
}
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+':copyWithin:len='+len, f );
}
}

main();
Loading

1 comment on commit cb20ecc

@stdlib-bot
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage Report

Package Statements Branches Functions Lines
array/bool $\color{red}2730/2747$
$\color{green}+99.38\%$
$\color{red}454/463$
$\color{green}+98.06\%$
$\color{green}55/55$
$\color{green}+100.00\%$
$\color{red}2730/2747$
$\color{green}+99.38\%$

The above coverage report was generated for the changes in this push.

Please sign in to comment.