Generated on for Gecode by doxygen 1.17.0
tiny-bit-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 * Linnea Ingmar <linnea.ingmar@hotmail.com>
5 *
6 * Contributing authors:
7 * Christian Schulte <schulte@gecode.dev>
8 *
9 * Copyright:
10 * Linnea Ingmar, 2017
11 * Christian Schulte, 2017
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
38namespace Gecode { namespace Int { namespace Extensional {
39
40 /*
41 * Tiny bit-set
42 *
43 */
44 template<unsigned int sz>
45 forceinline
46 TinyBitSet<sz>::TinyBitSet(Space&, unsigned int n, bool) {
47 assert(n <= sz);
49 for (unsigned int i=0U; i<n; i++)
50 _bits[i].init(true);
52 for (unsigned int i=n; i<sz; i++)
53 _bits[i].init(false);
54 }
55
56 template<unsigned int sz>
57 template<unsigned int largersz>
58 forceinline
60 GECODE_ASSUME(sz <= largersz);
61 for (unsigned int i=0U; i<sz; i++)
62 _bits[i] = sbs._bits[i];
63 assert(sbs.empty() || !empty());
64 }
65
66 template<unsigned int sz>
67 template<class IndexType>
68 forceinline
70 assert(sz == sbs.width());
71 assert(!sbs.empty());
72 for (unsigned int i=0U; i<sz; i++)
73 _bits[i].init(false);
74 for (unsigned int i=0U; i<sbs.words(); i++)
75 _bits[sbs._word_index[i]] = sbs._word_bits[i];
76 assert(!empty());
77 }
78
79 template<unsigned int sz>
80 forceinline void
82 for (unsigned int i=0U; i<sz; i++) {
83 mask[i].init(false);
84 assert(mask[i].none());
85 }
86 }
87
88 template<unsigned int sz>
89 forceinline void
91 for (unsigned int i=0U; i<sz; i++)
92 mask[i] = BitSetData::o(mask[i],b[i]);
93 }
94
95 template<unsigned int sz>
96 forceinline void
98 BitSetData* mask) const {
99 for (unsigned int i=0U; i<sz; i++) {
100 const BitSetData* w = find_support_word(support,i);
101 if (w != nullptr)
102 mask[i] = BitSetData::o(mask[i],*w);
103 }
104 }
105
106 template<unsigned int sz>
107 template<bool sparse>
108 forceinline void
110 for (unsigned int i=0U; i<sz; i++)
111 _bits[i] = BitSetData::a(_bits[i], mask[i]);
112 }
113
114 template<unsigned int sz>
115 forceinline void
117 for (unsigned int i=0U; i<sz; i++) {
118 const BitSetData* w = find_support_word(support,i);
119 if (w == nullptr)
120 _bits[i].init(false);
121 else
122 _bits[i] = BitSetData::a(_bits[i],*w);
123 }
124 }
125
126 template<unsigned int sz>
127 forceinline void
129 const BitSetData* b) {
130 for (unsigned int i=0U; i<sz; i++)
131 _bits[i] = BitSetData::a(_bits[i], BitSetData::o(a[i],b[i]));
132 }
133
134 template<unsigned int sz>
135 forceinline void
137 for (unsigned int i=0U; i<sz; i++) {
138 const BitSetData* sa = find_support_word(a,i);
139 const BitSetData* sb = find_support_word(b,i);
140 BitSetData m;
141 if (sa != nullptr) {
142 m = *sa;
143 } else {
144 m.init(false);
145 }
146 if (sb != nullptr)
147 m = BitSetData::o(m,*sb);
148 _bits[i] = BitSetData::a(_bits[i],m);
149 }
150 }
151
152 template<unsigned int sz>
153 forceinline void
155 for (unsigned int i=0U; i<sz; i++)
156 _bits[i] = BitSetData::a(_bits[i],~(b[i]));
157 }
158
159 template<unsigned int sz>
160 forceinline void
162 for (unsigned int i=0U; i<sz; i++) {
163 const BitSetData* w = find_support_word(support,i);
164 if (w != nullptr)
165 _bits[i] = BitSetData::a(_bits[i],~(*w));
166 }
167 }
168
169 template<unsigned int sz>
170 forceinline void
172 for (unsigned int i=0U; i<sz; i++)
173 _bits[i].init(false);
174 assert(empty());
175 }
176
177 template<unsigned int sz>
178 forceinline bool
180 for (unsigned int i=0U; i<sz; i++)
181 if (!BitSetData::a(_bits[i],b[i]).none())
182 return true;
183 return false;
184 }
185
186 template<unsigned int sz>
187 forceinline bool
189 for (unsigned int i=0U; i<sz; i++) {
190 const BitSetData* w = find_support_word(support,i);
191 if ((w != nullptr) && !BitSetData::a(_bits[i],*w).none())
192 return true;
193 }
194 return false;
195 }
196
197 template<unsigned int sz>
198 forceinline unsigned long long int
200 unsigned long long int o = 0U;
201 for (unsigned int i=0U; i<sz; i++)
202 o += static_cast<unsigned long long int>
203 (BitSetData::a(_bits[i],b[i]).ones());
204 return o;
205 }
206
207 template<unsigned int sz>
208 forceinline unsigned long long int
210 unsigned long long int o = 0U;
211 for (unsigned int i=0U; i<sz; i++) {
212 const BitSetData* w = find_support_word(support,i);
213 if (w != nullptr)
214 o += static_cast<unsigned long long int>
215 (BitSetData::a(_bits[i],*w).ones());
216 }
217 return o;
218 }
219
220 template<unsigned int sz>
221 forceinline unsigned long long int
223 unsigned long long int o = 0U;
224 for (unsigned int i=0U; i<sz; i++)
225 o += static_cast<unsigned long long int>(_bits[i].ones());
226 return o;
227 }
228
229 template<unsigned int sz>
230 forceinline unsigned long long int
232 return (static_cast<unsigned long long int>(sz) *
233 static_cast<unsigned long long int>(BitSetData::bpb));
234 }
235
236 template<unsigned int sz>
237 forceinline bool
238 TinyBitSet<sz>::empty(void) const { // Linear complexity...
239 for (unsigned int i=0U; i<sz; i++)
240 if (!_bits[i].none())
241 return false;
242 return true;
243 }
244
245 template<unsigned int sz>
246 forceinline unsigned int
248 assert(!empty());
250 for (unsigned int i=sz; i--; )
251 if (!_bits[i].none())
252 return i+1U;
254 return 0U;
255 }
256
257 template<unsigned int sz>
258 forceinline unsigned int
260 return width();
261 }
262
263 template<unsigned int sz>
264 forceinline unsigned int
266 return sz;
267 }
268
269}}}
270
271// STATISTICS: int-prop
BitSetData * _word_bits
Active word data.
unsigned int width(void) const
Return the highest active index.
Definition bit-set.hpp:107
IndexType * _word_index
Original word index for each active word position.
bool empty(void) const
Check whether the set is empty.
Definition bit-set.hpp:89
unsigned int words(void) const
Return the number of required bit set words.
Definition bit-set.hpp:95
Compressed tuple-word support list.
void intersect_with_mask(const BitSetData *mask)
Intersect with mask, sparse mask if sparse is true.
unsigned int words(void) const
Return the number of required bit set words.
unsigned int width(void) const
Return the highest active index.
bool intersects(const BitSetData *b)
Check if has a non-empty intersection with the set.
void add_to_mask(const BitSetData *b, BitSetData *mask) const
Add to mask.
unsigned long long int bits(void) const
Return an upper bound on the number of bits.
unsigned long long int ones(void) const
Return the number of ones.
unsigned int size(void) const
Return the total number of words.
bool empty(void) const
Check whether the set is empty.
void intersect_with_masks(const BitSetData *a, const BitSetData *b)
Intersect with the "or" of and b.
void flush(void)
Make the set empty.
void nand_with_mask(const BitSetData *b)
Perform "nand" with b.
void clear_mask(BitSetData *mask)
Clear the first limit words in mask.
Computation spaces.
Definition core.hpp:1775
static const unsigned int bpb
Bits per base.
void init(bool setbits=false)
Initialize with all bits set if setbits.
void a(BitSetData a)
Perform "and" with a.
void o(BitSetData a)
Perform "or" with a.
Extensional propagators
Definition int.hh:2335
const TupleSet::BitSetData * find_support_word(const CompressedSupport &s, unsigned int widx)
Definition bit-set.hpp:65
Gecode::Support::BitSetData BitSetData
Import type.
Finite domain integers.
Definition lastval.hh:52
Gecode toplevel namespace
#define GECODE_NEVER
Assert that this command is never executed.
Definition macros.hpp:56
#define GECODE_ASSUME(p)
Assert certain property.
Definition macros.hpp:114