Browse Source

chore: code modules using namespace klotski

master
Dnomd343 2 years ago
parent
commit
3f0569d6f9
  1. 2
      src/klotski/all_cases/all_cases.cc
  2. 2
      src/klotski/all_cases/basic_ranges.cc
  3. 3
      src/klotski/analyse/analyse.h
  4. 14
      src/klotski/common_code/common_code.cc
  5. 85
      src/klotski/common_code/common_code.h
  6. 4
      src/klotski/common_code/convert.cc
  7. 2
      src/klotski/common_code/serialize.cc
  8. 3
      src/klotski/fast_cal/fast_cal.h
  9. 2
      src/klotski/ffi/codec.cc
  10. 3
      src/klotski/raw_code/convert.cc
  11. 28
      src/klotski/raw_code/raw_code.cc
  12. 59
      src/klotski/raw_code/raw_code.h
  13. 3
      src/klotski/short_code/convert.cc
  14. 2
      src/klotski/short_code/serialize.cc
  15. 32
      src/klotski/short_code/serialize_chars.h
  16. 10
      src/klotski/short_code/short_code.cc
  17. 102
      src/klotski/short_code/short_code.h
  18. 2
      src/klotski/utils/common.cc
  19. 5
      test/all_cases.cc
  20. 2
      test/utils.cc

2
src/klotski/all_cases/all_cases.cc

@ -1,7 +1,7 @@
#include "common.h" #include "common.h"
#include "all_cases.h" #include "all_cases.h"
using namespace klotski; using klotski::AllCases;
/// static variable initialize /// static variable initialize
std::mutex AllCases::building; std::mutex AllCases::building;

2
src/klotski/all_cases/basic_ranges.cc

@ -3,7 +3,7 @@
#include "common.h" #include "common.h"
#include "basic_ranges.h" #include "basic_ranges.h"
using namespace klotski; using klotski::BasicRanges;
/// static variable initialize /// static variable initialize
std::mutex BasicRanges::building; std::mutex BasicRanges::building;

3
src/klotski/analyse/analyse.h

@ -11,6 +11,9 @@
// TODO: try double or 4-times size // TODO: try double or 4-times size
const uint32_t ANY_MAP_RESERVE = 65536; const uint32_t ANY_MAP_RESERVE = 65536;
// TODO: Analyse enter klotski namespace later
using namespace klotski;
class Analyse { class Analyse {
public: public:
typedef std::function<bool(uint64_t)> match_t; typedef std::function<bool(uint64_t)> match_t;

14
src/klotski/common_code/common_code.cc

@ -2,7 +2,7 @@
#include "common.h" #include "common.h"
#include "common_code.h" #include "common_code.h"
using namespace klotski; using klotski::CommonCode;
uint64_t CommonCode::unwrap() const { uint64_t CommonCode::unwrap() const {
return code; // raw uint64_t code return code; // raw uint64_t code
@ -33,11 +33,13 @@ bool CommonCode::operator==(const CommonCode &common_code) const {
return this->code == common_code.code; return this->code == common_code.code;
} }
std::ostream& operator<<(std::ostream &out, const CommonCode &self) { namespace klotski {
char str[10]; std::ostream &operator<<(std::ostream &out, const CommonCode &self) {
sprintf(str, "%09lX", self.code); char str[10];
out << str; sprintf(str, "%09lX", self.code);
return out; out << str;
return out;
}
} }
bool CommonCode::check(uint64_t common_code) { // whether common code is valid bool CommonCode::check(uint64_t common_code) { // whether common code is valid

85
src/klotski/common_code/common_code.h

@ -6,44 +6,47 @@
#include "raw_code.h" #include "raw_code.h"
#include "short_code.h" #include "short_code.h"
class RawCode; namespace klotski {
class ShortCode; /// import for convert interface
class RawCode;
class CommonCode { class ShortCode;
public:
bool valid() const; class CommonCode {
static bool check(uint64_t common_code); public:
bool valid() const;
/// Operators of CommonCode static bool check(uint64_t common_code);
explicit operator uint64_t() const { return code; }
bool operator==(const CommonCode &common_code) const; /// Operators of CommonCode
friend std::ostream& operator<<(std::ostream &out, const CommonCode &self); explicit operator uint64_t() const { return code; }
bool operator==(const CommonCode &common_code) const;
/// Export functions friend std::ostream& operator<<(std::ostream &out, const CommonCode &self);
uint64_t unwrap() const;
RawCode to_raw_code() const; /// Export functions
ShortCode to_short_code() const; uint64_t unwrap() const;
std::string to_string(bool shorten = false) const; RawCode to_raw_code() const;
ShortCode to_short_code() const;
/// CommonCode constructors std::string to_string(bool shorten = false) const;
explicit CommonCode(uint64_t common_code);
explicit CommonCode(const RawCode &raw_code); /// CommonCode constructors
explicit CommonCode(const ShortCode &short_code); explicit CommonCode(uint64_t common_code);
explicit CommonCode(const std::string &common_code); explicit CommonCode(const RawCode &raw_code);
explicit CommonCode(const ShortCode &short_code);
/// Rust-style initialization explicit CommonCode(const std::string &common_code);
static CommonCode create(uint64_t common_code);
static CommonCode unsafe_create(uint64_t common_code); /// Rust-style initialization
static CommonCode from_string(const std::string &common_code); static CommonCode create(uint64_t common_code);
static CommonCode unsafe_create(uint64_t common_code);
static CommonCode from_raw_code(uint64_t raw_code); static CommonCode from_string(const std::string &common_code);
static CommonCode from_raw_code(const RawCode &raw_code);
static CommonCode from_raw_code(uint64_t raw_code);
static CommonCode from_short_code(uint32_t short_code); static CommonCode from_raw_code(const RawCode &raw_code);
static CommonCode from_short_code(const ShortCode &short_code);
static CommonCode from_short_code(const std::string &short_code); static CommonCode from_short_code(uint32_t short_code);
static CommonCode from_short_code(const ShortCode &short_code);
private: static CommonCode from_short_code(const std::string &short_code);
uint64_t code;
CommonCode() = default; // unsafe initialize private:
}; uint64_t code;
CommonCode() = default; // unsafe initialize
};
}

4
src/klotski/common_code/convert.cc

@ -1,5 +1,9 @@
#include "common_code.h" #include "common_code.h"
using klotski::RawCode;
using klotski::ShortCode;
using klotski::CommonCode;
/// CommonCode to RawCode /// CommonCode to RawCode
RawCode CommonCode::to_raw_code() const { RawCode CommonCode::to_raw_code() const {
return RawCode(*this); // convert to raw code return RawCode(*this); // convert to raw code

2
src/klotski/common_code/serialize.cc

@ -1,5 +1,7 @@
#include "common_code.h" #include "common_code.h"
using klotski::CommonCode;
inline uint8_t binary_count(uint32_t bin) { // get number of non-zero bits inline uint8_t binary_count(uint32_t bin) { // get number of non-zero bits
bin -= (bin >> 1) & 0x55555555; bin -= (bin >> 1) & 0x55555555;
bin = (bin & 0x33333333) + ((bin >> 2) & 0x33333333); bin = (bin & 0x33333333) + ((bin >> 2) & 0x33333333);

3
src/klotski/fast_cal/fast_cal.h

@ -8,6 +8,9 @@
#include "core.h" #include "core.h"
#include "raw_code.h" #include "raw_code.h"
// TODO: FastCal enter klotski namespace later
using namespace klotski;
// TODO: using prime number // TODO: using prime number
const uint32_t FC_MAP_RESERVE = 65536 * 8; const uint32_t FC_MAP_RESERVE = 65536 * 8;

2
src/klotski/ffi/codec.cc

@ -1,6 +1,8 @@
#include "klotski.h" #include "klotski.h"
#include "short_code.h" #include "short_code.h"
using klotski::ShortCode;
void short_code_speed_up() { void short_code_speed_up() {
ShortCode::speed_up(ShortCode::NORMAL); ShortCode::speed_up(ShortCode::NORMAL);
} }

3
src/klotski/raw_code/convert.cc

@ -2,7 +2,8 @@
#include "common.h" #include "common.h"
#include "raw_code.h" #include "raw_code.h"
using namespace klotski; using klotski::RawCode;
using klotski::CommonCode;
/// RawCode to CommonCode /// RawCode to CommonCode
CommonCode RawCode::to_common_code() const { CommonCode RawCode::to_common_code() const {

28
src/klotski/raw_code/raw_code.cc

@ -2,6 +2,8 @@
#include "common.h" #include "common.h"
#include "raw_code.h" #include "raw_code.h"
using klotski::RawCode;
uint64_t RawCode::unwrap() const { uint64_t RawCode::unwrap() const {
return code; // raw uint64_t code return code; // raw uint64_t code
} }
@ -31,19 +33,21 @@ bool RawCode::operator==(const RawCode &raw_code) const {
return this->code == raw_code.code; return this->code == raw_code.code;
} }
std::ostream& operator<<(std::ostream &out, const RawCode &self) { namespace klotski {
char code[16]; std::ostream &operator<<(std::ostream &out, const RawCode &self) {
char dump_map[] = { char code[16];
/// 0x0 1x2 2x1 1x1 2x2 b101 b110 fill char dump_map[] = {
'.', '~', '|', '*', '@', '?', '?', '+' /// 0x0 1x2 2x1 1x1 2x2 b101 b110 fill
}; '.', '~', '|', '*', '@', '?', '?', '+'
sprintf(code, "%015lX", self.code); // code length -> 15 };
out << code << '\n'; sprintf(code, "%015lX", self.code); // code length -> 15
for (int addr = 0; addr < 60; addr += 3) { out << code << '\n';
out << dump_map[(self.code >> addr) & 0b111]; for (int addr = 0; addr < 60; addr += 3) {
out << " " << &"\n"[(addr & 0b11) != 0b01]; 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 bool RawCode::check(uint64_t raw_code) { // check whether raw code is valid

59
src/klotski/raw_code/raw_code.h

@ -39,40 +39,43 @@
#include <ostream> #include <ostream>
#include "common_code.h" #include "common_code.h"
class CommonCode; namespace klotski {
/// import for convert interface
class CommonCode;
class RawCode { class RawCode {
public: public:
bool valid() const; bool valid() const;
static bool check(uint64_t raw_code); static bool check(uint64_t raw_code);
/// Operators of RawCode /// Operators of RawCode
bool operator==(const RawCode &raw_code) const; bool operator==(const RawCode &raw_code) const;
explicit operator uint64_t() const { return code; } explicit operator uint64_t() const { return code; }
friend std::ostream& operator<<(std::ostream &out, const RawCode &self); friend std::ostream& operator<<(std::ostream &out, const RawCode &self);
/// Export functions /// Export functions
uint64_t unwrap() const; uint64_t unwrap() const;
CommonCode to_common_code() const; CommonCode to_common_code() const;
/// RawCode constructors /// RawCode constructors
explicit RawCode(uint64_t raw_code); explicit RawCode(uint64_t raw_code);
explicit RawCode(const CommonCode &common_code); explicit RawCode(const CommonCode &common_code);
/// Rust-style initialization /// Rust-style initialization
static RawCode create(uint64_t raw_code); static RawCode create(uint64_t raw_code);
static RawCode unsafe_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(uint64_t common_code);
static RawCode from_common_code(const CommonCode &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(const std::string &common_code);
// TODO: mirror functions // TODO: mirror functions
private: private:
uint64_t code; uint64_t code;
RawCode() = default; // unsafe initialize RawCode() = default; // unsafe initialize
static uint64_t compact(uint64_t raw_code); static uint64_t compact(uint64_t raw_code);
static uint64_t extract(uint64_t common_code); static uint64_t extract(uint64_t common_code);
}; };
}

3
src/klotski/short_code/convert.cc

@ -6,7 +6,8 @@
#include "basic_ranges_offset.h" #include "basic_ranges_offset.h"
#include "range_prefix_offset.h" #include "range_prefix_offset.h"
using namespace klotski; using klotski::ShortCode;
using klotski::CommonCode;
/// ShortCode to CommonCode /// ShortCode to CommonCode
CommonCode ShortCode::to_common_code() const { // convert to common code CommonCode ShortCode::to_common_code() const { // convert to common code

2
src/klotski/short_code/serialize.cc

@ -2,6 +2,8 @@
#include "short_code.h" #include "short_code.h"
#include "serialize_chars.h" #include "serialize_chars.h"
using klotski::ShortCode;
ShortCode ShortCode::from_string(const std::string &short_code) { ShortCode ShortCode::from_string(const std::string &short_code) {
return ShortCode(short_code); // convert from string return ShortCode(short_code); // convert from string
} }

32
src/klotski/short_code/serialize_chars.h

@ -2,19 +2,21 @@
#include <cstdint> #include <cstdint>
const int8_t SHORT_CODE_TABLE[32] = { namespace klotski {
'1', '2', '3', '4', '5', '6', '7', '8', '9', // skip `0` const int8_t SHORT_CODE_TABLE[32] = {
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', // skip `I` '1', '2', '3', '4', '5', '6', '7', '8', '9', // skip `0`
'J', 'K', // skip `L` 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', // skip `I`
'M', 'N', // skip `O` 'J', 'K', // skip `L`
'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'M', 'N', // skip `O`
}; 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
};
/// `1`(49) ~ `Z`(90) /// `1`(49) ~ `Z`(90)
const int8_t SHORT_CODE_TABLE_REV[42] = { const int8_t SHORT_CODE_TABLE_REV[42] = {
0, 1, 2, 3, 4, 5, 6, 7, 8, // `1`(49) ~ `9`(57) 0, 1, 2, 3, 4, 5, 6, 7, 8, // `1`(49) ~ `9`(57)
-1, -1, -1, -1, -1, -1, -1, // `:`(58) ~ `@`(64) -1, -1, -1, -1, -1, -1, -1, // `:`(58) ~ `@`(64)
9, 10, 11, 12, 13, 14, 15, 16, -1, 17, // `A`(65) ~ `J`(74) 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) 18, -1, 19, 20, -1, 21, 22, 23, 24, 25, // `K`(75) ~ `T`(84)
26, 27, 28, 29, 30, 31, // `U`(85) ~ `Z`(90) 26, 27, 28, 29, 30, 31, // `U`(85) ~ `Z`(90)
}; };
}

10
src/klotski/short_code/short_code.cc

@ -1,7 +1,7 @@
#include "all_cases.h" #include "all_cases.h"
#include "short_code.h" #include "short_code.h"
using namespace klotski; using klotski::ShortCode;
uint32_t ShortCode::unwrap() const { uint32_t ShortCode::unwrap() const {
return code; // raw uint32_t code return code; // raw uint32_t code
@ -30,9 +30,11 @@ bool ShortCode::operator==(const ShortCode &short_code) const {
return this->code == short_code.code; return this->code == short_code.code;
} }
std::ostream& operator<<(std::ostream &out, const ShortCode &self) { namespace klotski {
out << self.to_string() << "(" << self.code << ")"; // short code info std::ostream &operator<<(std::ostream &out, const ShortCode &self) {
return out; out << self.to_string() << "(" << self.code << ")"; // short code info
return out;
}
} }
bool ShortCode::fast_mode_available = false; bool ShortCode::fast_mode_available = false;

102
src/klotski/short_code/short_code.h

@ -5,53 +5,55 @@
#include <ostream> #include <ostream>
#include "common_code.h" #include "common_code.h"
const uint32_t SHORT_CODE_LIMIT = 29334498; namespace klotski {
/// import for convert interface
class CommonCode; class CommonCode;
class ShortCode { class ShortCode {
public: public:
enum Mode {NORMAL, FAST}; enum Mode {NORMAL, FAST};
bool valid() const; bool valid() const;
static void speed_up(enum Mode mode); static void speed_up(enum Mode mode);
static bool check(uint32_t short_code); static bool check(uint32_t short_code);
/// Operators of ShortCode /// Operators of ShortCode
explicit operator uint32_t() const { return code; } explicit operator uint32_t() const { return code; }
bool operator==(const ShortCode &short_code) const; bool operator==(const ShortCode &short_code) const;
friend std::ostream& operator<<(std::ostream &out, const ShortCode &self); friend std::ostream& operator<<(std::ostream &out, const ShortCode &self);
/// Export functions /// Export functions
uint32_t unwrap() const; uint32_t unwrap() const;
std::string to_string() const; std::string to_string() const;
CommonCode to_common_code() const; CommonCode to_common_code() const;
/// ShortCode constructors /// ShortCode constructors
explicit ShortCode(uint32_t short_code); explicit ShortCode(uint32_t short_code);
explicit ShortCode(const std::string &short_code); explicit ShortCode(const std::string &short_code);
explicit ShortCode(const CommonCode &common_code); explicit ShortCode(const CommonCode &common_code);
ShortCode(uint32_t short_code, enum Mode mode) : ShortCode(short_code) { speed_up(mode); } 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 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); } ShortCode(const CommonCode &common_code, enum Mode mode) : ShortCode(common_code) { speed_up(mode); }
/// Rust-style initialization /// Rust-style initialization
static ShortCode create(uint32_t short_code); static ShortCode create(uint32_t short_code);
static ShortCode from_string(const std::string &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(uint64_t common_code);
static ShortCode from_common_code(const CommonCode &common_code); static ShortCode from_common_code(const CommonCode &common_code);
static ShortCode from_common_code(const std::string &common_code); static ShortCode from_common_code(const std::string &common_code);
private: private:
uint32_t code; uint32_t code;
static bool fast_mode_available; static bool fast_mode_available;
static bool normal_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 enum Mode mode();
static uint32_t fast_encode(uint64_t common_code); static uint64_t fast_decode(uint32_t short_code);
static uint64_t tiny_decode(uint32_t short_code); static uint32_t fast_encode(uint64_t common_code);
static uint32_t tiny_encode(uint64_t common_code); static uint64_t tiny_decode(uint32_t short_code);
}; static uint32_t tiny_encode(uint64_t common_code);
};
}

2
src/klotski/utils/common.cc

@ -1,6 +1,6 @@
#include "common.h" #include "common.h"
using namespace klotski; using klotski::Common;
uint32_t Common::range_reverse(uint32_t bin) { // reverse binary every 2-bits uint32_t Common::range_reverse(uint32_t bin) { // reverse binary every 2-bits
bin = ((bin << 16) & 0xFFFF0000) | ((bin >> 16) & 0x0000FFFF); bin = ((bin << 16) & 0xFFFF0000) | ((bin >> 16) & 0x0000FFFF);

5
test/all_cases.cc

@ -3,7 +3,10 @@
#include "all_cases.h" #include "all_cases.h"
#include "gtest/gtest.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 /// basic ranges constants
const char BASIC_RANGES_MD5[] = "6f385dc171e201089ff96bb010b47212"; const char BASIC_RANGES_MD5[] = "6f385dc171e201089ff96bb010b47212";

2
test/utils.cc

@ -2,7 +2,7 @@
#include "common.h" #include "common.h"
#include "gtest/gtest.h" #include "gtest/gtest.h"
using namespace klotski; using klotski::Common;
TEST(Utils, range_reverse) { TEST(Utils, range_reverse) {
EXPECT_EQ(Common::range_reverse((uint32_t)0x00000003), (uint32_t)0xC0000000); EXPECT_EQ(Common::range_reverse((uint32_t)0x00000003), (uint32_t)0xC0000000);

Loading…
Cancel
Save