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 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 | 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 2x 2x 2x 2x 2x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 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 447x 447x 447x 447x 447x 447x 447x 447x 447x 447x 447x 447x 447x 447x 447x 447x 447x 447x 447x 447x 447x 447x 447x 447x 447x 447x 1x 1x 447x 1x 1x 1x 445x 445x 447x 447x 447x 447x 447x 447x 447x 2x 1x 1x 1x 1x 445x 445x 445x 445x 2976x 2976x 4x 4x 2972x 2972x 2972x 2972x 2976x 1806x 1806x 2976x 441x 445x 21x 21x 420x 444x 3x 1x 1x 2x 2x 417x 417x 417x 417x 442x 40x 40x 147x 40x 40x 147x 40x 40x 40x 40x 40x 20x 20x 20x 20x 377x 377x 377x 377x 377x 447x 121x 121x 121x 121x 121x 121x 81x 81x 41x 41x 40x 40x 81x 41x 41x 40x 40x 81x 81x 81x 81x 81x 81x 81x 40x 40x 41x 41x 40x 40x 40x 48x 36x 36x 18x 18x 18x 18x 121x 121x 260x 260x 260x 274x 252x 126x 126x 126x 126x 8x 447x 4x 4x 4x 447x 13x 13x 13x 13x 13x | /**
* @license Apache-2.0
*
* Copyright (c) 2023 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.
*/
'use strict';
// MODULES //
var isComplexDataType = require( '@stdlib/ndarray/base/assert/is-complex-floating-point-data-type' );
var isRealDataType = require( '@stdlib/ndarray/base/assert/is-real-data-type' );
var isComplexArray = require( '@stdlib/array/base/assert/is-complex-typed-array' );
var isBooleanArray = require( '@stdlib/array/base/assert/is-booleanarray' );
var iterationOrder = require( '@stdlib/ndarray/base/iteration-order' );
var strides2order = require( '@stdlib/ndarray/base/strides2order' );
var castReturn = require( '@stdlib/complex/base/cast-return' );
var complexCtors = require( '@stdlib/complex/ctors' );
var minmaxViewBufferIndex = require( '@stdlib/ndarray/base/minmax-view-buffer-index' );
var ndarray2object = require( '@stdlib/ndarray/base/ndarraylike2object' );
var reinterpretComplex = require( '@stdlib/strided/base/reinterpret-complex' );
var reinterpretBoolean = require( '@stdlib/strided/base/reinterpret-boolean' );
var gscal = require( '@stdlib/blas/base/gscal' );
var blockedaccessorassign2d = require( './2d_blocked_accessors.js' );
var blockedaccessorassign3d = require( './3d_blocked_accessors.js' );
var blockedaccessorassign4d = require( './4d_blocked_accessors.js' );
var blockedaccessorassign5d = require( './5d_blocked_accessors.js' );
var blockedaccessorassign6d = require( './6d_blocked_accessors.js' );
var blockedaccessorassign7d = require( './7d_blocked_accessors.js' );
var blockedaccessorassign8d = require( './8d_blocked_accessors.js' );
var blockedaccessorassign9d = require( './9d_blocked_accessors.js' );
var blockedaccessorassign10d = require( './10d_blocked_accessors.js' );
var blockedassign2d = require( './2d_blocked.js' );
var blockedassign3d = require( './3d_blocked.js' );
var blockedassign4d = require( './4d_blocked.js' );
var blockedassign5d = require( './5d_blocked.js' );
var blockedassign6d = require( './6d_blocked.js' );
var blockedassign7d = require( './7d_blocked.js' );
var blockedassign8d = require( './8d_blocked.js' );
var blockedassign9d = require( './9d_blocked.js' );
var blockedassign10d = require( './10d_blocked.js' );
var accessorassign0d = require( './0d_accessors.js' );
var accessorassign1d = require( './1d_accessors.js' );
var accessorassign2d = require( './2d_accessors.js' );
var accessorassign3d = require( './3d_accessors.js' );
var accessorassign4d = require( './4d_accessors.js' );
var accessorassign5d = require( './5d_accessors.js' );
var accessorassign6d = require( './6d_accessors.js' );
var accessorassign7d = require( './7d_accessors.js' );
var accessorassign8d = require( './8d_accessors.js' );
var accessorassign9d = require( './9d_accessors.js' );
var accessorassign10d = require( './10d_accessors.js' );
var accessorassignnd = require( './nd_accessors.js' );
var assign0d = require( './0d.js' );
var assign1d = require( './1d.js' );
var assign2d = require( './2d.js' );
var assign3d = require( './3d.js' );
var assign4d = require( './4d.js' );
var assign5d = require( './5d.js' );
var assign6d = require( './6d.js' );
var assign7d = require( './7d.js' );
var assign8d = require( './8d.js' );
var assign9d = require( './9d.js' );
var assign10d = require( './10d.js' );
var assignnd = require( './nd.js' );
// VARIABLES //
var ASSIGN = [
assign0d,
assign1d,
assign2d,
assign3d,
assign4d,
assign5d,
assign6d,
assign7d,
assign8d,
assign9d,
assign10d
];
var ACCESSOR_ASSIGN = [
accessorassign0d,
accessorassign1d,
accessorassign2d,
accessorassign3d,
accessorassign4d,
accessorassign5d,
accessorassign6d,
accessorassign7d,
accessorassign8d,
accessorassign9d,
accessorassign10d
];
var BLOCKED_ASSIGN = [
blockedassign2d, // 0
blockedassign3d,
blockedassign4d,
blockedassign5d,
blockedassign6d,
blockedassign7d,
blockedassign8d,
blockedassign9d,
blockedassign10d // 8
];
var BLOCKED_ACCESSOR_ASSIGN = [
blockedaccessorassign2d, // 0
blockedaccessorassign3d,
blockedaccessorassign4d,
blockedaccessorassign5d,
blockedaccessorassign6d,
blockedaccessorassign7d,
blockedaccessorassign8d,
blockedaccessorassign9d,
blockedaccessorassign10d // 8
];
var MAX_DIMS = ASSIGN.length - 1;
// TODO: consider adding a package utility for mapping a complex dtype to its complementary real-valued counterpart
var COMPLEX_TO_REAL = { // WARNING: this table needs to be manually updated if we add support for additional complex number dtypes
'complex128': 'float64',
'complex64': 'float32',
'complex32': 'float16'
};
// FUNCTIONS //
/**
* Converts a boolean ndarray to an 8-bit unsigned integer ndarray.
*
* ## Notes
*
* - The function mutates the input ndarray object.
*
* @private
* @param {Object} x - input ndarray object
* @returns {Object} output ndarray object
*/
function boolean2uint8( x ) {
x.data = reinterpretBoolean( x.data, 0 );
x.accessorProtocol = false;
return x;
}
/**
* Converts a complex-valued floating-point ndarray to a real-valued floating-point ndarray.
*
* ## Notes
*
* - The function mutates the input ndarray object.
*
* @private
* @param {Object} x - input ndarray object
* @returns {Object} output ndarray object
*/
function complex2real( x ) {
var ndims = x.shape.length;
x.data = reinterpretComplex( x.data, 0 );
x.accessorProtocol = false;
x.dtype = COMPLEX_TO_REAL[ String( x.dtype ) ];
x.strides = gscal( ndims, 2, x.strides, 1 );
x.offset *= 2;
// Append a trailing dimension where each element is the real and imaginary component for a corresponding element in the original input ndarray (note: this means that a two-dimensional complex-valued ndarray becomes a three-dimensional real-valued ndarray; while this does entail additional loop overhead, it is still significantly faster than sending complex-valued ndarrays down the accessor path):
x.shape.push( 2 ); // real and imaginary components
// Augment the strides, where we assume that real and imaginary components are adjacent in memory...
if ( ndims === 0 ) {
x.strides[ 0 ] = 1;
} else {
x.strides.push( 1 );
}
return x;
}
// MAIN //
/**
* Assigns elements in an input ndarray to elements in an 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 one input array and one output array
* @throws {Error} arrays must have the same number of dimensions
* @throws {Error} arrays must have the same shape
* @returns {void}
*
* @example
* var Float64Array = require( '@stdlib/array/float64' );
*
* // Create data buffers:
* var xbuf = 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 ] );
* var ybuf = new Float64Array( 6 );
*
* // Define the shape of the input and output arrays:
* var shape = [ 3, 1, 2 ];
*
* // Define the array strides:
* var sx = [ 4, 4, 1 ];
* var sy = [ 2, 2, 1 ];
*
* // Define the index offsets:
* var ox = 1;
* var oy = 0;
*
* // Create the input and output ndarray-like objects:
* var x = {
* 'dtype': 'float64',
* 'data': xbuf,
* 'shape': shape,
* 'strides': sx,
* 'offset': ox,
* 'order': 'row-major'
* };
* var y = {
* 'dtype': 'float64',
* 'data': ybuf,
* 'shape': shape,
* 'strides': sy,
* 'offset': oy,
* 'order': 'row-major'
* };
*
* // Copy elements:
* assign( [ x, y ] );
*
* console.log( y.data );
* // => <Float64Array>[ 2.0, 3.0, 6.0, 7.0, 10.0, 11.0 ]
*/
function assign( arrays ) {
var ndims;
var xmmv;
var ymmv;
var shx;
var shy;
var iox;
var ioy;
var len;
var ord;
var sx;
var sy;
var ox;
var oy;
var ns;
var x;
var y;
var d;
var i;
// Unpack the ndarrays and standardize ndarray meta data:
x = ndarray2object( arrays[ 0 ] );
y = ndarray2object( arrays[ 1 ] );
// Check for known array types which can be reinterpreted for better iteration performance...
if ( isBooleanArray( x.data ) && isBooleanArray( y.data ) ) {
x = boolean2uint8( x );
y = boolean2uint8( y );
} else if ( isComplexArray( x.data ) && isComplexArray( y.data ) ) {
x = complex2real( x );
y = complex2real( y );
}
// Determine whether we are casting a real data type to a complex data type and we need to use a specialized accessor (note: we don't support the other way, complex-to-real, as this is not an allowed (mostly) safe cast; note: we cannot create a specialized view for assigning only real components, as the imaginary component for each element in `y` also needs to be set to zero and while we could perform two passes, it's not clear it's worth the effort)...
else if ( isRealDataType( x.dtype ) && isComplexDataType( y.dtype ) ) {
x.accessorProtocol = true;
x.accessors[ 0 ] = castReturn( x.accessors[ 0 ], 2, complexCtors( String( y.dtype ) ) ); // eslint-disable-line max-len
}
// Verify that the input and output arrays have the same number of dimensions...
shx = x.shape;
shy = y.shape;
ndims = shx.length;
if ( ndims !== shy.length ) {
throw new Error( 'invalid arguments. Arrays must have the same number of dimensions (i.e., same rank). ndims(x) == '+ndims+'. ndims(y) == '+shy.length+'.' );
}
// Determine whether we can avoid iteration altogether...
if ( ndims === 0 ) {
if ( x.accessorProtocol || y.accessorProtocol ) {
return ACCESSOR_ASSIGN[ ndims ]( x, y );
}
return ASSIGN[ ndims ]( x, y );
}
// Verify that the input and output arrays have the same dimensions...
len = 1; // number of elements
ns = 0; // number of singleton dimensions
for ( i = 0; i < ndims; i++ ) {
d = shx[ i ];
if ( d !== shy[ i ] ) {
throw new Error( 'invalid arguments. Arrays must have the same shape.' );
}
// Note that, if one of the dimensions is `0`, the length will be `0`...
len *= d;
// Check whether the current dimension is a singleton dimension...
if ( d === 1 ) {
ns += 1;
}
}
// Check whether we were provided empty ndarrays...
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 || y.accessorProtocol ) {
return ACCESSOR_ASSIGN[ ndims ]( x, y );
}
return ASSIGN[ ndims ]( x, y );
}
sx = x.strides;
sy = y.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] ];
y.shape = x.shape;
x.strides = [ sx[i] ];
y.strides = [ sy[i] ];
if ( x.accessorProtocol || y.accessorProtocol ) {
return ACCESSOR_ASSIGN[ 1 ]( x, y );
}
return ASSIGN[ 1 ]( x, y );
}
iox = iterationOrder( sx ); // +/-1
ioy = iterationOrder( sy ); // +/-1
// Determine whether we can avoid blocked iteration...
ord = strides2order( sx );
if ( iox !== 0 && ioy !== 0 && ord === strides2order( sy ) ) {
// Determine the minimum and maximum linear indices which are accessible by the array views:
xmmv = minmaxViewBufferIndex( shx, sx, x.offset );
ymmv = minmaxViewBufferIndex( shy, sy, y.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 === ( ymmv[1]-ymmv[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 ( ioy === 1 ) {
oy = ymmv[ 0 ];
} else {
oy = ymmv[ 1 ];
}
x.shape = [ len ];
y.shape = x.shape;
x.strides = [ iox ];
y.strides = [ ioy ];
x.offset = ox;
y.offset = oy;
if ( x.accessorProtocol || y.accessorProtocol ) {
return ACCESSOR_ASSIGN[ 1 ]( x, y );
}
return ASSIGN[ 1 ]( x, 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 || y.accessorProtocol ) {
return ACCESSOR_ASSIGN[ ndims ]( x, y, ord === 1 );
}
return ASSIGN[ ndims ]( x, y, ord === 1 );
}
// 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 || y.accessorProtocol ) {
return BLOCKED_ACCESSOR_ASSIGN[ ndims-2 ]( x, y );
}
return BLOCKED_ASSIGN[ ndims-2 ]( x, 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 || y.accessorProtocol ) {
return accessorassignnd( x, y );
}
assignnd( x, y );
}
// EXPORTS //
module.exports = assign;
|