• R/O
  • SSH

コミット

タグ
未設定

よく使われているワード(クリックで追加)

javac++androidlinuxc#windowsobjective-ccocoa誰得qtpythonphprubygameguibathyscaphec計画中(planning stage)翻訳omegatframeworktwitterdomtestvb.netdirectxゲームエンジンbtronarduinopreviewer

Nonogram solver


コミットメタ情報

リビジョン37b201138608993cb5401cae307e05a531a0a8d1 (tree)
日時2021-02-12 20:58:46
作者Alexander Larin <scalar438@gmai...>
コミッターAlexander Larin

ログメッセージ

Cell implemented

変更サマリ

差分

diff -r 00e6fc6e0c18 -r 37b201138608 cell.cpp
--- a/cell.cpp Thu Feb 11 14:25:03 2021 +0300
+++ b/cell.cpp Fri Feb 12 14:58:46 2021 +0300
@@ -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+}
diff -r 00e6fc6e0c18 -r 37b201138608 cell.hpp
--- a/cell.hpp Thu Feb 11 14:25:03 2021 +0300
+++ b/cell.hpp Fri Feb 12 14:58:46 2021 +0300
@@ -17,9 +17,13 @@
1717
1818 void set_color_possible(int color_number, bool possible);
1919
20+ int max_colors() const;
21+
2022 private:
2123 // Bitmask with possible colors. 0 - "impossible" cell, it mustn't appear in normal puzzless
2224 // Least bit is background (i.e. if m_data == 0 then cell is background) all other bits are
2325 // colors
2426 uint32_t m_data;
27+
28+ int m_max_colors;
2529 };