From e8ecf9df25198af86912670604615e490c66b467 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B9=E6=99=93=E8=88=AA?= <1210603696@qq.com> Date: Mon, 9 Feb 2015 16:58:09 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90DFS?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TinySTL/Detail/Graph.impl.h | 39 ++++++++++++++++++++++++++++++++----- TinySTL/Graph.h | 15 +++++++++++--- 2 files changed, 46 insertions(+), 8 deletions(-) diff --git a/TinySTL/Detail/Graph.impl.h b/TinySTL/Detail/Graph.impl.h index a3118d9..38b7d27 100644 --- a/TinySTL/Detail/Graph.impl.h +++ b/TinySTL/Detail/Graph.impl.h @@ -10,6 +10,27 @@ namespace TinySTL{ return *ptr; } template + void graph::del_node(node *p){ + nodeAllocator::destroy(p); + nodeAllocator::deallocate(p); + } + template + typename graph::node& + graph::get_node(const Index& index){ + for (auto& pair : nodes_){ + if (equal_func(pair.first.first, index)) + return pair.first; + } + } + template + void graph::cleanup(){ + for (auto out = nodes_.begin(); out != nodes_.end(); ++out){ + for (auto in = (out->second).begin(); in != (out->second).end(); ++in){ + del_node(&(*in)); + } + } + } + template bool graph::is_contained(const Index& index){ for (auto& pair : nodes_){ if (equal_func(pair.first.first, index)) @@ -63,15 +84,23 @@ namespace TinySTL{ graph::adjacent_nodes(const node& n){ return adjacent_nodes(n.first); } + template + void graph::DFS(const Index& index, visiter_func_type func){ + node *start = &(get_node(index)); + Unordered_set, EqualFunc> visited(7); + + auto nodes = adjacent_nodes(start->first); + func(*start); + visited.insert(start->first); + for (auto& n : nodes){ + if (visited.count(n.first) == 0)//has not visited + DFS(n.first, func); + } + } //******************************************************************************** template graph_iterator& graph_iterator::operator ++(){ ++inner_it_; - //if (inner_it_ == (outter_it_->second).end()){//to end - // ++outter_it_; - // if (outter_it_ != container_->nodes_.end())//not to end - // inner_it_ = (outter_it_->second).begin(); - //} return *this; } template diff --git a/TinySTL/Graph.h b/TinySTL/Graph.h index 460aad2..84617a7 100644 --- a/TinySTL/Graph.h +++ b/TinySTL/Graph.h @@ -3,6 +3,7 @@ #include "Allocator.h" #include "List.h" +#include "Unordered_set.h" #include "Utility.h" #include "Vector.h" @@ -12,6 +13,7 @@ namespace TinySTL{ namespace Detail{ template class graph_iterator; + template> class graph{ public: @@ -27,13 +29,18 @@ namespace TinySTL{ typedef std::function visiter_func_type; public: graph() :size_(0){}; - virtual ~graph(){}; + virtual ~graph(){ cleanup(); }; + virtual void add_node(const node& item, const node_sets& nodes) = 0; //virtual void delte_node(const node& item) = 0; - //virtual void DFS(visiter_func_type func) = 0; + + void DFS(const Index& index, visiter_func_type func); //virtual void BFS(visiter_func_type func) = 0; - static node& new_node(const Index& index, const Value& val); + node& new_node(const Index& index, const Value& val); + void del_node(node *p); + node& get_node(const Index& index); + bool is_contained(const Index& index); inline static node_sets empty_node_set(); node_sets adjacent_nodes(const Index& index); @@ -47,6 +54,8 @@ namespace TinySTL{ list>> nodes_; equal_func_type equal_func; size_t size_; + protected: + void cleanup(); }; template>