Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions docs/bitset.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,9 @@ auto result = transform_reduce([](auto i) { return i * 2 },
// result is 1*2 + 3*2 + 5*2 + 7*2
----

NOTE: Both `for_each` and `transform_reduce` are also member functions on
`bitset`, as well as free functions.

=== `type_bitset`

A `type_bitset` is much the same as a `bitset`, except that it is indexed by types.
Expand Down
56 changes: 28 additions & 28 deletions include/stdx/bitset.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,37 +145,9 @@ class bitset {
return not std::is_enum_v<T> or std::is_same_v<T, decltype(Size)>;
}

template <detail::bit_spec Spec, typename F>
constexpr auto for_each(F &&f) const -> F {
std::size_t idx = 0;
for (auto i = std::size_t{}; i < storage_size - 1; ++i) {
Spec::template fn<bit, iter_arg_t,
std::numeric_limits<elem_t>::max()>(storage[i],
idx, f);
idx += std::numeric_limits<elem_t>::digits;
}
Spec::template fn<bit, iter_arg_t, lastmask>(highbits(), idx, f);
return std::forward<F>(f);
}

template <detail::bit_spec Spec, typename F, auto M, typename... S>
friend constexpr auto for_each(F &&f, bitset<M, S> const &...bs) -> F;

template <typename T, typename F, typename R>
constexpr auto transform_reduce(F &&f, R &&r, T init) const -> T {
std::size_t i = 0;
for (auto e : storage) {
while (e != 0) {
auto const offset = static_cast<std::size_t>(countr_zero(e));
e &= static_cast<elem_t>(~(bit << offset));
init =
r(std::move(init), f(static_cast<iter_arg_t>(i + offset)));
}
i += std::numeric_limits<elem_t>::digits;
}
return init;
}

template <typename T, typename F, typename R, auto M, typename... S>
friend constexpr auto transform_reduce(F &&f, R &&r, T init,
bitset<M, S> const &...bs) -> T;
Expand Down Expand Up @@ -494,6 +466,34 @@ class bitset {
}
return *this;
}

template <detail::bit_spec Spec = set_bit, typename F>
constexpr auto for_each(F &&f) const -> F {
std::size_t idx = 0;
for (auto i = std::size_t{}; i < storage_size - 1; ++i) {
Spec::template fn<bit, iter_arg_t,
std::numeric_limits<elem_t>::max()>(storage[i],
idx, f);
idx += std::numeric_limits<elem_t>::digits;
}
Spec::template fn<bit, iter_arg_t, lastmask>(highbits(), idx, f);
return std::forward<F>(f);
}

template <typename T, typename F, typename R>
constexpr auto transform_reduce(F &&f, R &&r, T init) const -> T {
std::size_t i = 0;
for (auto e : storage) {
while (e != 0) {
auto const offset = static_cast<std::size_t>(countr_zero(e));
e &= static_cast<elem_t>(~(bit << offset));
init =
r(std::move(init), f(static_cast<iter_arg_t>(i + offset)));
}
i += std::numeric_limits<elem_t>::digits;
}
return init;
}
};

template <detail::bit_spec Spec = set_bit, typename F, auto M, typename... S>
Expand Down
22 changes: 22 additions & 0 deletions test/bitset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,14 @@ TEMPLATE_TEST_CASE("for_each", "[bitset]", std::uint8_t, std::uint16_t,
CHECK(result == bs);
}

TEMPLATE_TEST_CASE("member for_each", "[bitset]", std::uint8_t, std::uint16_t,
std::uint32_t, std::uint64_t) {
constexpr auto bs = stdx::bitset<64, TestType>{0x01020304'05060708ul};
auto result = decltype(bs){};
bs.for_each([&](auto i) { result.set(i); });
CHECK(result == bs);
}

TEMPLATE_TEST_CASE("for_each (unset bits)", "[bitset]", std::uint8_t,
std::uint16_t, std::uint32_t, std::uint64_t) {
constexpr auto bs = stdx::bitset<64, TestType>{0x01020304'05060708ul};
Expand Down Expand Up @@ -396,6 +404,20 @@ TEMPLATE_TEST_CASE("transform_reduce", "[bitset]", std::uint8_t, std::uint16_t,
CHECK(calls == bs.count());
}

TEMPLATE_TEST_CASE("member transform_reduce", "[bitset]", std::uint8_t,
std::uint16_t, std::uint32_t, std::uint64_t) {
constexpr auto bs = stdx::bitset<8, TestType>{0b10101010ul};
int calls{};
auto const result = bs.transform_reduce(
[&](auto i) {
++calls;
return i == 3 and calls == 2;
},
std::logical_or{}, false);
CHECK(result);
CHECK(calls == bs.count());
}

TEMPLATE_TEST_CASE("set range of bits (lsb, length)", "[bitset]", std::uint8_t,
std::uint16_t, std::uint32_t, std::uint64_t) {
using namespace stdx::literals;
Expand Down