-
Notifications
You must be signed in to change notification settings - Fork 94
Expand file tree
/
Copy pathtable.h
More file actions
155 lines (138 loc) · 3.52 KB
/
table.h
File metadata and controls
155 lines (138 loc) · 3.52 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
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
144
145
146
147
148
149
150
151
152
153
154
155
#ifndef UTIL_TABLE_H
#define UTIL_TABLE_H
#include <iomanip>
#include <iostream>
#include <map>
#include <sstream>
#include <vector>
namespace caffe2 {
class Table {
enum class Alignment { None, Left, Right, Internal };
enum class Floatpoint { None, Fixed, Scientific, Hex, Default };
struct Entry {
std::string string;
int width;
Alignment align;
Floatpoint floatpoint;
int precision;
char fill;
Entry()
: string(""),
width(0),
align(Alignment::None),
floatpoint(Floatpoint::None),
precision(0),
fill('\0') {}
};
public:
Table() {}
void Add(const std::string &key, const Entry &entry) {
entries_[key] = entry;
keys_.push_back(key);
}
void AddFixed(const std::string &key, int width, int precision = -1) {
Entry entry;
entry.width = width;
entry.floatpoint = Floatpoint::Fixed;
entry.precision = precision + 1;
Add(key, entry);
}
void AddScientific(const std::string &key, int width, int precision = -1) {
Entry entry;
entry.width = width;
entry.floatpoint = Floatpoint::Scientific;
entry.precision = precision + 1;
Add(key, entry);
}
void Add(const std::string &key, int width) {
Entry entry;
entry.width = width;
Add(key, entry);
}
std::ostream &Setup(const Entry &entry, std::ostream &stream) {
if (entry.width > 0) {
stream << std::setw(entry.width);
}
switch (entry.align) {
case Alignment::Left:
stream << std::left;
break;
case Alignment::Right:
stream << std::right;
break;
case Alignment::Internal:
stream << std::internal;
break;
case Alignment::None:
break;
}
if (entry.fill != '\0') {
stream << std::setfill(entry.fill);
}
switch (entry.floatpoint) {
case Floatpoint::Fixed:
stream << std::fixed;
break;
case Floatpoint::Scientific:
stream << std::scientific;
break;
case Floatpoint::Hex:
stream << std::hexfloat;
break;
case Floatpoint::Default:
stream << std::defaultfloat;
break;
case Floatpoint::None:
break;
}
if (entry.precision > 0) {
stream << std::setprecision(entry.precision - 1);
}
return stream;
}
void Set(const std::string &key, float value) {
std::stringstream stream;
auto &entry = entries_.at(key);
Setup(entry, stream) << value;
entry.string = stream.str();
}
void WriteHeader(std::ostream &stream) const {
for (auto &key : keys_) {
auto &entry = entries_.at(key);
stream << hsep_ << std::setw(entry.width) << std::setfill(' ') << key;
}
stream << hsep_ << std::endl;
if (vsep_.size() > 0) {
for (auto &key : keys_) {
auto &entry = entries_.at(key);
stream << hsep_ << std::setw(entry.width) << std::setfill(vsep_[0])
<< vsep_;
}
stream << hsep_ << std::endl;
}
}
void Write(std::ostream &stream) const {
for (auto &key : keys_) {
auto entry = entries_.at(key);
stream << hsep_ << entry.string;
}
stream << hsep_;
}
void Border(bool show = true) {
if (show) {
hsep_ = " | ";
vsep_ = "-";
} else {
hsep_ = "";
vsep_ = "";
}
}
protected:
std::map<std::string, Entry> entries_;
std::vector<std::string> keys_;
std::string hsep_;
std::string vsep_;
};
std::ostream &operator<<(std::ostream &os, Table const &obj);
} // namespace caffe2
#endif // UTIL_TABLE_H