Skip to content
Snippets Groups Projects
Commit 0ead5ea7 authored by Tüske's avatar Tüske
Browse files

id err

parent 7060ca83
No related branches found
No related tags found
No related merge requests found
...@@ -16,13 +16,16 @@ private: ...@@ -16,13 +16,16 @@ private:
int ID; int ID;
public: public:
node(T&& t):t(t),ID(_id++){} node(T&& t):t(std::move(t)),ID(_id++){}
node(const T& t):t(t),ID(_id++){} node(const T& t):t(t),ID(_id++){}
node(node&)=delete; node(const node&)=delete;
node(node&&)=default; node(node&&)=default;
node& operator=(const node&)=delete;
node& operator=(node&&) =default;
T& data() { return t; } T& data() { return t; }
int id() { return ID; } int id() { return ID; }
...@@ -32,9 +35,9 @@ private: ...@@ -32,9 +35,9 @@ private:
class connection class connection
{ {
public: public:
connection(node* a, node *b):A(a),B(b){}
node * A; node * A;
node * B; node * B;
connection(node* a, node *b):A(a),B(b){}
}; };
std::vector<node> nodes{}; std::vector<node> nodes{};
...@@ -44,7 +47,9 @@ public: ...@@ -44,7 +47,9 @@ public:
graph(){} graph(){}
node& add_node(T &&t) { nodes.push_back(node(std::move(t))); } node& add_node(T &&t) {
nodes.push_back(node(std::move(t)));
}
node& add_node(T& t) {nodes.push_back(node(t));} node& add_node(T& t) {nodes.push_back(node(t));}
...@@ -54,22 +59,27 @@ public: ...@@ -54,22 +59,27 @@ public:
{ {
bool foundn=false; bool foundn=false;
bool foundo=false; bool foundo=false;
std::cout<<std::endl<<n.id()<<std::endl;
for(auto& i : nodes) for(auto& i : nodes)
{ {
std::cout<<std::endl<<i.id()<<std::endl;
if(i.id()==n.id()) if(i.id()==n.id())
foundn=true; foundn=true;
if(i.id()==o.id()) if(i.id()==o.id())
foundo=true; foundo=true;
} }
if(!foundn || !foundo) if(!foundn || !foundo)
{
std::cout<<foundn<<":"<<foundo;
return; return;
}
connections.push_back(connection{&n,&o}); connections.push_back(connection{&n,&o});
} }
void erase(node& n) void erase(node& n)
{ {
for(auto i =std::begin(connections);i!=std::end(connections);i++) 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->id() == n.id() || i->B->id() == n.id() )
if(i==std::begin(connections)) if(i==std::begin(connections))
{ {
...@@ -80,7 +90,7 @@ public: ...@@ -80,7 +90,7 @@ public:
else else
connections.erase(i); connections.erase(i);
for(auto i =std::begin(nodes);i!=std::end(nodes);i++) for(auto&& i =std::begin(nodes);i!=std::end(nodes);i++)
if(i->id()==n.id()) if(i->id()==n.id())
if(i==std::begin(nodes)) if(i==std::begin(nodes))
{ {
...@@ -98,7 +108,7 @@ public: ...@@ -98,7 +108,7 @@ public:
std::vector<node*> get_childs(node& n) std::vector<node*> get_childs(node& n)
{ {
std::vector<node*> ret{}; std::vector<node*> ret{};
for(auto i : connections) for(auto& i : connections)
{ {
if(n.id()==i.A->id()) if(n.id()==i.A->id())
ret.push_back(i.B); ret.push_back(i.B);
...@@ -120,9 +130,10 @@ int main() ...@@ -120,9 +130,10 @@ int main()
auto& t2=g.add_node(6); auto& t2=g.add_node(6);
std::cout<<g[0].data(); std::cout<<g[0].data();
auto& t = g.add_node(52); auto& t = g.add_node(52);
std::cout<<std::endl<<t.id()<<std::endl;
g.connect(t,g[0]); g.connect(t,g[0]);
//g.connect(t,t2); //g.connect(t,t2);
std::cout<<g.get_childs(t).size(); std::cout<<std::endl<<g.get_childs(t).size();
g.erase(t); g.erase(t);
return 0; return 0;
} }
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment