Skip to content

Commit e31493d

Browse files
committed
Add pkg to return the next larger array data type
1 parent 1b0e26f commit e31493d

File tree

9 files changed

+540
-0
lines changed

9 files changed

+540
-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 array [data type][@stdlib/array/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/array/next-dtype' );
41+
```
42+
43+
#### nextDataType( \[dtype] )
44+
45+
If provided a `dtype` argument, returns the next larger array [data type][@stdlib/array/dtypes] of the same kind.
46+
47+
```javascript
48+
var out = nextDataType( 'float32' );
49+
// returns 'float64'
50+
```
51+
52+
If a [data type][@stdlib/array/dtypes] does not have a next larger [data type][@stdlib/array/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/array/dtypes' );
95+
var nextDataType = require( '@stdlib/array/next-dtype' );
96+
97+
var DTYPES;
98+
var dt;
99+
var i;
100+
101+
// Get the list of supported array 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/array/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/array/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 array 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+
Array 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/array/dtypes' );
22+
var nextDataType = require( './../lib' );
23+
24+
var DTYPES;
25+
var dt;
26+
var i;
27+
28+
// Get the list of supported array 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: 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 array data type of the same kind.
23+
*
24+
* @module @stdlib/array/next-dtype
25+
*
26+
* @example
27+
* var nextDataType = require( '@stdlib/array/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;
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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 getKeys = require( '@stdlib/utils/keys' );
24+
var hasOwnProp = require( '@stdlib/assert/has-own-property' );
25+
var NEXT_DTYPES = require( './next_dtypes.json' );
26+
27+
28+
// FUNCTIONS //
29+
30+
/**
31+
* Generates a table.
32+
*
33+
* @private
34+
* @returns {Object} table
35+
*/
36+
function generateTable() {
37+
var dtypes;
38+
var ntypes;
39+
var out;
40+
var i;
41+
42+
out = {};
43+
dtypes = getKeys( NEXT_DTYPES );
44+
ntypes = dtypes.length;
45+
for ( i = 0; i < ntypes; i++ ) {
46+
out[ dtypes[i] ] = NEXT_DTYPES[ dtypes[i] ];
47+
}
48+
return out;
49+
}
50+
51+
52+
// MAIN //
53+
54+
/**
55+
* Returns the next larger array data type of the same kind.
56+
*
57+
* @param {string} [dtype] - array data type
58+
* @returns {(Object|string|null)} next larger data type(s) or null
59+
*
60+
* @example
61+
* var dt = nextDataType( 'float32' );
62+
* // returns 'float64'
63+
*/
64+
function nextDataType( dtype ) {
65+
if ( arguments.length === 0 ) {
66+
return generateTable();
67+
}
68+
if ( hasOwnProp( NEXT_DTYPES, dtype ) ) {
69+
return NEXT_DTYPES[ dtype ];
70+
}
71+
return null;
72+
}
73+
74+
75+
// EXPORTS //
76+
77+
module.exports = nextDataType;

0 commit comments

Comments
 (0)