8#ifndef Sawyer_BitFlags_H
9#define Sawyer_BitFlags_H
11#include <Sawyer/Sawyer.h>
62template<
typename E,
typename V =
int64_t>
71#ifdef SAWYER_HAVE_BOOST_SERIALIZATION
73 friend class boost::serialization::access;
76 void serialize(S &s,
const unsigned ) {
77 s & BOOST_SERIALIZATION_NVP(vector_);
81#ifdef SAWYER_HAVE_CEREAL
83 friend class cereal::access;
85 template<
class Archive>
86 void serialize(Archive &archive) {
87 archive(CEREAL_NVP(vector_));
101 : vector_(Vector(e)) {}
105 : vector_(other.vector_) {}
114 return (vector_ & Vector(e)) != 0;
119 return (vector_ & other.vector_) == other.vector_;
124 return (vector_ & other.vector_) != 0;
144 return (~vector_ & other.vector_) == other.vector_;
149 return (~vector_ & other.vector_) != 0;
154 vector_ |= Vector(e);
160 vector_ |= other.vector_;
166 vector_ &= ~Vector(e);
172 vector_ &= ~other.vector_;
184 bool retval =
isSet(e);
191 bool retval =
isSet(e);
204 vector_ = other.vector_;
212 return vector_ | other.vector_;
215 return vector_ | Vector(e);
223 return vector_ & other.vector_;
226 return vector_ & Vector(e);
240 return vector_ == other.vector_;
243 return vector_ != other.vector_;
246 return vector_ < other.vector_;
249 return vector_ <= other.vector_;
252 return vector_ > other.vector_;
255 return vector_ >= other.vector_;
264 std::vector<Enum>
split(std::vector<int64_t> constants, Vector &leftovers )
const {
265 leftovers = Vector(0);
266 std::vector<Enum> retval;
267 std::sort(constants.begin(), constants.end(), moreBits);
268 Vector tmp = vector_;
271 for (
size_t i=0; i<constants.size() && !found; ++i) {
272 if (Vector(tmp & constants[i]) == Vector(constants[i]) && constants[i] != 0) {
273 retval.push_back(Enum(constants[i]));
274 tmp &= ~constants[i];
288 std::vector<Enum> retval;
289 for (
size_t i = 0; i < 8*
sizeof(Enum); ++i) {
290 Enum e =
static_cast<Enum
>(uint64_t(1) << i);
297#if __cplusplus >= 201103L
302 void each(std::vector<int64_t> constants,
const F &functor)
const {
304 for (Enum e:
split(constants, leftovers))
312 void each(
const F &functor)
const {
313 for (Enum e:
split())
319 static size_t nBits(Vector
vector) {
321 for (
size_t i = 0; i < 8*
sizeof(Vector); ++i) {
322 if ((
vector & (Vector(1) << i)) != 0)
328 static bool moreBits(Vector a, Vector b) {
329 return nBits(a) > nBits(b);
Stores a vector of enum bit flags.
BitFlags & operator=(BitFlags other)
Set the vector to the same as another.
BitFlags & set(Enum e)
Set the specified bit.
BitFlags & clear(BitFlags other)
Clear all bits that are set in other.
bool operator<(BitFlags other) const
Compare two vectors.
bool isClear(Enum e) const
Test whether a bit is clear.
bool isAllClear(const BitFlags other) const
True if all specified bits are clear.
BitFlags & clear(Enum e)
Clear the specified bit.
BitFlags operator|(Enum e) const
Create a new vector that's the union of two vectors.
BitFlags(const BitFlags &other)
Copy constructor.
BitFlags operator|(BitFlags other) const
Create a new vector that's the union of two vectors.
bool operator<=(BitFlags other) const
Compare two vectors.
bool operator!=(BitFlags other) const
Compare two vectors.
Vector vector() const
Current value of the bit vector.
BitFlags intersection(Enum e) const
Create a new vector that's the intersection of two vectors.
bool operator==(BitFlags other) const
Compare two vectors.
std::vector< Enum > split(std::vector< int64_t > constants, Vector &leftovers) const
Split a vector into the individual enum values.
BitFlags intersection(BitFlags other) const
Create a new vector that's the intersection of two vectors.
bool operator>(BitFlags other) const
Compare two vectors.
bool isAllSet(BitFlags other) const
True if all specified bits are set.
bool testAndSet(Enum e)
Test whether a bit is set, then set it.
bool isAnySet() const
True if any bit is set.
BitFlags operator&(Enum e) const
Create a new vector that's the intersection of two vectors.
std::vector< Enum > split() const
Split a vector into the individual bits values.
bool testAndClear(Enum e)
Test whether a bit is set, then clear it.
BitFlags & clear()
Clear all bits.
BitFlags operator&(BitFlags other) const
Create a new vector that's the intersection of two vectors.
bool operator>=(BitFlags other) const
Compare two vectors.
bool isEmpty() const
True if no bits are set.
BitFlags & set(BitFlags other)
Set all bits that are set in other.
bool isAnySet(BitFlags other) const
True if any of the specified bits are set.
BitFlags(Vector v)
Construct bit vector from value or bit.
BitFlags & operator=(Vector v)
Set the vector to an exact value.
bool isSet(Enum e) const
Test whether a bit is set.
bool isAnyClear(const BitFlags other) const
True if any specified bit is clear.
BitFlags()
Default constructor with all bits clear.