+/**
+ * Returns the position of the next bit starting from (and including)
+ * a given position.
+ *
+ * @param bitset a bitset
+ * @param pos the first position to check
+ * @param last first position that is not checked anymore
+ * @param set if 0 search for unset bit, else for set bit
+ *
+ * @return the first position where a matched bit was found.
+ * (size_t)-1 if no bit was found.
+ */
+static inline size_t rbitset_next_max(const unsigned *bitset, size_t pos,
+ size_t last, bool set)
+{
+ size_t p;
+ size_t elem_pos = pos / BITS_PER_ELEM;
+ size_t bit_pos = pos % BITS_PER_ELEM;
+
+ unsigned elem = bitset[elem_pos];
+ unsigned mask = set ? 0u : ~0u;
+ size_t res = (size_t)-1;
+
+ /*
+ * Mask out the bits smaller than pos in the current unit.
+ * We are only interested in bits set higher than pos.
+ */
+ unsigned in_elem_mask = (1u << bit_pos) - 1;
+
+ assert(pos < last);
+
+ elem ^= mask;
+ p = ntz(elem & ~in_elem_mask);
+
+ /* If there is a bit set in the current elem, exit. */
+ if (p < BITS_PER_ELEM) {
+ res = elem_pos * BITS_PER_ELEM + p;
+ } else {
+ size_t n = BITSET_SIZE_ELEMS(last);
+ /* Else search for set bits in the next units. */
+ for (elem_pos++; elem_pos < n; elem_pos++) {
+ elem = bitset[elem_pos] ^ mask;
+
+ p = ntz(elem);
+ if (p < BITS_PER_ELEM) {
+ res = elem_pos * BITS_PER_ELEM + p;
+ break;
+ }
+ }
+ }
+ if (res >= last)
+ res = (size_t)-1;
+
+ return res;
+}
+
+/**
+ * Inplace Intersection of two sets.
+ *
+ * @param dst the destination bitset and first operand
+ * @param src the second bitset
+ * @param size size of both bitsets in bits
+ */
+static inline void rbitset_and(unsigned *dst, const unsigned *src, size_t size)
+{
+ size_t i, n = BITSET_SIZE_ELEMS(size);
+
+ for (i = 0; i < n; ++i) {
+ dst[i] &= src[i];
+ }
+}
+
+/**
+ * Inplace Union of two sets.
+ *
+ * @param dst the destination bitset and first operand
+ * @param src the second bitset
+ * @param size size of both bitsets in bits
+ */
+static inline void rbitset_or(unsigned *dst, const unsigned *src, size_t size)
+{
+ size_t i, n = BITSET_SIZE_ELEMS(size);
+
+ for (i = 0; i < n; ++i) {
+ dst[i] |= src[i];
+ }
+}
+
+/**
+ * Remove all bits in src from dst.
+ *
+ * @param dst the destination bitset and first operand
+ * @param src the second bitset
+ * @param size size of both bitsets in bits
+ */
+static inline void rbitset_andnot(unsigned *dst, const unsigned *src, size_t size)
+{
+ size_t i, n = BITSET_SIZE_ELEMS(size);
+
+ for (i = 0; i < n; ++i) {
+ dst[i] &= ~src[i];
+ }