From f7055d26925e515c8a3ffd139e9791f6b763b546 Mon Sep 17 00:00:00 2001 From: alwaysprince05 Date: Sat, 5 Sep 2026 00:46:51 +0530 Subject: [PATCH 1/2] fix(examples): replace switch statement with std::popcount in sudoku example MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The examine_potentials function in sudoku.cpp used a switch statement that only handled powers of 2 (1, 2, 4, 8, 16, 32, 64, 128, 256). If the potential set was not a power of 2, it hit the default case which asserted. This caused the sudoku example to crash in Debug mode with: Assertion failed: (!"potential set is not a power of 2") The fix uses std::popcount and std::countr_zero to handle non-power-of-2 values gracefully. When popcount == 1, it's a singleton and can be solved. When popcount > 1, it's normal during solving and continues. Fixes #2105 🤖 Generated with Codebuff Co-Authored-By: Codebuff --- examples/sudoku.cpp | 65 +++++---------------------------------------- 1 file changed, 6 insertions(+), 59 deletions(-) diff --git a/examples/sudoku.cpp b/examples/sudoku.cpp index 36f028f54..4bddd7c8c 100644 --- a/examples/sudoku.cpp +++ b/examples/sudoku.cpp @@ -24,6 +24,7 @@ #include #include +#include #include #include #include @@ -221,67 +222,13 @@ bool examine_potentials(board_element *b, bool *progress) { if (b[i].solved_element == 0 && b[i].potential_set == 0) // empty set return false; - switch (b[i].potential_set) + // Check if potential_set is a power of 2 (only one bit set) + if (b[i].potential_set != 0 && std::popcount(b[i].potential_set) == 1) { - case 1: - { - b[i].solved_element = 1; - singletons = true; - break; - } - case 2: - { - b[i].solved_element = 2; - singletons = true; - break; - } - case 4: - { - b[i].solved_element = 3; - singletons = true; - break; - } - case 8: - { - b[i].solved_element = 4; - singletons = true; - break; - } - case 16: - { - b[i].solved_element = 5; - singletons = true; - break; - } - case 32: - { - b[i].solved_element = 6; - singletons = true; - break; - } - case 64: - { - b[i].solved_element = 7; - singletons = true; - break; - } - case 128: - { - b[i].solved_element = 8; - singletons = true; - break; - } - case 256: - { - b[i].solved_element = 9; + // It's a singleton - solve it + // countr_zero gives the bit position (0-indexed), add 1 to get the element value + b[i].solved_element = std::countr_zero(b[i].potential_set) + 1; singletons = true; - break; - } - default: - { - assert(!"potential set is not a power of 2"); - break; - } } } *progress = singletons; From 9fdf92ec13bd01ae4d9a67b8459447d6ded1cde8 Mon Sep 17 00:00:00 2001 From: alwaysprince05 Date: Sat, 5 Sep 2026 04:59:50 +0530 Subject: [PATCH 2/2] fix(examples): simplify popcount check in sudoku examine_potentials MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Remove redundant `potential_set != 0` guard — std::popcount(0) == 0 which is never == 1, so the check was always false when potential_set was zero. Addresses review feedback from @ericniebler. 🤖 Generated with Codebuff Co-Authored-By: Codebuff --- examples/sudoku.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/sudoku.cpp b/examples/sudoku.cpp index 4bddd7c8c..a6a295198 100644 --- a/examples/sudoku.cpp +++ b/examples/sudoku.cpp @@ -223,7 +223,7 @@ bool examine_potentials(board_element *b, bool *progress) if (b[i].solved_element == 0 && b[i].potential_set == 0) // empty set return false; // Check if potential_set is a power of 2 (only one bit set) - if (b[i].potential_set != 0 && std::popcount(b[i].potential_set) == 1) + if (std::popcount(b[i].potential_set) == 1) { // It's a singleton - solve it // countr_zero gives the bit position (0-indexed), add 1 to get the element value