doasku

Sudoku solver
git clone git://git.dimitrijedobrota.com/doasku.git
Log | Files | Refs | README | LICENSE

ref.hpp (2841B)


      1 #ifndef DOASKU_REF_HPP
      2 #define DOASKU_REF_HPP
      3 
      4 #include <array>
      5 #include <cstdint>
      6 #include <utility>
      7 #include <vector>
      8 
      9 class ref
     10 {
     11 public:
     12   using change_t = std::tuple<uint8_t, uint16_t>;
     13   using changes_t = std::vector<change_t>;
     14 
     15   uint16_t get(uint8_t field) const { return m_value.at(field); }
     16   uint16_t get_ref(uint8_t value) const { return m_ref.at(value); }
     17   uint16_t get_value(uint8_t field) const { return m_res.at(field); }
     18 
     19   void clear(uint8_t field, uint8_t value);
     20   void set(uint8_t field, uint8_t value);
     21 
     22   changes_t get_hidden_singles() const;
     23   changes_t get_naked_singles() const;
     24 
     25   auto get_hidden_pairs() const { return get_hidden(2); }
     26   auto get_hidden_triplets() const { return get_hidden(3); }
     27   auto get_hidden_quads() const { return get_hidden(4); }
     28 
     29   auto get_naked_pairs() const { return get_naked(2); }
     30   auto get_naked_triplets() const { return get_naked(3); }
     31   auto get_naked_quads() const { return get_naked(4); }
     32 
     33   auto get_pointing_row() const { return get_pointing(0x7, 0); }
     34   auto get_pointing_col() const { return get_pointing(0x49, 1); }
     35 
     36 private:
     37   changes_t get_naked(uint8_t number) const;
     38   changes_t get_hidden(uint8_t number) const;
     39   changes_t get_pointing(uint16_t mask, size_t seen) const;
     40 
     41   bool get_hidden(changes_t& res,
     42                   uint8_t orig,
     43                   uint8_t number,
     44                   uint8_t first,
     45                   uint16_t val,
     46                   uint16_t mask) const;
     47 
     48   bool get_naked(changes_t& res,
     49                  uint8_t orig,
     50                  uint8_t number,
     51                  uint8_t first,
     52                  uint16_t val) const;
     53 
     54   static constexpr const std::int64_t mask_field = (1U << 9U) - 1;
     55   static constexpr const std::int64_t mask_value = 0x201008040201;
     56 
     57   std::array<uint16_t, 9> m_value = {mask_field,
     58                                      mask_field,
     59                                      mask_field,
     60                                      mask_field,
     61                                      mask_field,
     62                                      mask_field,
     63                                      mask_field,
     64                                      mask_field,
     65                                      mask_field};
     66 
     67   std::array<uint16_t, 9> m_ref = {mask_field,
     68                                    mask_field,
     69                                    mask_field,
     70                                    mask_field,
     71                                    mask_field,
     72                                    mask_field,
     73                                    mask_field,
     74                                    mask_field,
     75                                    mask_field};
     76 
     77   std::array<uint16_t, 9> m_res = {0};
     78 
     79   mutable std::array<uint16_t, 4> m_seen_hidden = {0};
     80   mutable std::array<uint16_t, 4> m_seen_naked = {0};
     81   mutable std::array<uint16_t, 2> m_seen_point = {0};
     82 };
     83 
     84 #endif