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 | 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 44x 44x 44x 44x 44x 44x 44x 44x 44x 44x 44x 44x 44x 44x 44x 401x 21x 21x 380x 401x 598x 176x 176x 176x 176x 598x 401x 44x 44x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 28x 28x 28x 28x 28x 28x 28x 171x 171x 12x 12x 159x 159x 16x 28x 1x 1x 1x 1x 1x 1x 1x 1x 1x 373x 373x 373x 1714x 369x 369x 1714x 4x 373x 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 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 10x 19x 9x 9x 19x 19x 19x 4x 4x 19x 11x 19x 4x 4x 4x 4x 4x 19x 6x 19x 5x 5x 5x 4x 4x 5x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 19x 44x 44x 448x 448x 448x 448x 448x 448x 448x 75x 75x 75x 373x 448x 369x 369x 373x 373x 373x 373x 448x 44x 44x 44x 44x 44x 44x 44x 44x 44x 373x 448x 973x 973x 604x 604x 973x 448x 44x 19x 2x 2x 969x 969x 2x 2x 7x 19x 1x 1x 1x 1x 1x | /**
* @license Apache-2.0
*
* Copyright (c) 2021 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.
*/
'use strict';
// MODULES //
var promotionRules = require( '@stdlib/ndarray/promotion-rules' );
var safeCasts = require( '@stdlib/ndarray/safe-casts' );
var resolveEnum = require( '@stdlib/strided/base/dtype-resolve-enum' );
var resolveStr = require( '@stdlib/strided/base/dtype-resolve-str' );
var format = require( '@stdlib/string/format' );
// FUNCTIONS //
/**
* Returns the intersection of two sorted lists.
*
* @private
* @param {ArrayLikeObject} list1 - first sorted list
* @param {ArrayLikeObject} list2 - second sorted list
* @returns {ArrayLikeObject} result
*
* @example
* var list1 = [ 'a', 'b', 'c', 'd' ];
* var list2 = [ 'b', 'd', 'e' ];
*
* var out = intersection( list1, list2 );
* // returns [ 'b', 'd' ]
*/
function intersection( list1, list2 ) {
var out;
var M;
var N;
var v;
var i;
var j;
var k;
M = list1.length;
N = list2.length;
out = [];
k = 0;
for ( i = 0; i < M; i++ ) {
if ( k >= N ) {
break;
}
v = list1[ i ];
for ( j = k; j < N; j++ ) {
if ( v === list2[ j ] ) {
k = j + 1;
out.push( v );
break;
}
}
}
return out;
}
/**
* Resolves a list of data types to data type strings.
*
* @private
* @param {ArrayLikeObject} dtypes - list of data types
* @returns {(StringArray|Error)} data type strings (or an error)
*
* @example
* var out = resolve( [ 1, 2, 3 ] );
* // returns [...]
*/
function resolve( dtypes ) {
var out;
var dt;
var i;
out = [];
for ( i = 0; i < dtypes.length; i++ ) {
dt = resolveStr( dtypes[ i ] );
if ( dt === null ) {
return new TypeError( format( 'invalid argument. Must provide recognized data types. Unable to resolve a data type string. Value: `%s`.', dtypes[ i ] ) );
}
out.push( dt );
}
return out;
}
/**
* Tests whether a provided array contains a specified value.
*
* @private
* @param {Array} arr - input array
* @param {*} value - search value
* @returns {boolean} boolean indicating whether a provided array contains a specified value
*/
function contains( arr, value ) {
var i;
for ( i = 0; i < arr.length; i++ ) {
if ( arr[ i ] === value ) {
return true;
}
}
return false;
}
// MAIN //
/**
* Generates a list of binary interface signatures from strided array data types.
*
* ## Notes
*
* - The function returns a strided array having a stride length of `3` (i.e., every `3` elements define a binary interface signature).
* - For each signature (i.e., set of three consecutive non-overlapping strided array elements), the first two elements are the input data types and the third element is the return data type.
* - All signatures follow type promotion rules.
*
* @param {Array} dtypes1 - list of supported data types for the first argument
* @param {Array} dtypes2 - list of supported data types for the second argument
* @param {Array} dtypes3 - list of supported data types for the output
* @param {Options} [options] - options
* @param {boolean} [options.enums=false] - boolean flag indicating whether to return signatures as a list of enumeration constants
* @throws {TypeError} must provide recognized data types
* @returns {Array} strided array containing binary interface signatures
*
* @example
* var dtypes = [
* 'float64',
* 'float32',
* 'int32',
* 'uint8'
* ];
*
* var sigs = signatures( dtypes, dtypes, dtypes );
* // returns [ 'float32', 'float32', 'float32', ... ]
*/
function signatures( dtypes1, dtypes2, dtypes3, options ) {
var cache;
var casts;
var opts;
var tmp;
var out;
var dt1;
var dt2;
var dt3;
var t1;
var t2;
var t3;
var t4;
var M;
var N;
var i;
var j;
var k;
if ( arguments.length > 3 ) {
opts = options;
} else {
opts = {};
}
// Resolve the list of provided data types to data type strings:
dt1 = resolve( dtypes1 );
if ( dt1 instanceof Error ) {
throw dt1;
}
if ( dtypes2 === dtypes1 ) { // don't do work if we don't need to
dt2 = dt1;
} else {
dt2 = resolve( dtypes2 );
if ( dt2 instanceof Error ) {
throw dt2;
}
}
if ( dtypes3 === dtypes1 ) { // don't do work if we don't need to
dt3 = dt1;
} else if ( dtypes3 === dtypes2 ) {
dt3 = dt2;
} else {
dt3 = resolve( dtypes3 );
if ( dt3 instanceof Error ) {
throw dt3;
}
}
// Sort the list of return dtypes:
dt3.sort();
// Initialize a cache for storing the safe casts for promoted dtypes:
cache = {};
// Generate the list of signatures...
M = dt1.length;
N = dt2.length;
out = [];
for ( i = 0; i < M; i++ ) {
t1 = dt1[ i ];
for ( j = 0; j < N; j++ ) {
t2 = dt2[ j ];
// Resolve the promoted dtype for the current dtype pair:
t3 = promotionRules( t1, t2 );
// Check whether the dtype pair promotes...
if ( t3 === -1 || t3 === null ) {
// The dtype pair does not promote:
continue;
}
// Check whether the promoted dtype is in our list of output dtypes...
if ( contains( dt3, t3 ) ) {
out.push( t1, t2, t3 );
}
// Retrieve the allowed casts for the promoted dtype:
casts = cache[ t3 ];
// If a list of allowed casts is not in the cache, we need to resolve them...
if ( casts === void 0 ) {
// Resolve the list of safe casts for the promoted dtype:
casts = safeCasts( t3 );
// Remove safe casts which are not among the supported output dtypes:
casts = intersection( dt3, casts.sort() );
// Store the list of safe casts in the cache:
cache[ t3 ] = casts;
}
// Generate signatures for allowed casts...
for ( k = 0; k < casts.length; k++ ) {
t4 = casts[ k ];
if ( t4 !== t3 ) {
out.push( t1, t2, t4 );
}
}
}
}
if ( opts.enums ) {
tmp = [];
for ( i = 0; i < out.length; i++ ) {
tmp.push( resolveEnum( out[ i ] ) );
}
out = tmp;
}
return out;
}
// EXPORTS //
module.exports = signatures;
|