Dune Core Modules (2.5.0)

bitsetvector.hh
Go to the documentation of this file.
1// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2// vi: set et ts=4 sw=2 sts=2:
3#ifndef DUNE_BLOCK_BITFIELD_HH
4#define DUNE_BLOCK_BITFIELD_HH
5
10#include <vector>
11#include <bitset>
12#include <iostream>
13#include <algorithm>
14
18
19namespace Dune {
20
21 template <int block_size, class Alloc> class BitSetVector;
22 template <int block_size, class Alloc> class BitSetVectorReference;
23
34 template <int block_size, class Alloc>
36 {
37 protected:
38
40 friend class Dune::BitSetVector<block_size, Alloc>;
41
42 BitSetVectorConstReference(const BitSetVector& blockBitField_, int block_number_) :
43 blockBitField(blockBitField_),
44 block_number(block_number_)
45 {
46 DUNE_ASSERT_BOUNDS(blockBitField_.size() > block_number_);
47 }
48
51
52 public:
53
54 typedef std::bitset<block_size> bitset;
55
56 // bitset interface typedefs
57 typedef typename std::vector<bool, Alloc>::const_reference reference;
58 typedef typename std::vector<bool, Alloc>::const_reference const_reference;
59 typedef size_t size_type;
60
62 bitset operator<<(size_type n) const
63 {
64 bitset b = *this;
65 b <<= n;
66 return b;
67 }
68
70 bitset operator>>(size_type n) const
71 {
72 bitset b = *this;
73 b >>= n;
74 return b;
75 }
76
78 bitset operator~() const
79 {
80 bitset b = *this;
81 b.flip();
82 return b;
83 }
84
86 size_type size() const
87 {
88 return block_size;
89 }
90
92 size_type count() const
93 {
94 size_type n = 0;
95 for(size_type i=0; i<block_size; ++i)
96 n += getBit(i);
97 return n;
98 }
99
101 bool any() const
102 {
103 return count();
104 }
105
107 bool none() const
108 {
109 return ! any();
110 }
111
113 bool all() const
114 {
115 for(size_type i=0; i<block_size; ++i)
116 if(not test(i))
117 return false;
118 return true;
119 }
120
122 bool test(size_type n) const
123 {
124 return getBit(n);
125 }
126
128 {
129 return getBit(i);
130 }
131
133 operator bitset() const
134 {
135 return blockBitField.getRepr(block_number);
136 }
137
139 bool operator== (const bitset& bs) const
140 {
141 return equals(bs);
142 }
143
146 {
147 return equals(bs);
148 }
149
151 bool operator!= (const bitset& bs) const
152 {
153 return ! equals(bs);
154 }
155
158 {
159 return ! equals(bs);
160 }
161
168 friend std::ostream& operator<< (std::ostream& s, const BitSetVectorConstReference& v)
169 {
170 s << "(";
171 for(int i=0; i<block_size; ++i)
172 s << v[i];
173 s << ")";
174 return s;
175 }
176
177 protected:
178 const BitSetVector& blockBitField;
179 int block_number;
180
181 const_reference getBit(size_type i) const
182 {
183 return blockBitField.getBit(block_number,i);
184 }
185
186 template<class BS>
187 bool equals(const BS & bs) const
188 {
189 bool eq = true;
190 for(int i=0; i<block_size; ++i)
191 eq &= (getBit(i) == bs[i]);
192 return eq;
193 }
194
195 private:
200 void operator & ();
201
202 friend class BitSetVectorReference<block_size, Alloc>;
203 };
204
217 template <int block_size, class Alloc>
218 class BitSetVectorReference : public BitSetVectorConstReference<block_size,Alloc>
219 {
220 protected:
221
223 friend class Dune::BitSetVector<block_size, Alloc>;
224
226
227 BitSetVectorReference(BitSetVector& blockBitField_, int block_number_) :
228 BitSetVectorConstReference(blockBitField_, block_number_),
229 blockBitField(blockBitField_)
230 {}
231
232 public:
233 typedef std::bitset<block_size> bitset;
234
238 typedef typename std::vector<bool, Alloc>::reference reference;
240 typedef typename std::vector<bool, Alloc>::const_reference const_reference;
242
244 typedef size_t size_type;
245
248 {
249 for(int i=0; i<block_size; ++i)
250 getBit(i) = b;
251 return (*this);
252 }
253
256 {
257 for(int i=0; i<block_size; ++i)
258 getBit(i) = b.test(i);
259 return (*this);
260 }
261
264 {
265 for(int i=0; i<block_size; ++i)
266 getBit(i) = b.test(i);
267 return (*this);
268 }
269
272 {
273 for(int i=0; i<block_size; ++i)
274 getBit(i) = b.test(i);
275 return (*this);
276 }
277
280 {
281 for (size_type i=0; i<block_size; i++)
282 getBit(i) = (test(i) & x.test(i));
283 return *this;
284 }
285
288 {
289 for (size_type i=0; i<block_size; i++)
290 getBit(i) = (test(i) & x.test(i));
291 return *this;
292 }
293
296 {
297 for (size_type i=0; i<block_size; i++)
298 getBit(i) = (test(i) | x.test(i));
299 return *this;
300 }
301
304 {
305 for (size_type i=0; i<block_size; i++)
306 getBit(i) = (test(i) | x.test(i));
307 return *this;
308 }
309
312 {
313 for (size_type i=0; i<block_size; i++)
314 getBit(i) = (test(i) ^ x.test(i));
315 return *this;
316 }
317
320 {
321 for (size_type i=0; i<block_size; i++)
322 getBit(i) = (test(i) ^ x.test(i));
323 return *this;
324 }
325
328 {
329 for (size_type i=0; i<block_size-n; i++)
330 getBit(i) = test(i+n);
331 return *this;
332 }
333
336 {
337 for (size_type i=0; i<block_size-n; i++)
338 getBit(i+n) = test(i);
339 return *this;
340 }
341
342 // Sets every bit.
344 {
345 for (size_type i=0; i<block_size; i++)
346 set(i);
347 return *this;
348 }
349
352 {
353 for (size_type i=0; i<block_size; i++)
354 flip(i);
355 return *this;
356 }
357
360 {}
361
363 BitSetVectorReference& set(size_type n, int val = 1)
364 {
365 getBit(n) = val;
366 return *this;
367 }
368
371 {
372 set(n, false);
373 return *this;
374 }
375
378 {
379 getBit(n).flip();
380 return *this;
381 }
382
384 using BitSetVectorConstReference::operator[];
385
387 {
388 return getBit(i);
389 }
390
391 protected:
392 BitSetVector& blockBitField;
393
394 using BitSetVectorConstReference::getBit;
395
396 reference getBit(size_type i)
397 {
398 return blockBitField.getBit(this->block_number,i);
399 }
400 };
401
405 template<int block_size, class Alloc>
406 struct const_reference< BitSetVectorReference<block_size,Alloc> >
407 {
409 };
410
411 template<int block_size, class Alloc>
412 struct const_reference< BitSetVectorConstReference<block_size,Alloc> >
413 {
415 };
416
417 template<int block_size, class Alloc>
418 struct mutable_reference< BitSetVectorReference<block_size,Alloc> >
419 {
420 typedef BitSetVectorReference<block_size,Alloc> type;
421 };
422
423 template<int block_size, class Alloc>
424 struct mutable_reference< BitSetVectorConstReference<block_size,Alloc> >
425 {
426 typedef BitSetVectorReference<block_size,Alloc> type;
427 };
428
432 template <int block_size, class Allocator=std::allocator<bool> >
433 class BitSetVector : private std::vector<bool, Allocator>
434 {
436 typedef std::vector<bool, Allocator> BlocklessBaseClass;
437
438 public:
441
443 typedef std::bitset<block_size> value_type;
444
447
450
453
456
458 typedef typename std::vector<bool, Allocator>::size_type size_type;
459
461 typedef Allocator allocator_type;
463
469
472 return iterator(*this, 0);
473 }
474
477 return const_iterator(*this, 0);
478 }
479
482 return iterator(*this, size());
483 }
484
487 return const_iterator(*this, size());
488 }
489
492 BlocklessBaseClass()
493 {}
494
496 BitSetVector(const BlocklessBaseClass& blocklessBitField) :
497 BlocklessBaseClass(blocklessBitField)
498 {
499 if (blocklessBitField.size()%block_size != 0)
500 DUNE_THROW(RangeError, "Vector size is not a multiple of the block size!");
501 }
502
506 explicit BitSetVector(int n) :
507 BlocklessBaseClass(n*block_size)
508 {}
509
511 BitSetVector(int n, bool v) :
512 BlocklessBaseClass(n*block_size,v)
513 {}
514
516 void clear()
517 {
518 BlocklessBaseClass::clear();
519 }
520
522 void resize(int n, bool v = bool())
523 {
524 BlocklessBaseClass::resize(n*block_size, v);
525 }
526
529 {
530 return BlocklessBaseClass::size()/block_size;
531 }
532
534 void setAll() {
535 this->assign(BlocklessBaseClass::size(), true);
536 }
537
539 void unsetAll() {
540 this->assign(BlocklessBaseClass::size(), false);
541 }
542
545 {
546 return reference(*this, i);
547 }
548
551 {
552 return const_reference(*this, i);
553 }
554
557 {
558 return reference(*this, size()-1);
559 }
560
563 {
564 return const_reference(*this, size()-1);
565 }
566
569 {
570 return std::count(BlocklessBaseClass::begin(), BlocklessBaseClass::end(), true);
571 }
572
575 {
576 size_type n = 0;
577 size_type blocks = size();
578 for(size_type i=0; i<blocks; ++i)
579 n += getBit(i,j);
580 return n;
581 }
582
584 friend std::ostream& operator<< (std::ostream& s, const BitSetVector& v)
585 {
586 for (size_t i=0; i<v.size(); i++)
587 s << v[i] << " ";
588 return s;
589 }
590
591 private:
592
594 value_type getRepr(int i) const
595 {
596 value_type bits;
597 for(int j=0; j<block_size; ++j)
598 bits.set(j, getBit(i,j));
599 return bits;
600 }
601
602 typename std::vector<bool>::reference getBit(size_type i, size_type j) {
603 DUNE_ASSERT_BOUNDS(j < block_size);
605 return BlocklessBaseClass::operator[](i*block_size+j);
606 }
607
608 typename std::vector<bool>::const_reference getBit(size_type i, size_type j) const {
609 DUNE_ASSERT_BOUNDS(j < block_size);
611 return BlocklessBaseClass::operator[](i*block_size+j);
612 }
613
614 friend class BitSetVectorReference<block_size,Allocator>;
615 friend class BitSetVectorConstReference<block_size,Allocator>;
616 };
617
618} // namespace Dune
619
620#endif
Macro for wrapping boundary checks.
A proxy class that acts as a const reference to a single bitset in a BitSetVector.
Definition: bitsetvector.hh:36
bool operator==(const bitset &bs) const
Equality of reference and std::bitset.
Definition: bitsetvector.hh:139
bool test(size_type n) const
Returns true if bit n is set.
Definition: bitsetvector.hh:122
bitset operator<<(size_type n) const
Returns a copy of *this shifted left by n bits.
Definition: bitsetvector.hh:62
BitSetVectorConstReference & operator=(const BitSetVectorConstReference &b)
hide assignment operator
bitset operator>>(size_type n) const
Returns a copy of *this shifted right by n bits.
Definition: bitsetvector.hh:70
bool operator!=(const bitset &bs) const
Inequality of reference and std::bitset.
Definition: bitsetvector.hh:151
bool all() const
Returns true if all bits are set.
Definition: bitsetvector.hh:113
bitset operator~() const
Returns a copy of *this with all of its bits flipped.
Definition: bitsetvector.hh:78
size_type size() const
Returns block_size.
Definition: bitsetvector.hh:86
size_type count() const
Returns the number of bits that are set.
Definition: bitsetvector.hh:92
bool none() const
Returns true if no bits are set.
Definition: bitsetvector.hh:107
bool any() const
Returns true if any bits are set.
Definition: bitsetvector.hh:101
A proxy class that acts as a mutable reference to a single bitset in a BitSetVector.
Definition: bitsetvector.hh:219
bool test(size_type n) const
Returns true if bit n is set.
Definition: bitsetvector.hh:122
BitSetVectorReference & operator=(const BitSetVectorConstReference &b)
Assignment from BitSetVectorConstReference.
Definition: bitsetvector.hh:263
BitSetVectorReference & reset(size_type n)
Clears bit n.
Definition: bitsetvector.hh:370
BitSetVectorReference & operator<<=(size_type n)
Left shift.
Definition: bitsetvector.hh:327
std::vector< bool, Alloc >::const_reference const_reference
A proxy class that acts as a const reference to a single bit.
Definition: bitsetvector.hh:240
BitSetVectorReference & operator=(const BitSetVectorReference &b)
Assignment from BitSetVectorReference.
Definition: bitsetvector.hh:271
BitSetVectorReference & operator&=(const BitSetVectorConstReference &x)
Bitwise and (for BitSetVectorConstReference and BitSetVectorReference)
Definition: bitsetvector.hh:287
size_t size_type
size_type typedef (an unsigned integral type)
Definition: bitsetvector.hh:244
BitSetVectorReference & operator=(const bitset &b)
Assignment from bitset.
Definition: bitsetvector.hh:255
BitSetVectorReference & reset()
Clears every bit.
Definition: bitsetvector.hh:359
BitSetVectorReference & operator|=(const BitSetVectorConstReference &x)
Bitwise inclusive or (for BitSetVectorConstReference and BitSetVectorReference)
Definition: bitsetvector.hh:303
BitSetVectorReference & set(size_type n, int val=1)
Sets bit n if val is nonzero, and clears bit n if val is zero.
Definition: bitsetvector.hh:363
BitSetVectorReference & operator^=(const bitset &x)
Bitwise exclusive or (for bitset).
Definition: bitsetvector.hh:311
std::vector< bool, Alloc >::reference reference
Definition: bitsetvector.hh:238
BitSetVectorReference & operator|=(const bitset &x)
Bitwise inclusive or (for bitset)
Definition: bitsetvector.hh:295
BitSetVectorReference & operator>>=(size_type n)
Right shift.
Definition: bitsetvector.hh:335
BitSetVectorReference & operator^=(const BitSetVectorConstReference &x)
Bitwise exclusive or (for BitSetVectorConstReference and BitSetVectorReference)
Definition: bitsetvector.hh:319
BitSetVectorReference & flip(size_type n)
Flips bit n.
Definition: bitsetvector.hh:377
BitSetVectorReference & flip()
Flips the value of every bit.
Definition: bitsetvector.hh:351
BitSetVectorReference & operator&=(const bitset &x)
Bitwise and (for bitset).
Definition: bitsetvector.hh:279
BitSetVectorReference & operator=(bool b)
Assignment from bool, sets each bit in the bitset to b.
Definition: bitsetvector.hh:247
A dynamic array of blocks of booleans.
Definition: bitsetvector.hh:434
const_reference operator[](int i) const
Return const reference to i-th block.
Definition: bitsetvector.hh:550
iterator begin()
Returns a iterator pointing to the beginning of the vector.
Definition: bitsetvector.hh:471
BitSetVectorConstReference< block_size, Allocator > * const_pointer
Const pointer to a small block of bits.
Definition: bitsetvector.hh:455
const_iterator end() const
Returns a const_iterator pointing to the end of the vector.
Definition: bitsetvector.hh:486
BitSetVectorReference< block_size, Allocator > reference
Reference to a small block of bits.
Definition: bitsetvector.hh:446
size_type countmasked(int j) const
Returns the number of set bits, while each block is masked with 1<<i.
Definition: bitsetvector.hh:574
BitSetVectorConstReference< block_size, Allocator > const_reference
Const reference to a small block of bits.
Definition: bitsetvector.hh:449
iterator end()
Returns an iterator pointing to the end of the vector.
Definition: bitsetvector.hh:481
size_type count() const
Returns the number of bits that are set.
Definition: bitsetvector.hh:568
BitSetVector()
Default constructor.
Definition: bitsetvector.hh:491
void setAll()
Sets all entries to true
Definition: bitsetvector.hh:534
std::bitset< block_size > value_type
Type of the values stored by the container.
Definition: bitsetvector.hh:443
reference back()
Return reference to last block.
Definition: bitsetvector.hh:556
BitSetVector(const BlocklessBaseClass &blocklessBitField)
Construction from an unblocked bitfield.
Definition: bitsetvector.hh:496
friend std::ostream & operator<<(std::ostream &s, const BitSetVector &v)
Send bitfield to an output stream.
Definition: bitsetvector.hh:584
const_reference back() const
Return const reference to last block.
Definition: bitsetvector.hh:562
void clear()
Erases all of the elements.
Definition: bitsetvector.hh:516
BitSetVectorReference< block_size, Allocator > * pointer
Pointer to a small block of bits.
Definition: bitsetvector.hh:452
reference operator[](int i)
Return reference to i-th block.
Definition: bitsetvector.hh:544
size_type size() const
Return the number of blocks.
Definition: bitsetvector.hh:528
std::vector< bool, Allocator >::size_type size_type
size type
Definition: bitsetvector.hh:458
BitSetVector(int n, bool v)
Constructor which initializes the field with true or false.
Definition: bitsetvector.hh:511
const_iterator begin() const
Returns a const_iterator pointing to the beginning of the vector.
Definition: bitsetvector.hh:476
Dune::GenericIterator< BitSetVector< block_size, Allocator >, value_type, reference, std::ptrdiff_t, ForwardIteratorFacade > iterator
Definition: bitsetvector.hh:466
void resize(int n, bool v=bool())
Resize field.
Definition: bitsetvector.hh:522
Allocator allocator_type
The type of the allocator.
Definition: bitsetvector.hh:461
BitSetVector(int n)
Definition: bitsetvector.hh:506
void unsetAll()
Sets all entries to false
Definition: bitsetvector.hh:539
Base class for stl conformant forward iterators.
Definition: iteratorfacades.hh:144
Generic class for stl-conforming iterators for container classes with operator[].
Definition: genericiterator.hh:151
Default exception class for range errors.
Definition: exceptions.hh:252
A few common exception classes.
Implements a generic iterator class for writing stl conformant iterators.
#define DUNE_ASSERT_BOUNDS(cond)
If DUNE_CHECK_BOUNDS is defined: check if condition cond holds; otherwise, do nothing.
Definition: boundschecking.hh:28
#define DUNE_THROW(E, m)
Definition: exceptions.hh:216
bool eq(const T &first, const T &second, typename EpsilonType< T >::Type epsilon)
test for equality using epsilon
Definition: float_cmp.cc:118
constexpr auto equals(T1 &&t1, T2 &&t2)
Equality comparison.
Definition: hybridutilities.hh:441
Dune namespace.
Definition: alignment.hh:11
Get the 'const' version of a reference to a mutable object.
Definition: genericiterator.hh:85
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Jul 15, 22:36, 2024)