42 |
|
#ifndef UTILS_BITSET_HPP |
43 |
|
#define UTILS_BITSET_HPP |
44 |
|
|
45 |
+ |
#include <iostream> |
46 |
+ |
#include <functional> |
47 |
|
#include <vector> |
48 |
|
namespace oopse { |
49 |
|
|
50 |
|
/** |
51 |
|
* @class BitSet BitSet.hpp "BitSet.hpp" |
52 |
< |
* @brief BitSet is a wrapper class of std::vector<char> |
52 |
> |
* @brief BitSet is a wrapper class of std::vector<char> to act as a growable std::bitset |
53 |
|
*/ |
54 |
|
class BitSet { |
55 |
|
public: |
56 |
|
/** */ |
57 |
|
BitSet() {} |
58 |
|
/** */ |
59 |
< |
BitSet(int nbits) { bitset_.resize(nbits); } |
59 |
> |
BitSet(int nbits) : bitset_(nbits) {clearAll(); } |
60 |
|
|
61 |
|
/** Returns the number of bits set to true in this BitSet. */ |
62 |
|
int countBits(); |
76 |
|
/** Returns a new BitSet composed of bits from this BitSet from fromIndex(inclusive) to toIndex(exclusive). */ |
77 |
|
BitSet get(int fromIndex, int toIndex); |
78 |
|
|
79 |
< |
/** Returns true if this BitSet contains no bits that are set to true. */ |
80 |
< |
bool isEmpty(); |
81 |
< |
|
79 |
> |
/** Returns true if any bits are set to true */ |
80 |
> |
bool any() {return !none(); } |
81 |
> |
|
82 |
> |
/** Returns true if no bits are set to true */ |
83 |
> |
bool none(); |
84 |
> |
|
85 |
|
/** Returns the index of the first bit that is set to false that occurs on or after the specified starting index.*/ |
86 |
< |
int nextOffBit(int fromIndex); |
86 |
> |
int nextOffBit(int fromIndex) const; |
87 |
|
|
88 |
|
/** Returns the index of the first bit that is set to true that occurs on or after the specified starting index. */ |
89 |
< |
int nextOnBit(int fromIndex); |
89 |
> |
int nextOnBit(int fromIndex) const; |
90 |
|
|
91 |
|
/** Performs a logical AND of this target bit set with the argument bit set. */ |
92 |
< |
void and(const BitSet& bs); |
93 |
< |
|
89 |
< |
/** Clears all of the bits in this BitSet whose corresponding bit is set in the specified BitSet. */ |
90 |
< |
void andNot(const BitSet& bs); |
91 |
< |
|
92 |
> |
void andOperator (const BitSet& bs); |
93 |
> |
|
94 |
|
/** Performs a logical OR of this bit set with the bit set argument. */ |
95 |
< |
void or(const BitSet& bs); |
95 |
> |
void orOperator (const BitSet& bs); |
96 |
|
|
97 |
|
/** Performs a logical XOR of this bit set with the bit set argument. */ |
98 |
< |
void xor(const BitSet& bs); |
98 |
> |
void xorOperator (const BitSet& bs); |
99 |
|
|
100 |
|
void setBitOn(int bitIndex) { setBit(bitIndex, true); } |
101 |
|
|
106 |
|
void setRangeOff(int fromIndex, int toIndex) { setBits(fromIndex, toIndex, false); } |
107 |
|
|
108 |
|
/** Sets all of the bits in this BitSet to false. */ |
109 |
< |
void clear() { setRangeOff(0, size()); } |
109 |
> |
void clearAll() { setRangeOff(0, size()); } |
110 |
> |
|
111 |
> |
void setAll() { setRangeOn(0, size()); } |
112 |
|
|
113 |
|
/** Returns the number of bits of space actually in use by this BitSet to represent bit values. */ |
114 |
< |
int size() { return bitset_.size(); } |
114 |
> |
int size() const { return bitset_.size(); } |
115 |
|
|
116 |
|
/** Changes the size of BitSet*/ |
117 |
< |
bool resize(int nbits) { bitset_.resize(); } |
117 |
> |
void resize(int nbits); |
118 |
|
|
119 |
< |
BitSet& operator&= (const BitSet &bs) { and(bs); return *this; } |
120 |
< |
BitSet& operator|= (const BitSet &bs) { or(bs); return *this; } |
121 |
< |
BitSet& operator^= (const BitSet &bs) { xor(bs); return *this; } |
122 |
< |
bool operator[] (int bitIndex) { return bitset_[bitIndex]; } |
123 |
< |
|
124 |
< |
friend BitSet operator| (BitSet& bs1, BitSet& bs2); |
125 |
< |
friend BitSet operator& (BitSet& bs1, BitSet& bs2); |
122 |
< |
friend BitSet operator^ (BitSet& bs1, BitSet& bs2); |
119 |
> |
BitSet& operator&= (const BitSet &bs) { andOperator (bs); return *this; } |
120 |
> |
BitSet& operator|= (const BitSet &bs) { orOperator (bs); return *this; } |
121 |
> |
BitSet& operator^= (const BitSet &bs) { xorOperator (bs); return *this; } |
122 |
> |
bool operator[] (int bitIndex) const { return bitset_[bitIndex]; } |
123 |
> |
friend BitSet operator| (const BitSet& bs1, const BitSet& bs2); |
124 |
> |
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); |
127 |
|
|
128 |
< |
friend std::istream& operator>> ( std::istream&, BitSet& bs); |
128 |
> |
friend std::istream& operator>> ( std::istream&, const BitSet& bs); |
129 |
|
friend std::ostream& operator<< ( std::ostream&, const BitSet& bs) ; |
130 |
|
|
131 |
|
private: |
134 |
|
void setBit(int bitIndex, bool value) { bitset_[bitIndex] = value; } |
135 |
|
|
136 |
|
/** Sets the bits from the specified fromIndex(inclusive) to the specified toIndex(exclusive) to the specified value. */ |
137 |
< |
void setBits(int fromIndex, int toIndex, bool value) {} |
137 |
> |
void setBits(int fromIndex, int toIndex, bool value); |
138 |
|
|
139 |
|
std::vector<char> bitset_; |
140 |
|
}; |
141 |
|
|
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 |
+ |
}; |
146 |
+ |
|
147 |
|
} |
148 |
|
#endif |