@@ -23,45 +23,45 @@ namespace VW.Serializer
23
23
{
24
24
public partial class VowpalWabbitDefaultMarshaller
25
25
{
26
- <# foreach(var t in new[] { typeof(byte), typeof(sbyte), typeof(Int16), typeof(Int32), typeof(UInt16), typeof(UInt32), typeof(float), typeof(Int64), typeof(UInt64), typeof(double) }) { #>
27
- /// <summary>
26
+ <# foreach(var t in new[] { typeof(byte), typeof(sbyte), typeof(Int16), typeof(Int32), typeof(UInt16), typeof(UInt32), typeof(float), typeof(Int64), typeof(UInt64), typeof(double) }) { #>
27
+ /// <summary>
28
28
/// Transfers feature data to native space.
29
29
/// </summary>
30
- /// <param name="context">The marshalling context.</param>
31
- /// <param name="ns">The namespace description.</param>
32
- /// <param name="feature">The feature description.</param>
33
- /// <param name="value">The feature value.</param>
30
+ /// <param name="context">The marshalling context.</param>
31
+ /// <param name="ns">The namespace description.</param>
32
+ /// <param name="feature">The feature description.</param>
33
+ /// <param name="value">The feature value.</param>
34
34
public void MarshalFeature(VowpalWabbitMarshalContext context, Namespace ns, PreHashedFeature feature, <#=t#> value)
35
35
{
36
36
Contract.Requires(context != null);
37
37
Contract.Requires(ns != null);
38
38
Contract.Requires(feature != null);
39
39
40
- <# if(new[]{ typeof(Int64), typeof(UInt64), typeof(double) }.Contains(t)) {#>
41
- #if DEBUG
40
+ <# if(new[]{ typeof(Int64), typeof(UInt64), typeof(double) }.Contains(t)) {#>
41
+ #if DEBUG
42
42
if (value > float.MaxValue || value < float.MinValue)
43
43
{
44
44
Trace.TraceWarning("Precision lost for feature value: " + value);
45
45
}
46
- #endif
47
- <# } #>
46
+ #endif
47
+ <# } #>
48
48
49
- context.NamespaceBuilder.AddFeature(feature.FeatureHash, (float)value);
49
+ context.NamespaceBuilder.AddFeature(feature.FeatureHash, (float)value);
50
50
51
51
context.AppendStringExample(
52
- feature.Dictify,
52
+ feature.Dictify,
53
53
" {0}:" + (context.VW.Settings.EnableStringFloatCompact ? "{1}" : "{1:E20}"),
54
54
feature.Name,
55
55
value);
56
56
}
57
57
58
- /// <summary>
58
+ /// <summary>
59
59
/// Transfers feature data to native space.
60
60
/// </summary>
61
- /// <param name="context">The marshalling context.</param>
62
- /// <param name="ns">The namespace description.</param>
63
- /// <param name="feature">The feature description.</param>
64
- /// <param name="value">The feature value.</param>
61
+ /// <param name="context">The marshalling context.</param>
62
+ /// <param name="ns">The namespace description.</param>
63
+ /// <param name="feature">The feature description.</param>
64
+ /// <param name="value">The feature value.</param>
65
65
public unsafe void MarshalFeature(VowpalWabbitMarshalContext context, Namespace ns, Feature feature, <#=t#>[] value)
66
66
{
67
67
if (value == null)
@@ -74,57 +74,57 @@ namespace VW.Serializer
74
74
// support anchor feature
75
75
if (feature.AddAnchor)
76
76
{
77
- context.NamespaceBuilder.PreAllocate(value.Length + 1);
77
+ context.NamespaceBuilder.PreAllocate(value.Length + 1);
78
78
79
79
context.NamespaceBuilder.AddFeature(ns.NamespaceHash, 1);
80
80
i++;
81
81
}
82
- else
83
- {
84
- context.NamespaceBuilder.PreAllocate(value.Length);
85
- }
82
+ else
83
+ {
84
+ context.NamespaceBuilder.PreAllocate(value.Length);
85
+ }
86
86
87
- <# if (t == typeof(float)) { #>
87
+ <# if (t == typeof(float)) { #>
88
88
89
- fixed (float* begin = value)
90
- {
91
- context.NamespaceBuilder.AddFeaturesUnchecked((ulong)(ns.NamespaceHash + i), begin, begin + value.Length);
92
- }
89
+ fixed (float* begin = value)
90
+ {
91
+ context.NamespaceBuilder.AddFeaturesUnchecked((ulong)(ns.NamespaceHash + i), begin, begin + value.Length);
92
+ }
93
93
94
- <# } else { #>
94
+ <# } else { #>
95
95
96
96
foreach (var v in value)
97
97
{
98
- <# if(new[]{ typeof(Int64), typeof(UInt64), typeof(double) }.Contains(t)) {#>
99
- #if DEBUG
100
- if (v > float.MaxValue || v < float.MinValue)
101
- {
102
- Trace.TraceWarning("Precision lost for feature value: " + v);
103
- }
104
- #endif
105
- <# } #>
98
+ <# if(new[]{ typeof(Int64), typeof(UInt64), typeof(double) }.Contains(t)) {#>
99
+ #if DEBUG
100
+ if (v > float.MaxValue || v < float.MinValue)
101
+ {
102
+ Trace.TraceWarning("Precision lost for feature value: " + v);
103
+ }
104
+ #endif
105
+ <# } #>
106
106
107
107
context.NamespaceBuilder.AddFeature((ulong)(ns.NamespaceHash + i), (float)v);
108
108
i++;
109
109
}
110
- <# } #>
110
+ <# } #>
111
111
112
112
if (context.StringExample == null)
113
113
{
114
114
return;
115
115
}
116
116
117
- string featureString;
118
- if (feature.Dictify && context.FastDictionary != null)
119
- {
120
- if (context.FastDictionary.TryGetValue(value, out featureString))
121
- {
122
- context.AppendStringExample(feature.Dictify, featureString);
123
- return;
124
- }
125
- }
117
+ string featureString;
118
+ if (feature.Dictify && context.FastDictionary != null)
119
+ {
120
+ if (context.FastDictionary.TryGetValue(value, out featureString))
121
+ {
122
+ context.AppendStringExample(feature.Dictify, featureString);
123
+ return;
124
+ }
125
+ }
126
126
127
- var featureBuilder = new StringBuilder();
127
+ var featureBuilder = new StringBuilder();
128
128
129
129
// support anchor feature
130
130
i = 0;
@@ -134,34 +134,34 @@ namespace VW.Serializer
134
134
i++;
135
135
}
136
136
137
- var format = " {0}:" + (context.VW.Settings.EnableStringFloatCompact ? "{1}" : "{1:E20}");
137
+ var format = " {0}:" + (context.VW.Settings.EnableStringFloatCompact ? "{1}" : "{1:E20}");
138
138
foreach (var v in value)
139
139
{
140
140
featureBuilder.AppendFormat(
141
- CultureInfo.InvariantCulture,
142
- format,
143
- i,
144
- v);
141
+ CultureInfo.InvariantCulture,
142
+ format,
143
+ i,
144
+ v);
145
145
i++;
146
146
}
147
147
148
- featureString = featureBuilder.ToString();
148
+ featureString = featureBuilder.ToString();
149
149
150
- if (feature.Dictify && context.FastDictionary != null)
151
- {
152
- context.FastDictionary.Add(value, featureString);
153
- }
150
+ if (feature.Dictify && context.FastDictionary != null)
151
+ {
152
+ context.FastDictionary.Add(value, featureString);
153
+ }
154
154
155
- context.AppendStringExample(feature.Dictify, featureString);
155
+ context.AppendStringExample(feature.Dictify, featureString);
156
156
}
157
157
158
- /// <summary>
158
+ /// <summary>
159
159
/// Transfers feature data to native space.
160
160
/// </summary>
161
- /// <param name="context">The marshalling context.</param>
162
- /// <param name="ns">The namespace description.</param>
163
- /// <param name="feature">The feature description.</param>
164
- /// <param name="value">The feature value.</param>
161
+ /// <param name="context">The marshalling context.</param>
162
+ /// <param name="ns">The namespace description.</param>
163
+ /// <param name="feature">The feature description.</param>
164
+ /// <param name="value">The feature value.</param>
165
165
public void MarshalFeature(VowpalWabbitMarshalContext context, Namespace ns, Feature feature, IEnumerable<<#=t#>> value)
166
166
{
167
167
if (value == null)
@@ -180,14 +180,14 @@ namespace VW.Serializer
180
180
181
181
foreach (var v in value)
182
182
{
183
- <# if(new[]{ typeof(Int64), typeof(UInt64), typeof(double) }.Contains(t)) {#>
184
- #if DEBUG
185
- if (v > float.MaxValue || v < float.MinValue)
186
- {
187
- Trace.TraceWarning("Precision lost for feature value: " + v);
188
- }
189
- #endif
190
- <# } #>
183
+ <# if(new[]{ typeof(Int64), typeof(UInt64), typeof(double) }.Contains(t)) {#>
184
+ #if DEBUG
185
+ if (v > float.MaxValue || v < float.MinValue)
186
+ {
187
+ Trace.TraceWarning("Precision lost for feature value: " + v);
188
+ }
189
+ #endif
190
+ <# } #>
191
191
192
192
context.NamespaceBuilder.AddFeature((ulong)(ns.NamespaceHash + i), (float)v);
193
193
i++;
@@ -198,17 +198,17 @@ namespace VW.Serializer
198
198
return;
199
199
}
200
200
201
- string featureString;
202
- if (feature.Dictify && context.FastDictionary != null)
203
- {
204
- if (context.FastDictionary.TryGetValue(value, out featureString))
205
- {
206
- context.AppendStringExample(feature.Dictify, featureString);
207
- return;
208
- }
209
- }
201
+ string featureString;
202
+ if (feature.Dictify && context.FastDictionary != null)
203
+ {
204
+ if (context.FastDictionary.TryGetValue(value, out featureString))
205
+ {
206
+ context.AppendStringExample(feature.Dictify, featureString);
207
+ return;
208
+ }
209
+ }
210
210
211
- var featureBuilder = new StringBuilder();
211
+ var featureBuilder = new StringBuilder();
212
212
213
213
// support anchor feature
214
214
i = 0;
@@ -218,84 +218,84 @@ namespace VW.Serializer
218
218
i++;
219
219
}
220
220
221
- var format = " {0}:" + (context.VW.Settings.EnableStringFloatCompact ? "{1}" : "{1:E20}");
221
+ var format = " {0}:" + (context.VW.Settings.EnableStringFloatCompact ? "{1}" : "{1:E20}");
222
222
foreach (var v in value)
223
223
{
224
224
featureBuilder.AppendFormat(
225
- CultureInfo.InvariantCulture,
226
- format,
227
- i,
228
- v);
225
+ CultureInfo.InvariantCulture,
226
+ format,
227
+ i,
228
+ v);
229
229
i++;
230
230
}
231
231
232
- featureString = featureBuilder.ToString();
232
+ featureString = featureBuilder.ToString();
233
233
234
- if (feature.Dictify && context.FastDictionary != null)
235
- {
236
- context.FastDictionary.Add(value, featureString);
237
- }
234
+ if (feature.Dictify && context.FastDictionary != null)
235
+ {
236
+ context.FastDictionary.Add(value, featureString);
237
+ }
238
238
239
- context.AppendStringExample(feature.Dictify, featureString);
239
+ context.AppendStringExample(feature.Dictify, featureString);
240
240
}
241
241
242
- <# } #>
242
+ <# } #>
243
243
244
- <# foreach(var t in new[] { typeof(byte), typeof(sbyte), typeof(Int16), typeof(Int32), typeof(UInt16) }) { #>
244
+ <# foreach(var t in new[] { typeof(byte), typeof(sbyte), typeof(Int16), typeof(Int32), typeof(UInt16) }) { #>
245
245
246
- <# foreach(var s in new[] { typeof(byte), typeof(sbyte), typeof(Int16), typeof(Int32), typeof(UInt16), typeof(UInt32), typeof(float), typeof(Int64), typeof(UInt64), typeof(double) }) { #>
246
+ <# foreach(var s in new[] { typeof(byte), typeof(sbyte), typeof(Int16), typeof(Int32), typeof(UInt16), typeof(UInt32), typeof(float), typeof(Int64), typeof(UInt64), typeof(double) }) { #>
247
247
248
- /// <summary>
248
+ /// <summary>
249
249
/// Transfers feature data to native space.
250
250
/// </summary>
251
- /// <param name="context">The marshalling context.</param>
252
- /// <param name="ns">The namespace description.</param>
253
- /// <param name="feature">The feature description.</param>
254
- /// <param name="value">The feature value.</param>
255
- [ContractVerification(false)]
251
+ /// <param name="context">The marshalling context.</param>
252
+ /// <param name="ns">The namespace description.</param>
253
+ /// <param name="feature">The feature description.</param>
254
+ /// <param name="value">The feature value.</param>
255
+ [ContractVerification(false)]
256
256
public void MarshalFeature(VowpalWabbitMarshalContext context, Namespace ns, Feature feature, IDictionary<<#=t#>, <#=s#>> value)
257
257
{
258
258
Contract.Requires(context != null);
259
259
Contract.Requires(ns != null);
260
260
Contract.Requires(feature != null);
261
261
262
- if (value == null)
263
- {
264
- return;
265
- }
262
+ if (value == null)
263
+ {
264
+ return;
265
+ }
266
266
267
267
foreach (var kvp in value)
268
268
{
269
- <# if(new[]{ typeof(Int64), typeof(UInt64), typeof(double) }.Contains(t)) {#>
270
- #if DEBUG
271
- if (kvp.Value > float.MaxValue || kvp.Value < float.MinValue)
272
- {
273
- Trace.TraceWarning("Precision lost for feature value: " + kvp.Value);
274
- }
275
- #endif
276
- <# } #>
277
-
278
- context.NamespaceBuilder.AddFeature(ns.NamespaceHash + (ulong)kvp.Key, (float)kvp.Value);
269
+ <# if(new[]{ typeof(Int64), typeof(UInt64), typeof(double) }.Contains(t)) {#>
270
+ #if DEBUG
271
+ if (kvp.Value > float.MaxValue || kvp.Value < float.MinValue)
272
+ {
273
+ Trace.TraceWarning("Precision lost for feature value: " + kvp.Value);
274
+ }
275
+ #endif
276
+ <# } #>
277
+
278
+ context.NamespaceBuilder.AddFeature(ns.NamespaceHash + (ulong)kvp.Key, (float)kvp.Value);
279
279
}
280
280
281
- if (context.StringExample == null)
281
+ if (context.StringExample == null)
282
282
{
283
283
return;
284
284
}
285
285
286
- var format = " {0}:" + (context.VW.Settings.EnableStringFloatCompact ? "{1}" : "{1:E20}");
287
- foreach (var kvp in value)
286
+ var format = " {0}:" + (context.VW.Settings.EnableStringFloatCompact ? "{1}" : "{1:E20}");
287
+ foreach (var kvp in value)
288
288
{
289
- // TODO: not sure if negative numbers will work
289
+ // TODO: not sure if negative numbers will work
290
290
context.AppendStringExample(
291
- feature.Dictify,
291
+ feature.Dictify,
292
292
format,
293
293
kvp.Key,
294
294
kvp.Value);
295
- }
295
+ }
296
296
}
297
- <# } #>
297
+ <# } #>
298
298
299
- <# } #>
299
+ <# } #>
300
300
}
301
301
}
0 commit comments