From 3f0569d6f92ba08b883ca555c8ddb5f53864eb77 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Fri, 27 Jan 2023 14:20:59 +0800 Subject: [PATCH] chore: code modules using namespace klotski --- src/klotski/all_cases/all_cases.cc | 2 +- src/klotski/all_cases/basic_ranges.cc | 2 +- src/klotski/analyse/analyse.h | 3 + src/klotski/common_code/common_code.cc | 14 ++-- src/klotski/common_code/common_code.h | 85 ++++++++++--------- src/klotski/common_code/convert.cc | 4 + src/klotski/common_code/serialize.cc | 2 + src/klotski/fast_cal/fast_cal.h | 3 + src/klotski/ffi/codec.cc | 2 + src/klotski/raw_code/convert.cc | 3 +- src/klotski/raw_code/raw_code.cc | 28 ++++--- src/klotski/raw_code/raw_code.h | 59 ++++++------- src/klotski/short_code/convert.cc | 3 +- src/klotski/short_code/serialize.cc | 2 + src/klotski/short_code/serialize_chars.h | 32 +++---- src/klotski/short_code/short_code.cc | 10 ++- src/klotski/short_code/short_code.h | 102 ++++++++++++----------- src/klotski/utils/common.cc | 2 +- test/all_cases.cc | 5 +- test/utils.cc | 2 +- 20 files changed, 202 insertions(+), 163 deletions(-) diff --git a/src/klotski/all_cases/all_cases.cc b/src/klotski/all_cases/all_cases.cc index 4a8bc78..d077b7f 100644 --- a/src/klotski/all_cases/all_cases.cc +++ b/src/klotski/all_cases/all_cases.cc @@ -1,7 +1,7 @@ #include "common.h" #include "all_cases.h" -using namespace klotski; +using klotski::AllCases; /// static variable initialize std::mutex AllCases::building; diff --git a/src/klotski/all_cases/basic_ranges.cc b/src/klotski/all_cases/basic_ranges.cc index d9c7608..8778b2f 100644 --- a/src/klotski/all_cases/basic_ranges.cc +++ b/src/klotski/all_cases/basic_ranges.cc @@ -3,7 +3,7 @@ #include "common.h" #include "basic_ranges.h" -using namespace klotski; +using klotski::BasicRanges; /// static variable initialize std::mutex BasicRanges::building; diff --git a/src/klotski/analyse/analyse.h b/src/klotski/analyse/analyse.h index 8d78be6..844b70a 100644 --- a/src/klotski/analyse/analyse.h +++ b/src/klotski/analyse/analyse.h @@ -11,6 +11,9 @@ // TODO: try double or 4-times size const uint32_t ANY_MAP_RESERVE = 65536; +// TODO: Analyse enter klotski namespace later +using namespace klotski; + class Analyse { public: typedef std::function match_t; diff --git a/src/klotski/common_code/common_code.cc b/src/klotski/common_code/common_code.cc index 14d6771..1a733f0 100644 --- a/src/klotski/common_code/common_code.cc +++ b/src/klotski/common_code/common_code.cc @@ -2,7 +2,7 @@ #include "common.h" #include "common_code.h" -using namespace klotski; +using klotski::CommonCode; uint64_t CommonCode::unwrap() const { return code; // raw uint64_t code @@ -33,11 +33,13 @@ bool CommonCode::operator==(const CommonCode &common_code) const { return this->code == common_code.code; } -std::ostream& operator<<(std::ostream &out, const CommonCode &self) { - char str[10]; - sprintf(str, "%09lX", self.code); - out << str; - return out; +namespace klotski { + std::ostream &operator<<(std::ostream &out, const CommonCode &self) { + char str[10]; + sprintf(str, "%09lX", self.code); + out << str; + return out; + } } bool CommonCode::check(uint64_t common_code) { // whether common code is valid diff --git a/src/klotski/common_code/common_code.h b/src/klotski/common_code/common_code.h index cbf1d5d..9c7cb84 100644 --- a/src/klotski/common_code/common_code.h +++ b/src/klotski/common_code/common_code.h @@ -6,44 +6,47 @@ #include "raw_code.h" #include "short_code.h" -class RawCode; -class ShortCode; - -class CommonCode { -public: - bool valid() const; - static bool check(uint64_t common_code); - - /// Operators of CommonCode - explicit operator uint64_t() const { return code; } - bool operator==(const CommonCode &common_code) const; - 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; - - /// 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_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; - CommonCode() = default; // unsafe initialize -}; +namespace klotski { + /// import for convert interface + class RawCode; + class ShortCode; + + class CommonCode { + public: + bool valid() const; + static bool check(uint64_t common_code); + + /// Operators of CommonCode + explicit operator uint64_t() const { return code; } + bool operator==(const CommonCode &common_code) const; + 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; + + /// 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_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; + CommonCode() = default; // unsafe initialize + }; +} diff --git a/src/klotski/common_code/convert.cc b/src/klotski/common_code/convert.cc index dbd95b1..7675688 100644 --- a/src/klotski/common_code/convert.cc +++ b/src/klotski/common_code/convert.cc @@ -1,5 +1,9 @@ #include "common_code.h" +using klotski::RawCode; +using klotski::ShortCode; +using klotski::CommonCode; + /// CommonCode to RawCode RawCode CommonCode::to_raw_code() const { return RawCode(*this); // convert to raw code diff --git a/src/klotski/common_code/serialize.cc b/src/klotski/common_code/serialize.cc index 9ce269e..141e61a 100644 --- a/src/klotski/common_code/serialize.cc +++ b/src/klotski/common_code/serialize.cc @@ -1,5 +1,7 @@ #include "common_code.h" +using klotski::CommonCode; + inline uint8_t binary_count(uint32_t bin) { // get number of non-zero bits bin -= (bin >> 1) & 0x55555555; bin = (bin & 0x33333333) + ((bin >> 2) & 0x33333333); diff --git a/src/klotski/fast_cal/fast_cal.h b/src/klotski/fast_cal/fast_cal.h index 314416c..f5ee4e6 100644 --- a/src/klotski/fast_cal/fast_cal.h +++ b/src/klotski/fast_cal/fast_cal.h @@ -8,6 +8,9 @@ #include "core.h" #include "raw_code.h" +// TODO: FastCal enter klotski namespace later +using namespace klotski; + // TODO: using prime number const uint32_t FC_MAP_RESERVE = 65536 * 8; diff --git a/src/klotski/ffi/codec.cc b/src/klotski/ffi/codec.cc index d75f61e..b51a51d 100644 --- a/src/klotski/ffi/codec.cc +++ b/src/klotski/ffi/codec.cc @@ -1,6 +1,8 @@ #include "klotski.h" #include "short_code.h" +using klotski::ShortCode; + void short_code_speed_up() { ShortCode::speed_up(ShortCode::NORMAL); } diff --git a/src/klotski/raw_code/convert.cc b/src/klotski/raw_code/convert.cc index ee3ff55..1fffff4 100644 --- a/src/klotski/raw_code/convert.cc +++ b/src/klotski/raw_code/convert.cc @@ -2,7 +2,8 @@ #include "common.h" #include "raw_code.h" -using namespace klotski; +using klotski::RawCode; +using klotski::CommonCode; /// RawCode to CommonCode CommonCode RawCode::to_common_code() const { diff --git a/src/klotski/raw_code/raw_code.cc b/src/klotski/raw_code/raw_code.cc index 719a293..9fb015e 100644 --- a/src/klotski/raw_code/raw_code.cc +++ b/src/klotski/raw_code/raw_code.cc @@ -2,6 +2,8 @@ #include "common.h" #include "raw_code.h" +using klotski::RawCode; + uint64_t RawCode::unwrap() const { return code; // raw uint64_t code } @@ -31,19 +33,21 @@ bool RawCode::operator==(const RawCode &raw_code) const { return this->code == raw_code.code; } -std::ostream& operator<<(std::ostream &out, const RawCode &self) { - char code[16]; - char dump_map[] = { - /// 0x0 1x2 2x1 1x1 2x2 b101 b110 fill - '.', '~', '|', '*', '@', '?', '?', '+' - }; - sprintf(code, "%015lX", self.code); // code length -> 15 - out << code << '\n'; - for (int addr = 0; addr < 60; addr += 3) { - out << dump_map[(self.code >> addr) & 0b111]; - out << " " << &"\n"[(addr & 0b11) != 0b01]; +namespace klotski { + std::ostream &operator<<(std::ostream &out, const RawCode &self) { + char code[16]; + char dump_map[] = { + /// 0x0 1x2 2x1 1x1 2x2 b101 b110 fill + '.', '~', '|', '*', '@', '?', '?', '+' + }; + sprintf(code, "%015lX", self.code); // code length -> 15 + out << code << '\n'; + for (int addr = 0; addr < 60; addr += 3) { + out << dump_map[(self.code >> addr) & 0b111]; + out << " " << &"\n"[(addr & 0b11) != 0b01]; + } + return out; } - return out; } bool RawCode::check(uint64_t raw_code) { // check whether raw code is valid diff --git a/src/klotski/raw_code/raw_code.h b/src/klotski/raw_code/raw_code.h index 459476b..67b1ead 100644 --- a/src/klotski/raw_code/raw_code.h +++ b/src/klotski/raw_code/raw_code.h @@ -39,40 +39,43 @@ #include #include "common_code.h" -class CommonCode; +namespace klotski { + /// import for convert interface + class CommonCode; -class RawCode { -public: - bool valid() const; - static bool check(uint64_t raw_code); + class RawCode { + public: + bool valid() const; + static bool check(uint64_t raw_code); - /// Operators of RawCode - bool operator==(const RawCode &raw_code) const; - explicit operator uint64_t() const { return code; } - friend std::ostream& operator<<(std::ostream &out, const RawCode &self); + /// Operators of RawCode + bool operator==(const RawCode &raw_code) const; + explicit operator uint64_t() const { return code; } + friend std::ostream& operator<<(std::ostream &out, const RawCode &self); - /// Export functions - uint64_t unwrap() const; - CommonCode to_common_code() const; + /// 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); + /// 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); + /// 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); + 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); - // TODO: mirror functions + // TODO: mirror functions -private: - uint64_t code; - RawCode() = default; // unsafe initialize + private: + uint64_t code; + RawCode() = default; // unsafe initialize - static uint64_t compact(uint64_t raw_code); - static uint64_t extract(uint64_t common_code); -}; + static uint64_t compact(uint64_t raw_code); + static uint64_t extract(uint64_t common_code); + }; +} diff --git a/src/klotski/short_code/convert.cc b/src/klotski/short_code/convert.cc index ed6306d..992c74b 100644 --- a/src/klotski/short_code/convert.cc +++ b/src/klotski/short_code/convert.cc @@ -6,7 +6,8 @@ #include "basic_ranges_offset.h" #include "range_prefix_offset.h" -using namespace klotski; +using klotski::ShortCode; +using klotski::CommonCode; /// ShortCode to CommonCode CommonCode ShortCode::to_common_code() const { // convert to common code diff --git a/src/klotski/short_code/serialize.cc b/src/klotski/short_code/serialize.cc index 2e91991..114a1ef 100644 --- a/src/klotski/short_code/serialize.cc +++ b/src/klotski/short_code/serialize.cc @@ -2,6 +2,8 @@ #include "short_code.h" #include "serialize_chars.h" +using klotski::ShortCode; + ShortCode ShortCode::from_string(const std::string &short_code) { return ShortCode(short_code); // convert from string } diff --git a/src/klotski/short_code/serialize_chars.h b/src/klotski/short_code/serialize_chars.h index 9411dac..570fa37 100644 --- a/src/klotski/short_code/serialize_chars.h +++ b/src/klotski/short_code/serialize_chars.h @@ -2,19 +2,21 @@ #include -const int8_t SHORT_CODE_TABLE[32] = { - '1', '2', '3', '4', '5', '6', '7', '8', '9', // skip `0` - 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', // skip `I` - 'J', 'K', // skip `L` - 'M', 'N', // skip `O` - 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', -}; +namespace klotski { + const int8_t SHORT_CODE_TABLE[32] = { + '1', '2', '3', '4', '5', '6', '7', '8', '9', // skip `0` + 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', // skip `I` + 'J', 'K', // skip `L` + 'M', 'N', // skip `O` + 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', + }; -/// `1`(49) ~ `Z`(90) -const int8_t SHORT_CODE_TABLE_REV[42] = { - 0, 1, 2, 3, 4, 5, 6, 7, 8, // `1`(49) ~ `9`(57) - -1, -1, -1, -1, -1, -1, -1, // `:`(58) ~ `@`(64) - 9, 10, 11, 12, 13, 14, 15, 16, -1, 17, // `A`(65) ~ `J`(74) - 18, -1, 19, 20, -1, 21, 22, 23, 24, 25, // `K`(75) ~ `T`(84) - 26, 27, 28, 29, 30, 31, // `U`(85) ~ `Z`(90) -}; + /// `1`(49) ~ `Z`(90) + const int8_t SHORT_CODE_TABLE_REV[42] = { + 0, 1, 2, 3, 4, 5, 6, 7, 8, // `1`(49) ~ `9`(57) + -1, -1, -1, -1, -1, -1, -1, // `:`(58) ~ `@`(64) + 9, 10, 11, 12, 13, 14, 15, 16, -1, 17, // `A`(65) ~ `J`(74) + 18, -1, 19, 20, -1, 21, 22, 23, 24, 25, // `K`(75) ~ `T`(84) + 26, 27, 28, 29, 30, 31, // `U`(85) ~ `Z`(90) + }; +} diff --git a/src/klotski/short_code/short_code.cc b/src/klotski/short_code/short_code.cc index 397a331..2db9273 100644 --- a/src/klotski/short_code/short_code.cc +++ b/src/klotski/short_code/short_code.cc @@ -1,7 +1,7 @@ #include "all_cases.h" #include "short_code.h" -using namespace klotski; +using klotski::ShortCode; uint32_t ShortCode::unwrap() const { return code; // raw uint32_t code @@ -30,9 +30,11 @@ bool ShortCode::operator==(const ShortCode &short_code) const { return this->code == short_code.code; } -std::ostream& operator<<(std::ostream &out, const ShortCode &self) { - out << self.to_string() << "(" << self.code << ")"; // short code info - return out; +namespace klotski { + 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; diff --git a/src/klotski/short_code/short_code.h b/src/klotski/short_code/short_code.h index 01ecde1..9014e49 100644 --- a/src/klotski/short_code/short_code.h +++ b/src/klotski/short_code/short_code.h @@ -5,53 +5,55 @@ #include #include "common_code.h" -const uint32_t SHORT_CODE_LIMIT = 29334498; - -class CommonCode; - -class ShortCode { -public: - enum Mode {NORMAL, FAST}; - - bool valid() const; - static void speed_up(enum Mode mode); - static bool check(uint32_t short_code); - - /// Operators of ShortCode - explicit operator uint32_t() const { return code; } - bool operator==(const ShortCode &short_code) const; - 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); - - 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; - static bool normal_mode_available; - - 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); - static uint32_t tiny_encode(uint64_t common_code); -}; +namespace klotski { + /// import for convert interface + class CommonCode; + + class ShortCode { + public: + enum Mode {NORMAL, FAST}; + + bool valid() const; + static void speed_up(enum Mode mode); + static bool check(uint32_t short_code); + + /// Operators of ShortCode + explicit operator uint32_t() const { return code; } + bool operator==(const ShortCode &short_code) const; + 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); + + 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; + 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); + static uint32_t tiny_encode(uint64_t common_code); + }; +} diff --git a/src/klotski/utils/common.cc b/src/klotski/utils/common.cc index 9535140..9d218c1 100644 --- a/src/klotski/utils/common.cc +++ b/src/klotski/utils/common.cc @@ -1,6 +1,6 @@ #include "common.h" -using namespace klotski; +using klotski::Common; uint32_t Common::range_reverse(uint32_t bin) { // reverse binary every 2-bits bin = ((bin << 16) & 0xFFFF0000) | ((bin >> 16) & 0x0000FFFF); diff --git a/test/all_cases.cc b/test/all_cases.cc index 6802901..5153091 100644 --- a/test/all_cases.cc +++ b/test/all_cases.cc @@ -3,7 +3,10 @@ #include "all_cases.h" #include "gtest/gtest.h" -using namespace klotski; +using klotski::AllCases; +using klotski::BasicRanges; +using klotski::ALL_CASES_SIZE; +using klotski::BASIC_RANGES_SIZE; /// basic ranges constants const char BASIC_RANGES_MD5[] = "6f385dc171e201089ff96bb010b47212"; diff --git a/test/utils.cc b/test/utils.cc index c3edf57..7b19d43 100644 --- a/test/utils.cc +++ b/test/utils.cc @@ -2,7 +2,7 @@ #include "common.h" #include "gtest/gtest.h" -using namespace klotski; +using klotski::Common; TEST(Utils, range_reverse) { EXPECT_EQ(Common::range_reverse((uint32_t)0x00000003), (uint32_t)0xC0000000);