spacenode.hpp
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: 2010-03-30 12:12:40 +0200 (Tue, 30 Mar 2010) $ by $Author: tack $ 00011 * $Revision: 10596 $ 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 namespace Gecode { namespace Gist { 00039 00040 forceinline SpaceNode* 00041 SpaceNode::getParent() { 00042 return static_cast<SpaceNode*>(Node::getParent()); 00043 } 00044 00045 forceinline SpaceNode* 00046 SpaceNode::getChild(int i) { 00047 return static_cast<SpaceNode*>(Node::getChild(i)); 00048 } 00049 00050 forceinline void 00051 SpaceNode::setFlag(int flag, bool value) { 00052 if (value) 00053 nstatus |= 1<<(flag-1); 00054 else 00055 nstatus &= ~(1<<(flag-1)); 00056 } 00057 00058 forceinline bool 00059 SpaceNode::getFlag(int flag) const { 00060 return (nstatus & (1<<(flag-1))) != 0; 00061 } 00062 00063 forceinline void 00064 SpaceNode::setHasOpenChildren(bool b) { 00065 setFlag(HASOPENCHILDREN, b); 00066 } 00067 00068 forceinline void 00069 SpaceNode::setHasFailedChildren(bool b) { 00070 setFlag(HASFAILEDCHILDREN, b); 00071 } 00072 00073 forceinline void 00074 SpaceNode::setHasSolvedChildren(bool b) { 00075 setFlag(HASSOLVEDCHILDREN, b); 00076 } 00077 00078 forceinline void 00079 SpaceNode::setStatus(NodeStatus s) { 00080 nstatus &= ~( STATUSMASK ); 00081 nstatus |= s << 20; 00082 } 00083 00084 forceinline NodeStatus 00085 SpaceNode::getStatus(void) const { 00086 return static_cast<NodeStatus>((nstatus & STATUSMASK) >> 20); 00087 } 00088 00089 forceinline void 00090 SpaceNode::setDistance(unsigned int d) { 00091 if (d > MAXDISTANCE) 00092 d = MAXDISTANCE; 00093 nstatus &= ~( DISTANCEMASK ); 00094 nstatus |= d; 00095 } 00096 00097 forceinline unsigned int 00098 SpaceNode::getDistance(void) const { 00099 return nstatus & DISTANCEMASK; 00100 } 00101 00102 forceinline 00103 SpaceNode::SpaceNode(void) 00104 : copy(NULL), workingSpace(NULL), ownBest(NULL), nstatus(0) { 00105 choice = NULL; 00106 setStatus(UNDETERMINED); 00107 setHasSolvedChildren(false); 00108 setHasFailedChildren(false); 00109 } 00110 00111 forceinline Space* 00112 SpaceNode::getSpace(BestNode* curBest, int c_d, int a_d) { 00113 acquireSpace(curBest,c_d,a_d); 00114 Space* ret = workingSpace; 00115 workingSpace = NULL; 00116 return ret; 00117 } 00118 00119 forceinline const Space* 00120 SpaceNode::getWorkingSpace(void) const { 00121 return workingSpace; 00122 } 00123 00124 forceinline void 00125 SpaceNode::purge(void) { 00126 if (getStatus() != SOLVED || ownBest == NULL) { 00127 // only delete working spaces from solutions if we are not in BAB 00128 delete workingSpace; 00129 workingSpace = NULL; 00130 } 00131 if (!isRoot()) { 00132 delete copy; 00133 copy = NULL; 00134 } 00135 } 00136 00137 00138 forceinline bool 00139 SpaceNode::isCurrentBest(BestNode* curBest) { 00140 return curBest != NULL && curBest->s == this; 00141 } 00142 00143 forceinline bool 00144 SpaceNode::isOpen(void) { 00145 return ((getStatus() == UNDETERMINED) || 00146 getFlag(HASOPENCHILDREN)); 00147 } 00148 00149 forceinline bool 00150 SpaceNode::hasFailedChildren(void) { 00151 return getFlag(HASFAILEDCHILDREN); 00152 } 00153 00154 forceinline bool 00155 SpaceNode::hasSolvedChildren(void) { 00156 return getFlag(HASSOLVEDCHILDREN); 00157 } 00158 00159 forceinline bool 00160 SpaceNode::hasOpenChildren(void) { 00161 return getFlag(HASOPENCHILDREN); 00162 } 00163 00164 forceinline bool 00165 SpaceNode::hasCopy(void) { 00166 return copy != NULL; 00167 } 00168 00169 forceinline bool 00170 SpaceNode::hasWorkingSpace(void) { 00171 return workingSpace != NULL; 00172 } 00173 00174 forceinline int 00175 SpaceNode::getAlternative(void) { 00176 SpaceNode* p = getParent(); 00177 if (p) { 00178 for (int i=p->getNumberOfChildren(); i--;) 00179 if (p->getChild(i) == this) 00180 return i; 00181 } 00182 return -1; 00183 } 00184 00185 forceinline const Choice* 00186 SpaceNode::getChoice(void) { 00187 return choice; 00188 } 00189 00190 }} 00191 00192 // STATISTICS: gist-any