/usr/include/gecode/support/bitset-base.hpp is in libgecode-dev 4.2.1-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 | /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
* Main authors:
* Mikael Lagerkvist <lagerkvist@gecode.org>
*
* Contributing authors:
* Christian Schulte <schulte@gecode.org>
*
* Copyright:
* Mikael Lagerkvist, 2007
* Christian Schulte, 2007
*
* Last modified:
* $Date: 2012-07-13 02:37:25 +0200 (Fri, 13 Jul 2012) $ by $Author: tack $
* $Revision: 12962 $
*
* 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.
*
*/
#include <climits>
#ifdef _MSC_VER
#include <intrin.h>
#if defined(_M_IX86)
#pragma intrinsic(_BitScanForward)
#define GECODE_SUPPORT_MSVC_32
#endif
#if defined(_M_X64) || defined(_M_IA64)
#pragma intrinsic(_BitScanForward64)
#define GECODE_SUPPORT_MSVC_64
#endif
#endif
namespace Gecode { namespace Support {
class BitSetBase;
/// Date item for bitsets
class BitSetData {
friend class BitSetBase;
protected:
#ifdef GECODE_SUPPORT_MSVC_64
/// Basetype for bits
typedef unsigned __int64 Base;
#else
/// Basetype for bits
typedef unsigned long int Base;
#endif
/// The bits
Base bits;
/// Bits per base
static const unsigned int bpb =
static_cast<unsigned int>(CHAR_BIT * sizeof(Base));
public:
/// Initialize with all bits set if \a set
void init(bool set=false);
/// Get number of data elements for \a s bits
static unsigned int data(unsigned int s);
/// Test wether any bit with position greater or equal to \a i is set
bool operator ()(unsigned int i=0U) const;
/// Access value at bit \a i
bool get(unsigned int i) const;
/// Set bit \a i
void set(unsigned int i);
/// Clear bit \a i
void clear(unsigned int i);
/// Return next set bit with position greater or equal to \a i (there must be a bit)
unsigned int next(unsigned int i=0U) const;
/// Whether all bits are set
bool all(void) const;
/// Whether all bits from bit 0 to bit \a i are set
bool all(unsigned int i) const;
/// Whether no bits are set
bool none(void) const;
/// Whether no bits from bit 0 to bit \a i are set
bool none(unsigned int i) const;
};
/// Status of a bitset
enum BitSetStatus {
BSS_NONE, ///< No bits set
BSS_ALL, ///< All bits set
BSS_SOME ///< Some but not all bits set
};
/// Basic bitset support
class BitSetBase {
protected:
/// Bits per base
static const unsigned int bpb = BitSetData::bpb;
/// Size of bitset (number of bits)
unsigned int sz;
/// Stored bits
BitSetData* data;
/// Access value at bit \a i (no index check)
bool _get(unsigned int i) const;
/// Set bit \a i (no index check)
void _set(unsigned int i);
private:
/// Copy constructor (disabled)
BitSetBase(const BitSetBase&);
/// Assignment operator (disabled)
BitSetBase& operator =(const BitSetBase&);
public:
/// Default constructor (yields empty set)
BitSetBase(void);
/// Initialize for \a s bits and allocator \a a
template<class A>
BitSetBase(A& a, unsigned int s, bool set=false);
/// Copy from bitset \a bs with allocator \a a
template<class A>
BitSetBase(A& a, const BitSetBase& bs);
/// Initialize for \a s bits and allocator \a a (only after default constructor)
template<class A>
void init(A& a, unsigned int s, bool set=false);
/// Return size of bitset (number of bits)
unsigned int size(void) const;
/// Access value at bit \a i
bool get(unsigned int i) const;
/// Set bit \a i
void set(unsigned int i);
/// Clear bit \a i
void clear(unsigned int i);
/// Return position greater or equal \a i of next set bit (\a i is allowed to be equal to size)
unsigned int next(unsigned int i) const;
/// Return status of bitset
BitSetStatus status(void) const;
/// Resize bitset to \a n elememts
template<class A>
void resize(A& a, unsigned int n, bool set=false);
/// Dispose memory for bit set
template<class A>
void dispose(A& a);
};
/*
* Bitset data
*
*/
forceinline void
BitSetData::init(bool set) {
bits = set ? ~static_cast<Base>(0) : static_cast<Base>(0);
}
forceinline unsigned int
BitSetData::data(unsigned int s) {
return s == 0 ? 0 : ((s-1) / bpb + 1);
}
forceinline bool
BitSetData::operator ()(unsigned int i) const {
return (bits >> i) != static_cast<Base>(0U);
}
forceinline bool
BitSetData::get(unsigned int i) const {
return (bits & (static_cast<Base>(1U) << i)) != static_cast<Base>(0U);
}
forceinline void
BitSetData::set(unsigned int i) {
bits |= (static_cast<Base>(1U) << i);
}
forceinline void
BitSetData::clear(unsigned int i) {
bits &= ~(static_cast<Base>(1U) << i);
}
forceinline unsigned int
BitSetData::next(unsigned int i) const {
assert(bits != static_cast<Base>(0));
#if defined(GECODE_SUPPORT_MSVC_32)
assert(bpb == 32);
unsigned long int p;
_BitScanForward(&p,bits >> i);
return static_cast<unsigned int>(p)+i;
#elif defined(GECODE_SUPPORT_MSVC_64)
assert(bpb == 64);
unsigned long int p;
_BitScanForward64(&p,bits >> i);
return static_cast<unsigned int>(p)+i;
#elif defined(GECODE_HAS_BUILTIN_FFSL)
if ((bpb == 32) || (bpb == 64)) {
int p = __builtin_ffsl(bits >> i);
assert(p > 0);
return static_cast<unsigned int>(p-1)+i;
}
#else
while (!get(i)) i++;
return i;
#endif
}
forceinline bool
BitSetData::all(void) const {
return bits == ~static_cast<Base>(0U);
}
forceinline bool
BitSetData::all(unsigned int i) const {
const Base mask = (static_cast<Base>(1U) << i) - static_cast<Base>(1U);
return (bits & mask) == mask;
}
forceinline bool
BitSetData::none(void) const {
return bits == static_cast<Base>(0U);
}
forceinline bool
BitSetData::none(unsigned int i) const {
const Base mask = (static_cast<Base>(1U) << i) - static_cast<Base>(1U);
return (bits & mask) == static_cast<Base>(0U);
}
/*
* Basic bit sets
*
*/
forceinline bool
BitSetBase::_get(unsigned int i) const {
return data[i / bpb].get(i % bpb);
}
forceinline void
BitSetBase::_set(unsigned int i) {
data[i / bpb].set(i % bpb);
}
template<class A>
void
BitSetBase::resize(A& a, unsigned int n, bool set) {
if (n>sz) {
data = a.template realloc<BitSetData>(data,BitSetData::data(sz+1),
BitSetData::data(n+1));
for (unsigned int i=BitSetData::data(sz)+1;
i<BitSetData::data(n+1); i++) {
data[i].init(set);
}
for (unsigned int i=(sz%bpb); i<bpb; i++)
if (set)
data[sz / bpb].set(i);
else
data[sz / bpb].clear(i);
}
sz = n; _set(sz);
}
template<class A>
forceinline void
BitSetBase::dispose(A& a) {
a.template free<BitSetData>(data,BitSetData::data(sz+1));
}
forceinline
BitSetBase::BitSetBase(void)
: sz(0), data(NULL) {}
template<class A>
forceinline
BitSetBase::BitSetBase(A& a, unsigned int s, bool set)
: sz(s),
data(a.template alloc<BitSetData>(BitSetData::data(sz+1))) {
for (unsigned int i = BitSetData::data(sz+1); i--; )
data[i].init(set);
// Set a bit at position sz as sentinel (for efficient next)
_set(sz);
}
template<class A>
forceinline
BitSetBase::BitSetBase(A& a, const BitSetBase& bs)
: sz(bs.sz),
data(a.template alloc<BitSetData>(BitSetData::data(sz+1))) {
for (unsigned int i = BitSetData::data(sz+1); i--; )
data[i] = bs.data[i];
// Set a bit at position sz as sentinel (for efficient next)
_set(sz);
}
template<class A>
forceinline void
BitSetBase::init(A& a, unsigned int s, bool set) {
assert((sz == 0) && (data == NULL));
sz=s; data=a.template alloc<BitSetData>(BitSetData::data(sz+1));
for (unsigned int i=BitSetData::data(sz+1); i--; )
data[i].init(set);
// Set a bit at position sz as sentinel (for efficient next)
_set(sz);
}
forceinline unsigned int
BitSetBase::size(void) const {
return sz;
}
forceinline bool
BitSetBase::get(unsigned int i) const {
assert(i < sz);
return _get(i);
}
forceinline void
BitSetBase::set(unsigned int i) {
assert(i < sz);
_set(i);
}
forceinline void
BitSetBase::clear(unsigned int i) {
assert(i < sz);
data[i / bpb].clear(i % bpb);
}
forceinline unsigned int
BitSetBase::next(unsigned int i) const {
assert(i <= sz);
unsigned int pos = i / bpb;
unsigned int bit = i % bpb;
if (data[pos](bit))
return pos * bpb + data[pos].next(bit);
// The sentinel bit guarantees that this loop always terminates
do {
pos++;
} while (!data[pos]());
return pos * bpb + data[pos].next();
}
forceinline BitSetStatus
BitSetBase::status(void) const {
unsigned int pos = sz / bpb;
unsigned int bits = sz % bpb;
if (pos > 0) {
if (data[0].all()) {
for (unsigned int i=1; i<pos; i++)
if (!data[i].all())
return BSS_SOME;
return data[pos].all(bits) ? BSS_ALL : BSS_SOME;
} else if (data[0].none()) {
for (unsigned int i=1; i<pos; i++)
if (!data[i].none())
return BSS_SOME;
return data[pos].none(bits) ? BSS_NONE : BSS_SOME;
} else {
return BSS_SOME;
}
}
if (data[0].all(bits))
return BSS_ALL;
if (data[0].none(bits))
return BSS_NONE;
return BSS_SOME;
}
}}
#ifdef GECODE_SUPPORT_MSVC_32
#undef GECODE_SUPPORT_MSVC_32
#endif
#ifdef GECODE_SUPPORT_MSVC_64
#undef GECODE_SUPPORT_MSVC_64
#endif
// STATISTICS: support-any
|