@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.
package/lib/ndarray.js ADDED
@@ -0,0 +1,164 @@
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 isNonNegativeInteger = require( '@stdlib/assert-is-nonnegative-integer' ).isPrimitive;
26
+ var resolve = require( '@stdlib/strided-base-dtype-resolve-enum' );
27
+ var reinterpretComplex64 = require( '@stdlib/strided-base-reinterpret-complex64' );
28
+ var reinterpretComplex128 = require( '@stdlib/strided-base-reinterpret-complex128' );
29
+ var offsetView = require( '@stdlib/strided-base-offset-view' );
30
+ var minViewBufferIndex = require( '@stdlib/strided-base-min-view-buffer-index' );
31
+ var format = require( '@stdlib/string-format' );
32
+
33
+
34
+ // VARIABLES //
35
+
36
+ var COMPLEX64 = resolve( 'complex64' );
37
+ var COMPLEX128 = resolve( 'complex128' );
38
+
39
+
40
+ // MAIN //
41
+
42
+ /**
43
+ * Returns a function which dispatches to a native add-on applying a nullary function using alternative indexing semantics.
44
+ *
45
+ * ## Notes
46
+ *
47
+ * - The returned function has the following signature:
48
+ *
49
+ * ```text
50
+ * f( N, dtypeX, x, strideX, offsetX )
51
+ * ```
52
+ *
53
+ * where
54
+ *
55
+ * - **N**: number of indexed elements.
56
+ * - **dtypeX**: `x` data type.
57
+ * - **x**: output array.
58
+ * - **strideX**: `x` stride length.
59
+ * - **offsetX**: starting `x` index.
60
+ *
61
+ * - The add-on function should have the following signature:
62
+ *
63
+ * ```text
64
+ * f( N, dtypeX, x, strideX )
65
+ * ```
66
+ *
67
+ * where
68
+ *
69
+ * - **N**: number of indexed elements.
70
+ * - **dtypeX**: `x` data type (enumeration constant).
71
+ * - **x**: output array.
72
+ * - **strideX**: `x` stride length.
73
+ *
74
+ * - The fallback function should have the following signature:
75
+ *
76
+ * ```text
77
+ * f( N, dtypeX, x, strideX, offsetX )
78
+ * ```
79
+ *
80
+ * where
81
+ *
82
+ * - **N**: number of indexed elements.
83
+ * - **dtypeX**: `x` data type.
84
+ * - **x**: output array.
85
+ * - **strideX**: `x` stride length.
86
+ * - **offsetX**: starting `x` index.
87
+ *
88
+ * @param {Function} addon - add-on interface
89
+ * @param {Function} fallback - fallback function
90
+ * @throws {TypeError} first argument must be a function
91
+ * @throws {TypeError} second argument must be a function
92
+ * @returns {Function} dispatch function
93
+ *
94
+ * @example
95
+ * function addon( N, dtypeX, x, strideX ) {
96
+ * // Call into native add-on...
97
+ * }
98
+ *
99
+ * function fallback( N, dtypeX, x, strideX, offsetX ) {
100
+ * // Fallback JavaScript implementation...
101
+ * }
102
+ *
103
+ * // Create a dispatch function:
104
+ * var f = dispatch( addon, fallback );
105
+ *
106
+ * // ...
107
+ *
108
+ * // Invoke the dispatch function with strided array arguments:
109
+ * f( 2, 'generic', [ 1, 2 ], 1, 0 );
110
+ */
111
+ function dispatch( addon, fallback ) {
112
+ if ( !isFunction( addon ) ) {
113
+ throw new TypeError( format( 'invalid argument. First argument must be a function. Value: `%s`.', addon ) );
114
+ }
115
+ if ( !isFunction( fallback ) ) {
116
+ throw new TypeError( format( 'invalid argument. Second argument must be a function. Value: `%s`.', fallback ) );
117
+ }
118
+ return dispatcher;
119
+
120
+ /**
121
+ * Dispatches to a native add-on.
122
+ *
123
+ * @private
124
+ * @param {integer} N - number of indexed elements
125
+ * @param {*} dtypeX - `x` data type
126
+ * @param {Collection} x - output array
127
+ * @param {integer} strideX - `x` stride length
128
+ * @param {NonNegativeInteger} offsetX - starting `x` index
129
+ * @throws {TypeError} fifth argument must be a nonnegative integer
130
+ * @throws {TypeError} unable to resolve a strided array function supporting the provided array argument data types
131
+ * @returns {Collection} `x`
132
+ */
133
+ function dispatcher( N, dtypeX, x, strideX, offsetX ) {
134
+ var viewX;
135
+
136
+ // 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)...
137
+ if ( !isTypedArrayLike( x ) ) {
138
+ fallback( N, dtypeX, x, strideX, offsetX );
139
+ return x;
140
+ }
141
+ dtypeX = resolve( dtypeX );
142
+ if ( dtypeX === null ) {
143
+ throw new TypeError( 'invalid arguments. Unable to resolve a strided array function supporting the provided array argument data types.' );
144
+ }
145
+ if ( !isNonNegativeInteger( offsetX ) ) {
146
+ throw new TypeError( format( 'invalid argument. Output array offset must be a nonnegative integer. Value: `%s`.', offsetX ) );
147
+ }
148
+ offsetX = minViewBufferIndex( N, strideX, offsetX );
149
+ if ( dtypeX === COMPLEX64 ) {
150
+ viewX = reinterpretComplex64( x, offsetX );
151
+ } else if ( dtypeX === COMPLEX128 ) {
152
+ viewX = reinterpretComplex128( x, offsetX );
153
+ } else {
154
+ viewX = offsetView( x, offsetX );
155
+ }
156
+ addon( N, dtypeX, viewX, strideX );
157
+ return x;
158
+ }
159
+ }
160
+
161
+
162
+ // EXPORTS //
163
+
164
+ module.exports = dispatch;
package/package.json ADDED
@@ -0,0 +1,95 @@
1
+ {
2
+ "name": "@stdlib/strided-base-nullary-addon-dispatch",
3
+ "version": "0.0.1",
4
+ "description": "Dispatch to a native add-on applying a nullary function.",
5
+ "license": "Apache-2.0",
6
+ "author": {
7
+ "name": "The Stdlib Authors",
8
+ "url": "https://github.com/stdlib-js/stdlib/graphs/contributors"
9
+ },
10
+ "contributors": [
11
+ {
12
+ "name": "The Stdlib Authors",
13
+ "url": "https://github.com/stdlib-js/stdlib/graphs/contributors"
14
+ }
15
+ ],
16
+ "main": "./lib",
17
+ "directories": {
18
+ "benchmark": "./benchmark",
19
+ "doc": "./docs",
20
+ "example": "./examples",
21
+ "lib": "./lib",
22
+ "test": "./test"
23
+ },
24
+ "types": "./docs/types",
25
+ "scripts": {
26
+ "test": "make test",
27
+ "test-cov": "make test-cov",
28
+ "examples": "make examples",
29
+ "benchmark": "make benchmark"
30
+ },
31
+ "homepage": "https://stdlib.io",
32
+ "repository": {
33
+ "type": "git",
34
+ "url": "git://github.com/stdlib-js/strided-base-nullary-addon-dispatch.git"
35
+ },
36
+ "bugs": {
37
+ "url": "https://github.com/stdlib-js/stdlib/issues"
38
+ },
39
+ "dependencies": {
40
+ "@stdlib/assert-is-function": "^0.0.x",
41
+ "@stdlib/assert-is-nonnegative-integer": "^0.0.x",
42
+ "@stdlib/assert-is-typed-array-like": "^0.0.x",
43
+ "@stdlib/strided-base-dtype-resolve-enum": "^0.0.x",
44
+ "@stdlib/strided-base-min-view-buffer-index": "^0.0.x",
45
+ "@stdlib/strided-base-offset-view": "^0.0.x",
46
+ "@stdlib/strided-base-reinterpret-complex128": "^0.0.x",
47
+ "@stdlib/strided-base-reinterpret-complex64": "^0.0.x",
48
+ "@stdlib/string-format": "^0.0.x",
49
+ "@stdlib/types": "^0.0.x",
50
+ "@stdlib/utils-define-nonenumerable-read-only-property": "^0.0.x"
51
+ },
52
+ "devDependencies": {
53
+ "@stdlib/array-complex128": "^0.0.x",
54
+ "@stdlib/array-complex64": "^0.0.x",
55
+ "@stdlib/array-float64": "^0.0.x",
56
+ "@stdlib/assert-has-own-property": "^0.0.x",
57
+ "@stdlib/assert-is-float32array": "^0.0.x",
58
+ "@stdlib/assert-is-float64array": "^0.0.x",
59
+ "@stdlib/bench": "^0.0.x",
60
+ "@stdlib/utils-noop": "^0.0.x",
61
+ "tape": "git+https://github.com/kgryte/tape.git#fix/globby",
62
+ "istanbul": "^0.4.1",
63
+ "tap-min": "git+https://github.com/Planeshifter/tap-min.git"
64
+ },
65
+ "engines": {
66
+ "node": ">=0.10.0",
67
+ "npm": ">2.7.0"
68
+ },
69
+ "os": [
70
+ "aix",
71
+ "darwin",
72
+ "freebsd",
73
+ "linux",
74
+ "macos",
75
+ "openbsd",
76
+ "sunos",
77
+ "win32",
78
+ "windows"
79
+ ],
80
+ "keywords": [
81
+ "stdlib",
82
+ "base",
83
+ "strided",
84
+ "array",
85
+ "dispatch",
86
+ "addon",
87
+ "add-on",
88
+ "native"
89
+ ],
90
+ "__stdlib__": {},
91
+ "funding": {
92
+ "type": "patreon",
93
+ "url": "https://www.patreon.com/athan"
94
+ }
95
+ }