39 |
|
* such damages. |
40 |
|
*/ |
41 |
|
|
42 |
– |
#include "utils/BitSet.hpp" |
42 |
|
#include <algorithm> |
43 |
+ |
#include <cassert> |
44 |
+ |
#include <string> |
45 |
+ |
|
46 |
+ |
#include "utils/BitSet.hpp" |
47 |
+ |
#include "utils/Algorithm.hpp" |
48 |
+ |
|
49 |
|
namespace oopse { |
50 |
|
int BitSet::countBits() { |
51 |
< |
std::count(bitset_.begin(), bitset_.end(), true); |
51 |
> |
#ifdef __RWSTD |
52 |
> |
//For the compiler(Sun, MSVC6.0) binding with RougeWave STL Library, we need to use old-style |
53 |
> |
// std::count which is error-prone. |
54 |
> |
int count = 0; |
55 |
> |
std::count(bitset_.begin(), bitset_.end(), true, count); |
56 |
> |
return count; |
57 |
> |
#else |
58 |
> |
return std::count(bitset_.begin(), bitset_.end(), true); |
59 |
> |
#endif |
60 |
|
} |
61 |
|
|
62 |
|
void BitSet::flip(int fromIndex, int toIndex) { |
82 |
|
return result; |
83 |
|
} |
84 |
|
|
85 |
< |
bool BitSet::isEmpty() { |
85 |
> |
bool BitSet::none() { |
86 |
|
std::vector<char>::iterator i = std::find(bitset_.begin(), bitset_.end(), true); |
87 |
|
return i == bitset_.end() ? true : false; |
88 |
|
} |
89 |
|
|
90 |
< |
int BitSet::nextOffBit(int fromIndex) { |
90 |
> |
int BitSet::nextOffBit(int fromIndex) const { |
91 |
> |
++fromIndex; |
92 |
> |
while (fromIndex < size()) { |
93 |
> |
if (!bitset_[fromIndex]) { |
94 |
> |
return fromIndex; |
95 |
> |
} |
96 |
> |
++fromIndex; |
97 |
> |
} |
98 |
|
|
99 |
+ |
return -1; |
100 |
|
} |
101 |
|
|
102 |
< |
int BitSet::nextOnBit(int fromIndex); |
102 |
> |
int BitSet::nextOnBit(int fromIndex) const { |
103 |
> |
++fromIndex; |
104 |
> |
while (fromIndex < size()) { |
105 |
> |
if (bitset_[fromIndex]) { |
106 |
> |
return fromIndex; |
107 |
> |
} |
108 |
> |
++fromIndex; |
109 |
> |
} |
110 |
|
|
111 |
< |
void BitSet::and(const BitSet& bs) { |
111 |
> |
return -1; |
112 |
> |
} |
113 |
> |
|
114 |
> |
void BitSet::andOperator (const BitSet& bs) { |
115 |
|
assert(size() == bs.size()); |
116 |
|
|
117 |
|
std::transform(bs.bitset_.begin(), bs.bitset_.end(), bitset_.begin(), bitset_.begin(), std::logical_and<bool>()); |
118 |
|
} |
119 |
|
|
120 |
< |
void BitSet::andNot(const BitSet& bs) { |
120 |
> |
void BitSet::orOperator (const BitSet& bs) { |
121 |
|
assert(size() == bs.size()); |
91 |
– |
std::transform(bs.bitset_.begin(), bs.bitset_.end(), bitset_.begin(), bitset_.begin(), oopse::logical_andNot<bool>()); |
92 |
– |
} |
93 |
– |
|
94 |
– |
void BitSet::or(const BitSet& bs){ |
95 |
– |
assert(size() == bs.size()); |
122 |
|
std::transform(bs.bitset_.begin(), bs.bitset_.end(), bitset_.begin(), bitset_.begin(), std::logical_or<bool>()); |
123 |
|
} |
124 |
|
|
125 |
< |
void BitSet::xor(const BitSet& bs); { |
125 |
> |
void BitSet::xorOperator (const BitSet& bs) { |
126 |
|
assert(size() == bs.size()); |
127 |
|
std::transform(bs.bitset_.begin(), bs.bitset_.end(), bitset_.begin(), bitset_.begin(), oopse::logical_xor<bool>()); |
128 |
|
} |
136 |
|
std::fill(first, last, value); |
137 |
|
} |
138 |
|
|
139 |
< |
BitSet operator| (BitSet& bs1, BitSet& bs2) { |
139 |
> |
void BitSet::resize(int nbits) { |
140 |
> |
int oldSize = size(); |
141 |
> |
bitset_.resize(nbits); |
142 |
> |
if (nbits > oldSize) { |
143 |
> |
std::fill(bitset_.begin()+oldSize, bitset_.begin()+nbits+1, false); |
144 |
> |
} |
145 |
> |
} |
146 |
> |
|
147 |
> |
BitSet operator| (const BitSet& bs1, const BitSet& bs2) { |
148 |
|
assert(bs1.size() == bs2.size()); |
149 |
|
|
150 |
|
BitSet result(bs1); |
152 |
|
return result; |
153 |
|
} |
154 |
|
|
155 |
< |
BitSet operator& (BitSet& bs1, BitSet& bs2) { |
155 |
> |
BitSet operator& (const BitSet& bs1, const BitSet& bs2) { |
156 |
|
assert(bs1.size() == bs2.size()); |
157 |
|
|
158 |
|
BitSet result(bs1); |
160 |
|
return result; |
161 |
|
} |
162 |
|
|
163 |
< |
BitSet operator^ (BitSet& bs1, BitSet& bs2) { |
163 |
> |
BitSet operator^ (const BitSet& bs1, const BitSet& bs2) { |
164 |
|
assert(bs1.size() == bs2.size()); |
165 |
|
|
166 |
|
BitSet result(bs1); |
173 |
|
return std::equal(bs1.bitset_.begin(), bs1.bitset_.end(), bs2.bitset_.begin()); |
174 |
|
} |
175 |
|
|
176 |
< |
std::istream& operator>> ( std::istream& is, BitSet& bs) { |
176 |
> |
std::istream& operator>> ( std::istream& is, const BitSet& bs) { |
177 |
|
|
178 |
+ |
return is; |
179 |
|
} |
180 |
|
|
181 |
|
std::ostream& operator<< ( std::ostream& os, const BitSet& bs) { |
182 |
< |
|
182 |
> |
for (int i = 0; i < bs.bitset_.size(); ++i) { |
183 |
> |
std::string val = bs[i] ? "true" : "false"; |
184 |
> |
os << "BitSet[" << i <<"] = " << val << std::endl; |
185 |
> |
} |
186 |
> |
|
187 |
> |
return os; |
188 |
|
} |
189 |
|
|
190 |
|
} |