diff options
author | Francisco Jerez <[email protected]> | 2018-02-12 14:09:24 -0800 |
---|---|---|
committer | Francisco Jerez <[email protected]> | 2018-02-24 15:28:36 -0800 |
commit | bdbc2ffa4219b39e47a27decbc603d445286d92d (patch) | |
tree | 9605f63105d3ad376ad271544a7e5f3008df87d0 /src/util | |
parent | 8d1f1ce4124c1e0dbfc5f3d0578fbee6e24140c8 (diff) |
util/bitset: Add C++ wrapper for static-size bitsets.
Reviewed-by: Plamena Manolova <[email protected]>
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 |