diff --git a/src/deptree.cpp b/src/deptree.cpp index 6f0de72..bd8b8a2 100644 --- a/src/deptree.cpp +++ b/src/deptree.cpp @@ -1,7 +1,7 @@ #include #include "deptree.h" -int DepTree::CreateNodeFromVar(const std::string& var, UsedType& used, CallStack& callstack) +int DepTree::CreateNodeFromVar(const std::string& var, DepTreeVars& vars, CallStack& callstack) { COUT(DEBUG)<<"DepTree::CreateNodeFromVar "<UsedIdents(ids); type=DepTree::VAR; name=var; - DepTree* d; + DepTreeVars::const_iterator d; int ret; callstack.insert(this); for(auto& i:ids) { - d=FindNodeByVar(i); - if(d==0) + d=vars.find(i); + if(d==vars.end()) { auto n=*childrens.insert(new DepTree).first; n->parents.insert(this); - ret=n->CreateNodeFromVar(i,used,callstack); + ret=n->CreateNodeFromVar(i,vars,callstack); if(ret!=0) { COUT(ERROR)<<" in definition of variable "<second)!=callstack.end()) { COUT(ERROR)<<"Circular dependency of variable "<parents.insert(this); + childrens.insert(d->second); + d->second->parents.insert(this); } } callstack.erase(this); @@ -51,7 +51,7 @@ int DepTree::CreateNodeFromVar(const std::string& var, UsedType& used, CallStack } -int DepTree::CreateNodeFromSP(DepTree::NodeType list, G_toType::size_type ind, UsedType& used) +int DepTree::CreateNodeFromSP(DepTree::NodeType list, G_toType::size_type ind, DepTreeVars& vars) { if(list!=DepTree::SAVE && list!=DepTree::PRINT) { @@ -68,16 +68,16 @@ int DepTree::CreateNodeFromSP(DepTree::NodeType list, G_toType::size_type ind, U if(type==DepTree::SAVE) G_tosave[index]->UsedIdents(ids); else if(type==DepTree::PRINT) G_toprint[index]->UsedIdents(ids); - DepTree* d; + DepTreeVars::const_iterator d; int ret; for(auto& i:ids) { - d=FindNodeByVar(i); - if(d==0) + d=vars.find(i); + if(d==vars.end()) { auto n=*childrens.insert(new DepTree).first; n->parents.insert(this); - ret=n->CreateNodeFromVar(i,used,callstack); + ret=n->CreateNodeFromVar(i,vars,callstack); if(ret!=0) { COUT(ERROR)<<" in "<<((type==DepTree::SAVE)?"save":"print")<<" directive "<<((type==DepTree::SAVE)?G_tosave:G_toprint)[index]->Dump()<<"."<parents.insert(this); + childrens.insert(d->second); + d->second->parents.insert(this); } } @@ -103,48 +103,30 @@ int DepTree::CreateGlobalTree(UsedType& used) return 2; } + DepTreeVars vars; + type=DepTree::ROOT; for(G_toType::size_type i=0; iparents.insert(this); - auto ret=n->CreateNodeFromSP(DepTree::SAVE,i,used); + auto ret=n->CreateNodeFromSP(DepTree::SAVE,i,vars); if(ret!=0) return ret; } for(G_toType::size_type i=0; iparents.insert(this); - auto ret=n->CreateNodeFromSP(DepTree::PRINT,i,used); + auto ret=n->CreateNodeFromSP(DepTree::PRINT,i,vars); if(ret!=0) return ret; } - return 0; -} - - -DepTree* DepTree::FindNodeByVarFromCurrent(const std::string& var) const -{ - if(type==DepTree::VAR && name==var) return const_cast(this); + for(auto& i: vars) used.insert(i.first); - DepTree* d; - for(auto& i:childrens) - { - d=i->FindNodeByVarFromCurrent(var); - if(d!=0) return d; - } return 0; } -DepTree* DepTree::FindNodeByVar(const std::string& var) const -{ - const DepTree* curnode=this; - while(curnode->parents.size()!=0) curnode=*(curnode->parents.begin()); - return curnode->FindNodeByVarFromCurrent(var); -} - - DepTree::LeafVector DepTree::FindLeafNodes() const { LeafVector leafs; diff --git a/src/deptree.h b/src/deptree.h index a74e529..2928c1e 100644 --- a/src/deptree.h +++ b/src/deptree.h @@ -10,6 +10,7 @@ class DepTree { typedef std::vector LeafVector; + typedef std::map DepTreeVars; enum NodeType {NOTDEF,ROOT,SAVE,PRINT,VAR}; typedef std::set NodeVector; @@ -21,10 +22,8 @@ class DepTree std::string name; mutable bool visited; - int CreateNodeFromVar(const std::string& var, UsedType& used, CallStack& callstack); - int CreateNodeFromSP(NodeType list, G_toType::size_type ind, UsedType& used); - DepTree* FindNodeByVarFromCurrent(const std::string& var) const; - DepTree* FindNodeByVar(const std::string& var) const; + int CreateNodeFromVar(const std::string& var, DepTreeVars& vars, CallStack& callstack); + int CreateNodeFromSP(NodeType list, G_toType::size_type ind, DepTreeVars& vars); LeafVector FindLeafNodes() const; public: DepTree():type(DepTree::NOTDEF),visited(false) {}