From 8f31c01808aac42ef0aa9970670a1edea8d1ec32 Mon Sep 17 00:00:00 2001 From: Ben Deane Date: Mon, 17 Feb 2025 13:38:54 -0700 Subject: [PATCH] :art: Make `bitset::lowest_unset` work correctly with enumerations --- include/stdx/bitset.hpp | 6 +++--- test/bitset.cpp | 5 +++++ 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/include/stdx/bitset.hpp b/include/stdx/bitset.hpp index 50ab7b2..c583858 100644 --- a/include/stdx/bitset.hpp +++ b/include/stdx/bitset.hpp @@ -341,16 +341,16 @@ class bitset { return n + static_cast(popcount(highbits())); } - [[nodiscard]] constexpr auto lowest_unset() const -> std::size_t { + [[nodiscard]] constexpr auto lowest_unset() const { std::size_t i = 0; for (auto e : storage) { if (auto offset = static_cast(countr_one(e)); offset != std::numeric_limits::digits) { - return i + offset; + return static_cast(i + offset); } i += std::numeric_limits::digits; } - return i; + return static_cast(i); } [[nodiscard]] constexpr auto operator~() const -> bitset { diff --git a/test/bitset.cpp b/test/bitset.cpp index e383ee0..e573cb1 100644 --- a/test/bitset.cpp +++ b/test/bitset.cpp @@ -485,6 +485,11 @@ TEST_CASE("use bitset with enum struct (transform_reduce)", "[bitset]") { bs)); } +TEST_CASE("use bitset with enum struct (lowest_unset)", "[bitset]") { + constexpr auto bs = stdx::bitset{stdx::place_bits, Bits::ZERO}; + CHECK(bs.lowest_unset() == Bits::ONE); +} + #if __cplusplus >= 202002L TEST_CASE("construct with a ct_string", "[bitset]") { using namespace stdx::literals;