Generated on Thu Mar 22 10:39:35 2012 for Gecode by doxygen 1.6.3

channel.cpp

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  *     Guido Tack <tack@gecode.org>
00006  *
00007  *  Copyright:
00008  *     Christian Schulte, 2006
00009  *     Guido Tack, 2011
00010  *
00011  *  Last modified:
00012  *     $Date: 2011-04-01 15:26:13 +0200 (Fri, 01 Apr 2011) $ by $Author: tack $
00013  *     $Revision: 11858 $
00014  *
00015  *  This file is part of Gecode, the generic constraint
00016  *  development environment:
00017  *     http://www.gecode.org
00018  *
00019  *  Permission is hereby granted, free of charge, to any person obtaining
00020  *  a copy of this software and associated documentation files (the
00021  *  "Software"), to deal in the Software without restriction, including
00022  *  without limitation the rights to use, copy, modify, merge, publish,
00023  *  distribute, sublicense, and/or sell copies of the Software, and to
00024  *  permit persons to whom the Software is furnished to do so, subject to
00025  *  the following conditions:
00026  *
00027  *  The above copyright notice and this permission notice shall be
00028  *  included in all copies or substantial portions of the Software.
00029  *
00030  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00031  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00032  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00033  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
00034  *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
00035  *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
00036  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00037  *
00038  */
00039 
00040 #include <gecode/int/channel.hh>
00041 
00042 namespace Gecode {
00043 
00044   void
00045   channel(Home home, const IntVarArgs& x, int xoff,
00046           const IntVarArgs& y, int yoff,
00047           IntConLevel icl) {
00048     using namespace Int;
00049     using namespace Channel;
00050     int n = x.size();
00051     if (n != y.size())
00052       throw ArgumentSizeMismatch("Int::channel");
00053     if (x.same(home) || y.same(home))
00054       throw ArgumentSame("Int::channel");
00055     Limits::check(xoff,"Int::channel");
00056     Limits::check(yoff,"Int::channel");
00057     if ((xoff < 0) || (yoff < 0))
00058       throw OutOfLimits("Int::channel");
00059     if (home.failed()) return;
00060     if (n == 0)
00061       return;
00062 
00063     if ((xoff < 2) && (yoff < 2) && (xoff == yoff)) {
00064       if (icl == ICL_DOM) {
00065         DomInfo<IntView,NoOffset<IntView> >* di = 
00066           static_cast<Space&>(home).
00067             alloc<DomInfo<IntView,NoOffset<IntView> > >(2*(n+xoff));
00068         for (int i=n; i--; ) {
00069           di[xoff+i    ].init(x[i],n+xoff);
00070           di[2*xoff+i+n].init(y[i],n+xoff);
00071         }
00072         if (xoff == 1) {
00073           IntVar x0(home,0,0);
00074           di[0].init(x0, n+xoff);
00075           IntVar y0(home,0,0);
00076           di[n+xoff].init(y0, n+xoff);
00077         }
00078         NoOffset<IntView> noff;
00079         if (x.same(home,y)) {
00080           GECODE_ES_FAIL((Dom<IntView,NoOffset<IntView>,true>
00081             ::post(home,n+xoff,di,noff,noff)));
00082         } else {
00083           GECODE_ES_FAIL((Dom<IntView,NoOffset<IntView>,false>
00084             ::post(home,n+xoff,di,noff,noff)));
00085         }
00086       } else {
00087         ValInfo<IntView>* vi = 
00088           static_cast<Space&>(home).alloc<ValInfo<IntView> >(2*(n+xoff));
00089         for (int i=n; i--; ) {
00090           vi[xoff+i    ].init(x[i],n+xoff);
00091           vi[2*xoff+i+n].init(y[i],n+xoff);
00092         }
00093         if (xoff == 1) {
00094           IntVar x0(home,0,0);
00095           vi[0].init(x0, n+xoff);
00096           IntVar y0(home,0,0);
00097           vi[n+xoff].init(y0, n+xoff);
00098         }
00099         NoOffset<IntView> noff;
00100         if (x.same(home,y)) {
00101           GECODE_ES_FAIL((Val<IntView,NoOffset<IntView>,true>
00102             ::post(home,n+xoff,vi,noff,noff)));
00103         } else {
00104           GECODE_ES_FAIL((Val<IntView,NoOffset<IntView>,false>
00105             ::post(home,n+xoff,vi,noff,noff)));
00106         }
00107       }
00108     } else {
00109       if (icl == ICL_DOM) {
00110         DomInfo<IntView,Offset>* di = 
00111           static_cast<Space&>(home).alloc<DomInfo<IntView,Offset> >(2*n);
00112         for (int i=n; i--; ) {
00113           di[i  ].init(x[i],n);
00114           di[i+n].init(y[i],n);
00115         }
00116         Offset ox(-xoff);
00117         Offset oy(-yoff);
00118         if (x.same(home,y)) {
00119           GECODE_ES_FAIL((Dom<IntView,Offset,true>
00120                           ::post(home,n,di,ox,oy)));
00121         } else {
00122           GECODE_ES_FAIL((Dom<IntView,Offset,false>
00123                           ::post(home,n,di,ox,oy)));
00124         }
00125       } else {
00126         ValInfo<IntView>* vi = 
00127           static_cast<Space&>(home).alloc<ValInfo<IntView> >(2*n);
00128         for (int i=n; i--; ) {
00129           vi[i  ].init(x[i],n);
00130           vi[i+n].init(y[i],n);
00131         }
00132         Offset ox(-xoff);
00133         Offset oy(-yoff);
00134         if (x.same(home,y)) {
00135           GECODE_ES_FAIL((Val<IntView,Offset,true>
00136                           ::post(home,n,vi,ox,oy)));
00137         } else {
00138           GECODE_ES_FAIL((Val<IntView,Offset,false>
00139                           ::post(home,n,vi,ox,oy)));
00140         }
00141       }
00142     }
00143 
00144   }
00145 
00146   void
00147   channel(Home home, const IntVarArgs& x, const IntVarArgs& y,
00148           IntConLevel icl) {
00149     channel(home, x, 0, y, 0, icl);
00150   }
00151   void
00152   channel(Home home, BoolVar x0, IntVar x1, IntConLevel) {
00153     using namespace Int;
00154     if (home.failed()) return;
00155     GECODE_ES_FAIL(Channel::LinkSingle::post(home,x0,x1));
00156   }
00157 
00158   void
00159   channel(Home home, const BoolVarArgs& x, IntVar y, int o,
00160           IntConLevel) {
00161     using namespace Int;
00162     if (x.same(home))
00163       throw ArgumentSame("Int::channel");
00164     Limits::check(o,"Int::channel");
00165     if (home.failed()) return;
00166     ViewArray<BoolView> xv(home,x);
00167     GECODE_ES_FAIL(Channel::LinkMulti::post(home,xv,y,o));
00168   }
00169 
00170 }
00171 
00172 // STATISTICS: int-post