-
Notifications
You must be signed in to change notification settings - Fork 17
/
ModelParser.h
143 lines (137 loc) · 3.66 KB
/
ModelParser.h
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#ifndef _MODELPARSER_H_
#define _MODELPARSER_H_
#include "StringUtil.h"
#include "LogFile.h"
#include "TypeConversion.h"
/**
* Parse "mb" to {"mb"}
* Parse "mb(nperm=10000, alpha=0.1)" then later use assign("nperm", &p) to set value for p
*/
class ModelParser{
public:
/**
* @return 0: if parse succeed.
* NOTE: will convert all letters to lower case
*/
int parse(const char* s){
std::string arg = s;
tolower(&arg);
size_t l = arg.find(ModelParser::LEFT_DELIM);
if ( l == std::string::npos){
this->name = arg;
return 0;
}
this->name = arg.substr(0, l);
if (arg[arg.size() - 1] != ModelParser::RIGHT_DELIM){
LOG <<("Please use this format: model(model_param1=v1)");
return -1;
}
std::vector<std::string> params;
std::string allParam = arg.substr(l + 1, arg.size() - 1 - 1 -l);
stringTokenize(allParam, ',', ¶ms);
for (size_t i = 0; i < params.size(); ++i) {
l = params[i].find('=');
if (l == std::string::npos) {
this->param[params[i]] = "";
} else {
std::string key = params[i].substr(0, l);
std::string value = params[i].substr(l + 1, params[i].size() - l - 1);
this->param[key] = value;
};
};
// LOG <<("load %zu parameters.", this->size());
return 0;
}
int parse(std::string& s){
return this->parse(s.c_str());
}
const std::string& getName() const {
return this->name;
};
/**
* assign @param tag to @param value
*/
ModelParser& assign(const char* tag, bool* value){
if (this->param.find(tag) ){
(*value) = true;
} else {
(*value) = false;
}
return (*this);
};
ModelParser& assign(const char* tag, double* value){
if (this->param.find(tag) ){
(*value) = toDouble(this->param[tag]);
} else {
LOG << "Cannot find parameter [ " << tag << " ]\n";
}
return (*this);
};
ModelParser& assign(const char* tag, int* value){
if (this->param.find(tag) ){
(*value) = toInt(this->param[tag]);
} else {
LOG << "Cannot find parameter [ " << tag << " ]\n";
}
return (*this);
};
ModelParser& assign(const char* tag, std::string* value){
if (this->param.find(tag) ){
(*value) = (this->param[tag]);
} else {
LOG << "Cannot find parameter [ " << tag << " ]\n";
}
return (*this);
};
/**
* if @param tag is provided, assign @param tag to @param value
* otherwise, set @param value to @param def
*/
ModelParser& assign(const char* tag, bool* value, const bool def){
if (this->param.find(tag) ){
(*value) = true;
} else {
(*value) = def;
}
return (*this);
};
ModelParser& assign(const char* tag, double* value, const double def){
if (this->param.find(tag) ){
(*value) = toDouble(this->param[tag]);
} else {
(*value) = def;
}
return (*this);
};
ModelParser& assign(const char* tag, int* value, const int def){
if (this->param.find(tag) ){
(*value) = toInt(this->param[tag]);
} else {
(*value) = def;
}
return (*this);
};
ModelParser& assign(const char* tag, std::string* value, const std::string& def){
if (this->param.find(tag) ){
(*value) = (this->param[tag]);
} else {
(*value) = def;
}
return (*this);
};
const size_t size() const {
return this->param.size();
};
const std::vector<std::string>& getParam() {
return param.getKey();
};
std::string getValue(int i) {
return param[param.getKey()[i]];
};
static const char LEFT_DELIM = '(';
static const char RIGHT_DELIM = ')';
private:
std::string name;
OrderedMap<std::string, std::string> param;
};
#endif /* _MODELPARSER_H_ */