diff options
| author | Sebastiano Tronto <sebastiano@tronto.net> | 2025-04-14 14:00:44 +0200 |
|---|---|---|
| committer | Sebastiano Tronto <sebastiano@tronto.net> | 2025-04-14 14:00:44 +0200 |
| commit | 3ab1012ea8c55f82812b5998fd6b4fdf53dea70e (patch) | |
| tree | d18552619a364fcb816d5e2d2331822edd7e2045 /cpp/nissy.h | |
| parent | 7b8c1e4634784b57bb6223cd780d5c67f5381d43 (diff) | |
| download | nissy-core-3ab1012ea8c55f82812b5998fd6b4fdf53dea70e.tar.gz nissy-core-3ab1012ea8c55f82812b5998fd6b4fdf53dea70e.zip | |
Improved C++ API and added info to README
Diffstat (limited to 'cpp/nissy.h')
| -rw-r--r-- | cpp/nissy.h | 174 |
1 files changed, 73 insertions, 101 deletions
diff --git a/cpp/nissy.h b/cpp/nissy.h index e336c7a..7ab466d 100644 --- a/cpp/nissy.h +++ b/cpp/nissy.h | |||
| @@ -15,119 +15,91 @@ C++20 header file for nissy. | |||
| 15 | #include <vector> | 15 | #include <vector> |
| 16 | 16 | ||
| 17 | namespace nissy { | 17 | namespace nissy { |
| 18 | /* Some constants for size for I/O buffers */ | ||
| 19 | namespace size { | ||
| 20 | constexpr size_t B32 = 22; | ||
| 21 | constexpr size_t H48 = 88; | ||
| 22 | constexpr size_t CUBE_MAX = H48; | ||
| 23 | constexpr size_t TRANSFORMATION = 12; | ||
| 24 | constexpr size_t SOLVE_STATS = 10; | ||
| 25 | constexpr size_t DATAID = 255; | ||
| 26 | } | ||
| 27 | 18 | ||
| 28 | /* Some structs definitions for better type safety */ | 19 | class nissflag { |
| 29 | struct nissflag_t { unsigned value; }; | 20 | public: |
| 30 | struct error_t { long long value; }; | 21 | unsigned value; |
| 31 | struct cube_t { std::string value; }; | ||
| 32 | struct moves_t { std::string value; }; | ||
| 33 | struct trans_t { std::string value; }; | ||
| 34 | struct cube_format_t { std::string value; }; | ||
| 35 | struct solver_t { std::string value; }; | ||
| 36 | struct solver_data_t { std::vector<std::byte> value; }; | ||
| 37 | struct solve_result_t { | ||
| 38 | std::vector<std::string> solutions; | ||
| 39 | std::array<long long, size::SOLVE_STATS> stats; | ||
| 40 | }; | ||
| 41 | |||
| 42 | /* Flags for NISS options */ | ||
| 43 | namespace nissflag { | ||
| 44 | constexpr nissflag_t NORMAL{1}; | ||
| 45 | constexpr nissflag_t INVERSE{2}; | ||
| 46 | constexpr nissflag_t MIXED{4}; | ||
| 47 | constexpr nissflag_t LINEAR{NORMAL.value | INVERSE.value}; | ||
| 48 | constexpr nissflag_t ALL{LINEAR.value | MIXED.value}; | ||
| 49 | } | ||
| 50 | |||
| 51 | /* Error codes */ | ||
| 52 | namespace error { | ||
| 53 | constexpr error_t UNSOLVABLE{-1}; | ||
| 54 | constexpr error_t INVALID_CUBE{-10}; | ||
| 55 | constexpr error_t UNSOLVABLE_CUBE{-11}; | ||
| 56 | constexpr error_t INVALID_MOVES{-20}; | ||
| 57 | constexpr error_t INVALID_TRANS{-30}; | ||
| 58 | constexpr error_t INVALID_FORMAT{-40}; | ||
| 59 | constexpr error_t INVALID_SOLVER{-50}; | ||
| 60 | constexpr error_t NULL_POINTER{-60}; | ||
| 61 | constexpr error_t BUFFER_SIZE{-61}; | ||
| 62 | constexpr error_t DATA{-70}; | ||
| 63 | constexpr error_t OPTIONS{-80}; | ||
| 64 | constexpr error_t UNKNOWN{-999}; | ||
| 65 | } | ||
| 66 | |||
| 67 | /* Cube constants */ | ||
| 68 | namespace cube { | ||
| 69 | const cube_t SOLVED{"ABCDEFGH=ABCDEFGHIJKL"}; | ||
| 70 | } | ||
| 71 | 22 | ||
| 72 | std::variant<cube_t, error_t> inverse( | 23 | static const nissflag NORMAL; |
| 73 | const cube_t& cube | 24 | static const nissflag INVERSE; |
| 74 | ); | 25 | static const nissflag MIXED; |
| 26 | static const nissflag LINEAR; | ||
| 27 | static const nissflag ALL; | ||
| 28 | }; | ||
| 75 | 29 | ||
| 76 | std::variant<cube_t, error_t> applymoves( | 30 | class error { |
| 77 | const cube_t& cube, | 31 | public: |
| 78 | const moves_t& moves | 32 | long long value; |
| 79 | ); | 33 | bool ok() const; |
| 80 | 34 | ||
| 81 | std::variant<cube_t, error_t> applytrans( | 35 | static const error OK; |
| 82 | const cube_t& cube, | 36 | static const error UNSOLVABLE; |
| 83 | const trans_t& trans | 37 | static const error INVALID_CUBE; |
| 84 | ); | 38 | static const error UNSOLVABLE_CUBE; |
| 39 | static const error INVALID_MOVES; | ||
| 40 | static const error INVALID_TRANS; | ||
| 41 | static const error INVALID_FORMAT; | ||
| 42 | static const error INVALID_SOLVER; | ||
| 43 | static const error NULL_POINTER; | ||
| 44 | static const error BUFFER_SIZE; | ||
| 45 | static const error DATA; | ||
| 46 | static const error OPTIONS; | ||
| 47 | static const error UNKNOWN; | ||
| 48 | }; | ||
| 85 | 49 | ||
| 86 | std::variant<std::string, error_t> convert( | 50 | class cube { |
| 87 | const cube_format_t& format_in, | 51 | public: |
| 88 | const cube_format_t& format_out, | 52 | cube(); |
| 89 | const std::string& cube_string | 53 | error move(const std::string&); |
| 90 | ); | 54 | error transform(const std::string&); |
| 55 | void invert(); | ||
| 56 | void compose(const cube&); | ||
| 57 | std::string to_string() const; | ||
| 58 | std::variant<std::string, error> to_string( | ||
| 59 | const std::string& format) const; | ||
| 91 | 60 | ||
| 92 | std::variant<cube_t, error_t> getcube( | 61 | static std::variant<cube, error> from_string( |
| 93 | long long ep, | 62 | const std::string&); |
| 94 | long long eo, | 63 | static std::variant<cube, error> from_string( |
| 95 | long long cp, | 64 | const std::string& str, const std::string& format); |
| 96 | long long co, | 65 | static std::variant<cube, error> get( |
| 97 | const std::string& options | 66 | long long ep, long long eo, long long cp, long long co); |
| 98 | ); | 67 | static std::variant<cube, error> get( |
| 68 | long long ep, long long eo, long long cp, long long co, | ||
| 69 | const std::string& options); | ||
| 99 | 70 | ||
| 100 | std::variant<std::pair<size_t, std::string>, error_t> solverinfo( | 71 | private: |
| 101 | const solver_t& solver | 72 | std::string m_b32{"ABCDEFGH=ABCDEFGHIJKL"}; |
| 102 | ); | 73 | }; |
| 103 | 74 | ||
| 104 | std::variant<solver_data_t, error_t> gendata( | 75 | class solver { |
| 105 | const solver_t& solver | 76 | public: |
| 106 | ); | 77 | struct solve_result { |
| 78 | error err; | ||
| 79 | std::vector<std::string> solutions; | ||
| 80 | std::array<long long, 10> stats; | ||
| 81 | }; | ||
| 107 | 82 | ||
| 108 | std::optional<error_t> checkdata( | 83 | const std::string name; |
| 109 | const solver_data_t& data | 84 | size_t size; |
| 110 | ); | 85 | std::string id; |
| 86 | std::vector<std::byte> data; | ||
| 111 | 87 | ||
| 112 | std::variant<solve_result_t, error_t> solve( | 88 | error generate_data(); |
| 113 | const cube_t& cube, | 89 | void read_data(std::ifstream&); |
| 114 | const solver_t& solver, | 90 | error check_data() const; |
| 115 | nissflag_t niss, | 91 | void unload_data(); |
| 116 | unsigned minmoves, | 92 | solve_result solve(const cube&, nissflag, unsigned minmoves, |
| 117 | unsigned maxmoves, | 93 | unsigned maxmoves, unsigned maxsols, int optimal, |
| 118 | unsigned maxsolutions, | 94 | int threads); |
| 119 | int optimal, | ||
| 120 | int threads, | ||
| 121 | const solver_data_t& data | ||
| 122 | ); | ||
| 123 | 95 | ||
| 124 | std::variant<unsigned, error_t> countmoves( | 96 | static std::variant<solver, error> get(const std::string&); |
| 125 | const moves_t& moves | 97 | private: |
| 126 | ); | 98 | solver(const std::string& name); |
| 99 | }; | ||
| 127 | 100 | ||
| 128 | void setlogger( | 101 | std::variant<unsigned, error> count_moves(const std::string&); |
| 129 | std::function<void(std::string&)> log | 102 | void set_logger(const std::function<void(const char*)>&); |
| 130 | ); | ||
| 131 | } | 103 | } |
| 132 | 104 | ||
| 133 | #endif | 105 | #endif |
