42 |
|
#ifndef UTILS_BITSET_HPP |
43 |
|
#define UTILS_BITSET_HPP |
44 |
|
|
45 |
< |
#include <functional> |
45 |
> |
#include <iostream> |
46 |
|
#include <vector> |
47 |
|
namespace oopse { |
48 |
|
|
55 |
|
/** */ |
56 |
|
BitSet() {} |
57 |
|
/** */ |
58 |
< |
BitSet(int nbits) { bitset_.resize(nbits); } |
58 |
> |
BitSet(int nbits) : bitset_(nbits) {clearAll(); } |
59 |
|
|
60 |
|
/** Returns the number of bits set to true in this BitSet. */ |
61 |
|
int countBits(); |
80 |
|
|
81 |
|
/** Returns true if no bits are set to true */ |
82 |
|
bool none(); |
83 |
+ |
|
84 |
+ |
int firstOffBit() { 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 |
< |
|
87 |
> |
int nextOffBit(int fromIndex) const; |
88 |
> |
|
89 |
> |
int firstOnBit() { return bitset_[0] ? 0 : nextOnBit(0); } |
90 |
> |
|
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); |
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); |
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 clear() { setRangeOff(0, size()); } |
112 |
> |
void clearAll() { setRangeOff(0, size()); } |
113 |
> |
|
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(); } |
118 |
|
|
119 |
|
/** Changes the size of BitSet*/ |
120 |
< |
void resize(int nbits) { bitset_.resize(nbits); } |
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]; } |
120 |
< |
|
125 |
> |
bool operator[] (int bitIndex) const { return bitset_[bitIndex]; } |
126 |
|
friend BitSet operator| (const BitSet& bs1, const BitSet& bs2); |
127 |
|
friend BitSet operator& (const BitSet& bs1, const BitSet& bs2); |
128 |
|
friend BitSet operator^ (const BitSet& bs1, const BitSet& bs2); |
142 |
|
std::vector<char> bitset_; |
143 |
|
}; |
144 |
|
|
140 |
– |
template<typename T> |
141 |
– |
struct logical_xor :public std::binary_function<T, T, bool> { |
142 |
– |
double operator()(T x, T y) { return x ^ y; } |
143 |
– |
}; |
145 |
|
|
146 |
|
} |
147 |
|
#endif |