/usr/include/dune/common/bitsetvector.hh is in libdune-common-dev 2.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 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 | #ifndef DUNE_BLOCK_BITFIELD_HH
#define DUNE_BLOCK_BITFIELD_HH
/** \file
\brief Efficient implementation of a dynamic array of static arrays of booleans
*/
#include <vector>
#include <bitset>
#include <iostream>
#include <algorithm>
#include <dune/common/genericiterator.hh>
#include <dune/common/exceptions.hh>
namespace Dune {
template <int block_size, class Alloc> class BitSetVector;
template <int block_size, class Alloc> class BitSetVectorReference;
/**
\brief A proxy class that acts as a const reference to a single
bitset in a BitSetVector.
It contains a conversion to std::bitset and most of the
interface of const std::bitset.
\warning As this is only a proxy class, you can not get the
address of the bitset.
*/
template <int block_size, class Alloc>
class BitSetVectorConstReference
{
protected:
typedef Dune::BitSetVector<block_size, Alloc> BitSetVector;
friend class Dune::BitSetVector<block_size, Alloc>;
BitSetVectorConstReference(const BitSetVector& blockBitField, int block_number) :
blockBitField(blockBitField),
block_number(block_number)
{};
//! hide assignment operator
BitSetVectorConstReference& operator=(const BitSetVectorConstReference & b);
public:
typedef std::bitset<block_size> bitset;
// bitset interface typedefs
typedef typename std::vector<bool, Alloc>::const_reference reference;
typedef typename std::vector<bool, Alloc>::const_reference const_reference;
typedef size_t size_type;
//! Returns a copy of *this shifted left by n bits.
bitset operator<<(size_type n) const
{
bitset b = *this;
b <<= n;
return b;
}
//! Returns a copy of *this shifted right by n bits.
bitset operator>>(size_type n) const
{
bitset b = *this;
b >>= n;
return b;
}
//! Returns a copy of *this with all of its bits flipped.
bitset operator~() const
{
bitset b = *this;
b.flip();
return b;
}
//! Returns block_size.
size_type size() const
{
return block_size;
}
//! Returns the number of bits that are set.
size_type count() const
{
size_type n = 0;
for(size_type i=0; i<block_size; ++i)
n += getBit(i);
return n;
}
//! Returns true if any bits are set.
bool any() const
{
return count();
}
//! Returns true if no bits are set.
bool none() const
{
return ! any();
}
//! Returns true if bit n is set.
bool test(size_type n) const
{
return getBit(n);
}
const_reference operator[](size_type i) const
{
return getBit(i);
}
//! cast to bitset
operator bitset() const
{
return blockBitField.getRepr(block_number);
}
//! Equality of reference and std::bitset
bool operator== (const bitset& bs) const
{
return equals(bs);
}
//! Equality of reference and other reference
bool operator== (const BitSetVectorConstReference& bs) const
{
return equals(bs);
}
//! Inequality of reference and std::bitset
bool operator!= (const bitset& bs) const
{
return ! equals(bs);
}
//! Inequality of reference and other reference
bool operator!= (const BitSetVectorConstReference& bs) const
{
return ! equals(bs);
}
/*!
missing operators:
- unsigned long to_ulong() const
*/
friend std::ostream& operator<< (std::ostream& s, const BitSetVectorConstReference& v)
{
s << "(";
for(int i=0; i<block_size; ++i)
s << v[i];
s << ")";
return s;
}
protected:
const BitSetVector& blockBitField;
int block_number;
const_reference getBit(size_type i) const
{
return blockBitField.getBit(block_number,i);
}
template<class BS>
bool equals(const BS & bs) const
{
bool eq = true;
for(int i=0; i<block_size; ++i)
eq &= (getBit(i) == bs[i]);
return eq;
}
private:
/**
This is only a Proxy class, you can't get the address of the
object it references
*/
void operator & ();
friend class BitSetVectorReference<block_size, Alloc>;
};
/**
\brief A proxy class that acts as a mutable reference to a
single bitset in a BitSetVector.
It contains an assignment operator from std::bitset. It
inherits the const std::bitset interface provided by
BitSetVectorConstReference and adds most of the non-const
methods of std::bitset.
\warning As this is only a proxy class, you can not get the
address of the bitset.
*/
template <int block_size, class Alloc>
class BitSetVectorReference : public BitSetVectorConstReference<block_size,Alloc>
{
protected:
typedef Dune::BitSetVector<block_size, Alloc> BitSetVector;
friend class Dune::BitSetVector<block_size, Alloc>;
typedef Dune::BitSetVectorConstReference<block_size,Alloc> BitSetVectorConstReference;
BitSetVectorReference(BitSetVector& blockBitField, int block_number) :
BitSetVectorConstReference(blockBitField, block_number),
blockBitField(blockBitField)
{};
public:
typedef std::bitset<block_size> bitset;
//! bitset interface typedefs
//! \{
//! A proxy class that acts as a reference to a single bit.
typedef typename std::vector<bool, Alloc>::reference reference;
//! A proxy class that acts as a const reference to a single bit.
typedef typename std::vector<bool, Alloc>::const_reference const_reference;
//! \}
//! size_type typedef (an unsigned integral type)
typedef size_t size_type;
//! Assignment from bool, sets each bit in the bitset to b
BitSetVectorReference& operator=(bool b)
{
for(int i=0; i<block_size; ++i)
getBit(i) = b;
return (*this);
}
//! Assignment from bitset
BitSetVectorReference& operator=(const bitset & b)
{
for(int i=0; i<block_size; ++i)
getBit(i) = b.test(i);
return (*this);
}
//! Assignment from BitSetVectorConstReference
BitSetVectorReference& operator=(const BitSetVectorConstReference & b)
{
for(int i=0; i<block_size; ++i)
getBit(i) = b.test(i);
return (*this);
}
//! Assignment from BitSetVectorReference
BitSetVectorReference& operator=(const BitSetVectorReference & b)
{
for(int i=0; i<block_size; ++i)
getBit(i) = b.test(i);
return (*this);
}
//! Bitwise and (for bitset).
BitSetVectorReference& operator&=(const bitset& x)
{
for (size_type i=0; i<block_size; i++)
getBit(i) = (test(i) & x.test(i));
return *this;
}
//! Bitwise and (for BitSetVectorConstReference and BitSetVectorReference)
BitSetVectorReference& operator&=(const BitSetVectorConstReference& x)
{
for (size_type i=0; i<block_size; i++)
getBit(i) = (test(i) & x.test(i));
return *this;
}
//! Bitwise inclusive or (for bitset)
BitSetVectorReference& operator|=(const bitset& x)
{
for (size_type i=0; i<block_size; i++)
getBit(i) = (test(i) | x.test(i));
return *this;
}
//! Bitwise inclusive or (for BitSetVectorConstReference and BitSetVectorReference)
BitSetVectorReference& operator|=(const BitSetVectorConstReference& x)
{
for (size_type i=0; i<block_size; i++)
getBit(i) = (test(i) | x.test(i));
return *this;
}
//! Bitwise exclusive or (for bitset).
BitSetVectorReference& operator^=(const bitset& x)
{
for (size_type i=0; i<block_size; i++)
getBit(i) = (test(i) ^ x.test(i));
return *this;
}
//! Bitwise exclusive or (for BitSetVectorConstReference and BitSetVectorReference)
BitSetVectorReference& operator^=(const BitSetVectorConstReference& x)
{
for (size_type i=0; i<block_size; i++)
getBit(i) = (test(i) ^ x.test(i));
return *this;
}
//! Left shift.
BitSetVectorReference& operator<<=(size_type n)
{
for (size_type i=0; i<block_size-n; i++)
getBit(i) = test(i+n);
return *this;
}
//! Right shift.
BitSetVectorReference& operator>>=(size_type n)
{
for (size_type i=0; i<block_size-n; i++)
getBit(i+n) = test(i);
return *this;
}
// Sets every bit.
BitSetVectorReference& set()
{
for (size_type i=0; i<block_size; i++)
set(i);
return *this;
}
//! Flips the value of every bit.
BitSetVectorReference& flip()
{
for (size_type i=0; i<block_size; i++)
flip(i);
return *this;
}
//! Clears every bit.
BitSetVectorReference& reset()
{
}
//! Sets bit n if val is nonzero, and clears bit n if val is zero.
BitSetVectorReference& set(size_type n, int val = 1)
{
getBit(n) = val;
return *this;
}
//! Clears bit n.
BitSetVectorReference& reset(size_type n)
{
set(n, false);
return *this;
}
//! Flips bit n.
BitSetVectorReference& flip(size_type n)
{
getBit(n).flip();
return *this;
}
using BitSetVectorConstReference::test;
using BitSetVectorConstReference::operator[];
reference operator[](size_type i)
{
return getBit(i);
}
protected:
BitSetVector& blockBitField;
using BitSetVectorConstReference::getBit;
reference getBit(size_type i)
{
return blockBitField.getBit(this->block_number,i);
}
};
/**
typetraits for BitSetVectorReference
*/
template<int block_size, class Alloc>
struct const_reference< BitSetVectorReference<block_size,Alloc> >
{
typedef BitSetVectorConstReference<block_size,Alloc> type;
};
template<int block_size, class Alloc>
struct const_reference< BitSetVectorConstReference<block_size,Alloc> >
{
typedef BitSetVectorConstReference<block_size,Alloc> type;
};
template<int block_size, class Alloc>
struct mutable_reference< BitSetVectorReference<block_size,Alloc> >
{
typedef BitSetVectorReference<block_size,Alloc> type;
};
template<int block_size, class Alloc>
struct mutable_reference< BitSetVectorConstReference<block_size,Alloc> >
{
typedef BitSetVectorReference<block_size,Alloc> type;
};
/**
\brief A dynamic %array of blocks of booleans
*/
template <int block_size, class Allocator=std::allocator<bool> >
class BitSetVector : private std::vector<bool, Allocator>
{
/** \brief The implementation class: an unblocked bitfield */
typedef std::vector<bool, Allocator> BlocklessBaseClass;
public:
//! container interface typedefs
//! \{
/** \brief Type of the values stored by the container */
typedef std::bitset<block_size> value_type;
/** \brief Reference to a small block of bits */
typedef BitSetVectorReference<block_size,Allocator> reference;
/** \brief Const reference to a small block of bits */
typedef BitSetVectorConstReference<block_size,Allocator> const_reference;
/** \brief Pointer to a small block of bits */
typedef BitSetVectorReference<block_size,Allocator>* pointer;
/** \brief Const pointer to a small block of bits */
typedef BitSetVectorConstReference<block_size,Allocator>* const_pointer;
/** \brief size type */
typedef typename std::vector<bool, Allocator>::size_type size_type;
/** \brief The type of the allocator */
typedef Allocator allocator_type;
//! \}
//! iterators
//! \{
typedef Dune::GenericIterator<BitSetVector<block_size,Allocator>, value_type, reference, std::ptrdiff_t, ForwardIteratorFacade> iterator;
typedef Dune::GenericIterator<const BitSetVector<block_size,Allocator>, const value_type, const_reference, std::ptrdiff_t, ForwardIteratorFacade> const_iterator;
//! \}
//! Returns a iterator pointing to the beginning of the vector.
iterator begin(){
return iterator(*this, 0);
}
//! Returns a const_iterator pointing to the beginning of the vector.
const_iterator begin() const{
return const_iterator(*this, 0);
}
//! Returns an iterator pointing to the end of the vector.
iterator end(){
return iterator(*this, size());
}
//! Returns a const_iterator pointing to the end of the vector.
const_iterator end() const{
return const_iterator(*this, size());
}
//! Default constructor
BitSetVector() :
BlocklessBaseClass()
{}
//! Construction from an unblocked bitfield
BitSetVector(const BlocklessBaseClass& blocklessBitField) :
BlocklessBaseClass(blocklessBitField)
{
if (blocklessBitField.size()%block_size != 0)
DUNE_THROW(RangeError, "Vector size is not a multiple of the block size!");
}
/** Constructor with a given length
\param n Number of blocks
*/
explicit BitSetVector(int n) :
BlocklessBaseClass(n*block_size)
{}
//! Constructor which initializes the field with true or false
BitSetVector(int n, bool v) :
BlocklessBaseClass(n*block_size,v)
{}
//! Erases all of the elements.
void clear()
{
BlocklessBaseClass::clear();
}
//! Resize field
void resize(int n, bool v = bool())
{
BlocklessBaseClass::resize(n*block_size, v);
}
/** \brief Return the number of blocks */
size_type size() const
{
return BlocklessBaseClass::size()/block_size;
}
//! Sets all entries to <tt> true </tt>
void setAll() {
this->assign(BlocklessBaseClass::size(), true);
}
//! Sets all entries to <tt> false </tt>
void unsetAll() {
this->assign(BlocklessBaseClass::size(), false);
}
/** \brief Return reference to i-th block */
reference operator[](int i)
{
return reference(*this, i);
}
/** \brief Return const reference to i-th block */
const_reference operator[](int i) const
{
return const_reference(*this, i);
}
/** \brief Return reference to last block */
reference back()
{
return reference(*this, size()-1);
}
/** \brief Return const reference to last block */
const_reference back() const
{
return const_reference(*this, size()-1);
}
//! Returns the number of bits that are set.
size_type count() const
{
return std::count(BlocklessBaseClass::begin(), BlocklessBaseClass::end(), true);
}
//! Returns the number of set bits, while each block is masked with 1<<i
size_type countmasked(int j) const
{
size_type n = 0;
size_type blocks = size();
for(size_type i=0; i<blocks; ++i)
n += getBit(i,j);
return n;
}
//! Send bitfield to an output stream
friend std::ostream& operator<< (std::ostream& s, const BitSetVector& v)
{
for (size_t i=0; i<v.size(); i++)
s << v[i] << " ";
return s;
}
private:
// get a prepresentation as value_type
value_type getRepr(int i) const
{
value_type bits;
for(int j=0; j<block_size; ++j)
bits.set(j, getBit(i,j));
return bits;
}
typename std::vector<bool>::reference getBit(size_type i, size_type j) {
return BlocklessBaseClass::operator[](i*block_size+j);
}
typename std::vector<bool>::const_reference getBit(size_type i, size_type j) const {
return BlocklessBaseClass::operator[](i*block_size+j);
}
friend class BitSetVectorReference<block_size,Allocator>;
friend class BitSetVectorConstReference<block_size,Allocator>;
};
} // namespace Dune
#endif
|