Browse Source

feat: CommonCode unsafe create

master
Dnomd343 2 years ago
parent
commit
f3252455bb
  1. 14
      src/common_code/common_code.cc
  2. 3
      src/common_code/common_code.h
  3. 6
      src/main.cc
  4. 4
      src/short_code/convert.cc
  5. 4
      src/short_code/short_code.cc

14
src/common_code/common_code.cc

@ -8,14 +8,20 @@ inline uint8_t last_zero_num(uint32_t bin) { // get last zero number
return __builtin_popcount(bin >> 1); return __builtin_popcount(bin >> 1);
} }
uint64_t CommonCode::unwrap() const { uint64_t CommonCode::unwrap() const { // get raw uint64_t code
return code; // get raw uint64_t code return code;
} }
ShortCode CommonCode::to_short_code() const { // convert to short code ShortCode CommonCode::to_short_code() const { // convert to short code
return ShortCode(*this); return ShortCode(*this);
} }
CommonCode CommonCode::unsafe_create(uint64_t code) { // create CommonCode without check
auto common_code = CommonCode(); // init directly
common_code.code = code;
return common_code;
}
CommonCode::CommonCode(uint64_t common_code) { CommonCode::CommonCode(uint64_t common_code) {
if (!CommonCode::check(common_code)) { // check input common code if (!CommonCode::check(common_code)) { // check input common code
throw std::invalid_argument("invalid common code"); throw std::invalid_argument("invalid common code");
@ -23,6 +29,10 @@ CommonCode::CommonCode(uint64_t common_code) {
code = common_code; code = common_code;
} }
CommonCode::CommonCode(const ShortCode &short_code) {
code = short_code.to_common_code().unwrap(); // init from short code
}
CommonCode::CommonCode(const std::string &common_code_str) { CommonCode::CommonCode(const std::string &common_code_str) {
if (common_code_str.length() > 9 || common_code_str.length() == 0) { // check string length if (common_code_str.length() > 9 || common_code_str.length() == 0) { // check string length
throw std::invalid_argument("common code format error"); throw std::invalid_argument("common code format error");

3
src/common_code/common_code.h

@ -11,11 +11,14 @@ public:
uint64_t unwrap() const; uint64_t unwrap() const;
ShortCode to_short_code() const; ShortCode to_short_code() const;
static bool check(uint64_t common_code); static bool check(uint64_t common_code);
static CommonCode unsafe_create(uint64_t code);
std::string to_string(bool shorten = false) const; std::string to_string(bool shorten = false) const;
explicit CommonCode(uint64_t common_code); explicit CommonCode(uint64_t common_code);
explicit CommonCode(const ShortCode &short_code);
explicit CommonCode(const std::string &common_code_str); explicit CommonCode(const std::string &common_code_str);
private: private:
uint64_t code; uint64_t code;
CommonCode() = default;
}; };

6
src/main.cc

@ -117,6 +117,12 @@ int main() {
// std::cout << ShortCode(CommonCode(0x6EC0F8800), ShortCode::NORMAL).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; // std::cout << ShortCode(CommonCode(0x6EC0F8800), ShortCode::FAST).to_string() << std::endl;
// std::cout << CommonCode(0x6EC0F8800).to_short_code().to_string() << std::endl;
std::cout << CommonCode(ShortCode(14323231)).to_string() << std::endl;
std::cout << ShortCode(14323231).to_common_code().to_string() << std::endl;
std::cout << ShortCode(CommonCode(0x6EC0F8800)).to_string() << std::endl;
std::cout << CommonCode(0x6EC0F8800).to_short_code().to_string() << std::endl; std::cout << CommonCode(0x6EC0F8800).to_short_code().to_string() << std::endl;
return 0; return 0;

4
src/short_code/convert.cc

@ -5,9 +5,9 @@
CommonCode ShortCode::to_common_code() const { // convert to common code CommonCode ShortCode::to_common_code() const { // convert to common code
if (ShortCode::check_mode() == ShortCode::NORMAL) { if (ShortCode::check_mode() == ShortCode::NORMAL) {
return CommonCode(tiny_decode(code)); // using normal mode return CommonCode::unsafe_create(tiny_decode(code)); // using normal mode
} }
return CommonCode(all_cases_list[code]); // using fast mode return CommonCode::unsafe_create(all_cases_list[code]); // using fast mode
} }
ShortCode::ShortCode(const CommonCode &common_code) { // convert from common code ShortCode::ShortCode(const CommonCode &common_code) { // convert from common code

4
src/short_code/short_code.cc

@ -2,8 +2,8 @@
#include "short_code.h" #include "short_code.h"
#include "short_code_chars.h" #include "short_code_chars.h"
uint32_t ShortCode::unwrap() const { uint32_t ShortCode::unwrap() const { // get raw uint32_t code
return code; // get raw uint32_t code return code;
} }
bool ShortCode::check(uint32_t short_code) { bool ShortCode::check(uint32_t short_code) {

Loading…
Cancel
Save