aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/u_bits.h
diff options
context:
space:
mode:
authorlemon <lsof@mailbox.org>2026-03-17 18:30:10 +0100
committerlemon <lsof@mailbox.org>2026-03-17 18:30:37 +0100
commit04930d578e65d560253d0c24af43e0ecd06117c8 (patch)
tree6e90f09af0030160dff034bb8e654580779f47a7 /src/u_bits.h
parent3eeb6f219e4d32160fa10895b57a8ddfefff5ff7 (diff)
Refactor: move some utils from antcc.h to their own headers
Diffstat (limited to 'src/u_bits.h')
-rw-r--r--src/u_bits.h76
1 files changed, 76 insertions, 0 deletions
diff --git a/src/u_bits.h b/src/u_bits.h
new file mode 100644
index 0000000..a4cef85
--- /dev/null
+++ b/src/u_bits.h
@@ -0,0 +1,76 @@
+#pragma once
+
+#include "antcc.h"
+
+static inline bool
+bstest(const struct bitset *bs, uint i)
+{
+ return bs[i/BSNBIT].u >> i%BSNBIT & 1;
+}
+
+static inline void
+bsset(struct bitset *bs, uint i)
+{
+ bs[i/BSNBIT].u |= 1ull << i%BSNBIT;
+}
+
+static inline void
+bsclr(struct bitset *bs, uint i)
+{
+ bs[i/BSNBIT].u &= ~(1ull << i%BSNBIT);
+}
+
+static inline void
+bszero(struct bitset bs[/*siz*/], uint siz)
+{
+ memset(bs, 0, siz * sizeof *bs);
+}
+
+static inline void
+bscopy(struct bitset dst[/*siz*/], const struct bitset src[/*siz*/], uint siz)
+{
+ while (siz--) dst++->u = src++->u;
+}
+
+static inline void
+bsunion(struct bitset dst[/*siz*/], const struct bitset src[/*siz*/], uint siz)
+{
+ while (siz--) dst++->u |= src++->u;
+}
+
+static inline uint
+bscount(struct bitset bs[/*siz*/], uint siz)
+{
+ uint n = 0;
+ while (siz--) n += popcnt(bs++->u);
+ return n;
+}
+
+static inline bool
+bsiter(uint *i, struct bitset bs[/*siz*/], uint siz)
+{
+ uint k = *i/BSNBIT, j = *i%BSNBIT;
+ if (k >= siz) return 0;
+ size_t t = bs[k].u & ~(((size_t)1 << j) - 1);
+ while (!t) {
+ if (++k >= siz) return 0;
+ t = bs[k].u;
+ }
+ *i = k*BSNBIT + lowestsetbit(t);
+ return 1;
+}
+#define bs_each(T, var, bs, siz) for (T (var) = 0; bsiter(&(var), (bs), (siz)); ++(var))
+
+static inline bool
+bsiterzr(uint *i, struct bitset bs[/*siz*/], uint siz)
+{
+ uint k = *i/BSNBIT, j = *i%BSNBIT;
+ if (k >= siz) return 0;
+ size_t t = ~bs[k].u & ~(((size_t)1 << j) - 1);
+ while (!t) {
+ if (++k >= siz) return 0;
+ t = ~bs[k].u;
+ }
+ *i = k*BSNBIT + lowestsetbit(t);
+ return 1;
+}