|
| 1 | +<!-- |
| 2 | +
|
| 3 | +@license Apache-2.0 |
| 4 | +
|
| 5 | +Copyright (c) 2025 The Stdlib Authors. |
| 6 | +
|
| 7 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | +you may not use this file except in compliance with the License. |
| 9 | +You may obtain a copy of the License at |
| 10 | +
|
| 11 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +
|
| 13 | +Unless required by applicable law or agreed to in writing, software |
| 14 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | +See the License for the specific language governing permissions and |
| 17 | +limitations under the License. |
| 18 | +
|
| 19 | +--> |
| 20 | + |
| 21 | +# dispatch |
| 22 | + |
| 23 | +> Dispatch to a native add-on applying a unary function to an input ndarray. |
| 24 | +
|
| 25 | +<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. --> |
| 26 | + |
| 27 | +<section class="intro"> |
| 28 | + |
| 29 | +</section> |
| 30 | + |
| 31 | +<!-- /.intro --> |
| 32 | + |
| 33 | +<!-- Package usage documentation. --> |
| 34 | + |
| 35 | +<section class="usage"> |
| 36 | + |
| 37 | +## Usage |
| 38 | + |
| 39 | +```javascript |
| 40 | +var dispatch = require( '@stdlib/ndarray/base/unary-addon-dispatch' ); |
| 41 | +``` |
| 42 | + |
| 43 | +#### dispatch( addon, fallback ) |
| 44 | + |
| 45 | +Returns a function which dispatches to a native add-on applying a unary function to an input ndarray. |
| 46 | + |
| 47 | +```javascript |
| 48 | +var array = require( '@stdlib/ndarray/array' ); |
| 49 | +var zeros = require( '@stdlib/ndarray/zeros' ); |
| 50 | +var dispatch = require( '@stdlib/ndarray/base/unary-addon-dispatch' ); |
| 51 | + |
| 52 | +function addon( x, metaX, y, metaY ) { |
| 53 | + // Call into native add-on... |
| 54 | +} |
| 55 | + |
| 56 | +function fallback( x, y ) { |
| 57 | + // Fallback JavaScript implementation... |
| 58 | +} |
| 59 | + |
| 60 | +// Create a dispatch function: |
| 61 | +var f = dispatch( addon, fallback ); |
| 62 | + |
| 63 | +// ... |
| 64 | + |
| 65 | +// Invoke the dispatch function with ndarray arguments: |
| 66 | +var x = array( [ [ 1, 2], [ 3, 4 ] ] ); |
| 67 | +var y = zeros( [ 2, 2 ] ); |
| 68 | +f( x, y ); |
| 69 | +``` |
| 70 | + |
| 71 | +The returned function has the following signature: |
| 72 | + |
| 73 | +```text |
| 74 | +f( x, y ) |
| 75 | +``` |
| 76 | + |
| 77 | +where |
| 78 | + |
| 79 | +- **x**: input ndarray. |
| 80 | +- **y**: output ndarray. |
| 81 | + |
| 82 | +The `addon` function should have the following signature: |
| 83 | + |
| 84 | +```text |
| 85 | +f( xbuf, metaX, ybuf, metaY ) |
| 86 | +``` |
| 87 | + |
| 88 | +where |
| 89 | + |
| 90 | +- **xbuf**: input ndarray data buffer. |
| 91 | +- **metaX**: [serialized][@stdlib/ndarray/base/serialize-meta-data] input ndarray meta data. |
| 92 | +- **ybuf**: output ndarray data buffer. |
| 93 | +- **metaY**: [serialized][@stdlib/ndarray/base/serialize-meta-data] output ndarray meta data. |
| 94 | + |
| 95 | +The `fallback` function should have the following signature: |
| 96 | + |
| 97 | +```text |
| 98 | +f( x, y ) |
| 99 | +``` |
| 100 | + |
| 101 | +where |
| 102 | + |
| 103 | +- **x**: input ndarray. |
| 104 | +- **y**: output ndarray. |
| 105 | + |
| 106 | +</section> |
| 107 | + |
| 108 | +<!-- /.usage --> |
| 109 | + |
| 110 | +<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 111 | + |
| 112 | +<section class="notes"> |
| 113 | + |
| 114 | +## Notes |
| 115 | + |
| 116 | +- To determine whether to dispatch to the `addon` function, the returned dispatch function checks whether the underlying ndarray data buffers are typed arrays. If the data buffers are typed arrays, the dispatch function invokes the `addon` function; otherwise, the dispatch function invokes the `fallback` function. |
| 117 | + |
| 118 | +</section> |
| 119 | + |
| 120 | +<!-- /.notes --> |
| 121 | + |
| 122 | +<!-- Package usage examples. --> |
| 123 | + |
| 124 | +<section class="examples"> |
| 125 | + |
| 126 | +## Examples |
| 127 | + |
| 128 | +<!-- eslint-disable max-len --> |
| 129 | + |
| 130 | +<!-- eslint no-undef: "error" --> |
| 131 | + |
| 132 | +```javascript |
| 133 | +var array = require( '@stdlib/ndarray/array' ); |
| 134 | +var zeros = require( '@stdlib/ndarray/zeros' ); |
| 135 | +var ndarray2array = require( '@stdlib/ndarray/to-array' ); |
| 136 | +var dispatch = require( '@stdlib/ndarray/base/unary-addon-dispatch' ); |
| 137 | + |
| 138 | +function addon( xbuf, metaX, ybuf, metaY ) { |
| 139 | + console.log( xbuf ); |
| 140 | + // => <Float64Array>[ 1, 2, 3, 4 ] |
| 141 | + |
| 142 | + console.log( ybuf ); |
| 143 | + // => <Float64Array>[ 0, 0, 0, 0 ] |
| 144 | +} |
| 145 | + |
| 146 | +function fallback( x, y ) { |
| 147 | + console.log( ndarray2array( x ) ); |
| 148 | + // => [ [ 1, 2 ], [ 3, 4 ] ] |
| 149 | + |
| 150 | + console.log( ndarray2array( y ) ); |
| 151 | + // => [ [ 0, 0 ], [ 0, 0 ] ] |
| 152 | +} |
| 153 | + |
| 154 | +// Create a dispatch function: |
| 155 | +var f = dispatch( addon, fallback ); |
| 156 | + |
| 157 | +// Create ndarrays: |
| 158 | +var opts = { |
| 159 | + 'dtype': 'float64', |
| 160 | + 'casting': 'unsafe' |
| 161 | +}; |
| 162 | +var x = array( [ [ 1, 2 ], [ 3, 4 ] ], opts ); |
| 163 | +var y = zeros( [ 2, 2 ], opts ); |
| 164 | + |
| 165 | +// Dispatch to the add-on function: |
| 166 | +f( x, y ); |
| 167 | + |
| 168 | +// Define new ndarrays: |
| 169 | +opts = { |
| 170 | + 'dtype': 'generic' |
| 171 | +}; |
| 172 | +x = array( [ [ 1, 2 ], [ 3, 4 ] ], opts ); |
| 173 | +y = zeros( [ 2, 2 ], opts ); |
| 174 | + |
| 175 | +// Dispatch to the fallback function: |
| 176 | +f( x, y ); |
| 177 | +``` |
| 178 | + |
| 179 | +</section> |
| 180 | + |
| 181 | +<!-- /.examples --> |
| 182 | + |
| 183 | +<!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 184 | + |
| 185 | +<section class="references"> |
| 186 | + |
| 187 | +</section> |
| 188 | + |
| 189 | +<!-- /.references --> |
| 190 | + |
| 191 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 192 | + |
| 193 | +<section class="related"> |
| 194 | + |
| 195 | +</section> |
| 196 | + |
| 197 | +<!-- /.related --> |
| 198 | + |
| 199 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 200 | + |
| 201 | +<section class="links"> |
| 202 | + |
| 203 | +[@stdlib/ndarray/base/serialize-meta-data]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/serialize-meta-data |
| 204 | + |
| 205 | +</section> |
| 206 | + |
| 207 | +<!-- /.links --> |
0 commit comments