| 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 |
> |
return std::count(bitset_.begin(), bitset_.end(), true); |
| 52 |
|
} |
| 53 |
|
|
| 54 |
|
void BitSet::flip(int fromIndex, int toIndex) { |
| 74 |
|
return result; |
| 75 |
|
} |
| 76 |
|
|
| 77 |
< |
bool BitSet::isEmpty() { |
| 77 |
> |
bool BitSet::none() { |
| 78 |
|
std::vector<char>::iterator i = std::find(bitset_.begin(), bitset_.end(), true); |
| 79 |
|
return i == bitset_.end() ? true : false; |
| 80 |
|
} |
| 81 |
|
|
| 82 |
< |
int BitSet::nextOffBit(int fromIndex) { |
| 82 |
> |
int BitSet::nextOffBit(int fromIndex) const { |
| 83 |
> |
++fromIndex; |
| 84 |
> |
while (fromIndex < size()) { |
| 85 |
> |
if (!bitset_[fromIndex]) { |
| 86 |
> |
return fromIndex; |
| 87 |
> |
} |
| 88 |
> |
++fromIndex; |
| 89 |
> |
} |
| 90 |
|
|
| 91 |
+ |
return -1; |
| 92 |
|
} |
| 93 |
|
|
| 94 |
< |
int BitSet::nextOnBit(int fromIndex); |
| 94 |
> |
int BitSet::nextOnBit(int fromIndex) const { |
| 95 |
> |
++fromIndex; |
| 96 |
> |
while (fromIndex < size()) { |
| 97 |
> |
if (bitset_[fromIndex]) { |
| 98 |
> |
return fromIndex; |
| 99 |
> |
} |
| 100 |
> |
++fromIndex; |
| 101 |
> |
} |
| 102 |
|
|
| 103 |
< |
void BitSet::and(const BitSet& bs) { |
| 103 |
> |
return -1; |
| 104 |
> |
} |
| 105 |
> |
|
| 106 |
> |
void BitSet::andOperator (const BitSet& bs) { |
| 107 |
|
assert(size() == bs.size()); |
| 108 |
|
|
| 109 |
|
std::transform(bs.bitset_.begin(), bs.bitset_.end(), bitset_.begin(), bitset_.begin(), std::logical_and<bool>()); |
| 110 |
|
} |
| 111 |
|
|
| 112 |
< |
void BitSet::andNot(const BitSet& bs) { |
| 112 |
> |
void BitSet::orOperator (const BitSet& bs) { |
| 113 |
|
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()); |
| 114 |
|
std::transform(bs.bitset_.begin(), bs.bitset_.end(), bitset_.begin(), bitset_.begin(), std::logical_or<bool>()); |
| 115 |
|
} |
| 116 |
|
|
| 117 |
< |
void BitSet::xor(const BitSet& bs); { |
| 117 |
> |
void BitSet::xorOperator (const BitSet& bs) { |
| 118 |
|
assert(size() == bs.size()); |
| 119 |
|
std::transform(bs.bitset_.begin(), bs.bitset_.end(), bitset_.begin(), bitset_.begin(), oopse::logical_xor<bool>()); |
| 120 |
|
} |
| 128 |
|
std::fill(first, last, value); |
| 129 |
|
} |
| 130 |
|
|
| 131 |
< |
BitSet operator| (BitSet& bs1, BitSet& bs2) { |
| 131 |
> |
void BitSet::resize(int nbits) { |
| 132 |
> |
int oldSize = size(); |
| 133 |
> |
bitset_.resize(nbits); |
| 134 |
> |
if (nbits > oldSize) { |
| 135 |
> |
std::fill(bitset_.begin()+oldSize, bitset_.begin()+nbits+1, false); |
| 136 |
> |
} |
| 137 |
> |
} |
| 138 |
> |
|
| 139 |
> |
BitSet operator| (const BitSet& bs1, const BitSet& bs2) { |
| 140 |
|
assert(bs1.size() == bs2.size()); |
| 141 |
|
|
| 142 |
|
BitSet result(bs1); |
| 144 |
|
return result; |
| 145 |
|
} |
| 146 |
|
|
| 147 |
< |
BitSet operator& (BitSet& bs1, BitSet& bs2) { |
| 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); |
| 165 |
|
return std::equal(bs1.bitset_.begin(), bs1.bitset_.end(), bs2.bitset_.begin()); |
| 166 |
|
} |
| 167 |
|
|
| 168 |
< |
std::istream& operator>> ( std::istream& is, BitSet& bs) { |
| 168 |
> |
std::istream& operator>> ( std::istream& is, const BitSet& bs) { |
| 169 |
|
|
| 170 |
+ |
return is; |
| 171 |
|
} |
| 172 |
|
|
| 173 |
|
std::ostream& operator<< ( std::ostream& os, const BitSet& bs) { |
| 174 |
< |
|
| 174 |
> |
for (int i = 0; i < bs.bitset_.size(); ++i) { |
| 175 |
> |
std::string val = bs[i] ? "true" : "false"; |
| 176 |
> |
os << "BitSet[" << i <<"] = " << val << std::endl; |
| 177 |
> |
} |
| 178 |
> |
|
| 179 |
> |
return os; |
| 180 |
|
} |
| 181 |
|
|
| 182 |
|
} |