aboutsummaryrefslogtreecommitdiff
path: root/src/bitset.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/bitset.h')
-rw-r--r--src/bitset.h38
1 files changed, 38 insertions, 0 deletions
diff --git a/src/bitset.h b/src/bitset.h
new file mode 100644
index 0000000..2b46090
--- /dev/null
+++ b/src/bitset.h
@@ -0,0 +1,38 @@
+#ifndef ORYX_BITSET_H
+#define ORYX_BITSET_H
+
+#include <limits.h>
+#include <stdbool.h>
+
+#include "alloc.h"
+#include "common.h"
+
+typedef unsigned char bitset_t;
+
+#define _BITSET_BITS (sizeof(bitset_t) * CHAR_BIT)
+#define _BITSLOT(x) ((x) / _BITSET_BITS)
+#define _BITMASK(x) (1 << ((x) % _BITSET_BITS))
+
+/* Set- or test the bit as position X in BS */
+#define SETBIT(bs, x) ((bs)[_BITSLOT(x)] |= _BITMASK(x))
+#define TESTBIT(bs, x) ((bool)((bs)[_BITSLOT(x)] & _BITMASK(x)))
+
+/* Set the bit DST in BS if the bit SRC is set */
+#define SET_DST_IF_SRC(bs, dst, src) \
+ do { \
+ if (TESTBIT(bs, src)) \
+ SETBIT(bs, dst); \
+ } while (false)
+
+/* Allocate a new bitset capable of holding N bits */
+static inline bitset_t *mkbitset(arena_t *, size_t n)
+ __attribute__((always_inline, returns_nonnull, nonnull, malloc));
+
+bitset_t *
+mkbitset(arena_t *a, size_t n)
+{
+ size_t bits = CHAR_BIT * sizeof(bitset_t);
+ return arena_new(a, bitset_t, (n + bits - 1) / bits);
+}
+
+#endif /* !ORYX_BITSET_H */