blob: 59e8420a45d241d350fdf9e2fbbd7fffc67114a0 (
plain)
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
|
#include "options.h"
namespace marian {
Options::Options()
#if FASTOPT
: fastOptions_(options_)
#endif
{}
Options::Options(const Options& other)
#if FASTOPT
: options_(YAML::Clone(other.options_)),
fastOptions_(options_) {}
#else
: options_(YAML::Clone(other.options_)) {}
#endif
Options Options::clone() const {
return Options(*this); // fastOptions_ get set in constructor above
}
YAML::Node Options::cloneToYamlNode() const {
return YAML::Clone(options_); // Do not give access to internal YAML object
}
void Options::parse(const std::string& yaml) {
auto node = YAML::Load(yaml);
for(auto it : node)
options_[it.first.as<std::string>()] = YAML::Clone(it.second);
#if FASTOPT
setLazyRebuild();
#endif
}
void Options::merge(const YAML::Node& node, bool overwrite) {
for(auto it : node)
if(overwrite || !options_[it.first.as<std::string>()])
options_[it.first.as<std::string>()] = YAML::Clone(it.second);
#if FASTOPT
setLazyRebuild();
#endif
}
void Options::merge(Ptr<Options> options) {
merge(options->options_);
}
std::string Options::asYamlString() {
std::stringstream ss;
ss << options_;
return ss.str();
}
bool Options::hasAndNotEmpty(const char* const key) const {
#if FASTOPT
lazyRebuild();
if(!fastOptions_.has(key)) {
return false;
} else {
auto& node = fastOptions_[key];
if(node.isSequence())
return node.size() != 0;
else if(node.isScalar()) // numerical values count as non-empty
return !node.as<std::string>().empty();
else
ABORT("Wrong node type");
}
#else
if(!options_[key]) {
return false;
} else {
auto& node = options_[key];
if(node.IsSequence())
return node.size() != 0;
else if(node.IsScalar()) // numerical values count as non-empty
return !node.as<std::string>().empty();
else
ABORT("Wrong node type");
}
#endif
}
bool Options::hasAndNotEmpty(const std::string& key) const {
return hasAndNotEmpty(key.c_str());
}
bool Options::has(const char* const key) const {
#if FASTOPT
lazyRebuild();
return fastOptions_.has(key);
#else
return options_[key];
#endif
}
bool Options::has(const std::string& key) const {
return has(key.c_str());
}
}
|