From d6c213f322c98fac23eee7a3a6946e75b7e10f6c Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Wed, 18 Jan 2023 14:07:35 +0800 Subject: [PATCH] update: enhance CommonCode convert interface --- src/common_code/CMakeLists.txt | 2 +- src/common_code/common_code.cc | 24 -------------------- src/common_code/common_code.h | 16 +++++++++---- src/common_code/convert.cc | 41 ++++++++++++++++++++++++++++++++++ src/main.cc | 3 +++ src/raw_code/CMakeLists.txt | 2 +- src/raw_code/convert.cc | 18 +++++++-------- src/short_code/convert.cc | 16 ++++++------- 8 files changed, 75 insertions(+), 47 deletions(-) create mode 100644 src/common_code/convert.cc diff --git a/src/common_code/CMakeLists.txt b/src/common_code/CMakeLists.txt index 702aa81..0d4ebd2 100644 --- a/src/common_code/CMakeLists.txt +++ b/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) diff --git a/src/common_code/common_code.cc b/src/common_code/common_code.cc index 2a3f273..7dfb297 100644 --- a/src/common_code/common_code.cc +++ b/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); diff --git a/src/common_code/common_code.h b/src/common_code/common_code.h index abfab35..dd5c6e3 100644 --- a/src/common_code/common_code.h +++ b/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; diff --git a/src/common_code/convert.cc b/src/common_code/convert.cc new file mode 100644 index 0000000..dbd95b1 --- /dev/null +++ b/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 +} diff --git a/src/main.cc b/src/main.cc index 3d83783..69d3bd5 100644 --- a/src/main.cc +++ b/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; diff --git a/src/raw_code/CMakeLists.txt b/src/raw_code/CMakeLists.txt index c5707cd..10f0b62 100644 --- a/src/raw_code/CMakeLists.txt +++ b/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) diff --git a/src/raw_code/convert.cc b/src/raw_code/convert.cc index 19319fd..a361b78 100644 --- a/src/raw_code/convert.cc +++ b/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; diff --git a/src/short_code/convert.cc b/src/short_code/convert.cc index 6caebd5..c90dca0 100644 --- a/src/short_code/convert.cc +++ b/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