-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcosts.hpp
More file actions
114 lines (100 loc) · 3.01 KB
/
costs.hpp
File metadata and controls
114 lines (100 loc) · 3.01 KB
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
#pragma once
#include <map>
#include "common.hpp"
#include "util.hpp"
#include "logger.hpp"
/*
Short class that handles cost information.
*/
namespace marzone {
typedef struct costField
{
int id; // original cost id
unsigned index; // zero-indexed cost id. Costs are indexed based on the order they appear.
} costField;
class Costs {
public:
Costs(sfname& fnames, Logger& logger) : costCount(0) {
if (!fnames.costsname.empty())
{
LoadCostNames(fnames.inputdir + fnames.costsname, logger);
}
else
{
DefaultCostNames();
}
}
// Prerequisite - costName must be defined in original cost file
unsigned GetCostIndex(string costName) {
return costNames[costName].index;
}
unsigned GetCostIndex(int costId) {
return costIds[costId];
}
// Checks if costName is defined
bool Contains(string costName) {
if (costNames.find(costName) == costNames.end()) {
return false;
}
return true;
}
// Debugging functions (for dumping)
void DumpCostNames(string filename)
{
ofstream myfile;
myfile.open(filename);
myfile << "costid,costname\n";
for (auto& [name, term]: costNames)
{
myfile << term.id << "," << name << "\n";
}
myfile.close();
}
unsigned costCount;
private:
void LoadCostNames(string filename, Logger& logger)
{
ifstream fp = openFile(filename);
string sLine;
int id;
string name;
// create the CostNames array
// load the data to an array
bool file_is_empty = true;
for (int line_num = 1; getline(fp, sLine); line_num++)
{
file_is_empty = false;
if (line_num == 1)
{
if (is_like_numerical_data(sLine))
logger.ShowWarningMessage("File " + filename + " has no header in the first line.\n");
else
continue;//skip header
}
if (sLine.empty())
continue;
stringstream ss = stream_line(sLine);
ss >> id;
string costName;
ss >> costName;
if (ss.fail())
logger.ShowErrorMessage("File " + filename + " has incorrect values at line " + to_string(line_num) + ".\n");
trim(costName);
costNames[costName] = {id, costCount};
costIds[id] = costCount;
costCount++;
}
fp.close();
if (file_is_empty)
logger.ShowErrorMessage("File " + filename + " cannot be read or is empty.\n");;
}
void DefaultCostNames()
{
costCount= 1;
costIds[1] = 0u;
costNames["cost"] = {1, 0u};
}
map<string, costField> costNames;
map<int, unsigned> costIds; // cost id to index.
};
} // namespace marzone