nodecursor.hpp
Go to the documentation of this file.
/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
* Main authors:
* Guido Tack <tack@gecode.org>
*
* Copyright:
* Guido Tack, 2006
*
* 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<class Node>
forceinline
NodeCursor<Node>::NodeCursor(Node* theNode,
const typename Node::NodeAllocator& na0)
: _startNode(theNode), _node(theNode),
_alternative(theNode->getAlternative(na0)),
na(na0) {}
template<class Node>
forceinline Node*
NodeCursor<Node>::node(void) { return _node; }
template<class Node>
forceinline unsigned int
NodeCursor<Node>::alternative(void) { return _alternative; }
template<class Node>
forceinline void
NodeCursor<Node>::alternative(unsigned int a) { _alternative=a; }
template<class Node>
forceinline Node*
NodeCursor<Node>::startNode(void) { return _startNode; }
template<class Node>
forceinline void
NodeCursor<Node>::node(Node* n) { _node = n; }
template<class Node>
forceinline bool
NodeCursor<Node>::mayMoveUpwards(void) {
return _node != _startNode && !_node->isRoot();
}
template<class Node>
forceinline void
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 (int i=p->getNumberOfChildren(); i--;) {
if (p->getChild(na,i) == _node) {
_alternative = i;
break;
}
}
}
}
template<class Node>
forceinline bool
NodeCursor<Node>::mayMoveDownwards(void) {
return _node->getNumberOfChildren() > 0;
}
template<class Node>
forceinline void
NodeCursor<Node>::moveDownwards(void) {
_alternative = 0;
_node = _node->getChild(na,0);
}
template<class Node>
forceinline bool
NodeCursor<Node>::mayMoveSidewards(void) {
return (!_node->isRoot()) && (_node != _startNode) &&
(_alternative < _node->getParent(na)->getNumberOfChildren() - 1);
}
template<class Node>
forceinline void
NodeCursor<Node>::moveSidewards(void) {
_node =
static_cast<Node*>(_node->getParent(na)->getChild(na,++_alternative));
}
forceinline bool
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,
const VisualNode::NodeAllocator& na,
bool onlyDirtyNodes)
: NodeCursor<VisualNode>(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<VisualNode>(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<VisualNode>(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<VisualNode>(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<VisualNode>::mayMoveDownwards();
}
forceinline void
NextSolCursor::moveDownwards(void) {
NodeCursor<VisualNode>::moveDownwards();
if (back) {
while (NodeCursor<VisualNode>::mayMoveSidewards())
NodeCursor<VisualNode>::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<VisualNode>::moveSidewards();
}
}
forceinline
StatCursor::StatCursor(VisualNode* root,
const VisualNode::NodeAllocator& na)
: NodeCursor<VisualNode>(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<VisualNode>::moveDownwards();
}
forceinline void
StatCursor::moveUpwards(void) {
curDepth--;
NodeCursor<VisualNode>::moveUpwards();
}
forceinline
BranchLabelCursor::BranchLabelCursor(VisualNode* root, BestNode* curBest,
int c_d, int a_d, bool clear,
VisualNode::NodeAllocator& na)
: NodeCursor<VisualNode>(root,na), _na(na), _curBest(curBest),
_c_d(c_d), _a_d(a_d), _clear(clear) {}
forceinline void
BranchLabelCursor::processCurrentNode(void) {
VisualNode* n = node();
if (!_clear) {
if (!na.hasLabel(n)) {
VisualNode* p = n->getParent(_na);
if (p) {
std::string l =
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,
const VisualNode::NodeAllocator& na)
: NodeCursor<VisualNode>(root,na) {}
forceinline void
DisposeCursor::processCurrentNode(void) {
node()->dispose();
}
}}
// STATISTICS: gist-any
Gecode::Gist::BestNode
Static reference to the currently best space.
Definition spacenode.hh:80
Gecode::Gist::BranchLabelCursor::BranchLabelCursor
BranchLabelCursor(VisualNode *theNode, BestNode *curBest, int c_d, int a_d, bool clear, VisualNode::NodeAllocator &na)
Constructor.
Definition nodecursor.hpp:256
Gecode::Gist::BranchLabelCursor::processCurrentNode
void processCurrentNode(void)
Definition nodecursor.hpp:263
Gecode::Gist::DisposeCursor::processCurrentNode
void processCurrentNode(void)
Dispose node.
Definition nodecursor.hpp:292
Gecode::Gist::DisposeCursor::DisposeCursor
DisposeCursor(VisualNode *theNode, const VisualNode::NodeAllocator &na)
Constructor.
Definition nodecursor.hpp:287
Gecode::Gist::HideFailedCursor::processCurrentNode
void processCurrentNode(void)
Process node.
Definition nodecursor.hpp:130
Gecode::Gist::HideFailedCursor::HideFailedCursor
HideFailedCursor(VisualNode *theNode, const VisualNode::NodeAllocator &na, bool onlyDirtyNodes)
Constructor.
Definition nodecursor.hpp:124
Gecode::Gist::HideFailedCursor::mayMoveDownwards
bool mayMoveDownwards(void)
Test if the cursor may move to the first child node.
Definition nodecursor.hpp:115
Gecode::Gist::NextSolCursor::processCurrentNode
void processCurrentNode(void)
Do nothing.
Definition nodecursor.hpp:175
Gecode::Gist::NextSolCursor::mayMoveUpwards
bool mayMoveUpwards(void)
Test if the cursor may move to the parent node.
Definition nodecursor.hpp:183
Gecode::Gist::NextSolCursor::NextSolCursor
NextSolCursor(VisualNode *theNode, bool backwards, const VisualNode::NodeAllocator &na)
Constructor.
Definition nodecursor.hpp:170
Gecode::Gist::NextSolCursor::moveSidewards
void moveSidewards(void)
Move cursor to the first sibling.
Definition nodecursor.hpp:215
Gecode::Gist::NextSolCursor::mayMoveDownwards
bool mayMoveDownwards(void)
Test if cursor may move to the first child node.
Definition nodecursor.hpp:188
Gecode::Gist::NextSolCursor::mayMoveSidewards
bool mayMoveSidewards(void)
Test if cursor may move to the first sibling.
Definition nodecursor.hpp:204
Gecode::Gist::NextSolCursor::moveDownwards
void moveDownwards(void)
Move cursor to the first child node.
Definition nodecursor.hpp:195
Gecode::Gist::NodeCursor::na
const Node::NodeAllocator & na
The node allocator.
Definition nodecursor.hh:53
Gecode::Gist::NodeCursor::moveDownwards
void moveDownwards(void)
Move cursor to the first child node.
Definition nodecursor.hpp:95
Gecode::Gist::NodeCursor::mayMoveUpwards
bool mayMoveUpwards(void)
Test if the cursor may move to the parent node.
Definition nodecursor.hpp:66
Gecode::Gist::NodeCursor::NodeCursor
NodeCursor(Node *theNode, const typename Node::NodeAllocator &na)
Construct cursor, initially set to theNode.
Definition nodecursor.hpp:38
Gecode::Gist::NodeCursor::node
Node * node(void)
Return current node.
Definition nodecursor.hpp:46
Gecode::Gist::NodeCursor::alternative
unsigned int alternative(void)
Return current alternative.
Definition nodecursor.hpp:50
Gecode::Gist::NodeCursor< VisualNode >::node
void node(VisualNode *n)
Definition nodecursor.hpp:62
Gecode::Gist::NodeCursor::startNode
Node * startNode(void)
Return start node.
Definition nodecursor.hpp:58
Gecode::Gist::NodeCursor::moveSidewards
void moveSidewards(void)
Move cursor to the first sibling.
Definition nodecursor.hpp:109
Gecode::Gist::NodeCursor::mayMoveSidewards
bool mayMoveSidewards(void)
Test if cursor may move to the first sibling.
Definition nodecursor.hpp:102
Gecode::Gist::NodeCursor::moveUpwards
void moveUpwards(void)
Move cursor to the parent node.
Definition nodecursor.hpp:72
Gecode::Gist::NodeCursor::mayMoveDownwards
bool mayMoveDownwards(void)
Test if cursor may move to the first child node.
Definition nodecursor.hpp:89
Gecode::Gist::Node
Base class for nodes of the search tree.
Definition node.hh:106
Gecode::Gist::Node::NodeAllocator
NodeAllocatorBase< VisualNode > NodeAllocator
Definition node.hh:143
Gecode::Gist::Node::getNumberOfChildren
unsigned int getNumberOfChildren(void) const
Return the number of children.
Definition node.hpp:214
Gecode::Gist::Node::getParent
int getParent(void) const
Return the parent.
Definition node.hpp:182
Gecode::Gist::Node::getChild
int getChild(int n) const
Return index of child no n.
Definition node.hpp:195
Gecode::Gist::SpaceNode::getChoice
const Choice * getChoice(void)
Return choice of this node.
Definition spacenode.hpp:181
Gecode::Gist::SpaceNode::hasSolvedChildren
bool hasSolvedChildren(void)
Return whether the subtree of this node has any solved children.
Definition spacenode.hpp:149
Gecode::Gist::SpaceNode::getStatus
NodeStatus getStatus(void) const
Return current status of the node.
Definition spacenode.hpp:71
Gecode::Gist::SpaceNode::purge
void purge(const NodeAllocator &na)
Clear working space and copy (if present and this is not the root).
Definition spacenode.hpp:120
Gecode::Gist::SpaceNode::getNoOfOpenChildren
int getNoOfOpenChildren(const NodeAllocator &na)
Return number of open children.
Definition spacenode.cpp:369
Gecode::Gist::StatCursor::choice
int choice
Number of choice nodes.
Definition nodecursor.hh:170
Gecode::Gist::StatCursor::moveDownwards
void moveDownwards(void)
Move cursor to the first child node.
Definition nodecursor.hpp:243
Gecode::Gist::StatCursor::failed
int failed
Number of failed nodes.
Definition nodecursor.hh:166
Gecode::Gist::StatCursor::StatCursor
StatCursor(VisualNode *theNode, const VisualNode::NodeAllocator &na)
Constructor.
Definition nodecursor.hpp:225
Gecode::Gist::StatCursor::depth
int depth
Depth of the search tree.
Definition nodecursor.hh:164
Gecode::Gist::StatCursor::processCurrentNode
void processCurrentNode(void)
Collect statistics.
Definition nodecursor.hpp:231
Gecode::Gist::StatCursor::open
int open
Number of open nodes.
Definition nodecursor.hh:172
Gecode::Gist::StatCursor::solved
int solved
Number of solved nodes.
Definition nodecursor.hh:168
Gecode::Gist::StatCursor::moveUpwards
void moveUpwards(void)
Move cursor to the parent node.
Definition nodecursor.hpp:250
Gecode::Gist::UnhideAllCursor::UnhideAllCursor
UnhideAllCursor(VisualNode *theNode, const VisualNode::NodeAllocator &na)
Constructor.
Definition nodecursor.hpp:142
Gecode::Gist::UnhideAllCursor::processCurrentNode
void processCurrentNode(void)
Process node.
Definition nodecursor.hpp:147
Gecode::Gist::UnstopAllCursor::processCurrentNode
void processCurrentNode(void)
Process node.
Definition nodecursor.hpp:161
Gecode::Gist::UnstopAllCursor::UnstopAllCursor
UnstopAllCursor(VisualNode *theNode, const VisualNode::NodeAllocator &na)
Constructor.
Definition nodecursor.hpp:156
Gecode::Gist::VisualNode
Node class that supports visual layout
Definition visualnode.hh:125
Gecode::Gist::VisualNode::isHidden
bool isHidden(void)
Return if node is hidden.
Definition visualnode.hpp:129
Gecode::Gist::VisualNode::setHidden
void setHidden(bool h)
Set hidden state to h.
Definition visualnode.hpp:134
Gecode::Gist::VisualNode::getBranchLabel
std::string getBranchLabel(NodeAllocator &na, VisualNode *p, const Choice *c, BestNode *curBest, int c_d, int a_d, int alt)
Return string that describes the branch.
Definition visualnode.cpp:284
Gecode::Gist::VisualNode::dirtyUp
void dirtyUp(const NodeAllocator &na)
Mark all nodes up the path to the parent as dirty.
Definition visualnode.cpp:102
Gecode::Gist::VisualNode::isDirty
bool isDirty(void)
Return whether node is marked as dirty.
Definition visualnode.hpp:153
Gecode::Gist::VisualNode::setChildrenLayoutDone
void setChildrenLayoutDone(bool d)
Mark node whether the layout of the node's children has been completed.
Definition visualnode.hpp:168
Gecode::Gist::VisualNode::setStop
void setStop(bool h)
Set stop state to h.
Definition visualnode.hpp:139
Gecode::Gist
The Gecode Interactive Search Tool.
Gecode::Gist::UNDETERMINED
@ UNDETERMINED
Node that has not been explored yet.
Definition spacenode.hh:48
Gecode::Gist::FAILED
@ FAILED
Node representing failure.
Definition spacenode.hh:46
Gecode::Gist::STOP
@ STOP
Node representing stop point.
Definition spacenode.hh:49
Gecode::Gist::SOLVED
@ SOLVED
Node representing a solution.
Definition spacenode.hh:45
Gecode::Gist::BRANCH
@ BRANCH
Node representing a branch.
Definition spacenode.hh:47
Gecode
Gecode toplevel namespace
forceinline
#define forceinline
Definition config.hpp:187