8000 GitHub - stdlib-js/array-bool at v0.0.1-deno
[go: up one dir, main page]

Skip to content

stdlib-js/array-bool

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

54 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

About stdlib...

We believe in a future in which the web is a preferred environment for numerical computation. To help realize this future, we've built stdlib. stdlib is a standard library, with an emphasis on numerical and scientific computation, written in JavaScript (and C) for execution in browsers and in Node.js.

The library is fully decomposable, being architected in such a way that you can swap out and mix and match APIs and functionality to cater to your exact preferences and use cases.

When you use stdlib, you can be absolutely certain that you are using the most thorough, rigorous, well-written, studied, documented, tested, measured, and high-quality code out there.

To join us in bringing numerical computing to the web, get started by checking us out on GitHub, and please consider financially supporting stdlib. We greatly appreciate your continued support!

BooleanArray

NPM version Build Status Coverage Status

Boolean array.

Usage

import BooleanArray from 'https://cdn.jsdelivr.net/gh/stdlib-js/array-bool@v0.0.1-deno/mod.js';

BooleanArray()

Creates a boolean array.

var arr = new BooleanArray();
// returns <BooleanArray>

BooleanArray( length )

Creates a boolean array having a specified length.

var arr = new BooleanArray( 10 );
// returns <BooleanArray>

var len = arr.length;
// returns 10

BooleanArray( booleanarray )

Creates a boolean array from another boolean array.

var arr1 = new BooleanArray( [ true, false, false, true ] );
// returns <BooleanArray>

var arr2 = new BooleanArray( arr1 );
// returns <BooleanArray>

var len = arr2.length;
// returns 4

BooleanArray( typedarray )

Creates a boolean array from a typed array.

import Uint8Array from 'https://cdn.jsdelivr.net/gh/stdlib-js/array-uint8@deno/mod.js';

var buf = new Uint8Array( [ 1, 0, 0, 1 ] );
// returns <Uint8Array>[ 1, 0, 0, 1 ]

var arr = new BooleanArray( buf );
// returns <BooleanArray>

var len = arr.length;
// returns 4

BooleanArray( obj )

Creates a boolean array from an array-like object or iterable.

// From an array of booleans:
var arr1 = new BooleanArray( [ true, false, false, true ] );
// returns <BooleanArray>

var len = arr1.length;
// returns 4

// From an array containing non-booleans:
var arr2 = new BooleanArray( [ {}, null, '', 4 ] );

len = arr2.length;
// returns 4

BooleanArray( buffer[, byteOffset[, length]] )

Returns a boolean array view of an ArrayBuffer.

import ArrayBuffer from 'https://cdn.jsdelivr.net/gh/stdlib-js/array-buffer@deno/mod.js';
var buf = new ArrayBuffer( 240 );

var arr1 = new BooleanArray( buf );
// returns <BooleanArray>

var len = arr1.length;
// returns 240

var arr2 = new BooleanArray( buf, 8 );
// returns <BooleanArray>

len = arr2.length;
// returns 232

var arr3 = new BooleanArray( buf, 8, 20 );
// returns <BooleanArray>

len = arr3.length;
// returns 20

Properties

BooleanArray.BYTES_PER_ELEMENT

Static property returning the size (in bytes) of each array element.

var nbytes = BooleanArray.BYTES_PER_ELEMENT;
// returns 1

BooleanArray.name

Static property returning the constructor name.

var str = BooleanArray.name;
// returns 'BooleanArray'

BooleanArray.prototype.buffer

Pointer to the underlying data buffer.

var arr = new BooleanArray( 2 );
// returns <BooleanArray>

var buf = arr.buffer;
// returns <ArrayBuffer>

BooleanArray.prototype.byteLength

Size (in bytes) of the array.

var arr = new BooleanArray( 10 );
// returns <BooleanArray>

var nbytes = arr.byteLength;
// returns 10

BooleanArray.prototype.byteOffset

Offset (in bytes) of the array from the start of its underlying ArrayBuffer.

import ArrayBuffer from 'https://cdn.jsdelivr.net/gh/stdlib-js/array-buffer@deno/mod.js';

var arr = new BooleanArray( 10 );
// returns <BooleanArray>

var offset = arr.byteOffset;
// returns 0

var buf = new ArrayBuffer( 240 );
arr = new BooleanArray( buf, 64 );
// returns <BooleanArray>

offset = arr.byteOffset;
// returns 64

BooleanArray.prototype.BYTES_PER_ELEMENT

Size (in bytes) of each array element.

var arr = new BooleanArray( 10 );
// returns <BooleanArray>

var nbytes = arr.BYTES_PER_ELEMENT;
// returns 1

BooleanArray.prototype.length

Number of array elements.

var arr = new BooleanArray( 10 );
// returns <BooleanArray>

var len = arr.length;
// returns 10

Methods

BooleanArray.from( src[, clbk[, thisArg]] )

Creates a new boolean array from an array-like object or an iterable.

var arr = BooleanArray.from( [ true, false ] );
// returns <BooleanArray>

var len = arr.length;
// returns 2

To invoke a function for each src value, provide a callback function.

function map( v ) {
    return !v;
}

// Create a source array:
var src = [ true, false ];

// Create a new boolean array by inverting the source array:
var arr = BooleanArray.from( src, map );
// returns <BooleanArray>

var len = arr.length;
// returns 2

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

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

A callback function is provided two arguments:

  • value: source value.
  • index: source index.

To set the callback execution context, provide a thisArg.

function map( v ) {
    this.count += 1;
    return !v;
}

// Create a source array:
var src = [ true, false ];

// Define an execution context:
var ctx = {
    'count': 0
};

// Create a new boolean array by inverting the source array:
var arr = BooleanArray.from( src, map, ctx );
// returns <BooleanArray>

var len = arr.length;
// returns 2

var n = ctx.count;
// returns 2

BooleanArray.of( element0[, element1[, ...elementN]] )

Creates a new boolean array from a variable number of arguments.

var arr = BooleanArray.of( true, false, false, true );
// returns <BooleanArray>

var len = arr.length;
// returns 4

BooleanArray.prototype.find( predicate[, thisArg] )

Returns the first element in an array for which a predicate function returns a truthy value.

function predicate( v ) {
    return v === true;
}

var arr = new BooleanArray( 3 );

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

var v = arr.find( predicate );
// returns true

The predicate 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.

function predicate( v ) {
    this.count += 1;
    return ( v === true );
}

var arr = new BooleanArray( 3 );

var context = {
    'count': 0
};

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

var z = arr.find( predicate, context );
// returns true

var count = context.count;
// returns 3

BooleanArray.prototype.findIndex( predicate[, thisArg] )

Returns the index of the first element in an array for which a predicate function returns a truthy value.

function predicate( v ) {
    return v === true;
}

var arr = new BooleanArray( 3 );

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

var v = arr.findIndex( predicate );
// returns 0

The predicate 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.

function predicate( v ) {
    this.count += 1;
    return ( v === true );
}

var arr = new BooleanArray( 3 );

var context = {
    'count': 0
};

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

var z = arr.findIndex( predicate, context );
// returns 2

var count = context.count;
// returns 3

Complex64Array.prototype.findLast( predicate[, thisArg] )

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

function predicate( v ) {
    return v === true;
}

var arr = new BooleanArray( 3 );

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

var v = arr.findLast( predicate );
// returns true

The predicate 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.

function predicate( v ) {
    this.count += 1;
    return ( v === true );
}

var arr = new BooleanArray( 3 );

var context = {
    'count': 0
};

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

var z = arr.findLast( predicate, context );
// returns true

var count = context.count;
// returns 3

BooleanArray.prototype.findLastIndex( predicate[, thisArg] )

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

function predicate( v ) {
    return v === true;
}

var arr = new BooleanArray( 3 );

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

var v = arr.findLastIndex( predicate );
// returns 2

The predicate 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.

function predicate( v ) {
    this.count += 1;
    return ( v === true );
}

var arr = new BooleanArray( 3 );

var context = {
    'count': 0
};

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

var z = arr.findLastIndex( predicate, context );
// returns 0

var count = context.count;
// returns 3

BooleanArray.prototype.get( i )

Returns an array element located at a nonnegative integer position (index) i.

var arr = new BooleanArray( 10 );

// Set the first element:
arr.set( true, 0 );

// Get the first element:
var v = arr.get( 0 );
// returns true

If provided an out-of-bounds index, the method returns undefined.

var arr = new BooleanArray( 10 );

var v = arr.get( 100 );
// returns undefined

BooleanArray.prototype.map( callbackFn[, thisArg] )

Returns a new array with each element being the result of a provided callback function.

function invert( v ) {
    return !v;
}

var arr = new BooleanArray( 3 );

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

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

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

z = out.get( 1 );
// returns true

z = out.get( 2 );
// returns false

The callback 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.

function invert( v, i ) {
    this.count += i;
    return !v;
}

var arr = new BooleanArray( 3 );

var context = {
    'count': 0
};

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

var out = arr.map( invert, context );
// returns <BooleanArray>

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

BooleanArray.prototype.reverse()

Reverses an array in-place.

var arr = new BooleanArray( 3 );

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

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

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

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

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

BooleanArray.prototype.set( v[, i] )

Sets one or more array elements.

var arr = new BooleanArray( 10 );

// Get the first element:
var v = arr.get( 0 );
// returns false

// Set the first element:
arr.set( true );

// Get the first element:
v = arr.get( 0 );
// returns true

By default, the method sets array elements starting at position (index) i = 0. To set elements starting elsewhere in the array, provide an index argument i.

var arr = new BooleanArray( 10 );

// Get the fifth element:
var v = arr.get( 4 );
// returns false

// Set the fifth element:
arr.set( true, 4 );

// Get the fifth element:
v = arr.get( 4 );
// returns true

In addition to providing a single value, to set one or more array elements, provide an array-like object containing truthy and falsy values

var arr = new BooleanArray( 10 );

// Define an array of values:
var buf = [ '', 1, null ];

// Set the fifth, sixth, and seventh elements:
arr.set( buf, 4 );

// Get the sixth element:
var v = arr.get( 5 );
// returns true

A few notes:

  • If i is out-of-bounds, the method throws an error.
  • 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 which shares an ArrayBuffer with the target array, the method will intelligently copy the source range to the destination range.

BooleanArray.prototype.sort( [compareFcn] )

Sorts an array in-place.

function compare( a, b ) {
    if ( a === false ) {
        if ( b === false ) {
            return 0;
        }
        return 1;
    }
    if ( b === true ) {
        return 0;
    }
    return -1;
}

var arr = new BooleanArray( 3 );

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

arr.sort( compare );

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

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

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

The compareFcn determines the order of the elements. The function is called with the following arguments:

  • a: the first boolean value for comparison.
  • b: the second boolean value for comparison.

The function should return a number where:

  • a negative value indicates that a should come before b.
  • a positive value indicates that a should come after b.
  • zero or NaN indicates that a and b are considered equal.

BooleanArray.prototype.toReversed()

Returns a new typed array containing the elements in reversed order.

var arr = new BooleanArray( 3 );

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

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

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

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

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

Notes

  • While a BooleanArray strives to maintain (but does not guarantee) consistency with typed arrays, significant deviations from ECMAScript-defined typed array behavior are as follows:

    • The constructor does not require the new operator.
    • Accessing array elements using bracket syntax (e.g., X[i]) is not supported. Instead, one must use the .get() method.

Examples

import Uint8Array from 'https://cdn.jsdelivr.net/gh/stdlib-js/array-uint8@deno/mod.js';
import logEach from 'https://cdn.jsdelivr.net/gh/stdlib-js/console-log-each@deno/mod.js';
import BooleanArray from 'https://cdn.jsdelivr.net/gh/stdlib-js/array-bool@v0.0.1-deno/mod.js';

// Create a boolean array by specifying a length:
var out = new BooleanArray( 3 );
logEach( '%s', out );

// Create a boolean array from an array of booleans:
var arr = [ true, false, true ];
out = new BooleanArray( arr );
logEach( '%s', out );

// Create a boolean array from an array buffer:
arr = new Uint8Array( [ 1, 0, 1, 1, 0, 1 ] );
out = new BooleanArray( arr.buffer );
logEach( '%s', out );

// Create a boolean array from an array buffer view:
arr = new Uint8Array( [ 1, 0, 1, 1, 0, 1 ] );
out = new BooleanArray( arr.buffer, 1, 2 );
logEach( '%s', out );

console.log( '%s', false );

Notice

This package is part of stdlib, a standard library with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more.

For more information on the project, filing bug reports and feature requests, and guidance on how to develop stdlib, see the main project repository.

Community

Chat


License

See LICENSE.

Copyright

Copyright © 2016-2024. The Stdlib Authors.

0