From a7c0779f5adad7e3cf8d4d7a8a0ab64e98243bb6 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Fri, 6 Jan 2023 11:24:43 +0800 Subject: [PATCH] feat: tiny encode function for short code --- klotski/main.cc | 1 + klotski/short_code.cc | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/klotski/main.cc b/klotski/main.cc index e243018..1ced2bb 100644 --- a/klotski/main.cc +++ b/klotski/main.cc @@ -34,6 +34,7 @@ int main() { auto s = ShortCode(); s.speed_up(ShortCode::Mode::NORMAL); // enter normal mode first + printf("%d\n", s.tiny_encode(0x6EC0F8800)); printf("%09lX\n", s.tiny_decode(14323231)); return 0; diff --git a/klotski/short_code.cc b/klotski/short_code.cc index edb61c3..38985ba 100644 --- a/klotski/short_code.cc +++ b/klotski/short_code.cc @@ -79,3 +79,23 @@ uint64_t ShortCode::tiny_decode(uint32_t short_code) { // short_code --low-mem-- } return (uint64_t)head << 32 | Common::range_reverse(range); // release common code } + +// TODO: ensure input common_code valid +// TODO: load basic ranges before tiny_encode +uint32_t ShortCode::tiny_encode(uint64_t common_code) { + uint32_t offset = 0; + uint32_t head = common_code >> 32; // common code head + uint32_t prefix = (common_code >> 24) & 0xFF; // common code range prefix + auto target = Common::range_reverse((uint32_t)common_code); // target range + + for (int index = 0; index < BASIC_RANGES_INDEX[prefix]; ++index) { // traverse basic ranges + uint32_t range = basic_ranges[index + BASIC_RANGES_OFFSET[prefix]]; + if (range == target) { + break; // found target range + } + if (Common::check_case(head, range)) { // search for valid cases + ++offset; // record sub offset + } + } + return ALL_CASES_OFFSET[head] + RANGE_PREFIX_OFFSET[head][prefix] + offset; +}