Generated on for Gecode by doxygen 1.15.0
tuple-set.hpp
Go to the documentation of this file.
1/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2/*
3 * Main authors:
4 * Mikael Zayenz Lagerkvist <lagerkvist@gecode.dev>
5 * Christian Schulte <schulte@gecode.dev>
6 *
7 * Copyright:
8 * Mikael Zayenz Lagerkvist, 2007
9 * Christian Schulte, 2017
10 *
11 * This file is part of Gecode, the generic constraint
12 * development environment:
13 * http://www.gecode.dev
14 *
15 * Permission is hereby granted, free of charge, to any person obtaining
16 * a copy of this software and associated documentation files (the
17 * "Software"), to deal in the Software without restriction, including
18 * without limitation the rights to use, copy, modify, merge, publish,
19 * distribute, sublicense, and/or sell copies of the Software, and to
20 * permit persons to whom the Software is furnished to do so, subject to
21 * the following conditions:
22 *
23 * The above copyright notice and this permission notice shall be
24 * included in all copies or substantial portions of the Software.
25 *
26 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
30 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
31 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
32 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 *
34 */
35
36#include <sstream>
37
38namespace Gecode {
39
40 /*
41 * Ranges
42 *
43 */
44 forceinline unsigned int
46 return static_cast<unsigned int>(max - min + 1);
47 }
48
50 TupleSet::Range::supports(unsigned int n_words, int n) const {
51 assert((min <= n) && (n <= max));
52 if (s == nullptr)
53 return nullptr;
54 const unsigned long offset =
55 static_cast<unsigned long>(n_words) *
56 static_cast<unsigned long>(n - min);
57 return s + offset;
58 }
59
60
61 /*
62 * Tuple set data
63 *
64 */
67 : arity(a), n_words(0U), // To be initialized in finalize
69 min(Int::Limits::max), max(Int::Limits::min), key(0),
70 td(heap.alloc<int>(n_initial_free * a)),
71 vd(heap.alloc<ValueData>(a)),
72 range(nullptr), range_base(nullptr), support(nullptr),
74 sparse_n_vals(0U), sparse_offsets(nullptr),
75 sparse_tuples(nullptr), sparse_tv(nullptr),
76 compressed_offsets(nullptr), compressed_words(nullptr),
78 }
79
80 forceinline bool
82 return (state == TS_DENSE) ||
83 (state == TS_SPARSE) ||
85 }
86
87 forceinline bool
89 return state == TS_FAILED;
90 }
91
92 forceinline bool
94 return state != TS_BUILDING;
95 }
96
99 if (n_free == 0)
100 resize();
101 assert(n_free > 0);
102 n_free--;
103 Tuple t = td + n_tuples*arity;
104 n_tuples++;
105 return t;
106 }
107
109 TupleSet::Data::get(int i) const {
110 assert((i >= 0) && (i < n_tuples));
111 return td + i*arity;
112 }
113
114 forceinline unsigned int
116 if (n > 1U) {
117 unsigned int l=0U, h=n-1U;
118 while (true) {
119 assert(l<=h);
120 unsigned int m = l + ((h-l) >> 1);
121 if (k < r[m].min)
122 h=m-1U;
123 else if (k > r[m].max)
124 l=m+1U;
125 else
126 return m;
127 }
129 } else {
130 return 0U;
131 }
132 }
133
134 forceinline void
135 TupleSet::Data::set(BitSetData* d, unsigned int i) {
136 d[i / BitSetData::bpb].set(i % BitSetData::bpb);
137 }
138
139 forceinline bool
140 TupleSet::Data::get(const BitSetData* d, unsigned int i) {
141 return d[i / BitSetData::bpb].get(i % BitSetData::bpb);
142 }
143
144 forceinline unsigned int
146 return static_cast<unsigned int>((t - td) / static_cast<unsigned int>(arity));
147 }
148
150 TupleSet::Data::fst(int i) const {
151 return &vd[i].r[0];
152 }
154 TupleSet::Data::lst(int i) const {
155 return &vd[i].r[vd[i].n-1U];
156 }
157
158
159 /*
160 * Tuple set
161 *
162 */
165 _add(t); return *this;
166 }
167
170
172 TupleSet::operator bool(void) const {
173 return object() != nullptr;
174 }
175
176 forceinline void
178 Data* d = static_cast<Data*>(object());
179 if (d == nullptr)
180 throw Int::UninitializedTupleSet("TupleSet::finalize()");
181 if (d->failed())
182 throw Int::AlreadyFinalized("TupleSet::finalize()");
183 if (!d->finalized())
184 d->finalize();
185 }
186
187 forceinline void
189 Data* d = static_cast<Data*>(object());
190 if (d == nullptr)
191 throw Int::UninitializedTupleSet("TupleSet::finalize()");
192 if (d->failed())
193 throw Int::AlreadyFinalized("TupleSet::finalize()");
194 if (!d->finalized())
195 d->finalize(epk);
196 }
197
198 forceinline bool
200 const Data* d = static_cast<Data*>(object());
201 return (d != nullptr) && d->finalized();
202 }
203
204 forceinline bool
205 TupleSet::failed(void) const {
206 const Data* d = static_cast<Data*>(object());
207 return (d != nullptr) && d->failed();
208 }
209
211 TupleSet::data(void) const {
212 Data* d = static_cast<Data*>(object());
213 if (d == nullptr)
214 throw Int::UninitializedTupleSet("TupleSet");
215 if (!d->finalized())
216 throw Int::NotYetFinalized("TupleSet");
217 return *d;
218 }
220 TupleSet::raw(void) const {
221 Data* d = static_cast<Data*>(object());
222 if (d == nullptr)
223 throw Int::UninitializedTupleSet("TupleSet");
224 return *d;
225 }
226
227 forceinline bool
229 return !(*this == t);
230 }
231 forceinline int
232 TupleSet::arity(void) const {
233 return raw().arity;
234 }
235 forceinline int
236 TupleSet::tuples(void) const {
237 return raw().n_tuples;
238 }
239 forceinline unsigned int
240 TupleSet::words(void) const {
241 return data().n_words;
242 }
243 forceinline int
244 TupleSet::min(void) const {
245 return data().min;
246 }
247 forceinline int
248 TupleSet::max(void) const {
249 return data().max;
250 }
253 return data().get(i);
254 }
256 TupleSet::fst(int i) const {
257 return data().fst(i);
258 }
260 TupleSet::lst(int i) const {
261 return data().lst(i);
262 }
263
264 forceinline bool
266 if (tuples() != t.tuples())
267 return false;
268 if (arity() != t.arity())
269 return false;
270 if (min() != t.min())
271 return false;
272 if (max() != t.max())
273 return false;
274 return equal(t);
275 }
276
277 forceinline std::size_t
278 TupleSet::hash(void) const {
279 return data().key;
280 }
281
284 switch (data().state) {
285 case Data::TS_DENSE:
286 return EPK_DENSE;
287 case Data::TS_SPARSE:
288 return EPK_SPARSE;
290 return EPK_DENSE_COMPRESSED;
291 case Data::TS_FAILED:
293 default:
295 return EPK_DENSE;
296 }
297 }
298
299 forceinline unsigned int
300 TupleSet::sparse_values(void) const {
301 return data().sparse_n_vals;
302 }
303
304 forceinline const unsigned int*
305 TupleSet::sparse_tuple_value_ids(void) const {
306 return data().sparse_tv;
307 }
308
309 forceinline const unsigned int*
310 TupleSet::sparse_support_offsets(void) const {
311 return data().sparse_offsets;
312 }
313
314 forceinline bool
315 TupleSet::support_id(int p, int n, unsigned int& gid) const {
316 const Data& d = data();
317 if ((p < 0) || (p >= d.arity))
318 return false;
319 const ValueData& v = d.vd[p];
320 if (v.base == nullptr)
321 return false;
322 unsigned int l = 0U, h = v.n;
323 while (l < h) {
324 const unsigned int m = l + ((h-l) >> 1);
325 if (n < v.r[m].min)
326 h = m;
327 else if (n > v.r[m].max)
328 l = m+1U;
329 else {
330 gid = v.base[m] + static_cast<unsigned int>(n - v.r[m].min);
331 return true;
332 }
333 }
334 return false;
335 }
336
337 forceinline bool
338 TupleSet::sparse_support(int p, int n,
339 const unsigned int*& b,
340 const unsigned int*& e,
341 unsigned int& gid) const {
342 const Data& d = data();
343 if ((d.sparse_offsets == nullptr) ||
344 (d.sparse_tuples == nullptr) ||
345 (d.sparse_n_vals == 0U))
346 return false;
347 if (!support_id(p,n,gid))
348 return false;
349 b = d.sparse_tuples + d.sparse_offsets[gid];
350 e = d.sparse_tuples + d.sparse_offsets[gid+1U];
351 return true;
352 }
353
354 forceinline bool
355 TupleSet::dense_compressed_support(int p, int n,
356 const CSupportWord*& b,
357 const CSupportWord*& e) const {
358 const Data& d = data();
359 if ((d.compressed_offsets == nullptr) ||
360 (d.compressed_words == nullptr))
361 return false;
362 unsigned int support_id0 = 0U;
363 if (!support_id(p,n,support_id0))
364 return false;
365 b = d.compressed_words + d.compressed_offsets[support_id0];
366 e = d.compressed_words + d.compressed_offsets[support_id0+1U];
367 return true;
368 }
369
370 namespace Int { namespace Extensional {
371
372 forceinline bool
373 support_offsets_size(unsigned long long n_vals,
374 unsigned int& n_offsets) {
375 if (n_vals >= static_cast<unsigned long long>
376 (std::numeric_limits<unsigned int>::max()))
377 return false;
378 n_offsets = static_cast<unsigned int>(n_vals) + 1U;
379 return true;
380 }
381
382 forceinline unsigned int
384 return ts.sparse_values();
385 }
386
387 forceinline const unsigned int*
389 return ts.sparse_tuple_value_ids();
390 }
391
392 forceinline const unsigned int*
394 return ts.sparse_support_offsets();
395 }
396
397 forceinline bool
398 TupleSetAccess::support_id(const TupleSet& ts, int p, int n,
399 unsigned int& gid) {
400 return ts.support_id(p,n,gid);
401 }
402
403 forceinline bool
405 const unsigned int*& b,
406 const unsigned int*& e,
407 unsigned int& gid) {
408 return ts.sparse_support(p,n,b,e,gid);
409 }
410
411 forceinline bool
413 const TupleSet::CSupportWord*& b,
414 const TupleSet::CSupportWord*& e) {
415 return ts.dense_compressed_support(p,n,b,e);
416 }
417
418 }}
419
420
421 template<class Char, class Traits>
422 std::basic_ostream<Char,Traits>&
423 operator <<(std::basic_ostream<Char,Traits>& os, const TupleSet& ts) {
424 std::basic_ostringstream<Char,Traits> s;
425 s.copyfmt(os); s.width(0);
426 s << "Number of tuples: " << ts.tuples()
427 << " (number of words: " << ts.words() << " with "
428 << Support::BitSetData::bpb << " bits)" << std::endl;
429 for (int a=0; a < ts.arity(); a++) {
430 unsigned int size = 0U;
431 for (const TupleSet::Range* c=ts.fst(a); c<=ts.lst(a); c++)
432 size += c->width();
433 s << "\t[" << a << "] size: " << size
434 << ", width: "
435 << static_cast<unsigned int>(ts.lst(a)->max - ts.fst(a)->min + 1)
436 << ", ranges: "
437 << (ts.lst(a) - ts.fst(a) + 1U)
438 << std::endl;
439 }
440 return os << s.str();
441 }
442
443
444 /*
445 * Range iterator
446 *
447 */
450 c = &(ts.data().vd[i].r[0]);
451 l = c + ts.data().vd[i].n;
452 }
453
454 forceinline bool
456 return c<l;
457 }
458 forceinline void
460 c++;
461 }
462
463 forceinline int
465 return c->min;
466 }
467 forceinline int
469 return c->max;
470 }
471 forceinline unsigned int
473 return c->width();
474 }
475
476}
477
478// STATISTICS: int-prop
Passing integer arguments.
Definition int.hh:652
Exception: Tuple set already finalized
static bool dense_compressed_support(const TupleSet &ts, int p, int n, const TupleSet::CSupportWord *&b, const TupleSet::CSupportWord *&e)
Return compressed support words for position/value.
static bool support_id(const TupleSet &ts, int p, int n, unsigned int &gid)
Return support id for position/value.
static unsigned int sparse_values(const TupleSet &ts)
Return number of sparse support values.
static bool sparse_support(const TupleSet &ts, int p, int n, const unsigned int *&b, const unsigned int *&e, unsigned int &gid)
Return sparse support tuple id range for position/value.
static const unsigned int * sparse_support_offsets(const TupleSet &ts)
Return sparse support offsets.
static const unsigned int * sparse_tuple_value_ids(const TupleSet &ts)
Return tuple-value sparse ids.
Exception: Tuple set not yet finalized
Exception: uninitialized tuple set
SharedHandle::Object * object(void) const
Access to the shared object.
static const unsigned int bpb
Bits per base.
Compressed support data for one tuple-word block.
Definition int.hh:2393
Data stored for a Table.
Definition int.hh:2434
int max
Largest value.
Definition int.hh:2459
unsigned int compressed_n_entries
Number of compressed support entries.
Definition int.hh:2487
int n_free
Number of free tuple entries of arity.
Definition int.hh:2455
bool terminal(void) const
Is datastructure no longer mutable.
Definition tuple-set.hpp:93
unsigned int * sparse_tuples
Sparse support tuple ids (size arity*n_tuples).
Definition int.hh:2479
void resize(void)
Resize tuple data.
BitSetData * support
Pointer to all support data.
Definition int.hh:2471
Data(int a)
Initialize as empty tuple set with arity a.
Definition tuple-set.hpp:66
unsigned int * compressed_offsets
Compressed support offsets (size n_vals+1).
Definition int.hh:2483
unsigned int n_words
Number of words for support.
Definition int.hh:2451
int min
Smallest value.
Definition int.hh:2457
bool failed(void) const
Has finalization failed.
Definition tuple-set.hpp:88
static void set(BitSetData *d, unsigned int n)
Set bit n in bitset data d.
unsigned int * sparse_offsets
Sparse support offsets (size sparse_n_vals+1).
Definition int.hh:2477
int n_tuples
Number of Tuples.
Definition int.hh:2453
unsigned int sparse_n_vals
Number of sparse support values.
Definition int.hh:2475
Tuple get(int i) const
Return tuple with number i.
unsigned int * sparse_tv
Tuple cell to sparse support id map (size arity*n_tuples).
Definition int.hh:2481
int * td
Tuple data.
Definition int.hh:2463
State state
Tuple set lifecycle state and finalized representation.
Definition int.hh:2473
CSupportWord * compressed_words
Compressed support words (size compressed_n_entries).
Definition int.hh:2485
unsigned int tuple2idx(Tuple t) const
Map tuple address to index.
Range * range
Pointer to all ranges.
Definition int.hh:2467
const Range * lst(int i) const
Return last range for position i.
bool finalized(void) const
Is datastructure finalized.
Definition tuple-set.hpp:81
ValueData * vd
Value data.
Definition int.hh:2465
const Range * fst(int i) const
Return first range for position i.
static const int n_initial_free
Initial number of free tuples.
Definition int.hh:2437
std::size_t key
Hash key.
Definition int.hh:2461
Tuple add(void)
Return newly added tuple.
Definition tuple-set.hpp:98
unsigned int * range_base
Pointer to all range support ids.
Definition int.hh:2469
Range information.
Definition int.hh:2399
BitSetData * s
Begin of supports.
Definition int.hh:2406
unsigned int width(void) const
Return the width.
Definition tuple-set.hpp:45
int max
Maximum value.
Definition int.hh:2404
int min
Minimum value.
Definition int.hh:2402
const BitSetData * supports(unsigned int n_words, int n) const
Return the dense supports for value n.
Definition tuple-set.hpp:50
bool operator()(void) const
Test whether iterator is still at a range.
Ranges(const TupleSet &ts, int i)
Initialize for column i.
int max(void) const
Return largest value of range.
const Range * l
Last range.
Definition int.hh:2654
int min(void) const
Return smallest value of range.
void operator++(void)
Move iterator to next range (if possible).
const Range * c
Current range.
Definition int.hh:2652
unsigned int width(void) const
Return width of range (distance between minimum and maximum).
Data about values in the table.
Definition int.hh:2419
unsigned int start(int n) const
Find start range for value n.
unsigned int n
Number of ranges.
Definition int.hh:2422
Class representing a set of tuples.
Definition int.hh:2382
TupleSet(void)
Construct an uninitialized tuple set.
bool failed(void) const
Has tuple-set finalization failed.
void _add(const IntArgs &t)
Add tuple t to tuple set.
int tuples(void) const
Number of tuples.
int max(void) const
Return maximal value in all tuples.
bool operator!=(const TupleSet &t) const
Test whether tuple set is different from t.
bool finalized(void) const
Is tuple set successfully finalized.
ExtensionalPropKind representation(void) const
Return materialized tuple-set representation.
TupleSet & add(const IntArgs &t)
Add tuple t to tuple set.
bool operator==(const TupleSet &t) const
Test whether tuple set is equal to t.
std::basic_ostream< Char, Traits > & operator<<(std::basic_ostream< Char, Traits > &os, const TupleSet &ts)
Tuple operator[](int i) const
Get tuple i.
const Range * lst(int i) const
Return last range for position i.
int * Tuple
Type of a tuple.
Definition int.hh:2389
std::size_t hash(void) const
Return hash key.
void finalize(void)
Finalize tuple set with dense support data.
bool equal(const TupleSet &t) const
Test whether tuple set is equal to t.
const Range * fst(int i) const
Return first range for position i.
unsigned int words(void) const
Return number of required bit set words.
int min(void) const
Return minimal value in all tuples.
Data & raw(void) const
Get raw data (must be initialized).
Gecode::Support::BitSetData BitSetData
Import bit set data type.
Definition int.hh:2391
Data & data(void) const
Get data (must be initialized and finalized).
int arity(void) const
Arity of tuple set.
Heap heap
The single global heap.
Definition heap.cpp:44
ExtensionalPropKind
Support representation selection for extensional tuple sets.
Definition int.hh:2355
Extensional propagators
Definition int.hh:2335
bool support_offsets_size(unsigned long long n_vals, unsigned int &n_offsets)
Numerical limits for integer variables.
Definition int.hh:118
Finite domain integers.
Definition lastval.hh:52
Gecode toplevel namespace
void max(Home home, FloatVar x0, FloatVar x1, FloatVar x2)
Post propagator for .
Gecode::FloatVal b(9, 12)
Gecode::IntSet d(v, 7)
#define forceinline
Definition config.hpp:141
#define GECODE_NEVER
Assert that this command is never executed.
Definition macros.hpp:56