/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */ /* * Main authors: * Guido Tack * * Copyright: * Guido Tack, 2006 * * Last modified: * $Date: 2010-08-12 17:48:30 +1000 (Thu, 12 Aug 2010) $ by $Author: tack $ * $Revision: 11345 $ * * This file is part of Gecode, the generic constraint * development environment: * http://www.gecode.org * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the * "Software"), to deal in the Software without restriction, including * without limitation the rights to use, copy, modify, merge, publish, * distribute, sublicense, and/or sell copies of the Software, and to * permit persons to whom the Software is furnished to do so, subject to * the following conditions: * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * */ namespace Gecode { namespace Gist { template forceinline NodeCursor::NodeCursor(Node* theNode, const typename Node::NodeAllocator& na0) : _startNode(theNode), _node(theNode), _alternative(theNode->getAlternative(na0)), na(na0) {} template forceinline Node* NodeCursor::node(void) { return _node; } template forceinline unsigned int NodeCursor::alternative(void) { return _alternative; } template forceinline void NodeCursor::alternative(unsigned int a) { _alternative=a; } template forceinline Node* NodeCursor::startNode(void) { return _startNode; } template forceinline void NodeCursor::node(Node* n) { _node = n; } template forceinline bool NodeCursor::mayMoveUpwards(void) { return _node != _startNode && !_node->isRoot(); } template forceinline void NodeCursor::moveUpwards(void) { _node = static_cast(_node->getParent(na)); if (_node->isRoot()) { _alternative = 0; } else { Node* p = static_cast(_node->getParent(na)); for (int i=p->getNumberOfChildren(); i--;) { if (p->getChild(na,i) == _node) { _alternative = i; break; } } } } template forceinline bool NodeCursor::mayMoveDownwards(void) { return _node->getNumberOfChildren() > 0; } template forceinline void NodeCursor::moveDownwards(void) { _alternative = 0; _node = _node->getChild(na,0); } template forceinline bool NodeCursor::mayMoveSidewards(void) { return (!_node->isRoot()) && (_node != _startNode) && (_alternative < _node->getParent(na)->getNumberOfChildren() - 1); } template forceinline void NodeCursor::moveSidewards(void) { _node = static_cast(_node->getParent(na)->getChild(na,++_alternative)); } forceinline bool HideFailedCursor::mayMoveDownwards(void) { VisualNode* n = node(); return (!onlyDirty || n->isDirty()) && NodeCursor::mayMoveDownwards() && (n->hasSolvedChildren() || n->getNoOfOpenChildren(na) > 0) && (! n->isHidden()); } forceinline HideFailedCursor::HideFailedCursor(VisualNode* root, const VisualNode::NodeAllocator& na, bool onlyDirtyNodes) : NodeCursor(root,na), onlyDirty(onlyDirtyNodes) {} forceinline void 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, const VisualNode::NodeAllocator& na) : NodeCursor(root,na) {} forceinline void UnhideAllCursor::processCurrentNode(void) { VisualNode* n = node(); if (n->isHidden()) { n->setHidden(false); n->dirtyUp(na); } } forceinline UnstopAllCursor::UnstopAllCursor(VisualNode* root, const VisualNode::NodeAllocator& na) : NodeCursor(root,na) {} forceinline void UnstopAllCursor::processCurrentNode(void) { VisualNode* n = node(); if (n->getStatus() == STOP) { n->setStop(false); n->dirtyUp(na); } } forceinline NextSolCursor::NextSolCursor(VisualNode* theNode, bool backwards, const VisualNode::NodeAllocator& na) : NodeCursor(theNode,na), back(backwards) {} forceinline void NextSolCursor::processCurrentNode(void) {} forceinline bool NextSolCursor::notOnSol(void) { return node() == startNode() || node()->getStatus() != SOLVED; } forceinline bool NextSolCursor::mayMoveUpwards(void) { return notOnSol() && !node()->isRoot(); } forceinline bool NextSolCursor::mayMoveDownwards(void) { return notOnSol() && !(back && node() == startNode()) && node()->hasSolvedChildren() && NodeCursor::mayMoveDownwards(); } forceinline void NextSolCursor::moveDownwards(void) { NodeCursor::moveDownwards(); if (back) { while (NodeCursor::mayMoveSidewards()) NodeCursor::moveSidewards(); } } forceinline bool NextSolCursor::mayMoveSidewards(void) { if (back) { return notOnSol() && !node()->isRoot() && alternative() > 0; } else { return notOnSol() && !node()->isRoot() && (alternative() < node()->getParent(na)->getNumberOfChildren() - 1); } } forceinline void NextSolCursor::moveSidewards(void) { if (back) { alternative(alternative()-1); node(node()->getParent(na)->getChild(na,alternative())); } else { NodeCursor::moveSidewards(); } } forceinline StatCursor::StatCursor(VisualNode* root, const VisualNode::NodeAllocator& na) : NodeCursor(root,na), curDepth(0), depth(0), failed(0), solved(0), choice(0), open(0) {} forceinline void StatCursor::processCurrentNode(void) { VisualNode* n = node(); switch (n->getStatus()) { case SOLVED: solved++; break; case FAILED: failed++; break; case BRANCH: choice++; break; case UNDETERMINED: open++; break; default: break; } } forceinline void StatCursor::moveDownwards(void) { curDepth++; depth = std::max(depth,curDepth); NodeCursor::moveDownwards(); } forceinline void StatCursor::moveUpwards(void) { curDepth--; NodeCursor::moveUpwards(); } forceinline DisposeCursor::DisposeCursor(VisualNode* root, const VisualNode::NodeAllocator& na) : NodeCursor(root,na) {} forceinline void DisposeCursor::processCurrentNode(void) { node()->dispose(); } }} // STATISTICS: gist-any