Browse Source

update: enhance ShortCode module

master
Dnomd343 2 years ago
parent
commit
acab8830d8
  1. 3
      src/all_cases/all_cases.h
  2. 7
      src/main.cc
  3. 18
      src/short_code/serialize.cc
  4. 53
      src/short_code/short_code.cc
  5. 11
      src/short_code/short_code.h

3
src/all_cases/all_cases.h

@ -18,13 +18,10 @@ public:
static enum Status status();
static const std::vector<uint32_t> (&fetch())[16];
// static int check_case(uint32_t head, uint32_t range);
private:
static bool available;
static std::mutex building;
static std::vector<uint32_t> data[16];
static void build_data();
// static int check_case(uint32_t head, uint32_t range);
};

7
src/main.cc

@ -279,8 +279,11 @@ int main() {
// printf("%09lX\n", ShortCode::tiny_decode(14323231));
// std::cout << ShortCode::tiny_encode(0x6EC0F8800) << std::endl;
std::cout << ShortCode(14323231).to_string() << std::endl;
std::cout << ShortCode::from_string("EP4HZ").unwrap() << std::endl;
// std::cout << ShortCode(14323231).to_string() << std::endl;
// std::cout << ShortCode::from_string("EP4HZ").unwrap() << std::endl;
std::cout << ShortCode("EP4HZ") << std::endl;
std::cout << ShortCode(14323231) << std::endl;
// printf("%09lX\n", ShortCode::fast_decode(14323231));
// std::cout << ShortCode::fast_encode(0x6EC0F8800) << std::endl;

18
src/short_code/serialize.cc

@ -10,9 +10,8 @@ std::string ShortCode::to_string() const { // encode as 5-bits string
uint32_t short_code = code;
std::string result(5, '\0'); // short code length 5
for (int n = 0; n < 5; ++n) {
uint8_t bit = short_code % 32;
short_code = (short_code - bit) / 32;
result[4 - n] = SHORT_CODE_TABLE[bit];
result[4 - n] = SHORT_CODE_TABLE[short_code & 0b11111]; // aka _ % 32
short_code >>= 5; // aka _ / 32
}
return result;
}
@ -21,24 +20,21 @@ ShortCode::ShortCode(const std::string &short_code) { // 5-bits string decode
if (short_code.length() != 5) { // check string length
throw std::invalid_argument("short code format error");
}
uint64_t result = 0;
for (auto bit : short_code) {
result *= 32;
result <<= 5; // aka _ * 32
if (bit >= 'a' && bit <= 'z') {
bit -= 32; // convert to uppercase
}
if (bit >= '1' && bit <= 'Z') {
if (bit >= '1' && bit <= 'Z') { // valid characters
result += (bit = SHORT_CODE_TABLE_REV[bit - 49]); // table convert
if (bit != -1) {
continue; // pass check
if (bit == -1) {
throw std::invalid_argument("short code format error"); // unknown characters
}
}
throw std::invalid_argument("short code format error"); // unknown characters
}
if (!ShortCode::check(result)) { // check converted short code
throw std::invalid_argument("invalid short code");
}
code = result;
code = result; // apply convert result
}

53
src/short_code/short_code.cc

@ -1,21 +1,14 @@
#include "all_cases.h"
#include "short_code.h"
bool ShortCode::fast_mode_available = false;
bool ShortCode::normal_mode_available = false;
uint32_t ShortCode::unwrap() const { // get raw uint32_t code
return code;
uint32_t ShortCode::unwrap() const {
return code; // raw uint32_t code
}
ShortCode ShortCode::create(uint32_t short_code) {
return ShortCode(short_code);
}
bool ShortCode::check(uint32_t short_code) {
return short_code < ShortCode::SHORT_CODE_LIMIT; // 0 ~ (SHORT_CODE_LIMIT - 1)
}
ShortCode::ShortCode(uint32_t short_code) {
if (!ShortCode::check(short_code)) { // check input short code
throw std::invalid_argument("invalid short code");
@ -23,24 +16,18 @@ ShortCode::ShortCode(uint32_t short_code) {
code = short_code;
}
bool ShortCode::check(uint32_t short_code) {
return short_code < SHORT_CODE_LIMIT; // 0 ~ (SHORT_CODE_LIMIT - 1)
}
void ShortCode::speed_up(ShortCode::Mode mode) {
if (fast_mode_available) {
return; // fast mode already available
}
if (mode == ShortCode::FAST) { // build fast mode data
// build_mappings();
// TODO: confirm AllCases data available
AllCases::build();
} else if (mode == ShortCode::NORMAL && !normal_mode_available) { // build normal mode data
BasicRanges::build(); // blocking function
normal_mode_available = true;
}
std::ostream& operator<<(std::ostream &out, const ShortCode &self) {
out << self.to_string() << "(" << self.code << ")"; // short code info
return out;
}
bool ShortCode::fast_mode_available = false;
bool ShortCode::normal_mode_available = false;
ShortCode::Mode ShortCode::mode() { // ensure speed up enabled and return current mode
if (fast_mode_available) {
return ShortCode::FAST; // fast mode already enabled
@ -48,9 +35,19 @@ ShortCode::Mode ShortCode::mode() { // ensure speed up enabled and return curren
if (normal_mode_available) {
return ShortCode::NORMAL; // normal mode already enabled
}
speed_up(ShortCode::Mode::NORMAL); // without initialized -> enter normal mode
return ShortCode::Mode::NORMAL; // use normal mode
speed_up(ShortCode::Mode::NORMAL); // uninitialized -> enable normal mode
return ShortCode::Mode::NORMAL; // normal mode enabled
}
void ShortCode::speed_up(ShortCode::Mode mode) {
if (fast_mode_available) {
return; // fast mode already available
}
if (mode == ShortCode::FAST) { // build fast mode data
AllCases::build(); // blocking function
fast_mode_available = true;
} else if (!normal_mode_available) { // build normal mode data
BasicRanges::build(); // blocking function
normal_mode_available = true;
}
}

11
src/short_code/short_code.h

@ -1,21 +1,24 @@
#pragma once
#include <cstdint>
#include <ostream>
#include "common_code.h"
const uint32_t SHORT_CODE_LIMIT = 29334498;
class CommonCode;
class ShortCode {
public:
enum Mode {NORMAL, FAST};
static void speed_up(enum Mode mode);
uint32_t unwrap() const;
std::string to_string() const;
CommonCode to_common_code() const;
static bool check(uint32_t short_code);
// TODO: std::cout << ShortCode(...) << std::endl;
static void speed_up(enum Mode mode);
static bool check(uint32_t short_code);
friend std::ostream& operator<<(std::ostream &out, const ShortCode &self);
explicit ShortCode(uint32_t short_code);
explicit ShortCode(const std::string &short_code);
@ -33,10 +36,8 @@ private:
uint32_t code;
static bool fast_mode_available;
static bool normal_mode_available;
static const uint32_t SHORT_CODE_LIMIT = 29334498;
static enum Mode mode();
static uint64_t fast_decode(uint32_t short_code);
static uint32_t fast_encode(uint64_t common_code);
static uint64_t tiny_decode(uint32_t short_code);

Loading…
Cancel
Save