diff options
Diffstat (limited to 'src/util')
-rw-r--r-- | src/util/bitset.h | 114 |
1 files changed, 114 insertions, 0 deletions
diff --git a/src/util/bitset.h b/src/util/bitset.h index 2404ce7f630..7bb5f3c83cf 100644 --- a/src/util/bitset.h +++ b/src/util/bitset.h @@ -132,4 +132,118 @@ __bitset_next_set(unsigned i, BITSET_WORD *tmp, for (__tmp = *(__set), __i = 0; \ (__i = __bitset_next_set(__i, &__tmp, __set, __size)) < __size;) +#ifdef __cplusplus + +/** + * Simple C++ wrapper of a bitset type of static size, with value semantics + * and basic bitwise arithmetic operators. The operators defined below are + * expected to have the same semantics as the same operator applied to other + * fundamental integer types. T is the name of the struct to instantiate + * it as, and N is the number of bits in the bitset. + */ +#define DECLARE_BITSET_T(T, N) struct T { \ + /* XXX - Replace this with an implicitly-defined \ + * constructor when support for C++11 defaulted \ + * constructors can be assumed (available on GCC 4.4 and \ + * later) in order to make the object trivially \ + * constructible like a fundamental integer type for \ + * convenience. \ + */ \ + T() \ + { \ + } \ + \ + T(BITSET_WORD x) \ + { \ + for (unsigned i = 0; i < BITSET_WORDS(N); i++, x = 0) \ + words[i] = x; \ + } \ + \ + EXPLICIT_CONVERSION \ + operator bool() const \ + { \ + for (unsigned i = 0; i < BITSET_WORDS(N); i++) \ + if (words[i]) \ + return true; \ + return false; \ + } \ + \ + friend bool \ + operator==(const T &b, const T &c) \ + { \ + return BITSET_EQUAL(b.words, c.words); \ + } \ + \ + friend bool \ + operator!=(const T &b, const T &c) \ + { \ + return !(b == c); \ + } \ + \ + friend T \ + operator~(const T &b) \ + { \ + T c; \ + for (unsigned i = 0; i < BITSET_WORDS(N); i++) \ + c.words[i] = ~b.words[i]; \ + return c; \ + } \ + \ + T & \ + operator|=(const T &b) \ + { \ + for (unsigned i = 0; i < BITSET_WORDS(N); i++) \ + words[i] |= b.words[i]; \ + return *this; \ + } \ + \ + friend T \ + operator|(const T &b, const T &c) \ + { \ + T d = b; \ + d |= c; \ + return d; \ + } \ + \ + T & \ + operator&=(const T &b) \ + { \ + for (unsigned i = 0; i < BITSET_WORDS(N); i++) \ + words[i] &= b.words[i]; \ + return *this; \ + } \ + \ + friend T \ + operator&(const T &b, const T &c) \ + { \ + T d = b; \ + d &= c; \ + return d; \ + } \ + \ + bool \ + test(unsigned i) const \ + { \ + return BITSET_TEST(words, i); \ + } \ + \ + T & \ + set(unsigned i) \ + { \ + BITSET_SET(words, i); \ + return *this; \ + } \ + \ + T & \ + clear(unsigned i) \ + { \ + BITSET_CLEAR(words, i); \ + return *this; \ + } \ + \ + BITSET_WORD words[BITSET_WORDS(N)]; \ + } + +#endif + #endif |