Skip to content

feat: add boolean dtype support to array/pool #2486

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 3 commits into from
Jul 1, 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
28 changes: 8 additions & 20 deletions lib/node_modules/@stdlib/array/pool/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

@license Apache-2.0

Copyright (c) 2018 The Stdlib Authors.
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.
Expand All @@ -18,7 +18,7 @@ limitations under the License.

-->

# Typed Array Pool
# typedarraypool

> Allocate typed arrays from a typed array memory pool.

Expand All @@ -42,7 +42,7 @@ var typedarraypool = require( '@stdlib/array/pool' );

#### typedarraypool( \[dtype] )

Returns an **uninitialized** [typed array][mdn-typed-array] having a specified data type `dtype`.
Returns an **uninitialized** [typed array][mdn-typed-array] having a specified [data type][@stdlib/array/typed-dtypes] `dtype`.

```javascript
var arr = typedarraypool();
Expand All @@ -53,21 +53,7 @@ var arr = typedarraypool();
typedarraypool.free( arr );
```

The function recognizes the following data types:

- `float64`: double-precision floating-point numbers (IEEE 754)
- `float32`: single-precision floating-point numbers (IEEE 754)
- `complex128`: double-precision complex floating-point numbers
- `complex64`: single-precision complex floating-point numbers
- `int32`: 32-bit two's complement signed integers
- `uint32`: 32-bit unsigned integers
- `int16`: 16-bit two's complement signed integers
- `uint16`: 16-bit unsigned integers
- `int8`: 8-bit two's complement signed integers
- `uint8`: 8-bit unsigned integers
- `uint8c`: 8-bit unsigned integers clamped to `0-255`

By default, the output [typed array][mdn-typed-array] is `float64`. To specify an alternative data type, set the `dtype` parameter.
By default, the output [typed array][mdn-typed-array] is `float64`. To specify an alternative [data type][@stdlib/array/typed-dtypes], set the `dtype` parameter.

```javascript
var arr = typedarraypool( 'int32' );
Expand Down Expand Up @@ -135,7 +121,7 @@ typedarraypool.free( arr2 );

#### typedarraypool.malloc( \[dtype] )

Returns an **uninitialized** [typed array][mdn-typed-array] having a specified data type `dtype`.
Returns an **uninitialized** [typed array][mdn-typed-array] having a specified [data type][@stdlib/array/typed-dtypes] `dtype`.

```javascript
var arr1 = typedarraypool.malloc();
Expand Down Expand Up @@ -207,7 +193,7 @@ typedarraypool.free( arr2 );

#### typedarraypool.calloc( \[dtype] )

Returns a **zero-initialized** [typed array][mdn-typed-array] having a specified data type `dtype`.
Returns a **zero-initialized** [typed array][mdn-typed-array] having a specified [data type][@stdlib/array/typed-dtypes] `dtype`.

```javascript
var arr1 = typedarraypool.calloc();
Expand Down Expand Up @@ -461,6 +447,8 @@ console.log( 'nbytes: %d', typedarray.nbytes );

[mdn-arraybuffer]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer

[@stdlib/array/typed-dtypes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/typed-dtypes

<!-- <related-links> -->

[@stdlib/array/typed]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/typed
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2018 The Stdlib Authors.
* 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.
Expand All @@ -23,6 +23,7 @@
var bench = require( '@stdlib/bench' );
var isTypedArray = require( '@stdlib/assert/is-typed-array' );
var isComplexTypedArray = require( '@stdlib/assert/is-complex-typed-array' );
var isBooleanArray = require( '@stdlib/assert/is-booleanarray' );
var pkg = require( './../package.json' ).name;
var typedarray = require( './../lib' );

Expand Down Expand Up @@ -209,6 +210,24 @@ bench( pkg+':calloc:dtype=uint8c', function benchmark( b ) {
b.end();
});

bench( pkg+':calloc:dtype=bool', function benchmark( b ) {
var arr;
var i;
b.tic();
for ( i = 0; i < b.iterations; i++ ) {
arr = typedarray.calloc( 0, 'bool' );
if ( arr.length !== 0 ) {
b.fail( 'should have length 0' );
}
}
b.toc();
if ( !isBooleanArray( arr ) ) {
b.fail( 'should return a boolean array' );
}
b.pass( 'benchmark finished' );
b.end();
});

bench( pkg+':calloc:dtype=complex64', function benchmark( b ) {
var arr;
var i;
Expand Down
21 changes: 20 additions & 1 deletion lib/node_modules/@stdlib/array/pool/benchmark/benchmark.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2018 The Stdlib Authors.
* 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.
Expand All @@ -23,6 +23,7 @@
var bench = require( '@stdlib/bench' );
var isTypedArray = require( '@stdlib/assert/is-typed-array' );
var isComplexTypedArray = require( '@stdlib/assert/is-complex-typed-array' );
var isBooleanArray = require('@stdlib/assert/is-booleanarray');
var pkg = require( './../package.json' ).name;
var typedarray = require( './../lib' );

Expand Down Expand Up @@ -209,6 +210,24 @@ bench( pkg+':dtype=uint8c', function benchmark( b ) {
b.end();
});

bench( pkg+':dtype=bool', function benchmark( b ) {
var arr;
var i;
b.tic();
for ( i = 0; i < b.iterations; i++ ) {
arr = typedarray( 0, 'bool' );
if ( arr.length !== 0 ) {
b.fail( 'should have length 0' );
}
}
b.toc();
if ( !isBooleanArray( arr ) ) {
b.fail( 'should return a boolean array' );
}
b.pass( 'benchmark finished' );
b.end();
});

bench( pkg+':dtype=complex64', function benchmark( b ) {
var arr;
var i;
Expand Down
102 changes: 102 additions & 0 deletions lib/node_modules/@stdlib/array/pool/benchmark/benchmark.length.bool.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/**
* @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 isBooleanArray = require( '@stdlib/assert/is-booleanarray' );
var pkg = require( './../package.json' ).name;
var typedarray = require( './../lib' );


// FUNCTIONS //

/**
* Creates a benchmark function.
*
* @private
* @param {Function} fcn - allocation function
* @param {PositiveInteger} len - array length
* @returns {Function} benchmark function
*/
function createBenchmark( fcn, len ) {
return benchmark;

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

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
arr = fcn( len, 'bool' );
if ( arr.length !== len ) {
b.fail( 'unexpected length' );
}
typedarray.free( arr );
}
b.toc();
if ( !isBooleanArray( arr ) ) {
b.fail( 'should return a boolean array' );
}
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( typedarray, len );
bench( pkg+':dtype=bool,len='+len, f );

f = createBenchmark( typedarray.malloc, len );
bench( pkg+':malloc:dtype=bool,len='+len, f );

f = createBenchmark( typedarray.calloc, len );
bench( pkg+':calloc:dtype=bool,len='+len, f );
}
}

main();
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2018 The Stdlib Authors.
* 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.
Expand All @@ -23,6 +23,7 @@
var bench = require( '@stdlib/bench' );
var isTypedArray = require( '@stdlib/assert/is-typed-array' );
var isComplexTypedArray = require( '@stdlib/assert/is-complex-typed-array' );
var isBooleanArray = require( '@stdlib/assert/is-booleanarray' );
var pkg = require( './../package.json' ).name;
var typedarray = require( './../lib' );

Expand Down Expand Up @@ -209,6 +210,24 @@ bench( pkg+':malloc:dtype=uint8c', function benchmark( b ) {
b.end();
});

bench( pkg+':malloc:dtype=bool', function benchmark( b ) {
var arr;
var i;
b.tic();
for ( i = 0; i < b.iterations; i++ ) {
arr = typedarray.malloc( 0, 'bool' );
if ( arr.length !== 0 ) {
b.fail( 'should have length 0' );
}
}
b.toc();
if ( !isBooleanArray( arr ) ) {
b.fail( 'should return a boolean array' );
}
b.pass( 'benchmark finished' );
b.end();
});

bench( pkg+':malloc:dtype=complex64', function benchmark( b ) {
var arr;
var i;
Expand Down
22 changes: 4 additions & 18 deletions lib/node_modules/@stdlib/array/pool/docs/repl.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,6 @@
Memory is **uninitialized**, which means that the contents of a returned
typed array may contain sensitive contents.

The function supports the following data types:

- float64: double-precision floating-point numbers (IEEE 754)
- float32: single-precision floating-point numbers (IEEE 754)
- complex128: double-precision complex floating-point numbers
- complex64: single-precision complex floating-point numbers
- int32: 32-bit two's complement signed integers
- uint32: 32-bit unsigned integers
- int16: 16-bit two's complement signed integers
- uint16: 16-bit unsigned integers
- int8: 8-bit two's complement signed integers
- uint8: 8-bit unsigned integers
- uint8c: 8-bit unsigned integers clamped to 0-255

The default typed array data type is `float64`.

Parameters
----------
dtype: string (optional)
Expand Down Expand Up @@ -272,7 +256,7 @@
--------
> var arr = {{alias}}( 5 )
<Float64Array>
> {{alias}}.free( arr )
> {{alias}}.free( arr );


{{alias}}.clear()
Expand All @@ -284,7 +268,7 @@
> var arr = {{alias}}( 5 )
<Float64Array>
> {{alias}}.free( arr );
> {{alias}}.clear()
> {{alias}}.clear();


{{alias}}.highWaterMark
Expand All @@ -295,6 +279,7 @@
Examples
--------
> {{alias}}.highWaterMark
<number>


{{alias}}.nbytes
Expand All @@ -318,6 +303,7 @@
> var arr = {{alias}}( 5 )
<Float64Array>
> {{alias}}.nbytes
<number>


{{alias}}.factory( [options] )
Expand Down
8 changes: 4 additions & 4 deletions lib/node_modules/@stdlib/array/pool/docs/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* @license Apache-2.0
*
* Copyright (c) 2021 The Stdlib Authors.
* 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.
Expand All @@ -20,13 +20,13 @@

/// <reference types="@stdlib/types"/>

import { ArrayLike, RealOrComplexTypedArray, NumericDataType as DataType } from '@stdlib/types/array';
import { ArrayLike, RealOrComplexTypedArray, BooleanArray, NumericDataType as DataType } from '@stdlib/types/array';
import ArrayBuffer = require( '@stdlib/array/buffer' );

/**
* Typed array or null.
*/
type TypedArrayOrNull = RealOrComplexTypedArray | null;
type TypedArrayOrNull = RealOrComplexTypedArray | BooleanArray | null;

/**
* Interface defining pool options.
Expand Down Expand Up @@ -100,7 +100,7 @@
* var arr2 = typedarraypool( arr1, 'float32' );
* // returns <Float32Array>[ 0.5, 0.5, 0.5 ]
*/
( arg: number | ArrayLike<any>, dtype?: DataType ): TypedArrayOrNull;

Check warning on line 103 in lib/node_modules/@stdlib/array/pool/docs/types/index.d.ts

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Unexpected any. Specify a different type

/**
* Returns a zero-initialized typed array.
Expand Down Expand Up @@ -230,7 +230,7 @@
* typedarraypool.free( arr1 );
* typedarraypool.free( arr2 );
*/
malloc( arg: number | ArrayLike<any>, dtype?: DataType ): TypedArrayOrNull;

Check warning on line 233 in lib/node_modules/@stdlib/array/pool/docs/types/index.d.ts

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Unexpected any. Specify a different type

/**
* Frees a typed array or typed array buffer for use in a future allocation.
Expand All @@ -255,7 +255,7 @@
* // Free the allocated typed array buffer for use in a future allocation:
* typedarraypool.free( arr.buffer );
*/
free( buf: RealOrComplexTypedArray | ArrayBuffer ): void;
free( buf: RealOrComplexTypedArray | BooleanArray | ArrayBuffer ): void;

/**
* Clears the typed array pool allowing garbage collection of previously allocated (and currently free) array buffers.
Expand Down
Loading
Loading