add invert to bitset

This commit is contained in:
Green Sky 2024-07-08 18:17:15 +02:00
parent 6b1bb50f16
commit 17d2baf736
No known key found for this signature in database

View File

@ -18,6 +18,7 @@ struct BitSet {
BitSet(size_t size) {
_bytes.resize((size+7)/8);
}
BitSet(const std::vector<uint8_t>& bytes) : _bytes(bytes) {}
BitSet& operator=(const BitSet&) = default;
BitSet& operator=(BitSet&&) = default;
@ -75,6 +76,20 @@ struct BitSet {
return _bytes.size();
}
BitSet& invert(void) {
for (auto& c : _bytes) {
c = ~c;
}
return *this;
}
[[nodiscard]] BitSet invert(void) const {
BitSet copy = *this;
copy.invert();
return copy;
}
BitSet& merge(const BitSet& other) {
if (other.size_bytes() > size_bytes()) {
_bytes.resize(other.size_bytes());
@ -87,6 +102,12 @@ struct BitSet {
return *this;
}
[[nodiscard]] BitSet merge(const BitSet& other) const {
BitSet copy = *this;
copy.merge(other);
return copy;
}
// start is the first bit in other relative to self
BitSet& merge(const BitSet& other, size_t start) {
// TODO: efficent implementation