-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathlibsvm_parser.cpp
More file actions
36 lines (31 loc) · 864 Bytes
/
libsvm_parser.cpp
File metadata and controls
36 lines (31 loc) · 864 Bytes
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
#include "boost/utility/string_ref.hpp"
namespace flexps {
using DataObj = std::pair<std::vector<std::pair<int, float>>, float>;
DataObj libsvm_parser(boost::string_ref record) {
DataObj this_obj;
char* pos;
std::unique_ptr<char> record_ptr(new char[record.size() + 1]);
strncpy(record_ptr.get(), record.data(), record.size());
record_ptr.get()[record.size()] = '\0';
char* tok = strtok_r(record_ptr.get(), " \t:", &pos);
int i = -1;
int idx;
float val;
while (tok != NULL) {
if (i == 0) {
idx = std::atoi(tok) - 1;
i = 1;
} else if (i == 1) {
val = std::atof(tok);
this_obj.first.push_back(std::make_pair(idx, val));
i = 0;
} else {
this_obj.second = std::atof(tok);
i = 0;
}
// Next key/value pair
tok = strtok_r(NULL, " \t:", &pos);
}
return this_obj;
}
} // namespace flexps