File: int.hpp

package info (click to toggle)
gecode 3.4.0-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 10,136 kB
  • ctags: 16,905
  • sloc: cpp: 124,581; perl: 2,004; makefile: 1,453; sh: 247
file content (314 lines) | stat: -rwxr-xr-x 7,684 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
/* -*- 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
 *
 *  Last modified:
 *     $Date: 2010-06-03 13:11:11 +0200 (Thu, 03 Jun 2010) $ by $Author: tack $
 *     $Revision: 11013 $
 *
 *  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::operator()(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(void) {
    unsigned int skip = Base::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)
    : Assignment(n,d), vals(new int[n]), a(a0) {
    for (int i=n; i--; )
      vals[i] = randval();
  }

  inline bool
  RandomAssignment::operator()(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) {
    unsigned int skip = Base::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)
    : Assignment(n0+n1,d0),vals(new int[n0+n1]),a(a0),_n1(n1),_d1(d1) {
    for (int i=n0; i--; )
      vals[i] = randval(d);
    for (int i=n1; i--; )
      vals[n0+i] = randval(_d1);
  }

  inline bool
  RandomMixAssignment::operator()(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
   *
   */
  inline
  Test::Test(const std::string& s, int a, const Gecode::IntSet& d,
             bool r, Gecode::IntConLevel i)
    : Base("Int::"+s), arity(a), dom(d), reified(r), icl(i),
      contest(icl == Gecode::ICL_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::IntConLevel i)
    : Base("Int::"+s), arity(a), dom(min,max), reified(r), icl(i),
      contest(icl == Gecode::ICL_DOM ? CTL_DOMAIN : CTL_NONE),
      testsearch(true), testfix(true) {}

  inline
  std::string
  Test::str(Gecode::ExtensionalPropKind epk) {
    using namespace Gecode;
    switch (epk) {
    case EPK_MEMORY: return "Memory";
    case EPK_SPEED:  return "Speed";
    default: return "Def";
    }
  }

  inline
  std::string
  Test::str(Gecode::IntConLevel icl) {
    using namespace Gecode;
    switch (icl) {
    case ICL_VAL: return "Val";
    case ICL_BND: return "Bnd";
    case ICL_DOM: return "Dom";
    default: return "Def";
    }
  }

  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(int i) {
    std::stringstream s;
    s << i;
    return s.str();
  }

  inline
  std::string
  Test::str(const Gecode::IntArgs& x) {
    std::string s = "";
    for (int i=0; i<x.size()-1; i++)
      s += str(x[i]) + ",";
    return "[" + s + str(x[x.size()-1]) + "]";
  }

  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
  IntConLevels::IntConLevels(void)
    : i(sizeof(icls)/sizeof(Gecode::IntConLevel)-1) {}
  inline bool
  IntConLevels::operator()(void) const {
    return i>=0;
  }
  inline void
  IntConLevels::operator++(void) {
    i--;
  }
  inline Gecode::IntConLevel
  IntConLevels::icl(void) const {
    return icls[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