Browse Source

feat: short code check function

master
Dnomd343 2 years ago
parent
commit
4cf9b88c49
  1. 17
      klotski/main.cc
  2. 29
      klotski/short_code.cc
  3. 3
      klotski/short_code.h

17
klotski/main.cc

@ -38,13 +38,20 @@ int main() {
// printf("%d\n", s.tiny_encode(0x6EC0F8800));
// printf("%09lX\n", s.tiny_decode(14323231));
// auto s = ShortCode();
auto s = ShortCode();
// auto s = ShortCode(ShortCode::Mode::NORMAL);
auto s = ShortCode(ShortCode::Mode::FAST);
// auto s = ShortCode(ShortCode::Mode::FAST);
// std::cout << "start" << std::endl;
// std::cout << s.zip_short_code(0x6EC0F8800) << std::endl;
// std::cout << "complete" << std::endl;
std::cout << "start" << std::endl;
std::cout << s.check_mode() << std::endl;
std::cout << "complete" << std::endl;
// if (ShortCode::check(14323231)) {
if (ShortCode::check(87654321)) {
std::cout << "true" << std::endl;
} else {
std::cout << "false" << std::endl;
}
return 0;
}

29
klotski/short_code.cc

@ -1,3 +1,4 @@
#include <stdexcept>
#include "common.h"
#include "all_cases.h"
#include "short_code.h"
@ -7,6 +8,10 @@ ShortCode::ShortCode(ShortCode::Mode mode) { // class initialize
speed_up(mode);
}
bool ShortCode::check(uint32_t short_code) {
return short_code < ShortCode::SHORT_CODE_LIMIT; // 0 ~ (SHORT_CODE_LIMIT - 1)
}
void ShortCode::speed_up(enum Mode mode) { // speed up handle short code
switch (mode) {
case Mode::NORMAL: // speed up into normal mode
@ -104,22 +109,30 @@ uint32_t ShortCode::zip_short_code(uint64_t common_code) { // common_code --zip-
// TODO: confirm common_code valid
// TODO: check Mode => NORMAL / FAST
// TODO: usage fast_encode or tiny_encode
return 0;
switch (check_mode()) {
case ShortCode::Mode::NORMAL:
return tiny_encode(common_code);
case ShortCode::Mode::FAST:
return fast_encode(common_code);
default:
throw std::runtime_error("unknown error");
}
}
uint64_t ShortCode::unzip_short_code(uint32_t short_code) { // short_code --unzip--> common_code
// TODO: confirm short_code valid
// TODO: check Mode => NORMAL / FAST
// TODO: usage fast_decode / tiny_decode
return 0;
switch (check_mode()) {
case ShortCode::Mode::NORMAL:
return tiny_decode(short_code);
case ShortCode::Mode::FAST:
return fast_decode(short_code);
default:
throw std::runtime_error("unknown error");
}
}
enum ShortCode::Mode ShortCode::check_mode() { // ensure speed up enabled and return current mode

3
klotski/short_code.h

@ -23,12 +23,13 @@ public:
enum Mode check_mode();
static bool check(uint32_t short_code);
private:
// std::vector<uint64_t> all_cases_list; // short_code -> common_code
// std::unordered_map<uint64_t, uint32_t> all_cases_dict; // common_code -> short_code
const uint32_t SHORT_CODE_LIMIT = 29334498;
static const uint32_t SHORT_CODE_LIMIT = 29334498;
uint64_t fast_decode(uint32_t short_code);
uint32_t fast_encode(uint64_t common_code);

Loading…
Cancel
Save