# | Line 42 | Line 42 | |
---|---|---|
42 | #ifndef UTILS_BITSET_HPP | |
43 | #define UTILS_BITSET_HPP | |
44 | ||
45 | + | #include <functional> |
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> |
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_.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(); | |
# | Line 74 | Line 75 | class BitSet { | |
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 this BitSet contains no bits that are set to true. */ |
79 | < | bool isEmpty(); |
80 | < | |
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(); |
83 | > | |
84 | /** Returns the index of the first bit that is set to false that occurs on or after the specified starting index.*/ | |
85 | int nextOffBit(int fromIndex); | |
86 | ||
# | Line 84 | Line 88 | class BitSet { | |
88 | int nextOnBit(int fromIndex); | |
89 | ||
90 | /** Performs a logical AND of this target bit set with the argument bit set. */ | |
91 | < | void and(const BitSet& bs); |
92 | < | |
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 | < | |
91 | > | void andOperator (const BitSet& bs); |
92 | > | |
93 | /** Performs a logical OR of this bit set with the bit set argument. */ | |
94 | < | void or(const BitSet& bs); |
94 | > | void orOperator (const BitSet& bs); |
95 | ||
96 | /** Performs a logical XOR of this bit set with the bit set argument. */ | |
97 | < | void xor(const BitSet& bs); |
97 | > | void xorOperator (const BitSet& bs); |
98 | ||
99 | void setBitOn(int bitIndex) { setBit(bitIndex, true); } | |
100 | ||
# | Line 104 | Line 105 | class BitSet { | |
105 | void setRangeOff(int fromIndex, int toIndex) { setBits(fromIndex, toIndex, false); } | |
106 | ||
107 | /** Sets all of the bits in this BitSet to false. */ | |
108 | < | void clear() { setRangeOff(0, size()); } |
108 | > | void clearAll() { setRangeOff(0, size()); } |
109 | > | |
110 | > | void setAll() { setRangeOn(0, size()); } |
111 | ||
112 | /** Returns the number of bits of space actually in use by this BitSet to represent bit values. */ | |
113 | < | int size() { return bitset_.size(); } |
113 | > | int size() const { return bitset_.size(); } |
114 | ||
115 | /** Changes the size of BitSet*/ | |
116 | < | bool resize(int nbits) { bitset_.resize(); } |
116 | > | void resize(int nbits); |
117 | ||
118 | < | BitSet& operator&= (const BitSet &bs) { and(bs); return *this; } |
119 | < | BitSet& operator|= (const BitSet &bs) { or(bs); return *this; } |
120 | < | BitSet& operator^= (const BitSet &bs) { xor(bs); return *this; } |
118 | > | BitSet& operator&= (const BitSet &bs) { andOperator (bs); return *this; } |
119 | > | BitSet& operator|= (const BitSet &bs) { orOperator (bs); return *this; } |
120 | > | BitSet& operator^= (const BitSet &bs) { xorOperator (bs); return *this; } |
121 | bool operator[] (int bitIndex) { return bitset_[bitIndex]; } | |
122 | ||
123 | < | friend BitSet operator| (BitSet& bs1, BitSet& bs2); |
124 | < | friend BitSet operator& (BitSet& bs1, BitSet& bs2); |
125 | < | friend BitSet operator^ (BitSet& bs1, BitSet& bs2); |
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: | |
# | Line 131 | Line 134 | class BitSet { | |
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 |
– | Removed lines |
+ | Added lines |
< | Changed lines |
> | Changed lines |