this repo has no description
1/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2/*
3 * Main authors:
4 * Guido Tack <tack@gecode.org>
5 * Christian Schulte <schulte@gecode.org>
6 * Gabor Szokoli <szokoli@gecode.org>
7 *
8 * Copyright:
9 * Guido Tack, 2004
10 * Christian Schulte, 2004
11 * Gabor Szokoli, 2004
12 *
13 * This file is part of Gecode, the generic constraint
14 * development environment:
15 * http://www.gecode.org
16 *
17 * Permission is hereby granted, free of charge, to any person obtaining
18 * a copy of this software and associated documentation files (the
19 * "Software"), to deal in the Software without restriction, including
20 * without limitation the rights to use, copy, modify, merge, publish,
21 * distribute, sublicense, and/or sell copies of the Software, and to
22 * permit persons to whom the Software is furnished to do so, subject to
23 * the following conditions:
24 *
25 * The above copyright notice and this permission notice shall be
26 * included in all copies or substantial portions of the Software.
27 *
28 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
29 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
30 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
31 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
32 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
33 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
34 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35 *
36 */
37
38#include <gecode/int.hh>
39
40namespace Gecode { namespace Set { namespace Channel {
41
42 template<class View>
43 forceinline
44 ChannelInt<View>::ChannelInt(Home home,
45 ViewArray<Gecode::Int::CachedView<
46 Gecode::Int::IntView> >& xs0,
47 ViewArray<CachedView<View> >& ys0)
48 : Propagator(home), xs(xs0), ys(ys0) {
49 for (int i=xs.size(); i--;)
50 xs[i].initCache(home,IntSet(0,ys.size()-1));
51 for (int i=ys.size(); i--;)
52 ys[i].initCache(home,IntSet::empty,IntSet(0,xs.size()-1));
53 xs.subscribe(home,*this, Gecode::Int::PC_INT_DOM);
54 ys.subscribe(home,*this, PC_SET_ANY);
55 }
56
57 template<class View>
58 forceinline
59 ChannelInt<View>::ChannelInt(Space& home, ChannelInt& p)
60 : Propagator(home,p) {
61 xs.update(home,p.xs);
62 ys.update(home,p.ys);
63 }
64
65 template<class View>
66 forceinline ExecStatus
67 ChannelInt<View>::post(Home home,
68 ViewArray<Gecode::Int::CachedView<
69 Gecode::Int::IntView> >& xs,
70 ViewArray<CachedView<View> >& ys) {
71 // Sharing of ys is taken care of in the propagator:
72 // The ys are propagated to be disjoint, so shared variables
73 // result in failure.
74 int xssize = xs.size();
75 for (int i=ys.size(); i--;) {
76 GECODE_ME_CHECK(ys[i].exclude(home, xssize, Limits::max));
77 GECODE_ME_CHECK(ys[i].exclude(home, Limits::min, -1));
78 }
79 int yssize = ys.size();
80 if (yssize > Gecode::Int::Limits::max)
81 return ES_FAILED;
82 for (int i=xs.size(); i--;) {
83 GECODE_ME_CHECK(xs[i].gq(home, 0));
84 GECODE_ME_CHECK(xs[i].le(home, static_cast<int>(yssize)));
85 }
86
87 (void) new (home) ChannelInt(home,xs,ys);
88 return ES_OK;
89 }
90
91 template<class View>
92 PropCost
93 ChannelInt<View>::cost(const Space&, const ModEventDelta&) const {
94 return PropCost::quadratic(PropCost::LO, xs.size()+ys.size());
95 }
96
97 template<class View>
98 void
99 ChannelInt<View>::reschedule(Space& home) {
100 xs.reschedule(home,*this, Gecode::Int::PC_INT_DOM);
101 ys.reschedule(home,*this, PC_SET_ANY);
102 }
103
104 template<class View>
105 forceinline size_t
106 ChannelInt<View>::dispose(Space& home) {
107 xs.cancel(home,*this, Gecode::Int::PC_INT_DOM);
108 ys.cancel(home,*this, PC_SET_ANY);
109 (void) Propagator::dispose(home);
110 return sizeof(*this);
111 }
112
113 template<class View>
114 Actor*
115 ChannelInt<View>::copy(Space& home) {
116 return new (home) ChannelInt(home,*this);
117 }
118
119 template<class View>
120 ExecStatus
121 ChannelInt<View>::propagate(Space& home, const ModEventDelta&) {
122 int assigned = 0;
123 for (int v=xs.size(); v--;) {
124 if (xs[v].assigned()) {
125 assigned++;
126 if (xs[v].modified())
127 GECODE_ME_CHECK(ys[xs[v].val()].include(home,v));
128 }
129 if (xs[v].modified()) {
130 Gecode::Int::ViewDiffRanges<Gecode::Int::IntView> d(xs[v]);
131 Iter::Ranges::ToValues<Gecode::Int::ViewDiffRanges<
132 Gecode::Int::IntView> > dv(d);
133 for (; dv(); ++dv)
134 GECODE_ME_CHECK(ys[dv.val()].exclude(home, v));
135 xs[v].cache(home);
136 }
137 }
138
139 for (int i=ys.size(); i--;) {
140 if (ys[i].glbModified()) {
141 GlbDiffRanges<View> yilb(ys[i]);
142 Iter::Ranges::ToValues<GlbDiffRanges<View> > dv(yilb);
143 for (;dv();++dv)
144 GECODE_ME_CHECK(xs[dv.val()].eq(home,i));
145 ys[i].cacheGlb(home);
146 }
147 if (ys[i].lubModified()) {
148 LubDiffRanges<View> yiub(ys[i]);
149 Iter::Ranges::ToValues<LubDiffRanges<View> > dv(yiub);
150 for (;dv();++dv)
151 GECODE_ME_CHECK(xs[dv.val()].nq(home,i));
152 ys[i].cacheLub(home);
153 }
154 }
155
156 return (assigned==xs.size()) ? home.ES_SUBSUMED(*this) : ES_NOFIX;
157 }
158
159}}}
160
161// STATISTICS: set-prop