~ubuntu-branches/ubuntu/natty/gecode/natty

« back to all changes in this revision

Viewing changes to set/view/singleton.icc

  • Committer: Bazaar Package Importer
  • Author(s): Kari Pahula
  • Date: 2005-12-24 07:51:25 UTC
  • Revision ID: james.westby@ubuntu.com-20051224075125-klkiqofvbfvusfvt
Tags: upstream-1.0.0.dfsg.1
ImportĀ upstreamĀ versionĀ 1.0.0.dfsg.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  Main authors:
 
3
 *     Guido Tack <tack@gecode.org>
 
4
 *
 
5
 *  Contributing authors:
 
6
 *     Christian Schulte <schulte@gecode.org>
 
7
 *
 
8
 *  Copyright:
 
9
 *     Guido Tack, 2004
 
10
 *     Christian Schulte, 2004
 
11
 *
 
12
 *  Last modified:
 
13
 *     $Date: 2005-11-29 18:02:48 +0100 (Tue, 29 Nov 2005) $ by $Author: tack $
 
14
 *     $Revision: 2665 $
 
15
 *
 
16
 *  This file is part of Gecode, the generic constraint
 
17
 *  development environment:
 
18
 *     http://www.gecode.org
 
19
 *
 
20
 *  See the file "LICENSE" for information on usage and
 
21
 *  redistribution of this file, and for a
 
22
 *     DISCLAIMER OF ALL WARRANTIES.
 
23
 *
 
24
 */
 
25
 
 
26
namespace Gecode { 
 
27
 
 
28
  namespace Set {
 
29
 
 
30
    forceinline
 
31
    SingletonView::SingletonView(void) {}
 
32
 
 
33
    forceinline
 
34
    SingletonView::SingletonView(Gecode::Int::IntView& i0)
 
35
      : DerivedViewBase<Gecode::Int::IntView>(i0) {}
 
36
 
 
37
    forceinline PropCond
 
38
    SingletonView::pc_settoint(PropCond pc) {
 
39
      switch(pc) {
 
40
      case PC_SET_VAL:
 
41
      case PC_SET_CGLB:
 
42
      case PC_SET_CARD:
 
43
        return Gecode::Int::PC_INT_VAL;
 
44
      default:
 
45
        return Gecode::Int::PC_INT_DOM;
 
46
      }
 
47
    }
 
48
 
 
49
    forceinline ModEvent
 
50
    SingletonView::me_inttoset(ModEvent me) {
 
51
      switch(me) {
 
52
      case Gecode::Int::ME_INT_FAILED: 
 
53
        return ME_SET_FAILED;
 
54
      case Gecode::Int::ME_INT_NONE: 
 
55
        return ME_SET_NONE;
 
56
      case Gecode::Int::ME_INT_VAL: 
 
57
        return ME_SET_VAL;
 
58
      case Gecode::Int::ME_INT_DOM: 
 
59
        return ME_SET_LUB;
 
60
      default:
 
61
        return ME_SET_LUB;
 
62
      }
 
63
    }
 
64
 
 
65
    forceinline bool
 
66
    SingletonView::assigned(void) const { return view.assigned(); }
 
67
 
 
68
    forceinline unsigned int
 
69
    SingletonView::glbSize(void) const { return view.assigned() ? 1 : 0; }
 
70
 
 
71
    forceinline unsigned int
 
72
    SingletonView::lubSize(void) const { return view.size(); }
 
73
 
 
74
    forceinline unsigned int
 
75
    SingletonView::unknownSize(void) const {
 
76
      return lubSize() - glbSize();
 
77
    }
 
78
 
 
79
    forceinline bool
 
80
    SingletonView::contains(int n) const { return view.assigned() ?
 
81
                                             (view.val()==n) : false; }
 
82
 
 
83
    forceinline bool
 
84
    SingletonView::notContains(int n) const { return !view.in(n); }
 
85
 
 
86
    forceinline unsigned int
 
87
    SingletonView::cardMin() const { return 1; }
 
88
 
 
89
    forceinline unsigned int
 
90
    SingletonView::cardMax() const { return 1; }
 
91
 
 
92
    forceinline int
 
93
    SingletonView::lubMin() const { return view.min(); }
 
94
 
 
95
    forceinline int
 
96
    SingletonView::lubMax() const { return view.max(); }
 
97
 
 
98
    forceinline int
 
99
    SingletonView::glbMin() const { return view.assigned() ?
 
100
                                      view.val() : BndSet::MIN_OF_EMPTY; }
 
101
 
 
102
    forceinline int
 
103
    SingletonView::glbMax() const { return view.assigned() ?
 
104
                                      view.val() : BndSet::MAX_OF_EMPTY; }
 
105
 
 
106
    forceinline ModEvent
 
107
    SingletonView::cardMin(Space* home,unsigned int c) {
 
108
      return c<=1 ? ME_SET_NONE : ME_SET_FAILED;
 
109
    }
 
110
 
 
111
    forceinline ModEvent
 
112
    SingletonView::cardMax(Space* home,unsigned int c) {
 
113
      return c<1 ? ME_SET_FAILED : ME_SET_NONE;
 
114
    }
 
115
 
 
116
    forceinline ModEvent
 
117
    SingletonView::include(Space* home,int c) { 
 
118
      return me_inttoset(view.eq(home,c)); 
 
119
    }
 
120
 
 
121
    forceinline ModEvent
 
122
    SingletonView::intersect(Space* home,int c) { 
 
123
      return me_inttoset(view.eq(home,c)); 
 
124
    }
 
125
 
 
126
    forceinline ModEvent
 
127
    SingletonView::intersect(Space* home,int i, int j) { 
 
128
      ModEvent me1 = me_inttoset(view.gq(home,i));
 
129
      ModEvent me2 = me_inttoset(view.lq(home,j));
 
130
      if (me_failed(me1) || me_failed(me2))
 
131
        return ME_SET_FAILED;
 
132
      switch (me1) {
 
133
      case ME_SET_NONE:
 
134
      case ME_SET_LUB:
 
135
        return me2;
 
136
      case ME_SET_VAL:
 
137
        return ME_SET_VAL;
 
138
      default:
 
139
        assert(false);
 
140
        return ME_SET_VAL;
 
141
      }
 
142
    }
 
143
 
 
144
    forceinline ModEvent
 
145
    SingletonView::exclude(Space* home,int c) { 
 
146
      return me_inttoset(view.nq(home,c)); 
 
147
    }
 
148
 
 
149
    forceinline ModEvent
 
150
    SingletonView::include(Space* home, int j, int k) {
 
151
      return j==k ? me_inttoset(view.eq(home,j)) : ME_SET_FAILED ;
 
152
    }
 
153
 
 
154
    forceinline ModEvent
 
155
    SingletonView::exclude(Space* home, int j, int k) {
 
156
      ModEvent me1 = me_inttoset(view.gr(home,j));
 
157
      ModEvent me2 = me_inttoset(view.le(home,k));
 
158
      if (me_failed(me1) || me_failed(me2))
 
159
        return ME_SET_FAILED;
 
160
      switch (me1) {
 
161
      case ME_SET_NONE:
 
162
      case ME_SET_LUB:
 
163
        return me2;
 
164
      case ME_SET_VAL:
 
165
        return ME_SET_VAL;
 
166
      default:
 
167
        assert(false);
 
168
        return ME_SET_VAL;
 
169
      }
 
170
    }
 
171
 
 
172
    template <class I> ModEvent
 
173
    SingletonView::excludeI(Space* home, I& iter) {
 
174
      return me_inttoset(view.minus(home,iter));
 
175
    }
 
176
 
 
177
    template <class I> ModEvent
 
178
    SingletonView::includeI(Space* home, I& iter) {
 
179
      if (!iter())
 
180
        return ME_SET_NONE;
 
181
 
 
182
      if (iter.min()!=iter.max())
 
183
        return ME_SET_FAILED;
 
184
 
 
185
      int val = iter.min();
 
186
      ++iter;
 
187
      if ( iter() )
 
188
        return ME_SET_FAILED;
 
189
 
 
190
      return me_inttoset(view.eq(home, val));
 
191
    }
 
192
 
 
193
    template <class I> ModEvent
 
194
    SingletonView::intersectI(Space* home, I& iter) {
 
195
      return me_inttoset(view.inter(home,iter));
 
196
    }
 
197
 
 
198
    forceinline void
 
199
    SingletonView::subscribe(Space* home, Propagator* p, PropCond pc) {
 
200
      view.subscribe(home,p,pc_settoint(pc));
 
201
    }
 
202
    forceinline void
 
203
    SingletonView::cancel(Propagator* p, PropCond pc) {
 
204
      view.cancel(p,pc_settoint(pc));
 
205
    }
 
206
 
 
207
 
 
208
    forceinline ModEvent
 
209
    SingletonView::pme(const Propagator* p) {
 
210
      return me_inttoset(Int::IntView::pme(p));
 
211
    }
 
212
    forceinline PropModEvent
 
213
    SingletonView::pme(ModEvent me) {
 
214
      // CHECK THIS
 
215
      return SetView::pme(me);
 
216
    }
 
217
 
 
218
    forceinline void
 
219
    SingletonView::update(Space* home, bool share, SingletonView& y) {
 
220
      view.update(home,share,y.view);
 
221
    }
 
222
 
 
223
    forceinline bool
 
224
    SingletonView::destruct(void) { return false; }
 
225
 
 
226
    /*
 
227
     * Iterators
 
228
     *
 
229
     */
 
230
 
 
231
    /**
 
232
     * \brief %Range iterator for least upper bound of singleton set view
 
233
     * \ingroup TaskActorSetView
 
234
     */
 
235
    template <>
 
236
    class LubRanges<SingletonView> : public Gecode::Int::IntVarImpFwd {
 
237
    public:
 
238
      /// \name Constructors and initialization
 
239
      //@{
 
240
      /// Default constructor
 
241
      LubRanges(void);
 
242
      /// Initialize with ranges for view \a x
 
243
      LubRanges(const SingletonView& x);
 
244
      /// Initialize with ranges for view \a x
 
245
      void init(const SingletonView& x);
 
246
      //@}
 
247
    };
 
248
 
 
249
    forceinline
 
250
    LubRanges<SingletonView>::LubRanges(void) {}
 
251
 
 
252
    forceinline
 
253
    LubRanges<SingletonView>::LubRanges(const SingletonView& s) :
 
254
      Gecode::Int::IntVarImpFwd(s.base().variable()) {}
 
255
 
 
256
    forceinline void
 
257
    LubRanges<SingletonView>::init(const SingletonView& s) {
 
258
      Gecode::Int::IntVarImpFwd::init(s.base().variable());
 
259
    }
 
260
 
 
261
    /**
 
262
     * \brief %Range iterator for greatest lower bound of singleton set view
 
263
     * \ingroup TaskActorSetView
 
264
     */
 
265
    template <>
 
266
    class GlbRanges<SingletonView> {
 
267
    private:
 
268
      int  val;
 
269
      bool flag;
 
270
    public:
 
271
      /// \name Constructors and initialization
 
272
      //@{
 
273
      /// Default constructor
 
274
      GlbRanges(void);
 
275
      /// Initialize with ranges for view \a x
 
276
      GlbRanges(const SingletonView& x);
 
277
      /// Initialize with ranges for view \a x
 
278
      void init(const SingletonView& x);
 
279
 
 
280
      /// \name Iteration control
 
281
      //@{
 
282
      /// Test whether iterator is still at a range or done
 
283
      bool operator()(void) const;
 
284
      /// Move iterator to next range (if possible)
 
285
      void operator++(void);
 
286
      //@}
 
287
 
 
288
      /// \name Range access
 
289
      //@{
 
290
      /// Return smallest value of range
 
291
      int min(void) const;
 
292
      /// Return largest value of range
 
293
      int max(void) const;
 
294
      /// Return width of ranges (distance between minimum and maximum)
 
295
      unsigned int width(void) const;
 
296
      //@}
 
297
    };
 
298
 
 
299
    forceinline
 
300
    GlbRanges<SingletonView>::GlbRanges(void) {}
 
301
 
 
302
    forceinline void
 
303
    GlbRanges<SingletonView>::init(const SingletonView& s) {
 
304
      if (s.base().assigned()) {
 
305
        val = s.base().val();
 
306
        flag = true;
 
307
      } else {
 
308
        val = 0;
 
309
        flag = false;
 
310
      }
 
311
    }
 
312
 
 
313
    forceinline
 
314
    GlbRanges<SingletonView>::GlbRanges(const SingletonView& s) {
 
315
      init(s);
 
316
    }
 
317
 
 
318
    forceinline bool
 
319
    GlbRanges<SingletonView>::operator()(void) const { return flag; }
 
320
 
 
321
    forceinline void
 
322
    GlbRanges<SingletonView>::operator++(void) { flag=false; }
 
323
 
 
324
    forceinline int
 
325
    GlbRanges<SingletonView>::min(void) const { return val; }
 
326
    forceinline int
 
327
    GlbRanges<SingletonView>::max(void) const { return val; }
 
328
    forceinline unsigned int
 
329
    GlbRanges<SingletonView>::width(void) const { return 1; }
 
330
 
 
331
  }
 
332
 
 
333
  /*
 
334
   * Testing
 
335
   *
 
336
   */
 
337
  forceinline bool
 
338
  same(const Set::SingletonView& x, const Set::SingletonView& y) {
 
339
    return same(x.base(),y.base());
 
340
  }
 
341
  forceinline bool
 
342
  before(const Set::SingletonView& x, const Set::SingletonView& y) {
 
343
    return before(x.base(),y.base());
 
344
  }
 
345
 
 
346
 
 
347
}
 
348
 
 
349
// STATISTICS: set-var
 
350