-
Notifications
You must be signed in to change notification settings - Fork 0
/
ParseParameters.cpp
74 lines (63 loc) · 2.23 KB
/
ParseParameters.cpp
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
#include <iostream>
#include <cstdlib>
#include <iomanip>
#include "ParseParameters.h"
#include "Exceptions.h"
ParseParameters *ParseParameters::mInstance = NULL;
ParseParameters *ParseParameters::getInstance(void) {
if (!mInstance)
mInstance = new ParseParameters;
return mInstance;
}
ParseParameters::ParseParameters() {
// Valid parameters with their default values
mDictionary["w0"] = 0.4;
mDictionary["k"] = 0.2;
#ifdef USE_ORIGINAL_PROPORTIONS
mDictionary["p0"] = 1.0;
mDictionary["p1"] = 0.1;
#else
mDictionary["p0"] = 0.25; // This way all the 4 proportions are equal to 0.25
mDictionary["p1"] = 0.25;
#endif
mDictionary["w2"] = 1.1;
}
void ParseParameters::addParameter(const char *aParamValuePair) {
// Parse string in the format: name=value (or name:value)
const char *p = aParamValuePair;
size_t i = 0;
for (; *p != '=' && *p != ':' && *p != '\0'; ++p, ++i) {
}
if (*p == '\0')
throw FastCodeMLFatal("Added pair with missing value in addParameter");
if (i == 0)
throw FastCodeMLFatal("Added pair with missing param name in addParameter");
// Extract the name part that should be present in the dictionary
std::string name(aParamValuePair, i);
if (mDictionary.count(name) < 1)
throw FastCodeMLFatal("Try to add a non-existing key in addParameter");
// Change the value corresponding to the parameter name
mDictionary[name] = atof(p + 1);
}
double ParseParameters::getParameter(const char *aParamName) const {
// The name should exist in the dictionary
std::map<std::string, double>::const_iterator ip(
mDictionary.find(aParamName));
if (ip == mDictionary.end())
throw FastCodeMLFatal("Invalid key requested in getParameter");
// Return the corresponding value
return ip->second;
}
std::ostream &operator<<(std::ostream &aOut,
const ParseParameters *aParamsList) {
// Print the dictionary
std::map<std::string, double>::const_iterator ip(
aParamsList->mDictionary.begin());
const std::map<std::string, double>::const_iterator end(
aParamsList->mDictionary.end());
for (; ip != end; ++ip) {
aOut << std::setw(20) << ip->first << " = " << std::fixed
<< std::setprecision(6) << ip->second << std::endl;
}
return aOut;
}