-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNode.h
More file actions
244 lines (209 loc) · 7.02 KB
/
Copy pathNode.h
File metadata and controls
244 lines (209 loc) · 7.02 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
//
// Created by 翟霄 on 2021/7/16.
//
#ifndef GRAPHCOMPUTE_NODE_H
#define GRAPHCOMPUTE_NODE_H
#include <vector>
#include <map>
#include <unordered_map>
#include <string>
#include <random>
#include "type_defination.h"
class Node {
public:
/**
* 默认构造方法
*/
Node();
/**
* 带参构造方法
*/
Node(const std::string &id, const std::string &type);
/**
* 析构方法
*/
~Node();
/**
* 获取点的ID
* @return 返回引用
*/
const std::string &getID() const;
/**
* 获取点的类型
* @return 返回引用
*/
const std::string &getType() const;
const std::string getTypeID() const;
/**
* 返回该点邻接点列表
* @return 点对象中邻接点列表的引用
*/
const std::vector<std::pair<Node*, bool>> &getLinkedNodeList() const;
/**
* 返回该点的分类型邻接表
* @return
*/
const std::unordered_map<std::string, std::vector<Node *>> &getLinkedNodeMapList() const;
/**
* 返回该点指定若干类型的合并邻接表
* @param typeList
* @return
*/
const std::vector<Node*> getLinkedNodeMapList(const std::vector<std::string> &typeList) const;
/**
* 返回指定类型的第一个邻接点
* @param type
* @return
*/
Node* getFirstLinkedNode(const std::string &type) const;
/**
* 增加边
* @param nodeID 边对应连接点的ID
* @param node 边对应连接点的对象的常量指针
*/
void addEdge(Node *const &node);
/**
* 获得当前点尚未访问的连接点列表
* @return 当前点尚未访问的连接点常量指针列表
* 返回值为常量指针列表则不允许通过其他方式修改列表中的元素,否则列表中原指针指向的内存将存在泄漏风险
*/
const std::vector<Node*> getNoVisitedLinkedNodeList() const;
/**
* 获奖点的基本信息
* @return
*/
const std::string to_string() const;
/**
* 获取当前点全部边的连接信息
* @return
*/
const std::string getLinkedInfo() const;
/**
* 判断当前节点是否可访问
* @return
*/
bool canVisit() const;
/**
* 访问点
* 访问次数加1
*/
void visit(const unsigned int &visitCount=1);
/**
* 判断当前节点是否访问过
* 若访问次数大于1则返回true否则返回false
* @return
*/
bool isVisited() const;
/**
* 获取当前节点的访问次数
* @return
*/
int getVisitedCount() const;
/**
* 获得当前点指定类型的邻接点
* @param strategy 边选择策略
* @param nextNode 邻接点指针
* @param type 邻接点类型
* @return
* 返回值为指针常量,说明指针指向内存无需调用者负责,同时也不允许通过其他方式修改指针指向,否则原指针指向内容将可能存在泄漏风险
*/
bool getNextLinkedNode(const EdgeChooseStrategy &strategy, Node *&nextNode, const std::string &type);
/**
* 获得当前点指定多个类型的邻接点
* @param strategy 边选择策略
* @param nextNode 邻接点指针
* @param typeList 邻接点类型数组
* @return
*/
bool getNextLinkedNode(const EdgeChooseStrategy &strategy, Node *&nextNode, const std::vector<std::string> &typeList);
/**
* 随机获取指定类型的邻接点
* @param nextNode 邻接点指针
* @param type 邻接点类型
* @return
*/
bool getNextRandomLinkedNode(Node *&nextNode, const std::string &type, std::mt19937 &randomEngine);
/**
* 随机获取多个类型的邻接点
* @param nextNode 邻接点指针
* @param typeList 邻接点类型数组
* @return
*/
bool getNextRandomLinkedNode(Node *&nextNode, const std::vector<std::string> &typeList, std::mt19937 &randomEngine);
/**
* 重置图遍历/游走的状态信息
*/
void reset(const bool &onlyVisitedCount = true);
/**
* 排除点
* 将当前节点的可访问状态置为false
*/
void exclude();
bool excludeEdge(std::string endNodeTypeID);
/**
* 包含点
* 将当前点的可访问状态置为true
*/
void include();
bool includeEdge(std::string endNodeTypeID);
/**
* 根据原始链表刷新分类型链表
* 判断原始链表中全部节点是否可访问
* 只将可访问节点加入分类型节点
*/
void flushLinkedNodes();
private:
/**
* 私有成员
*/
// 点ID
std::string id;
// 点类型
std::string type;
// 当前点是否可访问
bool canVisitFlag = true;
// 与当前点有边连接的链表
// 由于该指针对应的内容均为Graph对象创建,由Graph对象负责维护
// 所以在Node对象中不应该将其中的指针重指向其他点,否则将出现野指针
std::vector<std::pair<Node*, bool>> linkedNodeList;
// 与当前点有边连接的类型链表
// key为连接点的type
// value为对应type对应的连接点对象的指针列表
// 由于该指针对应的内容均为Graph对象创建,由Graph对象负责维护
// 所以在Node对象中不应该将其中的指针重指向其他点,否则将出现野指针
std::unordered_map<std::string, std::vector<Node *>> linkedNodeMapList;
/**
* 图遍历或游走的状态信息
*/
unsigned int vistedCount;
/**
* 私有方法
*/
/**
* 在给定的节点列表里根据指定策略选择边
* @return
*/
static bool getNextLinkedNode(const std::vector<Node *> &nodeList,
Node *&nextNode,
const EdgeChooseStrategy &strategy,
std::mt19937 &randomEngine,
std::uniform_int_distribution<unsigned> &randomDistribution);
/**
* 具体边选择策略的选边方法
* @param nodeList
* @param nextNode
* @return
*/
static bool getNextFirstLinkedNode(const std::vector<Node *> &nodeList, Node* &nextNode);
static bool getNextFirstNoVisitedLinkedNode(const std::vector<Node *> &nodeList, Node* &nextNode);
static bool getNextLastLinkedNode(const std::vector<Node *> &nodeList, Node* &nextNode);
static bool getNextLastNoVisitedLinkedNode(const std::vector<Node *> &nodeList, Node* &nextNode);
static bool getNextRandomLinkedNode(const std::vector<Node *> &nodeList,
Node* &nextNode,
std::mt19937 &randomEngine,
std::uniform_int_distribution<unsigned> &randomDistribution);
static bool getNextRandomNoVisitedLinkedNode(const std::vector<Node *> &nodeList,
Node* &nextNode,
std::mt19937 &randomEngine);
};
#endif //GRAPHCOMPUTE_NODE_H