Generated on Mon Aug 25 11:35:36 2008 for Gecode by doxygen 1.5.6

channel.cc

Go to the documentation of this file.
00001 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
00002 /*
00003  *  Main authors:
00004  *     Christian Schulte <schulte@gecode.org>
00005  *
00006  *  Copyright:
00007  *     Christian Schulte, 2006
00008  *
00009  *  Last modified:
00010  *     $Date: 2008-07-11 10:46:48 +0200 (Fri, 11 Jul 2008) $ by $Author: tack $
00011  *     $Revision: 7346 $
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/int/channel.hh"
00039 
00040 namespace Gecode {
00041 
00042   void
00043   channel(Space* home, const IntVarArgs& x, unsigned int xoff,
00044           const IntVarArgs& y, unsigned int yoff,
00045           IntConLevel icl, PropKind) {
00046     using namespace Int;
00047     using namespace Channel;
00048     int n = x.size();
00049     if (n != y.size())
00050       throw ArgumentSizeMismatch("Int::channel");
00051     if (x.same() || y.same())
00052       throw ArgumentSame("Int::channel");
00053     if (home->failed()) return;
00054     if (n == 0)
00055       return;
00056 
00057     if (xoff < 2 && yoff < 2 && xoff == yoff) {
00058       if (icl == ICL_DOM) {
00059         DomInfo<IntView>* di
00060           = DomInfo<IntView>::allocate(home,2*(n+xoff));
00061         for (int i=n; i--; ) {
00062           di[xoff+i    ].init(x[i],n+xoff);
00063           di[2*xoff+i+n].init(y[i],n+xoff);
00064         }
00065         if (xoff == 1) {
00066           IntVar x0(home,0,0);
00067           di[0].init(x0, n+xoff);
00068           IntVar y0(home,0,0);
00069           di[n+xoff].init(y0, n+xoff);
00070         }
00071         if (x.same(y)) {
00072           GECODE_ES_FAIL(home,(Dom<IntView,true>::post(home,n+xoff,di)));
00073         } else {
00074           GECODE_ES_FAIL(home,(Dom<IntView,false>::post(home,n+xoff,di)));
00075         }
00076       } else {
00077         ValInfo<IntView>* vi
00078           = ValInfo<IntView>::allocate(home,2*(n+xoff));
00079         for (int i=n; i--; ) {
00080           vi[xoff+i    ].init(x[i],n+xoff);
00081           vi[2*xoff+i+n].init(y[i],n+xoff);
00082         }
00083         if (xoff == 1) {
00084           IntVar x0(home,0,0);
00085           vi[0].init(x0, n+xoff);
00086           IntVar y0(home,0,0);
00087           vi[n+xoff].init(y0, n+xoff);
00088         }
00089         if (x.same(y)) {
00090           GECODE_ES_FAIL(home,(Val<IntView,true>::post(home,n+xoff,vi)));
00091         } else {
00092           GECODE_ES_FAIL(home,(Val<IntView,false>::post(home,n+xoff,vi)));
00093         }
00094       }
00095     } else {
00096       if (icl == ICL_DOM) {
00097         DomInfo<OffsetView>* di
00098           = DomInfo<OffsetView>::allocate(home,2*n);
00099         for (int i=n; i--; ) {
00100           di[i  ].init(OffsetView(x[i],-xoff),n);
00101           di[i+n].init(OffsetView(y[i],-yoff),n);
00102         }
00103         if (x.same(y)) {
00104           GECODE_ES_FAIL(home,(Dom<OffsetView,true>::post(home,n,di)));
00105         } else {
00106           GECODE_ES_FAIL(home,(Dom<OffsetView,false>::post(home,n,di)));
00107         }
00108       } else {
00109         ValInfo<OffsetView>* vi
00110           = ValInfo<OffsetView>::allocate(home,2*n);
00111         for (int i=n; i--; ) {
00112           vi[i  ].init(OffsetView(x[i],-xoff),n);
00113           vi[i+n].init(OffsetView(y[i],-yoff),n);
00114         }
00115         if (x.same(y)) {
00116           GECODE_ES_FAIL(home,(Val<OffsetView,true>::post(home,n,vi)));
00117         } else {
00118           GECODE_ES_FAIL(home,(Val<OffsetView,false>::post(home,n,vi)));
00119         }
00120       }
00121     }
00122 
00123   }
00124 
00125   void
00126   channel(Space* home, const IntVarArgs& x, const IntVarArgs& y,
00127           IntConLevel icl, PropKind pk) {
00128     channel(home, x, 0, y, 0, icl, pk);
00129   }
00130   void
00131   channel(Space* home, BoolVar x0, IntVar x1, IntConLevel, PropKind) {
00132     using namespace Int;
00133     if (home->failed()) return;
00134     GECODE_ES_FAIL(home,Channel::LinkSingle::post(home,x0,x1));
00135   }
00136 
00137   void
00138   channel(Space* home, const BoolVarArgs& x, IntVar y, int o, 
00139           IntConLevel, PropKind) {
00140     using namespace Int;
00141     if (x.same())
00142       throw ArgumentSame("Int::channel");
00143     Limits::check(o,"Int::channel");
00144     if (home->failed()) return;
00145     ViewArray<BoolView> xv(home,x);
00146     GECODE_ES_FAIL(home,Channel::LinkMulti::post(home,xv,y,o));
00147   }
00148 
00149   namespace {
00150     using namespace Int;
00151     GECODE_REGISTER2(Channel::Dom<IntView, false>);
00152     GECODE_REGISTER2(Channel::Dom<IntView, true>);
00153     GECODE_REGISTER2(Channel::Val<IntView, false>);
00154     GECODE_REGISTER2(Channel::Val<IntView, true>);
00155     GECODE_REGISTER2(Channel::Dom<OffsetView, false>);
00156     GECODE_REGISTER2(Channel::Dom<OffsetView, true>);
00157     GECODE_REGISTER2(Channel::Val<OffsetView, false>);
00158     GECODE_REGISTER2(Channel::Val<OffsetView, true>);
00159     GECODE_REGISTER1(Channel::LinkSingle);
00160     GECODE_REGISTER1(Channel::LinkMulti);
00161   }
00162 
00163 }
00164 
00165 // STATISTICS: int-post