-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtop2bottom.cpp
193 lines (158 loc) · 4.35 KB
/
top2bottom.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
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
/*
* Copyright © 2017 Alexandros Frantzis
*
* This file is part of top2bottom.
*
* top2bottom is free software: you can redistribute it and/or modify it under the
* terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later
* version.
*
* top2bottom is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* top2bottom. If not, see <http://www.gnu.org/licenses/>.
*/
#include "top2bottom.h"
#include <algorithm>
#include <cstddef>
#include <string>
#include <vector>
namespace
{
static auto constexpr size_t_max = std::numeric_limits<std::size_t>::max();
struct LineInfo
{
std::size_t depth;
bool has_content;
};
LineInfo parse_line(std::string const& line)
{
std::size_t depth = 0;
bool has_content = false;
bool last_was_quote = false;
for (auto c : line)
{
if (c == '>')
{
depth += 1;
last_was_quote = true;
}
else if (std::isspace(c))
{
last_was_quote = false;
continue;
}
else
{
has_content = true;
if (last_was_quote)
depth -= 1;
break;
}
}
return LineInfo{depth, has_content};
}
inline std::size_t body_key(std::size_t depth)
{
return size_t_max - 10 * depth;
}
inline std::size_t before_body_key(std::size_t depth)
{
return body_key(depth) - 1;
}
inline std::size_t attribution_key(std::size_t depth)
{
return depth;
}
struct AnnotatedLine
{
std::string const* line;
LineInfo info;
std::size_t key;
};
class AnnotationContext
{
public:
AnnotationContext(std::size_t s)
{
annotated.reserve(s);
}
void process(std::string const& line)
{
auto const info = parse_line(line);
update_interleaved_depth(info);
if (info.depth > depth)
fix_attribution();
annotated.push_back({&line, info, body_key(info.depth)});
depth = info.depth;
}
void post_process_for_interleaved()
{
for (auto& al : annotated)
{
if (al.info.depth >= interleaved_depth)
al.key = body_key(interleaved_depth);
}
}
std::vector<AnnotatedLine> annotated;
private:
std::size_t depth = 0;
bool depth_increasing = true;
std::size_t interleaved_depth = size_t_max;
std::size_t max_depth = 0;
void update_interleaved_depth(LineInfo const& info)
{
if (!depth_increasing && info.depth > depth)
{
interleaved_depth = std::min(interleaved_depth, depth);
if (info.depth > max_depth)
throw ConversionError{annotated.size()};
}
else if (info.depth < depth && depth_increasing)
{
depth_increasing = false;
}
max_depth = std::max(max_depth, info.depth);
}
void fix_attribution()
{
bool have_seen_content = false;
for (auto iter = annotated.rbegin();
iter != annotated.rend();
++iter)
{
auto const& info = iter->info;
if (info.depth < depth)
break;
have_seen_content = have_seen_content || info.has_content;
if (have_seen_content && !info.has_content)
{
iter->key = before_body_key(depth);
break;
}
else
{
iter->key = attribution_key(depth);
}
}
}
};
}
std::vector<std::string const*> top2bottom(std::vector<std::string> const& lines)
{
AnnotationContext ctx{lines.size()};
for (auto const& line : lines)
ctx.process(line);
ctx.post_process_for_interleaved();
std::stable_sort(
ctx.annotated.begin(), ctx.annotated.end(),
[] (auto const& al1, auto const& al2) { return al1.key < al2.key; });
std::vector<std::string const*> output{ctx.annotated.size()};
std::transform(ctx.annotated.begin(), ctx.annotated.end(), output.begin(),
[] (auto const& al) { return al.line; });
return output;
}