util/bitset: Add C++ wrapper for static-size bitsets.
authorFrancisco Jerez <currojerez@riseup.net>
Mon, 12 Feb 2018 22:09:24 +0000 (14:09 -0800)
committerFrancisco Jerez <currojerez@riseup.net>
Sat, 24 Feb 2018 23:28:36 +0000 (15:28 -0800)
Reviewed-by: Plamena Manolova <plamena.manolova@intel.com>
src/util/bitset.h

index 2404ce7f630296b1c7622201e443f8b58e619406..7bb5f3c83cfdb811e00d426d6ef0a7ab6dee41a3 100644 (file)
@@ -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