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 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | /**
* @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 id-length */
'use strict';
// MODULES //
var isFloatingPointDataType = require( '@stdlib/ndarray/base/assert/is-floating-point-data-type' );
var isRealFloatingPointDataType = require( '@stdlib/ndarray/base/assert/is-real-floating-point-data-type' );
var isComplexFloatingPointDataType = require( '@stdlib/ndarray/base/assert/is-complex-floating-point-data-type' );
var isIntegerDataType = require( '@stdlib/ndarray/base/assert/is-integer-data-type' );
var isSignedIntegerDataType = require( '@stdlib/ndarray/base/assert/is-signed-integer-data-type' );
var isUnsignedIntegerDataType = require( '@stdlib/ndarray/base/assert/is-unsigned-integer-data-type' );
var isRealDataType = require( '@stdlib/ndarray/base/assert/is-real-data-type' );
var isNumericDataType = require( '@stdlib/ndarray/base/assert/is-numeric-data-type' );
var isBooleanDataType = require( '@stdlib/ndarray/base/assert/is-boolean-data-type' );
var isIntegerIndexDataType = require( '@stdlib/ndarray/base/assert/is-integer-index-data-type' );
var isBooleanIndexDataType = require( '@stdlib/ndarray/base/assert/is-boolean-index-data-type' );
var isMaskIndexDataType = require( '@stdlib/ndarray/base/assert/is-mask-index-data-type' );
var isDataType = require( '@stdlib/ndarray/base/assert/is-data-type' );
var isString = require( '@stdlib/assert/is-string' ).isPrimitive;
var promoteDataTypes = require( '@stdlib/ndarray/base/promote-dtypes' );
var defaults = require( '@stdlib/ndarray/defaults' );
var join = require( '@stdlib/array/base/join' );
var format = require( '@stdlib/string/format' );
// VARIABLES //
var DEFAULT_DTYPE = defaults.get( 'dtypes.default' );
var DEFAULT_INDEX_DTYPE = defaults.get( 'dtypes.default_index' );
var DEFAULT_SIGNED_INTEGER_DTYPE = defaults.get( 'dtypes.signed_integer' );
var DEFAULT_UNSIGNED_INTEGER_DTYPE = defaults.get( 'dtypes.unsigned_integer' );
var DEFAULT_REAL_FLOATING_POINT_DTYPE = defaults.get( 'dtypes.real_floating_point' );
var DEFAULT_COMPLEX_FLOATING_POINT_DTYPE = defaults.get( 'dtypes.complex_floating_point' );
// Table where, for each respective policy, the value is a function which applies the policy to an input data type...
var POLICY_TABLE1 = {
'default': defaultPolicy,
'default_index': defaultIndexPolicy,
'same': samePolicy,
'promoted': promotedPolicy,
'accumulation': accumulationPolicy
};
// Table where, for each respective policy, the value is an array whose first element is an assertion and whose second element is a fallback data type...
var POLICY_TABLE2 = {
// Floating-point policies...
'floating_point': [
isFloatingPointDataType,
defaults.get( 'dtypes.floating_point' )
],
'floating_point_and_generic': [
wrap( isFloatingPointDataType ),
defaults.get( 'dtypes.floating_point' )
],
'real_floating_point': [
isRealFloatingPointDataType,
resolveDefaultRealFloatingPoint
],
'real_floating_point_and_generic': [
wrap( isRealFloatingPointDataType ),
resolveDefaultRealFloatingPoint
],
'complex_floating_point': [
isComplexFloatingPointDataType,
resolveDefaultComplexFloatingPoint
],
'complex_floating_point_and_generic': [
wrap( isComplexFloatingPointDataType ),
resolveDefaultComplexFloatingPoint
],
// Integer policies...
'integer': [
isIntegerDataType,
defaults.get( 'dtypes.integer' )
],
'integer_and_generic': [
wrap( isIntegerDataType ),
defaults.get( 'dtypes.integer' )
],
'signed_integer': [
isSignedIntegerDataType,
DEFAULT_SIGNED_INTEGER_DTYPE
],
'signed_integer_and_generic': [
wrap( isSignedIntegerDataType ),
DEFAULT_SIGNED_INTEGER_DTYPE
],
'unsigned_integer': [
isUnsignedIntegerDataType,
DEFAULT_UNSIGNED_INTEGER_DTYPE
],
'unsigned_integer_and_generic': [
wrap( isUnsignedIntegerDataType ),
DEFAULT_UNSIGNED_INTEGER_DTYPE
],
// Real-valued number policies...
'real': [
isRealDataType,
defaults.get( 'dtypes.real' )
],
'real_and_generic': [
wrap( isRealDataType ),
defaults.get( 'dtypes.real' )
],
// Real- and complex-valued number policies...
'numeric': [
isNumericDataType,
defaults.get( 'dtypes.numeric' )
],
'numeric_and_generic': [
wrap( isNumericDataType ),
defaults.get( 'dtypes.numeric' )
],
// Boolean policies...
'boolean': [
isBooleanDataType,
defaults.get( 'dtypes.boolean' )
],
'boolean_and_generic': [
wrap( isBooleanDataType ),
defaults.get( 'dtypes.boolean' )
],
// Index policies...
'integer_index': [
isIntegerIndexDataType,
defaults.get( 'dtypes.integer_index' )
],
'integer_index_and_generic': [
wrap( isIntegerIndexDataType ),
defaults.get( 'dtypes.integer_index' )
],
'boolean_index': [
isBooleanIndexDataType,
defaults.get( 'dtypes.boolean_index' )
],
'boolean_index_and_generic': [
wrap( isBooleanIndexDataType ),
defaults.get( 'dtypes.boolean_index' )
],
'mask_index': [
isMaskIndexDataType,
defaults.get( 'dtypes.mask_index' )
],
'mask_index_and_generic': [
wrap( isMaskIndexDataType ),
defaults.get( 'dtypes.mask_index' )
]
};
// Table mapping complex-valued floating-point data types to real-valued floating-point data types having the same precision:
var COMPLEX2FLOAT = {
'complex128': 'float64',
'complex64': 'float32',
'complex32': 'float16'
};
// Table mapping real-valued floating-point data types to complex-valued floating-point data types having the same precision:
var FLOAT2COMPLEX = {
'float64': 'complex128',
'float32': 'complex64',
'float16': 'complex32'
};
// FUNCTIONS //
/**
* Wraps a data type validation function to also check for a "generic" data type.
*
* @private
* @param {Function} fcn - validation function
* @returns {Function} wrapped validation function
*/
function wrap( fcn ) {
return wrapper;
/**
* Tests whether a provided data type is either "generic" or satisfies a data type validation function.
*
* @private
* @param {*} value - input value
* @returns {boolean} boolean indicating whether a provided value passes a test
*/
function wrapper( value ) {
return ( value === 'generic' ) || fcn( value );
}
}
/**
* Returns the default data type.
*
* @private
* @returns {string} output ndarray data type
*/
function defaultPolicy() {
// When the policy is "default", the output data type should always be the default data type without consideration for the input data types:
return DEFAULT_DTYPE;
}
/**
* Returns the default index data type.
*
* @private
* @returns {string} output ndarray data type
*/
function defaultIndexPolicy() {
// When the policy is "default_index", the output data type should always be the default index data type without consideration for the input data types:
return DEFAULT_INDEX_DTYPE;
}
/**
* Applies the "same" policy by returning the common input data type.
*
* @private
* @param {StringArray} dtypes - input ndarray data types
* @throws {Error} invalid data types
* @returns {string} output ndarray data type
*/
function samePolicy( dtypes ) {
var dt;
var i;
dt = dtypes[ 0 ];
for ( i = 1; i < dtypes.length; i++ ) {
// When the policy is "same", we require that all data types (both input and output) be the same...
if ( dtypes[ i ] !== dt ) {
throw new Error( format( 'invalid argument. Unable to resolve an output data type. The output data type policy is "same" and yet the input data types are not equal. Data types: [%s].', join( dtypes, ', ' ) ) );
}
}
return dt;
}
/**
* Applies the "promoted" policy by applying type promotion to the list of provided data types.
*
* @private
* @param {StringArray} dtypes - input ndarray data types
* @throws {Error} must provide data types which can be safely cast to a common data type
* @returns {string} output ndarray data type
*/
function promotedPolicy( dtypes ) {
var dt = promoteDataTypes( dtypes );
if ( dt === null ) {
throw new Error( format( 'invalid argument. Unable to apply type promotion rules when resolving a data type to which the input data types can be safely cast. Data types: [%s].', join( dtypes, ', ' ) ) );
}
return dt;
}
/**
* Applies the "accumulation" policy to the list of input data types.
*
* @private
* @param {StringArray} dtypes - input ndarray data types
* @throws {Error} must provide data types which can be safely cast to a common data type
* @returns {string} output ndarray data type
*/
function accumulationPolicy( dtypes ) {
var dt = promotedPolicy( dtypes );
// If the promoted data type is floating-point, allow accumulation in that data type as overflow/underflow is handled naturally as a built-in feature of that data type...
if ( isFloatingPointDataType( dt ) || dt === 'generic' ) { // NOTE: we may want to revisit this in the future for float16/complex32, where the value range is much more limited
return dt;
}
// Unless the promoted data type value range is larger than the default un/signed integer data type, accumulate in the default un/signed integer data type, as accumulating in small range integer data types (e.g., `int8`) is at high risk for overflow, especially for ndarrays containing many elements...
if ( isUnsignedIntegerDataType( dt ) ) {
return promoteDataTypes( [ dt, DEFAULT_UNSIGNED_INTEGER_DTYPE ] );
}
if ( isSignedIntegerDataType( dt ) ) {
return promoteDataTypes( [ dt, DEFAULT_SIGNED_INTEGER_DTYPE ] );
}
// For all other data types, accumulate in the default real-valued floating-point data type...
return DEFAULT_REAL_FLOATING_POINT_DTYPE;
}
/**
* Resolves a default real-valued floating-point data type which preserves floating-point precision.
*
* @private
* @param {string} dtype - input ndarray data type
* @returns {string} output ndarray data type
*/
function resolveDefaultRealFloatingPoint( dtype ) {
return COMPLEX2FLOAT[ dtype ] || DEFAULT_REAL_FLOATING_POINT_DTYPE;
}
/**
* Resolves a default complex-valued floating-point data type which preserves floating-point precision.
*
* @private
* @param {string} dtype - input ndarray data type
* @returns {string} output ndarray data type
*/
function resolveDefaultComplexFloatingPoint( dtype ) {
return FLOAT2COMPLEX[ dtype ] || DEFAULT_COMPLEX_FLOATING_POINT_DTYPE;
}
// MAIN //
/**
* Resolves the output data type from a list of input ndarray data types.
*
* @param {StringArray} dtypes - list of input ndarray data types
* @param {string} policy - output ndarray data type policy
* @throws {TypeError} second argument must be a recognized data type policy
* @throws {Error} unexpected error
* @returns {string} output ndarray data type
*
* @example
* var dt = resolve( [ 'float64' ], 'complex_floating_point' );
* // returns <string>
*/
function resolve( dtypes, policy ) {
var dt;
var p;
// First, check whether the policy mandates that the output data type always be a specific data type...
if ( isDataType( policy ) ) {
return policy;
}
// Next, check whether the policy is one in which we may be able to avoid type promotion...
p = POLICY_TABLE1[ policy ];
if ( p !== void 0 ) {
return p( dtypes );
}
// For all other policies, we always apply type promotion rules...
dt = promotedPolicy( dtypes );
// Check whether the policy requires determining the data type "kind"...
p = POLICY_TABLE2[ policy ];
if ( p !== void 0 ) {
// Check whether the promoted data type belongs to the data type "kind"...
if ( p[ 0 ]( dt ) ) {
// If so, we can just return the promoted data type:
return dt;
}
// Otherwise, we need to fallback to a default data type belonging to that "kind"...
if ( isString( p[ 1 ] ) ) {
return p[ 1 ];
}
return p[ 1 ]( dt );
}
throw new TypeError( format( 'invalid argument. Second argument must be a supported data type policy. Value: `%s`.', policy ) );
}
// EXPORTS //
module.exports = resolve;
|