Browse Source

feat: `to_common_code` interface

legacy
Dnomd343 2 years ago
parent
commit
e5b1e24839
  1. 2
      klotski/short_code.h
  2. 6
      src/main.cc
  3. 9
      src/short_code/convert.cc
  4. 9
      src/short_code/short_code.h

2
klotski/short_code.h

@ -40,6 +40,7 @@ public:
static bool check(uint32_t short_code); static bool check(uint32_t short_code);
uint32_t zip_short_code(uint64_t common_code); uint32_t zip_short_code(uint64_t common_code);
/// ok
uint64_t unzip_short_code(uint32_t short_code); uint64_t unzip_short_code(uint32_t short_code);
/// ok /// ok
@ -60,6 +61,7 @@ private:
enum Mode check_mode(); enum Mode check_mode();
void build_base_ranges(); void build_base_ranges();
/// ok
uint64_t tiny_decode(uint32_t short_code); uint64_t tiny_decode(uint32_t short_code);
uint32_t tiny_encode(uint64_t common_code); uint32_t tiny_encode(uint64_t common_code);
}; };

6
src/main.cc

@ -105,8 +105,10 @@ int main() {
// std::cout << ShortCode("EP4HZ", ShortCode::NORMAL).unwrap() << std::endl; // std::cout << ShortCode("EP4HZ", ShortCode::NORMAL).unwrap() << std::endl;
// std::cout << ShortCode("eP4hZ", ShortCode::FAST).to_string() << std::endl; // std::cout << ShortCode("eP4hZ", ShortCode::FAST).to_string() << std::endl;
std::cout << ShortCode::tiny_encode(0x6EC0F8800) << std::endl; // std::cout << ShortCode::tiny_encode(0x6EC0F8800) << std::endl;
printf("%09lX\n", ShortCode::tiny_decode(14323231)); // printf("%09lX\n", ShortCode::tiny_decode(14323231));
std::cout << ShortCode(14323231).to_common_code().to_string() << std::endl;
return 0; return 0;
} }

9
src/short_code/convert.cc

@ -3,6 +3,14 @@
#include "basic_ranges.h" #include "basic_ranges.h"
#include "short_code_mark.h" #include "short_code_mark.h"
CommonCode ShortCode::to_common_code() const { // convert to common code
if (ShortCode::check_mode() == ShortCode::NORMAL) {
return CommonCode(tiny_decode(code)); // using normal mode
}
return CommonCode(all_cases_list[code]); // using fast mode
}
/// ensure that input common code is valid
uint32_t ShortCode::tiny_encode(uint64_t common_code) { // common_code --low-memory--> short_code uint32_t ShortCode::tiny_encode(uint64_t common_code) { // common_code --low-memory--> short_code
uint32_t offset = 0; uint32_t offset = 0;
uint32_t head = common_code >> 32; // common code head uint32_t head = common_code >> 32; // common code head
@ -21,6 +29,7 @@ uint32_t ShortCode::tiny_encode(uint64_t common_code) { // common_code --low-mem
return ALL_CASES_OFFSET[head] + RANGE_PREFIX_OFFSET[head][prefix] + offset; return ALL_CASES_OFFSET[head] + RANGE_PREFIX_OFFSET[head][prefix] + offset;
} }
/// ensure that input short code is valid
uint64_t ShortCode::tiny_decode(uint32_t short_code) { // short_code --low-memory--> common_code uint64_t ShortCode::tiny_decode(uint32_t short_code) { // short_code --low-memory--> common_code
uint32_t head = 0, prefix = 0; uint32_t head = 0, prefix = 0;
for (; head < 16; ++head) { for (; head < 16; ++head) {

9
src/short_code/short_code.h

@ -4,6 +4,7 @@
#include <vector> #include <vector>
#include <cstdint> #include <cstdint>
#include <unordered_map> #include <unordered_map>
#include "common_code.h"
class ShortCode { class ShortCode {
public: public:
@ -12,6 +13,7 @@ public:
}; };
uint32_t unwrap() const; uint32_t unwrap() const;
std::string to_string() const; std::string to_string() const;
CommonCode to_common_code() const;
static bool check(uint32_t short_code); static bool check(uint32_t short_code);
static enum Mode check_mode(); static enum Mode check_mode();
@ -26,8 +28,6 @@ public:
speed_up(mode); speed_up(mode);
} }
static uint64_t tiny_decode(uint32_t short_code);
static uint32_t tiny_encode(uint64_t common_code);
private: private:
uint32_t code; uint32_t code;
@ -39,7 +39,6 @@ private:
static std::unordered_map<uint64_t, uint32_t> all_cases_dict; // common_code -> short_code static std::unordered_map<uint64_t, uint32_t> all_cases_dict; // common_code -> short_code
static void build_mappings(); static void build_mappings();
static uint64_t tiny_decode(uint32_t short_code);
// static uint64_t tiny_decode(uint32_t short_code); static uint32_t tiny_encode(uint64_t common_code);
// static uint32_t tiny_encode(uint64_t common_code);
}; };

Loading…
Cancel
Save