Skip to content

Commit e799140

Browse files
committed
feat: add wasm/base/array2dtype
1 parent f57bee2 commit e799140

File tree

10 files changed

+777
-0
lines changed

10 files changed

+777
-0
lines changed
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2024 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+
# array2dtype
22+
23+
> Return the WebAssembly data type for a provided array.
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 array2dtype = require( '@stdlib/wasm/base/array2dtype' );
41+
```
42+
43+
#### array2dtype( array )
44+
45+
Returns the WebAssembly data type for a provided `array`.
46+
47+
```javascript
48+
var Float64Array = require( '@stdlib/array/float64' );
49+
var arr = new Float64Array( 10 );
50+
51+
var dt = array2dtype( arr );
52+
// returns 'float64'
53+
```
54+
55+
If provided an argument having "generic" or an unknown/unsupported [data type][@stdlib/array/dtypes], the function assumes that array values can be stored as double-precision floating-point numbers and returns `'float64'`.
56+
57+
```javascript
58+
var dt = array2dtype( [] );
59+
// returns 'float64'
60+
```
61+
62+
</section>
63+
64+
<!-- /.usage -->
65+
66+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
67+
68+
<section class="notes">
69+
70+
</section>
71+
72+
<!-- /.notes -->
73+
74+
<!-- Package usage examples. -->
75+
76+
<section class="examples">
77+
78+
## Examples
79+
80+
<!-- eslint no-undef: "error" -->
81+
82+
```javascript
83+
var dtypes = require( '@stdlib/array/dtypes' );
84+
var empty = require( '@stdlib/array/empty' );
85+
var array2dtype = require( '@stdlib/wasm/base/array2dtype' );
86+
87+
// Get a list of supported array data types:
88+
var dt = dtypes();
89+
90+
// For each supported data type, create an array and return its WebAssembly data type...
91+
var v;
92+
var i;
93+
for ( i = 0; i < dt.length; i++ ) {
94+
v = array2dtype( empty( 10, dt[ i ] ) );
95+
console.log( '%s => %s', dt, v );
96+
}
97+
```
98+
99+
</section>
100+
101+
<!-- /.examples -->
102+
103+
<!-- 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. -->
104+
105+
<section class="references">
106+
107+
</section>
108+
109+
<!-- /.references -->
110+
111+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
112+
113+
<section class="related">
114+
115+
</section>
116+
117+
<!-- /.related -->
118+
119+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
120+
121+
<section class="links">
122+
123+
[@stdlib/array/dtypes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/dtypes
124+
125+
</section>
126+
127+
<!-- /.links -->
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 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 dtypes = require( '@stdlib/array/dtypes' );
25+
var empty = require( '@stdlib/array/empty' );
26+
var isString = require( '@stdlib/assert/is-string' ).isPrimitive;
27+
var pkg = require( './../package.json' ).name;
28+
var array2dtype = require( './../lib' );
29+
30+
31+
// VARIABLES //
32+
33+
var DTYPES = dtypes();
34+
35+
36+
// MAIN //
37+
38+
bench( pkg, function benchmark( b ) {
39+
var arrays;
40+
var dt;
41+
var i;
42+
43+
arrays = [];
44+
for ( i = 0; i < DTYPES.length; i++ ) {
45+
arrays.push( empty( 10, DTYPES[ i ] ) );
46+
}
47+
b.tic();
48+
for ( i = 0; i < b.iterations; i++ ) {
49+
dt = array2dtype( arrays[ i%arrays.length ] );
50+
if ( typeof dt !== 'string' ) {
51+
b.fail( 'should return a string' );
52+
}
53+
}
54+
b.toc();
55+
if ( !isString( dt ) ) {
56+
b.fail( 'should return a string' );
57+
}
58+
b.pass( 'benchmark finished' );
59+
b.end();
60+
});
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
2+
{{alias}}( array )
3+
Returns the WebAssembly data type for a provided array.
4+
5+
If provided an argument having an unknown or unsupported type, the function
6+
assumes that values can be stored as double-precision floating-point numbers
7+
and returns 'float64'.
8+
9+
Parameters
10+
----------
11+
array: ArrayLikeObject
12+
Input value.
13+
14+
Returns
15+
-------
16+
out: string
17+
Data type.
18+
19+
Examples
20+
--------
21+
> var arr = new {{alias:@stdlib/array/float64}}( 10 );
22+
> var dt = {{alias}}( arr )
23+
'float64'
24+
> dt = {{alias}}( [] )
25+
'float64'
26+
27+
See Also
28+
--------
29+

0 commit comments

Comments
 (0)