Browse Source

feat: new tiny decoder for ShortCode

master
Dnomd343 2 years ago
parent
commit
fa38e39306
  1. 34
      src/main.cc
  2. 46
      src/short_code/data_loader.cc
  3. 10
      src/short_code/offset/all_cases_offset.h
  4. 2
      src/short_code/offset/basic_ranges_offset.h
  5. 2
      src/short_code/offset/range_prefix_offset.h
  6. 4
      src/short_code/short_code.h

34
src/main.cc

@ -269,14 +269,16 @@ int main() {
// }
// }
auto br = BasicRanges::fetch();
for (auto &range : br) {
range = Common::range_reverse(range);
}
printf("%09lX\n", ShortCode::tiny_decode_demo(14323231));
// auto br = BasicRanges::fetch();
// for (auto &range : br) {
// range = Common::range_reverse(range);
// }
/// 1017983: 0FFFFFFF
/// 1017984: 10000000
/// 1017985: 1000000C
int sum = 0;
// int sum = 0;
// printf(" ");
// for (uint32_t prefix = 0; prefix < 0x1000; ++prefix) {
// printf("%7td, ", std::lower_bound(br.begin(), br.end(), prefix << 20) - br.begin());
@ -285,17 +287,17 @@ int main() {
// }
// }
for (int head = 0; head < 16; ++head) {
auto &a = AllCases::fetch()[head];
printf("\n/// --------------------------------- 0x%X ---------------------------------\n ", head);
for (uint32_t prefix = 0; prefix < 0x1000; ++prefix) {
printf("%7td, ", std::lower_bound(a.begin(), a.end(), prefix << 20) - a.begin());
if (sum++ % 8 == 7 and prefix != 0xFFF) {
printf("\n ");
}
}
}
printf("\n");
// for (int head = 0; head < 16; ++head) {
// auto &a = AllCases::fetch()[head];
// printf("\n/// --------------------------------- 0x%X ---------------------------------\n ", head);
// for (uint32_t prefix = 0; prefix < 0x1000; ++prefix) {
// printf("%7td, ", std::lower_bound(a.begin(), a.end(), prefix << 20) - a.begin());
// if (sum++ % 8 == 7 and prefix != 0xFFF) {
// printf("\n ");
// }
// }
// }
// printf("\n");
// printf("%09lX\n", ShortCode::fast_decode(14323231));
// std::cout << ShortCode::fast_encode(0x6EC0F8800) << std::endl;

46
src/short_code/data_loader.cc

@ -66,3 +66,49 @@ uint32_t ShortCode::fast_encode(uint64_t common_code) {
auto offset = std::lower_bound(ranges.begin(), ranges.end(), (uint32_t)common_code) - ranges.begin();
return ALL_CASES_OFFSET[head] + offset; // release short code
}
#include "basic_ranges_offset.h"
#include "range_prefix_offset.h"
#include <iostream>
#include "common.h"
uint64_t ShortCode::tiny_decode_demo(uint32_t short_code) {
/// match head index
auto offset = std::upper_bound( // binary search
ALL_CASES_OFFSET, ALL_CASES_OFFSET + 16, short_code
) - 1;
uint64_t head = offset - ALL_CASES_OFFSET; // head index
short_code -= *offset;
/// match range prefix
offset = std::upper_bound( // binary search
RANGE_PREFIX_OFFSET_[head], RANGE_PREFIX_OFFSET_[head] + 4096, short_code
) - 1;
uint32_t prefix = offset - RANGE_PREFIX_OFFSET_[head]; // range prefix
short_code -= *offset;
uint32_t start_point = BASIC_RANGES_OFFSET_[prefix];
printf("prefix: %X\n", prefix);
printf("start at %d\n", start_point);
printf("next %d valid case\n", short_code);
uint32_t range;
for (auto index = start_point; index < BasicRanges::fetch().size(); ++index) { // traverse basic ranges
range = BasicRanges::fetch()[index];
if (Common::check_case(head, range)) { // search for valid cases
if (!short_code--) { // short code approximate
break; // found target range
}
}
}
printf("range -> %08X\n", Common::range_reverse(range));
return (uint64_t)head << 32 | Common::range_reverse(range);
}

10
src/short_code/offset/all_cases_offset.h

@ -0,0 +1,10 @@
#pragma once
#include <cstdint>
const uint32_t ALL_CASES_OFFSET_[16] = {
0, 2942906, 5203298, 8146204,
8146204, 10468254, 12345199, 14667249,
14667249, 16989299, 18866244, 21188294,
21188294, 24131200, 26391592, 29334498,
};

2
src/short_code/offset/basic_ranges_offset.h

@ -2,7 +2,7 @@
#include <cstdint>
const uint32_t BASIC_RANGES_OFFSET[4096] = {
const uint32_t BASIC_RANGES_OFFSET_[4096] = {
0, 18272, 24960, 31648, 49920, 56608, 59056, 61504,
68192, 74880, 77328, 79776, 86464, 104736, 111424, 118112,
136384, 143072, 145520, 147968, 154656, 157104, 158000, 158896,

2
src/short_code/offset/range_prefix_offset.h

@ -2,7 +2,7 @@
#include <cstdint>
const uint32_t RANGE_PREFIX_OFFSET[16][4096] = {
const uint32_t RANGE_PREFIX_OFFSET_[16][4096] = {
/// --------------------------------- 0x0 ---------------------------------
0, 9527, 12991, 16067, 25594, 29058, 30527, 31657,
35121, 38525, 39766, 41041, 44445, 53972, 57436, 60512,

4
src/short_code/short_code.h

@ -37,8 +37,8 @@ public:
static uint64_t fast_decode(uint32_t short_code);
static uint32_t fast_encode(uint64_t common_code);
static uint64_t tidy_decode_demo(uint32_t short_code);
static uint32_t tidy_encode_demo(uint64_t common_code);
static uint64_t tiny_decode_demo(uint32_t short_code);
static uint32_t tiny_encode_demo(uint64_t common_code);
private:
uint32_t code;

Loading…
Cancel
Save