From 833201262e12005e38c10ee34b53c92cc876894e 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 11:45:55 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90delete=5Fnode?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TinySTL/Detail/Graph.impl.h | 25 +++++++++++++++++++++++-- TinySTL/Graph.h | 13 ++++++++----- 2 files changed, 31 insertions(+), 7 deletions(-) diff --git a/TinySTL/Detail/Graph.impl.h b/TinySTL/Detail/Graph.impl.h index ffe22ac..f5e867e 100644 --- a/TinySTL/Detail/Graph.impl.h +++ b/TinySTL/Detail/Graph.impl.h @@ -138,9 +138,9 @@ namespace TinySTL{ 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 << "[nil,nil]" << std::endl << std::setw(4) << "|" << std::endl; } - oss << "[NULL,NULL]" << std::endl; + oss << "[nil,nil]" << std::endl; str.append(oss.str().c_str()); return str; } @@ -221,4 +221,25 @@ namespace TinySTL{ void directed_graph::add_node(const Index& index, const node_sets& nodes){ add_node_helper(index, nodes); } + template + void directed_graph::delete_node(const Index& index){ + for (auto oit = nodes_.begin(); oit != nodes_.end();){ + if (equal_func((oit->first).first, index)) + oit = nodes_.erase(oit); + else{ + auto& l = oit->second; + for (auto iit = l.begin(); iit != l.end();){ + if (equal_func(iit->first, index)) + iit = l.erase(iit); + else + ++iit; + } + ++oit; + } + } + } + template + void directed_graph::delete_node(const node& item){ + delete_node(item.first); + } } \ No newline at end of file diff --git a/TinySTL/Graph.h b/TinySTL/Graph.h index fb41590..6382fbd 100644 --- a/TinySTL/Graph.h +++ b/TinySTL/Graph.h @@ -40,11 +40,11 @@ namespace TinySTL{ //node can be not in the graph virtual void add_node(const node& item, const node_sets& nodes) = 0; - //virtual void delte_node(const node& item) = 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 Index& index) = 0; + + virtual void delete_node(const node& item) = 0; + virtual void delete_node(const Index& index) = 0; void DFS(const Index& index, visiter_func_type func); void BFS(const Index& index, visiter_func_type func); @@ -133,8 +133,11 @@ namespace TinySTL{ directed_graph(); ~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; + void add_node(const node& n, const node_sets& nodes) override final; + void add_node(const Index& index, const node_sets& nodes) override final; + + void delete_node(const node& item) override final; + void delete_node(const Index& index) override final; private: void add_node_helper(const Index& index, const node_sets& nodes); };