|
|
@ -1,9 +1,13 @@ |
|
|
|
#include <algorithm> |
|
|
|
#include <functional> |
|
|
|
#include "common.h" |
|
|
|
#include "short_code.h" |
|
|
|
#include "basic_ranges.h" |
|
|
|
|
|
|
|
//#include "short_code_mark.h"
|
|
|
|
#include "all_cases.h" |
|
|
|
#include "all_cases_offset.h" |
|
|
|
#include "basic_ranges_offset.h" |
|
|
|
#include "range_prefix_offset.h" |
|
|
|
|
|
|
|
CommonCode ShortCode::to_common_code() const { // convert to common code
|
|
|
|
if (ShortCode::check_mode() == ShortCode::NORMAL) { |
|
|
@ -20,12 +24,7 @@ ShortCode::ShortCode(const CommonCode &common_code) { // convert from common cod |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
#include "all_cases.h" |
|
|
|
#include "all_cases_offset.h" |
|
|
|
#include "basic_ranges_offset.h" |
|
|
|
#include "range_prefix_offset.h" |
|
|
|
|
|
|
|
uint64_t ShortCode::fast_decode(uint32_t short_code) { // short code => common code
|
|
|
|
uint64_t ShortCode::fast_decode(uint32_t short_code) { // short code --> common code
|
|
|
|
auto offset = std::upper_bound( // using binary search
|
|
|
|
ALL_CASES_OFFSET, ALL_CASES_OFFSET + 16, short_code |
|
|
|
) - 1; |
|
|
@ -33,64 +32,46 @@ uint64_t ShortCode::fast_decode(uint32_t short_code) { // short code => common c |
|
|
|
return (head << 32) | AllCases::fetch()[head][short_code - *offset]; // release common code
|
|
|
|
} |
|
|
|
|
|
|
|
uint32_t ShortCode::fast_encode(uint64_t common_code) { // common code => short code
|
|
|
|
uint32_t ShortCode::fast_encode(uint64_t common_code) { // common code --> short code
|
|
|
|
auto head = common_code >> 32; // head index
|
|
|
|
const auto &ranges = AllCases::fetch()[head]; // available ranges
|
|
|
|
auto offset = std::lower_bound(ranges.begin(), ranges.end(), (uint32_t)common_code) - ranges.begin(); |
|
|
|
return ALL_CASES_OFFSET[head] + offset; // release short code
|
|
|
|
} |
|
|
|
|
|
|
|
/// ensure that input common code is valid
|
|
|
|
//uint32_t ShortCode::tiny_encode(uint64_t common_code) { // common_code --low-memory--> short_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 = BasicRanges::fetch()[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;
|
|
|
|
//}
|
|
|
|
|
|
|
|
/// ensure that input short code is valid
|
|
|
|
//uint64_t ShortCode::tiny_decode(uint32_t short_code) { // short_code --low-memory--> common_code
|
|
|
|
// uint32_t head = 0, prefix = 0;
|
|
|
|
// for (; head < 16; ++head) {
|
|
|
|
// if (short_code < ALL_CASES_INDEX[head]) { // match head
|
|
|
|
// break;
|
|
|
|
// }
|
|
|
|
// short_code -= ALL_CASES_INDEX[head]; // short code approximate
|
|
|
|
// }
|
|
|
|
// for (; prefix < 256; ++prefix) {
|
|
|
|
// if (short_code < RANGE_PREFIX_INDEX[head][prefix]) { // match range prefix
|
|
|
|
// break;
|
|
|
|
// }
|
|
|
|
// short_code -= RANGE_PREFIX_INDEX[head][prefix]; // short code approximate
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// uint32_t range;
|
|
|
|
// for (int index = 0; index < BASIC_RANGES_INDEX[prefix]; ++index) { // traverse basic ranges
|
|
|
|
// range = BasicRanges::fetch()[index + BASIC_RANGES_OFFSET[prefix]];
|
|
|
|
// if (Common::check_case(head, range)) { // search for valid cases
|
|
|
|
// if (short_code == 0) {
|
|
|
|
// break; // found target range
|
|
|
|
// }
|
|
|
|
// --short_code; // short code approximate
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// return (uint64_t)head << 32 | Common::range_reverse(range); // release common code
|
|
|
|
//}
|
|
|
|
void run(uint32_t head, uint32_t prefix, const std::function<bool(uint32_t)>& f) { |
|
|
|
|
|
|
|
|
|
|
|
const auto &basic_ranges = BasicRanges::fetch(); |
|
|
|
for (auto index = BASIC_RANGES_OFFSET[prefix]; index < basic_ranges.size(); ++index) { |
|
|
|
|
|
|
|
auto broken_offset = Common::check_range(head, basic_ranges[index]); |
|
|
|
if (broken_offset) { // case invalid
|
|
|
|
auto delta = (uint32_t)1 << (32 - broken_offset * 2); // delta to next possible range
|
|
|
|
/// !! -> broken
|
|
|
|
/// ( xx xx xx ) xx xx xx ... (reversed range)
|
|
|
|
/// +1 00 00 00 ... (delta)
|
|
|
|
auto next_min = (Common::range_reverse(basic_ranges[index]) & ~(delta - 1)) + delta; |
|
|
|
while (Common::range_reverse(basic_ranges[++index]) < next_min); // located next range
|
|
|
|
--index; |
|
|
|
} else { |
|
|
|
|
|
|
|
if (f(basic_ranges[index])) { |
|
|
|
|
|
|
|
// TODO: try to using 10-bits range prefix -> less static data
|
|
|
|
// printf("ret -> %09lX\n", (uint64_t)head << 32 | Common::range_reverse(basic_ranges[index]));
|
|
|
|
|
|
|
|
uint64_t ShortCode::tiny_decode(uint32_t short_code) { |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
#include <iostream> |
|
|
|
|
|
|
|
/// NOTE: ensure that input short code is valid
|
|
|
|
uint64_t ShortCode::tiny_decode(uint32_t short_code) { // short code --> common code
|
|
|
|
/// match head index
|
|
|
|
auto offset = std::upper_bound( // binary search
|
|
|
|
ALL_CASES_OFFSET, ALL_CASES_OFFSET + 16, short_code |
|
|
@ -106,41 +87,65 @@ uint64_t ShortCode::tiny_decode(uint32_t short_code) { |
|
|
|
short_code -= *offset; |
|
|
|
|
|
|
|
/// search target range
|
|
|
|
const auto &basic_ranges = BasicRanges::fetch(); |
|
|
|
for (auto index = BASIC_RANGES_OFFSET[prefix]; index < basic_ranges.size(); ++index) { |
|
|
|
// const auto &basic_ranges = BasicRanges::fetch();
|
|
|
|
// for (auto index = BASIC_RANGES_OFFSET[prefix]; index < basic_ranges.size(); ++index) {
|
|
|
|
//
|
|
|
|
// auto broken_offset = Common::check_range(head, basic_ranges[index]);
|
|
|
|
// if (broken_offset) { // case invalid
|
|
|
|
// auto delta = (uint32_t)1 << (32 - broken_offset * 2); // delta to next possible range
|
|
|
|
// /// !! -> broken
|
|
|
|
// /// ( xx xx xx ) xx xx xx ... (reversed range)
|
|
|
|
// /// +1 00 00 00 ... (delta)
|
|
|
|
// auto next_min = (Common::range_reverse(basic_ranges[index]) & ~(delta - 1)) + delta;
|
|
|
|
// while (Common::range_reverse(basic_ranges[++index]) < next_min); // located next range
|
|
|
|
// --index;
|
|
|
|
// } else {
|
|
|
|
//
|
|
|
|
// if (!short_code--) { // short code approximate
|
|
|
|
//
|
|
|
|
// /// found target range
|
|
|
|
// return head << 32 | Common::range_reverse(basic_ranges[index]);
|
|
|
|
//
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// }
|
|
|
|
|
|
|
|
uint32_t range = basic_ranges[index]; // traverse basic ranges
|
|
|
|
// std::cout << "short code = " << short_code << std::endl;
|
|
|
|
// return 0;
|
|
|
|
|
|
|
|
uint32_t broken = Common::check_range(head, basic_ranges[index]); // check and get broken address
|
|
|
|
uint32_t range; |
|
|
|
|
|
|
|
auto range_rev = Common::range_reverse(basic_ranges[index]); // reversed range
|
|
|
|
auto lambda = [&short_code, &range](uint32_t dat) -> bool { |
|
|
|
|
|
|
|
if (broken) { // invalid case
|
|
|
|
auto delta = (uint32_t)1 << (32 - broken * 2); // this --delta--> next possible range
|
|
|
|
auto next_min = (range_rev & ~(delta - 1)) + delta; |
|
|
|
while (Common::range_reverse(basic_ranges[++index]) < next_min); // located next range
|
|
|
|
--index; |
|
|
|
} else { |
|
|
|
// return (!short_code--);
|
|
|
|
|
|
|
|
// std::cout << "short code = " << short_code << std::endl;
|
|
|
|
if (!short_code--) { // short code approximate
|
|
|
|
|
|
|
|
// std::cout << "short code reach 0" << std::endl;
|
|
|
|
range = dat; |
|
|
|
return true; |
|
|
|
/// found target range
|
|
|
|
return head << 32 | range_rev; |
|
|
|
// return head << 32 | Common::range_reverse(basic_ranges[index]);
|
|
|
|
|
|
|
|
} |
|
|
|
return false; |
|
|
|
}; |
|
|
|
run(head, prefix, lambda); |
|
|
|
|
|
|
|
} |
|
|
|
return (uint64_t)head << 32 | Common::range_reverse(range); |
|
|
|
printf("ret -> %09lX\n", (uint64_t)head << 32 | Common::range_reverse(range)); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
printf("error\n"); |
|
|
|
|
|
|
|
return 0; // never reach when input valid
|
|
|
|
} |
|
|
|
|
|
|
|
#include <iostream> |
|
|
|
|
|
|
|
uint32_t ShortCode::tiny_encode(uint64_t common_code) { |
|
|
|
/// NOTE: ensure that input common code is valid
|
|
|
|
uint32_t ShortCode::tiny_encode(uint64_t common_code) { // common code --> short code
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|