Browse Source

update: enhance CommonCode convert interface

master
Dnomd343 2 years ago
parent
commit
d6c213f322
  1. 2
      src/common_code/CMakeLists.txt
  2. 24
      src/common_code/common_code.cc
  3. 16
      src/common_code/common_code.h
  4. 41
      src/common_code/convert.cc
  5. 3
      src/main.cc
  6. 2
      src/raw_code/CMakeLists.txt
  7. 18
      src/raw_code/convert.cc
  8. 16
      src/short_code/convert.cc

2
src/common_code/CMakeLists.txt

@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.0)
add_library(common_code serialize.cc common_code.cc)
add_library(common_code convert.cc serialize.cc common_code.cc)
target_link_libraries(common_code utils raw_code)

24
src/common_code/common_code.cc

@ -6,26 +6,10 @@ uint64_t CommonCode::unwrap() const {
return code; // raw uint64_t code
}
RawCode CommonCode::to_raw_code() const {
return RawCode(*this); // convert to raw code
}
ShortCode CommonCode::to_short_code() const {
return ShortCode(*this); // convert to short code
}
CommonCode CommonCode::create(uint64_t common_code) {
return CommonCode(common_code); // create from uint64_t
}
CommonCode CommonCode::from_raw_code(const RawCode &raw_code) {
return raw_code.to_common_code(); // create from raw code
}
CommonCode CommonCode::from_short_code(const ShortCode &short_code) {
return short_code.to_common_code(); // create from short code
}
CommonCode CommonCode::unsafe_create(uint64_t common_code) { // create without check
auto common = CommonCode(); // init directly
common.code = common_code;
@ -39,14 +23,6 @@ CommonCode::CommonCode(uint64_t common_code) {
code = common_code;
}
CommonCode::CommonCode(const RawCode &raw_code) { // load from raw code
code = raw_code.to_common_code().code;
}
CommonCode::CommonCode(const ShortCode &short_code) { // load from short code
code = short_code.to_common_code().code;
}
std::ostream& operator<<(std::ostream &out, const CommonCode &self) {
char str[10];
sprintf(str, "%09lX", self.code);

16
src/common_code/common_code.h

@ -11,24 +11,32 @@ class ShortCode;
class CommonCode {
public:
static bool check(uint64_t common_code);
friend std::ostream& operator<<(std::ostream &out, const CommonCode &self);
/// Export functions
uint64_t unwrap() const;
RawCode to_raw_code() const;
ShortCode to_short_code() const;
std::string to_string(bool shorten = false) const;
static bool check(uint64_t common_code);
friend std::ostream& operator<<(std::ostream &out, const CommonCode &self);
/// CommonCode constructors
explicit CommonCode(uint64_t common_code);
explicit CommonCode(const RawCode &raw_code);
explicit CommonCode(const ShortCode &short_code);
explicit CommonCode(const std::string &common_code);
/// Rust-style initialization
static CommonCode create(uint64_t common_code);
static CommonCode unsafe_create(uint64_t common_code);
static CommonCode from_raw_code(const RawCode &raw_code);
static CommonCode from_string(const std::string &common_code);
static CommonCode from_raw_code(uint64_t raw_code);
static CommonCode from_raw_code(const RawCode &raw_code);
static CommonCode from_short_code(uint32_t short_code);
static CommonCode from_short_code(const ShortCode &short_code);
static CommonCode from_short_code(const std::string &short_code);
private:
uint64_t code;

41
src/common_code/convert.cc

@ -0,0 +1,41 @@
#include "common_code.h"
/// CommonCode to RawCode
RawCode CommonCode::to_raw_code() const {
return RawCode(*this); // convert to raw code
}
/// CommonCode to ShortCode
ShortCode CommonCode::to_short_code() const {
return ShortCode(*this); // convert to short code
}
/// RawCode to CommonCode
CommonCode CommonCode::from_raw_code(uint64_t raw_code) {
return RawCode(raw_code).to_common_code();
}
CommonCode CommonCode::from_raw_code(const RawCode &raw_code) {
return raw_code.to_common_code();
}
CommonCode::CommonCode(const RawCode &raw_code) {
code = raw_code.to_common_code().code; // convert from raw code
}
/// ShortCode to CommonCode
CommonCode CommonCode::from_short_code(uint32_t short_code) {
return ShortCode(short_code).to_common_code();
}
CommonCode CommonCode::from_short_code(const ShortCode &short_code) {
return short_code.to_common_code();
}
CommonCode CommonCode::from_short_code(const std::string &short_code) {
return ShortCode(short_code).to_common_code();
}
CommonCode::CommonCode(const ShortCode &short_code) {
code = short_code.to_common_code().code; // convert from short code
}

3
src/main.cc

@ -76,6 +76,9 @@ int main() {
// std::cout << ShortCode(14323231) << std::endl;
std::cout << RawCode::from_common_code("4fea134") << std::endl;
std::cout << CommonCode::from_raw_code(0xE58FC85FFEBC4DB) << std::endl;
std::cout << ShortCode::from_common_code("4fea134") << std::endl;
std::cout << CommonCode::from_short_code("AXCZN") << std::endl;
// std::cerr << (clock() - start_time) / CLOCKS_PER_SEC << "s" << std::endl;

2
src/raw_code/CMakeLists.txt

@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.0)
add_library(raw_code convert.cc raw_code.cc)
target_link_libraries(raw_code utils common_code)
target_link_libraries(raw_code utils)

18
src/raw_code/convert.cc

@ -2,6 +2,15 @@
#include "common.h"
#include "raw_code.h"
/// RawCode to CommonCode
CommonCode RawCode::to_common_code() const {
if (!RawCode::check(code)) {
throw std::runtime_error("invalid raw code");
}
/// pass raw code checker -> common code must valid
return CommonCode::unsafe_create(RawCode::compact(code));
}
/// CommonCode to RawCode
RawCode RawCode::from_common_code(const CommonCode &common_code) {
return RawCode(common_code); // load from common code
@ -19,15 +28,6 @@ RawCode::RawCode(const CommonCode &common_code) {
code = RawCode::extract(common_code.unwrap()); // load from common code
}
/// RawCode to CommonCode
CommonCode RawCode::to_common_code() const {
if (!RawCode::check(code)) {
throw std::runtime_error("invalid raw code");
}
/// pass check -> common code must valid
return CommonCode::unsafe_create(RawCode::compact(code)); // release common code
}
/// NOTE: ensure that input raw code is valid!
uint64_t RawCode::compact(uint64_t raw_code) { // raw code --> common code
int unfilled = 16;

16
src/short_code/convert.cc

@ -6,6 +6,14 @@
#include "basic_ranges_offset.h"
#include "range_prefix_offset.h"
/// ShortCode to CommonCode
CommonCode ShortCode::to_common_code() const { // convert to common code
if (ShortCode::mode() == ShortCode::NORMAL) {
return CommonCode::unsafe_create(tiny_decode(code)); // normal mode
}
return CommonCode::unsafe_create(fast_decode(code)); // fast mode
}
/// CommonCode to ShortCode
ShortCode ShortCode::from_common_code(uint64_t common_code) {
return ShortCode(CommonCode(common_code));
@ -27,14 +35,6 @@ ShortCode::ShortCode(const CommonCode &common_code) { // convert from common cod
}
}
/// ShortCode to CommonCode
CommonCode ShortCode::to_common_code() const { // convert to common code
if (ShortCode::mode() == ShortCode::NORMAL) {
return CommonCode::unsafe_create(tiny_decode(code)); // normal mode
}
return CommonCode::unsafe_create(fast_decode(code)); // fast mode
}
/// NOTE: ensure that input common code is valid!
uint32_t ShortCode::fast_encode(uint64_t common_code) { // common code --> short code
auto head = common_code >> 32; // head index

Loading…
Cancel
Save