Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 | 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 436x 436x 436x 436x 436x 436x 436x 436x 436x 436x 436x 436x 436x 436x 436x 436x 436x 436x 436x 436x 436x 436x 436x 436x 436x 436x 436x 436x 436x 436x 436x 436x 436x 436x 436x 436x 436x 2921x 2921x 2921x 2921x 1454x 1454x 2921x 436x 436x 436x 436x 436x 436x 4x 2x 2x 2x 2x 432x 432x 432x 432x 18x 18x 414x 414x 432x 2x 1x 1x 1x 1x 412x 412x 412x 412x 430x 40x 40x 56x 40x 40x 56x 40x 40x 40x 40x 40x 20x 20x 20x 20x 372x 372x 372x 372x 436x 164x 164x 164x 164x 164x 164x 76x 76x 40x 40x 36x 36x 76x 40x 40x 36x 36x 76x 76x 76x 76x 76x 76x 76x 38x 38x 38x 38x 88x 88x 88x 92x 84x 84x 42x 42x 42x 42x 164x 164x 212x 212x 212x 220x 204x 102x 102x 102x 102x 8x 436x 4x 4x 4x 436x 13x 13x 13x 13x 13x | /** * @license Apache-2.0 * * Copyright (c) 2025 The Stdlib Authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /* eslint-disable max-len, id-length */ 'use strict'; // MODULES // var iterationOrder = require( '@stdlib/ndarray/base/iteration-order' ); var ndarray2object = require( '@stdlib/ndarray/base/ndarraylike2object' ); var minmaxViewBufferIndex = require( '@stdlib/ndarray/base/minmax-view-buffer-index' ); var format = require( '@stdlib/string/format' ); var numel = require( '@stdlib/ndarray/base/numel' ); var reinterpretBoolean = require( '@stdlib/strided/base/reinterpret-boolean' ); var isBooleanArray = require( '@stdlib/assert/is-booleanarray' ); var blockedaccessormskfilter2d = require( './2d_blocked_accessors.js' ); var blockedaccessormskfilter3d = require( './3d_blocked_accessors.js' ); var blockedaccessormskfilter4d = require( './4d_blocked_accessors.js' ); var blockedaccessormskfilter5d = require( './5d_blocked_accessors.js' ); var blockedaccessormskfilter6d = require( './6d_blocked_accessors.js' ); var blockedaccessormskfilter7d = require( './7d_blocked_accessors.js' ); var blockedaccessormskfilter8d = require( './8d_blocked_accessors.js' ); var blockedaccessormskfilter9d = require( './9d_blocked_accessors.js' ); var blockedaccessormskfilter10d = require( './10d_blocked_accessors.js' ); var blockedmskfilter2d = require( './2d_blocked.js' ); var blockedmskfilter3d = require( './3d_blocked.js' ); var blockedmskfilter4d = require( './4d_blocked.js' ); var blockedmskfilter5d = require( './5d_blocked.js' ); var blockedmskfilter6d = require( './6d_blocked.js' ); var blockedmskfilter7d = require( './7d_blocked.js' ); var blockedmskfilter8d = require( './8d_blocked.js' ); var blockedmskfilter9d = require( './9d_blocked.js' ); var blockedmskfilter10d = require( './10d_blocked.js' ); var accessormskfilter0d = require( './0d_accessors.js' ); var accessormskfilter1d = require( './1d_accessors.js' ); var accessormskfilter2d = require( './2d_accessors.js' ); var accessormskfilter3d = require( './3d_accessors.js' ); var accessormskfilter4d = require( './4d_accessors.js' ); var accessormskfilter5d = require( './5d_accessors.js' ); var accessormskfilter6d = require( './6d_accessors.js' ); var accessormskfilter7d = require( './7d_accessors.js' ); var accessormskfilter8d = require( './8d_accessors.js' ); var accessormskfilter9d = require( './9d_accessors.js' ); var accessormskfilter10d = require( './10d_accessors.js' ); var accessormskfilternd = require( './nd_accessors.js' ); var mskfilter0d = require( './0d.js' ); var mskfilter1d = require( './1d.js' ); var mskfilter2d = require( './2d.js' ); var mskfilter3d = require( './3d.js' ); var mskfilter4d = require( './4d.js' ); var mskfilter5d = require( './5d.js' ); var mskfilter6d = require( './6d.js' ); var mskfilter7d = require( './7d.js' ); var mskfilter8d = require( './8d.js' ); var mskfilter9d = require( './9d.js' ); var mskfilter10d = require( './10d.js' ); var mskfilternd = require( './nd.js' ); // VARIABLES // var MSKFILTER = [ mskfilter0d, mskfilter1d, mskfilter2d, mskfilter3d, mskfilter4d, mskfilter5d, mskfilter6d, mskfilter7d, mskfilter8d, mskfilter9d, mskfilter10d ]; var ACCESSOR_MSKFILTER = [ accessormskfilter0d, accessormskfilter1d, accessormskfilter2d, accessormskfilter3d, accessormskfilter4d, accessormskfilter5d, accessormskfilter6d, accessormskfilter7d, accessormskfilter8d, accessormskfilter9d, accessormskfilter10d ]; var BLOCKED_MSKFILTER = [ blockedmskfilter2d, blockedmskfilter3d, blockedmskfilter4d, blockedmskfilter5d, blockedmskfilter6d, blockedmskfilter7d, blockedmskfilter8d, blockedmskfilter9d, blockedmskfilter10d ]; var BLOCKED_ACCESSOR_MSKFILTER = [ blockedaccessormskfilter2d, blockedaccessormskfilter3d, blockedaccessormskfilter4d, blockedaccessormskfilter5d, blockedaccessormskfilter6d, blockedaccessormskfilter7d, blockedaccessormskfilter8d, blockedaccessormskfilter9d, blockedaccessormskfilter10d ]; var MAX_DIMS = MSKFILTER.length - 1; // MAIN // /** * Applies a mask to a provided input ndarray and assigns unmasked values to elements in a provided one-dimensional output ndarray. * * ## Notes * * - Each provided ndarray should be an `object` with the following properties: * * - **dtype**: data type. * - **data**: data buffer. * - **shape**: dimensions. * - **strides**: stride lengths. * - **offset**: index offset. * - **order**: specifies whether an ndarray is row-major (C-style) or column major (Fortran-style). * * @param {ArrayLikeObject<Object>} arrays - array-like object containing input array, mask array and output array * @throws {Error} input and mask arrays must have the same shape * @throws {Error} output array must have the one dimensional shape * @throws {Error} input and output arrays must have the same data type * @returns {void} * * @example * var Float64Array = require( '@stdlib/array/float64' ); * var Uint8Array = require( '@stdlib/array/uint8' ); * * // Define the shape of the input and mask arrays: * var shape = [ 3, 1, 2 ]; * * // Define the array strides: * var sx = [ 2, 2, 1 ]; * var sm = [ 2, 2, 1 ]; * var sy = [ 1 ]; * * // Define the index offsets: * var ox = 1; * var om = 0; * * // Create the input, mask and output ndarray-like objects: * var x = { * 'dtype': 'float64', * 'data': new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] ), * 'shape': shape, * 'strides': sx, * 'offset': ox, * 'order': 'row-major' * }; * var mask = { * 'dtype': 'uint8', * 'data': new Uint8Array( [ 1, 0, 1, 0, 1, 0 ] ), * 'shape': shape, * 'strides': sm, * 'offset': om, * 'order': 'row-major' * }; * var out = { * 'dtype': 'float64', * 'data': new Float64Array( 3 ), * 'shape': [ 3 ], * 'strides': sy, * 'offset': 0, * 'order': 'row-major' * }; * * mskfilter( [ x, mask, out ] ); * * console.log( out.data ); * // => <Float64Array>[ 2.0, 4.0, 6.0 ] */ function mskfilter( arrays ) { var ndims; var xmmv; var mmmv; var mask; var shm; var shx; var iox; var iom; var len; var ox; var om; var sx; var sm; var ns; var d; var x; var y; var i; // Unpack the ndarrays and standardize ndarray meta data: x = ndarray2object( arrays[ 0 ] ); if ( isBooleanArray( arrays[ 1 ] ) ) { mask = ndarray2object( reinterpretBoolean( arrays[ 1 ], 0 ) ); } else { mask = ndarray2object( arrays[ 1 ] ); } y = ndarray2object( arrays[ 2 ] ); // Verify that the input and mask arrays have the same number of dimensions... shx = x.shape; shm = mask.shape; ndims = shx.length; if ( ndims !== shm.length ) { throw new Error( format( 'invalid arguments. Arrays must have the same number of dimensions (i.e., same rank). ndims(x) == %s. ndims(mask) == %s', ndims, shm.length ) ); } // Verify that the input and mask arrays have the same shape... ns = 0; for ( i = 0; i < ndims; i++ ) { d = shx[i]; if ( d !== shm[ i ] ) { throw new Error( format( 'invalid arguments. Arrays must have the same shape. shape(x)[%s] == %s. shape(mask)[%s] == %s', i, d, i, shm[ i ] ) ); } // Check whether the current dimension is a singleton dimension... if ( d === 1 ) { ns += 1; } } // Verify that the output array has a one-dimensional shape... if ( y.shape.length !== 1 ) { throw new Error( 'invalid argument. Output array must have a one-dimensional shape. ndims(y) == %s', y.shape.length ); } // Determine whether we can avoid iteration altogether... if ( ndims === 0 ) { if ( x.accessorProtocol || mask.accessorProtocol || y.accessorProtocol ) { return ACCESSOR_MSKFILTER[ ndims ]( x, mask, y ); } return MSKFILTER[ ndims ]( x, mask, y ); } // Check whether we were provided an empty input ndarray... len = numel( shx ); if ( len === 0 ) { return; } // Determine whether the ndarrays are one-dimensional and thus readily translate to one-dimensional strided arrays... if ( ndims === 1 ) { if ( x.accessorProtocol || mask.accessorProtocol || y.accessorProtocol ) { return ACCESSOR_MSKFILTER[ ndims ]( x, mask, y ); } return MSKFILTER[ ndims ]( x, mask, y ); } sx = x.strides; sm = mask.strides; // Determine whether the ndarray has only **one** non-singleton dimension (e.g., ndims=4, shape=[10,1,1,1]) so that we can treat the ndarrays as being equivalent to one-dimensional strided arrays... if ( ns === ndims-1 ) { // Get the index of the non-singleton dimension... for ( i = 0; i < ndims; i++ ) { if ( shx[ i ] !== 1 ) { break; } } x.shape = [ shx[i] ]; mask.shape = x.shape; x.strides = [ sx[i] ]; mask.strides = [ sm[i] ]; if ( x.accessorProtocol || mask.accessorProtocol || y.accessorProtocol ) { return ACCESSOR_MSKFILTER[ 1 ]( x, mask, y ); } return MSKFILTER[ 1 ]( x, mask, y ); } iox = iterationOrder( sx ); // +/-1 iom = iterationOrder( sm ); // +/-1 // Determine whether we can avoid blocked iteration... if ( iox !== 0 && iom !== 0 && x.order === mask.order ) { // Determine the minimum and maximum linear indices which are accessible by the array views: xmmv = minmaxViewBufferIndex( shx, sx, x.offset ); mmmv = minmaxViewBufferIndex( shm, sm, mask.offset ); // Determine whether we can ignore shape (and strides) and treat the ndarrays as linear one-dimensional strided arrays... if ( len === ( xmmv[1]-xmmv[0]+1 ) && len === ( mmmv[1]-mmmv[0]+1 ) ) { // Note: the above is equivalent to @stdlib/ndarray/base/assert/is-contiguous, but in-lined so we can retain computed values... if ( iox === 1 ) { ox = xmmv[ 0 ]; } else { ox = xmmv[ 1 ]; } if ( iom === 1 ) { om = mmmv[ 0 ]; } else { om = mmmv[ 1 ]; } x.shape = [ len ]; mask.shape = x.shape; x.strides = [ iox ]; mask.strides = [ iom ]; x.offset = ox; mask.offset = om; if ( x.accessorProtocol || mask.accessorProtocol || y.accessorProtocol ) { return ACCESSOR_MSKFILTER[ 1 ]( x, mask, y ); } return MSKFILTER[ 1 ]( x, mask, y ); } // At least one ndarray is non-contiguous, so we cannot directly use one-dimensional array functionality... // Determine whether we can use simple nested loops... if ( ndims <= MAX_DIMS ) { // So long as iteration for each respective array always moves in the same direction (i.e., no mixed sign strides), we can leverage cache-optimal (i.e., normal) nested loops without resorting to blocked iteration... if ( x.accessorProtocol || mask.accessorProtocol || y.accessorProtocol ) { return ACCESSOR_MSKFILTER[ ndims ]( x, mask, y ); } return MSKFILTER[ ndims ]( x, mask, y ); } // Fall-through to blocked iteration... } // At this point, we're either dealing with non-contiguous n-dimensional arrays, high dimensional n-dimensional arrays, and/or arrays having differing memory layouts, so our only hope is that we can still perform blocked iteration... // Determine whether we can perform blocked iteration... if ( ndims <= MAX_DIMS ) { if ( x.accessorProtocol || mask.accessorProtocol || y.accessorProtocol ) { return BLOCKED_ACCESSOR_MSKFILTER[ ndims-2 ]( x, mask, y ); } return BLOCKED_MSKFILTER[ ndims-2 ]( x, mask, y ); } // Fall-through to linear view iteration without regard for how data is stored in memory (i.e., take the slow path)... if ( x.accessorProtocol || mask.accessorProtocol || y.accessorProtocol ) { return accessormskfilternd( x, mask, y ); } mskfilternd( x, mask, y ); } // EXPORTS // module.exports = mskfilter; |