From c669c368faf0e0527bf30a2302ac424600fde0bb Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Wed, 18 Jan 2023 13:28:40 +0800 Subject: [PATCH] update: more convert interface for RawCode --- src/all_cases/basic_ranges.cc | 1 + src/main.cc | 4 +++- src/raw_code/convert.cc | 16 ++++++++++++---- src/raw_code/raw_code.cc | 25 +++++++------------------ src/raw_code/raw_code.h | 3 ++- 5 files changed, 25 insertions(+), 24 deletions(-) diff --git a/src/all_cases/basic_ranges.cc b/src/all_cases/basic_ranges.cc index 064cf34..9c2a0c5 100644 --- a/src/all_cases/basic_ranges.cc +++ b/src/all_cases/basic_ranges.cc @@ -51,6 +51,7 @@ void BasicRanges::build_data() { // build basic ranges } } } + /// NOTE: multiple sets of ordered data -> use merge sort instead of quick sort std::stable_sort(BasicRanges::data.begin(), BasicRanges::data.end()); // sort basic ranges for (auto &range : BasicRanges::data) { range = Common::range_reverse(range); // basic ranges reverse diff --git a/src/main.cc b/src/main.cc index 5bf2e3c..3d83783 100644 --- a/src/main.cc +++ b/src/main.cc @@ -20,7 +20,7 @@ int main() { // std::cout << "wait 3s" << std::endl; // sleep(3); - std::cout << "start benchmark" << std::endl; +// std::cout << "start benchmark" << std::endl; auto start_time = clock(); AllCases::build(); @@ -75,6 +75,8 @@ int main() { // std::cout << ShortCode("EP4HZ") << std::endl; // std::cout << ShortCode(14323231) << std::endl; + std::cout << RawCode::from_common_code("4fea134") << std::endl; + // std::cerr << (clock() - start_time) / CLOCKS_PER_SEC << "s" << std::endl; std::cerr << (clock() - start_time) * 1000 / CLOCKS_PER_SEC << "ms" << std::endl; diff --git a/src/raw_code/convert.cc b/src/raw_code/convert.cc index cdfbde6..71d5cf1 100644 --- a/src/raw_code/convert.cc +++ b/src/raw_code/convert.cc @@ -2,14 +2,22 @@ #include "common.h" #include "raw_code.h" -RawCode::RawCode(const CommonCode &common_code) { - code = RawCode::extract(common_code.unwrap()); // load from common code -} - RawCode RawCode::from_common_code(const CommonCode &common_code) { return RawCode(common_code); // load from common code } +RawCode RawCode::from_common_code(uint64_t common_code) { + return RawCode(CommonCode(common_code)); // load from common code +} + +RawCode RawCode::from_common_code(const std::string &common_code) { + return RawCode(CommonCode(common_code)); // load from common code +} + +RawCode::RawCode(const CommonCode &common_code) { + code = RawCode::extract(common_code.unwrap()); // load from common code +} + CommonCode RawCode::to_common_code() const { if (!RawCode::check(code)) { throw std::runtime_error("invalid raw code"); diff --git a/src/raw_code/raw_code.cc b/src/raw_code/raw_code.cc index cf7de9b..611ca91 100644 --- a/src/raw_code/raw_code.cc +++ b/src/raw_code/raw_code.cc @@ -24,29 +24,18 @@ RawCode::RawCode(uint64_t raw_code) { } std::ostream& operator<<(std::ostream &out, const RawCode &self) { - char str[16]; - sprintf(str, "%015lX", self.code); - out << str; - return out; -} - -std::string RawCode::dump_case() const { - std::string result; - result.reserve(40); // 5 lines * ("x x x x\n") + char code[16]; char dump_map[] = { /// 0x0 1x2 2x1 1x1 2x2 b101 b110 fill '.', '~', '|', '*', '@', '?', '?', '+' }; - auto raw_code = code; - for (int addr = 0; addr < 20; ++addr, raw_code >>= 3) { - result.push_back(dump_map[raw_code & 0b111]); - if ((addr & 0b11) == 0b11) { - result.push_back('\n'); // new line - } else { - result.push_back(' '); // add space - } + sprintf(code, "%015lX", self.code); // code length -> 15 + out << code << '\n'; + for (int addr = 0; addr < 60; addr += 3) { + out << dump_map[(self.code >> addr) & 0b111]; + out << " " << &"\n"[(addr & 0b11) != 0b01]; } - return result; + return out; } bool RawCode::check(uint64_t raw_code) { // check whether raw code is valid diff --git a/src/raw_code/raw_code.h b/src/raw_code/raw_code.h index 1ea6289..91110d2 100644 --- a/src/raw_code/raw_code.h +++ b/src/raw_code/raw_code.h @@ -10,7 +10,6 @@ class CommonCode; class RawCode { public: uint64_t unwrap() const; - std::string dump_case() const; CommonCode to_common_code() const; static bool check(uint64_t raw_code); @@ -21,7 +20,9 @@ public: static RawCode create(uint64_t raw_code); static RawCode unsafe_create(uint64_t raw_code); + static RawCode from_common_code(uint64_t common_code); static RawCode from_common_code(const CommonCode &common_code); + static RawCode from_common_code(const std::string &common_code); // TODO: mirror functions