Skip to content

Commit ab3e8d0

Browse files
committed
Add pkg to return the next larger data type of the same kind
1 parent 8cfbe63 commit ab3e8d0

File tree

11 files changed

+646
-0
lines changed

11 files changed

+646
-0
lines changed
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2018 The Stdlib Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
21+
# Next Data Type
22+
23+
> Return the next larger ndarray [data type][@stdlib/ndarray/dtypes] of the same kind.
24+
25+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
26+
27+
<section class="intro">
28+
29+
</section>
30+
31+
<!-- /.intro -->
32+
33+
<!-- Package usage documentation. -->
34+
35+
<section class="usage">
36+
37+
## Usage
38+
39+
```javascript
40+
var nextDataType = require( '@stdlib/ndarray/next-dtype' );
41+
```
42+
43+
#### nextDataType( \[dtype] )
44+
45+
If provided a `dtype` argument, returns the next larger ndarray [data type][@stdlib/ndarray/dtypes] of the same kind.
46+
47+
```javascript
48+
var out = nextDataType( 'float32' );
49+
// returns 'float64'
50+
```
51+
52+
If a [data type][@stdlib/ndarray/dtypes] does not have a next larger [data type][@stdlib/ndarray/dtypes] or the next larger data type is not supported, the function returns `-1`.
53+
54+
```javascript
55+
var out = nextDataType( 'float64' );
56+
// returns -1
57+
```
58+
59+
If not provided a `dtype` argument, the function returns a table.
60+
61+
```javascript
62+
var out = nextDataType();
63+
// returns {...}
64+
```
65+
66+
If provided an unrecognized or unsupported `dtype`, the function returns `null`.
67+
68+
```javascript
69+
var out = nextDataType( 'foo' );
70+
// returns null
71+
```
72+
73+
</section>
74+
75+
<!-- /.usage -->
76+
77+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
78+
79+
<section class="notes">
80+
81+
</section>
82+
83+
<!-- /.notes -->
84+
85+
<!-- Package usage examples. -->
86+
87+
<section class="examples">
88+
89+
## Examples
90+
91+
<!-- eslint no-undef: "error" -->
92+
93+
```javascript
94+
var dtypes = require( '@stdlib/ndarray/dtypes' );
95+
var nextDataType = require( '@stdlib/ndarray/next-dtype' );
96+
97+
var DTYPES;
98+
var dt;
99+
var i;
100+
101+
// Get the list of supported ndarray data types:
102+
DTYPES = dtypes();
103+
104+
// Print the next larger data type for each supported data type...
105+
for ( i = 0; i < DTYPES.length; i++ ) {
106+
dt = nextDataType( DTYPES[ i ] );
107+
console.log( '%s => %s', DTYPES[ i ], dt );
108+
}
109+
```
110+
111+
</section>
112+
113+
<!-- /.examples -->
114+
115+
<!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
116+
117+
<section class="references">
118+
119+
</section>
120+
121+
<!-- /.references -->
122+
123+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
124+
125+
<section class="links">
126+
127+
[@stdlib/ndarray/dtypes]: https://github.com/stdlib-js/stdlib
128+
129+
</section>
130+
131+
<!-- /.links -->
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2018 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var isString = require( '@stdlib/assert/is-string' ).isPrimitive;
25+
var dtypes = require( '@stdlib/ndarray/dtypes' );
26+
var pkg = require( './../package.json' ).name;
27+
var nextDataType = require( './../lib' );
28+
29+
30+
// MAIN //
31+
32+
bench( pkg, function benchmark( b ) {
33+
var out;
34+
var i;
35+
36+
b.tic();
37+
for ( i = 0; i < b.iterations; i++ ) {
38+
out = nextDataType();
39+
if ( typeof out !== 'object' ) {
40+
b.fail( 'should return an object' );
41+
}
42+
}
43+
b.toc();
44+
if ( typeof out !== 'object' ) {
45+
b.fail( 'should return an object' );
46+
}
47+
b.pass( 'benchmark finished' );
48+
b.end();
49+
});
50+
51+
bench( pkg+'::dtype', function benchmark( b ) {
52+
var out;
53+
var dt;
54+
var i;
55+
56+
dt = dtypes();
57+
58+
b.tic();
59+
for ( i = 0; i < b.iterations; i++ ) {
60+
out = nextDataType( dt[ i%dt.length ] );
61+
if ( typeof out !== 'string' && out !== -1 ) {
62+
b.fail( 'should return a string or -1' );
63+
}
64+
}
65+
b.toc();
66+
if ( !isString( out ) && out !== -1 ) {
67+
b.fail( 'should return a string or -1' );
68+
}
69+
b.pass( 'benchmark finished' );
70+
b.end();
71+
});
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
2+
{{alias}}( [dtype] )
3+
Returns the next larger ndarray data type of the same kind.
4+
5+
If not provided a data type, the function returns a table.
6+
7+
If a data type does not have a next larger data type or the next larger type
8+
is not supported, the function returns `-1`.
9+
10+
If provided an unrecognized data type, the function returns `null`.
11+
12+
Parameters
13+
----------
14+
dtype: string (optional)
15+
ndarray data type.
16+
17+
Returns
18+
-------
19+
out: Object|string|null
20+
Next larger type(s).
21+
22+
Examples
23+
--------
24+
> var out = {{alias}}( 'float32' )
25+
'float64'
26+
27+
See Also
28+
--------
29+
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2018 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
var dtypes = require( '@stdlib/ndarray/dtypes' );
22+
var nextDataType = require( './../lib' );
23+
24+
var DTYPES;
25+
var dt;
26+
var i;
27+
28+
// Get the list of supported ndarray data types:
29+
DTYPES = dtypes();
30+
31+
// Print the next larger data type for each supported data type...
32+
for ( i = 0; i < DTYPES.length; i++ ) {
33+
dt = nextDataType( DTYPES[ i ] );
34+
console.log( '%s => %s', DTYPES[ i ], dt );
35+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2018 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
#ifndef STDLIB_NDARRAY_NEXT_DTYPE_H
20+
#define STDLIB_NDARRAY_NEXT_DTYPE_H
21+
22+
#include <stdint.h>
23+
#include "stdlib/ndarray/dtypes.h"
24+
25+
/*
26+
* If C++, prevent name mangling so that the compiler emits a binary file having undecorated names, thus mirroring the behavior of a C compiler.
27+
*/
28+
#ifdef __cplusplus
29+
extern "C" {
30+
#endif
31+
32+
const int8_t STDLIB_NDARRAY_NEXT_DTYPE_FLOAT64 = -1;
33+
const int8_t STDLIB_NDARRAY_NEXT_DTYPE_FLOAT32 = STDLIB_NDARRAY_FLOAT64;
34+
35+
const int8_t STDLIB_NDARRAY_NEXT_DTYPE_UINT64 = -1;
36+
const int8_t STDLIB_NDARRAY_NEXT_DTYPE_INT64 = -1;
37+
38+
const int8_t STDLIB_NDARRAY_NEXT_DTYPE_UINT32 = STDLIB_NDARRAY_UINT64;
39+
const int8_t STDLIB_NDARRAY_NEXT_DTYPE_INT32 = STDLIB_NDARRAY_INT64;
40+
41+
const int8_t STDLIB_NDARRAY_NEXT_DTYPE_UINT16 = STDLIB_NDARRAY_UINT32;
42+
const int8_t STDLIB_NDARRAY_NEXT_DTYPE_INT16 = STDLIB_NDARRAY_INT32;
43+
44+
const int8_t STDLIB_NDARRAY_NEXT_DTYPE_UINT8 = STDLIB_NDARRAY_UINT16;
45+
const int8_t STDLIB_NDARRAY_NEXT_DTYPE_INT8 = STDLIB_NDARRAY_INT16;
46+
47+
// Define a table of containing the above:
48+
const int8_t STDLIB_NDARRAY_NEXT_DTYPE[ STDLIB_NDARRAY_NDTYPES ] = {
49+
50+
[ STDLIB_NDARRAY_INT8 ] = STDLIB_NDARRAY_NEXT_DTYPE_INT8,
51+
[ STDLIB_NDARRAY_UINT8 ] = STDLIB_NDARRAY_NEXT_DTYPE_UINT8,
52+
[ STDLIB_NDARRAY_INT16 ] = STDLIB_NDARRAY_NEXT_DTYPE_INT16,
53+
[ STDLIB_NDARRAY_UINT16 ] = STDLIB_NDARRAY_NEXT_DTYPE_UINT16,
54+
[ STDLIB_NDARRAY_INT32 ] = STDLIB_NDARRAY_NEXT_DTYPE_INT32,
55+
[ STDLIB_NDARRAY_UINT32 ] = STDLIB_NDARRAY_NEXT_DTYPE_UINT32,
56+
[ STDLIB_NDARRAY_INT64 ] = STDLIB_NDARRAY_NEXT_DTYPE_INT64,
57+
[ STDLIB_NDARRAY_UINT64 ] = STDLIB_NDARRAY_NEXT_DTYPE_UINT64,
58+
59+
[ STDLIB_NDARRAY_FLOAT32 ] = STDLIB_NDARRAY_NEXT_DTYPE_FLOAT32,
60+
[ STDLIB_NDARRAY_FLOAT64 ] = STDLIB_NDARRAY_NEXT_DTYPE_FLOAT64
61+
};
62+
63+
#ifdef __cplusplus
64+
}
65+
#endif
66+
67+
#endif // !STDLIB_NDARRAY_NEXT_DTYPE_H
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2018 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
/**
22+
* Return the next larger ndarray data type of the same kind.
23+
*
24+
* @module @stdlib/ndarray/next-dtype
25+
*
26+
* @example
27+
* var nextDataType = require( '@stdlib/ndarray/next-dtype' );
28+
*
29+
* var dt = nextDataType( 'float32' );
30+
* // returns 'float64'
31+
*/
32+
33+
// MODULES //
34+
35+
var nextDataType = require( './main.js' );
36+
37+
38+
// EXPORTS //
39+
40+
module.exports = nextDataType;

0 commit comments

Comments
 (0)