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. 6
      src/klotski/common_code/common_code.cc
  5. 15
      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. 6
      src/klotski/raw_code/raw_code.cc
  12. 13
      src/klotski/raw_code/raw_code.h
  13. 3
      src/klotski/short_code/convert.cc
  14. 2
      src/klotski/short_code/serialize.cc
  15. 12
      src/klotski/short_code/serialize_chars.h
  16. 6
      src/klotski/short_code/short_code.cc
  17. 16
      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;

6
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) {
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

15
src/klotski/common_code/common_code.h

@ -6,11 +6,13 @@
#include "raw_code.h"
#include "short_code.h"
class RawCode;
class ShortCode;
namespace klotski {
/// import for convert interface
class RawCode;
class ShortCode;
class CommonCode {
public:
class CommonCode {
public:
bool valid() const;
static bool check(uint64_t common_code);
@ -43,7 +45,8 @@ public:
static CommonCode from_short_code(const ShortCode &short_code);
static CommonCode from_short_code(const std::string &short_code);
private:
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 {

6
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,7 +33,8 @@ bool RawCode::operator==(const RawCode &raw_code) const {
return this->code == raw_code.code;
}
std::ostream& operator<<(std::ostream &out, const RawCode &self) {
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
@ -44,6 +47,7 @@ std::ostream& operator<<(std::ostream &out, const RawCode &self) {
out << " " << &"\n"[(addr & 0b11) != 0b01];
}
return out;
}
}
bool RawCode::check(uint64_t raw_code) { // check whether raw code is valid

13
src/klotski/raw_code/raw_code.h

@ -39,10 +39,12 @@
#include <ostream>
#include "common_code.h"
class CommonCode;
namespace klotski {
/// import for convert interface
class CommonCode;
class RawCode {
public:
class RawCode {
public:
bool valid() const;
static bool check(uint64_t raw_code);
@ -69,10 +71,11 @@ public:
// TODO: mirror functions
private:
private:
uint64_t code;
RawCode() = default; // unsafe initialize
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
}

12
src/klotski/short_code/serialize_chars.h

@ -2,19 +2,21 @@
#include <cstdint>
const int8_t SHORT_CODE_TABLE[32] = {
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] = {
/// `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)
};
};
}

6
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) {
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;

16
src/klotski/short_code/short_code.h

@ -5,12 +5,12 @@
#include <ostream>
#include "common_code.h"
const uint32_t SHORT_CODE_LIMIT = 29334498;
namespace klotski {
/// import for convert interface
class CommonCode;
class CommonCode;
class ShortCode {
public:
class ShortCode {
public:
enum Mode {NORMAL, FAST};
bool valid() const;
@ -44,14 +44,16 @@ public:
static ShortCode from_common_code(const CommonCode &common_code);
static ShortCode from_common_code(const std::string &common_code);
private:
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