diff --git a/CHANGELOG.md b/CHANGELOG.md index c14f4b19..23b28aa7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,7 @@
-## Unreleased (2024-06-29) +## Unreleased (2024-07-01)
@@ -1872,6 +1872,28 @@ This release closes the following issue: +
+ +#### [@stdlib/array/pool](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/pool) + +
+ +
+ +##### Features + +- [`88cece6`](https://github.com/stdlib-js/stdlib/commit/88cece679d728150847dc2b5c957b395bffe7d90) - add boolean dtype support to `array/pool` [(#2486)](https://github.com/stdlib-js/stdlib/pull/2486) + +
+ + + +
+ +
+ + +
#### [@stdlib/array/promotion-rules](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/promotion-rules) @@ -2291,6 +2313,7 @@ A total of 13 people contributed to this release. Thank you to the following con
+- [`88cece6`](https://github.com/stdlib-js/stdlib/commit/88cece679d728150847dc2b5c957b395bffe7d90) - **feat:** add boolean dtype support to `array/pool` [(#2486)](https://github.com/stdlib-js/stdlib/pull/2486) _(by Jaysukh Makvana, Athan Reines)_ - [`3368a35`](https://github.com/stdlib-js/stdlib/commit/3368a3503ee7df4d1c0803ada84863b4250c76fa) - **docs:** update namespace TypeScript declarations [(#2477)](https://github.com/stdlib-js/stdlib/pull/2477) _(by stdlib-bot, Athan Reines)_ - [`3a3116e`](https://github.com/stdlib-js/stdlib/commit/3a3116e3ff5bef42e4b4f39e5375b89a877ccff0) - **feat:** add boolean dtype support to `array/from-scalar` [(#2470)](https://github.com/stdlib-js/stdlib/pull/2470) _(by Jaysukh Makvana, Athan Reines)_ - [`8a55ea2`](https://github.com/stdlib-js/stdlib/commit/8a55ea29ae7cc04e9ebef428ee90900641bbabe1) - **feat:** add boolean dtype support to `array/filled` [(#2471)](https://github.com/stdlib-js/stdlib/pull/2471) _(by Jaysukh Makvana, Athan Reines)_ diff --git a/pool/README.md b/pool/README.md index 3ef58688..a6b282ca 100644 --- a/pool/README.md +++ b/pool/README.md @@ -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. @@ -18,7 +18,7 @@ limitations under the License. --> -# Typed Array Pool +# typedarraypool > Allocate typed arrays from a typed array memory pool. @@ -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(); @@ -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' ); @@ -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(); @@ -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(); @@ -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/array/tree/main/typed-dtypes + [@stdlib/array/typed]: https://github.com/stdlib-js/array/tree/main/typed diff --git a/pool/benchmark/benchmark.calloc.js b/pool/benchmark/benchmark.calloc.js index 36232a1c..0872d55b 100644 --- a/pool/benchmark/benchmark.calloc.js +++ b/pool/benchmark/benchmark.calloc.js @@ -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. @@ -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' ); @@ -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; diff --git a/pool/benchmark/benchmark.js b/pool/benchmark/benchmark.js index 70491077..ed630f85 100644 --- a/pool/benchmark/benchmark.js +++ b/pool/benchmark/benchmark.js @@ -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. @@ -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' ); @@ -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; diff --git a/pool/benchmark/benchmark.length.bool.js b/pool/benchmark/benchmark.length.bool.js new file mode 100644 index 00000000..0833dbc1 --- /dev/null +++ b/pool/benchmark/benchmark.length.bool.js @@ -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(); diff --git a/pool/benchmark/benchmark.malloc.js b/pool/benchmark/benchmark.malloc.js index 37810365..544824a2 100644 --- a/pool/benchmark/benchmark.malloc.js +++ b/pool/benchmark/benchmark.malloc.js @@ -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. @@ -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' ); @@ -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; diff --git a/pool/docs/repl.txt b/pool/docs/repl.txt index 5244595f..a2727d64 100644 --- a/pool/docs/repl.txt +++ b/pool/docs/repl.txt @@ -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) @@ -272,7 +256,7 @@ -------- > var arr = {{alias}}( 5 ) - > {{alias}}.free( arr ) + > {{alias}}.free( arr ); {{alias}}.clear() @@ -284,7 +268,7 @@ > var arr = {{alias}}( 5 ) > {{alias}}.free( arr ); - > {{alias}}.clear() + > {{alias}}.clear(); {{alias}}.highWaterMark @@ -295,6 +279,7 @@ Examples -------- > {{alias}}.highWaterMark + {{alias}}.nbytes @@ -318,6 +303,7 @@ > var arr = {{alias}}( 5 ) > {{alias}}.nbytes + {{alias}}.factory( [options] ) diff --git a/pool/docs/types/index.d.ts b/pool/docs/types/index.d.ts index 9b78d577..ee01b0e3 100644 --- a/pool/docs/types/index.d.ts +++ b/pool/docs/types/index.d.ts @@ -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. @@ -20,13 +20,13 @@ /// -import { ArrayLike, RealOrComplexTypedArray, NumericDataType as DataType } from '@stdlib/types/array'; +import { ArrayLike, RealOrComplexTypedArray, BooleanArray, NumericDataType as DataType } from '@stdlib/types/array'; import ArrayBuffer = require( './../../../buffer' ); /** * Typed array or null. */ -type TypedArrayOrNull = RealOrComplexTypedArray | null; +type TypedArrayOrNull = RealOrComplexTypedArray | BooleanArray | null; /** * Interface defining pool options. @@ -255,7 +255,7 @@ interface Pool { * // 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. diff --git a/pool/lib/bytes_per_element.json b/pool/lib/bytes_per_element.json index f97fe705..30f02fcd 100644 --- a/pool/lib/bytes_per_element.json +++ b/pool/lib/bytes_per_element.json @@ -1,4 +1,5 @@ { + "bool": 1, "float64": 8, "float32": 4, "int16": 2, diff --git a/pool/lib/factory.js b/pool/lib/factory.js index 5423e275..823b517f 100644 --- a/pool/lib/factory.js +++ b/pool/lib/factory.js @@ -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. @@ -25,13 +25,16 @@ var isNonNegativeInteger = require( '@stdlib/assert/is-nonnegative-integer' ).is var isCollection = require( '@stdlib/assert/is-collection' ); var isTypedArrayLike = require( '@stdlib/assert/is-typed-array-like' ); var isArrayBuffer = require( '@stdlib/assert/is-arraybuffer' ); +var isComplexDataType = require( './../../base/assert/is-complex-floating-point-data-type' ); var isComplex64Array = require( '@stdlib/assert/is-complex64array' ); var isComplex128Array = require( '@stdlib/assert/is-complex128array' ); +var isBooleanArray = require( '@stdlib/assert/is-booleanarray' ); var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); var setReadOnlyAccessor = require( '@stdlib/utils/define-nonenumerable-read-only-accessor' ); var ctors = require( './../../typed-ctors' ); var reinterpret64 = require( '@stdlib/strided/base/reinterpret-complex64' ); var reinterpret128 = require( '@stdlib/strided/base/reinterpret-complex128' ); +var reinterpretBoolean = require( '@stdlib/strided/base/reinterpret-boolean' ); var resolveGetter = require( './../../base/resolve-getter' ); var resolveSetter = require( './../../base/resolve-setter' ); var ARRAY_DEFAULTS = require( './../../defaults' ); @@ -53,6 +56,7 @@ var BYTES_PER_ELEMENT = require( './bytes_per_element.json' ); var DEFAULT_DTYPE = ARRAY_DEFAULTS.get( 'dtypes.default' ); var Complex64Array = ctors( 'complex64' ); var Complex128Array = ctors( 'complex128' ); +var BooleanArray = ctors( 'bool' ); // FUNCTIONS // @@ -79,6 +83,17 @@ function isCmplx128Array( arr ) { return ( arr instanceof Complex128Array ); } +/** +* Tests whether an array is a boolean array. +* +* @private +* @param {Collection} arr - input array +* @returns {boolean} boolean indicating whether an input array is a boolean array +*/ +function isBoolArray( arr ) { + return ( arr instanceof BooleanArray ); +} + // MAIN // @@ -245,7 +260,9 @@ function factory( options ) { arr = reinterpret128( arr, 0 ); } else if ( isComplex64Array( arr ) ) { arr = reinterpret64( arr, 0 ); - } else if ( /^complex/.test( dtype ) ) { + } else if ( isBooleanArray( arr ) ) { + arr = reinterpretBoolean( arr, 0 ); + } else if ( isComplexDataType( dtype ) ) { // Assume we've been provided an array of interleaved real and imaginary components... len /= 2; } @@ -253,7 +270,7 @@ function factory( options ) { if ( out === null ) { return out; } - if ( isCmplx128Array( out ) || isCmplx64Array( out ) ) { + if ( isCmplx128Array( out ) || isCmplx64Array( out ) || isBoolArray( out ) ) { out.set( arr ); return out; } @@ -302,6 +319,8 @@ function factory( options ) { tmp = reinterpret128( out, 0 ); } else if ( isCmplx64Array( out ) ) { tmp = reinterpret64( out, 0 ); + } else if ( isBoolArray( out ) ) { + tmp = reinterpretBoolean( out, 0 ); } else { tmp = out; } diff --git a/pool/test/test.factory.js b/pool/test/test.factory.js index 53d61487..3caf93d0 100644 --- a/pool/test/test.factory.js +++ b/pool/test/test.factory.js @@ -3,7 +3,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. @@ -34,10 +34,12 @@ var Uint8Array = require( './../../uint8' ); var Uint8ClampedArray = require( './../../uint8c' ); var Complex64Array = require( './../../complex64' ); var Complex128Array = require( './../../complex128' ); +var BooleanArray = require( './../../bool' ); var instanceOf = require( '@stdlib/assert/instance-of' ); var randu = require( '@stdlib/random/base/randu' ); var Complex128 = require( '@stdlib/complex/float64/ctor' ); var Complex64 = require( '@stdlib/complex/float32/ctor' ); +var Boolean = require( '@stdlib/boolean/ctor' ); var real = require( '@stdlib/complex/real' ); var realf = require( '@stdlib/complex/realf' ); var imag = require( '@stdlib/complex/imag' ); @@ -375,6 +377,22 @@ tape( 'the function returns a function which returns a typed array (dtype=float3 t.end(); }); +tape( 'the function returns a function which returns a typed array (dtype=bool)', function test( t ) { + var typedarraypool; + var arr; + + typedarraypool = factory(); + + arr = typedarraypool( 'bool' ); + t.strictEqual( instanceOf( arr, BooleanArray ), true, 'returns expected value' ); + t.strictEqual( arr.length, 0, 'returns expected value' ); + + typedarraypool.free( arr ); + typedarraypool.clear(); + + t.end(); +}); + tape( 'the function returns a function which returns a typed array (dtype=complex128)', function test( t ) { var typedarraypool; var arr; @@ -565,6 +583,22 @@ tape( 'the function returns a function which returns a typed array (dtype=float3 t.end(); }); +tape( 'the function returns a function which returns a typed array (dtype=bool, length)', function test( t ) { + var typedarraypool; + var arr; + + typedarraypool = factory(); + + arr = typedarraypool( 10, 'bool' ); + t.strictEqual( instanceOf( arr, BooleanArray ), true, 'returns expected value' ); + t.strictEqual( arr.length, 10, 'returns expected value' ); + + typedarraypool.free( arr ); + typedarraypool.clear(); + + t.end(); +}); + tape( 'the function returns a function which returns a typed array (dtype=complex128, length)', function test( t ) { var typedarraypool; var arr; @@ -796,6 +830,45 @@ tape( 'the function returns a function which returns a typed array (dtype=float3 t.end(); }); +tape( 'the function returns a function which returns a typed array (dtype=bool, array)', function test( t ) { + var typedarraypool; + var arr; + var out; + var v; + + typedarraypool = factory(); + + arr = []; + out = typedarraypool( arr, 'bool' ); + t.strictEqual( instanceOf( out, BooleanArray ), true, 'returns expected value' ); + t.strictEqual( out.length, 0, 'returns expected value' ); + + typedarraypool.free( out ); + typedarraypool.clear(); + + arr = [ true, false, false, true ]; + out = typedarraypool( arr, 'bool' ); + t.strictEqual( instanceOf( out, BooleanArray ), true, 'returns expected value' ); + t.strictEqual( out.length, 4, 'returns expected value' ); + + v = out.get( 0 ); + t.strictEqual( v, arr[ 0 ], 'returns expected value' ); + + v = out.get( 1 ); + t.strictEqual( v, arr[ 1 ], 'returns expected value' ); + + v = out.get( 2 ); + t.strictEqual( v, arr[ 2 ], 'returns expected value' ); + + v = out.get( 3 ); + t.strictEqual( v, arr[ 3 ], 'returns expected value' ); + + typedarraypool.free( out ); + typedarraypool.clear(); + + t.end(); +}); + tape( 'the function returns a function which returns a typed array (dtype=complex128, array)', function test( t ) { var typedarraypool; var arr; @@ -1156,6 +1229,45 @@ tape( 'the function returns a function which returns a typed array (dtype=float3 t.end(); }); +tape( 'the function returns a function which returns a typed array (dtype=bool, typed array)', function test( t ) { + var typedarraypool; + var arr; + var out; + var v; + + typedarraypool = factory(); + + arr = new Uint8Array( 0 ); + out = typedarraypool( arr, 'bool' ); + t.strictEqual( instanceOf( out, BooleanArray ), true, 'returns expected value' ); + t.strictEqual( out.length, 0, 'returns expected value' ); + + typedarraypool.free( out ); + typedarraypool.clear(); + + arr = new Uint8Array( [ 1, 0, 0, 1 ] ); + out = typedarraypool( arr, 'bool' ); + t.strictEqual( instanceOf( out, BooleanArray ), true, 'returns expected value' ); + t.strictEqual( out.length, 4, 'returns expected value' ); + + v = out.get( 0 ); + t.strictEqual( v, true, 'returns expected value' ); + + v = out.get( 1 ); + t.strictEqual( v, false, 'returns expected value' ); + + v = out.get( 2 ); + t.strictEqual( v, false, 'returns expected value' ); + + v = out.get( 3 ); + t.strictEqual( v, true, 'returns expected value' ); + + typedarraypool.free( out ); + typedarraypool.clear(); + + t.end(); +}); + tape( 'the function returns a function which returns a typed array (dtype=complex128, typed array)', function test( t ) { var typedarraypool; var arr; @@ -1483,6 +1595,22 @@ tape( 'attached to the returned function is a method which returns a zero-initia t.end(); }); +tape( 'attached to the returned function is a method which returns a zero-initialized typed array (dtype=bool)', function test( t ) { + var typedarraypool; + var arr; + + typedarraypool = factory(); + + arr = typedarraypool.calloc( 'bool' ); + t.strictEqual( instanceOf( arr, BooleanArray ), true, 'returns expected value' ); + t.strictEqual( arr.length, 0, 'returns expected value' ); + + typedarraypool.free( arr ); + typedarraypool.clear(); + + t.end(); +}); + tape( 'attached to the returned function is a method which returns a zero-initialized typed array (dtype=complex128)', function test( t ) { var typedarraypool; var arr; @@ -1720,6 +1848,39 @@ tape( 'attached to the returned function is a method which returns a zero-initia t.end(); }); +tape( 'attached to the returned function is a method which returns a zero-initialized typed array (dtype=bool, length)', function test( t ) { + var typedarraypool; + var arr; + var tmp; + var v; + var i; + + typedarraypool = factory(); + typedarraypool.clear(); + + tmp = typedarraypool.malloc( 10, 'bool' ); + for ( i = 0; i < tmp.length; i++ ) { + tmp.set( Boolean( i%2 ), i ); + } + typedarraypool.free( tmp ); + + arr = typedarraypool.calloc( 10, 'bool' ); + t.strictEqual( instanceOf( arr, BooleanArray ), true, 'returns expected value' ); + t.notEqual( arr, tmp, 'returns a new view' ); + t.strictEqual( arr.buffer, tmp.buffer, 'same array buffer' ); + + t.strictEqual( arr.length, 10, 'returns expected value' ); + for ( i = 0; i < arr.length; i++ ) { + v = arr.get( i ); + t.strictEqual( v, false, 'returns expected value' ); + } + + typedarraypool.free( arr ); + typedarraypool.clear(); + + t.end(); +}); + tape( 'attached to the returned function is a method which returns a zero-initialized typed array (dtype=complex128, length)', function test( t ) { var typedarraypool; var arr; diff --git a/pool/test/test.js b/pool/test/test.js index 6db2d45c..865466ea 100644 --- a/pool/test/test.js +++ b/pool/test/test.js @@ -3,7 +3,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. @@ -34,10 +34,12 @@ var Uint8Array = require( './../../uint8' ); var Uint8ClampedArray = require( './../../uint8c' ); var Complex64Array = require( './../../complex64' ); var Complex128Array = require( './../../complex128' ); +var BooleanArray = require( './../../bool' ); var instanceOf = require( '@stdlib/assert/instance-of' ); var randu = require( '@stdlib/random/base/randu' ); var Complex128 = require( '@stdlib/complex/float64/ctor' ); var Complex64 = require( '@stdlib/complex/float32/ctor' ); +var Boolean = require( '@stdlib/boolean/ctor' ); var real = require( '@stdlib/complex/real' ); var realf = require( '@stdlib/complex/realf' ); var imag = require( '@stdlib/complex/imag' ); @@ -274,6 +276,17 @@ tape( 'the function returns a typed array (dtype=float32)', function test( t ) { t.end(); }); +tape( 'the function returns a typed array (dtype=bool)', function test( t ) { + var arr = typedarraypool( 'bool' ); + t.strictEqual( instanceOf( arr, BooleanArray ), true, 'returns expected value' ); + t.strictEqual( arr.length, 0, 'returns expected value' ); + + typedarraypool.free( arr ); + typedarraypool.clear(); + + t.end(); +}); + tape( 'the function returns a typed array (dtype=complex128)', function test( t ) { var arr = typedarraypool( 'complex128' ); t.strictEqual( instanceOf( arr, Complex128Array ), true, 'returns expected value' ); @@ -406,6 +419,17 @@ tape( 'the function returns a typed array (dtype=float32, length)', function tes t.end(); }); +tape( 'the function returns a typed array (dtype=bool, length)', function test( t ) { + var arr = typedarraypool( 10, 'bool' ); + t.strictEqual( instanceOf( arr, BooleanArray ), true, 'returns expected value' ); + t.strictEqual( arr.length, 10, 'returns expected value' ); + + typedarraypool.free( arr ); + typedarraypool.clear(); + + t.end(); +}); + tape( 'the function returns a typed array (dtype=complex128, length)', function test( t ) { var arr = typedarraypool( 10, 'complex128' ); t.strictEqual( instanceOf( arr, Complex128Array ), true, 'returns expected value' ); @@ -583,6 +607,42 @@ tape( 'the function returns a typed array (dtype=float32, array)', function test t.end(); }); +tape( 'the function returns a typed array (dtype=bool, array)', function test( t ) { + var arr; + var out; + var v; + + arr = []; + out = typedarraypool( arr, 'bool' ); + t.strictEqual( instanceOf( out, BooleanArray ), true, 'returns expected value' ); + t.strictEqual( out.length, 0, 'returns expected value' ); + + typedarraypool.free( out ); + typedarraypool.clear(); + + arr = [ true, false, false, true ]; + out = typedarraypool( arr, 'bool' ); + t.strictEqual( instanceOf( out, BooleanArray ), true, 'returns expected value' ); + t.strictEqual( out.length, 4, 'returns expected value' ); + + v = out.get( 0 ); + t.strictEqual( v, arr[ 0 ], 'returns expected value' ); + + v = out.get( 1 ); + t.strictEqual( v, arr[ 1 ], 'returns expected value' ); + + v = out.get( 2 ); + t.strictEqual( v, arr[ 2 ], 'returns expected value' ); + + v = out.get( 3 ); + t.strictEqual( v, arr[ 3 ], 'returns expected value' ); + + typedarraypool.free( out ); + typedarraypool.clear(); + + t.end(); +}); + tape( 'the function returns a typed array (dtype=complex128, array)', function test( t ) { var arr; var out; @@ -907,6 +967,42 @@ tape( 'the function returns a typed array (dtype=float32, typed array)', functio t.end(); }); +tape( 'the function returns a typed array (dtype=bool, typed array)', function test( t ) { + var arr; + var out; + var v; + + arr = new Uint8Array( 0 ); + out = typedarraypool( arr, 'bool' ); + t.strictEqual( instanceOf( out, BooleanArray ), true, 'returns expected value' ); + t.strictEqual( out.length, 0, 'returns expected value' ); + + typedarraypool.free( out ); + typedarraypool.clear(); + + arr = new Uint8Array( [ 1, 0, 0, 1 ] ); + out = typedarraypool( arr, 'bool' ); + t.strictEqual( instanceOf( out, BooleanArray ), true, 'returns expected value' ); + t.strictEqual( out.length, 4, 'returns expected value' ); + + v = out.get( 0 ); + t.strictEqual( v, true, 'returns expected value' ); + + v = out.get( 1 ); + t.strictEqual( v, false, 'returns expected value' ); + + v = out.get( 2 ); + t.strictEqual( v, false, 'returns expected value' ); + + v = out.get( 3 ); + t.strictEqual( v, true, 'returns expected value' ); + + typedarraypool.free( out ); + typedarraypool.clear(); + + t.end(); +}); + tape( 'the function returns a typed array (dtype=complex128, typed array)', function test( t ) { var arr; var out; @@ -1191,6 +1287,17 @@ tape( 'attached to the exported function is a method which returns a zero-initia t.end(); }); +tape( 'attached to the exported function is a method which returns a zero-initialized typed array (dtype=bool)', function test( t ) { + var arr = typedarraypool.calloc( 'bool' ); + t.strictEqual( instanceOf( arr, BooleanArray ), true, 'returns expected value' ); + t.strictEqual( arr.length, 0, 'returns expected value' ); + + typedarraypool.free( arr ); + typedarraypool.clear(); + + t.end(); +}); + tape( 'attached to the exported function is a method which returns a zero-initialized typed array (dtype=complex128)', function test( t ) { var arr = typedarraypool.calloc( 'complex128' ); t.strictEqual( instanceOf( arr, Complex128Array ), true, 'returns expected value' ); @@ -1377,6 +1484,37 @@ tape( 'attached to the exported function is a method which returns a zero-initia t.end(); }); +tape( 'attached to the exported function is a method which returns a zero-initialized typed array (dtype=bool, length)', function test( t ) { + var arr; + var tmp; + var v; + var i; + + typedarraypool.clear(); + + tmp = typedarraypool.malloc( 10, 'bool' ); + for ( i = 0; i < tmp.length; i++ ) { + tmp.set( Boolean( i%2 ), i ); + } + typedarraypool.free( tmp ); + + arr = typedarraypool.calloc( 10, 'bool' ); + t.strictEqual( instanceOf( arr, BooleanArray ), true, 'returns expected value' ); + t.notEqual( arr, tmp, 'returns a new view' ); + t.strictEqual( arr.buffer, tmp.buffer, 'same array buffer' ); + + t.strictEqual( arr.length, 10, 'returns expected value' ); + for ( i = 0; i < arr.length; i++ ) { + v = arr.get( i ); + t.strictEqual( v, false, 'returns expected value' ); + } + + typedarraypool.free( arr ); + typedarraypool.clear(); + + t.end(); +}); + tape( 'attached to the exported function is a method which returns a zero-initialized typed array (dtype=complex128, length)', function test( t ) { var arr; var tmp;