forked from dotnet/machinelearning
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConversion.cs
More file actions
1743 lines (1553 loc) · 66.2 KB
/
Copy pathConversion.cs
File metadata and controls
1743 lines (1553 loc) · 66.2 KB
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
#pragma warning disable 420 // volatile with Interlocked.CompareExchange
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Reflection;
using System.Text;
using System.Threading;
using Microsoft.ML.Internal.Utilities;
namespace Microsoft.ML.Data.Conversion
{
using BL = Boolean;
using DT = DateTime;
using DZ = DateTimeOffset;
using I1 = SByte;
using I2 = Int16;
using I4 = Int32;
using I8 = Int64;
using R4 = Single;
using R8 = Double;
using SB = StringBuilder;
using TS = TimeSpan;
using TX = ReadOnlyMemory<char>;
using U1 = Byte;
using U2 = UInt16;
using U4 = UInt32;
using U8 = UInt64;
using UG = RowId;
[BestFriend]
internal delegate bool TryParseMapper<T>(in TX src, out T dst);
/// <summary>
/// This type exists to provide efficient delegates for conversion between standard ColumnTypes,
/// as discussed in the IDataView Type System Specification. This is a singleton class.
/// Some conversions are "standard" conversions, conforming to the details in the spec.
/// Others are auxilliary conversions. The use of auxilliary conversions should be limited to
/// situations that genuinely require them and have been well designed in the particular context.
/// For example, this contains non-standard conversions from the standard primitive types to
/// text (and StringBuilder). These are needed by the standard TextSaver, which handles
/// differences between sparse and dense inputs in a semantically invariant way.
/// </summary>
[BestFriend]
internal sealed class Conversions
{
// REVIEW: Reconcile implementations with TypeUtils, and clarify the distinction.
// Singleton pattern.
private static volatile Conversions _instance;
public static Conversions Instance
{
get
{
if (_instance == null)
Interlocked.CompareExchange(ref _instance, new Conversions(), null);
return _instance;
}
}
private const DataKind _kindStringBuilder = (DataKind)100;
private readonly Dictionary<Type, DataKind> _kinds;
// Maps from {src,dst} pair of DataKind to ValueMapper. The {src,dst} pair is
// the two byte values packed into the low two bytes of an int, with src the lsb.
private readonly Dictionary<int, Delegate> _delegatesStd;
// Maps from {src,dst} pair of DataKind to ValueMapper. The {src,dst} pair is
// the two byte values packed into the low two bytes of an int, with src the lsb.
private readonly Dictionary<int, Delegate> _delegatesAll;
// This has RefPredicate<T> delegates for determining whether a value is NA.
private readonly Dictionary<DataKind, Delegate> _isNADelegates;
// This has RefPredicate<VBuffer<T>> delegates for determining whether a buffer contains any NA values.
private readonly Dictionary<DataKind, Delegate> _hasNADelegates;
// This has RefPredicate<T> delegates for determining whether a value is default.
private readonly Dictionary<DataKind, Delegate> _isDefaultDelegates;
// This has RefPredicate<VBuffer<T>> delegates for determining whether a buffer contains any zero values.
// The supported types are unsigned signed integer values (for determining whether a key type is NA).
private readonly Dictionary<DataKind, Delegate> _hasZeroDelegates;
// This has ValueGetter<T> delegates for producing an NA value of the given type.
private readonly Dictionary<DataKind, Delegate> _getNADelegates;
// This has TryParseMapper<T> delegates for parsing values from text.
private readonly Dictionary<DataKind, Delegate> _tryParseDelegates;
private Conversions()
{
// We fabricate a DataKind value for StringBuilder.
Contracts.Assert(!Enum.IsDefined(typeof(DataKind), _kindStringBuilder));
_kinds = new Dictionary<Type, DataKind>();
for (DataKind kind = DataKindExtensions.KindMin; kind < DataKindExtensions.KindLim; kind++)
_kinds.Add(kind.ToType(), kind);
// We don't put StringBuilder in _kinds, but there are conversions to StringBuilder.
Contracts.Assert(!_kinds.ContainsKey(typeof(StringBuilder)));
Contracts.Assert(_kinds.Count == 16);
_delegatesStd = new Dictionary<int, Delegate>();
_delegatesAll = new Dictionary<int, Delegate>();
_isNADelegates = new Dictionary<DataKind, Delegate>();
_hasNADelegates = new Dictionary<DataKind, Delegate>();
_isDefaultDelegates = new Dictionary<DataKind, Delegate>();
_hasZeroDelegates = new Dictionary<DataKind, Delegate>();
_getNADelegates = new Dictionary<DataKind, Delegate>();
_tryParseDelegates = new Dictionary<DataKind, Delegate>();
// !!! WARNING !!!: Do NOT add any standard conversions without clearing from the IDV Type System
// design committee. Any changes also require updating the IDV Type System Specification.
AddStd<I1, I1>(Convert);
AddStd<I1, I2>(Convert);
AddStd<I1, I4>(Convert);
AddStd<I1, I8>(Convert);
AddStd<I1, R4>(Convert);
AddStd<I1, R8>(Convert);
AddAux<I1, SB>(Convert);
AddStd<I2, I1>(Convert);
AddStd<I2, I2>(Convert);
AddStd<I2, I4>(Convert);
AddStd<I2, I8>(Convert);
AddStd<I2, R4>(Convert);
AddStd<I2, R8>(Convert);
AddAux<I2, SB>(Convert);
AddStd<I4, I1>(Convert);
AddStd<I4, I2>(Convert);
AddStd<I4, I4>(Convert);
AddStd<I4, I8>(Convert);
AddStd<I4, R4>(Convert);
AddStd<I4, R8>(Convert);
AddAux<I4, SB>(Convert);
AddStd<I8, I1>(Convert);
AddStd<I8, I2>(Convert);
AddStd<I8, I4>(Convert);
AddStd<I8, I8>(Convert);
AddStd<I8, R4>(Convert);
AddStd<I8, R8>(Convert);
AddAux<I8, SB>(Convert);
AddStd<U1, U1>(Convert);
AddStd<U1, U2>(Convert);
AddStd<U1, U4>(Convert);
AddStd<U1, U8>(Convert);
AddStd<U1, UG>(Convert);
AddStd<U1, R4>(Convert);
AddStd<U1, R8>(Convert);
AddAux<U1, SB>(Convert);
AddStd<U2, U1>(Convert);
AddStd<U2, U2>(Convert);
AddStd<U2, U4>(Convert);
AddStd<U2, U8>(Convert);
AddStd<U2, UG>(Convert);
AddStd<U2, R4>(Convert);
AddStd<U2, R8>(Convert);
AddAux<U2, SB>(Convert);
AddStd<U4, U1>(Convert);
AddStd<U4, U2>(Convert);
AddStd<U4, U4>(Convert);
AddStd<U4, U8>(Convert);
AddStd<U4, UG>(Convert);
AddStd<U4, R4>(Convert);
AddStd<U4, R8>(Convert);
AddAux<U4, SB>(Convert);
AddStd<U8, U1>(Convert);
AddStd<U8, U2>(Convert);
AddStd<U8, U4>(Convert);
AddStd<U8, U8>(Convert);
AddStd<U8, UG>(Convert);
AddStd<U8, R4>(Convert);
AddStd<U8, R8>(Convert);
AddAux<U8, SB>(Convert);
AddStd<UG, U1>(Convert);
AddStd<UG, U2>(Convert);
AddStd<UG, U4>(Convert);
AddStd<UG, U8>(Convert);
// REVIEW: Conversion from UG to R4/R8, should we?
AddAux<UG, SB>(Convert);
AddStd<R4, R4>(Convert);
AddStd<R4, R8>(Convert);
AddAux<R4, SB>(Convert);
AddStd<R8, R4>(Convert);
AddStd<R8, R8>(Convert);
AddAux<R8, SB>(Convert);
AddStd<TX, I1>(Convert);
AddStd<TX, U1>(Convert);
AddStd<TX, I2>(Convert);
AddStd<TX, U2>(Convert);
AddStd<TX, I4>(Convert);
AddStd<TX, U4>(Convert);
AddStd<TX, I8>(Convert);
AddStd<TX, U8>(Convert);
AddStd<TX, UG>(Convert);
AddStd<TX, R4>(Convert);
AddStd<TX, R8>(Convert);
AddStd<TX, TX>(Convert);
AddStd<TX, BL>(Convert);
AddAux<TX, SB>(Convert);
AddStd<TX, TS>(Convert);
AddStd<TX, DT>(Convert);
AddStd<TX, DZ>(Convert);
AddStd<BL, I1>(Convert);
AddStd<BL, I2>(Convert);
AddStd<BL, I4>(Convert);
AddStd<BL, I8>(Convert);
AddStd<BL, R4>(Convert);
AddStd<BL, R8>(Convert);
AddStd<BL, BL>(Convert);
AddAux<BL, SB>(Convert);
AddStd<TS, I8>(Convert);
AddStd<TS, R4>(Convert);
AddStd<TS, R8>(Convert);
AddAux<TS, SB>(Convert);
AddStd<DT, I8>(Convert);
AddStd<DT, R4>(Convert);
AddStd<DT, R8>(Convert);
AddAux<DT, SB>(Convert);
AddStd<DZ, I8>(Convert);
AddStd<DZ, R4>(Convert);
AddStd<DZ, R8>(Convert);
AddAux<DZ, SB>(Convert);
AddIsNA<R4>(IsNA);
AddIsNA<R8>(IsNA);
AddGetNA<R4>(GetNA);
AddGetNA<R8>(GetNA);
AddHasNA<R4>(HasNA);
AddHasNA<R8>(HasNA);
AddIsDef<I1>(IsDefault);
AddIsDef<I2>(IsDefault);
AddIsDef<I4>(IsDefault);
AddIsDef<I8>(IsDefault);
AddIsDef<R4>(IsDefault);
AddIsDef<R8>(IsDefault);
AddIsDef<BL>(IsDefault);
AddIsDef<TX>(IsDefault);
AddIsDef<U1>(IsDefault);
AddIsDef<U2>(IsDefault);
AddIsDef<U4>(IsDefault);
AddIsDef<U8>(IsDefault);
AddIsDef<UG>(IsDefault);
AddIsDef<TS>(IsDefault);
AddIsDef<DT>(IsDefault);
AddIsDef<DZ>(IsDefault);
AddHasZero<U1>(HasZero);
AddHasZero<U2>(HasZero);
AddHasZero<U4>(HasZero);
AddHasZero<U8>(HasZero);
AddTryParse<I1>(TryParse);
AddTryParse<I2>(TryParse);
AddTryParse<I4>(TryParse);
AddTryParse<I8>(TryParse);
AddTryParse<U1>(TryParse);
AddTryParse<U2>(TryParse);
AddTryParse<U4>(TryParse);
AddTryParse<U8>(TryParse);
AddTryParse<UG>(TryParse);
AddTryParse<R4>(TryParse);
AddTryParse<R8>(TryParse);
AddTryParse<BL>(TryParse);
AddTryParse<TX>(TryParse);
AddTryParse<TS>(TryParse);
AddTryParse<DT>(TryParse);
AddTryParse<DZ>(TryParse);
}
private static int GetKey(DataKind kindSrc, DataKind kindDst)
{
Contracts.Assert(Enum.IsDefined(typeof(DataKind), kindSrc));
Contracts.Assert(Enum.IsDefined(typeof(DataKind), kindDst) || kindDst == _kindStringBuilder);
Contracts.Assert(0 <= _kindStringBuilder && (int)_kindStringBuilder < (1 << 8));
return ((int)kindSrc << 8) | (int)kindDst;
}
// Add a standard conversion to the lookup tables.
private void AddStd<TSrc, TDst>(ValueMapper<TSrc, TDst> fn)
{
var kindSrc = _kinds[typeof(TSrc)];
var kindDst = _kinds[typeof(TDst)];
var key = GetKey(kindSrc, kindDst);
_delegatesStd.Add(key, fn);
_delegatesAll.Add(key, fn);
}
// Add a non-standard conversion to the lookup table.
private void AddAux<TSrc, TDst>(ValueMapper<TSrc, TDst> fn)
{
var kindSrc = _kinds[typeof(TSrc)];
var kindDst = typeof(TDst) == typeof(SB) ? _kindStringBuilder : _kinds[typeof(TDst)];
_delegatesAll.Add(GetKey(kindSrc, kindDst), fn);
}
private void AddIsNA<T>(InPredicate<T> fn)
{
var kind = _kinds[typeof(T)];
_isNADelegates.Add(kind, fn);
}
private void AddGetNA<T>(ValueGetter<T> fn)
{
var kind = _kinds[typeof(T)];
_getNADelegates.Add(kind, fn);
}
private void AddHasNA<T>(InPredicate<VBuffer<T>> fn)
{
var kind = _kinds[typeof(T)];
_hasNADelegates.Add(kind, fn);
}
private void AddIsDef<T>(InPredicate<T> fn)
{
var kind = _kinds[typeof(T)];
_isDefaultDelegates.Add(kind, fn);
}
private void AddHasZero<T>(InPredicate<VBuffer<T>> fn)
{
var kind = _kinds[typeof(T)];
_hasZeroDelegates.Add(kind, fn);
}
private void AddTryParse<T>(TryParseMapper<T> fn)
{
var kind = _kinds[typeof(T)];
_tryParseDelegates.Add(kind, fn);
}
/// <summary>
/// Return a standard conversion delegate from typeSrc to typeDst. If there is no such standard
/// conversion, this throws an exception.
/// </summary>
public ValueMapper<TSrc, TDst> GetStandardConversion<TSrc, TDst>(ColumnType typeSrc, ColumnType typeDst,
out bool identity)
{
ValueMapper<TSrc, TDst> conv;
if (!TryGetStandardConversion(typeSrc, typeDst, out conv, out identity))
throw Contracts.Except("No standard conversion from '{0}' to '{1}'", typeSrc, typeDst);
return conv;
}
/// <summary>
/// Determine whether there is a standard conversion from typeSrc to typeDst and if so,
/// set conv to the conversion delegate. The type parameters TSrc and TDst must match
/// the raw types of typeSrc and typeDst.
/// </summary>
public bool TryGetStandardConversion<TSrc, TDst>(ColumnType typeSrc, ColumnType typeDst,
out ValueMapper<TSrc, TDst> conv, out bool identity)
{
Contracts.CheckValue(typeSrc, nameof(typeSrc));
Contracts.CheckValue(typeDst, nameof(typeDst));
Contracts.Check(typeSrc.RawType == typeof(TSrc));
Contracts.Check(typeDst.RawType == typeof(TDst));
Delegate del;
if (!TryGetStandardConversion(typeSrc, typeDst, out del, out identity))
{
conv = null;
return false;
}
conv = (ValueMapper<TSrc, TDst>)del;
return true;
}
/// <summary>
/// Return a standard conversion delegate from typeSrc to typeDst. If there is no such standard
/// conversion, this throws an exception.
/// </summary>
public Delegate GetStandardConversion(ColumnType typeSrc, ColumnType typeDst)
{
bool identity;
Delegate conv;
if (!TryGetStandardConversion(typeSrc, typeDst, out conv, out identity))
throw Contracts.Except("No standard conversion from '{0}' to '{1}'", typeSrc, typeDst);
return conv;
}
/// <summary>
/// Determine whether there is a standard conversion from typeSrc to typeDst and if so,
/// set conv to the conversion delegate.
/// </summary>
public bool TryGetStandardConversion(ColumnType typeSrc, ColumnType typeDst,
out Delegate conv, out bool identity)
{
Contracts.CheckValue(typeSrc, nameof(typeSrc));
Contracts.CheckValue(typeDst, nameof(typeDst));
conv = null;
identity = false;
if (typeSrc is KeyType keySrc)
{
// Key types are only convertable to compatible key types or unsigned integer
// types that are large enough.
if (typeDst is KeyType keyDst)
{
// We allow the Min value to shift. We currently don't allow the counts to vary.
// REVIEW: Should we allow the counts to vary? Allowing the dst to be bigger is trivial.
// Smaller dst means mapping values to NA.
if (keySrc.Count != keyDst.Count)
return false;
if (keySrc.Count == 0 && keySrc.RawKind > keyDst.RawKind)
return false;
// REVIEW: Should we allow contiguous to be changed when Count is zero?
if (keySrc.Contiguous != keyDst.Contiguous)
return false;
}
else
{
// Technically there is no standard conversion from a key type to an unsigned integer type,
// but it's very convenient for client code, so we allow it here. Note that ConvertTransform
// does not allow this.
if (!KeyType.IsValidDataKind(typeDst.RawKind))
return false;
if (keySrc.RawKind > typeDst.RawKind)
{
if (keySrc.Count == 0)
return false;
if ((ulong)keySrc.Count > typeDst.RawKind.ToMaxInt())
return false;
}
}
// REVIEW: Should we look for illegal values and force them to zero? If so, then
// we'll need to set identity to false.
}
else if (typeDst is KeyType keyDst)
{
if (!typeSrc.IsText)
return false;
conv = GetKeyParse(keyDst);
return true;
}
else if (!typeDst.IsStandardScalar)
return false;
Contracts.Assert(typeSrc.RawKind != 0);
Contracts.Assert(typeDst.RawKind != 0);
int key = GetKey(typeSrc.RawKind, typeDst.RawKind);
identity = typeSrc.RawKind == typeDst.RawKind;
return _delegatesStd.TryGetValue(key, out conv);
}
public ValueMapper<TSrc, SB> GetStringConversion<TSrc>(ColumnType type)
{
ValueMapper<TSrc, SB> conv;
if (TryGetStringConversion(type, out conv))
return conv;
throw Contracts.Except($"No conversion from '{type}' to {nameof(StringBuilder)}");
}
public ValueMapper<TSrc, SB> GetStringConversion<TSrc>()
{
ValueMapper<TSrc, SB> conv;
if (TryGetStringConversion(out conv))
return conv;
throw Contracts.Except($"No conversion from '{typeof(TSrc)}' to {nameof(StringBuilder)}");
}
public bool TryGetStringConversion<TSrc>(ColumnType type, out ValueMapper<TSrc, SB> conv)
{
Contracts.CheckValue(type, nameof(type));
Contracts.Check(type.RawType == typeof(TSrc), "Wrong TSrc type argument");
if (type is KeyType keyType)
{
// Key string conversion always works.
conv = GetKeyStringConversion<TSrc>(keyType);
return true;
}
return TryGetStringConversion(out conv);
}
private bool TryGetStringConversion<TSrc>(out ValueMapper<TSrc, SB> conv)
{
DataKind kindSrc;
if (!_kinds.TryGetValue(typeof(TSrc), out kindSrc))
{
conv = null;
return false;
}
int key = GetKey(kindSrc, _kindStringBuilder);
Delegate del;
if (_delegatesAll.TryGetValue(key, out del))
{
conv = (ValueMapper<TSrc, SB>)del;
return true;
}
conv = null;
return false;
}
public ValueMapper<TSrc, SB> GetKeyStringConversion<TSrc>(KeyType key)
{
Contracts.Check(key.RawType == typeof(TSrc));
// For key types, first convert to ulong, then do the range check,
// then convert to StringBuilder.
U8 min = key.Min;
int count = key.Count;
Contracts.Assert(count >= 0 && (U8)count <= U8.MaxValue - min);
bool identity;
var convSrc = GetStandardConversion<TSrc, U8>(key, NumberType.U8, out identity);
var convU8 = GetStringConversion<U8>(NumberType.U8);
if (count > 0)
{
return
(in TSrc src, ref SB dst) =>
{
ulong tmp = 0;
convSrc(in src, ref tmp);
if (tmp == 0 || tmp > (ulong)count)
ClearDst(ref dst);
else
{
tmp = tmp + min - 1;
convU8(in tmp, ref dst);
}
};
}
else
{
return
(in TSrc src, ref SB dst) =>
{
U8 tmp = 0;
convSrc(in src, ref tmp);
if (tmp == 0 || min > 1 && tmp > U8.MaxValue - min + 1)
ClearDst(ref dst);
else
{
tmp = tmp + min - 1;
convU8(in tmp, ref dst);
}
};
}
}
public TryParseMapper<TDst> GetTryParseConversion<TDst>(ColumnType typeDst)
{
Contracts.CheckValue(typeDst, nameof(typeDst));
Contracts.CheckParam(typeDst.IsStandardScalar || typeDst.IsKey, nameof(typeDst),
"Parse conversion only supported for standard types");
Contracts.Check(typeDst.RawType == typeof(TDst), "Wrong TDst type parameter");
if (typeDst is KeyType keyType)
return GetKeyTryParse<TDst>(keyType);
Contracts.Assert(_tryParseDelegates.ContainsKey(typeDst.RawKind));
return (TryParseMapper<TDst>)_tryParseDelegates[typeDst.RawKind];
}
private TryParseMapper<TDst> GetKeyTryParse<TDst>(KeyType key)
{
Contracts.Assert(key.RawType == typeof(TDst));
// First parse as ulong, then convert to T.
ulong min = key.Min;
ulong max;
ulong count = DataKindExtensions.ToMaxInt(key.RawKind);
if (key.Count > 0)
max = min - 1 + (ulong)key.Count;
else if (min == 0)
max = count - 1;
else if (key.RawKind == DataKind.U8)
max = ulong.MaxValue;
else if (min - 1 > ulong.MaxValue - count)
max = ulong.MaxValue;
else
max = min - 1 + count;
bool identity;
var fnConv = GetStandardConversion<U8, TDst>(NumberType.U8, NumberType.FromKind(key.RawKind), out identity);
return
(in TX src, out TDst dst) =>
{
ulong uu;
dst = default(TDst);
if (!TryParseKey(in src, min, max, out uu))
return false;
// REVIEW: This call to fnConv should never need range checks, so could be made faster.
// Also, it would be nice to be able to assert that it doesn't overflow....
fnConv(in uu, ref dst);
return true;
};
}
private Delegate GetKeyParse(KeyType key)
{
Func<KeyType, ValueMapper<TX, int>> del = GetKeyParse<int>;
var meth = del.GetMethodInfo().GetGenericMethodDefinition().MakeGenericMethod(key.RawType);
return (Delegate)meth.Invoke(this, new object[] { key });
}
private ValueMapper<TX, TDst> GetKeyParse<TDst>(KeyType key)
{
Contracts.Assert(key.RawType == typeof(TDst));
// First parse as ulong, then convert to T.
ulong min = key.Min;
ulong max;
ulong count = DataKindExtensions.ToMaxInt(key.RawKind);
if (key.Count > 0)
max = min - 1 + (ulong)key.Count;
else if (min == 0)
max = count - 1;
else if (key.RawKind == DataKind.U8)
max = ulong.MaxValue;
else if (min - 1 > ulong.MaxValue - count)
max = ulong.MaxValue;
else
max = min - 1 + count;
bool identity;
var fnConv = GetStandardConversion<U8, TDst>(NumberType.U8, NumberType.FromKind(key.RawKind), out identity);
return
(in TX src, ref TDst dst) =>
{
ulong uu;
dst = default(TDst);
if (!TryParseKey(in src, min, max, out uu))
{
dst = default(TDst);
return;
}
// REVIEW: This call to fnConv should never need range checks, so could be made faster.
// Also, it would be nice to be able to assert that it doesn't overflow....
fnConv(in uu, ref dst);
};
}
private static StringBuilder ClearDst(ref StringBuilder dst)
{
if (dst == null)
dst = new StringBuilder();
else
dst.Clear();
return dst;
}
public InPredicate<T> GetIsDefaultPredicate<T>(ColumnType type)
{
Contracts.CheckValue(type, nameof(type));
Contracts.CheckParam(!type.IsVector, nameof(type));
Contracts.CheckParam(type.RawType == typeof(T), nameof(type));
var t = type;
Delegate del;
if (!t.IsStandardScalar && !t.IsKey || !_isDefaultDelegates.TryGetValue(t.RawKind, out del))
throw Contracts.Except("No IsDefault predicate for '{0}'", type);
return (InPredicate<T>)del;
}
public InPredicate<T> GetIsNAPredicate<T>(ColumnType type)
{
InPredicate<T> pred;
if (TryGetIsNAPredicate(type, out pred))
return pred;
throw Contracts.Except("No IsNA predicate for '{0}'", type);
}
public bool TryGetIsNAPredicate<T>(ColumnType type, out InPredicate<T> pred)
{
Contracts.CheckValue(type, nameof(type));
Contracts.CheckParam(type.RawType == typeof(T), nameof(type));
Delegate del;
if (!TryGetIsNAPredicate(type, out del))
{
pred = null;
return false;
}
Contracts.Assert(del is InPredicate<T>);
pred = (InPredicate<T>)del;
return true;
}
public bool TryGetIsNAPredicate(ColumnType type, out Delegate del)
{
Contracts.CheckValue(type, nameof(type));
Contracts.CheckParam(!type.IsVector, nameof(type));
var t = type;
if (t.IsKey)
{
// REVIEW: Should we test for out of range when KeyCount > 0?
Contracts.Assert(_isDefaultDelegates.ContainsKey(t.RawKind));
del = _isDefaultDelegates[t.RawKind];
}
else if (!t.IsStandardScalar || !_isNADelegates.TryGetValue(t.RawKind, out del))
{
del = null;
return false;
}
Contracts.Assert(del != null);
return true;
}
public InPredicate<VBuffer<T>> GetHasMissingPredicate<T>(VectorType type)
{
Contracts.CheckValue(type, nameof(type));
Contracts.CheckParam(type.ItemType.RawType == typeof(T), nameof(type));
var t = type.ItemType;
Delegate del;
if (t.IsKey)
{
// REVIEW: Should we test for out of range when KeyCount > 0?
Contracts.Assert(_hasZeroDelegates.ContainsKey(t.RawKind));
del = _hasZeroDelegates[t.RawKind];
}
else if (!t.IsStandardScalar || !_hasNADelegates.TryGetValue(t.RawKind, out del))
throw Contracts.Except("No HasMissing predicate for '{0}'", type);
return (InPredicate<VBuffer<T>>)del;
}
/// <summary>
/// Returns the NA value of the given type, if it has one, otherwise, it returns
/// default of the type. This only knows about NA values of standard scalar types
/// and key types.
/// </summary>
public T GetNAOrDefault<T>(ColumnType type)
{
Contracts.CheckValue(type, nameof(type));
Contracts.CheckParam(type.RawType == typeof(T), nameof(type));
Delegate del;
if (!_getNADelegates.TryGetValue(type.RawKind, out del))
return default(T);
T res = default(T);
((ValueGetter<T>)del)(ref res);
return res;
}
/// <summary>
/// Returns the NA value of the given type, if it has one, otherwise, it returns
/// default of the type. This only knows about NA values of standard scalar types
/// and key types. Returns whether the returned value is the default value or not.
/// </summary>
public T GetNAOrDefault<T>(ColumnType type, out bool isDefault)
{
Contracts.CheckValue(type, nameof(type));
Contracts.CheckParam(type.RawType == typeof(T), nameof(type));
Delegate del;
if (!_getNADelegates.TryGetValue(type.RawKind, out del))
{
isDefault = true;
return default(T);
}
T res = default(T);
((ValueGetter<T>)del)(ref res);
isDefault = false;
#if DEBUG
Delegate isDefPred;
if (_isDefaultDelegates.TryGetValue(type.RawKind, out isDefPred))
Contracts.Assert(!((InPredicate<T>)isDefPred)(in res));
#endif
return res;
}
/// <summary>
/// Returns a ValueGetter{T} that produces the NA value of the given type, if it has one,
/// otherwise, it produces default of the type. This only knows about NA values of standard
/// scalar types and key types.
/// </summary>
public ValueGetter<T> GetNAOrDefaultGetter<T>(ColumnType type)
{
Contracts.CheckValue(type, nameof(type));
Contracts.CheckParam(type.RawType == typeof(T), nameof(type));
Delegate del;
if (!_getNADelegates.TryGetValue(type.RawKind, out del))
return (ref T res) => res = default(T);
return (ValueGetter<T>)del;
}
// The IsNA methods are for efficient delegates (instance instead of static).
#region IsNA
private bool IsNA(in R4 src) => R4.IsNaN(src);
private bool IsNA(in R8 src) => R8.IsNaN(src);
#endregion IsNA
#region HasNA
private bool HasNA(in VBuffer<R4> src) { var srcValues = src.GetValues(); for (int i = 0; i < srcValues.Length; i++) { if (R4.IsNaN(srcValues[i])) return true; } return false; }
private bool HasNA(in VBuffer<R8> src) { var srcValues = src.GetValues(); for (int i = 0; i < srcValues.Length; i++) { if (R8.IsNaN(srcValues[i])) return true; } return false; }
#endregion HasNA
#region IsDefault
private bool IsDefault(in I1 src) => src == default(I1);
private bool IsDefault(in I2 src) => src == default(I2);
private bool IsDefault(in I4 src) => src == default(I4);
private bool IsDefault(in I8 src) => src == default(I8);
private bool IsDefault(in R4 src) => src == 0;
private bool IsDefault(in R8 src) => src == 0;
private bool IsDefault(in TX src) => src.IsEmpty;
private bool IsDefault(in BL src) => src == default;
private bool IsDefault(in U1 src) => src == 0;
private bool IsDefault(in U2 src) => src == 0;
private bool IsDefault(in U4 src) => src == 0;
private bool IsDefault(in U8 src) => src == 0;
private bool IsDefault(in UG src) => src.Equals(default(UG));
private bool IsDefault(in TS src) => src.Equals(default(TS));
private bool IsDefault(in DT src) => src.Equals(default(DT));
private bool IsDefault(in DZ src) => src.Equals(default(DZ));
#endregion IsDefault
#region HasZero
private bool HasZero(in VBuffer<U1> src) { if (!src.IsDense) return true; var srcValues = src.GetValues(); for (int i = 0; i < srcValues.Length; i++) { if (srcValues[i] == 0) return true; } return false; }
private bool HasZero(in VBuffer<U2> src) { if (!src.IsDense) return true; var srcValues = src.GetValues(); for (int i = 0; i < srcValues.Length; i++) { if (srcValues[i] == 0) return true; } return false; }
private bool HasZero(in VBuffer<U4> src) { if (!src.IsDense) return true; var srcValues = src.GetValues(); for (int i = 0; i < srcValues.Length; i++) { if (srcValues[i] == 0) return true; } return false; }
private bool HasZero(in VBuffer<U8> src) { if (!src.IsDense) return true; var srcValues = src.GetValues(); for (int i = 0; i < srcValues.Length; i++) { if (srcValues[i] == 0) return true; } return false; }
#endregion HasZero
#region GetNA
private void GetNA(ref R4 value) => value = R4.NaN;
private void GetNA(ref R8 value) => value = R8.NaN;
#endregion GetNA
#region ToI1
public void Convert(in I1 src, ref I1 dst) => dst = src;
public void Convert(in I2 src, ref I1 dst) => dst = (I1)src;
public void Convert(in I4 src, ref I1 dst) => dst = (I1)src;
public void Convert(in I8 src, ref I1 dst) => dst = (I1)src;
#endregion ToI1
#region ToI2
public void Convert(in I1 src, ref I2 dst) => dst = src;
public void Convert(in I2 src, ref I2 dst) => dst = src;
public void Convert(in I4 src, ref I2 dst) => dst = (I2)src;
public void Convert(in I8 src, ref I2 dst) => dst = (I2)src;
#endregion ToI2
#region ToI4
public void Convert(in I1 src, ref I4 dst) => dst = src;
public void Convert(in I2 src, ref I4 dst) => dst = src;
public void Convert(in I4 src, ref I4 dst) => dst = src;
public void Convert(in I8 src, ref I4 dst) => dst = (I4)src;
#endregion ToI4
#region ToI8
public void Convert(in I1 src, ref I8 dst) => dst = src;
public void Convert(in I2 src, ref I8 dst) => dst = src;
public void Convert(in I4 src, ref I8 dst) => dst = src;
public void Convert(in I8 src, ref I8 dst) => dst = src;
public void Convert(in TS src, ref I8 dst) => dst = (I8)src.Ticks;
public void Convert(in DT src, ref I8 dst) => dst = (I8)src.Ticks;
public void Convert(in DZ src, ref I8 dst) => dst = (I8)src.UtcDateTime.Ticks;
#endregion ToI8
#region ToU1
public void Convert(in U1 src, ref U1 dst) => dst = src;
public void Convert(in U2 src, ref U1 dst) => dst = src <= U1.MaxValue ? (U1)src : (U1)0;
public void Convert(in U4 src, ref U1 dst) => dst = src <= U1.MaxValue ? (U1)src : (U1)0;
public void Convert(in U8 src, ref U1 dst) => dst = src <= U1.MaxValue ? (U1)src : (U1)0;
public void Convert(in UG src, ref U1 dst) => dst = src.High == 0 && src.Low <= U1.MaxValue ? (U1)src.Low : (U1)0;
#endregion ToU1
#region ToU2
public void Convert(in U1 src, ref U2 dst) => dst = src;
public void Convert(in U2 src, ref U2 dst) => dst = src;
public void Convert(in U4 src, ref U2 dst) => dst = src <= U2.MaxValue ? (U2)src : (U2)0;
public void Convert(in U8 src, ref U2 dst) => dst = src <= U2.MaxValue ? (U2)src : (U2)0;
public void Convert(in UG src, ref U2 dst) => dst = src.High == 0 && src.Low <= U2.MaxValue ? (U2)src.Low : (U2)0;
#endregion ToU2
#region ToU4
public void Convert(in U1 src, ref U4 dst) => dst = src;
public void Convert(in U2 src, ref U4 dst) => dst = src;
public void Convert(in U4 src, ref U4 dst) => dst = src;
public void Convert(in U8 src, ref U4 dst) => dst = src <= U4.MaxValue ? (U4)src : (U4)0;
public void Convert(in UG src, ref U4 dst) => dst = src.High == 0 && src.Low <= U4.MaxValue ? (U4)src.Low : (U4)0;
#endregion ToU4
#region ToU8
public void Convert(in U1 src, ref U8 dst) => dst = src;
public void Convert(in U2 src, ref U8 dst) => dst = src;
public void Convert(in U4 src, ref U8 dst) => dst = src;
public void Convert(in U8 src, ref U8 dst) => dst = src;
public void Convert(in UG src, ref U8 dst) => dst = src.High == 0 ? src.Low : (U8)0;
#endregion ToU8
#region ToUG
public void Convert(in U1 src, ref UG dst) => dst = new UG(src, 0);
public void Convert(in U2 src, ref UG dst) => dst = new UG(src, 0);
public void Convert(in U4 src, ref UG dst) => dst = new UG(src, 0);
public void Convert(in U8 src, ref UG dst) => dst = new UG(src, 0);
public void Convert(in UG src, ref UG dst) => dst = src;
#endregion ToUG
#region ToR4
public void Convert(in I1 src, ref R4 dst) => dst = (R4)src;
public void Convert(in I2 src, ref R4 dst) => dst = (R4)src;
public void Convert(in I4 src, ref R4 dst) => dst = (R4)src;
public void Convert(in I8 src, ref R4 dst) => dst = (R4)src;
public void Convert(in U1 src, ref R4 dst) => dst = src;
public void Convert(in U2 src, ref R4 dst) => dst = src;
public void Convert(in U4 src, ref R4 dst) => dst = src;
// REVIEW: The 64-bit JIT has a bug in that it rounds incorrectly from ulong
// to floating point when the high bit of the ulong is set. Should we work around the bug
// or just live with it? See the DoubleParser code for details.
public void Convert(in U8 src, ref R4 dst) => dst = src;
public void Convert(in TS src, ref R4 dst) => dst = (R4)src.Ticks;
public void Convert(in DT src, ref R4 dst) => dst = (R4)src.Ticks;
public void Convert(in DZ src, ref R4 dst) => dst = (R4)src.UtcDateTime.Ticks;
#endregion ToR4
#region ToR8
public void Convert(in I1 src, ref R8 dst) => dst = (R8)src;
public void Convert(in I2 src, ref R8 dst) => dst = (R8)src;
public void Convert(in I4 src, ref R8 dst) => dst = (R8)src;
public void Convert(in I8 src, ref R8 dst) => dst = (R8)src;
public void Convert(in U1 src, ref R8 dst) => dst = src;
public void Convert(in U2 src, ref R8 dst) => dst = src;
public void Convert(in U4 src, ref R8 dst) => dst = src;
// REVIEW: The 64-bit JIT has a bug in that it rounds incorrectly from ulong
// to floating point when the high bit of the ulong is set. Should we work around the bug
// or just live with it? See the DoubleParser code for details.
public void Convert(in U8 src, ref R8 dst) => dst = src;
public void Convert(in TS src, ref R8 dst) => dst = (R8)src.Ticks;
public void Convert(in DT src, ref R8 dst) => dst = (R8)src.Ticks;
public void Convert(in DZ src, ref R8 dst) => dst = (R8)src.UtcDateTime.Ticks;
#endregion ToR8
#region ToStringBuilder
public void Convert(in I1 src, ref SB dst) { ClearDst(ref dst); dst.Append(src); }
public void Convert(in I2 src, ref SB dst) { ClearDst(ref dst); dst.Append(src); }
public void Convert(in I4 src, ref SB dst) { ClearDst(ref dst); dst.Append(src); }
public void Convert(in I8 src, ref SB dst) { ClearDst(ref dst); dst.Append(src); }
public void Convert(in U1 src, ref SB dst) => ClearDst(ref dst).Append(src);
public void Convert(in U2 src, ref SB dst) => ClearDst(ref dst).Append(src);
public void Convert(in U4 src, ref SB dst) => ClearDst(ref dst).Append(src);
public void Convert(in U8 src, ref SB dst) => ClearDst(ref dst).Append(src);
public void Convert(in UG src, ref SB dst) { ClearDst(ref dst); dst.AppendFormat("0x{0:x16}{1:x16}", src.High, src.Low); }
public void Convert(in R4 src, ref SB dst) { ClearDst(ref dst); if (R4.IsNaN(src)) dst.AppendFormat(CultureInfo.InvariantCulture, "{0}", "?"); else dst.AppendFormat(CultureInfo.InvariantCulture, "{0:R}", src); }
public void Convert(in R8 src, ref SB dst) { ClearDst(ref dst); if (R8.IsNaN(src)) dst.AppendFormat(CultureInfo.InvariantCulture, "{0}", "?"); else dst.AppendFormat(CultureInfo.InvariantCulture, "{0:G17}", src); }
public void Convert(in BL src, ref SB dst)
{
ClearDst(ref dst);
if (!src)
dst.Append("0");
else
dst.Append("1");
}
public void Convert(in TS src, ref SB dst) { ClearDst(ref dst); dst.AppendFormat("{0:c}", src); }
public void Convert(in DT src, ref SB dst) { ClearDst(ref dst); dst.AppendFormat("{0:o}", src); }
public void Convert(in DZ src, ref SB dst) { ClearDst(ref dst); dst.AppendFormat("{0:o}", src); }
#endregion ToStringBuilder
#region FromR4
public void Convert(in R4 src, ref R4 dst) => dst = src;
public void Convert(in R4 src, ref R8 dst) => dst = src;
#endregion FromR4
#region FromR8
public void Convert(in R8 src, ref R4 dst) => dst = (R4)src;
public void Convert(in R8 src, ref R8 dst) => dst = src;
#endregion FromR8
#region FromTX