Skip to content

Commit f660e38

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: na - task: lint_package_json status: na - task: lint_repl_help status: na - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - 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: na - 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: na - task: lint_typescript_tests status: na - task: lint_license_headers status: passed ---
1 parent fa26ede commit f660e38

File tree

3 files changed

+201
-3
lines changed

3 files changed

+201
-3
lines changed

lib/node_modules/@stdlib/ndarray/base/dtype-resolve-enum/benchmark/benchmark.js

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
var bench = require( '@stdlib/bench' );
2424
var isInteger = require( '@stdlib/assert/is-integer' ).isPrimitive;
2525
var str2enum = require( '@stdlib/ndarray/base/dtype-str2enum' );
26+
var DataType = require( '@stdlib/ndarray/dtype-ctor' );
27+
var structFactory = require( '@stdlib/dstructs/struct' );
2628
var pkg = require( './../package.json' ).name;
2729
var resolve = require( './../lib' );
2830

@@ -82,3 +84,112 @@ bench( pkg+'::integer', function benchmark( b ) {
8284
b.pass( 'benchmark finished' );
8385
b.end();
8486
});
87+
88+
bench( pkg+'::data_type_instance,string', function benchmark( b ) {
89+
var values;
90+
var out;
91+
var i;
92+
93+
values = [
94+
new DataType( 'float64' ),
95+
new DataType( 'float32' ),
96+
new DataType( 'int32' ),
97+
new DataType( 'int8' )
98+
];
99+
100+
b.tic();
101+
for ( i = 0; i < b.iterations; i++ ) {
102+
out = resolve( values[ i%values.length ] );
103+
if ( typeof out !== 'number' ) {
104+
b.fail( 'should return a number' );
105+
}
106+
}
107+
b.toc();
108+
if ( !isInteger( out ) ) {
109+
b.fail( 'should return an integer' );
110+
}
111+
b.pass( 'benchmark finished' );
112+
b.end();
113+
});
114+
115+
bench( pkg+'::struct', function benchmark( b ) {
116+
var schemas;
117+
var values;
118+
var out;
119+
var i;
120+
121+
schemas = [
122+
[
123+
{
124+
'name': 'foo',
125+
'type': 'float64'
126+
}
127+
],
128+
[
129+
{
130+
'name': 'foo',
131+
'type': 'float32'
132+
}
133+
]
134+
];
135+
136+
values = [
137+
structFactory( schemas[ 0 ] ),
138+
structFactory( schemas[ 1 ] )
139+
];
140+
141+
b.tic();
142+
for ( i = 0; i < b.iterations; i++ ) {
143+
out = resolve( values[ i%values.length ] );
144+
if ( typeof out !== 'number' ) {
145+
b.fail( 'should return a number' );
146+
}
147+
}
148+
b.toc();
149+
if ( !isInteger( out ) ) {
150+
b.fail( 'should return an integer' );
151+
}
152+
b.pass( 'benchmark finished' );
153+
b.end();
154+
});
155+
156+
bench( pkg+'::data_type_instance,struct', function benchmark( b ) {
157+
var schemas;
158+
var values;
159+
var out;
160+
var i;
161+
162+
schemas = [
163+
[
164+
{
165+
'name': 'foo',
166+
'type': 'float64'
167+
}
168+
],
169+
[
170+
{
171+
'name': 'foo',
172+
'type': 'float32'
173+
}
174+
]
175+
];
176+
177+
values = [
178+
new DataType( structFactory( schemas[ 0 ] ) ),
179+
new DataType( structFactory( schemas[ 1 ] ) )
180+
];
181+
182+
b.tic();
183+
for ( i = 0; i < b.iterations; i++ ) {
184+
out = resolve( values[ i%values.length ] );
185+
if ( typeof out !== 'number' ) {
186+
b.fail( 'should return a number' );
187+
}
188+
}
189+
b.toc();
190+
if ( !isInteger( out ) ) {
191+
b.fail( 'should return an integer' );
192+
}
193+
b.pass( 'benchmark finished' );
194+
b.end();
195+
});

lib/node_modules/@stdlib/ndarray/base/dtype-resolve-enum/lib/main.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020

2121
// MODULES //
2222

23+
var isStructConstructorLike = require( '@stdlib/assert/is-struct-constructor-like' );
24+
var isInteger = require( '@stdlib/assert/is-integer' ).isPrimitive;
2325
var enum2str = require( '@stdlib/ndarray/base/dtype-enum2str' );
2426
var str2enum = require( '@stdlib/ndarray/base/dtype-str2enum' );
2527

@@ -48,6 +50,12 @@ function resolve( dtype ) {
4850
if ( t === 'string' ) {
4951
return str2enum( dtype );
5052
}
53+
if ( t === 'object' && isInteger( dtype.enum ) ) {
54+
return dtype.enum;
55+
}
56+
if ( isStructConstructorLike( dtype ) ) {
57+
return str2enum( 'userdefined_type' );
58+
}
5159
return null;
5260
}
5361

lib/node_modules/@stdlib/ndarray/base/dtype-resolve-enum/test/test.js

Lines changed: 82 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
// MODULES //
2222

2323
var tape = require( 'tape' );
24+
var DataType = require( '@stdlib/ndarray/dtype-ctor' );
25+
var structFactory = require( '@stdlib/dstructs/struct' );
2426
var str2enum = require( '@stdlib/ndarray/base/dtype-str2enum' );
2527
var resolve = require( './../lib' );
2628

@@ -35,8 +37,6 @@ var DTYPES = [
3537
'uint16',
3638
'int32',
3739
'uint32',
38-
'int64',
39-
'uint64',
4040

4141
'float32',
4242
'float64',
@@ -54,7 +54,7 @@ tape( 'main export is a function', function test( t ) {
5454
t.end();
5555
});
5656

57-
tape( 'the function returns the enumeration constant associated with an ndarray data type value', function test( t ) {
57+
tape( 'the function returns the enumeration constant associated with an ndarray data type value (string)', function test( t ) {
5858
var v;
5959
var i;
6060
for ( i = 0; i < DTYPES.length; i++ ) {
@@ -65,6 +65,85 @@ tape( 'the function returns the enumeration constant associated with an ndarray
6565
t.end();
6666
});
6767

68+
tape( 'the function returns the enumeration constant associated with an ndarray data type value (data type, string)', function test( t ) {
69+
var v;
70+
var i;
71+
for ( i = 0; i < DTYPES.length; i++ ) {
72+
v = str2enum( DTYPES[ i ] );
73+
t.strictEqual( resolve( new DataType( DTYPES[ i ] ) ), v, 'returns expected value' );
74+
t.strictEqual( resolve( v ), v, 'returns expected value' );
75+
}
76+
t.end();
77+
});
78+
79+
tape( 'the function returns the enumeration constant associated with an ndarray data type value (struct)', function test( t ) {
80+
var schemas;
81+
var values;
82+
var v;
83+
var i;
84+
85+
schemas = [
86+
[
87+
{
88+
'name': 'foo',
89+
'type': 'float64'
90+
}
91+
],
92+
[
93+
{
94+
'name': 'foo',
95+
'type': 'float32'
96+
}
97+
]
98+
];
99+
100+
values = [
101+
structFactory( schemas[ 0 ] ),
102+
structFactory( schemas[ 1 ] )
103+
];
104+
105+
for ( i = 0; i < values.length; i++ ) {
106+
v = str2enum( 'userdefined_type' );
107+
t.strictEqual( resolve( values[ i ] ), v, 'returns expected value' );
108+
t.strictEqual( resolve( v ), v, 'returns expected value' );
109+
}
110+
t.end();
111+
});
112+
113+
tape( 'the function returns the enumeration constant associated with an ndarray data type value (data type, struct)', function test( t ) {
114+
var schemas;
115+
var values;
116+
var v;
117+
var i;
118+
119+
schemas = [
120+
[
121+
{
122+
'name': 'foo',
123+
'type': 'float64'
124+
}
125+
],
126+
[
127+
{
128+
'name': 'foo',
129+
'type': 'float32'
130+
}
131+
]
132+
];
133+
134+
values = [
135+
structFactory( schemas[ 0 ] ),
136+
structFactory( schemas[ 1 ] )
137+
];
138+
139+
for ( i = 0; i < values.length; i++ ) {
140+
v = str2enum( 'userdefined_type' );
141+
t.strictEqual( resolve( new DataType( values[ i ] ) ), v, 'returns expected value' );
142+
t.strictEqual( resolve( v ), v, 'returns expected value' );
143+
}
144+
t.end();
145+
});
146+
68147
tape( 'the function returns `null` if unable to resolve an enumeration constant', function test( t ) {
69148
var values;
70149
var i;

0 commit comments

Comments
 (0)