doasku

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

cord.hpp (1194B)


      1 #ifndef DOASKO_CORD_HPP
      2 #define DOASKO_CORD_HPP
      3 
      4 #include <cassert>
      5 #include <cinttypes>
      6 #include <utility>
      7 
      8 struct cord_t
      9 {
     10   explicit cord_t(uint8_t value)
     11       : m_value(value)
     12   {
     13     assert(value < 9);
     14   }
     15 
     16   cord_t(uint8_t row, uint8_t col)
     17       : m_value(static_cast<uint8_t>(row * 3 + col))
     18   {
     19     assert(row < 3 && col < 3);
     20   }
     21 
     22   // NOLINTNEXTLINE
     23   operator uint8_t() const { return m_value; }
     24 
     25   uint8_t row() const { return m_value / 3; }
     26   uint8_t col() const { return m_value % 3; }
     27 
     28   uint8_t m_value;
     29 };
     30 
     31 class acord_t
     32 {
     33 public:
     34   acord_t(cord_t subgrid, cord_t field)
     35       : m_subgrid(subgrid)
     36       , m_field(field)
     37   {
     38   }
     39 
     40   acord_t(uint8_t row, uint8_t col)
     41       : m_subgrid(row / 3, col / 3)
     42       , m_field(row % 3, col % 3)
     43   {
     44   }
     45 
     46   cord_t subgrid() const { return m_subgrid; }
     47   cord_t field() const { return m_field; }
     48 
     49   uint8_t row() const
     50   {
     51     return static_cast<uint8_t>(m_subgrid.row() * 3 + m_field.row());
     52   }
     53   uint8_t col() const
     54   {
     55     return static_cast<uint8_t>(m_subgrid.col() * 3 + m_field.col());
     56   }
     57 
     58   std::tuple<cord_t, cord_t> relative() const { return {m_subgrid, m_field}; }
     59 
     60 private:
     61   cord_t m_subgrid;
     62   cord_t m_field;
     63 };
     64 
     65 #endif