From 1597247dda4351d02d4467b9e3ab8a5dfd0406ea Mon Sep 17 00:00:00 2001 From: torin <torin@git.sch.bme.hu> Date: Mon, 25 May 2015 23:36:04 +0200 Subject: [PATCH] donw cache local --- main.cpp | 69 ++++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 52 insertions(+), 17 deletions(-) diff --git a/main.cpp b/main.cpp index 7285624..e3d1e4b 100644 --- a/main.cpp +++ b/main.cpp @@ -6,7 +6,7 @@ template<class T> class graph { -private: +public: class node { @@ -32,16 +32,18 @@ private: }; +private: class connection { public: - node * A; - node * B; - connection(node* a, node *b):A(a),B(b){} + int A; + int B; + connection(int a, int b):A(a),B(b){} }; std::vector<node> nodes{}; std::vector<connection> connections{}; + std::vector<connection> weak_connections{}; public: @@ -68,20 +70,20 @@ public: for(auto& i : nodes) { if(i.id()==n.id()) - foundn=true; + foundn=true; if(i.id()==o.id()) - foundo=true; + foundo=true; } if(!foundn || !foundo) return; - connections.push_back(connection{&n,&o}); + connections.push_back(connection{n.id(),o.id()}); } void erase(node& n) { for(auto&& i =std::begin(connections);i!=std::end(connections);i++) - if( i->A->id() == n.id() || i->B->id() == n.id() ) + if( i->A == n.id() || i->B == n.id() ) if(i==std::begin(connections)) { connections.erase(i); @@ -115,14 +117,48 @@ public: std::vector<node*> ret{}; for(auto& i : connections) { - if(n.id()==i.A->id()) - ret.push_back(i.B); - if(n.id()==i.B->id()) - ret.push_back(i.A); + if(n.id()==i.A) + for(auto& re:nodes) + if(re.id()==i.A) + ret.push_back(&re); } return ret; } + void run(std::function<void(T&)> funct) + { + for(auto& i:nodes) + funct(i.data()); + } + + void flow_after(node& node, std::function<void(graph<T>::node&)> funct) + { + funct(node); + for(auto& i : connections) + { + if(node.id()==i.A) + { + for(auto& j : nodes) + if(j.id()==i.B) + flow_after(j,funct); + } + } + } + + void flow_before(node& node, std::function<void(graph<T>::node&)> funct) + { + for(auto& i : connections) + { + if(node.id()==i.A) + { + for(auto& j : nodes) + if(j.id()==i.B) + flow_before(j,funct); + } + } + funct(node); + } + }; template<class T> @@ -135,10 +171,9 @@ int main() auto& t2=g.add_node(6); std::cout<<"ha"<<std::endl; auto& t = g.add_node(52); - g.connect(t,g[0]); - g.connect(t,t2); - std::cout<<std::endl<<g.get_childs(t).size(); - g.erase(t2); - std::cout<<std::endl<<g.get_childs(t).size(); + g.connect(g[0],t); + g.connect(g[0],t2); + std::cout<<std::endl<<g.get_childs(t).size()<<std::endl; + g.flow_before(g[0],[](graph<int>::node& a){std::cout<<a.data()<<":"<<a.id()<<std::endl;}); return 0; } -- GitLab