this repo has no description
1/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */ 2/* 3 * Main authors: 4 * Christian Schulte <schulte@gecode.org> 5 * Guido Tack <tack@gecode.org> 6 * 7 * Copyright: 8 * Christian Schulte, 2006 9 * Guido Tack, 2011 10 * 11 * This file is part of Gecode, the generic constraint 12 * development environment: 13 * http://www.gecode.org 14 * 15 * Permission is hereby granted, free of charge, to any person obtaining 16 * a copy of this software and associated documentation files (the 17 * "Software"), to deal in the Software without restriction, including 18 * without limitation the rights to use, copy, modify, merge, publish, 19 * distribute, sublicense, and/or sell copies of the Software, and to 20 * permit persons to whom the Software is furnished to do so, subject to 21 * the following conditions: 22 * 23 * The above copyright notice and this permission notice shall be 24 * included in all copies or substantial portions of the Software. 25 * 26 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 27 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 28 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 29 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 30 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 31 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 32 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 33 * 34 */ 35 36#include <gecode/int/channel.hh> 37 38namespace Gecode { 39 40 void 41 channel(Home home, const IntVarArgs& x, int xoff, 42 const IntVarArgs& y, int yoff, 43 IntPropLevel ipl) { 44 using namespace Int; 45 using namespace Channel; 46 int n = x.size(); 47 if (n != y.size()) 48 throw ArgumentSizeMismatch("Int::channel"); 49 if (same(x) || same(y)) 50 throw ArgumentSame("Int::channel"); 51 Limits::check(xoff,"Int::channel"); 52 Limits::check(yoff,"Int::channel"); 53 if ((xoff < 0) || (yoff < 0)) 54 throw OutOfLimits("Int::channel"); 55 GECODE_POST; 56 if (n == 0) 57 return; 58 59 if ((xoff < 2) && (yoff < 2) && (xoff == yoff)) { 60 if (vbd(ipl) == IPL_DOM) { 61 DomInfo<IntView,NoOffset<IntView> >* di = 62 static_cast<Space&>(home). 63 alloc<DomInfo<IntView,NoOffset<IntView> > >(2*(n+xoff)); 64 for (int i=0; i<n; i++) { 65 di[xoff+i ].init(x[i],n+xoff); 66 di[2*xoff+i+n].init(y[i],n+xoff); 67 } 68 if (xoff == 1) { 69 IntVar x0(home,0,0); 70 di[0].init(x0, n+xoff); 71 IntVar y0(home,0,0); 72 di[n+xoff].init(y0, n+xoff); 73 } 74 NoOffset<IntView> noff; 75 if (same(x,y)) { 76 GECODE_ES_FAIL((Dom<IntView,NoOffset<IntView>,true> 77 ::post(home,n+xoff,di,noff,noff))); 78 } else { 79 GECODE_ES_FAIL((Dom<IntView,NoOffset<IntView>,false> 80 ::post(home,n+xoff,di,noff,noff))); 81 } 82 } else { 83 ValInfo<IntView>* vi = 84 static_cast<Space&>(home).alloc<ValInfo<IntView> >(2*(n+xoff)); 85 for (int i=0; i<n; i++) { 86 vi[xoff+i ].init(x[i],n+xoff); 87 vi[2*xoff+i+n].init(y[i],n+xoff); 88 } 89 if (xoff == 1) { 90 IntVar x0(home,0,0); 91 vi[0].init(x0, n+xoff); 92 IntVar y0(home,0,0); 93 vi[n+xoff].init(y0, n+xoff); 94 } 95 NoOffset<IntView> noff; 96 if (same(x,y)) { 97 GECODE_ES_FAIL((Val<IntView,NoOffset<IntView>,true> 98 ::post(home,n+xoff,vi,noff,noff))); 99 } else { 100 GECODE_ES_FAIL((Val<IntView,NoOffset<IntView>,false> 101 ::post(home,n+xoff,vi,noff,noff))); 102 } 103 } 104 } else { 105 if (vbd(ipl) == IPL_DOM) { 106 DomInfo<IntView,Offset>* di = 107 static_cast<Space&>(home).alloc<DomInfo<IntView,Offset> >(2*n); 108 for (int i=0; i<n; i++) { 109 di[i ].init(x[i],n); 110 di[i+n].init(y[i],n); 111 } 112 Offset ox(-xoff); 113 Offset oy(-yoff); 114 if (same(x,y)) { 115 GECODE_ES_FAIL((Dom<IntView,Offset,true> 116 ::post(home,n,di,ox,oy))); 117 } else { 118 GECODE_ES_FAIL((Dom<IntView,Offset,false> 119 ::post(home,n,di,ox,oy))); 120 } 121 } else { 122 ValInfo<IntView>* vi = 123 static_cast<Space&>(home).alloc<ValInfo<IntView> >(2*n); 124 for (int i=0; i<n; i++) { 125 vi[i ].init(x[i],n); 126 vi[i+n].init(y[i],n); 127 } 128 Offset ox(-xoff); 129 Offset oy(-yoff); 130 if (same(x,y)) { 131 GECODE_ES_FAIL((Val<IntView,Offset,true> 132 ::post(home,n,vi,ox,oy))); 133 } else { 134 GECODE_ES_FAIL((Val<IntView,Offset,false> 135 ::post(home,n,vi,ox,oy))); 136 } 137 } 138 } 139 140 } 141 142 void 143 channel(Home home, const IntVarArgs& x, const IntVarArgs& y, 144 IntPropLevel ipl) { 145 channel(home, x, 0, y, 0, ipl); 146 } 147 void 148 channel(Home home, BoolVar x0, IntVar x1, IntPropLevel) { 149 using namespace Int; 150 GECODE_POST; 151 GECODE_ES_FAIL(Channel::LinkSingle::post(home,x0,x1)); 152 } 153 154 void 155 channel(Home home, const BoolVarArgs& x, IntVar y, int o, 156 IntPropLevel) { 157 using namespace Int; 158 if (same(x)) 159 throw ArgumentSame("Int::channel"); 160 Limits::check(o,"Int::channel"); 161 GECODE_POST; 162 ViewArray<BoolView> xv(home,x); 163 GECODE_ES_FAIL(Channel::LinkMulti::post(home,xv,y,o)); 164 } 165 166} 167 168// STATISTICS: int-post