Storm
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
BitVector.h
Go to the documentation of this file.
1#ifndef STORM_STORAGE_BITVECTOR_H_
2#define STORM_STORAGE_BITVECTOR_H_
3
4#include <cmath>
5#include <cstdint>
6#include <functional>
7#include <iterator>
8#include <ostream>
9#include <ranges>
10#include <vector>
11
12namespace storm {
13namespace storage {
14
18class BitVector {
19 public:
26 // Declare the BitVector class as a friend class to access its internal storage.
27 friend class BitVector;
28
29 public:
30 // Define iterator
31 using iterator_category = std::forward_iterator_tag;
32 using value_type = uint_fast64_t;
33 using difference_type = std::ptrdiff_t;
34 using pointer = uint_fast64_t*;
35 using reference = uint_fast64_t&;
36
38
49 const_iterator(uint64_t const* dataPtr, uint_fast64_t startIndex, uint_fast64_t endIndex, bool setOnFirstBit = true);
50
56 const_iterator(const_iterator const& other);
57
64
72
80
85 const_iterator& operator+=(size_t n);
86
92 uint_fast64_t operator*() const;
93
100 bool operator!=(const_iterator const& other) const;
101
108 bool operator==(const_iterator const& other) const;
109
110 private:
111 // The underlying bit vector of this iterator.
112 uint64_t const* dataPtr;
113
114 // The index of the bit this iterator currently points to.
115 uint_fast64_t currentIndex;
116
117 // The index of the bit that is past the end of the range of this iterator.
118 uint_fast64_t endIndex;
119 };
126 // Declare the BitVector class as a friend class to access its internal storage.
127 friend class BitVector;
128
129 public:
130 // Define iterator
131 using iterator_category = std::forward_iterator_tag;
132 using value_type = uint_fast64_t;
133 using difference_type = std::ptrdiff_t;
134 using pointer = uint_fast64_t*;
135 using reference = uint_fast64_t&;
136
148 const_reverse_iterator(uint64_t const* dataPtr, uint64_t upperBound, uint64_t lowerBound = 0ull, bool setOnFirstBit = true);
151
157
163
168
174 uint_fast64_t operator*() const;
175
182 bool operator!=(const_reverse_iterator const& other) const;
183
190 bool operator==(const_reverse_iterator const& other) const;
191
192 private:
194 uint64_t const* dataPtr;
195
197 uint64_t currentIndex;
198
200 uint64_t lowerBound;
201 };
202
206 BitVector();
207
211 ~BitVector();
212
220 explicit BitVector(uint_fast64_t length, bool init = false);
221
230 template<typename InputIterator>
231 BitVector(uint_fast64_t length, InputIterator first, InputIterator last);
232
236 BitVector(uint_fast64_t length, std::vector<uint_fast64_t> setEntries);
237
243 BitVector(BitVector const& other);
244
250 BitVector(BitVector&& other);
251
258 bool operator==(BitVector const& other) const;
259
266 bool operator!=(BitVector const& other) const;
267
275 BitVector& operator=(BitVector const& other);
276
284 BitVector& operator=(BitVector&& other);
285
292 bool operator<(BitVector const& other) const;
293
300 void set(uint_fast64_t index, bool value = true);
301
308 template<typename InputIterator>
309 void set(InputIterator first, InputIterator last, bool value = true);
310
318 bool operator[](uint_fast64_t index) const;
319
327 bool get(uint_fast64_t index) const;
328
336 void resize(uint_fast64_t newLength, bool init = false);
337
344 void concat(BitVector const& extension);
345
346 /*
347 * Expands the size to a multiple of 64.
348 */
349 void expandSize(bool init = false);
350
360 void grow(uint_fast64_t minimumLength, bool init = false);
361
369 BitVector operator&(BitVector const& other) const;
370
380 BitVector& operator&=(BitVector const& other);
381
389 BitVector operator|(BitVector const& other) const;
390
400 BitVector& operator|=(BitVector const& other);
401
409 BitVector operator^(BitVector const& other) const;
410
422 BitVector operator%(BitVector const& filter) const;
423
429 BitVector operator~() const;
430
434 void complement();
435
441 void increment();
442
450 BitVector implies(BitVector const& other) const;
451
460 bool isSubsetOf(BitVector const& other) const;
461
471 bool isDisjointFrom(BitVector const& other) const;
472
481 bool matches(uint_fast64_t bitIndex, BitVector const& other) const;
482
491 void set(uint_fast64_t bitIndex, BitVector const& other);
492
496 void setMultiple(uint64_t bitIndex, uint64_t nrOfBits, bool newValue = true);
497
504 BitVector permute(std::vector<uint64_t> const& inversePermutation) const;
505
512 BitVector permuteGroupedVector(std::vector<uint64_t> const& inversePermutation, std::vector<uint64_t> const& rowGroupIndices) const;
513
522 storm::storage::BitVector get(uint_fast64_t bitIndex, uint_fast64_t numberOfBits) const;
523
531 uint_fast64_t getAsInt(uint_fast64_t bitIndex, uint_fast64_t numberOfBits) const;
532
538 uint_fast64_t getTwoBitsAligned(uint_fast64_t bitIndex) const;
539
547 void setFromInt(uint_fast64_t bitIndex, uint_fast64_t numberOfBits, uint64_t value);
548
554 bool empty() const;
555
562 bool full() const;
563
567 void clear();
568
572 void fill();
573
579 uint_fast64_t getNumberOfSetBits() const;
580
587 uint_fast64_t getNumberOfSetBitsBeforeIndex(uint_fast64_t index) const;
588
594 std::vector<uint_fast64_t> getNumberOfSetBitsBeforeIndices() const;
595
601 size_t size() const;
602
608 std::size_t getSizeInBytes() const;
609
614 const_iterator begin() const;
615
620 const_iterator begin(uint64_t lowerBound) const;
621
625 const_iterator end() const;
626
632
637 const_reverse_iterator rbegin(uint64_t upperBound) const;
638
643
653 uint_fast64_t getNextSetIndex(uint_fast64_t startingIndex) const;
654
664 uint_fast64_t getNextUnsetIndex(uint_fast64_t startingIndex) const;
665
675 uint64_t getStartOfZeroSequenceBefore(uint64_t endIndex) const;
676
686 uint64_t getStartOfOneSequenceBefore(uint64_t endIndex) const;
687
697 bool compareAndSwap(uint_fast64_t start1, uint_fast64_t start2, uint_fast64_t length);
698
699 friend std::ostream& operator<<(std::ostream& out, BitVector const& bitVector);
700
701 void store(std::ostream&) const;
702 static BitVector load(std::string const& description);
703
704 friend struct std::hash<storm::storage::BitVector>;
705 friend struct FNV1aBitVectorHash;
706
707 template<typename StateType>
708 friend struct Murmur3BitVectorHash;
709
710 private:
717 BitVector(uint_fast64_t bucketCount, uint_fast64_t bitCount);
718
732 template<bool Value, bool Backward = false>
733 static uint_fast64_t getNextIndexWithValue(uint64_t const* dataPtr, uint_fast64_t startingIndex, uint_fast64_t endIndex);
734
738 void truncateLastBucket();
739
747 BitVector getAsBitVector(uint_fast64_t start, uint_fast64_t length) const;
748
756 void setFromBitVector(uint_fast64_t start, BitVector const& other);
757
763 void printBits(std::ostream& out) const;
764
770 size_t bucketCount() const;
771
772 // The number of bits that this bit vector can hold.
773 uint_fast64_t bitCount;
774
775 // The underlying storage of 64-bit buckets for all bits of this bit vector.
776 uint64_t* buckets;
777
778 // A bit mask that can be used to reduce a modulo 64 operation to a logical "and".
779 static const uint_fast64_t mod64mask = (1 << 6) - 1;
780};
781
782static_assert(std::ranges::forward_range<BitVector>);
783
784struct FNV1aBitVectorHash {
785 std::size_t operator()(storm::storage::BitVector const& bv) const;
786};
787
788template<typename StateType>
789struct Murmur3BitVectorHash {
790 StateType operator()(storm::storage::BitVector const& bv) const;
791};
792
793} // namespace storage
794} // namespace storm
795
796namespace std {
797template<>
798struct hash<storm::storage::BitVector> {
799 std::size_t operator()(storm::storage::BitVector const& bv) const;
800};
801} // namespace std
802
803#endif // STORM_STORAGE_BITVECTOR_H_
A class that enables iterating over the indices of the bit vector whose corresponding bits are set to...
Definition BitVector.h:25
uint_fast64_t operator*() const
Returns the index of the current bit to which this iterator points.
Definition BitVector.cpp:63
const_iterator & operator++()
Increases the position of the iterator to the position of the next bit that is set to true in the und...
Definition BitVector.cpp:45
bool operator==(const_iterator const &other) const
Compares the iterator with another iterator for equality.
Definition BitVector.cpp:71
const_iterator & operator+=(size_t n)
Increases the position of the iterator to the position of the n'th next bit that is set to true in th...
Definition BitVector.cpp:56
const_iterator & operator=(const_iterator const &other)
Assigns the contents of the given iterator to the current one via copying the former's contents.
Definition BitVector.cpp:35
std::forward_iterator_tag iterator_category
Definition BitVector.h:31
bool operator!=(const_iterator const &other) const
Compares the iterator with another iterator for inequality.
Definition BitVector.cpp:67
A class that enables iterating over the indices of the bit vector whose corresponding bits are set to...
Definition BitVector.h:125
bool operator==(const_reverse_iterator const &other) const
Compares the iterator with another iterator for equality.
const_reverse_iterator()
Constructs a reverse iterator over the indices of the set bits in the given bit vector,...
Definition BitVector.cpp:75
const_reverse_iterator & operator+=(size_t n)
Lets the iterator point to the n'th previous bit with value 1.
const_reverse_iterator & operator++()
Lets the iterator point to the previous bit with value 1.
const_reverse_iterator & operator=(const_reverse_iterator const &other)
Definition BitVector.cpp:92
uint_fast64_t operator*() const
Returns the index of the current bit to which this iterator points.
bool operator!=(const_reverse_iterator const &other) const
Compares the iterator with another iterator for inequality.
A bit vector that is internally represented as a vector of 64-bit values.
Definition BitVector.h:18
~BitVector()
Deconstructs a bit vector by deleting the underlying storage.
void complement()
Negates all bits in the bit vector.
BitVector & operator|=(BitVector const &other)
Performs a logical "or" with the given bit vector and assigns the result to the current bit vector.
bool compareAndSwap(uint_fast64_t start1, uint_fast64_t start2, uint_fast64_t length)
Compare two intervals [start1, start1+length] and [start2, start2+length] and swap them if the second...
BitVector operator^(BitVector const &other) const
Performs a logical "xor" with the given bit vector.
void setMultiple(uint64_t bitIndex, uint64_t nrOfBits, bool newValue=true)
Sets multiple bits to the given value.
bool operator<(BitVector const &other) const
Retrieves whether the current bit vector is (in some order) smaller than the given one.
const_reverse_iterator rbegin() const
Returns a reverse iterator to the indices of the set bits in the bit vector.
void fill()
Sets all bits from the bit vector.
bool operator[](uint_fast64_t index) const
Retrieves the truth value of the bit at the given index.
bool matches(uint_fast64_t bitIndex, BitVector const &other) const
Checks whether the given bit vector matches the bits starting from the given index in the current bit...
bool isDisjointFrom(BitVector const &other) const
Checks whether none of the bits that are set in the current bit vector are also set in the given bit ...
uint_fast64_t getTwoBitsAligned(uint_fast64_t bitIndex) const
bool full() const
Retrieves whether all bits are set in this bit vector.
const_reverse_iterator rend() const
Returns a reverse iterator pointing at the element past the front of the bit vector.
BitVector()
Constructs an empty bit vector of length 0.
const_iterator end() const
Returns an iterator pointing at the element past the back of the bit vector.
uint_fast64_t getNextSetIndex(uint_fast64_t startingIndex) const
Retrieves the index of the bit that is the next bit set to true in the bit vector.
void store(std::ostream &) const
void setFromInt(uint_fast64_t bitIndex, uint_fast64_t numberOfBits, uint64_t value)
Sets the selected number of lowermost bits of the provided value at the given bit index.
BitVector operator%(BitVector const &filter) const
Computes a bit vector that contains only the values of the bits given by the filter.
BitVector operator|(BitVector const &other) const
Performs a logical "or" with the given bit vector.
bool empty() const
Retrieves whether no bits are set to true in this bit vector.
std::size_t getSizeInBytes() const
Returns (an approximation of) the size of the bit vector measured in bytes.
void clear()
Removes all set bits from the bit vector.
bool isSubsetOf(BitVector const &other) const
Checks whether all bits that are set in the current bit vector are also set in the given bit vector.
BitVector implies(BitVector const &other) const
Performs a logical "implies" with the given bit vector.
BitVector & operator=(BitVector const &other)
Assigns the contents of the given bit vector to the current bit vector via a deep copy.
BitVector operator&(BitVector const &other) const
Performs a logical "and" with the given bit vector.
void resize(uint_fast64_t newLength, bool init=false)
Resizes the bit vector to hold the given new number of bits.
BitVector permute(std::vector< uint64_t > const &inversePermutation) const
Apply a permutation of entries.
void increment()
Increments the (unsigned) number represented by this BitVector by one.
const_iterator begin() const
Returns an iterator to the indices of the set bits in the bit vector.
uint64_t getStartOfZeroSequenceBefore(uint64_t endIndex) const
Retrieves the smallest index i such that all bits in the range [i,endIndex) are 0.
friend std::ostream & operator<<(std::ostream &out, BitVector const &bitVector)
BitVector operator~() const
Performs a logical "not" on the bit vector.
bool operator!=(BitVector const &other) const
Compares the given bit vector with the current one.
void set(uint_fast64_t index, bool value=true)
Sets the given truth value at the given index.
size_t size() const
Retrieves the number of bits this bit vector can store.
uint_fast64_t getNextUnsetIndex(uint_fast64_t startingIndex) const
Retrieves the index of the bit that is the next bit set to false in the bit vector.
BitVector & operator&=(BitVector const &other)
Performs a logical "and" with the given bit vector and assigns the result to the current bit vector.
uint_fast64_t getNumberOfSetBits() const
Returns the number of bits that are set to true in this bit vector.
void grow(uint_fast64_t minimumLength, bool init=false)
Enlarges the bit vector such that it holds at least the given number of bits (but possibly more).
uint_fast64_t getAsInt(uint_fast64_t bitIndex, uint_fast64_t numberOfBits) const
Retrieves the content of the current bit vector at the given index for the given number of bits as an...
static BitVector load(std::string const &description)
std::vector< uint_fast64_t > getNumberOfSetBitsBeforeIndices() const
Retrieves a vector that holds at position i the number of bits set before index i.
void expandSize(bool init=false)
bool get(uint_fast64_t index) const
Retrieves the truth value of the bit at the given index and performs a bound check.
bool operator==(BitVector const &other) const
Compares the given bit vector with the current one.
BitVector permuteGroupedVector(std::vector< uint64_t > const &inversePermutation, std::vector< uint64_t > const &rowGroupIndices) const
Apply a permutation of entries assuming a grouped vector.
uint_fast64_t getNumberOfSetBitsBeforeIndex(uint_fast64_t index) const
Retrieves the number of bits set in this bit vector with an index strictly smaller than the given one...
uint64_t getStartOfOneSequenceBefore(uint64_t endIndex) const
Retrieves the smallest index i such that all bits in the range [i,endIndex) are 1.
void concat(BitVector const &extension)
Concatenate this bitvector with another bitvector.
LabParser.cpp.
Definition cli.cpp:18