Skip to content
Snippets Groups Projects
Commit 7060ca83 authored by torin's avatar torin
Browse files

cache local solution

parent 4173383c
No related branches found
No related tags found
No related merge requests found
[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
......
......@@ -4,164 +4,125 @@
#include <vector>
template<class T>
class node
class graph
{
private:
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{};
int ID;
public:
node()=delete;
node(T&& t):t(std::move(t)),ID(_id++){}
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::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()
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);
graph(){}
for(auto it= std::begin(_bad_childs);it!=std::end(_bad_childs);it++)
if((*it).lock()->id() == t.id())
_bad_childs.erase(it);
}
node& add_node(T &&t) { nodes.push_back(node(std::move(t))); }
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(t));}
node& operator[](int i) { return nodes[i]; }
void flow_before(std::function<bool(node<T>&)> funct)
void connect(node& n, node& o)
{
for(auto& child : _childs)
child->flow_before(funct);
if (funct(*this))
bool foundn=false;
bool foundo=false;
for(auto& i : nodes)
{
if(i.id()==n.id())
foundn=true;
if(i.id()==o.id())
foundo=true;
}
if(!foundn || !foundo)
return;
connections.push_back(connection{&n,&o});
}
node<T>& add_child(node<T>* w)
void erase(node& n)
{
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)
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))
{
_childs.push_back(std::make_shared<node<T>>(*w));
connections.erase(i);
i=std::begin(connections);
i--;
}
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;
}
connections.erase(i);
node<T>& add_child(node<T>& w)
for(auto i =std::begin(nodes);i!=std::end(nodes);i++)
if(i->id()==n.id())
if(i==std::begin(nodes))
{
return add_child(&w);
nodes.erase(i);
i=std::begin(nodes);
i--;
}
node<T>& add_child(const T& t)
else
{
_childs.push_back(std::make_shared<node<T>>(node<T>{t}));
return *this;
nodes.erase(i);
}
};
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)
std::vector<node*> get_childs(node& n)
{
std::vector<node*> ret{};
for(auto i : connections)
{
erase_from_graph(root,*t);
if(n.id()==i.A->id())
ret.push_back(i.B);
if(n.id()==i.B->id())
ret.push_back(i.A);
}
return ret;
}
};
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;
}
template<class 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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment