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