From 6a9c1bb49a8ac8a35256fd0dceadeefc28626d7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B9=E6=99=93=E8=88=AA?= <1210603696@qq.com> Date: Tue, 10 Feb 2015 10:50:38 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90to=5Fstring=E5=92=8C=E9=87=8D?= =?UTF-8?q?=E6=9E=84=E4=BA=86add=5Fnode?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TinySTL/Detail/Graph.impl.h | 37 +++++++++++++++++++++++++++++++------ TinySTL/Graph.h | 13 ++++++++++++- 2 files changed, 43 insertions(+), 7 deletions(-) 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); }; }