Skip to content

Commit d2d6c1d

Browse files
committed
refactor: reduce code complexity by reducing branching logic
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: na - task: lint_package_json status: na - task: lint_repl_help status: na - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: na - task: lint_javascript_benchmarks status: na - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: na - task: lint_typescript_tests status: na - task: lint_license_headers status: passed ---
1 parent 57c02a7 commit d2d6c1d

File tree

1 file changed

+94
-35
lines changed
  • lib/node_modules/@stdlib/ndarray/base/unary-output-dtype/lib

1 file changed

+94
-35
lines changed

lib/node_modules/@stdlib/ndarray/base/unary-output-dtype/lib/main.js

Lines changed: 94 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,18 @@ var DEFAULT_SIGNED_INTEGER_DTYPE = defaults.get( 'dtypes.signed_integer' );
4848
var DEFAULT_UNSIGNED_INTEGER_DTYPE = defaults.get( 'dtypes.unsigned_integer' );
4949
var DEFAULT_REAL_FLOATING_POINT_DTYPE = defaults.get( 'dtypes.real_floating_point' );
5050

51-
var POLICY_TABLE = {
51+
// Table where, for each respective policy, the value is a function which applies the policy to an input data type...
52+
var POLICY_TABLE1 = {
53+
'default': defaultPolicy,
54+
'default_index': defaultIndexPolicy,
55+
'same': samePolicy,
56+
'promoted': promotedPolicy,
57+
'accumulation': accumulationPolicy
58+
};
59+
60+
// 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...
61+
var POLICY_TABLE2 = {
62+
// Floating-point policies...
5263
'floating_point': [
5364
isFloatingPointDataType,
5465
defaults.get( 'dtypes.floating_point' )
@@ -74,6 +85,7 @@ var POLICY_TABLE = {
7485
defaults.get( 'dtypes.complex_floating_point' )
7586
],
7687

88+
// Integer policies...
7789
'integer': [
7890
isIntegerDataType,
7991
defaults.get( 'dtypes.integer' )
@@ -99,6 +111,7 @@ var POLICY_TABLE = {
99111
DEFAULT_UNSIGNED_INTEGER_DTYPE
100112
],
101113

114+
// Real-valued number policies...
102115
'real': [
103116
isRealDataType,
104117
defaults.get( 'dtypes.real' )
@@ -108,6 +121,7 @@ var POLICY_TABLE = {
108121
defaults.get( 'dtypes.real' )
109122
],
110123

124+
// Real- and complex-valued number policies...
111125
'numeric': [
112126
isNumericDataType,
113127
defaults.get( 'dtypes.numeric' )
@@ -117,6 +131,7 @@ var POLICY_TABLE = {
117131
defaults.get( 'dtypes.numeric' )
118132
],
119133

134+
// Boolean policies...
120135
'boolean': [
121136
isBooleanDataType,
122137
defaults.get( 'dtypes.boolean' )
@@ -126,6 +141,7 @@ var POLICY_TABLE = {
126141
defaults.get( 'dtypes.boolean' )
127142
],
128143

144+
// Index policies...
129145
'integer_index': [
130146
isIntegerIndexDataType,
131147
defaults.get( 'dtypes.integer_index' )
@@ -177,6 +193,73 @@ function wrap( fcn ) {
177193
}
178194
}
179195

196+
/**
197+
* Returns the default data type.
198+
*
199+
* @private
200+
* @returns {string} output ndarray data type
201+
*/
202+
function defaultPolicy() {
203+
// When the policy is "default", the output data type should always be the default data type without consideration for the input data type:
204+
return DEFAULT_DTYPE;
205+
}
206+
207+
/**
208+
* Returns the default index data type.
209+
*
210+
* @private
211+
* @returns {string} output ndarray data type
212+
*/
213+
function defaultIndexPolicy() {
214+
// When the policy is "default_index", the output data type should always be the default index data type without consideration for the input data type:
215+
return DEFAULT_INDEX_DTYPE;
216+
}
217+
218+
/**
219+
* Applies the "same" policy by returning the input data type.
220+
*
221+
* @private
222+
* @param {string} dtype - input ndarray data type
223+
* @returns {string} output ndarray data type
224+
*/
225+
function samePolicy( dtype ) {
226+
return dtype;
227+
}
228+
229+
/**
230+
* Applies the "promoted" policy by returning the input data type, as applying type promotion to a single data type is a no-op.
231+
*
232+
* @private
233+
* @param {string} dtype - input ndarray data type
234+
* @returns {string} output ndarray data type
235+
*/
236+
function promotedPolicy( dtype ) {
237+
return dtype;
238+
}
239+
240+
/**
241+
* Applies the "accumulation" policy to an input data type.
242+
*
243+
* @private
244+
* @param {string} dtype - input ndarray data type
245+
* @returns {string} output ndarray data type
246+
*/
247+
function accumulationPolicy( dtype ) {
248+
// If the input 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...
249+
if ( isFloatingPointDataType( dtype ) || dtype === 'generic' ) { // NOTE: we may want to revisit this in the future for float16/complex32, where the value range is much more limited
250+
return dtype;
251+
}
252+
// Unless the input 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`) are at high risk for overflow, especially for ndarrays containing many elements...
253+
if ( isUnsignedIntegerDataType( dtype ) ) {
254+
return promotionRules( dtype, DEFAULT_UNSIGNED_INTEGER_DTYPE );
255+
}
256+
if ( isSignedIntegerDataType( dtype ) ) {
257+
return promotionRules( dtype, DEFAULT_SIGNED_INTEGER_DTYPE );
258+
}
259+
// For all other input data types, accumulate in the default real-valued floating-point data type...
260+
return DEFAULT_REAL_FLOATING_POINT_DTYPE;
261+
}
262+
180263

181264
// MAIN //
182265

@@ -194,45 +277,21 @@ function wrap( fcn ) {
194277
* // returns <string>
195278
*/
196279
function resolve( dtype, policy ) {
197-
var p;
198-
if ( policy === 'default' ) {
199-
// When the policy is "default", the output data type should always be the default data type without consideration for the input data type:
200-
return DEFAULT_DTYPE;
201-
}
202-
if ( policy === 'default_index' ) {
203-
// When the policy is "default_index", the output data type should always be the default index data type without consideration for the input data type:
204-
return DEFAULT_INDEX_DTYPE;
280+
var p = POLICY_TABLE1[ policy ];
281+
if ( p !== void 0 ) {
282+
return p( dtype );
205283
}
206-
if ( policy === 'same' || policy === 'promoted' ) { // note: for unary APIs, the "promoted" data type is the same as the input data type
207-
return dtype;
208-
}
209-
if ( policy === 'accumulation' ) {
210-
// If the input 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...
211-
if ( isFloatingPointDataType( dtype ) || dtype === 'generic' ) { // NOTE: we may want to revisit this in the future for float16/complex32, where the value range is much more limited
284+
p = POLICY_TABLE2[ policy ];
285+
if ( p !== void 0 ) {
286+
if ( p[ 0 ]( dtype ) ) {
212287
return dtype;
213288
}
214-
// Unless the input 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`) are at high risk for overflow, especially for ndarrays containing many elements...
215-
if ( isUnsignedIntegerDataType( dtype ) ) {
216-
return promotionRules( dtype, DEFAULT_UNSIGNED_INTEGER_DTYPE );
217-
}
218-
if ( isSignedIntegerDataType( dtype ) ) {
219-
return promotionRules( dtype, DEFAULT_SIGNED_INTEGER_DTYPE );
220-
}
221-
// For all other input data types, accumulate in the default real-valued floating-point data type...
222-
return DEFAULT_REAL_FLOATING_POINT_DTYPE;
289+
return p[ 1 ];
223290
}
224-
p = POLICY_TABLE[ policy ];
225-
if ( p === void 0 ) {
226-
// Check for an explicit data type...
227-
if ( isDataType( policy ) ) {
228-
return policy;
229-
}
230-
throw new TypeError( format( 'invalid argument. Second argument must be a supported data type policy. Value: `%s`.', policy ) );
231-
}
232-
if ( p[ 0 ]( dtype ) ) {
233-
return dtype;
291+
if ( isDataType( policy ) ) {
292+
return policy;
234293
}
235-
return p[ 1 ];
294+
throw new TypeError( format( 'invalid argument. Second argument must be a supported data type policy. Value: `%s`.', policy ) );
236295
}
237296

238297

0 commit comments

Comments
 (0)