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] [CMake]
Build Directory Count=1 Build Directory Count=1
Current Build Directory Index=0 Current Build Directory Index=0
ProjectRootRelative=./ ProjectRootRelative=./
[CMake][CMake Build Directory 0] [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 Build Type=Debug
CMake Binary=file:///usr/bin/cmake CMake Binary=file:///usr/bin/cmake
Environment Profile= Environment Profile=
Extra Arguments= Extra Arguments=
Install Directory= Install Directory=file:///usr/local
[Defines And Includes][Compiler] [Defines And Includes][Compiler]
Name=GCC Name=GCC
...@@ -30,10 +33,10 @@ Arguments= ...@@ -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) 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 Dependency Action=Build
EnvironmentGroup= EnvironmentGroup=
Executable=file:///home/junior/projects/szorg5/ Executable=file:///home/thorn/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=true Use External Terminal=false
Working Directory= Working Directory=
isExecutable=false isExecutable=false
......
...@@ -4,164 +4,125 @@ ...@@ -4,164 +4,125 @@
#include <vector> #include <vector>
template<class T> template<class T>
class node class graph
{ {
private: private:
class node
{
private:
T t; T t;
const int ID;
static int _id; static int _id;
std::vector<std::shared_ptr<node<T>>> _childs{}; int ID;
std::vector<std::weak_ptr<node<T>>> _bad_childs{};
public: public:
node(T&& t):t(t),ID(_id++){}
node()=delete;
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(node&&)=default;
T& data() { return t; } T& data() { return t; }
int id() { return ID; } 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{}; public:
for(auto& it : _childs) connection(node* a, node *b):A(a),B(b){}
ret.push_back(&(*it)); node * A;
for(auto& it : _bad_childs) node * B;
ret.push_back(&(*(it.lock()))); };
return ret;
}
std::vector<node<T>*> inf_round_childs() std::vector<node> nodes{};
{ std::vector<connection> connections{};
std::vector<node<T>*> ret{};
for(auto& it : _bad_childs)
ret.push_back(&(*(it.lock())));
return ret;
}
std::vector<node<T>*> safe_childs() public:
{
std::vector<node<T>*> ret{};
for(auto& it : _childs)
ret.push_back(&(*it));
return ret;
}
void erase_child(node<T>& t) graph(){}
{
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++) node& add_node(T &&t) { nodes.push_back(node(std::move(t))); }
if((*it).lock()->id() == t.id())
_bad_childs.erase(it);
}
void flow_after(std::function<bool(node<T>&)> funct) node& add_node(T& t) {nodes.push_back(node(t));}
{
if (funct(*this)) node& operator[](int i) { return nodes[i]; }
return;
for(auto& child : _childs)
child->flow_after(funct);
}
void flow_before(std::function<bool(node<T>&)> funct) void connect(node& n, node& o)
{ {
for(auto& child : _childs) bool foundn=false;
child->flow_before(funct); bool foundo=false;
if (funct(*this)) for(auto& i : nodes)
{
if(i.id()==n.id())
foundn=true;
if(i.id()==o.id())
foundo=true;
}
if(!foundn || !foundo)
return; return;
connections.push_back(connection{&n,&o});
} }
node<T>& add_child(node<T>* w) void erase(node& n)
{ {
if(w==this) for(auto i =std::begin(connections);i!=std::end(connections);i++)
return *this; if( i->A->id() == n.id() || i->B->id() == n.id() )
if(i==std::begin(connections))
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)); connections.erase(i);
i=std::begin(connections);
i--;
} }
else else
{ connections.erase(i);
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) 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--;
} }
else
node<T>& add_child(const T& t)
{ {
_childs.push_back(std::make_shared<node<T>>(node<T>{t})); nodes.erase(i);
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> std::vector<node*> get_childs(node& n)
void erase_from_graph(node<T>& root , node<T>* t) {
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() template<class T>
{ int graph<T>::node::_id=0;
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()
{ {
int t; graph<int> g{};
std::cout<<"creating after"<<std::endl; g.add_node(6);
std::cin>>t; auto& t2=g.add_node(6);
test(); std::cout<<g[0].data();
std::cout<<"deleted"<<std::endl; auto& t = g.add_node(52);
std::cin>>t; g.connect(t,g[0]);
//g.connect(t,t2);
std::cout<<g.get_childs(t).size();
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