Skip to content

Commit

Permalink
feat: add boolean dtype support to array/convert-same
Browse files Browse the repository at this point in the history
PR-URL: #2494
Ref: #2304
Co-authored-by: Athan Reines <[email protected]>
Reviewed-by: Athan Reines <[email protected]>
  • Loading branch information
Jaysukh-409 and kgryte authored Jul 3, 2024
1 parent d2d46ed commit 48fd331
Show file tree
Hide file tree
Showing 7 changed files with 277 additions and 41 deletions.
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: Uint8Array ): Uint8Array;
*/
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
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

1 comment on commit 48fd331

@stdlib-bot
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage Report

Package Statements Branches Functions Lines
array/convert-same $\color{green}103/103$
$\color{green}+100.00\%$
$\color{green}5/5$
$\color{green}+100.00\%$
$\color{green}1/1$
$\color{green}+100.00\%$
$\color{green}103/103$
$\color{green}+100.00\%$

The above coverage report was generated for the changes in this push.

Please sign in to comment.