diff --git a/.kdev4/szorg5.kdev4 b/.kdev4/szorg5.kdev4 index ee36f572935d9e48711dee80b233df7f9166629c..6f76ef7872272acdb19d7816b96ae1d03b307dfc 100644 --- a/.kdev4/szorg5.kdev4 +++ b/.kdev4/szorg5.kdev4 @@ -1,15 +1,18 @@ +[Buildset] +BuildItems=@Variant(\x00\x00\x00\t\x00\x00\x00\x00\x01\x00\x00\x00\x0b\x00\x00\x00\x00\x01\x00\x00\x00\x0c\x00s\x00z\x00o\x00r\x00g\x005) + [CMake] Build Directory Count=1 Current Build Directory Index=0 ProjectRootRelative=./ [CMake][CMake Build Directory 0] -Build Directory Path=file:///home/junior/projects/szorg5/build +Build Directory Path=file:///home/thorn/projects/szorg5/build Build Type=Debug CMake Binary=file:///usr/bin/cmake Environment Profile= Extra Arguments= -Install Directory= +Install Directory=file:///usr/local [Defines And Includes][Compiler] Name=GCC @@ -30,10 +33,10 @@ Arguments= Dependencies=@Variant(\x00\x00\x00\t\x00\x00\x00\x00\x01\x00\x00\x00\x0b\x00\x00\x00\x00\x02\x00\x00\x00\x0c\x00s\x00z\x00o\x00r\x00g\x005\x00\x00\x00\x0c\x00s\x00z\x00o\x00r\x00g\x005) Dependency Action=Build EnvironmentGroup= -Executable=file:///home/junior/projects/szorg5/ +Executable=file:///home/thorn/projects/szorg5/ External Terminal=konsole --noclose --workdir %workdir -e %exe Project Target=szorg5,szorg5 -Use External Terminal=true +Use External Terminal=false Working Directory= isExecutable=false diff --git a/main.cpp b/main.cpp index 408e22c90d9afaae218bbafe4e6351060cea00d9..dff0e530d9b6cb7074a25bfa1218e7d9482c5732 100644 --- a/main.cpp +++ b/main.cpp @@ -4,164 +4,125 @@ #include <vector> template<class T> -class node +class graph { private: - T t; - const int ID; - static int _id; - std::vector<std::shared_ptr<node<T>>> _childs{}; - std::vector<std::weak_ptr<node<T>>> _bad_childs{}; - -public: - - node()=delete; - - node(T&& t):t(std::move(t)),ID(_id++){} - - node( const T& t):t(t),ID(_id++){} - - T& data() { return t; } - - int id() { return ID; } - - std::shared_ptr<node<T>>* get_original(node<T>& t) + class node { - for(auto& i: _childs) - if(i->id() == t.id()) - return &i; - return nullptr; - } + private: + T t; + static int _id; + int ID; + + public: + node(T&& t):t(t),ID(_id++){} + + node(const T& t):t(t),ID(_id++){} + + node(node&)=delete; + node(node&&)=default; + + T& data() { return t; } + + int id() { return ID; } + + }; - std::vector<node<T>*> childs() + class connection { - std::vector<node<T>*> ret{}; - for(auto& it : _childs) - ret.push_back(&(*it)); - for(auto& it : _bad_childs) - ret.push_back(&(*(it.lock()))); - return ret; - } + public: + connection(node* a, node *b):A(a),B(b){} + node * A; + node * B; + }; - std::vector<node<T>*> inf_round_childs() - { - std::vector<node<T>*> ret{}; - for(auto& it : _bad_childs) - ret.push_back(&(*(it.lock()))); - return ret; - } + std::vector<node> nodes{}; + std::vector<connection> connections{}; - std::vector<node<T>*> safe_childs() - { - std::vector<node<T>*> ret{}; - for(auto& it : _childs) - ret.push_back(&(*it)); - return ret; - } +public: - void erase_child(node<T>& t) - { - for(auto it= std::begin(_childs);it!=std::end(_childs);it++) - if((*it)->id() == t.id()) - _childs.erase(it); - - for(auto it= std::begin(_bad_childs);it!=std::end(_bad_childs);it++) - if((*it).lock()->id() == t.id()) - _bad_childs.erase(it); - } + graph(){} - void flow_after(std::function<bool(node<T>&)> funct) - { - if (funct(*this)) - return; - for(auto& child : _childs) - child->flow_after(funct); - } + node& add_node(T &&t) { nodes.push_back(node(std::move(t))); } - void flow_before(std::function<bool(node<T>&)> funct) - { - for(auto& child : _childs) - child->flow_before(funct); - if (funct(*this)) - return; - } + node& add_node(T& t) {nodes.push_back(node(t));} - node<T>& add_child(node<T>* w) + node& operator[](int i) { return nodes[i]; } + + void connect(node& n, node& o) { - if(w==this) - return *this; - - bool found=false; - auto& me=*this; - w->flow_after([&found,&me](node<T>& t){if(&t==&me) found=true; return found;}); - if (!found) - { - _childs.push_back(std::make_shared<node<T>>(*w)); - } - else + bool foundn=false; + bool foundo=false; + for(auto& i : nodes) { - std::shared_ptr<node<T>>* fo=nullptr; - w->flow_after([&me,&fo](node<T>& t){ fo=t.get_original(me); if(fo!=nullptr) return true; }); - _bad_childs.push_back(std::weak_ptr<node<T>>{*fo}); + if(i.id()==n.id()) + foundn=true; + if(i.id()==o.id()) + foundo=true; } - return *this; + if(!foundn || !foundo) + return; + + connections.push_back(connection{&n,&o}); } - node<T>& add_child(node<T>& w) + void erase(node& n) { - return add_child(&w); + 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); + + 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); + } + } - node<T>& add_child(const T& t) + std::vector<node*> get_childs(node& n) { - _childs.push_back(std::make_shared<node<T>>(node<T>{t})); - return *this; + 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); + } + return ret; } }; template<class T> -int node<T>::_id=0; - -template<class T> -void erase_from_graph(node<T>& root , node<T>& to_del) -{ - root.flow_after([&to_del](node<T>& t){ int i= t.childs().size(); t.erase_child(to_del); if(i>t.childs().size()) return true; return false; }); -} - -template<class T> -void erase_from_graph(node<T>& root , node<T>* t) -{ - erase_from_graph(root,*t); -} - - -void test() -{ - int t; - std::vector<node<int>> a; - for(int i=0;i<1000000;i++) - a.push_back(node<int>{5}); - a[0].add_child(int{6}); - a[0].childs()[0]->add_child(int{7}); - a[0].childs()[0]->add_child(int{8}); - auto to_del = a[0].childs()[0]->childs()[0]; - a[0].childs()[0]->add_child(a[0]); - a[0].flow_after([]( node<int>& elem ){ std::cout<<"inf rounds from here:"<<elem.inf_round_childs().size()<<" .id:"; std::cout<< elem.id() << ":" << elem.data() <<std::endl; return false; }); - erase_from_graph(a[0],to_del); - a[0].flow_after([]( node<int>& elem ){ std::cout<<"inf rounds from here:"<<elem.inf_round_childs().size()<<" .id:"; std::cout<< elem.id() << ":" << elem.data() <<std::endl; return false; }); - std::cout<<"created"<<std::endl; - std::cin>>t; -} +int graph<T>::node::_id=0; int main() { - int t; - std::cout<<"creating after"<<std::endl; - std::cin>>t; - test(); - std::cout<<"deleted"<<std::endl; - std::cin>>t; + graph<int> g{}; + g.add_node(6); + auto& t2=g.add_node(6); + std::cout<<g[0].data(); + auto& t = g.add_node(52); + g.connect(t,g[0]); + //g.connect(t,t2); + std::cout<<g.get_childs(t).size(); + g.erase(t); return 0; } \ No newline at end of file