Skip to content

Commit

Permalink
feat: add support for extending data type kind subsets with a "generi…
Browse files Browse the repository at this point in the history
…c" data type
  • Loading branch information
kgryte committed Jul 14, 2024
1 parent 1cba41b commit d06165b
Show file tree
Hide file tree
Showing 6 changed files with 297 additions and 4 deletions.
7 changes: 7 additions & 0 deletions lib/node_modules/@stdlib/ndarray/dtypes/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,13 @@ The function supports the following data type kinds:
- `typed`: typed data types.
- `all`: all data types.

Additionally, the function supports extending the "kinds" listed above by appending an `_and_generic` suffix to the kind name (e.g., `real_and_generic`).

```javascript
var out = dtypes( 'floating_point_and_generic' );
// returns [...]
```

</section>

<!-- /.usage -->
Expand Down
26 changes: 26 additions & 0 deletions lib/node_modules/@stdlib/ndarray/dtypes/benchmark/benchmark.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,29 @@ bench( pkg+'::kind', function benchmark( b ) {
b.pass( 'benchmark finished' );
b.end();
});

bench( pkg+'::kind,generic', function benchmark( b ) {
var values;
var out;
var i;

values = [
'floating_point_and_generic',
'integer_and_generic',
'boolean_and_generic'
];

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
out = dtypes( values[ i%values.length ] );
if ( out.length === 0 ) {
b.fail( 'should return a non-empty array' );
}
}
b.toc();
if ( !isStringArray( out ) ) {
b.fail( 'should return an array of strings' );
}
b.pass( 'benchmark finished' );
b.end();
});
27 changes: 25 additions & 2 deletions lib/node_modules/@stdlib/ndarray/dtypes/docs/repl.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,24 @@
{{alias}}( [kind] )
Returns a list of ndarray data types.

When not provided a data type "kind", the function returns an array
containing 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.
- bool: boolean values.
- 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.
- binary: binary.

The function supports the following data type "kinds":

- floating_point: floating-point data types.
Expand All @@ -16,6 +34,9 @@
- typed: typed data types.
- all: all data types.

Additionally, the function supports extending the "kinds" listed above by
appending a '_and_generic' suffix to the kind name (e.g., real_and_generic).

Parameters
----------
kind: string (optional)
Expand All @@ -29,9 +50,11 @@
Examples
--------
> var out = {{alias}}()
<Array>
[...]
> out = {{alias}}( 'floating_point' )
<Array>
[...]
> out = {{alias}}( 'floating_point_and_generic' )
[...]

See Also
--------
Expand Down
1 change: 1 addition & 0 deletions lib/node_modules/@stdlib/ndarray/dtypes/docs/types/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import dtypes = require( './index' );
{
dtypes(); // $ExpectType DataType[]
dtypes( 'floating_point' ); // $ExpectType DataType[]
dtypes( 'floating_point_and_generic' ); // $ExpectType DataType[]
}

// The compiler throws an error if the function is provided an unsupported number of arguments...
Expand Down
24 changes: 22 additions & 2 deletions lib/node_modules/@stdlib/ndarray/dtypes/lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,15 @@

// MODULES //

var replace = require( '@stdlib/string/base/replace' );
var DTYPES = require( './dtypes.json' );


// VARIABLES //

var RE_SUFFIX = /_and_generic$/;


// MAIN //

/**
Expand All @@ -40,12 +46,26 @@ var DTYPES = require( './dtypes.json' );
* // returns [...]
*/
function dtypes() {
var kind;
var out;
var FLG;
if ( arguments.length === 0 ) {
return DTYPES.all.slice();
}
out = DTYPES[ arguments[ 0 ] ];
return ( out ) ? out.slice() : [];
FLG = false;
kind = arguments[ 0 ];
if ( RE_SUFFIX.test( kind ) ) {
kind = replace( kind, RE_SUFFIX, '' );
if ( kind !== 'all' ) {
FLG = true;
}
}
out = DTYPES[ kind ];
out = ( out ) ? out.slice() : [];
if ( FLG && out.length > 0 ) {
out.push( 'generic' );
}
return out;
}


Expand Down
Loading

0 comments on commit d06165b

Please sign in to comment.