Skip to content

Commit 1908bae

Browse files
committed
feat: add support for struct and DataType dtype values
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: passed - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: passed - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed ---
1 parent a83bb3d commit 1908bae

File tree

12 files changed

+298
-40
lines changed

12 files changed

+298
-40
lines changed

lib/node_modules/@stdlib/ndarray/base/bytes-per-element/README.md

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ limitations under the License.
2020

2121
# Bytes per Element
2222

23-
> Return the number of bytes per element provided an underlying [array data type][@stdlib/ndarray/dtypes].
23+
> Return the number of bytes per element for a provided underlying [ndarray data type][@stdlib/ndarray/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,7 +42,7 @@ var bytesPerElement = require( '@stdlib/ndarray/base/bytes-per-element' );
4242

4343
#### bytesPerElement( dtype )
4444

45-
Returns the number of bytes per element provided an underlying [array data type][@stdlib/ndarray/dtypes].
45+
Returns the number of bytes per element for a provided underlying [ndarray data type][@stdlib/ndarray/dtypes].
4646

4747
```javascript
4848
var nbytes = bytesPerElement( 'float64' );
@@ -82,11 +82,7 @@ var nbytes = bytesPerElement( 'foobar' );
8282
```javascript
8383
var bytesPerElement = require( '@stdlib/ndarray/base/bytes-per-element' );
8484

85-
var dtypes;
86-
var nbytes;
87-
var i;
88-
89-
dtypes = [
85+
var dtypes = [
9086
'float64',
9187
'float32',
9288
'int8',
@@ -101,6 +97,8 @@ dtypes = [
10197
'foobar'
10298
];
10399

100+
var nbytes;
101+
var i;
104102
for ( i = 0; i < dtypes.length; i++ ) {
105103
nbytes = bytesPerElement( dtypes[ i ] );
106104
nbytes = ( nbytes ) ? nbytes+' bytes' : 'null';

lib/node_modules/@stdlib/ndarray/base/bytes-per-element/benchmark/benchmark.js

Lines changed: 125 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,15 @@
2121
// MODULES //
2222

2323
var bench = require( '@stdlib/bench' );
24+
var DataType = require( '@stdlib/ndarray/dtype-ctor' );
25+
var structFactory = require( '@stdlib/dstructs/struct' );
2426
var pkg = require( './../package.json' ).name;
2527
var bytesPerElement = require( './../lib' );
2628

2729

2830
// MAIN //
2931

30-
bench( pkg, function benchmark( b ) {
32+
bench( pkg+'::string', function benchmark( b ) {
3133
var dtypes;
3234
var dtype;
3335
var out;
@@ -63,3 +65,125 @@ bench( pkg, function benchmark( b ) {
6365
b.pass( 'benchmark finished' );
6466
b.end();
6567
});
68+
69+
bench( pkg+'::struct', function benchmark( b ) {
70+
var schemas;
71+
var dtypes;
72+
var dtype;
73+
var out;
74+
var i;
75+
76+
schemas = [
77+
[
78+
{
79+
'name': 'foo',
80+
'type': 'float64'
81+
}
82+
],
83+
[
84+
{
85+
'name': 'foo',
86+
'type': 'float32'
87+
}
88+
]
89+
];
90+
91+
dtypes = [
92+
structFactory( schemas[ 0 ] ),
93+
structFactory( schemas[ 1 ] )
94+
];
95+
96+
b.tic();
97+
for ( i = 0; i < b.iterations; i++ ) {
98+
dtype = dtypes[ i%dtypes.length ];
99+
out = bytesPerElement( dtype );
100+
if ( out !== out ) {
101+
b.fail( 'should not return NaN' );
102+
}
103+
}
104+
b.toc();
105+
if ( out !== out ) {
106+
b.fail( 'should not return NaN' );
107+
}
108+
b.pass( 'benchmark finished' );
109+
b.end();
110+
});
111+
112+
bench( pkg+'::data_type_instance,string', function benchmark( b ) {
113+
var dtypes;
114+
var dtype;
115+
var out;
116+
var i;
117+
118+
dtypes = [
119+
new DataType( 'float64' ),
120+
new DataType( 'float32' ),
121+
new DataType( 'int8' ),
122+
new DataType( 'uint8' ),
123+
new DataType( 'uint8c' ),
124+
new DataType( 'int16' ),
125+
new DataType( 'uint16' ),
126+
new DataType( 'int32' ),
127+
new DataType( 'uint32' ),
128+
new DataType( 'binary' ),
129+
new DataType( 'generic' )
130+
];
131+
132+
b.tic();
133+
for ( i = 0; i < b.iterations; i++ ) {
134+
dtype = dtypes[ i%dtypes.length ];
135+
out = bytesPerElement( dtype );
136+
if ( out !== out ) {
137+
b.fail( 'should not return NaN' );
138+
}
139+
}
140+
b.toc();
141+
if ( out !== out ) {
142+
b.fail( 'should not return NaN' );
143+
}
144+
b.pass( 'benchmark finished' );
145+
b.end();
146+
});
147+
148+
bench( pkg+'::data_type_instance,struct', function benchmark( b ) {
149+
var schemas;
150+
var dtypes;
151+
var dtype;
152+
var out;
153+
var i;
154+
155+
schemas = [
156+
[
157+
{
158+
'name': 'foo',
159+
'type': 'float64'
160+
}
161+
],
162+
[
163+
{
164+
'name': 'foo',
165+
'type': 'float32'
166+
}
167+
]
168+
];
169+
170+
dtypes = [
171+
new DataType( structFactory( schemas[ 0 ] ) ),
172+
new DataType( structFactory( schemas[ 1 ] ) )
173+
];
174+
175+
b.tic();
176+
for ( i = 0; i < b.iterations; i++ ) {
177+
dtype = dtypes[ i%dtypes.length ];
178+
out = bytesPerElement( dtype );
179+
if ( out !== out ) {
180+
b.fail( 'should not return NaN' );
181+
}
182+
}
183+
b.toc();
184+
if ( out !== out ) {
185+
b.fail( 'should not return NaN' );
186+
}
187+
b.pass( 'benchmark finished' );
188+
b.end();
189+
});

lib/node_modules/@stdlib/ndarray/base/bytes-per-element/docs/repl.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11

22
{{alias}}( dtype )
3-
Returns the number of bytes per element provided an underlying array data
4-
type.
3+
Returns the number of bytes per element for a provided underlying ndarray
4+
data type.
55

66
If provided an unknown/unsupported data type, the function returns `null`.
77

88
Parameters
99
----------
10-
dtype: string
10+
dtype: string|DataType
1111
Data type.
1212

1313
Returns

lib/node_modules/@stdlib/ndarray/base/bytes-per-element/docs/types/index.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
// TypeScript Version: 4.1
2020

2121
/**
22-
* Returns the number of bytes per element provided an underlying array data type.
22+
* Returns the number of bytes per element for a provided underlying ndarray data type.
2323
*
2424
* @param dtype - data type
2525
* @returns number of bytes per element
@@ -31,7 +31,7 @@
3131
* nbytes = bytesPerElement( 'generic' );
3232
* // returns null
3333
*/
34-
declare function bytesPerElement( dtype: string ): number | null;
34+
declare function bytesPerElement( dtype: any ): number | null;
3535

3636

3737
// EXPORTS //

lib/node_modules/@stdlib/ndarray/base/bytes-per-element/docs/types/test.ts

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,8 @@ import bytesPerElement = require( './index' );
2727
bytesPerElement( 'generic' ); // $ExpectType number | null
2828
}
2929

30-
// The compiler throws an error if the function is provided a value other than a string...
31-
{
32-
bytesPerElement( true ); // $ExpectError
33-
bytesPerElement( false ); // $ExpectError
34-
bytesPerElement( null ); // $ExpectError
35-
bytesPerElement( undefined ); // $ExpectError
36-
bytesPerElement( 5 ); // $ExpectError
37-
bytesPerElement( [] ); // $ExpectError
38-
bytesPerElement( {} ); // $ExpectError
39-
bytesPerElement( ( x: number ): number => x ); // $ExpectError
40-
}
41-
42-
// The compiler throws an error if the function is provided insufficient arguments...
30+
// The compiler throws an error if the function is provided unsupported number of arguments...
4331
{
4432
bytesPerElement(); // $ExpectError
33+
bytesPerElement( 'float64', {} ); // $ExpectError
4534
}

lib/node_modules/@stdlib/ndarray/base/bytes-per-element/examples/index.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,7 @@
2020

2121
var bytesPerElement = require( './../lib' );
2222

23-
var dtypes;
24-
var nbytes;
25-
var i;
26-
27-
dtypes = [
23+
var dtypes = [
2824
'float64',
2925
'float32',
3026
'int8',
@@ -39,6 +35,8 @@ dtypes = [
3935
'foobar'
4036
];
4137

38+
var nbytes;
39+
var i;
4240
for ( i = 0; i < dtypes.length; i++ ) {
4341
nbytes = bytesPerElement( dtypes[ i ] );
4442
nbytes = ( nbytes ) ? nbytes+' bytes' : 'null';

lib/node_modules/@stdlib/ndarray/base/bytes-per-element/include/stdlib/ndarray/base/bytes_per_element.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ enum STDLIB_NDARRAY_BYTES_PER_ELEMENT {
6363
};
6464

6565
/**
66-
* Returns the number of bytes per element for a given data type.
66+
* Returns the number of bytes per element for a provided data type.
6767
*/
6868
int64_t stdlib_ndarray_bytes_per_element( enum STDLIB_NDARRAY_DTYPE dtype );
6969

lib/node_modules/@stdlib/ndarray/base/bytes-per-element/lib/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
'use strict';
2020

2121
/**
22-
* Return the number of bytes per element provided an underlying array data type.
22+
* Return the number of bytes per element for a provided underlying ndarray data type.
2323
*
2424
* @module @stdlib/ndarray/base/bytes-per-element
2525
*

lib/node_modules/@stdlib/ndarray/base/bytes-per-element/lib/main.js

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,26 +20,51 @@
2020

2121
// MODULES //
2222

23+
var isString = require( '@stdlib/assert/is-string' ).isPrimitive;
24+
var isPositiveInteger = require( '@stdlib/assert/is-positive-integer' ).isPrimitive;
2325
var BYTES_PER_ELEMENT = require( './bytes_per_element.json' );
2426

2527

2628
// MAIN //
2729

2830
/**
29-
* Returns the number of bytes per element provided an underlying array data type.
31+
* Returns the number of bytes per element for a provided underlying ndarray data type.
3032
*
31-
* @param {string} dtype - data type
32-
* @returns {(NonNegativeInteger|null)} number of bytes per element
33+
* @param {*} dtype - data type
34+
* @returns {(PositiveInteger|null)} number of bytes per element
3335
*
3436
* @example
3537
* var nbytes = bytesPerElement( 'float64' );
3638
* // returns 8
3739
*
3840
* nbytes = bytesPerElement( 'generic' );
3941
* // returns null
42+
*
43+
* @example
44+
* var structFactory = require( '@stdlib/dstructs/struct' );
45+
*
46+
* var schema = [
47+
* {
48+
* 'name': 'value',
49+
* 'type': 'float64'
50+
* }
51+
* ];
52+
* var Struct = structFactory( schema );
53+
* // returns <Function>
54+
*
55+
* var nbytes = bytesPerElement( Struct );
56+
* // returns 8
4057
*/
4158
function bytesPerElement( dtype ) {
42-
return BYTES_PER_ELEMENT[ dtype ] || null;
59+
var v;
60+
if ( isString( dtype ) ) {
61+
return BYTES_PER_ELEMENT[ dtype ] || null;
62+
}
63+
v = dtype.byteLength;
64+
if ( isPositiveInteger( v ) ) {
65+
return v;
66+
}
67+
return null;
4368
}
4469

4570

lib/node_modules/@stdlib/ndarray/base/bytes-per-element/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@stdlib/ndarray/base/bytes-per-element",
33
"version": "0.0.0",
4-
"description": "Return the number of bytes per element provided an underlying array data type.",
4+
"description": "Return the number of bytes per element for a provided underlying ndarray data type.",
55
"license": "Apache-2.0",
66
"author": {
77
"name": "The Stdlib Authors",

0 commit comments

Comments
 (0)