mirror of https://github.com/dnomd343/klotski.git
Dnomd343
2 years ago
6 changed files with 107 additions and 57 deletions
@ -1,4 +1,4 @@ |
|||
cmake_minimum_required(VERSION 3.0) |
|||
|
|||
add_library(short_code short_code.cc) |
|||
add_library(short_code data_loader.cc short_code.cc) |
|||
target_link_libraries(short_code all_cases) |
|||
|
@ -0,0 +1,52 @@ |
|||
#include "all_cases.h" |
|||
#include "short_code.h" |
|||
#include "basic_ranges.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(); |
|||
} 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(); |
|||
} |
@ -1,51 +1,27 @@ |
|||
#include "all_cases.h" |
|||
#include "short_code.h" |
|||
#include "basic_ranges.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; |
|||
uint32_t ShortCode::unwrap() const { |
|||
return code; // get raw uint32_t code
|
|||
} |
|||
|
|||
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(); |
|||
} else if (mode == ShortCode::NORMAL && !normal_mode_available) { // build normal mode data
|
|||
BasicRanges::build(); // blocking function
|
|||
normal_mode_available = true; |
|||
} |
|||
bool ShortCode::check(uint32_t short_code) { |
|||
return short_code < ShortCode::SHORT_CODE_LIMIT; // 0 ~ (SHORT_CODE_LIMIT - 1)
|
|||
} |
|||
|
|||
ShortCode::Mode ShortCode::check_mode() { // ensure speed up enabled and return current mode
|
|||
if (fast_mode_available) { |
|||
return ShortCode::FAST; // fast mode already enabled
|
|||
ShortCode::ShortCode(uint32_t short_code) { |
|||
if (!ShortCode::check(short_code)) { // check input short code
|
|||
throw std::invalid_argument("invalid short code"); |
|||
} |
|||
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
|
|||
code = short_code; |
|||
} |
|||
|
|||
/// 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(); |
|||
#include <iostream> |
|||
|
|||
std::string ShortCode::to_string() const { |
|||
|
|||
// this->code ==> std::string
|
|||
|
|||
std::cout << "short code: " << code << std::endl; |
|||
|
|||
return ""; |
|||
} |
|||
|
Loading…
Reference in new issue