nodecursor.hpp /usr/include/gecode/gist/nodecursor.hh Gecode Gecode::Gist /*-*-mode:C++;c-basic-offset:2;indent-tabs-mode:nil-*-*/ /* *Mainauthors: *GuidoTack<tack@gecode.org> * *Copyright: *GuidoTack,2006 * *Lastmodified: *$Date:2013-05-0609:02:17+0200(Mon,06May2013)$by$Author:tack$ *$Revision:13613$ * *ThisfileispartofGecode,thegenericconstraint *developmentenvironment: *http://www.gecode.org * *Permissionisherebygranted,freeofcharge,toanypersonobtaining *acopyofthissoftwareandassociateddocumentationfiles(the *"Software"),todealintheSoftwarewithoutrestriction,including *withoutlimitationtherightstouse,copy,modify,merge,publish, *distribute,sublicense,and/orsellcopiesoftheSoftware,andto *permitpersonstowhomtheSoftwareisfurnishedtodoso,subjectto *thefollowingconditions: * *Theabovecopyrightnoticeandthispermissionnoticeshallbe *includedinallcopiesorsubstantialportionsoftheSoftware. * *THESOFTWAREISPROVIDED"ASIS",WITHOUTWARRANTYOFANYKIND, *EXPRESSORIMPLIED,INCLUDINGBUTNOTLIMITEDTOTHEWARRANTIESOF *MERCHANTABILITY,FITNESSFORAPARTICULARPURPOSEAND *NONINFRINGEMENT.INNOEVENTSHALLTHEAUTHORSORCOPYRIGHTHOLDERSBE *LIABLEFORANYCLAIM,DAMAGESOROTHERLIABILITY,WHETHERINANACTION *OFCONTRACT,TORTOROTHERWISE,ARISINGFROM,OUTOFORINCONNECTION *WITHTHESOFTWAREORTHEUSEOROTHERDEALINGSINTHESOFTWARE. * */ namespaceGecode{namespaceGist{ template<classNode> forceinline NodeCursor<Node>::NodeCursor(Node*theNode, consttypenameNode::NodeAllocator&na0) :_startNode(theNode),_node(theNode), _alternative(theNode->getAlternative(na0)), na(na0){} template<classNode> forceinlineNode* NodeCursor<Node>::node(void){return_node;} template<classNode> forceinlineunsignedint NodeCursor<Node>::alternative(void){return_alternative;} template<classNode> forceinlinevoid NodeCursor<Node>::alternative(unsignedinta){_alternative=a;} template<classNode> forceinlineNode* NodeCursor<Node>::startNode(void){return_startNode;} template<classNode> forceinlinevoid NodeCursor<Node>::node(Node*n){_node=n;} template<classNode> forceinlinebool NodeCursor<Node>::mayMoveUpwards(void){ return_node!=_startNode&&!_node->isRoot(); } template<classNode> forceinlinevoid NodeCursor<Node>::moveUpwards(void){ _node=static_cast<Node*>(_node->getParent(na)); if(_node->isRoot()){ _alternative=0; }else{ Node*p=static_cast<Node*>(_node->getParent(na)); for(inti=p->getNumberOfChildren();i--;){ if(p->getChild(na,i)==_node){ _alternative=i; break; } } } } template<classNode> forceinlinebool NodeCursor<Node>::mayMoveDownwards(void){ return_node->getNumberOfChildren()>0; } template<classNode> forceinlinevoid NodeCursor<Node>::moveDownwards(void){ _alternative=0; _node=_node->getChild(na,0); } template<classNode> forceinlinebool NodeCursor<Node>::mayMoveSidewards(void){ return(!_node->isRoot())&&(_node!=_startNode)&& (_alternative<_node->getParent(na)->getNumberOfChildren()-1); } template<classNode> forceinlinevoid NodeCursor<Node>::moveSidewards(void){ _node= static_cast<Node*>(_node->getParent(na)->getChild(na,++_alternative)); } forceinlinebool HideFailedCursor::mayMoveDownwards(void){ VisualNode*n=node(); return(!onlyDirty||n->isDirty())&& NodeCursor<VisualNode>::mayMoveDownwards()&& (n->hasSolvedChildren()||n->getNoOfOpenChildren(na)>0)&& (!n->isHidden()); } forceinline HideFailedCursor::HideFailedCursor(VisualNode*root, constVisualNode::NodeAllocator&na, boolonlyDirtyNodes) :NodeCursor<VisualNode>(root,na),onlyDirty(onlyDirtyNodes){} forceinlinevoid HideFailedCursor::processCurrentNode(void){ VisualNode*n=node(); if(n->getStatus()==BRANCH&& !n->hasSolvedChildren()&& n->getNoOfOpenChildren(na)==0){ n->setHidden(true); n->setChildrenLayoutDone(false); n->dirtyUp(na); } } forceinline UnhideAllCursor::UnhideAllCursor(VisualNode*root, constVisualNode::NodeAllocator&na) :NodeCursor<VisualNode>(root,na){} forceinlinevoid UnhideAllCursor::processCurrentNode(void){ VisualNode*n=node(); if(n->isHidden()){ n->setHidden(false); n->dirtyUp(na); } } forceinline UnstopAllCursor::UnstopAllCursor(VisualNode*root, constVisualNode::NodeAllocator&na) :NodeCursor<VisualNode>(root,na){} forceinlinevoid UnstopAllCursor::processCurrentNode(void){ VisualNode*n=node(); if(n->getStatus()==STOP){ n->setStop(false); n->dirtyUp(na); } } forceinline NextSolCursor::NextSolCursor(VisualNode*theNode,boolbackwards, constVisualNode::NodeAllocator&na) :NodeCursor<VisualNode>(theNode,na),back(backwards){} forceinlinevoid NextSolCursor::processCurrentNode(void){} forceinlinebool NextSolCursor::notOnSol(void){ returnnode()==startNode()||node()->getStatus()!=SOLVED; } forceinlinebool NextSolCursor::mayMoveUpwards(void){ returnnotOnSol()&&!node()->isRoot(); } forceinlinebool NextSolCursor::mayMoveDownwards(void){ returnnotOnSol()&&!(back&&node()==startNode()) &&node()->hasSolvedChildren() &&NodeCursor<VisualNode>::mayMoveDownwards(); } forceinlinevoid NextSolCursor::moveDownwards(void){ NodeCursor<VisualNode>::moveDownwards(); if(back){ while(NodeCursor<VisualNode>::mayMoveSidewards()) NodeCursor<VisualNode>::moveSidewards(); } } forceinlinebool NextSolCursor::mayMoveSidewards(void){ if(back){ returnnotOnSol()&&!node()->isRoot()&&alternative()>0; }else{ returnnotOnSol()&&!node()->isRoot()&& (alternative()< node()->getParent(na)->getNumberOfChildren()-1); } } forceinlinevoid NextSolCursor::moveSidewards(void){ if(back){ alternative(alternative()-1); node(node()->getParent(na)->getChild(na,alternative())); }else{ NodeCursor<VisualNode>::moveSidewards(); } } forceinline StatCursor::StatCursor(VisualNode*root, constVisualNode::NodeAllocator&na) :NodeCursor<VisualNode>(root,na), curDepth(0),depth(0),failed(0),solved(0),choice(0),open(0){} forceinlinevoid StatCursor::processCurrentNode(void){ VisualNode*n=node(); switch(n->getStatus()){ caseSOLVED:solved++;break; caseFAILED:failed++;break; caseBRANCH:choice++;break; caseUNDETERMINED:open++;break; default:break; } } forceinlinevoid StatCursor::moveDownwards(void){ curDepth++; depth=std::max(depth,curDepth); NodeCursor<VisualNode>::moveDownwards(); } forceinlinevoid StatCursor::moveUpwards(void){ curDepth--; NodeCursor<VisualNode>::moveUpwards(); } forceinline BranchLabelCursor::BranchLabelCursor(VisualNode*root,BestNode*curBest, intc_d,inta_d,boolclear, VisualNode::NodeAllocator&na) :NodeCursor<VisualNode>(root,na),_na(na),_curBest(curBest), _c_d(c_d),_a_d(a_d),_clear(clear){} forceinlinevoid BranchLabelCursor::processCurrentNode(void){ VisualNode*n=node(); if(!_clear){ if(!na.hasLabel(n)){ VisualNode*p=n->getParent(_na); if(p){ std::stringl= n->getBranchLabel(_na,p,p->getChoice(), _curBest,_c_d,_a_d,alternative()); _na.setLabel(n,QString(l.c_str())); if(n->getNumberOfChildren()<1&& alternative()==p->getNumberOfChildren()-1) p->purge(_na); }else{ _na.setLabel(n,""); } } }else{ _na.clearLabel(n); } n->dirtyUp(na); } forceinline DisposeCursor::DisposeCursor(VisualNode*root, constVisualNode::NodeAllocator&na) :NodeCursor<VisualNode>(root,na){} forceinlinevoid DisposeCursor::processCurrentNode(void){ node()->dispose(); } }} //STATISTICS:gist-any