Skip to content

Commit 2ef0977

Browse files
committed
修改namespace结构和目录结构使得工程有更强的维护性与拓展性
1 parent 0880825 commit 2ef0977

File tree

9 files changed

+156
-133
lines changed

9 files changed

+156
-133
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ CMakeFiles
33
CMakeScripts
44
Testing
55
bin
6+
lib
67
*~
78
*/*~
89
*/*/*~

CMakeLists.txt

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ cmake_minimum_required(VERSION 3.0)
22

33
project(ALGORITHM_MODULE)
44

5-
if (CMAKE_BUILD_TYPE STREQUAL Debug)
6-
set(DEBUG YES)
5+
if (CMAKE_BUILD_TYPE STREQUAL Debug)
6+
set(LIBRARY_TYPE STATIC)
77
else()
8-
set(DEBUG NO)
8+
set(LIBRARY_TYPE SHARED)
99
endif()
1010

1111
include_directories(${ALGORITHM_MODULE_SOURCE_DIR}/include)

include/algorithm_module.h

+91-68
Original file line numberDiff line numberDiff line change
@@ -14,77 +14,100 @@
1414
#define PUBLIC
1515
#endif
1616

17-
namespace GraphAlgorithm {
18-
19-
//适配器类型声明
20-
21-
//@Param idx_v:节点编号
22-
//@Return:节点idx_v相连的第一条边的编号,无边时返回NIL
23-
typedef std::function<int(int idx_v)> FirstEdgeAdapter;
24-
25-
//@Param: idx_v:节点编号
26-
//@Param: idx_e:当前扫描到的边
27-
//@Return:返回下一条边的id,无边时返回NIL
28-
typedef std::function<int(int idx_v, int idx_e)> NextEdgeAdapter;
29-
30-
//@Param: idx_v:节点编号
31-
//@Param: idx_e:当前扫描到的边
32-
//@Return:返回指向节点的id,无边时返回NIL
33-
typedef std::function<int(int idx_v, int idx_e)> EdgeToAdapter;
34-
35-
//@Param idx_v:表示此边与v相连
36-
//@Param idx_e:表示此边编号
37-
//@Return:节点idx_v的编号为idx_e的边的长度
38-
typedef std::function<double(int idx_v, int idx_e)> EdgeLengthAdapter;
39-
40-
//@Param idx_v:表示节点idx_v
41-
//@Param idx_e:表示与idx_v相连的编号为idx_e的边
42-
//@Return:节点idx_v的编号为idx_e的边的容量
43-
typedef std::function <double(int idx_v, int idx_e)> EdgeContainAdapter;
44-
45-
//@Param first:表示点的序列
46-
//@Param second:表示边的序列
47-
//@Description:如果无路径,则点的序列为空
48-
typedef std::pair<std::list<int>,std::list<int>> PathType;
17+
namespace Algorithm {
4918

5019
//不存在的编号,长度..
5120
const int NIL = -1;
52-
const static PathType NIL_PATH = std::make_pair(std::list<int>(), std::list<int>());
53-
54-
struct GraphAdapter {
55-
FirstEdgeAdapter firstOf;
56-
NextEdgeAdapter nextOf;
57-
EdgeToAdapter destOf;
58-
EdgeLengthAdapter lengthOf;
59-
EdgeContainAdapter containOf;
60-
};
61-
62-
//@Param idx_start_v:最初的节点编号
63-
//@Param idx_dest_v:目标节点编号
64-
//@Param firstOf,nextOf,edgeTo:图的属性
65-
//@Return: 返回所需的步数
66-
67-
PUBLIC PathType
68-
bfs(
69-
int idx_start_v,int idx_dest_v,
70-
const FirstEdgeAdapter & firstOf,const NextEdgeAdapter & nextOf,
71-
const EdgeToAdapter & destOf);
72-
73-
inline int
74-
bfsStep(
75-
int idx_start_v, int idx_dest_v,
76-
const FirstEdgeAdapter & firstOf, const NextEdgeAdapter & nextOf,
77-
const EdgeToAdapter & destOf) {
78-
79-
PathType tmp = bfs(idx_start_v, idx_dest_v, firstOf, nextOf, destOf);
80-
return tmp.first.size() - 1;
81-
};
82-
83-
inline int
84-
bfsStep(
85-
int idx_start_v, int idx_dest_v,
86-
const GraphAdapter & graph) {
87-
return bfsStep(idx_start_v, idx_dest_v,
21+
22+
namespace Graph {
23+
24+
//适配器类型声明
25+
26+
//@Param idx_v:节点编号
27+
//@Return:节点idx_v相连的第一条边的编号,无边时返回NIL
28+
typedef std::function<int(int idx_v)> FirstEdgeAdapter;
29+
30+
//@Param: idx_v:节点编号
31+
//@Param: idx_e:当前扫描到的边
32+
//@Return:返回下一条边的id,无边时返回NIL
33+
typedef std::function<int(int idx_v, int idx_e)> NextEdgeAdapter;
34+
35+
//@Param: idx_v:节点编号
36+
//@Param: idx_e:当前扫描到的边
37+
//@Return:返回指向节点的id,无边时返回NIL
38+
typedef std::function<int(int idx_v, int idx_e)> EdgeToAdapter;
39+
40+
//@Param idx_v:表示此边与v相连
41+
//@Param idx_e:表示此边编号
42+
//@Return:节点idx_v的编号为idx_e的边的长度
43+
typedef std::function<double(int idx_v, int idx_e)> EdgeLengthAdapter;
44+
45+
//@Param idx_v:表示节点idx_v
46+
//@Param idx_e:表示与idx_v相连的编号为idx_e的边
47+
//@Return:节点idx_v的编号为idx_e的边的容量
48+
typedef std::function <double(int idx_v, int idx_e)> EdgeContainAdapter;
49+
50+
//@Param first:表示点的序列
51+
//@Param second:表示边的序列
52+
//@Description:如果无路径,则点的序列为空
53+
typedef std::pair<std::list<int>, std::list<int>> PathType;
54+
55+
const static PathType NIL_PATH = std::make_pair(std::list<int>(), std::list<int>());
56+
57+
struct GraphAdapter {
58+
FirstEdgeAdapter firstOf;
59+
NextEdgeAdapter nextOf;
60+
EdgeToAdapter destOf;
61+
EdgeLengthAdapter lengthOf;
62+
EdgeContainAdapter containOf;
63+
};
64+
65+
//@Param idx_start_v:最初的节点编号
66+
//@Param idx_dest_v:目标节点编号
67+
//@Param firstOf,nextOf,edgeTo:图的属性
68+
//@Return: 返回所走的路径
69+
PUBLIC PathType
70+
bfs(
71+
int idx_start_v, int idx_dest_v,
72+
const FirstEdgeAdapter & firstOf, const NextEdgeAdapter & nextOf,
73+
const EdgeToAdapter & destOf);
74+
75+
//@Param idx_start_v:最初的节点编号
76+
//@Param idx_dest_v:目标节点编号
77+
//@Param graph:图的属性
78+
//@Return: 返回所走的路径
79+
inline PathType
80+
bfs(
81+
int idx_start_v, int idx_dest_v,
82+
const GraphAdapter& graph) {
83+
return bfs(idx_start_v, idx_dest_v,
84+
graph.firstOf, graph.nextOf, graph.destOf);
85+
}
86+
87+
//@Param idx_start_v:最初的节点编号
88+
//@Param idx_dest_v:目标节点编号
89+
//@Param firstOf,nextOf,edgeTo:图的属性
90+
//@Return: 返回所需的步数
91+
inline int
92+
bfsStep(
93+
int idx_start_v, int idx_dest_v,
94+
const FirstEdgeAdapter & firstOf, const NextEdgeAdapter & nextOf,
95+
const EdgeToAdapter & destOf) {
96+
97+
PathType tmp = bfs(idx_start_v, idx_dest_v, firstOf, nextOf, destOf);
98+
return tmp.first.size() - 1;
99+
};
100+
101+
//@Param idx_start_v:最初的节点编号
102+
//@Param idx_dest_v:目标节点编号
103+
//@Param graph:图的属性
104+
//@Return: 返回所需的步数
105+
inline int
106+
bfsStep(
107+
int idx_start_v, int idx_dest_v,
108+
const GraphAdapter & graph) {
109+
return bfsStep(idx_start_v, idx_dest_v,
88110
graph.firstOf, graph.nextOf, graph.destOf);
111+
}
89112
}
90113
}

src/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
ADD_SUBDIRECTORY(algorithm_module)
2+
23
ADD_SUBDIRECTORY(test)

src/algorithm_module/CMakeLists.txt

+1-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
SET(LIBRARY_OUTPUT_PATH ${ALGORITHM_MODULE_SOURCE_DIR}/lib)
2-
SET(SRC_LIST algorithm_module.cpp)
32

4-
IF(DEBUG)
5-
SET(LIBRARY_TYPE STATIC)
6-
ELSE(DEBUG)
7-
SET(LIBRARY_TYPE SHARED)
8-
ENDIF(DEBUG)
3+
aux_source_directory (. SRC_LIST)
94

105
INCLUDE_DIRECTORIES(${ALGORITHM_MODULE_SOURCE_DIR}/include)
116
ADD_LIBRARY(algorithm_module ${LIBRARY_TYPE} ${SRC_LIST})

src/algorithm_module/Graph.cpp

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include "algorithm_module.h"
2+
3+
using namespace Algorithm;
4+
using namespace Algorithm::Graph;
5+
6+
PUBLIC PathType
7+
Graph::bfs(
8+
int idx_start_v, int idx_dest_v,
9+
const FirstEdgeAdapter & firstOf, const NextEdgeAdapter & nextOf,
10+
const EdgeToAdapter & destOf) {
11+
12+
std::list<int> indexList{ idx_start_v };
13+
std::unordered_map<int, int> prevOf{ std::make_pair(idx_start_v,NIL) };
14+
std::unordered_map<int, int> prevEdgeOf{ std::make_pair(idx_start_v,NIL) };
15+
16+
for (int v : indexList) {
17+
if (v == idx_dest_v) {
18+
PathType ret;
19+
for (int u = v; u != NIL; u = prevOf[u]) {
20+
ret.first.push_front(u);
21+
ret.second.push_back(prevEdgeOf[u]);
22+
}
23+
ret.second.pop_front(); //prevEdgeOf[idx_start_v] == NIL
24+
return ret;
25+
}
26+
if (v != idx_start_v) indexList.pop_front();
27+
28+
for (int edge = firstOf(v); edge != NIL; edge = nextOf(v, edge)) {
29+
const int v1 = destOf(v, edge);
30+
31+
if (prevOf.count(v1) != 0) continue;
32+
33+
prevOf.insert(std::make_pair(v1, v));
34+
prevEdgeOf.insert(std::make_pair(v1, edge));
35+
indexList.push_back(v1);
36+
}
37+
}
38+
return NIL_PATH;
39+
}

src/algorithm_module/algorithm_module.cpp

-42
This file was deleted.

src/test/CMakeLists.txt

+7-5
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ SET(EXECUTABLE_OUTPUT_PATH ${ALGORITHM_MODULE_SOURCE_DIR}/bin)
22

33
aux_source_directory (. SRC_LIST)
44

5-
FOREACH(source_file ${SRC_LIST})
6-
STRING (REGEX REPLACE "(./)|(_main.cpp)" "" bin_name "${source_file}")
7-
ADD_EXECUTABLE ("${bin_name}" "${source_file}")
8-
TARGET_LINK_LIBRARIES ("${bin_name}" algorithm_module)
9-
ENDFOREACH()
5+
if (LIBRARY_TYPE STREQUAL STATIC)
6+
FOREACH(source_file ${SRC_LIST})
7+
STRING (REGEX REPLACE "(./)|(_main.cpp)" "" bin_name "${source_file}")
8+
ADD_EXECUTABLE ("${bin_name}" "${source_file}")
9+
TARGET_LINK_LIBRARIES ("${bin_name}" algorithm_module)
10+
ENDFOREACH()
11+
endif()

src/test/poj_2251_main.cpp

+13-9
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1-
// DLLTester.cpp: 定义控制台应用程序的入口点。
2-
//
1+
// http://poj.org/problem?id=2251
2+
// 这是原题,不过。。因为POJ不支持C++11,所以就算把代码复制粘贴好也不能送上去评测
3+
34
#include "algorithm_module.h"
45
#include <cstdio>
56

7+
using namespace Algorithm;
8+
using namespace Algorithm::Graph;
9+
610
const int maxn = 30;
711
int a, b, t;
812
char s[maxn][maxn][maxn];
@@ -27,19 +31,19 @@ inline int walk(int v, int i) {
2731
const static int dh[] = { 0, 0, 0, 0, 1, -1 };
2832
const Node next(v % (a*b) / b + dr[i], v % b + dc[i], v / (a*b) + dh[i]);
2933
if (inside(next)) return encoder(next.h, next.r, next.c);
30-
else return GraphAlgorithm::NIL;
34+
else return NIL;
3135
}
3236

33-
GraphAlgorithm::NextEdgeAdapter nextOf =
37+
NextEdgeAdapter nextOf =
3438
[](int v, int e) -> int{
3539
for (int e1 = e+1; e1 < 6; e1++) {
36-
if (walk(v, e1) != GraphAlgorithm::NIL)
40+
if (walk(v, e1) != NIL)
3741
return e1;
3842
}
39-
return GraphAlgorithm::NIL;
43+
return NIL;
4044
};
4145

42-
GraphAlgorithm::GraphAdapter graph = {
46+
GraphAdapter graph = {
4347
std::bind2nd(nextOf,-1),
4448
nextOf,
4549
walk
@@ -59,8 +63,8 @@ int main() {
5963
}
6064

6165
const int ans =
62-
GraphAlgorithm::bfsStep(start, dest, graph);
63-
if (ans != GraphAlgorithm::NIL) printf("Escaped in %d minute(s).\n", ans);
66+
bfsStep(start, dest, graph);
67+
if (ans != NIL) printf("Escaped in %d minute(s).\n", ans);
6468
else printf("Trapped!\n");
6569
}
6670
return 0;

0 commit comments

Comments
 (0)