8
8
using System . Collections . Generic ;
9
9
using System . Globalization ;
10
10
using System . Reflection ;
11
+ using System . Runtime . InteropServices ;
11
12
using System . Text ;
12
13
using System . Threading ;
13
14
using Microsoft . ML . Internal . Utilities ;
@@ -394,7 +395,7 @@ public bool TryGetStandardConversion(ColumnType typeSrc, ColumnType typeDst,
394
395
// Smaller dst means mapping values to NA.
395
396
if ( keySrc . Count != keyDst . Count )
396
397
return false ;
397
- if ( keySrc . Count == 0 && keySrc . RawKind > keyDst . RawKind )
398
+ if ( keySrc . Count == 0 && Marshal . SizeOf ( keySrc . RawType ) > Marshal . SizeOf ( keyDst . RawType ) )
398
399
return false ;
399
400
// REVIEW: Should we allow contiguous to be changed when Count is zero?
400
401
if ( keySrc . Contiguous != keyDst . Contiguous )
@@ -407,11 +408,11 @@ public bool TryGetStandardConversion(ColumnType typeSrc, ColumnType typeDst,
407
408
// does not allow this.
408
409
if ( ! KeyType . IsValidDataType ( typeDst . RawType ) )
409
410
return false ;
410
- if ( keySrc . RawKind > typeDst . RawKind )
411
+ if ( Marshal . SizeOf ( keySrc . RawType ) > Marshal . SizeOf ( typeDst . RawType ) )
411
412
{
412
413
if ( keySrc . Count == 0 )
413
414
return false ;
414
- if ( ( ulong ) keySrc . Count > typeDst . RawKind . ToMaxInt ( ) )
415
+ if ( ( ulong ) keySrc . Count > typeDst . RawType . ToMaxInt ( ) )
415
416
return false ;
416
417
}
417
418
}
@@ -549,20 +550,19 @@ private TryParseMapper<TDst> GetKeyTryParse<TDst>(KeyType key)
549
550
ulong min = key . Min ;
550
551
ulong max ;
551
552
552
- ulong count = DataKindExtensions . ToMaxInt ( key . RawKind ) ;
553
+ ulong count = key . RawType . ToMaxInt ( ) ;
553
554
if ( key . Count > 0 )
554
555
max = min - 1 + ( ulong ) key . Count ;
555
556
else if ( min == 0 )
556
557
max = count - 1 ;
557
- else if ( key . RawKind == DataKind . U8 )
558
+ else if ( key . RawType == typeof ( ulong ) )
558
559
max = ulong . MaxValue ;
559
560
else if ( min - 1 > ulong . MaxValue - count )
560
561
max = ulong . MaxValue ;
561
562
else
562
563
max = min - 1 + count ;
563
564
564
- bool identity ;
565
- var fnConv = GetStandardConversion < U8 , TDst > ( NumberType . U8 , NumberType . FromKind ( key . RawKind ) , out identity ) ;
565
+ var fnConv = GetKeyStandardConversion < TDst > ( ) ;
566
566
return
567
567
( in TX src , out TDst dst ) =>
568
568
{
@@ -592,20 +592,19 @@ private ValueMapper<TX, TDst> GetKeyParse<TDst>(KeyType key)
592
592
ulong min = key . Min ;
593
593
ulong max ;
594
594
595
- ulong count = DataKindExtensions . ToMaxInt ( key . RawKind ) ;
595
+ ulong count = key . RawType . ToMaxInt ( ) ;
596
596
if ( key . Count > 0 )
597
597
max = min - 1 + ( ulong ) key . Count ;
598
598
else if ( min == 0 )
599
599
max = count - 1 ;
600
- else if ( key . RawKind == DataKind . U8 )
600
+ else if ( key . RawType == typeof ( U8 ) )
601
601
max = ulong . MaxValue ;
602
602
else if ( min - 1 > ulong . MaxValue - count )
603
603
max = ulong . MaxValue ;
604
604
else
605
605
max = min - 1 + count ;
606
606
607
- bool identity ;
608
- var fnConv = GetStandardConversion < U8 , TDst > ( NumberType . U8 , NumberType . FromKind ( key . RawKind ) , out identity ) ;
607
+ var fnConv = GetKeyStandardConversion < TDst > ( ) ;
609
608
return
610
609
( in TX src , ref TDst dst ) =>
611
610
{
@@ -622,6 +621,14 @@ private ValueMapper<TX, TDst> GetKeyParse<TDst>(KeyType key)
622
621
} ;
623
622
}
624
623
624
+ private ValueMapper < U8 , TDst > GetKeyStandardConversion < TDst > ( )
625
+ {
626
+ var delegatesKey = ( typeof ( U8 ) , typeof ( TDst ) ) ;
627
+ if ( ! _delegatesStd . TryGetValue ( delegatesKey , out Delegate del ) )
628
+ throw Contracts . Except ( "No standard conversion from '{0}' to '{1}'" , typeof ( U8 ) , typeof ( TDst ) ) ;
629
+ return ( ValueMapper < U8 , TDst > ) del ;
630
+ }
631
+
625
632
private static StringBuilder ClearDst ( ref StringBuilder dst )
626
633
{
627
634
if ( dst == null )
0 commit comments