Skip to content

Commit adb47ab

Browse files
Jaysukh-409kgryte
andauthored
feat: add boolean dtype support to array/convert
PR-URL: #2488 Ref: #2304 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]>
1 parent bdb7cab commit adb47ab

File tree

7 files changed

+319
-39
lines changed

7 files changed

+319
-39
lines changed

Diff for: lib/node_modules/@stdlib/array/convert/README.md

+4-17
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ limitations under the License.
2020

2121
# convert
2222

23-
> Convert an array to an array of a different data type.
23+
> Convert an array to an array of a different [data type][@stdlib/array/dtypes].
2424
2525
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
2626

@@ -42,29 +42,14 @@ var convert = require( '@stdlib/array/convert' );
4242

4343
#### convert( arr, dtype )
4444

45-
Converts an array to an array of a different data type.
45+
Converts an array to an array of a different [data type][@stdlib/array/dtypes].
4646

4747
```javascript
4848
var arr = [ 1.0, 2.0, 3.0 ];
4949
var out = convert( arr, 'float32' );
5050
// returns <Float32Array>[ 1.0, 2.0, 3.0 ]
5151
```
5252

53-
The function supports the following data types:
54-
55-
- `float32`: single-precision floating-point numbers.
56-
- `float64`: double-precision floating-point numbers.
57-
- `complex64`: single-precision complex floating-point numbers.
58-
- `complex128`: double-precision complex floating-point numbers.
59-
- `generic`: values of any type.
60-
- `int16`: signed 16-bit integers.
61-
- `int32`: signed 32-bit integers.
62-
- `int8`: signed 8-bit integers.
63-
- `uint16`: unsigned 16-bit integers.
64-
- `uint32`: unsigned 32-bit integers.
65-
- `uint8`: unsigned 8-bit integers.
66-
- `uint8c`: unsigned clamped 8-bit integers.
67-
6853
</section>
6954

7055
<!-- /.usage -->
@@ -136,6 +121,8 @@ for ( i = 0; i < DTYPES.length; i++ ) {
136121

137122
<section class="links">
138123

124+
[@stdlib/array/dtypes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/dtypes
125+
139126
<!-- <related-links> -->
140127

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

Diff for: lib/node_modules/@stdlib/array/convert/benchmark/benchmark.js

+26-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* @license Apache-2.0
33
*
4-
* Copyright (c) 2018 The Stdlib Authors.
4+
* Copyright (c) 2024 The Stdlib Authors.
55
*
66
* Licensed under the Apache License, Version 2.0 (the "License");
77
* you may not use this file except in compliance with the License.
@@ -103,6 +103,31 @@ bench( pkg+':dtype=float32', function benchmark( b ) {
103103
b.end();
104104
});
105105

106+
bench( pkg+':dtype=bool', function benchmark( b ) {
107+
var arr;
108+
var out;
109+
var i;
110+
111+
arr = [];
112+
for ( i = 0; i < 10; i++ ) {
113+
arr.push( i );
114+
}
115+
b.tic();
116+
for ( i = 0; i < b.iterations; i++ ) {
117+
arr[ 0 ] += 1;
118+
out = convertArray( arr, 'bool' );
119+
if ( out.length !== arr.length ) {
120+
b.fail( 'should have expected length' );
121+
}
122+
}
123+
b.toc();
124+
if ( !isCollection( out ) ) {
125+
b.fail( 'should return an array-like object' );
126+
}
127+
b.pass( 'benchmark finished' );
128+
b.end();
129+
});
130+
106131
bench( pkg+':dtype=complex128', function benchmark( b ) {
107132
var arr;
108133
var out;

Diff for: lib/node_modules/@stdlib/array/convert/benchmark/benchmark.length.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* @license Apache-2.0
33
*
4-
* Copyright (c) 2018 The Stdlib Authors.
4+
* Copyright (c) 2024 The Stdlib Authors.
55
*
66
* Licensed under the Apache License, Version 2.0 (the "License");
77
* you may not use this file except in compliance with the License.
@@ -103,6 +103,9 @@ function main() {
103103
f = createBenchmark( len, 'float32' );
104104
bench( pkg+':len='+len+',dtype=float32', f );
105105

106+
f = createBenchmark( len, 'bool' );
107+
bench( pkg+':len='+len+',dtype=bool', f );
108+
106109
f = createBenchmark( len, 'complex128' );
107110
bench( pkg+':len='+len+',dtype=complex128', f );
108111

Diff for: lib/node_modules/@stdlib/array/convert/docs/repl.txt

-15
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,6 @@
22
{{alias}}( arr, dtype )
33
Converts an input array to an array of a different data type.
44

5-
The function supports the following data types:
6-
7-
- float32: single-precision floating-point numbers.
8-
- float64: double-precision floating-point numbers.
9-
- complex64: single-precision complex floating-point numbers.
10-
- complex128: double-precision complex floating-point numbers.
11-
- generic: values of any type.
12-
- int16: signed 16-bit integers.
13-
- int32: signed 32-bit integers.
14-
- int8: signed 8-bit integers.
15-
- uint16: unsigned 16-bit integers.
16-
- uint32: unsigned 32-bit integers.
17-
- uint8: unsigned 8-bit integers.
18-
- uint8c: unsigned clamped 8-bit integers.
19-
205
Parameters
216
----------
227
arr: ArrayLikeObject

Diff for: lib/node_modules/@stdlib/array/convert/docs/types/test.ts

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import convert = require( './index' );
3434
convert( [ 1.0, 2.0, 3.0, 4.0 ], 'uint8c' ); // $ExpectType Uint8ClampedArray
3535
convert( [ 1.0, 2.0, 3.0, 4.0 ], 'complex128' ); // $ExpectType Complex128Array
3636
convert( [ 1.0, 2.0, 3.0, 4.0 ], 'complex64' ); // $ExpectType Complex64Array
37+
convert( [ 1.0, 2.0, 3.0, 4.0 ], 'bool' ); // $ExpectType BooleanArray
3738
convert( [ 1.0, 2.0, 3.0, 4.0 ], 'generic' ); // $ExpectType number[]
3839
convert( [ 'a', 'b', 'c', 'd' ], 'generic' ); // $ExpectType string[]
3940
}

Diff for: lib/node_modules/@stdlib/array/convert/lib/main.js

+76-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* @license Apache-2.0
33
*
4-
* Copyright (c) 2018 The Stdlib Authors.
4+
* Copyright (c) 2024 The Stdlib Authors.
55
*
66
* Licensed under the Apache License, Version 2.0 (the "License");
77
* you may not use this file except in compliance with the License.
@@ -25,9 +25,11 @@ var getType = require( '@stdlib/array/dtype' );
2525
var ctors = require( '@stdlib/array/ctors' );
2626
var reinterpret128 = require( '@stdlib/strided/base/reinterpret-complex128' );
2727
var reinterpret64 = require( '@stdlib/strided/base/reinterpret-complex64' );
28-
var format = require( '@stdlib/string/format' );
28+
var reinterpretBoolean = require( '@stdlib/strided/base/reinterpret-boolean' );
2929
var gcopy = require( '@stdlib/blas/base/gcopy' );
3030
var copy = require( '@stdlib/array/base/copy' );
31+
var resolveGetter = require( '@stdlib/array/base/resolve-getter' );
32+
var format = require( '@stdlib/string/format' );
3133

3234

3335
// FUNCTIONS //
@@ -70,6 +72,25 @@ function isComplex128( dtype ) {
7072
return ( dtype === 'complex128' );
7173
}
7274

75+
/**
76+
* Tests whether a data type is a boolean data type.
77+
*
78+
* @private
79+
* @param {string} dtype - data type
80+
* @returns {boolean} boolean indicating whether a provided data type is a boolean data type
81+
*
82+
* @example
83+
* var bool = isBool( 'bool' );
84+
* // returns true
85+
*
86+
* @example
87+
* var bool = isBool( 'complex128' );
88+
* // returns false
89+
*/
90+
function isBool( dtype ) {
91+
return ( dtype === 'bool' );
92+
}
93+
7394

7495
// MAIN //
7596

@@ -93,9 +114,11 @@ function convert( x, dtype ) {
93114
var ctor;
94115
var xbuf;
95116
var obuf;
117+
var get;
96118
var out;
97119
var len;
98120
var t;
121+
var i;
99122

100123
if ( !isCollection( x ) ) {
101124
throw new TypeError( format( 'invalid argument. First argument must be an array-like object. Value: `%s`.', x ) );
@@ -136,10 +159,47 @@ function convert( x, dtype ) {
136159
gcopy( len*2, xbuf, 1, obuf, 1 );
137160
return out;
138161
}
162+
// Check whether the output data type is a boolean data type...
163+
if ( isBool( dtype ) ) { // cmplx => bool
164+
obuf = reinterpretBoolean( out, 0 );
165+
for ( i = 0; i < len; i++ ) {
166+
// A complex number is only falsy when both the real and imaginary components are zero...
167+
if ( xbuf[ 2*i ] || xbuf[ (2*i)+1 ] ) {
168+
obuf[ i ] = 1;
169+
} else {
170+
obuf[ i ] = 0;
171+
}
172+
}
173+
return out;
174+
}
139175
// We assume that the output data type is a real number data type, given that we're looking to convert a provided complex number array; in which case, we'll only extract the real components from the complex number input array...
140176
gcopy( len, xbuf, 2, out, 1 ); // cmplx => real
141177
return out;
142178
}
179+
// Check whether the input array is a boolean array...
180+
if ( isBool( t ) ) {
181+
xbuf = reinterpretBoolean( x, 0 );
182+
183+
// Check whether the output data type is a boolean data type...
184+
if ( isBool( dtype ) ) { // bool => bool
185+
obuf = reinterpretBoolean( out, 0 );
186+
gcopy( len, xbuf, 1, obuf, 1 );
187+
return out;
188+
}
189+
// Check whether the output data type is a complex number data type...
190+
if ( isComplex64( dtype ) ) { // bool => cmplx
191+
obuf = reinterpret64( out, 0 );
192+
gcopy( len, xbuf, 1, obuf, 2 );
193+
return out;
194+
}
195+
if ( isComplex128( dtype ) ) { // bool => cmplx
196+
obuf = reinterpret128( out, 0 );
197+
gcopy( len, xbuf, 1, obuf, 2 );
198+
return out;
199+
}
200+
gcopy( len, xbuf, 1, out, 1 ); // bool => real
201+
return out;
202+
}
143203
// Check whether we need to explicitly handle complex number output arrays...
144204
isc64 = isComplex64( dtype );
145205
if ( isc64 || isComplex128( dtype ) ) {
@@ -152,7 +212,20 @@ function convert( x, dtype ) {
152212
gcopy( len, x, 1, obuf, 2 ); // real => cmplx
153213
return out;
154214
}
155-
// At this point, we're no longer handling complex number arrays, so we'll just assume that we can perform a straightforward copy...
215+
// Check whether the output data type is a boolean data type...
216+
if ( isBool( dtype ) ) {
217+
obuf = reinterpretBoolean( out, 0 );
218+
get = resolveGetter( x );
219+
for ( i = 0; i < len; i++ ) {
220+
if ( get( x, i ) ) {
221+
obuf[ i ] = 1;
222+
} else {
223+
obuf[ i ] = 0;
224+
}
225+
}
226+
return out;
227+
}
228+
// At this point, we're no longer handling complex number or boolean arrays, so we'll just assume that we can perform a straightforward copy...
156229
gcopy( len, x, 1, out, 1 ); // note: `gcopy` is assumed to support arrays using accessors
157230
return out;
158231
}

0 commit comments

Comments
 (0)