Generated on for Gecode by doxygen 1.17.0
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.dev>
5 *
6 * Contributing authors:
7 * Mikael Zayenz Lagerkvist <lagerkvist@gecode.dev>
8 *
9 * Copyright:
10 * Christian Schulte, 2003
11 * Mikael Zayenz Lagerkvist, 2026
12 *
13 * This file is part of Gecode, the generic constraint
14 * development environment:
15 * http://www.gecode.dev
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 <sstream>
39
40namespace Gecode {
41
42 /*
43 * Integer sets
44 *
45 */
46 forceinline
48
56 template<class I>
57 class IntSetInit {
58 public:
60 static void init(IntSet& s, I& i) {
61 Region reg;
63 int n=0;
64 unsigned int size = 0;
65 while (i()) {
66 d[n].min = i.min(); d[n].max = i.max(); size += i.width();
67 ++n; ++i;
68 }
69 if (n > 0) {
70 IntSet::IntSetObject* o = IntSet::IntSetObject::allocate(n);
71 for (int j=0; j<n; j++)
72 o->r[j]=d[j];
73 o->size = size;
74 s.object(o);
75 }
76 }
77 };
78
80 template<>
82 public:
83 static void init(IntSet& s, const IntSet& i) {
84 s.object(i.object());
85 }
86 };
87
89 template<class I>
91 IntSetInit<I>::init(*this,i);
92 }
93
95 template<class I>
96 IntSet::IntSet(const I& i) {
97 IntSetInit<I>::init(*this,i);
98 }
99
100 forceinline
101 IntSet::IntSet(const int r[][2], int n) {
102 if (n > 0)
103 init(r,n);
104 }
105
106 forceinline
107 IntSet::IntSet(const int r[], int n) {
108 if (n > 0)
109 init(r,n);
110 }
111
113 template<>
114 inline
115 IntSet::IntSet(const std::vector<int>& r) {
116 int n = static_cast<int>(r.size());
117 if (n > 0) {
118 Region reg;
119 Range* dr = reg.alloc<Range>(n);
120 for (int i=0; i<n; i++)
121 dr[i].min=dr[i].max=r[static_cast<unsigned int>(i)];
122 normalize(&dr[0],n);
123 }
124 }
125
131 template<>
132 inline
133 IntSet::IntSet(const std::vector<std::pair<int,int>>& r) {
134 int n = static_cast<int>(r.size());
135 if (n > 0) {
136 Region reg;
137 Range* dr = reg.alloc<Range>(n);
138 int j=0;
139 for (int i=0; i<n; i++)
140 if (r[static_cast<unsigned int>(i)].first <=
141 r[static_cast<unsigned int>(i)].second) {
142 dr[j].min=r[static_cast<unsigned int>(i)].first;
143 dr[j].max=r[static_cast<unsigned int>(i)].second;
144 j++;
145 }
146 normalize(&dr[0],j);
147 }
148 }
149
150 forceinline
151 IntSet::IntSet(int n, int m) {
152 init(n,m);
153 }
154
155 forceinline int
156 IntSet::min(int i) const {
157 assert(object() != nullptr);
158 return static_cast<IntSetObject*>(object())->r[i].min;
159 }
160
161 forceinline int
162 IntSet::max(int i) const {
163 assert(object() != nullptr);
164 return static_cast<IntSetObject*>(object())->r[i].max;
165 }
166
167 forceinline unsigned int
168 IntSet::width(int i) const {
169 assert(object() != nullptr);
170 IntSetObject* o = static_cast<IntSetObject*>(object());
171 return static_cast<unsigned int>(o->r[i].max) -
172 static_cast<unsigned int>(o->r[i].min) + 1U;
173 }
174
175 forceinline int
176 IntSet::ranges(void) const {
177 IntSetObject* o = static_cast<IntSetObject*>(object());
178 return (o == nullptr) ? 0 : o->n;
179 }
180
181 forceinline bool
182 IntSet::in(int n) const {
183 IntSetObject* o = static_cast<IntSetObject*>(object());
184 if ((o == nullptr) || (n < o->r[0].min) || (n > o->r[o->n-1].max))
185 return false;
186 else
187 return o->in(n);
188 }
189
190 forceinline int
191 IntSet::min(void) const {
192 IntSetObject* o = static_cast<IntSetObject*>(object());
193 return (o == nullptr) ? Int::Limits::max : o->r[0].min;
194 }
195
196 forceinline int
197 IntSet::max(void) const {
198 IntSetObject* o = static_cast<IntSetObject*>(object());
199 return (o == nullptr) ? Int::Limits::min : o->r[o->n-1].max;
200 }
201
202 forceinline unsigned int
203 IntSet::size(void) const {
204 IntSetObject* o = static_cast<IntSetObject*>(object());
205 return (o == nullptr) ? 0U : o->size;
206 }
207
208 forceinline unsigned int
209 IntSet::width(void) const {
210 IntSetObject* o = static_cast<IntSetObject*>(object());
211 return (o == nullptr) ? 0U :
212 static_cast<unsigned int>(max()) - static_cast<unsigned int>(min()) + 1U;
213 }
214
215 forceinline bool
216 IntSet::operator ==(const IntSet& s) const {
217 IntSetObject* o1 = static_cast<IntSetObject*>(object());
218 IntSetObject* o2 = static_cast<IntSetObject*>(s.object());
219 if (o1 == o2)
220 return true;
221 if ((o1 == nullptr) || (o2 == nullptr))
222 return false;
223 if ((o1->size != o2->size) || (o1->n != o2->n))
224 return false;
225 return o1->equal(*o2);
226 }
227
228 forceinline bool
229 IntSet::operator !=(const IntSet& s) const {
230 return !(*this == s);
231 }
232
233
234 /*
235 * Range iterator for integer sets
236 *
237 */
238
239 forceinline
241 forceinline
242 void
244 int n = s.ranges();
245 if (n > 0) {
246 i = &static_cast<IntSet::IntSetObject*>(s.object())->r[0]; e = i+n;
247 } else {
248 i = e = nullptr;
249 }
250 }
251 forceinline
253
254
255 forceinline void
257 i++;
258 }
259 forceinline bool
261 return i<e;
262 }
263
264 forceinline int
265 IntSetRanges::min(void) const {
266 return i->min;
267 }
268 forceinline int
269 IntSetRanges::max(void) const {
270 return i->max;
271 }
272 forceinline unsigned int
274 return static_cast<unsigned int>(i->max) -
275 static_cast<unsigned int>(i->min) + 1U;
276 }
277
278 /*
279 * Value iterator for integer sets
280 *
281 */
282 forceinline
284
285 forceinline
290
291 forceinline void
296
297 template<class Char, class Traits>
298 std::basic_ostream<Char,Traits>&
299 operator <<(std::basic_ostream<Char,Traits>& os, const IntSet& is) {
300 std::basic_ostringstream<Char,Traits> s;
301 s.copyfmt(os); s.width(0);
302 s << '{';
303 for (int i = 0; i < is.ranges(); ) {
304 int min = is.min(i);
305 int max = is.max(i);
306 if (min == max)
307 s << min;
308 else
309 s << min << ".." << max;
310 i++;
311 if (i < is.ranges())
312 s << ',';
313 }
314 s << '}';
315 return os << s.str();
316 }
317
318}
319
320// STATISTICS: int-var
static void init(IntSet &s, const IntSet &i)
Definition int-set-1.hpp:83
Integer set initialization.
Definition int-set-1.hpp:57
static void init(IntSet &s, I &i)
Initialize s with iterator i.
Definition int-set-1.hpp:60
Range iterator for integer sets.
Definition int.hh:310
unsigned int width(void) const
Return width of range (distance between minimum and maximum).
bool operator()(void) const
Test whether iterator is still at a range or done.
int max(void) const
Return largest value of range.
void operator++(void)
Move iterator to next range (if possible).
void init(const IntSet &s)
Initialize with ranges for set s.
int min(void) const
Return smallest value of range.
IntSetRanges(void)
Default constructor.
void init(const IntSet &s)
Initialize with values for s.
IntSetValues(void)
Default constructor.
Integer sets.
Definition int.hh:178
int min(void) const
Return minimum of entire set.
unsigned int width(void) const
Return width of set (distance between maximum and minimum).
int min(int i) const
Return minimum of range at position i.
bool in(int n) const
Return whether n is included in the set.
int max(int i) const
Return maximum of range at position i.
int max(void) const
Return maximum of entire set.
int ranges(void) const
Return number of ranges of the specification.
bool operator==(const IntSet &s) const
Return whether s is equal.
unsigned int size(void) const
Return size (cardinality) of set.
IntSet(void)
Initialize as empty set.
Definition int-set-1.hpp:47
bool operator!=(const IntSet &s) const
Return whether s is not equal.
void init(I &i)
Initialize with values from range iterator i.
Handle to region.
Definition region.hpp:55
T * alloc(long unsigned int n)
Allocate block of n objects of type T from region.
Definition region.hpp:386
SharedHandle::Object * object(void) const
Access to the shared object.
Array with arbitrary number of elements.
const int min
Smallest allowed integer value.
Definition int.hh:122
const int max
Largest allowed integer value.
Definition int.hh:120
Gecode toplevel namespace
Archive & operator<<(Archive &e, FloatNumBranch nl)
Definition val-sel.hpp:39
void min(Home home, FloatVar x0, FloatVar x1, FloatVar x2)
Post propagator for .
void max(Home home, FloatVar x0, FloatVar x1, FloatVar x2)
Post propagator for .