Browse Source

feat: ShortCode init with CommonCode

legacy
Dnomd343 2 years ago
parent
commit
e586239c37
  1. 13
      src/main.cc
  2. 8
      src/short_code/convert.cc
  3. 11
      src/short_code/short_code.h

13
src/main.cc

@ -105,10 +105,17 @@ 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;
// printf("%09lX\n", ShortCode::tiny_decode(14323231));
std::cout << ShortCode(14323231).to_common_code().to_string() << std::endl; // ShortCode::speed_up(ShortCode::FAST);
// std::cout << ShortCode(14323231).to_common_code().to_string() << std::endl;
// std::cout << ShortCode(CommonCode(0x6EC0F8800)).to_string() << std::endl;
//
//// ShortCode::speed_up(ShortCode::FAST);
// std::cout << ShortCode(14323231).to_common_code().to_string() << std::endl;
// std::cout << ShortCode(CommonCode(0x6EC0F8800)).to_string() << std::endl;
std::cout << ShortCode(CommonCode(0x6EC0F8800), ShortCode::NORMAL).to_string() << std::endl;
std::cout << ShortCode(CommonCode(0x6EC0F8800), ShortCode::FAST).to_string() << std::endl;
return 0; return 0;
} }

8
src/short_code/convert.cc

@ -10,6 +10,14 @@ CommonCode ShortCode::to_common_code() const { // convert to common code
return CommonCode(all_cases_list[code]); // using fast mode return CommonCode(all_cases_list[code]); // using fast mode
} }
ShortCode::ShortCode(const CommonCode &common_code) { // convert from common code
if (ShortCode::check_mode() == ShortCode::NORMAL) {
code = tiny_encode(common_code.unwrap()); // using normal mode
} else {
code = all_cases_dict[common_code.unwrap()]; // using fast mode
}
}
/// ensure that input common code is valid /// 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;

11
src/short_code/short_code.h

@ -11,24 +11,27 @@ public:
enum Mode { enum Mode {
NORMAL, FAST NORMAL, FAST
}; };
static enum Mode check_mode();
static void speed_up(enum Mode mode);
uint32_t unwrap() const; uint32_t unwrap() const;
std::string to_string() const; std::string to_string() const;
CommonCode to_common_code() 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 void speed_up(enum Mode mode);
explicit ShortCode(uint32_t short_code); explicit ShortCode(uint32_t short_code);
explicit ShortCode(const CommonCode &common_code);
explicit ShortCode(const std::string &short_code_str); explicit ShortCode(const std::string &short_code_str);
ShortCode(uint32_t short_code, enum Mode mode) : ShortCode(short_code) { ShortCode(uint32_t short_code, enum Mode mode) : ShortCode(short_code) {
speed_up(mode); speed_up(mode);
} }
ShortCode(const CommonCode &common_code, enum Mode mode) : ShortCode(common_code) {
speed_up(mode);
}
ShortCode(const std::string &short_code_str, enum Mode mode) : ShortCode(short_code_str) { ShortCode(const std::string &short_code_str, enum Mode mode) : ShortCode(short_code_str) {
speed_up(mode); speed_up(mode);
} }
private: private:
uint32_t code; uint32_t code;
static std::mutex map_building; static std::mutex map_building;

Loading…
Cancel
Save