@stdlib/strided-base-nullary-addon-dispatch 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,197 @@
1
+ /*
2
+ * @license Apache-2.0
3
+ *
4
+ * Copyright (c) 2022 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
+ // TypeScript Version: 2.0
20
+
21
+ /// <reference types="@stdlib/types"/>
22
+
23
+ import { Collection } from '@stdlib/types/object';
24
+
25
+ /**
26
+ * Add-on function.
27
+ *
28
+ * @param N - number of indexed elements
29
+ * @param dtypeX - `x` data type (enumeration constant)
30
+ * @param x - output array
31
+ * @param strideX - `x` stride length
32
+ *
33
+ * @example
34
+ * function addon( N, dtypeX, x, strideX ) {
35
+ * // Call into native add-on...
36
+ * }
37
+ */
38
+ type AddonFcn = ( N: number, dtypeX: number, x: Collection, strideX: number ) => any; // tslint:disable-line:max-line-length
39
+
40
+ /**
41
+ * Fallback function.
42
+ *
43
+ * @param N - number of indexed elements
44
+ * @param dtypeX - `x` data type
45
+ * @param x - output array
46
+ * @param strideX - `x` stride length
47
+ *
48
+ * @example
49
+ * function fallback( N, dtypeX, x, strideX ) {
50
+ * // Fallback JavaScript implementation...
51
+ * }
52
+ */
53
+ type FallbackFcn = ( N: number, dtypeX: any, x: Collection, strideX: number ) => any; // tslint:disable-line:max-line-length
54
+
55
+ /**
56
+ * Fallback function supporting alternative indexing semantics.
57
+ *
58
+ * @param N - number of indexed elements
59
+ * @param dtypeX - `x` data type
60
+ * @param x - output array
61
+ * @param strideX - `x` stride length
62
+ * @param offsetX - starting `x` index
63
+ *
64
+ * @example
65
+ * function fallback( N, dtypeX, x, strideX, offsetX ) {
66
+ * // Fallback JavaScript implementation...
67
+ * }
68
+ */
69
+ type FallbackFcnWithOffsets = ( N: number, dtypeX: any, x: Collection, strideX: number, offsetX: number ) => any; // tslint:disable-line:max-line-length
70
+
71
+ /**
72
+ * Dispatches to a native add-on.
73
+ *
74
+ * @param N - number of indexed elements
75
+ * @param dtypeX - `x` data type
76
+ * @param x - output array
77
+ * @param strideX - `x` stride length
78
+ * @returns `x`
79
+ */
80
+ type Dispatcher = ( N: number, dtypeX: any, x: Collection, strideX: number ) => Collection; // tslint:disable-line:max-line-length
81
+
82
+ /**
83
+ * Dispatches to a native add-on.
84
+ *
85
+ * @param N - number of indexed elements
86
+ * @param dtypeX - `x` data type
87
+ * @param x - output array
88
+ * @param strideX - `x` stride length
89
+ * @param offsetX - starting `x` index
90
+ * @returns `x`
91
+ */
92
+ type DispatcherWithOffsets = ( N: number, dtypeX: any, x: Collection, strideX: number, offsetX: number ) => Collection; // tslint:disable-line:max-line-length
93
+
94
+ /**
95
+ * Interface for creating a native add-on dispatcher.
96
+ */
97
+ interface Dispatch {
98
+ /**
99
+ * Returns a function which dispatches to a native add-on applying a nullary function.
100
+ *
101
+ * @param addon - add-on function
102
+ * @param fallback - fallback function
103
+ * @returns dispatch function
104
+ *
105
+ * @example
106
+ * function addon( N, dtypeX, x, strideX ) {
107
+ * // Call into native add-on...
108
+ * }
109
+ *
110
+ * function fallback( N, dtypeX, x, strideX ) {
111
+ * // Fallback JavaScript implementation...
112
+ * }
113
+ *
114
+ * // Create a dispatch function:
115
+ * var f = dispatch( addon, fallback );
116
+ *
117
+ * // ...
118
+ *
119
+ * // Invoke the dispatch function with strided array arguments:
120
+ * f( 2, 'generic', [ 1, 2 ], 1 );
121
+ */
122
+ ( addon: AddonFcn, fallback: FallbackFcn ): Dispatcher;
123
+
124
+ /**
125
+ * Returns a function which dispatches to a native add-on applying a nullary function using alternative indexing semantics.
126
+ *
127
+ * @param addon - add-on function
128
+ * @param fallback - fallback function
129
+ * @returns dispatch function
130
+ *
131
+ * @example
132
+ * function addon( N, dtypeX, x, strideX ) {
133
+ * // Call into native add-on...
134
+ * }
135
+ *
136
+ * function fallback( N, dtypeX, x, strideX, offsetX ) {
137
+ * // Fallback JavaScript implementation...
138
+ * }
139
+ *
140
+ * // Create a dispatch function:
141
+ * var f = dispatch.ndarray( addon, fallback );
142
+ *
143
+ * // ...
144
+ *
145
+ * // Invoke the dispatch function with strided array arguments:
146
+ * f( 2, 'generic', [ 1, 2 ], 1, 0 );
147
+ */
148
+ ndarray( addon: AddonFcn, fallback: FallbackFcnWithOffsets ): DispatcherWithOffsets; // tslint:disable-line:max-line-length
149
+ }
150
+
151
+ /**
152
+ * Returns a function which dispatches to a native add-on applying a nullary function.
153
+ *
154
+ * @param addon - add-on function
155
+ * @param fallback - fallback function
156
+ * @returns dispatch function
157
+ *
158
+ * @example
159
+ * function addon( N, dtypeX, x, strideX ) {
160
+ * // Call into native add-on...
161
+ * }
162
+ *
163
+ * function fallback( N, dtypeX, x, strideX ) {
164
+ * // Fallback JavaScript implementation...
165
+ * }
166
+ *
167
+ * // Create a dispatch function:
168
+ * var f = dispatch( addon, fallback );
169
+ *
170
+ * // ...
171
+ *
172
+ * // Invoke the dispatch function with strided array arguments:
173
+ * f( 2, 'generic', [ 1, 2 ], 1 );
174
+ *
175
+ * @example
176
+ * function addon( N, dtypeX, x, strideX ) {
177
+ * // Call into native add-on...
178
+ * }
179
+ *
180
+ * function fallback( N, dtypeX, x, strideX, offsetX ) {
181
+ * // Fallback JavaScript implementation...
182
+ * }
183
+ *
184
+ * // Create a dispatch function:
185
+ * var f = dispatch.ndarray( addon, fallback );
186
+ *
187
+ * // ...
188
+ *
189
+ * // Invoke the dispatch function with strided array arguments:
190
+ * f( 2, 'generic', [ 1, 2 ], 1, 0 );
191
+ */
192
+ declare var dispatch: Dispatch;
193
+
194
+
195
+ // EXPORTS //
196
+
197
+ export = dispatch;
@@ -0,0 +1,269 @@
1
+ /*
2
+ * @license Apache-2.0
3
+ *
4
+ * Copyright (c) 2022 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
+ /// <reference types="@stdlib/types"/>
20
+
21
+ import { Collection } from '@stdlib/types/object';
22
+ import dispatch = require( './index' );
23
+
24
+
25
+ // FUNCTIONS //
26
+
27
+ /**
28
+ * Add-on function.
29
+ *
30
+ * @param N - number of indexed elements
31
+ * @param dtypeX - `x` data type (enumeration constant)
32
+ * @param x - output array
33
+ * @param strideX - `x` stride length
34
+ */
35
+ function addon( N: number, dtypeX: number, x: Collection, strideX: number ): void { // tslint:disable-line:max-line-length
36
+ let i;
37
+ if ( dtypeX !== dtypeX ) {
38
+ throw new Error( 'beep' );
39
+ }
40
+ for ( i = 0; i < N; i += 1 ) {
41
+ x[ i * strideX ] = i;
42
+ }
43
+ }
44
+
45
+ /**
46
+ * Fallback function.
47
+ *
48
+ * @param N - number of indexed elements
49
+ * @param dtypeX - `x` data type
50
+ * @param x - output array
51
+ * @param strideX - `x` stride length
52
+ */
53
+ function fallback( N: number, dtypeX: any, x: Collection, strideX: number ): void { // tslint:disable-line:max-line-length
54
+ let i;
55
+ if ( dtypeX !== dtypeX ) {
56
+ throw new Error( 'beep' );
57
+ }
58
+ for ( i = 0; i < N; i += 1 ) {
59
+ x[ i * strideX ] = i;
60
+ }
61
+ }
62
+
63
+ /**
64
+ * Fallback function with alternative indexing semantics.
65
+ *
66
+ * @param N - number of indexed elements
67
+ * @param dtypeX - `x` data type
68
+ * @param x - output array
69
+ * @param strideX - `x` stride length
70
+ * @param offsetX - starting `x` index
71
+ */
72
+ function fallbackWithOffsets( N: number, dtypeX: any, x: Collection, strideX: number, offsetX: number ): void { // tslint:disable-line:max-line-length
73
+ let i;
74
+ if ( dtypeX !== dtypeX ) {
75
+ throw new Error( 'beep' );
76
+ }
77
+ for ( i = 0; i < N; i += 1 ) {
78
+ x[ offsetX + ( i * strideX ) ] = i;
79
+ }
80
+ }
81
+
82
+
83
+ // TESTS //
84
+
85
+ // The function returns a dispatch function...
86
+ {
87
+ dispatch( addon, fallback ); // $ExpectType Dispatcher
88
+ }
89
+
90
+ // The compiler throws an error if not provided a first argument which is an add-on function...
91
+ {
92
+ dispatch( '10', fallback ); // $ExpectError
93
+ dispatch( 10, fallback ); // $ExpectError
94
+ dispatch( true, fallback ); // $ExpectError
95
+ dispatch( false, fallback ); // $ExpectError
96
+ dispatch( null, fallback ); // $ExpectError
97
+ dispatch( undefined, fallback ); // $ExpectError
98
+ dispatch( [], fallback ); // $ExpectError
99
+ dispatch( {}, fallback ); // $ExpectError
100
+ dispatch( ( x: string ): string => x, fallback ); // $ExpectError
101
+ }
102
+
103
+ // The compiler throws an error if not provided a second argument which is a fallback function...
104
+ {
105
+ dispatch( addon, '10' ); // $ExpectError
106
+ dispatch( addon, 10 ); // $ExpectError
107
+ dispatch( addon, true ); // $ExpectError
108
+ dispatch( addon, false ); // $ExpectError
109
+ dispatch( addon, null ); // $ExpectError
110
+ dispatch( addon, undefined ); // $ExpectError
111
+ dispatch( addon, [] ); // $ExpectError
112
+ dispatch( addon, {} ); // $ExpectError
113
+ dispatch( addon, ( x: string ): string => x ); // $ExpectError
114
+ }
115
+
116
+ // The returned function returns a collection...
117
+ {
118
+ const x = new Float64Array( 10 );
119
+
120
+ const f = dispatch( addon, fallback );
121
+
122
+ f( x.length, 'float64', x, 1 ); // $ExpectType Collection
123
+ }
124
+
125
+ // The compiler throws an error if the returned function is not provided a first argument which is a number...
126
+ {
127
+ const x = new Float64Array( 10 );
128
+
129
+ const f = dispatch( addon, fallback );
130
+
131
+ f( '10', 'float64', x, 1 ); // $ExpectError
132
+ f( true, 'float64', x, 1 ); // $ExpectError
133
+ f( false, 'float64', x, 1 ); // $ExpectError
134
+ f( null, 'float64', x, 1 ); // $ExpectError
135
+ f( undefined, 'float64', x, 1 ); // $ExpectError
136
+ f( [], 'float64', x, 1 ); // $ExpectError
137
+ f( {}, 'float64', x, 1 ); // $ExpectError
138
+ f( ( x: number ): number => x, 'float64', x, 1 ); // $ExpectError
139
+ }
140
+
141
+ // The compiler throws an error if the returned function is not provided a third argument which is a collection...
142
+ {
143
+ const x = new Float64Array( 10 );
144
+
145
+ const f = dispatch( addon, fallback );
146
+
147
+ f( x.length, 'float64', true, 1 ); // $ExpectError
148
+ f( x.length, 'float64', false, 1 ); // $ExpectError
149
+ f( x.length, 'float64', null, 1 ); // $ExpectError
150
+ f( x.length, 'float64', undefined, 1 ); // $ExpectError
151
+ f( x.length, 'float64', {}, 1 ); // $ExpectError
152
+ }
153
+
154
+ // The compiler throws an error if the returned function is not provided a fourth argument which is a number...
155
+ {
156
+ const x = new Float64Array( 10 );
157
+
158
+ const f = dispatch( addon, fallback );
159
+
160
+ f( x.length, 'float64', x, '10' ); // $ExpectError
161
+ f( x.length, 'float64', x, true ); // $ExpectError
162
+ f( x.length, 'float64', x, false ); // $ExpectError
163
+ f( x.length, 'float64', x, null ); // $ExpectError
164
+ f( x.length, 'float64', x, undefined ); // $ExpectError
165
+ f( x.length, 'float64', x, [] ); // $ExpectError
166
+ f( x.length, 'float64', x, {} ); // $ExpectError
167
+ f( x.length, 'float64', x, ( x: number ): number => x ); // $ExpectError
168
+ }
169
+
170
+ // Attached to the main export is an `ndarray` method which returns a dispatch function...
171
+ {
172
+ dispatch.ndarray( addon, fallbackWithOffsets ); // $ExpectType DispatcherWithOffsets
173
+ }
174
+
175
+ // The compiler throws an error if the `ndarray` method is not provided a first argument which is an add-on function...
176
+ {
177
+ dispatch.ndarray( '10', fallbackWithOffsets ); // $ExpectError
178
+ dispatch.ndarray( 10, fallbackWithOffsets ); // $ExpectError
179
+ dispatch.ndarray( true, fallbackWithOffsets ); // $ExpectError
180
+ dispatch.ndarray( false, fallbackWithOffsets ); // $ExpectError
181
+ dispatch.ndarray( null, fallbackWithOffsets ); // $ExpectError
182
+ dispatch.ndarray( undefined, fallbackWithOffsets ); // $ExpectError
183
+ dispatch.ndarray( [], fallbackWithOffsets ); // $ExpectError
184
+ dispatch.ndarray( {}, fallbackWithOffsets ); // $ExpectError
185
+ dispatch.ndarray( ( x: string ): string => x, fallbackWithOffsets ); // $ExpectError
186
+ }
187
+
188
+ // The compiler throws an error if the `ndarray` method is not provided a second argument which is a fallback function...
189
+ {
190
+ dispatch.ndarray( addon, '10' ); // $ExpectError
191
+ dispatch.ndarray( addon, 10 ); // $ExpectError
192
+ dispatch.ndarray( addon, true ); // $ExpectError
193
+ dispatch.ndarray( addon, false ); // $ExpectError
194
+ dispatch.ndarray( addon, null ); // $ExpectError
195
+ dispatch.ndarray( addon, undefined ); // $ExpectError
196
+ dispatch.ndarray( addon, [] ); // $ExpectError
197
+ dispatch.ndarray( addon, {} ); // $ExpectError
198
+ dispatch.ndarray( addon, ( x: string ): string => x ); // $ExpectError
199
+ }
200
+
201
+ // The `ndarray` method returns a function which returns a collection...
202
+ {
203
+ const x = new Float64Array( 10 );
204
+
205
+ const f = dispatch.ndarray( addon, fallbackWithOffsets );
206
+
207
+ f( x.length, 'float64', x, 1, 0 ); // $ExpectType Collection
208
+ }
209
+
210
+ // The compiler throws an error if the returned function is not provided a first argument which is a number...
211
+ {
212
+ const x = new Float64Array( 10 );
213
+
214
+ const f = dispatch.ndarray( addon, fallbackWithOffsets );
215
+
216
+ f( '10', 'float64', x, 1, 0 ); // $ExpectError
217
+ f( true, 'float64', x, 1, 0 ); // $ExpectError
218
+ f( false, 'float64', x, 1, 0 ); // $ExpectError
219
+ f( null, 'float64', x, 1, 0 ); // $ExpectError
220
+ f( undefined, 'float64', x, 1, 0 ); // $ExpectError
221
+ f( [], 'float64', x, 1, 0 ); // $ExpectError
222
+ f( {}, 'float64', x, 1, 0 ); // $ExpectError
223
+ f( ( x: number ): number => x, 'float64', x, 1, 0 ); // $ExpectError
224
+ }
225
+
226
+ // The compiler throws an error if the returned function is not provided a third argument which is a collection...
227
+ {
228
+ const x = new Float64Array( 10 );
229
+
230
+ const f = dispatch.ndarray( addon, fallbackWithOffsets );
231
+
232
+ f( x.length, 'float64', true, 1, 0 ); // $ExpectError
233
+ f( x.length, 'float64', false, 1, 0 ); // $ExpectError
234
+ f( x.length, 'float64', null, 1, 0 ); // $ExpectError
235
+ f( x.length, 'float64', undefined, 1, 0 ); // $ExpectError
236
+ f( x.length, 'float64', {}, 1, 0 ); // $ExpectError
237
+ }
238
+
239
+ // The compiler throws an error if the returned function is not provided a fourth argument which is a number...
240
+ {
241
+ const x = new Float64Array( 10 );
242
+
243
+ const f = dispatch.ndarray( addon, fallbackWithOffsets );
244
+
245
+ f( x.length, 'float64', x, '10', 0 ); // $ExpectError
246
+ f( x.length, 'float64', x, true, 0 ); // $ExpectError
247
+ f( x.length, 'float64', x, false, 0 ); // $ExpectError
248
+ f( x.length, 'float64', x, null, 0 ); // $ExpectError
249
+ f( x.length, 'float64', x, undefined, 0 ); // $ExpectError
250
+ f( x.length, 'float64', x, [], 0 ); // $ExpectError
251
+ f( x.length, 'float64', x, {}, 0 ); // $ExpectError
252
+ f( x.length, 'float64', x, ( x: number ): number => x, 0 ); // $ExpectError
253
+ }
254
+
255
+ // The compiler throws an error if the returned function is not provided a fifth argument which is a number...
256
+ {
257
+ const x = new Float64Array( 10 );
258
+
259
+ const f = dispatch.ndarray( addon, fallbackWithOffsets );
260
+
261
+ f( x.length, 'float64', x, 1, '10' ); // $ExpectError
262
+ f( x.length, 'float64', x, 1, true ); // $ExpectError
263
+ f( x.length, 'float64', x, 1, false ); // $ExpectError
264
+ f( x.length, 'float64', x, 1, null ); // $ExpectError
265
+ f( x.length, 'float64', x, 1, undefined ); // $ExpectError
266
+ f( x.length, 'float64', x, 1, [] ); // $ExpectError
267
+ f( x.length, 'float64', x, 1, {} ); // $ExpectError
268
+ f( x.length, 'float64', x, 1, ( x: number ): number => x ); // $ExpectError
269
+ }
package/lib/index.js ADDED
@@ -0,0 +1,60 @@
1
+ /**
2
+ * @license Apache-2.0
3
+ *
4
+ * Copyright (c) 2022 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
+ * Dispatch to a native add-on applying a nullary function.
23
+ *
24
+ * @module @stdlib/strided-base-nullary-addon-dispatch
25
+ *
26
+ * @example
27
+ * var dispatch = require( '@stdlib/strided-base-nullary-addon-dispatch' );
28
+ *
29
+ * function addon( N, dtypeX, x, strideX ) {
30
+ * // Call into native add-on...
31
+ * }
32
+ *
33
+ * function fallback( N, dtypeX, x, strideX ) {
34
+ * // Fallback JavaScript implementation...
35
+ * }
36
+ *
37
+ * // Create a dispatch function:
38
+ * var f = dispatch( addon, fallback );
39
+ *
40
+ * // ...
41
+ *
42
+ * // Invoke the dispatch function with strided array arguments:
43
+ * f( 2, 'generic', [ 1, 2 ], 1 );
44
+ */
45
+
46
+ // MODULES //
47
+
48
+ var setReadOnly = require( '@stdlib/utils-define-nonenumerable-read-only-property' );
49
+ var main = require( './main.js' );
50
+ var ndarray = require( './ndarray.js' );
51
+
52
+
53
+ // MAIN //
54
+
55
+ setReadOnly( main, 'ndarray', ndarray );
56
+
57
+
58
+ // EXPORTS //
59
+
60
+ module.exports = main;
package/lib/main.js ADDED
@@ -0,0 +1,153 @@
1
+ /**
2
+ * @license Apache-2.0
3
+ *
4
+ * Copyright (c) 2022 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 isFunction = require( '@stdlib/assert-is-function' );
24
+ var isTypedArrayLike = require( '@stdlib/assert-is-typed-array-like' );
25
+ var resolve = require( '@stdlib/strided-base-dtype-resolve-enum' );
26
+ var reinterpretComplex64 = require( '@stdlib/strided-base-reinterpret-complex64' );
27
+ var reinterpretComplex128 = require( '@stdlib/strided-base-reinterpret-complex128' );
28
+ var format = require( '@stdlib/string-format' );
29
+
30
+
31
+ // VARIABLES //
32
+
33
+ var COMPLEX64 = resolve( 'complex64' );
34
+ var COMPLEX128 = resolve( 'complex128' );
35
+
36
+
37
+ // MAIN //
38
+
39
+ /**
40
+ * Returns a function which dispatches to a native add-on applying a nullary function.
41
+ *
42
+ * ## Notes
43
+ *
44
+ * - The returned function has the following signature:
45
+ *
46
+ * ```text
47
+ * f( N, dtypeX, x, strideX )
48
+ * ```
49
+ *
50
+ * where
51
+ *
52
+ * - **N**: number of indexed elements.
53
+ * - **dtypeX**: `x` data type.
54
+ * - **x**: output array.
55
+ * - **strideX**: `x` stride length.
56
+ *
57
+ * - The add-on function should have the following signature:
58
+ *
59
+ * ```text
60
+ * f( N, dtypeX, x, strideX )
61
+ * ```
62
+ *
63
+ * where
64
+ *
65
+ * - **N**: number of indexed elements.
66
+ * - **dtypeX**: `x` data type (enumeration constant).
67
+ * - **x**: output array.
68
+ * - **strideX**: `x` stride length.
69
+ *
70
+ * - The fallback function should have the following signature:
71
+ *
72
+ * ```text
73
+ * f( N, dtypeX, x, strideX )
74
+ * ```
75
+ *
76
+ * where
77
+ *
78
+ * - **N**: number of indexed elements.
79
+ * - **dtypeX**: `x` data type.
80
+ * - **x**: output array.
81
+ * - **strideX**: `x` stride length.
82
+ *
83
+ * @param {Function} addon - add-on interface
84
+ * @param {Function} fallback - fallback function
85
+ * @throws {TypeError} first argument must be a function
86
+ * @throws {TypeError} second argument must be a function
87
+ * @returns {Function} dispatch function
88
+ *
89
+ * @example
90
+ * function addon( N, dtypeX, x, strideX ) {
91
+ * // Call into native add-on...
92
+ * }
93
+ *
94
+ * function fallback( N, dtypeX, x, strideX ) {
95
+ * // Fallback JavaScript implementation...
96
+ * }
97
+ *
98
+ * // Create a dispatch function:
99
+ * var f = dispatch( addon, fallback );
100
+ *
101
+ * // ...
102
+ *
103
+ * // Invoke the dispatch function with strided array arguments:
104
+ * f( 2, 'generic', [ 1, 2 ], 1 );
105
+ */
106
+ function dispatch( addon, fallback ) {
107
+ if ( !isFunction( addon ) ) {
108
+ throw new TypeError( format( 'invalid argument. First argument must be a function. Value: `%s`.', addon ) );
109
+ }
110
+ if ( !isFunction( fallback ) ) {
111
+ throw new TypeError( format( 'invalid argument. Second argument must be a function. Value: `%s`.', fallback ) );
112
+ }
113
+ return dispatcher;
114
+
115
+ /**
116
+ * Dispatches to a native add-on.
117
+ *
118
+ * @private
119
+ * @param {integer} N - number of indexed elements
120
+ * @param {*} dtypeX - `x` data type
121
+ * @param {Collection} x - output array
122
+ * @param {integer} strideX - `x` stride length
123
+ * @throws {TypeError} unable to resolve a strided array function supporting the provided array argument data types
124
+ * @returns {Collection} `x`
125
+ */
126
+ function dispatcher( N, dtypeX, x, strideX ) {
127
+ var viewX;
128
+
129
+ // WARNING: we assume that, if we're provided something resembling a typed array, we're provided a typed array; however, this can lead to potential unintended errors as the native add-on may not work with non-typed array objects (e.g., generic arrays)...
130
+ if ( !isTypedArrayLike( x ) ) {
131
+ fallback( N, dtypeX, x, strideX );
132
+ return x;
133
+ }
134
+ dtypeX = resolve( dtypeX );
135
+ if ( dtypeX === null ) {
136
+ throw new TypeError( 'invalid arguments. Unable to resolve a strided array function supporting the provided array argument data types.' );
137
+ }
138
+ if ( dtypeX === COMPLEX64 ) {
139
+ viewX = reinterpretComplex64( x, 0 );
140
+ } else if ( dtypeX === COMPLEX128 ) {
141
+ viewX = reinterpretComplex128( x, 0 );
142
+ } else {
143
+ viewX = x;
144
+ }
145
+ addon( N, dtypeX, viewX, strideX );
146
+ return x;
147
+ }
148
+ }
149
+
150
+
151
+ // EXPORTS //
152
+
153
+ module.exports = dispatch;