Browse Source

update: more convert interface for RawCode

legacy
Dnomd343 2 years ago
parent
commit
c669c368fa
  1. 1
      src/all_cases/basic_ranges.cc
  2. 4
      src/main.cc
  3. 16
      src/raw_code/convert.cc
  4. 25
      src/raw_code/raw_code.cc
  5. 3
      src/raw_code/raw_code.h

1
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 std::stable_sort(BasicRanges::data.begin(), BasicRanges::data.end()); // sort basic ranges
for (auto &range : BasicRanges::data) { for (auto &range : BasicRanges::data) {
range = Common::range_reverse(range); // basic ranges reverse range = Common::range_reverse(range); // basic ranges reverse

4
src/main.cc

@ -20,7 +20,7 @@ int main() {
// std::cout << "wait 3s" << std::endl; // std::cout << "wait 3s" << std::endl;
// sleep(3); // sleep(3);
std::cout << "start benchmark" << std::endl; // std::cout << "start benchmark" << std::endl;
auto start_time = clock(); auto start_time = clock();
AllCases::build(); AllCases::build();
@ -75,6 +75,8 @@ int main() {
// std::cout << ShortCode("EP4HZ") << std::endl; // std::cout << ShortCode("EP4HZ") << std::endl;
// std::cout << ShortCode(14323231) << 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) / CLOCKS_PER_SEC << "s" << std::endl;
std::cerr << (clock() - start_time) * 1000 / CLOCKS_PER_SEC << "ms" << std::endl; std::cerr << (clock() - start_time) * 1000 / CLOCKS_PER_SEC << "ms" << std::endl;

16
src/raw_code/convert.cc

@ -2,14 +2,22 @@
#include "common.h" #include "common.h"
#include "raw_code.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) { RawCode RawCode::from_common_code(const CommonCode &common_code) {
return RawCode(common_code); // load from 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 { CommonCode RawCode::to_common_code() const {
if (!RawCode::check(code)) { if (!RawCode::check(code)) {
throw std::runtime_error("invalid raw code"); throw std::runtime_error("invalid raw code");

25
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) { std::ostream& operator<<(std::ostream &out, const RawCode &self) {
char str[16]; char code[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 dump_map[] = { char dump_map[] = {
/// 0x0 1x2 2x1 1x1 2x2 b101 b110 fill /// 0x0 1x2 2x1 1x1 2x2 b101 b110 fill
'.', '~', '|', '*', '@', '?', '?', '+' '.', '~', '|', '*', '@', '?', '?', '+'
}; };
auto raw_code = code; sprintf(code, "%015lX", self.code); // code length -> 15
for (int addr = 0; addr < 20; ++addr, raw_code >>= 3) { out << code << '\n';
result.push_back(dump_map[raw_code & 0b111]); for (int addr = 0; addr < 60; addr += 3) {
if ((addr & 0b11) == 0b11) { out << dump_map[(self.code >> addr) & 0b111];
result.push_back('\n'); // new line out << " " << &"\n"[(addr & 0b11) != 0b01];
} else {
result.push_back(' '); // add space
}
} }
return result; return out;
} }
bool RawCode::check(uint64_t raw_code) { // check whether raw code is valid bool RawCode::check(uint64_t raw_code) { // check whether raw code is valid

3
src/raw_code/raw_code.h

@ -10,7 +10,6 @@ class CommonCode;
class RawCode { class RawCode {
public: public:
uint64_t unwrap() const; uint64_t unwrap() const;
std::string dump_case() const;
CommonCode to_common_code() const; CommonCode to_common_code() const;
static bool check(uint64_t raw_code); static bool check(uint64_t raw_code);
@ -21,7 +20,9 @@ public:
static RawCode create(uint64_t raw_code); static RawCode create(uint64_t raw_code);
static RawCode unsafe_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 CommonCode &common_code);
static RawCode from_common_code(const std::string &common_code);
// TODO: mirror functions // TODO: mirror functions

Loading…
Cancel
Save