File: int.hpp

package info (click to toggle)
gecode-snapshot 6.2.0%2Bgit20240207-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 35,308 kB
  • sloc: cpp: 475,516; perl: 2,077; makefile: 1,816; sh: 198
file content (362 lines) | stat: -rwxr-xr-x 9,410 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
 *  Main authors:
 *     Christian Schulte <schulte@gecode.org>
 *     Mikael Lagerkvist <lagerkvist@gecode.org>
 *
 *  Copyright:
 *     Christian Schulte, 2005
 *     Mikael Lagerkvist, 2006
 *
 *  This file is part of Gecode, the generic constraint
 *  development environment:
 *     http://www.gecode.org
 *
 *  Permission is hereby granted, free of charge, to any person obtaining
 *  a copy of this software and associated documentation files (the
 *  "Software"), to deal in the Software without restriction, including
 *  without limitation the rights to use, copy, modify, merge, publish,
 *  distribute, sublicense, and/or sell copies of the Software, and to
 *  permit persons to whom the Software is furnished to do so, subject to
 *  the following conditions:
 *
 *  The above copyright notice and this permission notice shall be
 *  included in all copies or substantial portions of the Software.
 *
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 */

namespace Test { namespace Int {

  /*
   * Assignments
   *
   */
  inline
  Assignment::Assignment(int n0, const Gecode::IntSet& d0)
    : n(n0), d(d0) {}
  inline int
  Assignment::size(void) const {
    return n;
  }
  inline
  Assignment::~Assignment(void) {}


  inline
  CpltAssignment::CpltAssignment(int n, const Gecode::IntSet& d)
    : Assignment(n,d),
      dsv(new Gecode::IntSetValues[static_cast<unsigned int>(n)]) {
    for (int i=n; i--; )
      dsv[i].init(d);
  }
  inline bool
  CpltAssignment::has_more(void) const {
    return dsv[0]();
  }
  inline int
  CpltAssignment::operator[](int i) const {
    assert((i>=0) && (i<n));
    return dsv[i].val();
  }
  inline
  CpltAssignment::~CpltAssignment(void) {
    delete [] dsv;
  }


  forceinline int
  RandomAssignment::randval(Gecode::Support::RandomGenerator& rand) {
    unsigned int skip = rand(d.size());
    for (Gecode::IntSetRanges it(d); true; ++it) {
      if (it.width() > skip)
        return it.min() + static_cast<int>(skip);
      skip -= it.width();
    }
    GECODE_NEVER;
    return 0;
  }

  inline
  RandomAssignment::RandomAssignment(int n, const Gecode::IntSet& d, int a0, Gecode::Support::RandomGenerator& rand)
    : Assignment(n,d), vals(new int[static_cast<size_t>(n)]), a(a0) {
    for (int i=n; i--; )
      vals[i] = randval(rand);
  }

  inline bool
  RandomAssignment::has_more(void) const {
    return a>0;
  }
  inline int
  RandomAssignment::operator[](int i) const {
    assert((i>=0) && (i<n));
    return vals[i];
  }
  inline
  RandomAssignment::~RandomAssignment(void) {
    delete [] vals;
  }

  forceinline int
  RandomMixAssignment::randval(const Gecode::IntSet& d, Gecode::Support::RandomGenerator& rand) {
    unsigned int skip = rand(d.size());
    for (Gecode::IntSetRanges it(d); true; ++it) {
      if (it.width() > skip)
        return it.min() + static_cast<int>(skip);
      skip -= it.width();
    }
    GECODE_NEVER;
    return 0;
  }

  inline
  RandomMixAssignment::RandomMixAssignment(int n0, const Gecode::IntSet& d0, int n1, const Gecode::IntSet& d1, int a0,
                                           Gecode::Support::RandomGenerator& rand)
    : Assignment(n0+n1,d0),vals(new int[static_cast<size_t>(n0+n1)]),
      a(a0),_n1(n1),_d1(d1) {
    for (int i=n0; i--; )
      vals[i] = randval(d, rand);
    for (int i=n1; i--; )
      vals[n0+i] = randval(_d1, rand);
  }

  inline bool
  RandomMixAssignment::has_more(void) const {
    return a>0;
  }

  inline int
  RandomMixAssignment::operator[](int i) const {
    assert((i>=0) && (i<n));
    return vals[i];
  }

  inline
  RandomMixAssignment::~RandomMixAssignment(void) {
    delete [] vals;
  }

  /*
   * Tests with integer constraints
   *
   */
  forceinline bool
  Test::eqv(void) const {
    return reified && ((rms & (1 << Gecode::RM_EQV)) != 0);
  }
  forceinline bool
  Test::imp(void) const {
    return reified && ((rms & (1 << Gecode::RM_IMP)) != 0);
  }
  forceinline bool
  Test::pmi(void) const {
    return reified && ((rms & (1 << Gecode::RM_PMI)) != 0);
  }
  inline
  Test::Test(const std::string& p, const std::string& s,
             int a, const Gecode::IntSet& d, bool r,
             Gecode::IntPropLevel i)
    : Base(p+s), arity(a), dom(d),
      reified(r), rms((1 << Gecode::RM_EQV) |
                      (1 << Gecode::RM_IMP) |
                      (1 << Gecode::RM_PMI)),
      ipl(i), contest(ipl == Gecode::IPL_DOM ? CTL_DOMAIN : CTL_NONE),
      testsearch(true), testfix(true) {}

  inline
  Test::Test(const std::string& s,
             int a, const Gecode::IntSet& d, bool r,
             Gecode::IntPropLevel i)
    : Base("Int::"+s), arity(a), dom(d),
      reified(r), rms((1 << Gecode::RM_EQV) |
                      (1 << Gecode::RM_IMP) |
                      (1 << Gecode::RM_PMI)),
      ipl(i), contest(ipl == Gecode::IPL_DOM ? CTL_DOMAIN : CTL_NONE),
      testsearch(true), testfix(true) {}

  inline
  Test::Test(const std::string& p, const std::string& s,
             int a, int min, int max, bool r,
             Gecode::IntPropLevel i)
    : Base(p+s), arity(a), dom(min,max),
      reified(r), rms((1 << Gecode::RM_EQV) |
                      (1 << Gecode::RM_IMP) |
                      (1 << Gecode::RM_PMI)),
      ipl(i), contest(ipl == Gecode::IPL_DOM ? CTL_DOMAIN : CTL_NONE),
      testsearch(true), testfix(true) {}

  inline
  Test::Test(const std::string& s,
             int a, int min, int max, bool r, Gecode::IntPropLevel i)
    : Base("Int::"+s), arity(a), dom(min,max),
      reified(r), rms((1 << Gecode::RM_EQV) |
                      (1 << Gecode::RM_IMP) |
                      (1 << Gecode::RM_PMI)),
      ipl(i), contest(ipl == Gecode::IPL_DOM ? CTL_DOMAIN : CTL_NONE),
      testsearch(true), testfix(true) {}

  inline
  std::string
  Test::str(Gecode::IntPropLevel ipl) {
    using namespace Gecode;
    std::stringstream s;
    switch (vbd(ipl)) {
    case IPL_VAL: s << "Val"; break;
    case IPL_BND: s << "Bnd"; break;
    case IPL_DOM: s << "Dom"; break;
    default: s << "Def"; break;
    }
    if (ipl & IPL_BASIC) s << "+B";
    if (ipl & IPL_ADVANCED) s << "+A";
    return s.str();
  }

  inline
  std::string
  Test::str(Gecode::IntRelType irt) {
    using namespace Gecode;
    switch (irt) {
    case IRT_LQ: return "Lq";
    case IRT_LE: return "Le";
    case IRT_GQ: return "Gq";
    case IRT_GR: return "Gr";
    case IRT_EQ: return "Eq";
    case IRT_NQ: return "Nq";
    default: ;
    }
    GECODE_NEVER;
    return "NONE";
  }

  inline std::string
  Test::str(Gecode::BoolOpType bot) {
    using namespace Gecode;
    switch (bot) {
    case BOT_AND: return "And";
    case BOT_OR:  return "Or";
    case BOT_IMP: return "Imp";
    case BOT_EQV: return "Eqv";
    case BOT_XOR: return "Xor";
    default: GECODE_NEVER;
    }
    GECODE_NEVER;
    return "NONE";
  }

  inline std::string
  Test::str(bool b) {
    return Base::str(b);
  }

  inline std::string
  Test::str(int i) {
    return Base::str(i);
  }

  inline std::string
  Test::str(const Gecode::IntArgs& x) {
    return Base::str(x);
  }



  template<class T>
  inline bool
  Test::cmp(T x, Gecode::IntRelType r, T y) {
    using namespace Gecode;
    switch (r) {
    case IRT_EQ: return x == y;
    case IRT_NQ: return x != y;
    case IRT_LQ: return x <= y;
    case IRT_LE: return x < y;
    case IRT_GR: return x > y;
    case IRT_GQ: return x >= y;
    default: ;
    }
    return false;
  }



  inline
  IntPropLevels::IntPropLevels(void)
    : i(sizeof(ipls)/sizeof(Gecode::IntPropLevel)-1) {}
  inline bool
  IntPropLevels::operator()(void) const {
    return i>=0;
  }
  inline void
  IntPropLevels::operator++(void) {
    i--;
  }
  inline Gecode::IntPropLevel
  IntPropLevels::ipl(void) const {
    return ipls[i];
  }


  inline
  IntPropBasicAdvanced::IntPropBasicAdvanced(void)
    : i(sizeof(ipls)/sizeof(Gecode::IntPropLevel)-1) {}
  inline bool
  IntPropBasicAdvanced::operator()(void) const {
    return i>=0;
  }
  inline void
  IntPropBasicAdvanced::operator++(void) {
    i--;
  }
  inline Gecode::IntPropLevel
  IntPropBasicAdvanced::ipl(void) const {
    return ipls[i];
  }


  inline
  IntRelTypes::IntRelTypes(void)
    : i(sizeof(irts)/sizeof(Gecode::IntRelType)-1) {}
  inline void
  IntRelTypes::reset(void) {
    i = sizeof(irts)/sizeof(Gecode::IntRelType)-1;
  }
  inline bool
  IntRelTypes::operator()(void) const {
    return i>=0;
  }
  inline void
  IntRelTypes::operator++(void) {
    i--;
  }
  inline Gecode::IntRelType
  IntRelTypes::irt(void) const {
    return irts[i];
  }

  inline
  BoolOpTypes::BoolOpTypes(void)
    : i(sizeof(bots)/sizeof(Gecode::BoolOpType)-1) {}
  inline bool
  BoolOpTypes::operator()(void) const {
    return i>=0;
  }
  inline void
  BoolOpTypes::operator++(void) {
    i--;
  }
  inline Gecode::BoolOpType
  BoolOpTypes::bot(void) const {
    return bots[i];
  }

}}

// STATISTICS: test-int