From 17a1b1a73d8cbd6119d0e17d1d96e2ab1d9ada4c Mon Sep 17 00:00:00 2001 From: torin <torin@git.sch.bme.hu> Date: Mon, 25 May 2015 22:49:33 +0200 Subject: [PATCH] all bugs removed --- main.cpp | 111 +++++++++++++++++++++++++++++-------------------------- 1 file changed, 58 insertions(+), 53 deletions(-) diff --git a/main.cpp b/main.cpp index 9a473bd..7285624 100644 --- a/main.cpp +++ b/main.cpp @@ -7,31 +7,31 @@ template<class T> class graph { private: - + class node { private: T t; static int _id; int ID; - + public: node(T&& t):t(std::move(t)),ID(_id++){} - + node(const T& t):t(t),ID(_id++){} - + node(const node&)=delete; node(node&&)=default; - + node& operator=(const node&)=delete; node& operator=(node&&) =default; - + T& data() { return t; } - + int id() { return ID; } - + }; - + class connection { public: @@ -39,72 +39,77 @@ private: node * B; connection(node* a, node *b):A(a),B(b){} }; - + std::vector<node> nodes{}; std::vector<connection> connections{}; - + public: - + graph(){} - - node& add_node(T &&t) { + + node& add_node(T &&t) + { nodes.push_back(node(std::move(t))); - } - - node& add_node(T& t) {nodes.push_back(node(t));} - + return nodes[nodes.size()-1]; + } + + node& add_node(T& t) + { + nodes.push_back(node(t)); + return nodes[nodes.size()-1]; + } + node& operator[](int i) { return nodes[i]; } - + void connect(node& n, node& o) { bool foundn=false; bool foundo=false; - std::cout<<std::endl<<n.id()<<std::endl; for(auto& i : nodes) { - std::cout<<std::endl<<i.id()<<std::endl; if(i.id()==n.id()) foundn=true; if(i.id()==o.id()) foundo=true; } if(!foundn || !foundo) - { - std::cout<<foundn<<":"<<foundo; return; - } - + connections.push_back(connection{&n,&o}); } - + 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==std::begin(connections)) - { - connections.erase(i); - i=std::begin(connections); - i--; - } - else - connections.erase(i); - + if(i==std::begin(connections)) + { + connections.erase(i); + i=std::begin(connections); + i--; + } + else + { + connections.erase(i); + i--; + } + for(auto&& i =std::begin(nodes);i!=std::end(nodes);i++) if(i->id()==n.id()) - if(i==std::begin(nodes)) - { - nodes.erase(i); - i=std::begin(nodes); - i--; - } - else - { - nodes.erase(i); - } + if(i==std::begin(nodes)) + { + nodes.erase(i); + i=std::begin(nodes); + i--; + } + else + { + nodes.erase(i); + i--; + } } - + std::vector<node*> get_childs(node& n) { std::vector<node*> ret{}; @@ -114,10 +119,10 @@ public: ret.push_back(i.B); if(n.id()==i.B->id()) ret.push_back(i.A); - } + } return ret; } - + }; template<class T> @@ -128,12 +133,12 @@ int main() graph<int> g{}; g.add_node(6); auto& t2=g.add_node(6); - std::cout<<g[0].data(); + std::cout<<"ha"<<std::endl; auto& t = g.add_node(52); - std::cout<<std::endl<<t.id()<<std::endl; g.connect(t,g[0]); - //g.connect(t,t2); + 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.erase(t); return 0; -} \ No newline at end of file +} -- GitLab