Generated on Fri Oct 19 11:25:00 2018 for Gecode by doxygen 1.6.3

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  *  This file is part of Gecode, the generic constraint
00010  *  development environment:
00011  *     http://www.gecode.org
00012  *
00013  * Permission is hereby granted, free of charge, to any person obtaining
00014  * a copy of this software and associated documentation files (the
00015  * "Software"), to deal in the Software without restriction, including
00016  * without limitation the rights to use, copy, modify, merge, publish,
00017  * distribute, sublicense, and/or sell copies of the Software, and to
00018  * permit persons to whom the Software is furnished to do so, subject to
00019  * the following conditions:
00020  *
00021  * The above copyright notice and this permission notice shall be
00022  * included in all copies or substantial portions of the Software.
00023  *
00024  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00025  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00026  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00027  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
00028  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
00029  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
00030  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00031  *
00032  */
00033 
00034 namespace Gecode { namespace Gist {
00035 
00036   forceinline void
00037   SpaceNode::setFlag(int flag, bool value) {
00038     if (value)
00039       nstatus |= 1<<(flag-1);
00040     else
00041       nstatus &= ~(1<<(flag-1));
00042   }
00043 
00044   forceinline bool
00045   SpaceNode::getFlag(int flag) const {
00046     return (nstatus & (1<<(flag-1))) != 0;
00047   }
00048 
00049   forceinline void
00050   SpaceNode::setHasOpenChildren(bool b) {
00051     setFlag(HASOPENCHILDREN, b);
00052   }
00053 
00054   forceinline void
00055   SpaceNode::setHasFailedChildren(bool b) {
00056     setFlag(HASFAILEDCHILDREN, b);
00057   }
00058 
00059   forceinline void
00060   SpaceNode::setHasSolvedChildren(bool b) {
00061     setFlag(HASSOLVEDCHILDREN, b);
00062   }
00063 
00064   forceinline void
00065   SpaceNode::setStatus(NodeStatus s) {
00066     nstatus &= ~( STATUSMASK );
00067     nstatus |= s << 20;
00068   }
00069 
00070   forceinline NodeStatus
00071   SpaceNode::getStatus(void) const {
00072     return static_cast<NodeStatus>((nstatus & STATUSMASK) >> 20);
00073   }
00074 
00075   forceinline void
00076   SpaceNode::setDistance(unsigned int d) {
00077     if (d > MAXDISTANCE)
00078       d = MAXDISTANCE;
00079     nstatus &= ~( DISTANCEMASK );
00080     nstatus |= d;
00081   }
00082 
00083   forceinline unsigned int
00084   SpaceNode::getDistance(void) const {
00085     return nstatus & DISTANCEMASK;
00086   }
00087 
00088   forceinline
00089   SpaceNode::SpaceNode(int p)
00090   : Node(p), copy(NULL), nstatus(0) {
00091     choice = NULL;
00092     setStatus(UNDETERMINED);
00093     setHasSolvedChildren(false);
00094     setHasFailedChildren(false);
00095   }
00096 
00097   forceinline Space*
00098   SpaceNode::getSpace(NodeAllocator& na,
00099                       BestNode* curBest, int c_d, int a_d) {
00100     acquireSpace(na,curBest,c_d,a_d);
00101     Space* ret;
00102     if (Support::marked(copy)) {
00103       ret = static_cast<Space*>(Support::unmark(copy));
00104       copy = NULL;
00105     } else {
00106       ret = copy->clone();
00107     }
00108     return ret;
00109   }
00110 
00111   forceinline const Space*
00112   SpaceNode::getWorkingSpace(void) const {
00113     assert(copy != NULL);
00114     if (Support::marked(copy))
00115       return static_cast<Space*>(Support::unmark(copy));
00116     return copy;
00117   }
00118 
00119   forceinline void
00120   SpaceNode::purge(const NodeAllocator& na) {
00121     if (!isRoot() && (getStatus() != SOLVED || !na.bab())) {
00122       // only delete copies from solutions if we are not in BAB
00123       if (Support::marked(copy))
00124         delete static_cast<Space*>(Support::unmark(copy));
00125       else
00126         delete copy;
00127       copy = NULL;
00128     }
00129   }
00130 
00131 
00132   forceinline bool
00133   SpaceNode::isCurrentBest(BestNode* curBest) {
00134     return curBest != NULL && curBest->s == this;
00135   }
00136 
00137   forceinline bool
00138   SpaceNode::isOpen(void) {
00139     return ((getStatus() == UNDETERMINED) ||
00140             getFlag(HASOPENCHILDREN));
00141   }
00142 
00143   forceinline bool
00144   SpaceNode::hasFailedChildren(void) {
00145     return getFlag(HASFAILEDCHILDREN);
00146   }
00147 
00148   forceinline bool
00149   SpaceNode::hasSolvedChildren(void) {
00150     return getFlag(HASSOLVEDCHILDREN);
00151   }
00152 
00153   forceinline bool
00154   SpaceNode::hasOpenChildren(void) {
00155     return getFlag(HASOPENCHILDREN);
00156   }
00157 
00158   forceinline bool
00159   SpaceNode::hasCopy(void) {
00160     return copy != NULL;
00161   }
00162 
00163   forceinline bool
00164   SpaceNode::hasWorkingSpace(void) {
00165     return copy != NULL && Support::marked(copy);
00166   }
00167 
00168   forceinline int
00169   SpaceNode::getAlternative(const NodeAllocator& na) const {
00170     SpaceNode* p = getParent(na);
00171     if (p == NULL)
00172       return -1;
00173     for (int i=static_cast<int>(p->getNumberOfChildren()); i--;)
00174       if (p->getChild(na,i) == this)
00175         return i;
00176     GECODE_NEVER;
00177     return -1;
00178   }
00179 
00180   forceinline const Choice*
00181   SpaceNode::getChoice(void) {
00182     return choice;
00183   }
00184 
00185 }}
00186 
00187 // STATISTICS: gist-any