Skip to content

Commit d2e781a

Browse files
committed
[重构]: 改进代码规范、修复命名并增强测试
- **代码检查配置**: 调整.clang-tidy配置,禁用llvm-header-guard检查项,优化GitHub Actions的clang-tidy忽略路径 - **命名规范化**: 统一算法模块参数命名(v→vec)、线程池变量命名(n→availableCores)、服务器函数参数命名(fd→fileDescriptor) - **设计模式改进**: - Factory模式添加构造日志,改用pragma once,增强拷贝控制 - Singleton模式重构为单元测试驱动,移除main.cpp实现,增加多线程安全测试用例 - **代码现代化改造**: - 线程池任务调度改用lambda表达式替代std::bind - 自旋锁明确禁用移动语义(DISABLE_MOVE) - 头文件保护全面转为#pragma once - **工具类增强**: 扩展utils/object.hpp拷贝控制宏,新增DEFAULT_COPY/MOVE系列宏 - **测试覆盖**: 为Singleton模式添加gtest单元测试,验证单例唯一性及多线程安全性 - **文档更新**: 同步修订README项目结构说明,保持与代码结构一致
1 parent 3f7ad4d commit d2e781a

File tree

22 files changed

+253
-237
lines changed

22 files changed

+253
-237
lines changed

.clang-tidy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
Checks: 'clang-analyzer-*,readability-*,performance-*,modernize-*,bugprone-*,cert-*,portability-*,llvm-*,google-*'
2+
Checks: 'clang-analyzer-*,readability-*,performance-*,modernize-*,bugprone-*,cert-*,portability-*,llvm-*,-llvm-header-guard,google-*'
33
WarningsAsErrors: ''
44
HeaderFilterRegex: '.'
55
AnalyzeTemporaryDtors: false

.github/workflows/cpp_linter.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ jobs:
4848
step-summary: true
4949
database: build
5050
ignore: .github | build
51-
ignore-tidy: .github | MonitorDir/monitordir_win.cc | MonitorDir/monitordir_mac.cc
51+
ignore-tidy: .github | Client | Icmp| Server | MonitorDir/monitordir_win.cc | MonitorDir/monitordir_mac.cc
5252

5353
- name: Fail fast?!
5454
if: steps.linter.outputs.clang-tidy-checks-failed > 0

Algorithm/Search/search.hpp

Lines changed: 43 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88

99
// 顺序查找
1010
template<typename T>
11-
auto sequential_search(const std::vector<T> &v, const T &value) -> int
11+
auto sequential_search(const std::vector<T> &vec, const T &value) -> int
1212
{
13-
for (int i = 0; i < v.size(); ++i) { // 从头到尾遍历
14-
if (v[i] == value) { // 如果找到了,就返回下标
13+
for (int i = 0; i < vec.size(); ++i) { // 从头到尾遍历
14+
if (vec[i] == value) { // 如果找到了,就返回下标
1515
return i;
1616
}
1717
}
@@ -20,15 +20,15 @@ auto sequential_search(const std::vector<T> &v, const T &value) -> int
2020

2121
// 二分查找
2222
template<typename T>
23-
auto binary_search(const std::vector<T> &v, const T &value) -> int
23+
auto binary_search(const std::vector<T> &vec, const T &value) -> int
2424
{
2525
int low = 0; // 最小下标
26-
int high = v.size() - 1; // 最大下标
26+
int high = vec.size() - 1; // 最大下标
2727
while (low <= high) { // 当最小下标小于等于最大下标时
2828
int mid = (low + high) / 2; // 取中间下标
29-
if (v[mid] == value) { // 如果中间元素等于要查找的元素,就返回下标
29+
if (vec[mid] == value) { // 如果中间元素等于要查找的元素,就返回下标
3030
return mid;
31-
} else if (v[mid] < value) { // 如果中间元素小于要查找的元素,就在右半部分查找
31+
} else if (vec[mid] < value) { // 如果中间元素小于要查找的元素,就在右半部分查找
3232
low = mid + 1;
3333
} else { // 如果中间元素大于要查找的元素,就在左半部分查找
3434
high = mid - 1;
@@ -39,23 +39,23 @@ auto binary_search(const std::vector<T> &v, const T &value) -> int
3939

4040
// 斐波那契查找
4141
template<typename T>
42-
auto fibonacci_search(const std::vector<T> &v, const T &value) -> int
42+
auto fibonacci_search(const std::vector<T> &vec, const T &value) -> int
4343
{
44-
std::vector<int> fibonacci{1, 1}; // 斐波那契数列
45-
while (fibonacci.back() < v.size()) { // 生成斐波那契数列
44+
std::vector<int> fibonacci{1, 1}; // 斐波那契数列
45+
while (fibonacci.back() < vec.size()) { // 生成斐波那契数列
4646
fibonacci.push_back(fibonacci[fibonacci.size() - 1] + fibonacci[fibonacci.size() - 2]);
4747
}
4848
int low = 0; // 最小下标
49-
int high = v.size() - 1; // 最大下标
49+
int high = vec.size() - 1; // 最大下标
5050
int k = fibonacci.size() - 1; // 斐波那契数列的下标
5151
while (low <= high) { // 当最小下标小于等于最大下标时
5252
int mid = low + fibonacci[k - 1] - 1; // 取黄金分割点
53-
if (mid >= v.size()) { // 如果黄金分割点大于等于数组长度
54-
mid = v.size() - 1; // 就将黄金分割点设置为数组最后一个元素的下标
53+
if (mid >= vec.size()) { // 如果黄金分割点大于等于数组长度
54+
mid = vec.size() - 1; // 就将黄金分割点设置为数组最后一个元素的下标
5555
}
56-
if (v[mid] == value) { // 如果中间元素等于要查找的元素,就返回下标
56+
if (vec[mid] == value) { // 如果中间元素等于要查找的元素,就返回下标
5757
return mid;
58-
} else if (v[mid] < value) { // 如果中间元素小于要查找的元素,就在右半部分查找
58+
} else if (vec[mid] < value) { // 如果中间元素小于要查找的元素,就在右半部分查找
5959
low = mid + 1;
6060
k -= 2;
6161
} else { // 如果中间元素大于要查找的元素,就在左半部分查找
@@ -68,13 +68,13 @@ auto fibonacci_search(const std::vector<T> &v, const T &value) -> int
6868

6969
// 线性索引查找
7070
template<typename T>
71-
auto linear_index_search(const std::vector<T> &v, const T &value) -> int
71+
auto linear_index_search(const std::vector<T> &vec, const T &value) -> int
7272
{
73-
std::vector<T> index; // 索引
74-
index.push_back(v[0]); // 第一个索引为第一个元素
75-
for (int i = 1; i < v.size(); ++i) { // 生成索引
76-
if (v[i] > index.back()) { // 如果当前元素大于索引的最后一个元素
77-
index.push_back(v[i]);
73+
std::vector<T> index; // 索引
74+
index.push_back(vec[0]); // 第一个索引为第一个元素
75+
for (int i = 1; i < vec.size(); ++i) { // 生成索引
76+
if (vec[i] > index.back()) { // 如果当前元素大于索引的最后一个元素
77+
index.push_back(vec[i]);
7878
}
7979
}
8080
int low = 0; // 最小下标
@@ -94,7 +94,7 @@ auto linear_index_search(const std::vector<T> &v, const T &value) -> int
9494

9595
// KMP查找
9696
template<typename T>
97-
auto kmp_search(const std::vector<T> &v, const std::vector<T> &pattern) -> int
97+
auto kmp_search(const std::vector<T> &vec, const std::vector<T> &pattern) -> int
9898
{
9999
std::vector<int> next(pattern.size()); // next数组
100100
next[0] = -1; // next数组的第一个元素为-1
@@ -109,11 +109,11 @@ auto kmp_search(const std::vector<T> &v, const std::vector<T> &pattern) -> int
109109
next[i] = k; // 将k赋值给next数组的当前元素
110110
}
111111
k = -1; // next数组的下标
112-
for (int i = 0; i < v.size(); ++i) {
113-
while (k > -1 && pattern[k + 1] != v[i]) { // 如果k大于-1且下一个元素不等于当前元素
112+
for (int i = 0; i < vec.size(); ++i) {
113+
while (k > -1 && pattern[k + 1] != vec[i]) { // 如果k大于-1且下一个元素不等于当前元素
114114
k = next[k];
115115
}
116-
if (pattern[k + 1] == v[i]) { // 如果下一个元素等于当前元素
116+
if (pattern[k + 1] == vec[i]) { // 如果下一个元素等于当前元素
117117
++k;
118118
}
119119
if (k == pattern.size() - 1) { // 如果k等于模式串的最后一个元素的下标
@@ -139,7 +139,7 @@ auto move_by_suffix(int j, const std::vector<int> &suffix, const std::vector<boo
139139
}
140140
// BM查找
141141
template<typename T>
142-
auto bm_search(const std::vector<T> &v, const std::vector<T> &pattern) -> int
142+
auto bm_search(const std::vector<T> &vec, const std::vector<T> &pattern) -> int
143143
{
144144
std::vector<int> bc(256, -1); // 坏字符哈希表
145145
for (int i = 0; i < pattern.size(); ++i) { // 生成坏字符哈希表
@@ -161,71 +161,71 @@ auto bm_search(const std::vector<T> &v, const std::vector<T> &pattern) -> int
161161
}
162162
}
163163
int i = 0;
164-
while (i <= v.size() - pattern.size()) { // 从头到尾遍历
164+
while (i <= vec.size() - pattern.size()) { // 从头到尾遍历
165165
int j = pattern.size() - 1;
166-
while (j >= 0 && v[i + j] == pattern[j]) { // 从后往前比较
166+
while (j >= 0 && vec[i + j] == pattern[j]) { // 从后往前比较
167167
--j;
168168
}
169169
if (j == -1) { // 如果模式串的所有元素都匹配了
170170
return i;
171171
}
172-
i += std::max(move_by_suffix(j, suffix, prefix), j - bc[v[i + j]]); // 将模式串向后移动
172+
i += std::max(move_by_suffix(j, suffix, prefix), j - bc[vec[i + j]]); // 将模式串向后移动
173173
}
174174
return -1; // not found
175175
}
176176

177177
// Sunday查找
178178
template<typename T>
179-
auto sunday_search(const std::vector<T> &v, const std::vector<T> &pattern) -> int
179+
auto sunday_search(const std::vector<T> &vec, const std::vector<T> &pattern) -> int
180180
{
181181
std::vector<int> bc(256, -1); // 坏字符哈希表
182182
for (int i = 0; i < pattern.size(); ++i) { // 生成坏字符哈希表
183183
bc[pattern[i]] = i;
184184
}
185185
int i = 0;
186-
while (i <= v.size() - pattern.size()) { // 从头到尾遍历
186+
while (i <= vec.size() - pattern.size()) { // 从头到尾遍历
187187
int j = 0;
188-
while (j < pattern.size() && v[i + j] == pattern[j]) { // 从前往后比较
188+
while (j < pattern.size() && vec[i + j] == pattern[j]) { // 从前往后比较
189189
++j;
190190
}
191191
if (j == pattern.size()) { // 如果模式串的所有元素都匹配了
192192
return i;
193193
}
194-
if (i + pattern.size() == v.size()) { // 如果模式串的最后一个元素在主串中不存在
195-
return -1; // not found
194+
if (i + pattern.size() == vec.size()) { // 如果模式串的最后一个元素在主串中不存在
195+
return -1; // not found
196196
}
197-
i += pattern.size() - bc[v[i + pattern.size()]]; // 将模式串向后移动
197+
i += pattern.size() - bc[vec[i + pattern.size()]]; // 将模式串向后移动
198198
}
199199
return -1; // not found
200200
}
201201

202202
// Rabin-Karp查找
203203
template<typename T>
204-
auto rabin_karp_search(const std::vector<T> &v, const std::vector<T> &pattern) -> int
204+
auto rabin_karp_search(const std::vector<T> &vec, const std::vector<T> &pattern) -> int
205205
{
206206
int pattern_hash = 0; // 模式串的哈希值
207207
for (int i = 0; i < pattern.size(); ++i) {
208208
pattern_hash += pattern[i];
209209
}
210210
int v_hash = 0; // 主串的哈希值
211211
for (int i = 0; i < pattern.size(); ++i) {
212-
v_hash += v[i];
212+
v_hash += vec[i];
213213
}
214214
int i = 0;
215-
while (i <= v.size() - pattern.size()) { // 从头到尾遍历
216-
if (v_hash == pattern_hash) { // 如果模式串的哈希值等于主串的哈希值
215+
while (i <= vec.size() - pattern.size()) { // 从头到尾遍历
216+
if (v_hash == pattern_hash) { // 如果模式串的哈希值等于主串的哈希值
217217
int j = 0;
218-
while (j < pattern.size() && v[i + j] == pattern[j]) { // 从前往后比较
218+
while (j < pattern.size() && vec[i + j] == pattern[j]) { // 从前往后比较
219219
++j;
220220
}
221221
if (j == pattern.size()) { // 如果模式串的所有元素都匹配了
222222
return i;
223223
}
224224
}
225-
if (i + pattern.size() == v.size()) { // 如果模式串的最后一个元素在主串中不存在
226-
return -1; // not found
225+
if (i + pattern.size() == vec.size()) { // 如果模式串的最后一个元素在主串中不存在
226+
return -1; // not found
227227
}
228-
v_hash += v[i + pattern.size()] - v[i]; // 将模式串向后移动
228+
v_hash += vec[i + pattern.size()] - vec[i]; // 将模式串向后移动
229229
++i;
230230
}
231231
return -1; // not found

DesignPattern/Factory/factory.cc

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
#include <iostream>
44

5-
Factory::Factory() {}
5+
Factory::Factory()
6+
{
7+
std::cout << "Factory" << std::endl;
8+
}
69

710
Factory::~Factory()
811
{
@@ -19,7 +22,9 @@ class ComputerFactory : public Factory
1922
public:
2023
ComputerFactory()
2124
: Factory()
22-
{}
25+
{
26+
std::cout << "ComputerFactory" << std::endl;
27+
}
2328
virtual ~ComputerFactory() { std::cout << "~ComputerFactory" << std::endl; }
2429

2530
virtual void name() { std::cout << "ComputerFactory" << std::endl; }
@@ -30,7 +35,9 @@ class PhoneFactory : public Factory
3035
public:
3136
PhoneFactory()
3237
: Factory()
33-
{}
38+
{
39+
std::cout << "PhoneFactory" << std::endl;
40+
}
3441
virtual ~PhoneFactory() { std::cout << "~PhoneFactory" << std::endl; }
3542

3643
virtual void name() { std::cout << "PhoneFactory" << std::endl; }

DesignPattern/Factory/factory.hpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
#ifndef FACTORY_HPP
2-
#define FACTORY_HPP
1+
#pragma once
32

4-
class Factory
3+
#include <utils/object.hpp>
4+
5+
class Factory : noncopyable
56
{
67
public:
78
Factory();
@@ -15,8 +16,6 @@ class Factory
1516
virtual void name();
1617
};
1718

18-
enum FactoryType { Computer, Phone };
19-
20-
auto createFactory(FactoryType type) -> Factory *;
19+
enum FactoryType : int { Computer, Phone };
2120

22-
#endif // FACTORY_HPP
21+
auto createFactory(FactoryType type) -> Factory *;

DesignPattern/Factory/main.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ auto main(int argc, char *argv[]) -> int
77
(void) argc;
88
(void) argv;
99

10-
std::unique_ptr<Factory> f(createFactory(FactoryType::Computer));
11-
std::unique_ptr<Factory> f1(createFactory(FactoryType::Phone));
12-
//std::unique_ptr<Factory> f2(createFactory(FactoryType(-1)));
10+
std::unique_ptr<Factory> computerFactoryPtr(createFactory(FactoryType::Computer));
11+
std::unique_ptr<Factory> phoneFactoryPtr(createFactory(FactoryType::Phone));
12+
// std::unique_ptr<Factory> invalidFactoryPtr(createFactory(FactoryType(-1)));
1313

1414
return 0;
1515
}

DesignPattern/Observer/observer.hpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
#ifndef OBSERVER_HPP
2-
#define OBSERVER_HPP
1+
#pragma once
32

43
#include <iostream>
54
#include <list>
@@ -64,5 +63,3 @@ class ConcreteSubject : public Subject
6463
std::list<std::weak_ptr<Observer>> m_Observers;
6564
int m_state = 0;
6665
};
67-
68-
#endif // OBSERVER_HPP
Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
1-
add_executable(Singleton main.cpp singleton.cc singleton.hpp
2-
singletonmanager.hpp)
1+
add_executable(singleton_unittest singleton_unittest.cc singleton.cc
2+
singleton.hpp)
3+
target_link_libraries(singleton_unittest PRIVATE GTest::gtest GTest::gtest_main
4+
GTest::gmock GTest::gmock_main)
5+
add_test(NAME singleton_unittest COMMAND singleton_unittest)

DesignPattern/Singleton/main.cpp

Lines changed: 0 additions & 31 deletions
This file was deleted.

DesignPattern/Singleton/singleton.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ namespace Hungry {
44

55
// ② This the second way to implement Singleton
66
// std::unique_ptr<Singleton> Singleton::s_singleton_ptr(new Singleton);
7-
std::unique_ptr<Singleton> Singleton::s_singleton_ptr;
8-
std::mutex Singleton::s_mutex;
7+
std::unique_ptr<Singleton> Singleton::singletonPtr;
8+
std::mutex Singleton::mutex;
99

1010
} // namespace Hungry

0 commit comments

Comments
 (0)