8
#include "parse_args.h"
11
// This interface now provides "wide" functions for compatibility with .NET interop
12
// The default functions assume a wide (16 bit char pointer) that is converted to a utf8-string and passed to
13
// a function which takes a narrow (8 bit char pointer) function. Both are exposed in the c/c++ API
14
// so that programs using 8 bit wide characters can use the direct call without conversion and
15
// programs using 16 bit characters can use the default wide versions of the functions.
16
// "Ansi versions (FcnA instead of Fcn) have only been written for functions which handle strings.
18
// a future optimization would be to write an inner version of hash feature which either hashed the
19
// wide string directly (and live with the different hash values) or incorporate the UTF-16 to UTF-8 conversion
20
// in the hashing to avoid allocating an intermediate string.
25
VW_DLL_MEMBER VW_HANDLE VW_CALLING_CONV VW_Initialize(const char16_t * pstrArgs)
27
std::wstring_convert<std::codecvt_utf8<char16_t>, char16_t> convert;
28
std::string sa(convert.to_bytes(pstrArgs));
29
return VW_InitializeA(sa.c_str());
33
VW_DLL_MEMBER VW_HANDLE VW_CALLING_CONV VW_InitializeA(const char * pstrArgs)
36
vw* all = VW::initialize(s);
37
initialize_parser_datastructures(*all);
38
return static_cast<VW_HANDLE>(all);
41
VW_DLL_MEMBER void VW_CALLING_CONV VW_Finish(VW_HANDLE handle)
43
vw * pointer = static_cast<vw*>(handle);
44
if (pointer->numpasses > 1)
46
adjust_used_index(*pointer);
47
pointer->do_reset_source = true;
48
VW::start_parser(*pointer,false);
49
pointer->l.drive(pointer);
50
VW::end_parser(*pointer);
53
release_parser_datastructures(*pointer);
58
VW_DLL_MEMBER VW_EXAMPLE VW_CALLING_CONV VW_ImportExample(VW_HANDLE handle, VW_FEATURE_SPACE* features, size_t len)
60
vw * pointer = static_cast<vw*>(handle);
61
VW::primitive_feature_space * f = reinterpret_cast<VW::primitive_feature_space*>( features );
62
return static_cast<VW_EXAMPLE>(VW::import_example(*pointer, f, len));
65
VW_DLL_MEMBER VW_FEATURE_SPACE VW_CALLING_CONV VW_ExportExample(VW_HANDLE handle, VW_EXAMPLE e, size_t * plen)
67
vw* pointer = static_cast<vw*>(handle);
68
example* ex = static_cast<example*>(e);
69
return static_cast<VW_FEATURE_SPACE>(VW::export_example(*pointer, ex, *plen));
72
VW_DLL_MEMBER void VW_CALLING_CONV VW_ReleaseFeatureSpace(VW_FEATURE_SPACE* features, size_t len)
74
VW::primitive_feature_space * f = reinterpret_cast<VW::primitive_feature_space*>( features );
75
VW::releaseFeatureSpace(f, len);
78
VW_DLL_MEMBER VW_EXAMPLE VW_CALLING_CONV VW_ReadExample(VW_HANDLE handle, const char16_t * line)
80
std::wstring_convert<std::codecvt_utf8<char16_t>, char16_t> convert;
81
std::string sa(convert.to_bytes(line));
82
return VW_ReadExampleA(handle, sa.c_str());
85
VW_DLL_MEMBER VW_EXAMPLE VW_CALLING_CONV VW_ReadExampleA(VW_HANDLE handle, const char * line)
87
vw * pointer = static_cast<vw*>(handle);
88
// BUGBUG: I really dislike this const_cast. should VW really change the input string?
89
return static_cast<VW_EXAMPLE>(VW::read_example(*pointer, const_cast<char*>(line)));
92
VW_DLL_MEMBER void VW_CALLING_CONV VW_StartParser(VW_HANDLE handle, bool do_init)
94
vw * pointer = static_cast<vw*>(handle);
95
VW::start_parser(*pointer, do_init);
98
VW_DLL_MEMBER void VW_CALLING_CONV VW_EndParser(VW_HANDLE handle)
100
vw * pointer = static_cast<vw*>(handle);
101
VW::end_parser(*pointer);
104
VW_DLL_MEMBER VW_EXAMPLE VW_CALLING_CONV VW_GetExample(VW_HANDLE handle)
106
vw * pointer = static_cast<vw*>(handle);
107
parser * parser_pointer = static_cast<parser *>(pointer->p);
108
return static_cast<VW_EXAMPLE>(VW::get_example(parser_pointer));
111
VW_DLL_MEMBER void VW_CALLING_CONV VW_FinishExample(VW_HANDLE handle, VW_EXAMPLE e)
113
vw * pointer = static_cast<vw*>(handle);
114
VW::finish_example(*pointer, static_cast<example*>(e));
117
VW_DLL_MEMBER size_t VW_CALLING_CONV VW_HashSpace(VW_HANDLE handle, const char16_t * s)
119
std::wstring_convert<std::codecvt_utf8<char16_t>, char16_t> convert;
120
std::string sa(convert.to_bytes(s));
121
return VW_HashSpaceA(handle,sa.c_str());
124
VW_DLL_MEMBER size_t VW_CALLING_CONV VW_HashSpaceA(VW_HANDLE handle, const char * s)
126
vw * pointer = static_cast<vw*>(handle);
128
return VW::hash_space(*pointer, str);
132
VW_DLL_MEMBER size_t VW_CALLING_CONV VW_HashFeature(VW_HANDLE handle, const char16_t * s, unsigned long u)
134
std::wstring_convert<std::codecvt_utf8<char16_t>, char16_t> convert;
135
std::string sa(convert.to_bytes(s));
136
return VW_HashFeatureA(handle,sa.c_str(),u);
140
VW_DLL_MEMBER size_t VW_CALLING_CONV VW_HashFeatureA(VW_HANDLE handle, const char * s, unsigned long u)
142
vw * pointer = static_cast<vw*>(handle);
144
return VW::hash_feature(*pointer, str, u);
147
VW_DLL_MEMBER void VW_CALLING_CONV VW_AddLabel(VW_EXAMPLE e, float label, float weight, float base)
149
example* ex = static_cast<example*>(e);
150
return VW::add_label(ex, label, weight, base);
153
VW_DLL_MEMBER float VW_CALLING_CONV VW_Learn(VW_HANDLE handle, VW_EXAMPLE e)
155
vw * pointer = static_cast<vw*>(handle);
156
example * ex = static_cast<example*>(e);
158
return ex->final_prediction;
161
VW_DLL_MEMBER float VW_CALLING_CONV VW_Get_Weight(VW_HANDLE handle, size_t index)
163
vw* pointer = static_cast<vw*>(handle);
164
return VW::get_weight(*pointer, (uint32_t) index);
167
VW_DLL_MEMBER size_t VW_CALLING_CONV VW_Num_Weights(VW_HANDLE handle)
169
vw* pointer = static_cast<vw*>(handle);
170
return VW::num_weights(*pointer);