-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbatch_parser.cpp
More file actions
153 lines (126 loc) · 4.62 KB
/
batch_parser.cpp
File metadata and controls
153 lines (126 loc) · 4.62 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
#include "docx_comment_parser.h"
#include <thread>
#include <mutex>
#include <queue>
#include <functional>
#include <condition_variable>
#include <unordered_map>
namespace docx {
// ─── Simple thread-pool ───────────────────────────────────────────────────────
class ThreadPool {
public:
explicit ThreadPool(unsigned int n) {
n = (n == 0) ? std::max(1u, std::thread::hardware_concurrency()) : n;
workers_.reserve(n);
for (unsigned i = 0; i < n; ++i)
workers_.emplace_back([this]{ worker_loop(); });
}
~ThreadPool() {
{
std::unique_lock<std::mutex> lk(mu_);
stop_ = true;
}
cv_.notify_all();
for (auto& t : workers_) t.join();
}
template<typename F>
void submit(F&& f) {
{
std::unique_lock<std::mutex> lk(mu_);
tasks_.push(std::forward<F>(f));
}
cv_.notify_one();
}
void wait_all() {
std::unique_lock<std::mutex> lk(mu_);
done_cv_.wait(lk, [this]{ return tasks_.empty() && active_ == 0; });
}
private:
void worker_loop() {
while (true) {
std::function<void()> task;
{
std::unique_lock<std::mutex> lk(mu_);
cv_.wait(lk, [this]{ return stop_ || !tasks_.empty(); });
if (stop_ && tasks_.empty()) return;
task = std::move(tasks_.front());
tasks_.pop();
++active_;
}
task();
{
std::unique_lock<std::mutex> lk(mu_);
--active_;
}
done_cv_.notify_all();
}
}
std::vector<std::thread> workers_;
std::queue<std::function<void()>> tasks_;
std::mutex mu_;
std::condition_variable cv_;
std::condition_variable done_cv_;
bool stop_{false};
int active_{0};
};
// ─── BatchParser::Impl ───────────────────────────────────────────────────────
struct BatchParser::Impl {
unsigned int max_threads;
struct FileResult {
std::vector<CommentMetadata> comments;
DocumentCommentStats stats;
};
std::unordered_map<std::string, FileResult> results_;
std::unordered_map<std::string, std::string> errors_;
mutable std::mutex mu_;
explicit Impl(unsigned int t) : max_threads(t) {}
void parse_all(const std::vector<std::string>& paths) {
ThreadPool pool(max_threads);
for (const auto& p : paths) {
pool.submit([this, p]{
DocxParser parser;
try {
parser.parse(p);
FileResult r;
r.comments = parser.comments();
r.stats = parser.stats();
std::lock_guard<std::mutex> lk(mu_);
results_[p] = std::move(r);
} catch (const std::exception& ex) {
std::lock_guard<std::mutex> lk(mu_);
errors_[p] = ex.what();
}
});
}
pool.wait_all();
}
};
// ─── BatchParser public API ───────────────────────────────────────────────────
BatchParser::BatchParser(unsigned int max_threads)
: impl_(std::make_unique<Impl>(max_threads)) {}
BatchParser::~BatchParser() = default;
void BatchParser::parse_all(const std::vector<std::string>& file_paths) {
impl_->parse_all(file_paths);
}
const std::vector<CommentMetadata>& BatchParser::comments(const std::string& fp) const {
auto it = impl_->results_.find(fp);
if (it == impl_->results_.end())
throw DocxParserError("File not parsed: " + fp);
return it->second.comments;
}
const DocumentCommentStats& BatchParser::stats(const std::string& fp) const {
auto it = impl_->results_.find(fp);
if (it == impl_->results_.end())
throw DocxParserError("File not parsed: " + fp);
return it->second.stats;
}
const std::unordered_map<std::string, std::string>& BatchParser::errors() const noexcept {
return impl_->errors_;
}
void BatchParser::release(const std::string& fp) {
impl_->results_.erase(fp);
}
void BatchParser::release_all() {
impl_->results_.clear();
}
} // namespace docx