@@ -32,15 +32,27 @@ public sealed class Arguments
32
32
33
33
private sealed class Bindings
34
34
{
35
- // A collection of source column indexes after removing those we want to drop. Specifically, j=_sources[i] means
36
- // that the i-th output column in the output schema is the j-th column in the input schema.
35
+ /// <summary>
36
+ /// A collection of source column indexes after removing those we want to drop. Specifically, j=_sources[i] means
37
+ /// that the i-th output column in the output schema is the j-th column in the input schema.
38
+ /// </summary>
37
39
private readonly int [ ] _sources ;
38
40
39
- // Input schema of this transform. It's useful when determining column dependencies and other
40
- // relations between input and output schemas.
41
+ /// <summary>
42
+ /// Input schema of this transform. It's useful when determining column dependencies and other
43
+ /// relations between input and output schemas.
44
+ /// </summary>
41
45
private readonly Schema _sourceSchema ;
42
46
43
- // Variable used for saving in backward-compatible format. It's not needed actually.
47
+ /// <summary>
48
+ /// Some column indexes in the input schema. <see cref="_sources"/> is computed from <see cref="_selectedColumnIndexes"/>
49
+ /// and <see cref="_drop"/>.
50
+ /// </summary>
51
+ private readonly int [ ] _selectedColumnIndexes ;
52
+
53
+ /// <summary>
54
+ /// True, if this transform drops selected columns indexed by <see cref="_selectedColumnIndexes"/>.
55
+ /// </summary>
44
56
private readonly bool _drop ;
45
57
46
58
// This transform's output schema.
@@ -52,20 +64,34 @@ internal Bindings(Arguments args, Schema sourceSchema)
52
64
Contracts . AssertValue ( sourceSchema ) ;
53
65
54
66
_sourceSchema = sourceSchema ;
67
+
68
+ // Store user-specified arguments as the major state of this transform. Only the major states will
69
+ // be saved and all other attributes can be reconstructed from them.
55
70
_drop = args . Drop ;
71
+ _selectedColumnIndexes = args . Index ;
72
+
73
+ // Compute actually used attributes in runtime from those major states.
74
+ ComputeSources ( _drop , _selectedColumnIndexes , _sourceSchema , out _sources ) ;
75
+
76
+ // All necessary fields in this class are set, so we can compute output schema now.
77
+ OutputSchema = ComputeOutputSchema ( ) ;
78
+ }
56
79
57
- if ( args . Drop )
80
+ /// <summary>
81
+ /// Common method of computing <see cref="_sources"/> from necessary parameters. This function is used in constructors.
82
+ /// </summary>
83
+ private static void ComputeSources ( bool drop , int [ ] selectedColumnIndexes , Schema sourceSchema , out int [ ] sources )
84
+ {
85
+ // Compute the mapping, <see cref="_sources"/>, from output column index to input column index.
86
+ if ( drop )
58
87
// Drop columns indexed by args.Index
59
- _sources = Enumerable . Range ( 0 , _sourceSchema . ColumnCount ) . Except ( args . Index ) . ToArray ( ) ;
88
+ sources = Enumerable . Range ( 0 , sourceSchema . ColumnCount ) . Except ( selectedColumnIndexes ) . ToArray ( ) ;
60
89
else
61
90
// Keep columns indexed by args.Index
62
- _sources = args . Index . ToArray ( ) ;
91
+ sources = selectedColumnIndexes ;
63
92
64
93
// Make sure the output of this transform is meaningful.
65
- Contracts . Check ( _sources . Length > 0 , "Choose columns by index has no output column." ) ;
66
-
67
- // All necessary fields in this class are set, so we can compute output schema now.
68
- OutputSchema = ComputeOutputSchema ( ) ;
94
+ Contracts . Check ( sources . Length > 0 , "Choose columns by index has no output column." ) ;
69
95
}
70
96
71
97
/// <summary>
@@ -97,11 +123,16 @@ internal Bindings(ModelLoadContext ctx, Schema sourceSchema)
97
123
Contracts . AssertValue ( ctx ) ;
98
124
Contracts . AssertValue ( sourceSchema ) ;
99
125
126
+ _sourceSchema = sourceSchema ;
127
+
100
128
// *** Binary format ***
101
129
// bool (as byte): operation mode
102
130
// int[]: selected source column indices
103
131
_drop = ctx . Reader . ReadBoolByte ( ) ;
104
- _sources = ctx . Reader . ReadIntArray ( ) ;
132
+ _selectedColumnIndexes = ctx . Reader . ReadIntArray ( ) ;
133
+
134
+ // Compute actually used attributes in runtime from those major states.
135
+ ComputeSources ( _drop , _selectedColumnIndexes , _sourceSchema , out _sources ) ;
105
136
106
137
_sourceSchema = sourceSchema ;
107
138
OutputSchema = ComputeOutputSchema ( ) ;
@@ -115,7 +146,7 @@ internal void Save(ModelSaveContext ctx)
115
146
// bool (as byte): operation mode
116
147
// int[]: selected source column indices
117
148
ctx . Writer . WriteBoolByte ( _drop ) ;
118
- ctx . Writer . WriteIntArray ( _sources ) ;
149
+ ctx . Writer . WriteIntArray ( _selectedColumnIndexes ) ;
119
150
}
120
151
121
152
internal bool [ ] GetActive ( Func < int , bool > predicate )
0 commit comments