From 0d3859986e042d4058195479df1a9f96c4898c68 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Wed, 18 Jan 2023 13:47:11 +0800 Subject: [PATCH] update: more convert interface for ShortCode --- src/raw_code/convert.cc | 6 ++++-- src/raw_code/raw_code.h | 10 +++++++--- src/short_code/convert.cc | 20 +++++++++++++++----- src/short_code/short_code.h | 22 ++++++++++++++-------- 4 files changed, 40 insertions(+), 18 deletions(-) diff --git a/src/raw_code/convert.cc b/src/raw_code/convert.cc index 71d5cf1..19319fd 100644 --- a/src/raw_code/convert.cc +++ b/src/raw_code/convert.cc @@ -2,6 +2,7 @@ #include "common.h" #include "raw_code.h" +/// CommonCode to RawCode RawCode RawCode::from_common_code(const CommonCode &common_code) { return RawCode(common_code); // load from common code } @@ -18,6 +19,7 @@ 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"); @@ -26,7 +28,7 @@ CommonCode RawCode::to_common_code() const { return CommonCode::unsafe_create(RawCode::compact(code)); // release common code } -/// NOTE: ensure that input raw code is valid !!! +/// NOTE: ensure that input raw code is valid! uint64_t RawCode::compact(uint64_t raw_code) { // raw code --> common code int unfilled = 16; uint64_t head = 0; // 2x2 block address @@ -55,7 +57,7 @@ uint64_t RawCode::compact(uint64_t raw_code) { // raw code --> common code return head | (range << (unfilled << 1)); // fill low bits as zero } -/// NOTE: ensure that input common code is valid !!! +/// NOTE: ensure that input common code is valid! uint64_t RawCode::extract(uint64_t common_code) { // common code --> raw code auto code = C_2x2 << (common_code >> 32) * 3; // flag for 2x2 block auto range = Common::range_reverse((uint32_t)common_code); // reversed range diff --git a/src/raw_code/raw_code.h b/src/raw_code/raw_code.h index 91110d2..fdc8613 100644 --- a/src/raw_code/raw_code.h +++ b/src/raw_code/raw_code.h @@ -9,17 +9,21 @@ class CommonCode; class RawCode { public: - uint64_t unwrap() const; - CommonCode to_common_code() const; - static bool check(uint64_t raw_code); friend std::ostream& operator<<(std::ostream &out, const RawCode &self); + /// Export functions + uint64_t unwrap() const; + CommonCode to_common_code() const; + + /// RawCode constructors explicit RawCode(uint64_t raw_code); explicit RawCode(const CommonCode &common_code); + /// Rust-style initialization static RawCode create(uint64_t raw_code); static RawCode unsafe_create(uint64_t raw_code); + static RawCode from_common_code(uint64_t common_code); static RawCode from_common_code(const CommonCode &common_code); static RawCode from_common_code(const std::string &common_code); diff --git a/src/short_code/convert.cc b/src/short_code/convert.cc index 6e6a004..6caebd5 100644 --- a/src/short_code/convert.cc +++ b/src/short_code/convert.cc @@ -6,8 +6,17 @@ #include "basic_ranges_offset.h" #include "range_prefix_offset.h" +/// CommonCode to ShortCode +ShortCode ShortCode::from_common_code(uint64_t common_code) { + return ShortCode(CommonCode(common_code)); +} + ShortCode ShortCode::from_common_code(const CommonCode &common_code) { - return ShortCode(common_code); // convert from common code + return ShortCode(common_code); +} + +ShortCode ShortCode::from_common_code(const std::string &common_code) { + return ShortCode(CommonCode(common_code)); } ShortCode::ShortCode(const CommonCode &common_code) { // convert from common code @@ -18,6 +27,7 @@ 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 @@ -25,7 +35,7 @@ CommonCode ShortCode::to_common_code() const { // convert to common code return CommonCode::unsafe_create(fast_decode(code)); // fast mode } -/// NOTE: ensure that input common code is valid !!! +/// 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 const auto &ranges = AllCases::fetch()[head]; // available ranges @@ -33,7 +43,7 @@ uint32_t ShortCode::fast_encode(uint64_t common_code) { // common code --> short return ALL_CASES_OFFSET[head] + offset; // release short code } -/// NOTE: ensure that input short code is valid !!! +/// NOTE: ensure that input short code is valid! 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 @@ -42,7 +52,7 @@ uint64_t ShortCode::fast_decode(uint32_t short_code) { // short code --> common return (head << 32) | AllCases::fetch()[head][short_code - *offset]; // release common code } -/// NOTE: ensure that input common code is valid !!! +/// NOTE: ensure that input common code is valid! uint32_t ShortCode::tiny_encode(uint64_t common_code) { // common code --> short code /// load head index and range prefix uint32_t head = common_code >> 32; @@ -70,7 +80,7 @@ uint32_t ShortCode::tiny_encode(uint64_t common_code) { // common code --> short return ALL_CASES_OFFSET[head] + RANGE_PREFIX_OFFSET[head][prefix] + offset; } -/// NOTE: ensure that input short code is valid !!! +/// 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 diff --git a/src/short_code/short_code.h b/src/short_code/short_code.h index eb6f724..ca26a44 100644 --- a/src/short_code/short_code.h +++ b/src/short_code/short_code.h @@ -13,26 +13,32 @@ class ShortCode { public: enum Mode {NORMAL, FAST}; - uint32_t unwrap() const; - std::string to_string() const; - CommonCode to_common_code() const; - static void speed_up(enum Mode mode); static bool check(uint32_t short_code); friend std::ostream& operator<<(std::ostream &out, const ShortCode &self); + /// Export functions + uint32_t unwrap() const; + std::string to_string() const; + CommonCode to_common_code() const; + + /// ShortCode constructors explicit ShortCode(uint32_t short_code); explicit ShortCode(const std::string &short_code); explicit ShortCode(const CommonCode &common_code); - static ShortCode create(uint32_t short_code); - static ShortCode from_string(const std::string &short_code); - static ShortCode from_common_code(const CommonCode &common_code); - ShortCode(uint32_t short_code, enum Mode mode) : ShortCode(short_code) { speed_up(mode); } ShortCode(const std::string &short_code, enum Mode mode) : ShortCode(short_code) { speed_up(mode); } ShortCode(const CommonCode &common_code, enum Mode mode) : ShortCode(common_code) { speed_up(mode); } + /// Rust-style initialization + static ShortCode create(uint32_t short_code); + static ShortCode from_string(const std::string &short_code); + + static ShortCode from_common_code(uint64_t common_code); + static ShortCode from_common_code(const CommonCode &common_code); + static ShortCode from_common_code(const std::string &common_code); + private: uint32_t code; static bool fast_mode_available;