diff --git a/TinySTL/Detail/Graph.impl.h b/TinySTL/Detail/Graph.impl.h index d8ec21d..25e6edb 100644 --- a/TinySTL/Detail/Graph.impl.h +++ b/TinySTL/Detail/Graph.impl.h @@ -106,6 +106,23 @@ namespace TinySTL{ DFS(n.first, func); } } + template + string graph::to_string(){ + string str; + std::ostringstream oss; + for (auto oit = begin(); oit != end(); ++oit){ + + oss << "[" << oit->first << "," << oit->second << "]" << ":"; + auto eit = end(oit->first); + for (auto iit = begin(oit->first); iit != eit; ++iit){ + oss << "[" << iit->first << ", " << iit->second << "]" << "-"; + } + oss << "[NULL,NULL]" << std::endl << std::setw(4) << "|" << std::endl; + } + oss << "[NULL,NULL]" << std::endl; + str.append(oss.str().c_str()); + return str; + } //******************************************************************************** template inner_iterator& inner_iterator::operator ++(){ @@ -155,17 +172,13 @@ namespace TinySTL{ template directed_graph::directed_graph():graph(){} template - void directed_graph::add_node(const node& n, const node_sets& nodes){ - if (!is_contained(n.first)){ - nodes_.push_front(make_pair(n, list())); - ++size_; - } + void directed_graph::add_node_helper(const Index& index, const node_sets& nodes){ if (nodes.empty()) return; //find node n's list list* l; for (auto& pair : nodes_){ - if (equal_func(pair.first.first, n.first)) + if (equal_func(pair.first.first, index)) l = &(pair.second); } for (const auto& item : nodes){ @@ -175,4 +188,16 @@ namespace TinySTL{ } } } + template + void directed_graph::add_node(const node& n, const node_sets& nodes){ + if (!is_contained(n.first)){ + nodes_.push_front(make_pair(n, list())); + ++size_; + } + add_node_helper(n.first, nodes); + } + template + void directed_graph::add_node(const Index& index, const node_sets& nodes){ + add_node_helper(index, nodes); + } } \ No newline at end of file diff --git a/TinySTL/Graph.h b/TinySTL/Graph.h index 656d5f5..363abcd 100644 --- a/TinySTL/Graph.h +++ b/TinySTL/Graph.h @@ -3,11 +3,14 @@ #include "Allocator.h" #include "List.h" +#include "String.h" #include "Unordered_set.h" #include "Utility.h" #include "Vector.h" #include +#include +#include namespace TinySTL{ namespace Detail{ @@ -17,7 +20,7 @@ namespace TinySTL{ class outter_iterator; template> - class graph{ + class graph{//base class public: friend class inner_iterator < Index, Value, EqualFunc >; friend class outter_iterator < Index, Value, EqualFunc > ; @@ -35,7 +38,10 @@ namespace TinySTL{ graph() :size_(0){}; virtual ~graph(){ cleanup(); }; + //node can be not in the graph virtual void add_node(const node& item, const node_sets& nodes) = 0; + //node of the index must in the graph + virtual void add_node(const Index& index, const node_sets& nodes) = 0; //virtual void delte_node(const node& item) = 0; void DFS(const Index& index, visiter_func_type func); @@ -56,6 +62,8 @@ namespace TinySTL{ inline inner_iterator end(const Index& index); inline iterator begin(); inline iterator end(); + + string to_string(); protected: list>> nodes_; equal_func_type equal_func; @@ -124,6 +132,9 @@ namespace TinySTL{ ~directed_graph(){} //node n -> every node in the nodes set void add_node(const node& n, const node_sets& nodes) override; + void add_node(const Index& index, const node_sets& nodes) override; + private: + void add_node_helper(const Index& index, const node_sets& nodes); }; }