Skip to content

feat: add boolean dtype support to array/convert-same #2494

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 4 commits into from
Jul 3, 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
23 changes: 5 additions & 18 deletions lib/node_modules/@stdlib/array/convert-same/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 @@ -20,7 +20,7 @@ limitations under the License.

# convertSame

> Convert an array to the same data type as a second input array.
> Convert an array to the same [data type][@stdlib/array/dtypes] as a second input array.

<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->

Expand All @@ -42,7 +42,7 @@ var convertSame = require( '@stdlib/array/convert-same' );

#### convertSame( x, y )

Converts an array to the same data type as a second input array.
Converts an array to the same [data type][@stdlib/array/dtypes] as a second input array.

```javascript
var Float32Array = require( '@stdlib/array/float32' );
Expand All @@ -54,21 +54,6 @@ var out = convertSame( x, y );
// returns <Float32Array>[ 1.0, 2.0, 3.0 ]
```

The function supports input arrays having the following data types:

- `float32`: single-precision floating-point numbers.
- `float64`: double-precision floating-point numbers.
- `complex64`: single-precision complex floating-point numbers.
- `complex128`: double-precision complex floating-point numbers.
- `generic`: values of any type.
- `int16`: signed 16-bit integers.
- `int32`: signed 32-bit integers.
- `int8`: signed 8-bit integers.
- `uint16`: unsigned 16-bit integers.
- `uint32`: unsigned 32-bit integers.
- `uint8`: unsigned 8-bit integers.
- `uint8c`: unsigned clamped 8-bit integers.

</section>

<!-- /.usage -->
Expand Down Expand Up @@ -147,6 +132,8 @@ for ( i = 0; i < DTYPES.length; i++ ) {

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

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

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

</section>
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 Down Expand Up @@ -33,6 +33,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 pkg = require( './../package.json' ).name;
var convertArraySame = require( './../lib' );

Expand Down Expand Up @@ -120,6 +121,34 @@ bench( pkg+':dtype=float32', function benchmark( b ) {
b.end();
});

bench( pkg+':dtype=bool', function benchmark( b ) {
var arr;
var out;
var v;
var i;

arr = [];
for ( i = 0; i < 10; i++ ) {
arr.push( i );
}
v = new BooleanArray( 0 );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
arr[ 0 ] += 1;
out = convertArraySame( arr, v );
if ( out.length !== arr.length ) {
b.fail( 'should have expected length' );
}
}
b.toc();
if ( !isCollection( out ) ) {
b.fail( 'should return an array-like object' );
}
b.pass( 'benchmark finished' );
b.end();
});

bench( pkg+':dtype=complex128', function benchmark( b ) {
var arr;
var out;
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 Down Expand Up @@ -34,6 +34,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 pkg = require( './../package.json' ).name;
var convertArraySame = require( './../lib' );

Expand Down Expand Up @@ -135,6 +136,9 @@ function main() {
f = createBenchmark( len, new Uint8ClampedArray( 0 ) );
bench( pkg+':len='+len+',dtype=uint8c', f );

f = createBenchmark( len, new BooleanArray( 0 ) );
bench( pkg+':len='+len+',dtype=bool', f );

f = createBenchmark( len, new Complex128Array( 0 ) );
bench( pkg+':len='+len+',dtype=complex128', f );

Expand Down
15 changes: 0 additions & 15 deletions lib/node_modules/@stdlib/array/convert-same/docs/repl.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,6 @@
{{alias}}( x, y )
Converts an input array to the same data type as a second input array.

The function supports input arrays having the following data types:

- float32: single-precision floating-point numbers.
- float64: double-precision floating-point numbers.
- complex64: single-precision complex floating-point numbers.
- complex128: double-precision complex floating-point numbers.
- generic: values of any type.
- int16: signed 16-bit integers.
- int32: signed 32-bit integers.
- int8: signed 8-bit integers.
- uint16: unsigned 16-bit integers.
- uint32: unsigned 32-bit integers.
- uint8: unsigned 8-bit integers.
- uint8c: unsigned clamped 8-bit integers.

Parameters
----------
x: ArrayLikeObject
Expand Down
22 changes: 20 additions & 2 deletions lib/node_modules/@stdlib/array/convert-same/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,7 +20,7 @@

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

import { AnyArray, Collection, Complex128Array, Complex64Array } from '@stdlib/types/array';
import { AnyArray, Collection, Complex128Array, Complex64Array, BooleanArray } from '@stdlib/types/array';

/**
* Converts an array to the same data type as a `Float64Array`.
Expand Down Expand Up @@ -184,6 +184,24 @@
*/
declare function convertSame( x: Collection, y: Uint8ClampedArray ): Uint8ClampedArray;

/**
* Converts an array to a `BooleanArray`.
*
* @param x - array to convert
* @param y - array having the desired output data type
* @returns output array
*
* @example
* var BooleanArray = require( '@stdlib/array/bool' );
*
* var x = [ -1, 0, 1, 2 ];
* var y = new BooleanArray( 0 );
*
* var out = convertSame( x, y );
* // returns <BooleanArray>
*/
declare function convertSame( x: Collection, y: BooleanArray ): BooleanArray;

/**
* Converts an array to a `Complex128Array`.
*
Expand Down Expand Up @@ -236,7 +254,7 @@
* var out = convertSame( x, y );
* // returns [ 1.0, 2.0, 3.0, 4.0 ]
*/
declare function convertSame( x: Collection, y: Array<any> ): Array<any>;

Check warning on line 257 in lib/node_modules/@stdlib/array/convert-same/docs/types/index.d.ts

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Unexpected any. Specify a different type

Check warning on line 257 in lib/node_modules/@stdlib/array/convert-same/docs/types/index.d.ts

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Unexpected any. Specify a different type

/**
* Converts an array to the same data type as a second input array.
Expand Down
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 @@ -18,6 +18,7 @@

import Complex128Array = require( '@stdlib/array/complex128' );
import Complex64Array = require( '@stdlib/array/complex64' );
import BooleanArray = require( '@stdlib/array/bool' );
import convertSame = require( './index' );


Expand All @@ -36,6 +37,7 @@ import convertSame = require( './index' );
convertSame( [ 1.0, 2.0, 3.0, 4.0 ], new Uint8ClampedArray( 0 ) ); // $ExpectType Uint8ClampedArray
convertSame( [ 1.0, 2.0, 3.0, 4.0 ], new Complex128Array( 0 ) ); // $ExpectType Complex128Array
convertSame( [ 1.0, 2.0, 3.0, 4.0 ], new Complex64Array( 0 ) ); // $ExpectType Complex64Array
convertSame( [ 1.0, 2.0, 3.0, 4.0 ], new BooleanArray( 0 ) ); // $ExpectType BooleanArray
convertSame( [ 1.0, 2.0, 3.0, 4.0 ], [] ); // $ExpectType any[]
}

Expand Down
Loading
Loading