Skip to content

Commit 1d568e3

Browse files
committed
format: format files
1 parent 35f3cd0 commit 1d568e3

File tree

122 files changed

+14959
-15881
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

122 files changed

+14959
-15881
lines changed

.clang-format

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
BasedOnStyle: LLVM
2+
AccessModifierOffset: -4
3+
AlignAfterOpenBracket: BlockIndent
4+
AlignArrayOfStructures: Left
5+
AlignConsecutiveDeclarations:
6+
Enabled: true
7+
AcrossEmptyLines: false
8+
AcrossComments: false
9+
AlignConsecutiveAssignments:
10+
Enabled: true
11+
AcrossEmptyLines: false
12+
AcrossComments: false
13+
AlignCompound: true
14+
PadOperators: true
15+
AlignConsecutiveMacros:
16+
Enabled: true
17+
AcrossEmptyLines: false
18+
AcrossComments: false
19+
AllowAllParametersOfDeclarationOnNextLine: false
20+
AllowAllArgumentsOnNextLine: false
21+
AlignOperands: AlignAfterOperator
22+
AlignConsecutiveBitFields:
23+
Enabled: true
24+
AcrossEmptyLines: false
25+
AcrossComments: false
26+
AllowShortLambdasOnASingleLine: All
27+
AllowShortBlocksOnASingleLine: Empty
28+
AllowShortIfStatementsOnASingleLine: AllIfsAndElse
29+
AllowShortLoopsOnASingleLine: true
30+
AlwaysBreakAfterDefinitionReturnType: None
31+
AlwaysBreakTemplateDeclarations: 'Yes'
32+
BinPackArguments: false
33+
BinPackParameters: false
34+
BreakBeforeBraces: Custom
35+
BreakBeforeBinaryOperators: NonAssignment
36+
ColumnLimit: 120
37+
CommentPragmas: '^ IWYU pragma:'
38+
ConstructorInitializerIndentWidth: 0
39+
IndentWidth: 4
40+
Language: Cpp
41+
MaxEmptyLinesToKeep: 2
42+
PackConstructorInitializers: CurrentLine
43+
PointerAlignment: Left
44+
TabWidth: 4
45+
UseTab: Never
46+
SortIncludes: CaseSensitive

.clang-tidy

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
# Generated from CLion Inspection settings
2+
---
3+
Checks: '-*,
4+
bugprone-argument-comment,
5+
bugprone-assert-side-effect,
6+
bugprone-bad-signal-to-kill-thread,
7+
bugprone-branch-clone,
8+
bugprone-copy-constructor-init,
9+
bugprone-dangling-handle,
10+
bugprone-dynamic-static-initializers,
11+
bugprone-fold-init-type,
12+
bugprone-forward-declaration-namespace,
13+
bugprone-forwarding-reference-overload,
14+
bugprone-inaccurate-erase,
15+
bugprone-incorrect-roundings,
16+
bugprone-integer-division,
17+
bugprone-lambda-function-name,
18+
bugprone-macro-parentheses,
19+
bugprone-macro-repeated-side-effects,
20+
bugprone-misplaced-operator-in-strlen-in-alloc,
21+
bugprone-misplaced-pointer-arithmetic-in-alloc,
22+
bugprone-misplaced-widening-cast,
23+
bugprone-move-forwarding-reference,
24+
bugprone-multiple-statement-macro,
25+
bugprone-no-escape,
26+
bugprone-not-null-terminated-result,
27+
bugprone-parent-virtual-call,
28+
bugprone-posix-return,
29+
bugprone-reserved-identifier,
30+
bugprone-sizeof-container,
31+
bugprone-sizeof-expression,
32+
bugprone-spuriously-wake-up-functions,
33+
bugprone-string-constructor,
34+
bugprone-string-integer-assignment,
35+
bugprone-string-literal-with-embedded-nul,
36+
bugprone-suspicious-enum-usage,
37+
bugprone-suspicious-include,
38+
bugprone-suspicious-memory-comparison,
39+
bugprone-suspicious-memset-usage,
40+
bugprone-suspicious-missing-comma,
41+
bugprone-suspicious-semicolon,
42+
bugprone-suspicious-string-compare,
43+
bugprone-swapped-arguments,
44+
bugprone-terminating-continue,
45+
bugprone-throw-keyword-missing,
46+
bugprone-too-small-loop-variable,
47+
bugprone-undefined-memory-manipulation,
48+
bugprone-undelegated-constructor,
49+
bugprone-unhandled-self-assignment,
50+
bugprone-unused-raii,
51+
bugprone-unused-return-value,
52+
bugprone-use-after-move,
53+
bugprone-virtual-near-miss,
54+
cert-dcl21-cpp,
55+
cert-dcl58-cpp,
56+
cert-err34-c,
57+
cert-err52-cpp,
58+
cert-err60-cpp,
59+
cert-flp30-c,
60+
cert-msc50-cpp,
61+
cert-msc51-cpp,
62+
cert-str34-c,
63+
cppcoreguidelines-interfaces-global-init,
64+
cppcoreguidelines-narrowing-conversions,
65+
cppcoreguidelines-pro-type-member-init,
66+
cppcoreguidelines-slicing,
67+
google-default-arguments,
68+
google-explicit-constructor,
69+
google-runtime-operator,
70+
hicpp-exception-baseclass,
71+
hicpp-multiway-paths-covered,
72+
misc-misplaced-const,
73+
misc-new-delete-overloads,
74+
misc-non-copyable-objects,
75+
misc-throw-by-value-catch-by-reference,
76+
misc-unconventional-assign-operator,
77+
misc-uniqueptr-reset-release,
78+
modernize-avoid-bind,
79+
modernize-concat-nested-namespaces,
80+
modernize-deprecated-headers,
81+
modernize-deprecated-ios-base-aliases,
82+
modernize-loop-convert,
83+
modernize-make-shared,
84+
modernize-make-unique,
85+
modernize-pass-by-value,
86+
modernize-raw-string-literal,
87+
modernize-redundant-void-arg,
88+
modernize-replace-auto-ptr,
89+
modernize-replace-disallow-copy-and-assign-macro,
90+
modernize-replace-random-shuffle,
91+
modernize-return-braced-init-list,
92+
modernize-shrink-to-fit,
93+
modernize-unary-static-assert,
94+
modernize-use-auto,
95+
modernize-use-bool-literals,
96+
modernize-use-emplace,
97+
modernize-use-equals-default,
98+
modernize-use-equals-delete,
99+
modernize-use-nodiscard,
100+
modernize-use-noexcept,
101+
modernize-use-nullptr,
102+
modernize-use-override,
103+
modernize-use-transparent-functors,
104+
modernize-use-uncaught-exceptions,
105+
mpi-buffer-deref,
106+
mpi-type-mismatch,
107+
openmp-use-default-none,
108+
performance-faster-string-find,
109+
performance-for-range-copy,
110+
performance-implicit-conversion-in-loop,
111+
performance-inefficient-algorithm,
112+
performance-inefficient-string-concatenation,
113+
performance-inefficient-vector-operation,
114+
performance-move-const-arg,
115+
performance-move-constructor-init,
116+
performance-no-automatic-move,
117+
performance-noexcept-move-constructor,
118+
performance-trivially-destructible,
119+
performance-type-promotion-in-math-fn,
120+
performance-unnecessary-copy-initialization,
121+
performance-unnecessary-value-param,
122+
portability-simd-intrinsics,
123+
readability-avoid-const-params-in-decls,
124+
readability-const-return-type,
125+
readability-container-size-empty,
126+
readability-convert-member-functions-to-static,
127+
readability-delete-null-pointer,
128+
readability-deleted-default,
129+
readability-inconsistent-declaration-parameter-name,
130+
readability-make-member-function-const,
131+
readability-misleading-indentation,
132+
readability-misplaced-array-index,
133+
readability-non-const-parameter,
134+
readability-redundant-control-flow,
135+
readability-redundant-declaration,
136+
readability-redundant-function-ptr-dereference,
137+
readability-redundant-smartptr-get,
138+
readability-redundant-string-cstr,
139+
readability-redundant-string-init,
140+
readability-simplify-subscript-expr,
141+
readability-static-accessed-through-instance,
142+
readability-static-definition-in-anonymous-namespace,
143+
readability-string-compare,
144+
readability-uniqueptr-delete-release,
145+
readability-use-anyofallof'

scripts/format_all.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# execute clang-format at src with multi-threading
2+
3+
import os
4+
import subprocess
5+
import multiprocessing
6+
import re
7+
8+
9+
def format_file(file):
10+
print("formatting {0}".format(file))
11+
12+
with open(file, "r") as f:
13+
content = f.read()
14+
with open(file, "w") as f:
15+
if not content.endswith("\n"):
16+
content += "\n"
17+
if file.endswith(".h") and not content.startswith("#pragma once"):
18+
content = "#pragma once\n" + content
19+
f.write(content)
20+
f.close()
21+
22+
subprocess.run(["clang-format", "-i", file])
23+
24+
25+
def format_all():
26+
print("formatting")
27+
pool = multiprocessing.Pool(
28+
processes=max(multiprocessing.cpu_count()-2, 1))
29+
for root, dirs, files in os.walk("./src"):
30+
for file in files:
31+
if file.endswith(".h") or file.endswith(".cpp"):
32+
pool.apply_async(format_file, (os.path.join(root, file),))
33+
pool.close()
34+
pool.join()
35+
36+
37+
if __name__ == "__main__":
38+
try:
39+
format_all()
40+
except Exception as e:
41+
print(f"An error occurred: {e}")

src/ThreadPool.h

Lines changed: 48 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#pragma once
12
#ifndef THREAD_POOL_H
23
#define THREAD_POOL_H
34

@@ -14,75 +15,71 @@
1415

1516
class ThreadPool {
1617
public:
17-
ThreadPool(size_t);
18+
ThreadPool(size_t);
1819

19-
// add new work item to the pool
20-
template <class F, class... Args>
21-
decltype(auto) enqueue(F&& f, Args&&... args) {
22-
auto task =
23-
std::make_shared<std::packaged_task<std::invoke_result_t<F, Args...>()>>([f = std::forward<F>(f), args...] {
24-
return f(args...);
25-
});
26-
auto res = task->get_future();
27-
{
28-
std::unique_lock<std::mutex> lock(queue_mutex);
20+
// add new work item to the pool
21+
template <class F, class... Args>
22+
decltype(auto) enqueue(F&& f, Args&&... args) {
23+
auto task =
24+
std::make_shared<std::packaged_task<std::invoke_result_t<F, Args...>()>>([f = std::forward<F>(f), args...] {
25+
return f(args...);
26+
});
27+
auto res = task->get_future();
28+
{
29+
std::unique_lock<std::mutex> lock(queue_mutex);
2930

30-
// don't allow enqueueing after stopping the pool
31-
if (stop)
32-
throw std::runtime_error("enqueue on stopped ThreadPool");
31+
// don't allow enqueueing after stopping the pool
32+
if (stop) throw std::runtime_error("enqueue on stopped ThreadPool");
3333

34-
tasks.emplace([task] { (*task)(); });
35-
}
36-
condition.notify_one();
34+
tasks.emplace([task] { (*task)(); });
35+
}
36+
condition.notify_one();
3737

38-
return res;
39-
}
38+
return res;
39+
}
4040

41-
~ThreadPool();
41+
~ThreadPool();
4242

4343
private:
44-
// need to keep track of threads so we can join them
45-
std::vector<std::thread> workers;
46-
// the task queue
47-
std::queue<std::function<void()>> tasks;
44+
// need to keep track of threads so we can join them
45+
std::vector<std::thread> workers;
46+
// the task queue
47+
std::queue<std::function<void()>> tasks;
4848

49-
// synchronization
50-
std::mutex queue_mutex;
51-
std::condition_variable condition;
52-
bool stop;
49+
// synchronization
50+
std::mutex queue_mutex;
51+
std::condition_variable condition;
52+
bool stop;
5353
};
5454

5555
// the constructor just launches some amount of workers
5656
inline ThreadPool::ThreadPool(size_t threads) : stop(false) {
57-
for (size_t i = 0; i < threads; ++i)
58-
workers.emplace_back([this] {
59-
for (;;) {
60-
std::function<void()> task;
57+
for (size_t i = 0; i < threads; ++i)
58+
workers.emplace_back([this] {
59+
for (;;) {
60+
std::function<void()> task;
6161

62-
{
63-
std::unique_lock<std::mutex> lock(this->queue_mutex);
64-
this->condition.wait(
65-
lock, [this] { return this->stop || !this->tasks.empty(); });
66-
if (this->stop && this->tasks.empty())
67-
return;
68-
task = std::move(this->tasks.front());
69-
this->tasks.pop();
70-
}
62+
{
63+
std::unique_lock<std::mutex> lock(this->queue_mutex);
64+
this->condition.wait(lock, [this] { return this->stop || !this->tasks.empty(); });
65+
if (this->stop && this->tasks.empty()) return;
66+
task = std::move(this->tasks.front());
67+
this->tasks.pop();
68+
}
7169

72-
task();
73-
}
74-
});
70+
task();
71+
}
72+
});
7573
}
7674

7775
// the destructor joins all threads
7876
inline ThreadPool::~ThreadPool() {
79-
{
80-
std::unique_lock<std::mutex> lock(queue_mutex);
81-
stop = true;
82-
}
83-
condition.notify_all();
84-
for (std::thread &worker : workers)
85-
worker.join();
77+
{
78+
std::unique_lock<std::mutex> lock(queue_mutex);
79+
stop = true;
80+
}
81+
condition.notify_all();
82+
for (std::thread& worker : workers) worker.join();
8683
}
8784

8885
#endif

0 commit comments

Comments
 (0)