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 "all_cases.h"
using namespace klotski;
using klotski::AllCases;
/// static variable initialize
std::mutex AllCases::building;

2
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;

3
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<bool(uint64_t)> match_t;

14
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

85
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
};
}

4
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

2
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);

3
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;

2
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);
}

3
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 {

28
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

59
src/klotski/raw_code/raw_code.h

@ -39,40 +39,43 @@
#include <ostream>
#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);
};
}

3
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

2
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
}

32
src/klotski/short_code/serialize_chars.h

@ -2,19 +2,21 @@
#include <cstdint>
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)
};
}

10
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;

102
src/klotski/short_code/short_code.h

@ -5,53 +5,55 @@
#include <ostream>
#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);
};
}

2
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);

5
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";

2
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);

Loading…
Cancel
Save