mirror of https://github.com/dnomd343/klotski.git
Dnomd343
2 years ago
8 changed files with 117 additions and 161 deletions
@ -1,70 +0,0 @@ |
|||
#include "all_cases.h" |
|||
#include "short_code.h" |
|||
#include "basic_ranges.h" |
|||
//#include "short_code_mark.h"
|
|||
|
|||
//std::mutex ShortCode::map_building;
|
|||
bool ShortCode::fast_mode_available = false; |
|||
bool ShortCode::normal_mode_available = false; |
|||
|
|||
//std::vector<uint64_t> ShortCode::all_cases_list;
|
|||
//std::unordered_map<uint64_t, uint32_t> ShortCode::all_cases_dict;
|
|||
|
|||
void ShortCode::speed_up(ShortCode::Mode mode) { |
|||
if (fast_mode_available) { |
|||
return; // fast mode already available
|
|||
} |
|||
if (mode == ShortCode::FAST) { // build fast mode data
|
|||
// build_mappings();
|
|||
|
|||
// TODO: confirm AllCases data available
|
|||
AllCases::build(); |
|||
|
|||
} else if (mode == ShortCode::NORMAL && !normal_mode_available) { // build normal mode data
|
|||
BasicRanges::build(); // blocking function
|
|||
normal_mode_available = true; |
|||
} |
|||
} |
|||
|
|||
ShortCode::Mode ShortCode::check_mode() { // ensure speed up enabled and return current mode
|
|||
if (fast_mode_available) { |
|||
return ShortCode::FAST; // fast mode already enabled
|
|||
} |
|||
if (normal_mode_available) { |
|||
return ShortCode::NORMAL; // normal mode already enabled
|
|||
} |
|||
speed_up(ShortCode::Mode::NORMAL); // without initialized -> enter normal mode
|
|||
return ShortCode::Mode::NORMAL; // use normal mode
|
|||
} |
|||
|
|||
/// ensure that fast_mode_available == false
|
|||
//void ShortCode::build_mappings() { // build fast search mappings
|
|||
// if (map_building.try_lock()) { // lock success -> start building
|
|||
// for (int head = 0; head < 16; ++head) {
|
|||
// uint64_t prefix = (uint64_t)head << 32;
|
|||
// for (const auto &range : AllCases::fetch()[head]) { // blocking function
|
|||
// all_cases_list.emplace_back(prefix | range); // short_code -> common_code
|
|||
// }
|
|||
// }
|
|||
// for (int index = 0; index < all_cases_list.size(); ++index) {
|
|||
// all_cases_dict[all_cases_list[index]] = index; // common_code -> short_code
|
|||
// }
|
|||
// fast_mode_available = true; // set available flag
|
|||
// } else { // another thread building
|
|||
// map_building.lock(); // blocking waiting
|
|||
// }
|
|||
// map_building.unlock();
|
|||
//}
|
|||
|
|||
//uint32_t ShortCode::fast_encode_legacy(uint64_t common_code) {
|
|||
// return all_cases_dict[common_code];
|
|||
//}
|
|||
|
|||
#include <algorithm> |
|||
|
|||
#include "basic_ranges_offset.h" |
|||
#include "range_prefix_offset.h" |
|||
|
|||
#include <iostream> |
|||
#include "common.h" |
|||
|
@ -0,0 +1,44 @@ |
|||
#include <stdexcept> |
|||
#include "short_code.h" |
|||
#include "serialize_chars.h" |
|||
|
|||
ShortCode ShortCode::from_string(const std::string &short_code) { |
|||
return ShortCode(short_code); // convert from string
|
|||
} |
|||
|
|||
std::string ShortCode::to_string() const { // encode as 5-bits string
|
|||
uint32_t short_code = code; |
|||
std::string result(5, '\0'); // short code length 5
|
|||
for (int n = 0; n < 5; ++n) { |
|||
uint8_t bit = short_code % 32; |
|||
short_code = (short_code - bit) / 32; |
|||
result[4 - n] = SHORT_CODE_TABLE[bit]; |
|||
} |
|||
return result; |
|||
} |
|||
|
|||
ShortCode::ShortCode(const std::string &short_code) { // 5-bits string decode
|
|||
if (short_code.length() != 5) { // check string length
|
|||
throw std::invalid_argument("short code format error"); |
|||
} |
|||
|
|||
uint64_t result = 0; |
|||
for (auto bit : short_code) { |
|||
result *= 32; |
|||
if (bit >= 'a' && bit <= 'z') { |
|||
bit -= 32; // convert to uppercase
|
|||
} |
|||
if (bit >= '1' && bit <= 'Z') { |
|||
result += (bit = SHORT_CODE_TABLE_REV[bit - 49]); // table convert
|
|||
if (bit != -1) { |
|||
continue; // pass check
|
|||
} |
|||
} |
|||
throw std::invalid_argument("short code format error"); // unknown characters
|
|||
} |
|||
|
|||
if (!ShortCode::check(result)) { // check converted short code
|
|||
throw std::invalid_argument("invalid short code"); |
|||
} |
|||
code = result; |
|||
} |
Loading…
Reference in new issue