Generated on Sat Jun 2 2018 07:17:44 for Gecode by doxygen 1.8.13
int-set-1.hpp
Go to the documentation of this file.
1 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2 /*
3  * Main authors:
4  * Christian Schulte <schulte@gecode.org>
5  *
6  * Copyright:
7  * Christian Schulte, 2003
8  *
9  * This file is part of Gecode, the generic constraint
10  * development environment:
11  * http://www.gecode.org
12  *
13  * Permission is hereby granted, free of charge, to any person obtaining
14  * a copy of this software and associated documentation files (the
15  * "Software"), to deal in the Software without restriction, including
16  * without limitation the rights to use, copy, modify, merge, publish,
17  * distribute, sublicense, and/or sell copies of the Software, and to
18  * permit persons to whom the Software is furnished to do so, subject to
19  * the following conditions:
20  *
21  * The above copyright notice and this permission notice shall be
22  * included in all copies or substantial portions of the Software.
23  *
24  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31  *
32  */
33 
34 #include <sstream>
35 
36 namespace Gecode {
37 
38  /*
39  * Integer sets
40  *
41  */
43  IntSet::IntSet(void) {}
44 
52  template<class I>
53  class IntSetInit {
54  public:
56  static void init(IntSet& s, I& i) {
58  int n=0;
59  unsigned int size = 0;
60  while (i()) {
61  d[n].min = i.min(); d[n].max = i.max(); size += i.width();
62  ++n; ++i;
63  }
64  if (n > 0) {
65  IntSet::IntSetObject* o = IntSet::IntSetObject::allocate(n);
66  for (int j=n; j--; )
67  o->r[j]=d[j];
68  o->size = size;
69  s.object(o);
70  }
71  }
72  };
73 
75  template<>
76  class IntSetInit<IntSet> {
77  public:
78  static void init(IntSet& s, const IntSet& i) {
79  s.object(i.object());
80  }
81  };
82 
84  template<class I>
86  IntSetInit<I>::init(*this,i);
87  }
88 
90  template<class I>
91  IntSet::IntSet(const I& i) {
92  IntSetInit<I>::init(*this,i);
93  }
94 
96  IntSet::IntSet(const int r[][2], int n) {
97  init(r,n);
98  }
99 
101  IntSet::IntSet(const int r[], int n) {
102  init(r,n);
103  }
104 
106  IntSet::IntSet(int n, int m) {
107  init(n,m);
108  }
109 
110  forceinline int
111  IntSet::min(int i) const {
112  assert(object() != NULL);
113  return static_cast<IntSetObject*>(object())->r[i].min;
114  }
115 
116  forceinline int
117  IntSet::max(int i) const {
118  assert(object() != NULL);
119  return static_cast<IntSetObject*>(object())->r[i].max;
120  }
121 
122  forceinline unsigned int
123  IntSet::width(int i) const {
124  assert(object() != NULL);
125  IntSetObject* o = static_cast<IntSetObject*>(object());
126  return static_cast<unsigned int>(o->r[i].max-o->r[i].min)+1;
127  }
128 
129  forceinline int
130  IntSet::ranges(void) const {
131  IntSetObject* o = static_cast<IntSetObject*>(object());
132  return (o == NULL) ? 0 : o->n;
133  }
134 
135  forceinline bool
136  IntSet::in(int n) const {
137  IntSetObject* o = static_cast<IntSetObject*>(object());
138  if ((o == NULL) || (n < o->r[0].min) || (n > o->r[o->n-1].max))
139  return false;
140  else
141  return o->in(n);
142  }
143 
144  forceinline int
145  IntSet::min(void) const {
146  IntSetObject* o = static_cast<IntSetObject*>(object());
147  return (o == NULL) ? Int::Limits::max : o->r[0].min;
148  }
149 
150  forceinline int
151  IntSet::max(void) const {
152  IntSetObject* o = static_cast<IntSetObject*>(object());
153  return (o == NULL) ? Int::Limits::min : o->r[o->n-1].max;
154  }
155 
156  forceinline unsigned int
157  IntSet::size(void) const {
158  IntSetObject* o = static_cast<IntSetObject*>(object());
159  return (o == NULL) ? 0 : o->size;
160  }
161 
162  forceinline unsigned int
163  IntSet::width(void) const {
164  return static_cast<unsigned int>(max()-min()+1);
165  }
166 
167 
168  /*
169  * Range iterator for integer sets
170  *
171  */
172 
176  void
178  int n = s.ranges();
179  if (n > 0) {
180  i = &static_cast<IntSet::IntSetObject*>(s.object())->r[0]; e = i+n;
181  } else {
182  i = e = NULL;
183  }
184  }
186  IntSetRanges::IntSetRanges(const IntSet& s) { init(s); }
187 
188 
189  forceinline void
191  i++;
192  }
193  forceinline bool
195  return i<e;
196  }
197 
198  forceinline int
199  IntSetRanges::min(void) const {
200  return i->min;
201  }
202  forceinline int
203  IntSetRanges::max(void) const {
204  return i->max;
205  }
206  forceinline unsigned int
207  IntSetRanges::width(void) const {
208  return static_cast<unsigned int>(i->max - i->min) + 1;
209  }
210 
211  /*
212  * Value iterator for integer sets
213  *
214  */
217 
220  IntSetRanges r(s);
222  }
223 
224  forceinline void
226  IntSetRanges r(s);
228  }
229 
230  template<class Char, class Traits>
231  std::basic_ostream<Char,Traits>&
232  operator <<(std::basic_ostream<Char,Traits>& os, const IntSet& is) {
233  std::basic_ostringstream<Char,Traits> s;
234  s.copyfmt(os); s.width(0);
235  s << '{';
236  for (int i = 0; i < is.ranges(); ) {
237  int min = is.min(i);
238  int max = is.max(i);
239  if (min == max)
240  s << min;
241  else
242  s << min << ".." << max;
243  i++;
244  if (i < is.ranges())
245  s << ',';
246  }
247  s << '}';
248  return os << s.str();
249  }
250 
251 }
252 
253 // STATISTICS: int-var
254 
int max(void) const
Return maximum of entire set.
Definition: int-set-1.hpp:151
void init(I &i)
Initialize with values from range iterator i.
Range iterator for integer sets.
Definition: int.hh:268
IntSetValues(void)
Default constructor.
Definition: int-set-1.hpp:216
void operator++(void)
Move iterator to next range (if possible)
Definition: int-set-1.hpp:190
unsigned int width(int i) const
Return width of range at position i.
Definition: int-set-1.hpp:123
void init(const IntSet &s)
Initialize with values for s.
Definition: int-set-1.hpp:225
Array with arbitrary number of elements.
bool operator()(void) const
Test whether iterator is still at a range or done.
Definition: int-set-1.hpp:194
#define forceinline
Definition: config.hpp:185
const int max
Largest allowed integer value.
Definition: int.hh:112
void init(const IntSet &s)
Initialize with ranges for set s.
Definition: int-set-1.hpp:177
const int min
Smallest allowed integer value.
Definition: int.hh:114
static void init(IntSet &s, const IntSet &i)
Definition: int-set-1.hpp:78
Gecode::IntSet d(v, 7)
Gecode::IntArgs i(4, 1, 2, 3, 4)
int n
Number of negative literals for node type.
Definition: bool-expr.cpp:234
static void init(IntSet &s, I &i)
Initialize s with iterator i.
Definition: int-set-1.hpp:56
SharedHandle::Object * object(void) const
Access to the shared object.
unsigned int size(void) const
Return size (cardinality) of set.
Definition: int-set-1.hpp:157
Integer set initialization.
Definition: int-set-1.hpp:53
Integer sets.
Definition: int.hh:170
unsigned int width(void) const
Return width of range (distance between minimum and maximum)
Definition: int-set-1.hpp:207
unsigned int width(void) const
Return width of set (distance between maximum and minimum)
Definition: int-set-1.hpp:163
IntSet(void)
Initialize as empty set.
Definition: int-set-1.hpp:43
Post propagator for SetVar SetOpType SetVar SetRelType r
Definition: set.hh:765
int min(void) const
Return smallest value of range.
Definition: int-set-1.hpp:199
int min(void) const
Return minimum of entire set.
Definition: int-set-1.hpp:145
Heap heap
The single global heap.
Definition: heap.cpp:44
int ranges(void) const
Return number of ranges of the specification.
Definition: int-set-1.hpp:130
int max(void) const
Return largest value of range.
Definition: int-set-1.hpp:203
Gecode toplevel namespace
bool in(int n) const
Return whether n is included in the set.
Definition: int-set-1.hpp:136
IntSetRanges(void)
Default constructor.
Definition: int-set-1.hpp:174