Skip to content

Commit 26d65cd

Browse files
committed
refactor: use generalized utility
--- 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 b51882d commit 26d65cd

File tree

1 file changed

+2
-255
lines changed
  • lib/node_modules/@stdlib/ndarray/base/unary-output-dtype/lib

1 file changed

+2
-255
lines changed

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

Lines changed: 2 additions & 255 deletions
Original file line numberDiff line numberDiff line change
@@ -16,249 +16,11 @@
1616
* limitations under the License.
1717
*/
1818

19-
/* eslint-disable id-length */
20-
2119
'use strict';
2220

2321
// MODULES //
2422

25-
var isFloatingPointDataType = require( '@stdlib/ndarray/base/assert/is-floating-point-data-type' );
26-
var isRealFloatingPointDataType = require( '@stdlib/ndarray/base/assert/is-real-floating-point-data-type' );
27-
var isComplexFloatingPointDataType = require( '@stdlib/ndarray/base/assert/is-complex-floating-point-data-type' );
28-
var isIntegerDataType = require( '@stdlib/ndarray/base/assert/is-integer-data-type' );
29-
var isSignedIntegerDataType = require( '@stdlib/ndarray/base/assert/is-signed-integer-data-type' );
30-
var isUnsignedIntegerDataType = require( '@stdlib/ndarray/base/assert/is-unsigned-integer-data-type' );
31-
var isRealDataType = require( '@stdlib/ndarray/base/assert/is-real-data-type' );
32-
var isNumericDataType = require( '@stdlib/ndarray/base/assert/is-numeric-data-type' );
33-
var isBooleanDataType = require( '@stdlib/ndarray/base/assert/is-boolean-data-type' );
34-
var isIntegerIndexDataType = require( '@stdlib/ndarray/base/assert/is-integer-index-data-type' );
35-
var isBooleanIndexDataType = require( '@stdlib/ndarray/base/assert/is-boolean-index-data-type' );
36-
var isMaskIndexDataType = require( '@stdlib/ndarray/base/assert/is-mask-index-data-type' );
37-
var isDataType = require( '@stdlib/ndarray/base/assert/is-data-type' );
38-
var promotionRules = require( '@stdlib/ndarray/promotion-rules' );
39-
var defaults = require( '@stdlib/ndarray/defaults' );
40-
var format = require( '@stdlib/string/format' );
41-
42-
43-
// VARIABLES //
44-
45-
var DEFAULT_DTYPE = defaults.get( 'dtypes.default' );
46-
var DEFAULT_INDEX_DTYPE = defaults.get( 'dtypes.default_index' );
47-
var DEFAULT_SIGNED_INTEGER_DTYPE = defaults.get( 'dtypes.signed_integer' );
48-
var DEFAULT_UNSIGNED_INTEGER_DTYPE = defaults.get( 'dtypes.unsigned_integer' );
49-
var DEFAULT_REAL_FLOATING_POINT_DTYPE = defaults.get( 'dtypes.real_floating_point' );
50-
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...
63-
'floating_point': [
64-
isFloatingPointDataType,
65-
defaults.get( 'dtypes.floating_point' )
66-
],
67-
'floating_point_and_generic': [
68-
wrap( isFloatingPointDataType ),
69-
defaults.get( 'dtypes.floating_point' )
70-
],
71-
'real_floating_point': [
72-
isRealFloatingPointDataType,
73-
DEFAULT_REAL_FLOATING_POINT_DTYPE
74-
],
75-
'real_floating_point_and_generic': [
76-
wrap( isRealFloatingPointDataType ),
77-
DEFAULT_REAL_FLOATING_POINT_DTYPE
78-
],
79-
'complex_floating_point': [
80-
isComplexFloatingPointDataType,
81-
defaults.get( 'dtypes.complex_floating_point' )
82-
],
83-
'complex_floating_point_and_generic': [
84-
wrap( isComplexFloatingPointDataType ),
85-
defaults.get( 'dtypes.complex_floating_point' )
86-
],
87-
88-
// Integer policies...
89-
'integer': [
90-
isIntegerDataType,
91-
defaults.get( 'dtypes.integer' )
92-
],
93-
'integer_and_generic': [
94-
wrap( isIntegerDataType ),
95-
defaults.get( 'dtypes.integer' )
96-
],
97-
'signed_integer': [
98-
isSignedIntegerDataType,
99-
DEFAULT_SIGNED_INTEGER_DTYPE
100-
],
101-
'signed_integer_and_generic': [
102-
wrap( isSignedIntegerDataType ),
103-
DEFAULT_SIGNED_INTEGER_DTYPE
104-
],
105-
'unsigned_integer': [
106-
isUnsignedIntegerDataType,
107-
DEFAULT_UNSIGNED_INTEGER_DTYPE
108-
],
109-
'unsigned_integer_and_generic': [
110-
wrap( isUnsignedIntegerDataType ),
111-
DEFAULT_UNSIGNED_INTEGER_DTYPE
112-
],
113-
114-
// Real-valued number policies...
115-
'real': [
116-
isRealDataType,
117-
defaults.get( 'dtypes.real' )
118-
],
119-
'real_and_generic': [
120-
wrap( isRealDataType ),
121-
defaults.get( 'dtypes.real' )
122-
],
123-
124-
// Real- and complex-valued number policies...
125-
'numeric': [
126-
isNumericDataType,
127-
defaults.get( 'dtypes.numeric' )
128-
],
129-
'numeric_and_generic': [
130-
wrap( isNumericDataType ),
131-
defaults.get( 'dtypes.numeric' )
132-
],
133-
134-
// Boolean policies...
135-
'boolean': [
136-
isBooleanDataType,
137-
defaults.get( 'dtypes.boolean' )
138-
],
139-
'boolean_and_generic': [
140-
wrap( isBooleanDataType ),
141-
defaults.get( 'dtypes.boolean' )
142-
],
143-
144-
// Index policies...
145-
'integer_index': [
146-
isIntegerIndexDataType,
147-
defaults.get( 'dtypes.integer_index' )
148-
],
149-
'integer_index_and_generic': [
150-
wrap( isIntegerIndexDataType ),
151-
defaults.get( 'dtypes.integer_index' )
152-
],
153-
'boolean_index': [
154-
isBooleanIndexDataType,
155-
defaults.get( 'dtypes.boolean_index' )
156-
],
157-
'boolean_index_and_generic': [
158-
wrap( isBooleanIndexDataType ),
159-
defaults.get( 'dtypes.boolean_index' )
160-
],
161-
'mask_index': [
162-
isMaskIndexDataType,
163-
defaults.get( 'dtypes.mask_index' )
164-
],
165-
'mask_index_and_generic': [
166-
wrap( isMaskIndexDataType ),
167-
defaults.get( 'dtypes.mask_index' )
168-
]
169-
};
170-
171-
172-
// FUNCTIONS //
173-
174-
/**
175-
* Wraps a data type validation function to also check for a "generic" data type.
176-
*
177-
* @private
178-
* @param {Function} fcn - validation function
179-
* @returns {Function} wrapped validation function
180-
*/
181-
function wrap( fcn ) {
182-
return wrapper;
183-
184-
/**
185-
* Tests whether a provided data type is either "generic" or satisfies a data type validation function.
186-
*
187-
* @private
188-
* @param {*} value - input value
189-
* @returns {boolean} boolean indicating whether a provided value passes a test
190-
*/
191-
function wrapper( value ) {
192-
return ( value === 'generic' ) || fcn( value );
193-
}
194-
}
195-
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-
}
23+
var outputDataType = require( '@stdlib/ndarray/base/output-dtype' );
26224

26325

26426
// MAIN //
@@ -269,29 +31,14 @@ function accumulationPolicy( dtype ) {
26931
* @param {string} dtype - input ndarray data type
27032
* @param {string} policy - output ndarray data type policy
27133
* @throws {TypeError} second argument must be a recognized data type policy
272-
* @throws {Error} unexpected error
27334
* @returns {string} output ndarray data type
27435
*
27536
* @example
27637
* var dt = resolve( 'float64', 'complex_floating_point' );
27738
* // returns <string>
27839
*/
27940
function resolve( dtype, policy ) {
280-
var p = POLICY_TABLE1[ policy ];
281-
if ( p !== void 0 ) {
282-
return p( dtype );
283-
}
284-
p = POLICY_TABLE2[ policy ];
285-
if ( p !== void 0 ) {
286-
if ( p[ 0 ]( dtype ) ) {
287-
return dtype;
288-
}
289-
return p[ 1 ];
290-
}
291-
if ( isDataType( policy ) ) {
292-
return policy;
293-
}
294-
throw new TypeError( format( 'invalid argument. Second argument must be a supported data type policy. Value: `%s`.', policy ) );
41+
return outputDataType( [ dtype ], policy );
29542
}
29643

29744

0 commit comments

Comments
 (0)