/*
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see .
*/
/*
simple bitmask class
*/
#pragma once
#include
#include
#include
template
class Bitmask {
public:
Bitmask() :
numbits(num_bits),
numwords((num_bits+31)/32) {
clearall();
}
Bitmask &operator=(const Bitmask&other) {
memcpy(bits, other.bits, sizeof(bits[0])*other.numwords);
return *this;
}
bool operator==(const Bitmask&other) {
if (other.numbits != numbits) {
return false;
} else {
return memcmp(bits, other.bits, sizeof(bits[0])*numwords) == 0;
}
}
bool operator!=(const Bitmask&other) {
return !(*this == other);
}
Bitmask(const Bitmask &other) = delete;
// set given bitnumber
void set(uint16_t bit) {
// ignore an invalid bit number
if (bit >= numbits) {
INTERNAL_ERROR(AP_InternalError::error_t::bitmask_range);
return;
}
uint16_t word = bit/32;
uint8_t ofs = bit & 0x1f;
bits[word] |= (1U << ofs);
}
// set all bits
void setall(void) {
// set all words to 111...
for (uint16_t i=0; i= numbits) {
INTERNAL_ERROR(AP_InternalError::error_t::bitmask_range);
return false;
}
#endif
return (bits[word] & (1U << ofs)) != 0;
}
// return true if all bits are clear
bool empty(void) const {
for (uint16_t i=0; i