Repository navigation
Expand file tree
/
Copy pathplanPrinter.cpp
More file actions
121 lines (105 loc) · 3.21 KB
/
Copy pathplanPrinter.cpp
File metadata and controls
121 lines (105 loc) · 3.21 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
#include "../include/PlanPrinter.hpp"
#include <algorithm>
#include <string>
#include <vector>
#include "../include/PlanNode.hpp"
namespace {
struct Block{
std::vector<std::string> lines;
size_t anchor = 0;
};
size_t widthOf(const Block& b){
size_t w = 0;
for(const auto& l : b.lines)
w = std::max(w, l.size());
return w;
}
void putChar(std::string& line, size_t col, char c){
if(line.size() < col + 1)
line.resize(col + 1, ' ');
line[col] = c;
}
std::string centeredAt(size_t anchor, const std::string& text){
size_t half = text.size() / 2;
size_t off = (anchor > half) ? anchor - half : 0;
return std::string(off, ' ') + text;
}
Block layout(const PlanNode& node){
const std::vector<std::string> label = node.labelLines();
size_t labelW = 0;
for(const auto& l : label)
labelW = std::max(labelW, l.size());
const auto kids = node.children();
Block b;
if(kids.empty()){
b.anchor = labelW / 2;
for(const auto& l : label)
b.lines.push_back(centeredAt(b.anchor, l));
return b;
}
if(kids.size() == 1){
Block c = layout(*kids[0]);
b.anchor = std::max(c.anchor, labelW / 2);
size_t shift = b.anchor - c.anchor;
for(const auto& l : label)
b.lines.push_back(centeredAt(b.anchor, l));
b.lines.push_back(std::string(b.anchor, ' ') + "|");
for(const auto& l : c.lines)
b.lines.push_back(std::string(shift, ' ') + l);
return b;
}
Block L = layout(*kids[0]);
Block R = layout(*kids[1]);
size_t rOff = widthOf(L) + 3;
size_t la = L.anchor;
size_t ra = rOff + R.anchor;
size_t p = (la + ra) / 2;
size_t shift = (labelW / 2 > p) ? labelW / 2 - p : 0;
la += shift;
ra += shift;
rOff += shift;
p += shift;
b.anchor = p;
for(const auto& l : label)
b.lines.push_back(centeredAt(p, l));
size_t leftLen = std::max<size_t>(1, p - la - 1);
size_t rightLen = std::max<size_t>(1, ra - p - 1);
size_t rows = std::max(leftLen, rightLen);
for(size_t i = 0; i < rows; i++){
std::string line;
if(i < leftLen)
putChar(line, p - 1 - i, '/');
else
putChar(line, la, '|');
if(i < rightLen)
putChar(line, p + 1 + i, '\\');
else
putChar(line, ra, '|');
b.lines.push_back(std::move(line));
}
size_t childRows = std::max(L.lines.size(), R.lines.size());
for(size_t i = 0; i < childRows; i++){
std::string line;
if(i < L.lines.size())
line = std::string(shift, ' ') + L.lines[i];
if(i < R.lines.size()){
if(line.size() < rOff)
line.resize(rOff, ' ');
line += R.lines[i];
}
b.lines.push_back(std::move(line));
}
return b;
}
}
void printPlanTree(const PlanNode& root, std::ostream& out, size_t leftPad){
Block b = layout(root);
for(auto& line : b.lines){
while(!line.empty() && line.back() == ' ')
line.pop_back();
out << std::string(leftPad, ' ') << line << "\n";
}
}
void PlanNode::print(std::ostream& out, size_t indent) const{
printPlanTree(*this, out, indent);
}