Skip to content

Commit 8d28862

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: na - 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: 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: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed ---
1 parent af83db7 commit 8d28862

File tree

9 files changed

+288
-38
lines changed

9 files changed

+288
-38
lines changed

lib/node_modules/@stdlib/ndarray/base/buffer-ctors/README.md

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -77,17 +77,12 @@ var ctor = ctors( 'float' );
7777
<!-- eslint no-undef: "error" -->
7878

7979
```javascript
80+
var logEachMap = require( '@stdlib/console/log-each-map' );
8081
var dtypes = require( '@stdlib/ndarray/dtypes' );
8182
var ctors = require( '@stdlib/ndarray/base/buffer-ctors' );
8283

83-
var DTYPES = dtypes();
84-
85-
var ctor;
86-
var i;
87-
for ( i = 0; i < DTYPES.length; i++ ) {
88-
ctor = ctors( DTYPES[ i ] );
89-
console.log( ctor );
90-
}
84+
var dts = dtypes( 'integer_and_generic' );
85+
logEachMap( '%s => %s', dts, ctors );
9186
```
9287

9388
</section>

lib/node_modules/@stdlib/ndarray/base/buffer-ctors/benchmark/benchmark.js

Lines changed: 104 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,22 @@
2121
// MODULES //
2222

2323
var bench = require( '@stdlib/bench' );
24+
var DataType = require( '@stdlib/ndarray/dtype-ctor' );
2425
var dtypes = require( '@stdlib/ndarray/dtypes' );
26+
var structFactory = require( '@stdlib/dstructs/struct' );
2527
var isFunction = require( '@stdlib/assert/is-function' );
2628
var pkg = require( './../package.json' ).name;
2729
var ctors = require( './../lib' );
2830

2931

3032
// VARIABLES //
3133

32-
var DTYPES = dtypes();
34+
var DTYPES = dtypes( 'integer_and_generic' );
3335

3436

3537
// MAIN //
3638

37-
bench( pkg, function benchmark( b ) {
39+
bench( pkg+'::strings', function benchmark( b ) {
3840
var ctor;
3941
var i;
4042

@@ -52,3 +54,103 @@ bench( pkg, function benchmark( b ) {
5254
b.pass( 'benchmark finished' );
5355
b.end();
5456
});
57+
58+
bench( pkg+'::data_type_instances,strings', function benchmark( b ) {
59+
var values;
60+
var ctor;
61+
var i;
62+
63+
values = [
64+
new DataType( 'float64' ),
65+
new DataType( 'float32' ),
66+
new DataType( 'int32' )
67+
];
68+
69+
b.tic();
70+
for ( i = 0; i < b.iterations; i++ ) {
71+
ctor = ctors( values[ i%values.length ] );
72+
if ( typeof ctor !== 'function' ) {
73+
b.fail( 'should return a function' );
74+
}
75+
}
76+
b.toc();
77+
if ( !isFunction( ctor ) ) {
78+
b.fail( 'should return a function' );
79+
}
80+
b.pass( 'benchmark finished' );
81+
b.end();
82+
});
83+
84+
bench( pkg+'::structs', function benchmark( b ) {
85+
var schema;
86+
var values;
87+
var ctor;
88+
var i;
89+
90+
schema = [
91+
{
92+
'name': 'beep',
93+
'type': 'float64'
94+
},
95+
{
96+
'name': 'boop',
97+
'type': 'int32'
98+
}
99+
];
100+
101+
values = [
102+
structFactory( schema ),
103+
structFactory( schema )
104+
];
105+
106+
b.tic();
107+
for ( i = 0; i < b.iterations; i++ ) {
108+
ctor = ctors( values[ i%values.length ] );
109+
if ( typeof ctor !== 'function' ) {
110+
b.fail( 'should return a function' );
111+
}
112+
}
113+
b.toc();
114+
if ( !isFunction( ctor ) ) {
115+
b.fail( 'should return a function' );
116+
}
117+
b.pass( 'benchmark finished' );
118+
b.end();
119+
});
120+
121+
bench( pkg+'::data_type_instances,structs', function benchmark( b ) {
122+
var schema;
123+
var values;
124+
var ctor;
125+
var i;
126+
127+
schema = [
128+
{
129+
'name': 'beep',
130+
'type': 'float64'
131+
},
132+
{
133+
'name': 'boop',
134+
'type': 'int32'
135+
}
136+
];
137+
138+
values = [
139+
new DataType( structFactory( schema ) ),
140+
new DataType( structFactory( schema ) )
141+
];
142+
143+
b.tic();
144+
for ( i = 0; i < b.iterations; i++ ) {
145+
ctor = ctors( values[ i%values.length ] );
146+
if ( typeof ctor !== 'function' ) {
147+
b.fail( 'should return a function' );
148+
}
149+
}
150+
b.toc();
151+
if ( !isFunction( ctor ) ) {
152+
b.fail( 'should return a function' );
153+
}
154+
b.pass( 'benchmark finished' );
155+
b.end();
156+
});

lib/node_modules/@stdlib/ndarray/base/buffer-ctors/docs/repl.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
Parameters
66
----------
7-
dtype: string
7+
dtype: string|Struct|DataType
88
Data type.
99

1010
Returns

lib/node_modules/@stdlib/ndarray/base/buffer-ctors/docs/types/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
* var ctor = ctors( 'float' );
3333
* // returns null
3434
*/
35-
declare function ctors( dtype: string ): Function | null;
35+
declare function ctors( dtype: any ): Function | null;
3636

3737

3838
// EXPORTS //

lib/node_modules/@stdlib/ndarray/base/buffer-ctors/docs/types/test.ts

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

30-
// The compiler throws an error if the function is provided a value other than a string...
31-
{
32-
ctors( true ); // $ExpectError
33-
ctors( false ); // $ExpectError
34-
ctors( null ); // $ExpectError
35-
ctors( undefined ); // $ExpectError
36-
ctors( 5 ); // $ExpectError
37-
ctors( [] ); // $ExpectError
38-
ctors( {} ); // $ExpectError
39-
ctors( ( 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 an unsupported number of arguments...
4331
{
4432
ctors(); // $ExpectError
33+
ctors( 'float64', {} ); // $ExpectError
4534
}

lib/node_modules/@stdlib/ndarray/base/buffer-ctors/examples/index.js

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,9 @@
1818

1919
'use strict';
2020

21+
var logEachMap = require( '@stdlib/console/log-each-map' );
2122
var dtypes = require( '@stdlib/ndarray/dtypes' );
2223
var ctors = require( './../lib' );
2324

24-
var DTYPES = dtypes();
25-
26-
var ctor;
27-
var i;
28-
for ( i = 0; i < DTYPES.length; i++ ) {
29-
ctor = ctors( DTYPES[ i ] );
30-
console.log( ctor );
31-
}
25+
var dts = dtypes( 'integer_and_generic' );
26+
logEachMap( '%s => %s', dts, ctors );

lib/node_modules/@stdlib/ndarray/base/buffer-ctors/lib/ctors.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,27 @@ var Complex128Array = require( '@stdlib/array/complex128' );
3535
var BooleanArray = require( '@stdlib/array/bool' );
3636

3737

38+
// FUNCTIONS //
39+
40+
/**
41+
* Throws an error.
42+
*
43+
* @private
44+
* @throws {Error} not implemented
45+
*/
46+
function notImplemented() {
47+
throw new Error( 'not implemented' );
48+
}
49+
50+
3851
// MAIN //
3952

4053
// Mapping from data types to underlying buffer constructors...
41-
var ctors = {
54+
var ctors = { // eslint-disable-line vars-on-top
4255
'binary': Buffer,
4356
'float64': Float64Array,
4457
'float32': Float32Array,
58+
'float16': notImplemented, // FIXME: replace with Float16Array constructor once implemented
4559
'generic': Array, // TODO: replace with `stdlib` pkg
4660
'int16': Int16Array,
4761
'int32': Int32Array,
@@ -50,6 +64,7 @@ var ctors = {
5064
'uint32': Uint32Array,
5165
'uint8': Uint8Array,
5266
'uint8c': Uint8ClampedArray,
67+
'complex32': notImplemented, // FIXME: replace with Complex32Array constructor once implemented
5368
'complex64': Complex64Array,
5469
'complex128': Complex128Array,
5570
'bool': BooleanArray

lib/node_modules/@stdlib/ndarray/base/buffer-ctors/lib/main.js

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,24 @@
2020

2121
// MODULES //
2222

23+
var isFunction = require( '@stdlib/assert/is-function' );
24+
var isStructDataType = require( '@stdlib/ndarray/base/assert/is-struct-data-type' );
25+
var structFactory = require( '@stdlib/array/struct-factory' );
2326
var table = require( './ctors.js' );
2427

2528

29+
// VARIABLES //
30+
31+
// Initialize a cache to storing memoized struct array constructors:
32+
var CACHE = {}; // TODO: consider adding/using `@stdlib/array/memoized-struct-factory`; should we need to memoize struct array constructors elsewhere, we should centralize that logic, rather than have multiple caches.
33+
34+
2635
// MAIN //
2736

2837
/**
2938
* Returns an ndarray data buffer constructor.
3039
*
31-
* @param {string} dtype - data type
40+
* @param {*} dtype - data type
3241
* @returns {(Function|null)} data buffer constructor or null
3342
*
3443
* @example
@@ -40,7 +49,37 @@ var table = require( './ctors.js' );
4049
* // returns null
4150
*/
4251
function ctors( dtype ) {
43-
return table[ dtype ] || null;
52+
var struct;
53+
var ctor;
54+
var key;
55+
56+
// Case: string || dtype_instance_with_string
57+
if ( !isFunction( dtype ) ) {
58+
key = String( dtype );
59+
ctor = table[ key ] || null;
60+
if ( ctor ) {
61+
return ctor;
62+
}
63+
}
64+
// Case: struct_ctor || dtype_instance_with_struct_ctor
65+
if ( isStructDataType( dtype ) ) {
66+
if ( key ) {
67+
// Data type instance (note: we assume that `key === struct.layout`):
68+
struct = dtype.value;
69+
} else {
70+
// Struct constructor:
71+
struct = dtype;
72+
key = struct.layout;
73+
}
74+
ctor = CACHE[ key ];
75+
if ( ctor ) {
76+
return ctor;
77+
}
78+
ctor = structFactory( struct );
79+
CACHE[ key ] = ctor;
80+
return ctor;
81+
}
82+
return null;
4483
}
4584

4685

0 commit comments

Comments
 (0)