Browse Source

feat: tiny encode function for short code

master
Dnomd343 2 years ago
parent
commit
a7c0779f5a
  1. 1
      klotski/main.cc
  2. 20
      klotski/short_code.cc

1
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;

20
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;
}

Loading…
Cancel
Save