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) |
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) |
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 "short_code.h" |
||||
#include "basic_ranges.h" |
|
||||
|
|
||||
std::mutex ShortCode::map_building; |
uint32_t ShortCode::unwrap() const { |
||||
bool ShortCode::fast_mode_available = false; |
return code; // get raw uint32_t code
|
||||
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) { |
bool ShortCode::check(uint32_t short_code) { |
||||
if (fast_mode_available) { |
return short_code < ShortCode::SHORT_CODE_LIMIT; // 0 ~ (SHORT_CODE_LIMIT - 1)
|
||||
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
|
ShortCode::ShortCode(uint32_t short_code) { |
||||
if (fast_mode_available) { |
if (!ShortCode::check(short_code)) { // check input short code
|
||||
return ShortCode::FAST; // fast mode already enabled
|
throw std::invalid_argument("invalid short code"); |
||||
} |
} |
||||
if (normal_mode_available) { |
code = short_code; |
||||
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
|
#include <iostream> |
||||
void ShortCode::build_mappings() { // build fast search mappings
|
|
||||
if (map_building.try_lock()) { // lock success -> start building
|
std::string ShortCode::to_string() const { |
||||
for (int head = 0; head < 16; ++head) { |
|
||||
uint64_t prefix = (uint64_t)head << 32; |
// this->code ==> std::string
|
||||
for (const auto &range : (*AllCases::fetch())[head]) { // blocking function
|
|
||||
all_cases_list.emplace_back(prefix | range); // short_code -> common_code
|
std::cout << "short code: " << code << std::endl; |
||||
} |
|
||||
} |
return ""; |
||||
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(); |
|
||||
} |
} |
||||
|
Loading…
Reference in new issue