| 42 |
|
#ifndef UTILS_BITSET_HPP |
| 43 |
|
#define UTILS_BITSET_HPP |
| 44 |
|
|
| 45 |
< |
#include <functional> |
| 45 |
> |
#include <iostream> |
| 46 |
|
#include <vector> |
| 47 |
|
namespace oopse { |
| 48 |
|
|
| 49 |
< |
/** |
| 50 |
< |
* @class BitSet BitSet.hpp "BitSet.hpp" |
| 51 |
< |
* @brief BitSet is a wrapper class of std::vector<char> to act as a growable std::bitset |
| 52 |
< |
*/ |
| 53 |
< |
class BitSet { |
| 54 |
< |
public: |
| 55 |
< |
/** */ |
| 56 |
< |
BitSet() {} |
| 57 |
< |
/** */ |
| 58 |
< |
BitSet(int nbits) : bitset_(nbits) {clearAll(); } |
| 49 |
> |
/** |
| 50 |
> |
* @class BitSet BitSet.hpp "BitSet.hpp" |
| 51 |
> |
* @brief BitSet is a wrapper class of std::vector<char> to act as a growable std::bitset |
| 52 |
> |
*/ |
| 53 |
> |
class BitSet { |
| 54 |
> |
public: |
| 55 |
> |
/** */ |
| 56 |
> |
BitSet() {} |
| 57 |
> |
/** */ |
| 58 |
> |
BitSet(int nbits) : bitset_(nbits) {clearAll(); } |
| 59 |
|
|
| 60 |
< |
/** Returns the number of bits set to true in this BitSet. */ |
| 61 |
< |
int countBits(); |
| 60 |
> |
/** Returns the number of bits set to true in this BitSet. */ |
| 61 |
> |
int countBits(); |
| 62 |
|
|
| 63 |
< |
/** Sets the bit at the specified index to to the complement of its current value. */ |
| 64 |
< |
void flip(int bitIndex) { bitset_[bitIndex] = !bitset_[bitIndex]; } |
| 63 |
> |
/** Sets the bit at the specified index to to the complement of its current value. */ |
| 64 |
> |
void flip(int bitIndex) { bitset_[bitIndex] = !bitset_[bitIndex]; } |
| 65 |
|
|
| 66 |
< |
/** Sets each bit from the specified fromIndex(inclusive) to the specified toIndex(exclusive) to the complement of its current value. */ |
| 67 |
< |
void flip(int fromIndex, int toIndex); |
| 66 |
> |
/** Sets each bit from the specified fromIndex(inclusive) to the specified toIndex(exclusive) to the complement of its current value. */ |
| 67 |
> |
void flip(int fromIndex, int toIndex); |
| 68 |
|
|
| 69 |
< |
/** Sets each bit to the complement of its current value. */ |
| 70 |
< |
void flip() { flip(0, size()); } |
| 69 |
> |
/** Sets each bit to the complement of its current value. */ |
| 70 |
> |
void flip() { flip(0, size()); } |
| 71 |
|
|
| 72 |
< |
/** Returns the value of the bit with the specified index. */ |
| 73 |
< |
bool get(int bitIndex) { return bitset_[bitIndex]; } |
| 72 |
> |
/** Returns the value of the bit with the specified index. */ |
| 73 |
> |
bool get(int bitIndex) { return bitset_[bitIndex]; } |
| 74 |
|
|
| 75 |
< |
/** Returns a new BitSet composed of bits from this BitSet from fromIndex(inclusive) to toIndex(exclusive). */ |
| 76 |
< |
BitSet get(int fromIndex, int toIndex); |
| 75 |
> |
/** Returns a new BitSet composed of bits from this BitSet from fromIndex(inclusive) to toIndex(exclusive). */ |
| 76 |
> |
BitSet get(int fromIndex, int toIndex); |
| 77 |
|
|
| 78 |
< |
/** Returns true if any bits are set to true */ |
| 79 |
< |
bool any() {return !none(); } |
| 78 |
> |
/** Returns true if any bits are set to true */ |
| 79 |
> |
bool any() {return !none(); } |
| 80 |
|
|
| 81 |
< |
/** Returns true if no bits are set to true */ |
| 82 |
< |
bool none(); |
| 81 |
> |
/** Returns true if no bits are set to true */ |
| 82 |
> |
bool none(); |
| 83 |
> |
|
| 84 |
> |
int firstOffBit() const { return !bitset_[0] ? 0 : nextOffBit(0); } |
| 85 |
|
|
| 86 |
< |
/** Returns the index of the first bit that is set to false that occurs on or after the specified starting index.*/ |
| 87 |
< |
int nextOffBit(int fromIndex); |
| 88 |
< |
|
| 89 |
< |
/** Returns the index of the first bit that is set to true that occurs on or after the specified starting index. */ |
| 88 |
< |
int nextOnBit(int fromIndex); |
| 86 |
> |
/** Returns the index of the first bit that is set to false that occurs on or after the specified starting index.*/ |
| 87 |
> |
int nextOffBit(int fromIndex) const; |
| 88 |
> |
|
| 89 |
> |
int firstOnBit() const { return bitset_[0] ? 0 : nextOnBit(0); } |
| 90 |
|
|
| 91 |
< |
/** Performs a logical AND of this target bit set with the argument bit set. */ |
| 92 |
< |
void andOperator (const BitSet& bs); |
| 91 |
> |
/** Returns the index of the first bit that is set to true that occurs on or after the specified starting index. */ |
| 92 |
> |
int nextOnBit(int fromIndex) const; |
| 93 |
> |
|
| 94 |
> |
/** Performs a logical AND of this target bit set with the argument bit set. */ |
| 95 |
> |
void andOperator (const BitSet& bs); |
| 96 |
|
|
| 97 |
< |
/** Performs a logical OR of this bit set with the bit set argument. */ |
| 98 |
< |
void orOperator (const BitSet& bs); |
| 97 |
> |
/** Performs a logical OR of this bit set with the bit set argument. */ |
| 98 |
> |
void orOperator (const BitSet& bs); |
| 99 |
|
|
| 100 |
< |
/** Performs a logical XOR of this bit set with the bit set argument. */ |
| 101 |
< |
void xorOperator (const BitSet& bs); |
| 100 |
> |
/** Performs a logical XOR of this bit set with the bit set argument. */ |
| 101 |
> |
void xorOperator (const BitSet& bs); |
| 102 |
|
|
| 103 |
< |
void setBitOn(int bitIndex) { setBit(bitIndex, true); } |
| 103 |
> |
void setBitOn(int bitIndex) { setBit(bitIndex, true); } |
| 104 |
|
|
| 105 |
< |
void setBitOff(int bitIndex) { setBit(bitIndex, false); } |
| 105 |
> |
void setBitOff(int bitIndex) { setBit(bitIndex, false); } |
| 106 |
|
|
| 107 |
< |
void setRangeOn(int fromIndex, int toIndex) { setBits(fromIndex, toIndex, true); } |
| 107 |
> |
void setRangeOn(int fromIndex, int toIndex) { setBits(fromIndex, toIndex, true); } |
| 108 |
|
|
| 109 |
< |
void setRangeOff(int fromIndex, int toIndex) { setBits(fromIndex, toIndex, false); } |
| 109 |
> |
void setRangeOff(int fromIndex, int toIndex) { setBits(fromIndex, toIndex, false); } |
| 110 |
|
|
| 111 |
< |
/** Sets all of the bits in this BitSet to false. */ |
| 112 |
< |
void clearAll() { setRangeOff(0, size()); } |
| 111 |
> |
/** Sets all of the bits in this BitSet to false. */ |
| 112 |
> |
void clearAll() { setRangeOff(0, size()); } |
| 113 |
|
|
| 114 |
< |
void setAll() { setRangeOn(0, size()); } |
| 114 |
> |
void setAll() { setRangeOn(0, size()); } |
| 115 |
|
|
| 116 |
< |
/** Returns the number of bits of space actually in use by this BitSet to represent bit values. */ |
| 117 |
< |
int size() const { return bitset_.size(); } |
| 116 |
> |
/** Returns the number of bits of space actually in use by this BitSet to represent bit values. */ |
| 117 |
> |
int size() const { return bitset_.size(); } |
| 118 |
|
|
| 119 |
< |
/** Changes the size of BitSet*/ |
| 120 |
< |
void resize(int nbits); |
| 119 |
> |
/** Changes the size of BitSet*/ |
| 120 |
> |
void resize(int nbits); |
| 121 |
|
|
| 122 |
< |
BitSet& operator&= (const BitSet &bs) { andOperator (bs); return *this; } |
| 123 |
< |
BitSet& operator|= (const BitSet &bs) { orOperator (bs); return *this; } |
| 124 |
< |
BitSet& operator^= (const BitSet &bs) { xorOperator (bs); return *this; } |
| 125 |
< |
bool operator[] (int bitIndex) { return bitset_[bitIndex]; } |
| 122 |
> |
BitSet& operator&= (const BitSet &bs) { andOperator (bs); return *this; } |
| 123 |
> |
BitSet& operator|= (const BitSet &bs) { orOperator (bs); return *this; } |
| 124 |
> |
BitSet& operator^= (const BitSet &bs) { xorOperator (bs); return *this; } |
| 125 |
> |
BitSet& operator-= (const BitSet &bs) { |
| 126 |
> |
BitSet tmp = *this ^ bs; |
| 127 |
> |
*this &= tmp; |
| 128 |
> |
return *this; |
| 129 |
> |
} |
| 130 |
> |
|
| 131 |
> |
bool operator[] (int bitIndex) const { return bitset_[bitIndex]; } |
| 132 |
> |
friend BitSet operator| (const BitSet& bs1, const BitSet& bs2); |
| 133 |
> |
friend BitSet operator& (const BitSet& bs1, const BitSet& bs2); |
| 134 |
> |
friend BitSet operator^ (const BitSet& bs1, const BitSet& bs2); |
| 135 |
> |
friend BitSet operator- (const BitSet& bs1, const BitSet& bs2); |
| 136 |
> |
|
| 137 |
> |
friend bool operator== (const BitSet & bs1, const BitSet &bs2); |
| 138 |
|
|
| 139 |
< |
friend BitSet operator| (const BitSet& bs1, const BitSet& bs2); |
| 140 |
< |
friend BitSet operator& (const BitSet& bs1, const BitSet& bs2); |
| 125 |
< |
friend BitSet operator^ (const BitSet& bs1, const BitSet& bs2); |
| 126 |
< |
friend bool operator== (const BitSet & bs1, const BitSet &bs2); |
| 139 |
> |
//friend std::istream& operator>> ( std::istream&, const BitSet& bs); |
| 140 |
> |
friend std::ostream& operator<< ( std::ostream&, const BitSet& bs) ; |
| 141 |
|
|
| 142 |
< |
friend std::istream& operator>> ( std::istream&, const BitSet& bs); |
| 129 |
< |
friend std::ostream& operator<< ( std::ostream&, const BitSet& bs) ; |
| 142 |
> |
private: |
| 143 |
|
|
| 144 |
< |
private: |
| 145 |
< |
|
| 133 |
< |
/** Sets the bit at the specified index to the specified value. */ |
| 134 |
< |
void setBit(int bitIndex, bool value) { bitset_[bitIndex] = value; } |
| 144 |
> |
/** Sets the bit at the specified index to the specified value. */ |
| 145 |
> |
void setBit(int bitIndex, bool value) { bitset_[bitIndex] = value; } |
| 146 |
|
|
| 147 |
< |
/** Sets the bits from the specified fromIndex(inclusive) to the specified toIndex(exclusive) to the specified value. */ |
| 148 |
< |
void setBits(int fromIndex, int toIndex, bool value); |
| 147 |
> |
/** Sets the bits from the specified fromIndex(inclusive) to the specified toIndex(exclusive) to the specified value. */ |
| 148 |
> |
void setBits(int fromIndex, int toIndex, bool value); |
| 149 |
|
|
| 150 |
< |
std::vector<char> bitset_; |
| 151 |
< |
}; |
| 150 |
> |
std::vector<char> bitset_; |
| 151 |
> |
}; |
| 152 |
|
|
| 142 |
– |
template<typename T> |
| 143 |
– |
struct logical_xor :public std::binary_function<T, T, bool> { |
| 144 |
– |
double operator()(T x, T y) { return x ^ y; } |
| 145 |
– |
}; |
| 153 |
|
|
| 154 |
|
} |
| 155 |
|
#endif |