You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// Table where, for each respective policy, the value is a function which applies the policy to an input data type...
52
+
varPOLICY_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
+
varPOLICY_TABLE2={
62
+
// Floating-point policies...
52
63
'floating_point': [
53
64
isFloatingPointDataType,
54
65
defaults.get('dtypes.floating_point')
@@ -74,6 +85,7 @@ var POLICY_TABLE = {
74
85
defaults.get('dtypes.complex_floating_point')
75
86
],
76
87
88
+
// Integer policies...
77
89
'integer': [
78
90
isIntegerDataType,
79
91
defaults.get('dtypes.integer')
@@ -99,6 +111,7 @@ var POLICY_TABLE = {
99
111
DEFAULT_UNSIGNED_INTEGER_DTYPE
100
112
],
101
113
114
+
// Real-valued number policies...
102
115
'real': [
103
116
isRealDataType,
104
117
defaults.get('dtypes.real')
@@ -108,6 +121,7 @@ var POLICY_TABLE = {
108
121
defaults.get('dtypes.real')
109
122
],
110
123
124
+
// Real- and complex-valued number policies...
111
125
'numeric': [
112
126
isNumericDataType,
113
127
defaults.get('dtypes.numeric')
@@ -117,6 +131,7 @@ var POLICY_TABLE = {
117
131
defaults.get('dtypes.numeric')
118
132
],
119
133
134
+
// Boolean policies...
120
135
'boolean': [
121
136
isBooleanDataType,
122
137
defaults.get('dtypes.boolean')
@@ -126,6 +141,7 @@ var POLICY_TABLE = {
126
141
defaults.get('dtypes.boolean')
127
142
],
128
143
144
+
// Index policies...
129
145
'integer_index': [
130
146
isIntegerIndexDataType,
131
147
defaults.get('dtypes.integer_index')
@@ -177,6 +193,73 @@ function wrap( fcn ) {
177
193
}
178
194
}
179
195
196
+
/**
197
+
* Returns the default data type.
198
+
*
199
+
* @private
200
+
* @returns {string} output ndarray data type
201
+
*/
202
+
functiondefaultPolicy(){
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
+
returnDEFAULT_DTYPE;
205
+
}
206
+
207
+
/**
208
+
* Returns the default index data type.
209
+
*
210
+
* @private
211
+
* @returns {string} output ndarray data type
212
+
*/
213
+
functiondefaultIndexPolicy(){
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
+
returnDEFAULT_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
+
functionsamePolicy(dtype){
226
+
returndtype;
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
+
functionpromotedPolicy(dtype){
237
+
returndtype;
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
+
functionaccumulationPolicy(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
+
returndtype;
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...
// For all other input data types, accumulate in the default real-valued floating-point data type...
260
+
returnDEFAULT_REAL_FLOATING_POINT_DTYPE;
261
+
}
262
+
180
263
181
264
// MAIN //
182
265
@@ -194,45 +277,21 @@ function wrap( fcn ) {
194
277
* // returns <string>
195
278
*/
196
279
functionresolve(dtype,policy){
197
-
varp;
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
-
returnDEFAULT_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
-
returnDEFAULT_INDEX_DTYPE;
280
+
varp=POLICY_TABLE1[policy];
281
+
if(p!==void0){
282
+
returnp(dtype);
205
283
}
206
-
if(policy==='same'||policy==='promoted'){// note: for unary APIs, the "promoted" data type is the same as the input data type
207
-
returndtype;
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!==void0){
286
+
if(p[0](dtype)){
212
287
returndtype;
213
288
}
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...
0 commit comments