Nonogram solver
リビジョン | 37b201138608993cb5401cae307e05a531a0a8d1 (tree) |
---|---|
日時 | 2021-02-12 20:58:46 |
作者 | Alexander Larin <scalar438@gmai...> |
コミッター | Alexander Larin |
Cell implemented
@@ -0,0 +1,61 @@ | ||
1 | +#include "cell.hpp" | |
2 | + | |
3 | +namespace | |
4 | +{ | |
5 | + | |
6 | +// Calculate number of enabled bit. | |
7 | +constexpr int get_bit_number(uint32_t val) | |
8 | +{ | |
9 | + static const int MultiplyDeBruijnBitPosition[32] = {0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, | |
10 | + 15, 25, 17, 4, 8, 31, 27, 13, 23, 21, 19, | |
11 | + 16, 7, 26, 12, 18, 6, 11, 5, 10, 9}; | |
12 | + return MultiplyDeBruijnBitPosition[(val * 0x077CB531U) >> 27u]; | |
13 | +} | |
14 | + | |
15 | +constexpr bool test_get_bit_number() | |
16 | +{ | |
17 | + for (uint32_t i = 0; i < 32; ++i) | |
18 | + { | |
19 | + if (get_bit_number(1u << i) != i) return false; | |
20 | + } | |
21 | + return true; | |
22 | +} | |
23 | + | |
24 | +} // namespace | |
25 | + | |
26 | +Cell::Cell(int max_colors) : m_max_colors(max_colors) | |
27 | +{ | |
28 | + // TODO: replace it to actual exception | |
29 | + if (max_colors > MAX_COLORS) throw 42; | |
30 | + m_data = 1u >> (max_colors + 1) - 1u; | |
31 | +} | |
32 | + | |
33 | +std::optional<int> Cell::get_color() const | |
34 | +{ | |
35 | + if (m_data == 0 || (m_data & (m_data - 1)) != 0) return std::nullopt; | |
36 | + static_assert(test_get_bit_number()); | |
37 | + return get_bit_number(m_data); | |
38 | +} | |
39 | + | |
40 | +bool Cell::is_color_possible(int color_number) const | |
41 | +{ | |
42 | + return m_data & (1u << color_number) != 0; | |
43 | +} | |
44 | + | |
45 | +bool Cell::is_impossible() const | |
46 | +{ | |
47 | + return m_data == 0; | |
48 | +} | |
49 | + | |
50 | +void Cell::set_color_possible(int color_number, bool possible) | |
51 | +{ | |
52 | + if (possible) | |
53 | + m_data |= (1u << color_number); | |
54 | + else | |
55 | + m_data &= ~(1u << color_number); | |
56 | +} | |
57 | + | |
58 | +int Cell::max_colors() const | |
59 | +{ | |
60 | + return m_max_colors; | |
61 | +} |
@@ -17,9 +17,13 @@ | ||
17 | 17 | |
18 | 18 | void set_color_possible(int color_number, bool possible); |
19 | 19 | |
20 | + int max_colors() const; | |
21 | + | |
20 | 22 | private: |
21 | 23 | // Bitmask with possible colors. 0 - "impossible" cell, it mustn't appear in normal puzzless |
22 | 24 | // Least bit is background (i.e. if m_data == 0 then cell is background) all other bits are |
23 | 25 | // colors |
24 | 26 | uint32_t m_data; |
27 | + | |
28 | + int m_max_colors; | |
25 | 29 | }; |