Repository navigation
Expand file tree
/
Copy pathNode.cpp
More file actions
222 lines (201 loc) · 5.35 KB
/
Copy pathNode.cpp
File metadata and controls
222 lines (201 loc) · 5.35 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#include <functional>
#include <iostream>
#include <sstream>
#include <string>
#include <unordered_map>
#include <vector>
#include "Node.h"
#include "string_utils.h"
namespace HierMUS {
using std::string;
using std::vector;
void getLeaves(const MapNode &node, std::set<std::string> &leaves) {
if (!node.children.empty()) {
for (const MapNode &child : node.children)
getLeaves(child, leaves);
} else {
vector<string> split_leaves = utils::split(node.con_id, '#', false);
for (const string &l : split_leaves)
leaves.insert(l);
}
}
std::set<std::string> MapNode::getLeaves() const {
std::set<std::string> leaves;
HierMUS::getLeaves(*this, leaves);
return leaves;
}
MapNode &MapNode::addPath(std::vector<std::string> &splitpath, unsigned int p) {
if (p == splitpath.size()) {
if (!children.empty()) {
children.push_back(MapNode("_"));
return children.back();
}
return *this;
}
for (MapNode &child : children) {
if (child.path == splitpath[p]) {
return child.addPath(splitpath, p + 1);
}
}
if (!con_id.empty()) {
children.push_back(MapNode(path, con_id));
con_id.clear();
}
children.push_back(MapNode(splitpath[p]));
return children.back().addPath(splitpath, p + 1);
}
MapNode &MapNode::addPath(std::string &path) {
vector<string> splitpath = utils::split(path, MAJOR_SEP);
return addPath(splitpath, 0);
}
void MapNode::getCounts(counts &cs, int depth) const {
if (depth > cs.maxdepth)
cs.maxdepth = depth;
if (children.size() == 0) {
cs.nleaves++;
} else {
if (!con_id.empty()) {
std::cerr << "Found lost leaf: " << con_id << std::endl;
}
cs.nbranches++;
}
for (const MapNode &n : children)
n.getCounts(cs, depth + 1);
}
void MapNode::getIncompleteCounts(counts &cs, int depth) const {
if (depth > cs.maxdepth)
cs.maxdepth = depth;
if (var.isLeaf) {
cs.nleaves++;
} else {
cs.nbranches++;
for (const MapNode &n : children)
n.getCounts(cs, depth + 1);
}
}
counts MapNode::getCounts(bool complete) const {
counts cs;
if (complete) {
getCounts(cs, 1);
} else {
getIncompleteCounts(cs, 1);
}
return cs;
}
void MapNode::compact() {
if (children.size() == 0)
return;
if (children.size() == 1) {
MapNode the_child = children[0];
children.clear();
std::stringstream new_path;
if (the_child.children.size() == 0)
con_id = the_child.con_id;
new_path << path << MAJOR_SEP << the_child.path;
path = new_path.str();
for (MapNode &child : the_child.children)
children.push_back(child);
}
for (MapNode &child : children)
child.compact();
}
void MapNode::mergeLeaves() {
std::unordered_map<std::string, vector<std::string>> paths_leaf_names;
vector<MapNode> children_copy = children;
children.clear();
for (MapNode &child : children_copy) {
if (child.children.empty()) {
paths_leaf_names[child.path].push_back(child.con_id);
} else {
child.mergeLeaves();
children.push_back(child);
}
}
for (auto &p : paths_leaf_names) {
children.push_back(MapNode(p.first, utils::join(p.second, "#")));
}
}
void MapNode::makeBinary(std::function<bool(const MapNode &)> cond) {
if (cond(*this)) {
vector<MapNode> left(children.begin(),
children.begin() + children.size() / 2);
vector<MapNode> right(children.begin() + (children.size() / 2),
children.end());
children.clear();
children.push_back(MapNode(path + "_L", {}, left));
children.push_back(MapNode(path + "_R", {}, right));
}
for (MapNode &child : children) {
child.makeBinary(cond);
}
}
std::ostream &operator<<(std::ostream &os, MapNode const &mn) {
os << printMapNode(true, "c_", &mn);
return os;
}
std::ostream &operator<<(std::ostream &os, NodeSet const &mns) {
return streamMapNodeSet(os, mns, true, "c_");
}
//#define SHOW_PARENT 1
#undef SHOW_PARENT
std::ostream& operator<<(std::ostream& os, ExpandedNode const& en) {
#ifdef SHOW_PARENT
os << "(" << printMapNode(true, "c_", en.parent) << ", ";
#endif
os << printMapNode(true, "e_", en.child);
#ifdef SHOW_PARENT
os << ")";
#endif
return os;
}
std::ostream &operator<<(std::ostream &os, ExpandedNodeSet const &inc) {
bool first = true;
os << "{";
for (const ExpandedNode en : inc) {
if (!first) {
os << ", ";
}
os << en;
first = false;
}
os << "}";
return os;
}
inline std::string printMapNode(bool pol, const std::string &prefix,
const MapNode *mn) {
std::stringstream ss;
if (!pol)
ss << "~";
if (!mn->var.isLeaf)
ss << prefix;
ss << mn->path;
return ss.str();
}
std::ostream &streamExpandedNodeSet(std::ostream &os,
ExpandedNodeSet const &mns, bool pol,
std::string prefix) {
bool first = true;
os << "{";
for (const MapNode *mn : mns.get_nodes()) {
if (!first)
os << ", ";
os << printMapNode(pol, prefix, mn);
first = false;
}
os << "}";
return os;
}
std::ostream &streamMapNodeSet(std::ostream &os, NodeSet const &mns,
bool pol, std::string prefix) {
bool first = true;
os << "{";
for (const MapNode *mn : mns) {
if (!first)
os << ", ";
os << printMapNode(pol, prefix, mn);
first = false;
}
os << "}";
return os;
}
} // namespace HierMUS