~ubuntu-branches/ubuntu/vivid/vowpal-wabbit/vivid

« back to all changes in this revision

Viewing changes to vowpalwabbit/vwdll.cpp

  • Committer: Package Import Robot
  • Author(s): Yaroslav Halchenko
  • Date: 2013-08-27 20:52:23 UTC
  • mfrom: (1.2.1) (7.1.2 experimental)
  • Revision ID: package-import@ubuntu.com-20130827205223-q005ps71tqinh25v
Tags: 7.3-1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <memory>
 
2
#include <codecvt>
 
3
#include <locale>
 
4
#include <string>
 
5
 
 
6
#include "vwdll.h"
 
7
#include "parser.h"
 
8
#include "parse_args.h"
 
9
#include "vw.h"
 
10
 
 
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.
 
17
 
 
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.
 
21
 
 
22
extern "C"
 
23
{
 
24
 
 
25
        VW_DLL_MEMBER VW_HANDLE VW_CALLING_CONV VW_Initialize(const char16_t * pstrArgs)
 
26
        {
 
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());
 
30
        }
 
31
 
 
32
        
 
33
        VW_DLL_MEMBER VW_HANDLE VW_CALLING_CONV VW_InitializeA(const char * pstrArgs)
 
34
        {
 
35
                string s(pstrArgs);
 
36
                vw* all = VW::initialize(s);
 
37
                initialize_parser_datastructures(*all);
 
38
                return static_cast<VW_HANDLE>(all);
 
39
        }
 
40
        
 
41
        VW_DLL_MEMBER void      VW_CALLING_CONV VW_Finish(VW_HANDLE handle)
 
42
        {
 
43
                vw * pointer = static_cast<vw*>(handle);
 
44
                if (pointer->numpasses > 1)
 
45
                        {
 
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); 
 
51
                        }
 
52
                else
 
53
                        release_parser_datastructures(*pointer);
 
54
 
 
55
                VW::finish(*pointer);
 
56
        }
 
57
 
 
58
        VW_DLL_MEMBER VW_EXAMPLE VW_CALLING_CONV VW_ImportExample(VW_HANDLE handle, VW_FEATURE_SPACE* features, size_t len)
 
59
        {
 
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));
 
63
        }
 
64
        
 
65
        VW_DLL_MEMBER VW_FEATURE_SPACE VW_CALLING_CONV VW_ExportExample(VW_HANDLE handle, VW_EXAMPLE e, size_t * plen)
 
66
        {
 
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));
 
70
        }
 
71
 
 
72
        VW_DLL_MEMBER void VW_CALLING_CONV VW_ReleaseFeatureSpace(VW_FEATURE_SPACE* features, size_t len)
 
73
        {
 
74
                VW::primitive_feature_space * f = reinterpret_cast<VW::primitive_feature_space*>( features );
 
75
                VW::releaseFeatureSpace(f, len);
 
76
        }
 
77
        
 
78
        VW_DLL_MEMBER VW_EXAMPLE VW_CALLING_CONV VW_ReadExample(VW_HANDLE handle, const char16_t * line)
 
79
        {
 
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());
 
83
        }
 
84
        
 
85
        VW_DLL_MEMBER VW_EXAMPLE VW_CALLING_CONV VW_ReadExampleA(VW_HANDLE handle, const char * line)
 
86
        {
 
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)));
 
90
        }
 
91
        
 
92
        VW_DLL_MEMBER void VW_CALLING_CONV VW_StartParser(VW_HANDLE handle, bool do_init)
 
93
        {
 
94
                vw * pointer = static_cast<vw*>(handle);
 
95
                VW::start_parser(*pointer, do_init);
 
96
        }
 
97
 
 
98
        VW_DLL_MEMBER void VW_CALLING_CONV VW_EndParser(VW_HANDLE handle)
 
99
        {
 
100
                vw * pointer = static_cast<vw*>(handle);
 
101
                VW::end_parser(*pointer);
 
102
        }
 
103
 
 
104
        VW_DLL_MEMBER VW_EXAMPLE VW_CALLING_CONV VW_GetExample(VW_HANDLE handle)
 
105
        {
 
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));
 
109
        }
 
110
 
 
111
        VW_DLL_MEMBER void VW_CALLING_CONV VW_FinishExample(VW_HANDLE handle, VW_EXAMPLE e)
 
112
        {
 
113
                vw * pointer = static_cast<vw*>(handle);
 
114
                VW::finish_example(*pointer, static_cast<example*>(e));
 
115
        }
 
116
 
 
117
        VW_DLL_MEMBER size_t VW_CALLING_CONV VW_HashSpace(VW_HANDLE handle, const char16_t * s)
 
118
        {
 
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());
 
122
        }
 
123
 
 
124
        VW_DLL_MEMBER size_t VW_CALLING_CONV VW_HashSpaceA(VW_HANDLE handle, const char * s)
 
125
        {
 
126
                vw * pointer = static_cast<vw*>(handle);
 
127
                string str(s);
 
128
                return VW::hash_space(*pointer, str);
 
129
        }
 
130
 
 
131
 
 
132
        VW_DLL_MEMBER size_t VW_CALLING_CONV VW_HashFeature(VW_HANDLE handle, const char16_t * s, unsigned long u)
 
133
        {
 
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);
 
137
        }
 
138
 
 
139
 
 
140
        VW_DLL_MEMBER size_t VW_CALLING_CONV VW_HashFeatureA(VW_HANDLE handle, const char * s, unsigned long u)
 
141
        {
 
142
                vw * pointer = static_cast<vw*>(handle);
 
143
                string str(s);
 
144
                return VW::hash_feature(*pointer, str, u);
 
145
        }
 
146
        
 
147
        VW_DLL_MEMBER void  VW_CALLING_CONV VW_AddLabel(VW_EXAMPLE e, float label, float weight, float base)
 
148
        {
 
149
                example* ex = static_cast<example*>(e);
 
150
                return VW::add_label(ex, label, weight, base);
 
151
        }
 
152
 
 
153
        VW_DLL_MEMBER float VW_CALLING_CONV VW_Learn(VW_HANDLE handle, VW_EXAMPLE e)
 
154
        {
 
155
                vw * pointer = static_cast<vw*>(handle);
 
156
                example * ex = static_cast<example*>(e);
 
157
                pointer->learn(ex);
 
158
                return ex->final_prediction;
 
159
        }
 
160
 
 
161
        VW_DLL_MEMBER float VW_CALLING_CONV VW_Get_Weight(VW_HANDLE handle, size_t index)
 
162
        {
 
163
                vw* pointer = static_cast<vw*>(handle);
 
164
                return VW::get_weight(*pointer, (uint32_t) index);
 
165
        }
 
166
 
 
167
        VW_DLL_MEMBER size_t VW_CALLING_CONV VW_Num_Weights(VW_HANDLE handle)
 
168
        {
 
169
                vw* pointer = static_cast<vw*>(handle);
 
170
                return VW::num_weights(*pointer);
 
171
        }
 
172
}