Browse Source

feat: `to_common_code` interface

master
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);
uint32_t zip_short_code(uint64_t common_code);
/// ok
uint64_t unzip_short_code(uint32_t short_code);
/// ok
@ -60,6 +61,7 @@ private:
enum Mode check_mode();
void build_base_ranges();
/// ok
uint64_t tiny_decode(uint32_t short_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::FAST).to_string() << std::endl;
std::cout << ShortCode::tiny_encode(0x6EC0F8800) << std::endl;
printf("%09lX\n", ShortCode::tiny_decode(14323231));
// std::cout << ShortCode::tiny_encode(0x6EC0F8800) << std::endl;
// printf("%09lX\n", ShortCode::tiny_decode(14323231));
std::cout << ShortCode(14323231).to_common_code().to_string() << std::endl;
return 0;
}

9
src/short_code/convert.cc

@ -3,6 +3,14 @@
#include "basic_ranges.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 offset = 0;
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;
}
/// ensure that input short code is valid
uint64_t ShortCode::tiny_decode(uint32_t short_code) { // short_code --low-memory--> common_code
uint32_t head = 0, prefix = 0;
for (; head < 16; ++head) {

9
src/short_code/short_code.h

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

Loading…
Cancel
Save