From da77f9dd2811ada1d158f2a0de5b51dd129734a1 Mon Sep 17 00:00:00 2001 From: Jaysukh-409 Date: Wed, 26 Jun 2024 16:38:54 +0530 Subject: [PATCH 1/5] feat: add boolean dtype support to array/empty-like --- .../@stdlib/array/empty-like/README.md | 3 +- .../benchmark/benchmark.length.bool.js | 95 +++++++++++++++++++ .../@stdlib/array/empty-like/docs/repl.txt | 1 + .../array/empty-like/docs/types/test.ts | 3 +- .../@stdlib/array/empty-like/test/test.js | 16 +++- 5 files changed, 115 insertions(+), 3 deletions(-) create mode 100644 lib/node_modules/@stdlib/array/empty-like/benchmark/benchmark.length.bool.js diff --git a/lib/node_modules/@stdlib/array/empty-like/README.md b/lib/node_modules/@stdlib/array/empty-like/README.md index eac7ec05dc9e..31355916e634 100644 --- a/lib/node_modules/@stdlib/array/empty-like/README.md +++ b/lib/node_modules/@stdlib/array/empty-like/README.md @@ -2,7 +2,7 @@ @license Apache-2.0 -Copyright (c) 2023 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. @@ -57,6 +57,7 @@ The function recognizes the following data types: - `float32`: single-precision floating-point numbers (IEEE 754) - `complex128`: double-precision complex floating-point numbers - `complex64`: single-precision complex floating-point numbers +- `bool`: boolean values - `int32`: 32-bit two's complement signed integers - `uint32`: 32-bit unsigned integers - `int16`: 16-bit two's complement signed integers diff --git a/lib/node_modules/@stdlib/array/empty-like/benchmark/benchmark.length.bool.js b/lib/node_modules/@stdlib/array/empty-like/benchmark/benchmark.length.bool.js new file mode 100644 index 000000000000..9f0bde188d7e --- /dev/null +++ b/lib/node_modules/@stdlib/array/empty-like/benchmark/benchmark.length.bool.js @@ -0,0 +1,95 @@ +/** +* @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 isTypedArrayLike = require( '@stdlib/assert/is-typed-array-like' ); +var zeros = require( '@stdlib/array/zeros' ); +var pkg = require( './../package.json' ).name; +var emptyLike = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var x = zeros( len, 'bool' ); + 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 = emptyLike( x ); + if ( arr.length !== len ) { + b.fail( 'unexpected length' ); + } + } + b.toc(); + if ( !isTypedArrayLike( arr ) ) { + b.fail( 'should return a typed 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( len ); + bench( pkg+':dtype=bool,len='+len, f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/array/empty-like/docs/repl.txt b/lib/node_modules/@stdlib/array/empty-like/docs/repl.txt index baaf88551969..c0a3a3e691db 100644 --- a/lib/node_modules/@stdlib/array/empty-like/docs/repl.txt +++ b/lib/node_modules/@stdlib/array/empty-like/docs/repl.txt @@ -17,6 +17,7 @@ - float32: single-precision floating-point numbers (IEEE 754) - complex128: double-precision complex floating-point numbers - complex64: single-precision complex floating-point numbers + - bool: boolean values - int32: 32-bit two's complement signed integers - uint32: 32-bit unsigned integers - int16: 16-bit two's complement signed integers diff --git a/lib/node_modules/@stdlib/array/empty-like/docs/types/test.ts b/lib/node_modules/@stdlib/array/empty-like/docs/types/test.ts index 58f865dd9318..777f0f355f42 100644 --- a/lib/node_modules/@stdlib/array/empty-like/docs/types/test.ts +++ b/lib/node_modules/@stdlib/array/empty-like/docs/types/test.ts @@ -1,7 +1,7 @@ /* * @license Apache-2.0 * -* Copyright (c) 2023 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. @@ -47,6 +47,7 @@ import emptyLike = require( './index' ); emptyLike( [ 0, 0 ], 'complex128' ); // $ExpectType Complex128Array emptyLike( [ 0, 0 ], 'complex64' ); // $ExpectType Complex64Array emptyLike( [ 0, 0 ], 'complex64' ); // $ExpectType Complex64Array + emptyLike( [ 0, 0 ], 'bool' ); // $ExpectType BooleanArray emptyLike( [ 0, 0 ], 'int32' ); // $ExpectType Int32Array emptyLike( [ 0, 0 ], 'int16' ); // $ExpectType Int16Array emptyLike( [ 0, 0 ], 'int8' ); // $ExpectType Int8Array diff --git a/lib/node_modules/@stdlib/array/empty-like/test/test.js b/lib/node_modules/@stdlib/array/empty-like/test/test.js index 7b7c6d221e58..90e540492eb7 100644 --- a/lib/node_modules/@stdlib/array/empty-like/test/test.js +++ b/lib/node_modules/@stdlib/array/empty-like/test/test.js @@ -1,7 +1,7 @@ /** * @license Apache-2.0 * -* Copyright (c) 2023 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. @@ -32,6 +32,7 @@ var Uint8Array = require( '@stdlib/array/uint8' ); var Uint8ClampedArray = require( '@stdlib/array/uint8c' ); var Complex64Array = require( '@stdlib/array/complex64' ); var Complex128Array = require( '@stdlib/array/complex128' ); +var BooleanArray = require( '@stdlib/array/bool' ); var instanceOf = require( '@stdlib/assert/instance-of' ); var emptyLike = require( './../lib' ); @@ -189,6 +190,19 @@ tape( 'the function returns an empty array (dtype=float32)', function test( t ) t.end(); }); +tape( 'the function returns an empty array (bool)', function test( t ) { + var arr; + var x; + + x = new BooleanArray( 2 ); + + arr = emptyLike( x ); + t.strictEqual( instanceOf( arr, BooleanArray ), true, 'returns expected value' ); + t.strictEqual( arr.length, x.length, 'returns expected value' ); + + t.end(); +}); + tape( 'the function returns an empty array (complex128)', function test( t ) { var arr; var x; From 404fbd4d75dc3fad6e03f02191141cc16fc7157a Mon Sep 17 00:00:00 2001 From: Jaysukh-409 Date: Wed, 26 Jun 2024 16:57:23 +0530 Subject: [PATCH 2/5] bench: updated benchmark --- .../array/empty-like/benchmark/benchmark.js | 24 ++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/array/empty-like/benchmark/benchmark.js b/lib/node_modules/@stdlib/array/empty-like/benchmark/benchmark.js index b5e5354d7736..37b898ec74a6 100644 --- a/lib/node_modules/@stdlib/array/empty-like/benchmark/benchmark.js +++ b/lib/node_modules/@stdlib/array/empty-like/benchmark/benchmark.js @@ -1,7 +1,7 @@ /** * @license Apache-2.0 * -* Copyright (c) 2023 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. @@ -96,6 +96,28 @@ bench( pkg+':dtype=float32', function benchmark( b ) { b.end(); }); +bench( pkg+':dtype=bool', function benchmark( b ) { + var arr; + var x; + var i; + + x = zeros( 0, 'bool' ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + arr = emptyLike( x ); + if ( arr.length !== 0 ) { + b.fail( 'should have length 0' ); + } + } + b.toc(); + if ( !isTypedArrayLike( arr ) ) { + b.fail( 'should return a typed array' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + bench( pkg+':dtype=complex128', function benchmark( b ) { var arr; var x; From f0839cbade7763433e7d5c82233f48d3b5b8d64a Mon Sep 17 00:00:00 2001 From: Athan Reines Date: Wed, 26 Jun 2024 16:01:56 -0700 Subject: [PATCH 3/5] fix: add missing type info and tests --- .../array/empty-like/docs/types/index.d.ts | 19 ++----------------- .../array/empty-like/docs/types/test.ts | 2 ++ 2 files changed, 4 insertions(+), 17 deletions(-) diff --git a/lib/node_modules/@stdlib/array/empty-like/docs/types/index.d.ts b/lib/node_modules/@stdlib/array/empty-like/docs/types/index.d.ts index 189f7afd9cdd..6f8397c49ced 100644 --- a/lib/node_modules/@stdlib/array/empty-like/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/array/empty-like/docs/types/index.d.ts @@ -20,7 +20,7 @@ /// -import { AnyArray, DataTypeMap, TypedArray, ComplexTypedArray } from '@stdlib/types/array'; +import { AnyArray, DataTypeMap, TypedArray, BooleanTypedArray, ComplexTypedArray } from '@stdlib/types/array'; /** * Creates an uninitialized array having the same length and data type as a provided input array. @@ -64,7 +64,7 @@ declare function emptyLike( x: Array ): Array; * var arr = emptyLike( x, 'float32' ); * // returns */ -declare function emptyLike( x: T ): T; +declare function emptyLike( x: T ): T; /** * Creates an uninitialized array having the same length as a provided input array. @@ -75,21 +75,6 @@ declare function emptyLike( x: T ): T; * - If `dtype` is `'generic'`, the function always returns a zero-filled array. * - In Node.js versions `>=3.0.0`, the underlying memory of returned typed arrays is **not** initialized. Memory contents are unknown and may contain **sensitive** data. * -* 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` -* - `generic`: generic JavaScript values -* * @param x - input array from which to derive the output array length * @param dtype - data type * @returns empty array diff --git a/lib/node_modules/@stdlib/array/empty-like/docs/types/test.ts b/lib/node_modules/@stdlib/array/empty-like/docs/types/test.ts index 777f0f355f42..12819ef9e4a9 100644 --- a/lib/node_modules/@stdlib/array/empty-like/docs/types/test.ts +++ b/lib/node_modules/@stdlib/array/empty-like/docs/types/test.ts @@ -18,6 +18,7 @@ import Complex128Array = require( '@stdlib/array/complex128' ); import Complex64Array = require( '@stdlib/array/complex64' ); +import BooleanArray = require( '@stdlib/array/bool' ); import emptyLike = require( './index' ); @@ -39,6 +40,7 @@ import emptyLike = require( './index' ); emptyLike( new Uint16Array( [ 0, 0 ] ) ); // $ExpectType Uint16Array emptyLike( new Uint8Array( [ 0, 0 ] ) ); // $ExpectType Uint8Array emptyLike( new Uint8ClampedArray( [ 0, 0 ] ) ); // $ExpectType Uint8ClampedArray + emptyLike( new BooleanArray( [ 0, 0 ] ) ); // $ExpectType BooleanArray emptyLike( [ 'a', 'b', 'c' ] ); // $ExpectType number[] emptyLike( [ 0, 0 ], 'float64' ); // $ExpectType Float64Array From b6100a8f3de59fe46b51f2d1c569cbca6a555a3f Mon Sep 17 00:00:00 2001 From: Athan Reines Date: Wed, 26 Jun 2024 16:04:05 -0700 Subject: [PATCH 4/5] test: fix missing test --- .../@stdlib/array/empty-like/docs/repl.txt | 16 ---------------- .../@stdlib/array/empty-like/test/test.js | 13 +++++++++++++ 2 files changed, 13 insertions(+), 16 deletions(-) diff --git a/lib/node_modules/@stdlib/array/empty-like/docs/repl.txt b/lib/node_modules/@stdlib/array/empty-like/docs/repl.txt index c0a3a3e691db..16de82e7fcea 100644 --- a/lib/node_modules/@stdlib/array/empty-like/docs/repl.txt +++ b/lib/node_modules/@stdlib/array/empty-like/docs/repl.txt @@ -11,22 +11,6 @@ is *not* initialized. Memory contents are unknown and may contain *sensitive* data. - 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 - - bool: boolean values - - 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 - - generic: generic JavaScript values - Parameters ---------- x: TypedArray|Array diff --git a/lib/node_modules/@stdlib/array/empty-like/test/test.js b/lib/node_modules/@stdlib/array/empty-like/test/test.js index 90e540492eb7..02c12788aa24 100644 --- a/lib/node_modules/@stdlib/array/empty-like/test/test.js +++ b/lib/node_modules/@stdlib/array/empty-like/test/test.js @@ -203,6 +203,19 @@ tape( 'the function returns an empty array (bool)', function test( t ) { t.end(); }); +tape( 'the function returns an empty array (dtype=bool)', function test( t ) { + var arr; + var x; + + x = new Float64Array( 5 ); + + arr = emptyLike( x, 'bool' ); + t.strictEqual( instanceOf( arr, BooleanArray ), true, 'returns expected value' ); + t.strictEqual( arr.length, x.length, 'returns expected value' ); + + t.end(); +}); + tape( 'the function returns an empty array (complex128)', function test( t ) { var arr; var x; From e59eca2f856a2c5905398bb89cfb40573e9b03cd Mon Sep 17 00:00:00 2001 From: Athan Reines Date: Wed, 26 Jun 2024 16:05:25 -0700 Subject: [PATCH 5/5] docs: reduce future maintenance burden by linking to `dtypes` package --- .../@stdlib/array/empty-like/README.md | 22 ++++--------------- .../@stdlib/array/empty-like/package.json | 2 ++ 2 files changed, 6 insertions(+), 18 deletions(-) diff --git a/lib/node_modules/@stdlib/array/empty-like/README.md b/lib/node_modules/@stdlib/array/empty-like/README.md index 31355916e634..fc6db5ab36f1 100644 --- a/lib/node_modules/@stdlib/array/empty-like/README.md +++ b/lib/node_modules/@stdlib/array/empty-like/README.md @@ -42,7 +42,7 @@ var emptyLike = require( '@stdlib/array/empty-like' ); #### emptyLike( x\[, dtype] ) -Creates an uninitialized array having the same length and data type as a provided array `x`. +Creates an uninitialized array having the same length and [data type][@stdlib/array/dtypes] as a provided array `x`. ```javascript var x = [ 1, 2, 3, 4, 5 ]; @@ -51,23 +51,7 @@ var arr = emptyLike( x ); // returns [ 0, 0, 0, 0, 0 ]; ``` -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 -- `bool`: boolean values -- `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` -- `generic`: generic JavaScript values - -By default, the output array data type is inferred from the provided array `x`. To return an array having a different data type, provide a `dtype` argument. +By default, the output array [data type][@stdlib/array/dtypes] is inferred from the provided array `x`. To return an array having a different [data type][@stdlib/array/dtypes], provide a `dtype` argument. ```javascript var x = [ 1, 1 ]; @@ -156,6 +140,8 @@ for ( i = 0; i < dt.length; i++ ) {