Skip to content
Snippets Groups Projects
Commit 4173383c authored by torin's avatar torin
Browse files

gráf, első megoldás

parent 3d038bf2
No related branches found
No related tags found
No related merge requests found
...@@ -33,7 +33,7 @@ EnvironmentGroup= ...@@ -33,7 +33,7 @@ EnvironmentGroup=
Executable=file:///home/junior/projects/szorg5/ Executable=file:///home/junior/projects/szorg5/
External Terminal=konsole --noclose --workdir %workdir -e %exe External Terminal=konsole --noclose --workdir %workdir -e %exe
Project Target=szorg5,szorg5 Project Target=szorg5,szorg5
Use External Terminal=false Use External Terminal=true
Working Directory= Working Directory=
isExecutable=false isExecutable=false
......
#include <iostream> #include <iostream>
#include <iterator>
#include <memory>
#include <vector>
template<class T>
class node
{
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)
{
for(auto& i: _childs)
if(i->id() == t.id())
return &i;
return nullptr;
}
std::vector<node<T>*> childs()
{
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;
}
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<T>*> safe_childs()
{
std::vector<node<T>*> ret{};
for(auto& it : _childs)
ret.push_back(&(*it));
return ret;
}
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);
}
void flow_after(std::function<bool(node<T>&)> funct)
{
if (funct(*this))
return;
for(auto& child : _childs)
child->flow_after(funct);
}
void flow_before(std::function<bool(node<T>&)> funct)
{
for(auto& child : _childs)
child->flow_before(funct);
if (funct(*this))
return;
}
node<T>& add_child(node<T>* w)
{
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
{
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});
}
return *this;
}
node<T>& add_child(node<T>& w)
{
return add_child(&w);
}
node<T>& add_child(const T& t)
{
_childs.push_back(std::make_shared<node<T>>(node<T>{t}));
return *this;
}
};
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 main() int main()
{ {
std::cout<<"bibi"; int t;
std::cout<<"creating after"<<std::endl;
std::cin>>t;
test();
std::cout<<"deleted"<<std::endl;
std::cin>>t;
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