Skip to content

Commit 1860a14

Browse files
committed
[重构CI配置并集成代码质量检查工具]: 优化持续集成流程,引入代码静态分析和依赖管理改进
- **新增自定义复合Action**:创建`.github/actions/install-dependencies`统一管理跨平台依赖安装,集成vcpkg安装/缓存/OS特定构建工具链配置 - **简化build工作流**:重构`.github/workflows/build.yml`调用新复合Action,消除重复的依赖安装步骤 - **增强代码质量检查**: - 新增`codeql.yml`工作流进行C++代码安全扫描 - 新增`cpp_linter.yml`工作流实现基于clang-tidy的静态分析 - **依赖管理更新**: - 升级vcpkg内置基线至`d5cb5b9`提交 - 规范vcpkg缓存策略,提升CI效率
1 parent 2b41d7e commit 1860a14

File tree

6 files changed

+165
-57
lines changed

6 files changed

+165
-57
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
name: 'Install Dependencies'
2+
description: 'Install dependencies for the environment'
3+
4+
runs:
5+
using: 'composite'
6+
7+
steps:
8+
- name: Install Custom VCPKG
9+
uses: RealChuan/install-vcpkg@main
10+
with:
11+
repo: 'https://github.com/RealChuan/vcpkg.git'
12+
branch: 'dev'
13+
14+
- name: Update vcpkg manifest baseline
15+
shell: bash
16+
run: |
17+
vcpkg x-update-baseline
18+
19+
- name: Cache vcpkg
20+
uses: actions/cache@v4
21+
with:
22+
path: ${{ github.workspace }}/build/vcpkg_installed
23+
key: ${{ runner.os }}-vcpkg-installed-${{ github.sha }}
24+
restore-keys: |
25+
${{ runner.os }}-vcpkg-installed-
26+
${{ runner.os }}-
27+
28+
- name: Install dependencies on windows
29+
if: runner.os == 'Windows'
30+
shell: bash
31+
run: |
32+
choco install ninja
33+
ninja --version
34+
cmake --version
35+
36+
- name: Install dependencies on macos
37+
if: runner.os == 'macOS'
38+
shell: bash
39+
run: |
40+
brew install nasm python-setuptools
41+
ninja --version
42+
cmake --version
43+
clang --version
44+
45+
- name: Install dependencies on linux
46+
if: runner.os == 'Linux'
47+
shell: bash
48+
run: |
49+
sudo apt-get update
50+
sudo apt-get install ninja-build clang
51+
ninja --version
52+
cmake --version
53+
gcc --version
54+

.github/workflows/build.yml

+1-43
Original file line numberDiff line numberDiff line change
@@ -34,53 +34,11 @@ jobs:
3434
- "Ninja"
3535

3636
steps:
37-
- name: Install dependencies on windows
38-
if: startsWith(matrix.os, 'windows')
39-
run: |
40-
choco install ninja
41-
ninja --version
42-
cmake --version
43-
- name: Install dependencies on macos
44-
if: startsWith(matrix.os, 'macos')
45-
shell: bash
46-
run: |
47-
brew install ninja python-setuptools
48-
ninja --version
49-
cmake --version
50-
clang --version
51-
- name: Install dependencies on ubuntu
52-
if: startsWith(matrix.os, 'ubuntu')
53-
run: |
54-
sudo apt-get update
55-
sudo apt-get install ninja-build clang
56-
ninja --version
57-
cmake --version
58-
gcc --version
59-
6037
- uses: actions/checkout@v4
6138
with:
6239
fetch-depth: 1
6340

64-
- name: Install custom vcpkg
65-
uses: RealChuan/install-vcpkg@main
66-
with:
67-
repo: 'https://github.com/RealChuan/vcpkg.git'
68-
branch: 'dev'
69-
70-
- name: Update vcpkg manifest baseline
71-
shell: bash
72-
run: |
73-
vcpkg x-update-baseline
74-
75-
- name: Cache vcpkg
76-
uses: actions/cache@v4
77-
with:
78-
path: ${{ github.workspace }}/build/vcpkg_installed
79-
key: ${{ matrix.os }}-vcpkg-installed-${{ runner.os }}-${{ github.sha }}
80-
restore-keys: |
81-
${{ matrix.os }}-vcpkg-installed-${{ runner.os }}-
82-
${{ matrix.os }}-vcpkg-installed-
83-
${{ matrix.os }}-
41+
- uses: ./.github/actions/install-dependencies
8442

8543
- name: Configure and build windows
8644
if: startsWith(matrix.os, 'windows')

.github/workflows/codeql.yml

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
name: CodeQL
2+
3+
on:
4+
push:
5+
paths-ignore:
6+
- '.clang*'
7+
- '.gitignore'
8+
- 'LICENSE'
9+
- 'README*'
10+
pull_request:
11+
paths-ignore:
12+
- '.clang*'
13+
- '.gitignore'
14+
- 'LICENSE'
15+
- 'README*'
16+
17+
schedule:
18+
- cron: '0 0 1 * *'
19+
workflow_dispatch:
20+
21+
22+
jobs:
23+
CodeQL:
24+
runs-on: ubuntu-latest
25+
26+
steps:
27+
- uses: actions/checkout@v4
28+
with:
29+
fetch-depth: 1
30+
31+
- uses: ./.github/actions/install-dependencies
32+
33+
- name: Initialize CodeQL
34+
uses: github/codeql-action/init@v3
35+
with:
36+
languages: cpp
37+
38+
- name: Autobuild
39+
uses: github/codeql-action/autobuild@v3
40+
41+
- name: Perform CodeQL Analysis
42+
uses: github/codeql-action/analyze@v3
43+

.github/workflows/cpp_linter.yml

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
name: CPP Linter
2+
3+
on:
4+
push:
5+
paths-ignore:
6+
- '.clang*'
7+
- '.gitignore'
8+
- 'LICENSE'
9+
- 'README*'
10+
pull_request:
11+
paths-ignore:
12+
- '.clang*'
13+
- '.gitignore'
14+
- 'LICENSE'
15+
- 'README*'
16+
17+
jobs:
18+
cpp-linter:
19+
runs-on: ubuntu-latest
20+
steps:
21+
- uses: actions/checkout@v4
22+
with:
23+
fetch-depth: 1
24+
25+
- uses: ./.github/actions/install-dependencies
26+
27+
- name: Configure and build on ubuntu
28+
shell: bash
29+
run: |
30+
cmake \
31+
-S . \
32+
-B ./build \
33+
-DCMAKE_BUILD_TYPE=Release \
34+
-DCMAKE_EXPORT_COMPILE_COMMANDS=ON \
35+
-G "Ninja" \
36+
|| (cat ./build/vcpkg_installed/vcpkg/issue_body.md && exit 1)
37+
cmake --build ./build --config Release
38+
39+
- uses: cpp-linter/cpp-linter-action@main
40+
id: linter
41+
continue-on-error: true
42+
env:
43+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
44+
with:
45+
style: file
46+
files-changed-only: false
47+
thread-comments: false
48+
step-summary: true
49+
database: build
50+
51+
- name: Fail fast?!
52+
if: steps.linter.outputs.clang-tidy-checks-failed > 0
53+
run: exit 1

Algorithm/Search/search.hpp

+13-13
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ auto fibonacci_search(const std::vector<T> &v, const T &value) -> int
5151
while (low <= high) { // 当最小下标小于等于最大下标时
5252
int mid = low + fibonacci[k - 1] - 1; // 取黄金分割点
5353
if (mid >= v.size()) { // 如果黄金分割点大于等于数组长度
54-
mid = v.size() - 1; // 就将黄金分割点设置为数组最后一个元素的下标
54+
mid = v.size() - 1; // 就将黄金分割点设置为数组最后一个元素的下标
5555
}
56-
if (v[mid] == value) { // 如果中间元素等于要查找的元素,就返回下标
56+
if (v[mid] == value) { // 如果中间元素等于要查找的元素,就返回下标
5757
return mid;
5858
} else if (v[mid] < value) { // 如果中间元素小于要查找的元素,就在右半部分查找
5959
low = mid + 1;
@@ -96,19 +96,19 @@ auto linear_index_search(const std::vector<T> &v, const T &value) -> int
9696
template<typename T>
9797
auto kmp_search(const std::vector<T> &v, const std::vector<T> &pattern) -> int
9898
{
99-
std::vector<int> next(pattern.size()); // next数组
100-
next[0] = -1; // next数组的第一个元素为-1
101-
int k = -1; // next数组的下标
102-
for (int i = 1; i < pattern.size(); ++i) { // 生成next数组
99+
std::vector<int> next(pattern.size()); // next数组
100+
next[0] = -1; // next数组的第一个元素为-1
101+
int k = -1; // next数组的下标
102+
for (int i = 1; i < pattern.size(); ++i) { // 生成next数组
103103
while (k > -1 && pattern[k + 1] != pattern[i]) { // 如果k大于-1且下一个元素不等于当前元素
104104
k = next[k];
105105
}
106106
if (pattern[k + 1] == pattern[i]) { // 如果下一个元素等于当前元素
107107
++k;
108108
}
109-
next[i] = k; // 将k赋值给next数组的当前元素
109+
next[i] = k; // 将k赋值给next数组的当前元素
110110
}
111-
k = -1; // next数组的下标
111+
k = -1; // next数组的下标
112112
for (int i = 0; i < v.size(); ++i) {
113113
while (k > -1 && pattern[k + 1] != v[i]) { // 如果k大于-1且下一个元素不等于当前元素
114114
k = next[k];
@@ -146,7 +146,7 @@ auto bm_search(const std::vector<T> &v, const std::vector<T> &pattern) -> int
146146
bc[pattern[i]] = i;
147147
}
148148

149-
std::vector<int> suffix(pattern.size(), -1); // 好后缀
149+
std::vector<int> suffix(pattern.size(), -1); // 好后缀
150150
std::vector<bool> prefix(pattern.size(), false); // 好后缀的前缀子串是否在模式串的前缀子串中存在
151151
for (int i = 0; i < pattern.size() - 1; ++i) {
152152
int j = i;
@@ -161,7 +161,7 @@ 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 <= v.size() - pattern.size()) { // 从头到尾遍历
165165
int j = pattern.size() - 1;
166166
while (j >= 0 && v[i + j] == pattern[j]) { // 从后往前比较
167167
--j;
@@ -171,7 +171,7 @@ auto bm_search(const std::vector<T> &v, const std::vector<T> &pattern) -> int
171171
}
172172
i += std::max(move_by_suffix(j, suffix, prefix), j - bc[v[i + j]]); // 将模式串向后移动
173173
}
174-
return -1; // not found
174+
return -1; // not found
175175
}
176176

177177
// Sunday查找
@@ -183,7 +183,7 @@ auto sunday_search(const std::vector<T> &v, const std::vector<T> &pattern) -> in
183183
bc[pattern[i]] = i;
184184
}
185185
int i = 0;
186-
while (i <= v.size() - pattern.size()) { // 从头到尾遍历
186+
while (i <= v.size() - pattern.size()) { // 从头到尾遍历
187187
int j = 0;
188188
while (j < pattern.size() && v[i + j] == pattern[j]) { // 从前往后比较
189189
++j;
@@ -196,7 +196,7 @@ auto sunday_search(const std::vector<T> &v, const std::vector<T> &pattern) -> in
196196
}
197197
i += pattern.size() - bc[v[i + pattern.size()]]; // 将模式串向后移动
198198
}
199-
return -1; // not found
199+
return -1; // not found
200200
}
201201

202202
// Rabin-Karp查找

vcpkg.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,5 @@
2727
]
2828
}
2929
],
30-
"builtin-baseline": "3de032f834a9b28a455e35600b03e9d365ce3b85"
30+
"builtin-baseline": "d5cb5b9392b89fe9ad25786022415999202277d6"
3131
}

0 commit comments

Comments
 (0)