nodecursor.cpp
Go to the documentation of this file.00001 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */ 00002 /* 00003 * Main authors: 00004 * Guido Tack <tack@gecode.org> 00005 * 00006 * Copyright: 00007 * Guido Tack, 2006 00008 * 00009 * Last modified: 00010 * $Date: 2009-05-13 04:26:28 +0200 (Wed, 13 May 2009) $ by $Author: tack $ 00011 * $Revision: 9077 $ 00012 * 00013 * This file is part of Gecode, the generic constraint 00014 * development environment: 00015 * http://www.gecode.org 00016 * 00017 * Permission is hereby granted, free of charge, to any person obtaining 00018 * a copy of this software and associated documentation files (the 00019 * "Software"), to deal in the Software without restriction, including 00020 * without limitation the rights to use, copy, modify, merge, publish, 00021 * distribute, sublicense, and/or sell copies of the Software, and to 00022 * permit persons to whom the Software is furnished to do so, subject to 00023 * the following conditions: 00024 * 00025 * The above copyright notice and this permission notice shall be 00026 * included in all copies or substantial portions of the Software. 00027 * 00028 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 00029 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 00030 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 00031 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 00032 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 00033 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 00034 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 00035 * 00036 */ 00037 00038 #include <gecode/gist/nodecursor.hh> 00039 00040 namespace Gecode { namespace Gist { 00041 00042 HideFailedCursor::HideFailedCursor(VisualNode* root) 00043 : NodeCursor<VisualNode>(root) {} 00044 00045 void 00046 HideFailedCursor::processCurrentNode(void) { 00047 VisualNode* n = node(); 00048 if ((n->getStatus() == BRANCH || 00049 ((n->getStatus() == SPECIAL || n->getStatus() == STEP) && n->hasFailedChildren())) && 00050 !n->hasSolvedChildren() && 00051 n->getNoOfOpenChildren() == 0) { 00052 n->setHidden(true); 00053 n->setChildrenLayoutDone(false); 00054 n->dirtyUp(); 00055 } 00056 } 00057 00058 UnhideAllCursor::UnhideAllCursor(VisualNode* root) 00059 : NodeCursor<VisualNode>(root) {} 00060 00061 void 00062 UnhideAllCursor::processCurrentNode(void) { 00063 VisualNode* n = node(); 00064 if (n->isHidden()) { 00065 n->setHidden(false); 00066 n->dirtyUp(); 00067 } 00068 } 00069 00070 NextSolCursor::NextSolCursor(VisualNode* theNode, bool backwards) 00071 : NodeCursor<VisualNode>(theNode), back(backwards) {} 00072 00073 void 00074 NextSolCursor::processCurrentNode(void) {} 00075 00076 bool 00077 NextSolCursor::notOnSol(void) { 00078 return node() == startNode() || node()->getStatus() != SOLVED; 00079 } 00080 00081 bool 00082 NextSolCursor::mayMoveUpwards(void) { 00083 return notOnSol() && !node()->isRoot(); 00084 } 00085 00086 bool 00087 NextSolCursor::mayMoveDownwards(void) { 00088 return notOnSol() && !(back && node() == startNode()) 00089 && node()->hasSolvedChildren() 00090 && NodeCursor<VisualNode>::mayMoveDownwards(); 00091 } 00092 00093 void 00094 NextSolCursor::moveDownwards(void) { 00095 NodeCursor<VisualNode>::moveDownwards(); 00096 if (back) { 00097 while (NodeCursor<VisualNode>::mayMoveSidewards()) 00098 NodeCursor<VisualNode>::moveSidewards(); 00099 } 00100 } 00101 00102 bool 00103 NextSolCursor::mayMoveSidewards(void) { 00104 if (back) { 00105 return notOnSol() && !node()->isRoot() && alternative() > 0; 00106 } else { 00107 return notOnSol() && !node()->isRoot() && 00108 (alternative() < node()->getParent()->getNumberOfChildren() - 1); 00109 } 00110 } 00111 00112 void 00113 NextSolCursor::moveSidewards(void) { 00114 if (back) { 00115 alternative(alternative()-1); 00116 node(node()->getParent()->getChild(alternative())); 00117 } else { 00118 NodeCursor<VisualNode>::moveSidewards(); 00119 } 00120 } 00121 00122 StatCursor::StatCursor(VisualNode* root) 00123 : NodeCursor<VisualNode>(root), 00124 curDepth(0), depth(0), failed(0), solved(0), choice(0), open(0) {} 00125 00126 void 00127 StatCursor::processCurrentNode(void) { 00128 VisualNode* n = node(); 00129 switch (n->getStatus()) { 00130 case SOLVED: solved++; break; 00131 case FAILED: failed++; break; 00132 case BRANCH: choice++; break; 00133 case UNDETERMINED: open++; break; 00134 default: break; 00135 } 00136 } 00137 00138 void 00139 StatCursor::moveDownwards(void) { 00140 curDepth++; 00141 depth = std::max(depth,curDepth); 00142 NodeCursor<VisualNode>::moveDownwards(); 00143 } 00144 00145 void 00146 StatCursor::moveUpwards(void) { 00147 curDepth--; 00148 NodeCursor<VisualNode>::moveUpwards(); 00149 } 00150 00151 }} 00152 00153 // STATISTICS: gist-any