Skip to content

Commit 9bf5bb1

Browse files
committed
Auto-generated commit
1 parent 5f5b04c commit 9bf5bb1

File tree

3 files changed

+111
-226
lines changed

3 files changed

+111
-226
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,8 @@ A total of 15 issues were closed in this release:
379379

380380
<details>
381381

382+
- [`ad9966a`](https://github.com/stdlib-js/stdlib/commit/ad9966a5d6c560c6a4b2c785f2caafdd472b8399) - **refactor:** reuse existing logic by building on `ndarray/base/unary-output-dtype` _(by Athan Reines)_
383+
- [`d2d6c1d`](https://github.com/stdlib-js/stdlib/commit/d2d6c1daa1671c5e174e815640efffe63c7f528f) - **refactor:** reduce code complexity by reducing branching logic _(by Athan Reines)_
382384
- [`0973f6f`](https://github.com/stdlib-js/stdlib/commit/0973f6fd8117253edf90c856b82c97b4e3a9181b) - **docs:** update namespace table of contents [(#6968)](https://github.com/stdlib-js/stdlib/pull/6968) _(by stdlib-bot)_
383385
- [`556e832`](https://github.com/stdlib-js/stdlib/commit/556e832729f770a48692fc796fb4d5c9b4b5ae34) - **feat:** add `unaryStrided1dDispatchFactory` to namespace _(by Athan Reines)_
384386
- [`1133bce`](https://github.com/stdlib-js/stdlib/commit/1133bceaec004d01bf20932b6334529dc7a79648) - **feat:** add `unaryStrided1dDispatch` to namespace _(by Athan Reines)_

base/binary-output-dtype/lib/main.js

Lines changed: 15 additions & 191 deletions
Original file line numberDiff line numberDiff line change
@@ -16,168 +16,16 @@
1616
* limitations under the License.
1717
*/
1818

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

2321
// MODULES //
2422

25-
var isFloatingPointDataType = require( './../../../base/assert/is-floating-point-data-type' );
26-
var isRealFloatingPointDataType = require( './../../../base/assert/is-real-floating-point-data-type' );
27-
var isComplexFloatingPointDataType = require( './../../../base/assert/is-complex-floating-point-data-type' );
28-
var isIntegerDataType = require( './../../../base/assert/is-integer-data-type' );
29-
var isSignedIntegerDataType = require( './../../../base/assert/is-signed-integer-data-type' );
30-
var isUnsignedIntegerDataType = require( './../../../base/assert/is-unsigned-integer-data-type' );
31-
var isRealDataType = require( './../../../base/assert/is-real-data-type' );
32-
var isNumericDataType = require( './../../../base/assert/is-numeric-data-type' );
33-
var isBooleanDataType = require( './../../../base/assert/is-boolean-data-type' );
34-
var isIntegerIndexDataType = require( './../../../base/assert/is-integer-index-data-type' );
35-
var isBooleanIndexDataType = require( './../../../base/assert/is-boolean-index-data-type' );
36-
var isMaskIndexDataType = require( './../../../base/assert/is-mask-index-data-type' );
23+
var unaryOutputDataType = require( './../../../base/unary-output-dtype' );
3724
var isDataType = require( './../../../base/assert/is-data-type' );
3825
var promotionRules = require( './../../../promotion-rules' );
39-
var defaults = require( './../../../defaults' );
4026
var format = require( '@stdlib/string/format' );
4127

4228

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-
var POLICY_TABLE = {
52-
'floating_point': [
53-
isFloatingPointDataType,
54-
defaults.get( 'dtypes.floating_point' )
55-
],
56-
'floating_point_and_generic': [
57-
wrap( isFloatingPointDataType ),
58-
defaults.get( 'dtypes.floating_point' )
59-
],
60-
'real_floating_point': [
61-
isRealFloatingPointDataType,
62-
DEFAULT_REAL_FLOATING_POINT_DTYPE
63-
],
64-
'real_floating_point_and_generic': [
65-
wrap( isRealFloatingPointDataType ),
66-
DEFAULT_REAL_FLOATING_POINT_DTYPE
67-
],
68-
'complex_floating_point': [
69-
isComplexFloatingPointDataType,
70-
defaults.get( 'dtypes.complex_floating_point' )
71-
],
72-
'complex_floating_point_and_generic': [
73-
wrap( isComplexFloatingPointDataType ),
74-
defaults.get( 'dtypes.complex_floating_point' )
75-
],
76-
77-
'integer': [
78-
isIntegerDataType,
79-
defaults.get( 'dtypes.integer' )
80-
],
81-
'integer_and_generic': [
82-
wrap( isIntegerDataType ),
83-
defaults.get( 'dtypes.integer' )
84-
],
85-
'signed_integer': [
86-
isSignedIntegerDataType,
87-
DEFAULT_SIGNED_INTEGER_DTYPE
88-
],
89-
'signed_integer_and_generic': [
90-
wrap( isSignedIntegerDataType ),
91-
DEFAULT_SIGNED_INTEGER_DTYPE
92-
],
93-
'unsigned_integer': [
94-
isUnsignedIntegerDataType,
95-
DEFAULT_UNSIGNED_INTEGER_DTYPE
96-
],
97-
'unsigned_integer_and_generic': [
98-
wrap( isUnsignedIntegerDataType ),
99-
DEFAULT_UNSIGNED_INTEGER_DTYPE
100-
],
101-
102-
'real': [
103-
isRealDataType,
104-
defaults.get( 'dtypes.real' )
105-
],
106-
'real_and_generic': [
107-
wrap( isRealDataType ),
108-
defaults.get( 'dtypes.real' )
109-
],
110-
111-
'numeric': [
112-
isNumericDataType,
113-
defaults.get( 'dtypes.numeric' )
114-
],
115-
'numeric_and_generic': [
116-
wrap( isNumericDataType ),
117-
defaults.get( 'dtypes.numeric' )
118-
],
119-
120-
'boolean': [
121-
isBooleanDataType,
122-
defaults.get( 'dtypes.boolean' )
123-
],
124-
'boolean_and_generic': [
125-
wrap( isBooleanDataType ),
126-
defaults.get( 'dtypes.boolean' )
127-
],
128-
129-
'integer_index': [
130-
isIntegerIndexDataType,
131-
defaults.get( 'dtypes.integer_index' )
132-
],
133-
'integer_index_and_generic': [
134-
wrap( isIntegerIndexDataType ),
135-
defaults.get( 'dtypes.integer_index' )
136-
],
137-
'boolean_index': [
138-
isBooleanIndexDataType,
139-
defaults.get( 'dtypes.boolean_index' )
140-
],
141-
'boolean_index_and_generic': [
142-
wrap( isBooleanIndexDataType ),
143-
defaults.get( 'dtypes.boolean_index' )
144-
],
145-
'mask_index': [
146-
isMaskIndexDataType,
147-
defaults.get( 'dtypes.mask_index' )
148-
],
149-
'mask_index_and_generic': [
150-
wrap( isMaskIndexDataType ),
151-
defaults.get( 'dtypes.mask_index' )
152-
]
153-
};
154-
155-
156-
// FUNCTIONS //
157-
158-
/**
159-
* Wraps a data type validation function to also check for a "generic" data type.
160-
*
161-
* @private
162-
* @param {Function} fcn - validation function
163-
* @returns {Function} wrapped validation function
164-
*/
165-
function wrap( fcn ) {
166-
return wrapper;
167-
168-
/**
169-
* Tests whether a provided data type is either "generic" or satisfies a data type validation function.
170-
*
171-
* @private
172-
* @param {*} value - input value
173-
* @returns {boolean} boolean indicating whether a provided value passes a test
174-
*/
175-
function wrapper( value ) {
176-
return ( value === 'generic' ) || fcn( value );
177-
}
178-
}
179-
180-
18129
// MAIN //
18230

18331
/**
@@ -196,14 +44,11 @@ function wrap( fcn ) {
19644
*/
19745
function resolve( xdtype, ydtype, policy ) {
19846
var dt;
199-
var p;
200-
if ( policy === 'default' ) {
201-
// When the policy is "default", the output data type should always be the default data type without consideration for the input data types:
202-
return DEFAULT_DTYPE;
203-
}
204-
if ( policy === 'default_index' ) {
205-
// 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:
206-
return DEFAULT_INDEX_DTYPE;
47+
48+
// Check for a policy mandating an explicit data type...
49+
if ( isDataType( policy ) ) {
50+
// When the policy is a specific data type, the output data type should always be the specified data type without consideration for the input data types:
51+
return policy;
20752
}
20853
if ( policy === 'same' ) {
20954
// When the policy is "same", we require that all data types (both input and output) be the same...
@@ -212,43 +57,22 @@ function resolve( xdtype, ydtype, policy ) {
21257
}
21358
return xdtype;
21459
}
215-
// Check for an explicit data type...
216-
if ( isDataType( policy ) ) {
217-
// When the policy is a specific data type, the output data type should always be the specified data type without consideration for the input data types:
218-
return policy;
60+
if ( policy === 'default' || policy === 'default_index' ) {
61+
return unaryOutputDataType( xdtype, policy ); // note: these policies are independent of the input data type, so it doesn't matter what data type we provide as the first argument
21962
}
220-
// From this point forward, we always apply type promotion rules...
63+
// For all other policies, we always apply type promotion rules...
22164
dt = promotionRules( xdtype, ydtype );
22265
if ( dt === null || dt === -1 ) {
22366
throw new Error( format( 'invalid arguments. Unable to apply type promotion rules when resolving a data type to which the input data types can be safely cast. Data types: [%s, %s].', xdtype, ydtype ) );
22467
}
225-
if ( policy === 'promoted' ) {
226-
// If the policy is "promoted", we're done...
227-
return dt;
228-
}
229-
if ( policy === 'accumulation' ) {
230-
// 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...
231-
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
232-
return dt;
233-
}
234-
// 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`) are at high risk for overflow, especially for ndarrays containing many elements...
235-
if ( isUnsignedIntegerDataType( dt ) ) {
236-
return promotionRules( dt, DEFAULT_UNSIGNED_INTEGER_DTYPE );
237-
}
238-
if ( isSignedIntegerDataType( dt ) ) {
239-
return promotionRules( dt, DEFAULT_SIGNED_INTEGER_DTYPE );
240-
}
241-
// For all other promoted data types, accumulate in the default real-valued floating-point data type...
242-
return DEFAULT_REAL_FLOATING_POINT_DTYPE;
243-
}
244-
p = POLICY_TABLE[ policy ];
245-
if ( p === void 0 ) {
68+
// Resolve the output data type by treating this scenario as equivalent to passing the promoted data type as an input to a unary function...
69+
try {
70+
dt = unaryOutputDataType( dt, policy );
71+
} catch ( err ) { // eslint-disable-line no-unused-vars
72+
// We should only get here if the policy is invalid...
24673
throw new TypeError( format( 'invalid argument. Third argument must be a supported data type policy. Value: `%s`.', policy ) );
24774
}
248-
if ( p[ 0 ]( dt ) ) {
249-
return dt;
250-
}
251-
return p[ 1 ];
75+
return dt;
25276
}
25377

25478

0 commit comments

Comments
 (0)