提交 d24cfa29 编写于 作者: 邹晓航

bug fix

上级 58f37adf
...@@ -8,13 +8,14 @@ namespace TinySTL{ ...@@ -8,13 +8,14 @@ namespace TinySTL{
return node_type(index, val); return node_type(index, val);
} }
template<class Index, class Value, class EqualFunc> template<class Index, class Value, class EqualFunc>
typename graph<Index, Value, EqualFunc>::node_type& typename const graph<Index, Value, EqualFunc>::node_type&
graph<Index, Value, EqualFunc>::get_node(const Index& index){ graph<Index, Value, EqualFunc>::get_node(const Index& index){
for (auto& pair : nodes_){ for (auto& pair : nodes_){
if (equal_func(pair.first.first, index)) if (equal_func(pair.first.first, index))
return pair.first; return pair.first;
} }
return node_type(); static node_type empty_node;
return empty_node;
} }
template<class Index, class Value, class EqualFunc> template<class Index, class Value, class EqualFunc>
bool graph<Index, Value, EqualFunc>::is_contained(const Index& index){ bool graph<Index, Value, EqualFunc>::is_contained(const Index& index){
...@@ -78,26 +79,28 @@ namespace TinySTL{ ...@@ -78,26 +79,28 @@ namespace TinySTL{
return adjacent_nodes(n.first); return adjacent_nodes(n.first);
} }
template<class Index, class Value, class EqualFunc> template<class Index, class Value, class EqualFunc>
void graph<Index, Value, EqualFunc>::DFS(const Index& index, visiter_func_type func){ void graph<Index, Value, EqualFunc>::_DFS(node_type& node,
node_type *start = &(get_node(index)); visiter_func_type func, Unordered_set<Index, std::hash<Index>, EqualFunc>& visited){
Unordered_set<Index, std::hash<Index>, EqualFunc> visited(7); auto nodes = adjacent_nodes(node.first);
func(node);
auto nodes = adjacent_nodes(start->first); visited.insert(node.first);
func(*start); for (auto& n : nodes){
visited.insert(start->first);
for (const auto& n : nodes){
if (visited.count(n.first) == 0)//has not visited if (visited.count(n.first) == 0)//has not visited
DFS(n.first, func); _DFS(n, func, visited);
} }
} }
template<class Index, class Value, class EqualFunc> template<class Index, class Value, class EqualFunc>
void graph<Index, Value, EqualFunc>::BFS(const Index& index, visiter_func_type func){ void graph<Index, Value, EqualFunc>::DFS(const Index& index, visiter_func_type func){
node_type *start = &(get_node(index)); node_type start = (get_node(index));
Unordered_set<Index, std::hash<Index>, EqualFunc> visited(7); Unordered_set<Index, std::hash<Index>, EqualFunc> visited(7);
_DFS(start, func, visited);
auto nodes = adjacent_nodes(start->first); }
func(*start); template<class Index, class Value, class EqualFunc>
visited.insert(start->first); void graph<Index, Value, EqualFunc>::_BFS(node_type& node,
visiter_func_type func, Unordered_set<Index, std::hash<Index>, EqualFunc>& visited){
auto nodes = adjacent_nodes(node.first);
func(node);
visited.insert(node.first);
do{ do{
nodes_set_type temp; nodes_set_type temp;
for (auto it = nodes.begin(); it != nodes.end(); ++it){ for (auto it = nodes.begin(); it != nodes.end(); ++it){
...@@ -112,6 +115,27 @@ namespace TinySTL{ ...@@ -112,6 +115,27 @@ namespace TinySTL{
} while (!nodes.empty()); } while (!nodes.empty());
} }
template<class Index, class Value, class EqualFunc> template<class Index, class Value, class EqualFunc>
void graph<Index, Value, EqualFunc>::BFS(const Index& index, visiter_func_type func){
node_type start = (get_node(index));
Unordered_set<Index, std::hash<Index>, EqualFunc> visited(7);
_BFS(start, func, visited);
//auto nodes = adjacent_nodes(start->first);
//func(*start);
//visited.insert(start->first);
//do{
// nodes_set_type temp;
// for (auto it = nodes.begin(); it != nodes.end(); ++it){
// if (visited.count(it->first) == 0){//has not visited
// func(*it);
// visited.insert(it->first);
// auto s = adjacent_nodes(it->first);
// temp.insert(temp.end(), s.begin(), s.end());
// }
// }
// nodes = temp;
//} while (!nodes.empty());
}
template<class Index, class Value, class EqualFunc>
string graph<Index, Value, EqualFunc>::to_string(){ string graph<Index, Value, EqualFunc>::to_string(){
string str; string str;
std::ostringstream oss; std::ostringstream oss;
...@@ -120,7 +144,7 @@ namespace TinySTL{ ...@@ -120,7 +144,7 @@ namespace TinySTL{
oss << "[" << oit->first << "," << oit->second << "]" << ":"; oss << "[" << oit->first << "," << oit->second << "]" << ":";
auto eit = end(oit->first); auto eit = end(oit->first);
for (auto iit = begin(oit->first); iit != eit; ++iit){ for (auto iit = begin(oit->first); iit != eit; ++iit){
oss << "[" << iit->first << ", " << iit->second << "]" << "->"; oss << "[" << iit->first << "," << iit->second << "]" << "->";
} }
oss << "[nil]" << std::endl << std::setw(4) << "|" << std::endl; oss << "[nil]" << std::endl << std::setw(4) << "|" << std::endl;
} }
...@@ -231,4 +255,12 @@ namespace TinySTL{ ...@@ -231,4 +255,12 @@ namespace TinySTL{
void directed_graph<Index, Value, EqualFunc>::delete_node(const node_type& item){ void directed_graph<Index, Value, EqualFunc>::delete_node(const node_type& item){
delete_node(item.first); delete_node(item.first);
} }
template<class Index, class Value, class EqualFunc>
void directed_graph<Index, Value, EqualFunc>::make_edge(const Index& index1, const Index& index2){
auto node1 = get_node(index1), node2 = get_node(index2);
for (auto it = nodes_.begin(); it != nodes_.end(); ++it){
if (equal_func((it->first).first, index1))
(it->second).push_front(node2);
}
}
} }
\ No newline at end of file
...@@ -41,6 +41,7 @@ namespace TinySTL{ ...@@ -41,6 +41,7 @@ namespace TinySTL{
virtual void add_node(const node_type& item, const nodes_set_type& nodes) = 0; virtual void add_node(const node_type& item, const nodes_set_type& nodes) = 0;
//node of the index must in the graph //node of the index must in the graph
virtual void add_node(const Index& index, const nodes_set_type& nodes) = 0; virtual void add_node(const Index& index, const nodes_set_type& nodes) = 0;
virtual void make_edge(const Index& index1, const Index& index2) = 0;
virtual void delete_node(const node_type& item) = 0; virtual void delete_node(const node_type& item) = 0;
virtual void delete_node(const Index& index) = 0; virtual void delete_node(const Index& index) = 0;
...@@ -49,7 +50,7 @@ namespace TinySTL{ ...@@ -49,7 +50,7 @@ namespace TinySTL{
void BFS(const Index& index, visiter_func_type func); void BFS(const Index& index, visiter_func_type func);
node_type make_node(const Index& index, const Value& val); node_type make_node(const Index& index, const Value& val);
node_type& get_node(const Index& index); const node_type& get_node(const Index& index);
bool is_contained(const Index& index); bool is_contained(const Index& index);
inline static nodes_set_type empty_node_set(); inline static nodes_set_type empty_node_set();
...@@ -65,6 +66,9 @@ namespace TinySTL{ ...@@ -65,6 +66,9 @@ namespace TinySTL{
equal_func_type get_equal_func()const; equal_func_type get_equal_func()const;
string to_string(); string to_string();
protected:
void _DFS(node_type& node, visiter_func_type func, Unordered_set<Index, std::hash<Index>, EqualFunc>& visited);
void _BFS(node_type& node, visiter_func_type func, Unordered_set<Index, std::hash<Index>, EqualFunc>& visited);
protected: protected:
list<pair<node_type, list<node_type>>> nodes_; list<pair<node_type, list<node_type>>> nodes_;
equal_func_type equal_func; equal_func_type equal_func;
...@@ -138,6 +142,8 @@ namespace TinySTL{ ...@@ -138,6 +142,8 @@ namespace TinySTL{
//node n -> every node_type in the nodes set //node n -> every node_type in the nodes set
void add_node(const node_type& n, const nodes_set_type& nodes) override final; void add_node(const node_type& n, const nodes_set_type& nodes) override final;
void add_node(const Index& index, const nodes_set_type& nodes) override final; void add_node(const Index& index, const nodes_set_type& nodes) override final;
//node index1 -> node index2
void make_edge(const Index& index1, const Index& index2) override final;
void delete_node(const node_type& item) override final; void delete_node(const node_type& item) override final;
void delete_node(const Index& index) override final; void delete_node(const Index& index) override final;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册