|
@ -3,10 +3,8 @@ |
|
|
#include "basic_ranges.h" |
|
|
#include "basic_ranges.h" |
|
|
|
|
|
|
|
|
std::mutex ShortCode::map_building; |
|
|
std::mutex ShortCode::map_building; |
|
|
|
|
|
|
|
|
bool ShortCode::fast_mode_available = false; |
|
|
bool ShortCode::fast_mode_available = false; |
|
|
bool ShortCode::normal_mode_available = false; |
|
|
bool ShortCode::normal_mode_available = false; |
|
|
|
|
|
|
|
|
std::vector<uint64_t> ShortCode::all_cases_list; |
|
|
std::vector<uint64_t> ShortCode::all_cases_list; |
|
|
std::unordered_map<uint64_t, uint32_t> ShortCode::all_cases_dict; |
|
|
std::unordered_map<uint64_t, uint32_t> ShortCode::all_cases_dict; |
|
|
|
|
|
|
|
@ -15,27 +13,28 @@ void ShortCode::speed_up(ShortCode::Mode mode) { |
|
|
return; // fast mode already available
|
|
|
return; // fast mode already available
|
|
|
} |
|
|
} |
|
|
if (mode == ShortCode::FAST) { // build fast mode data
|
|
|
if (mode == ShortCode::FAST) { // build fast mode data
|
|
|
|
|
|
|
|
|
// build_mappings
|
|
|
|
|
|
build_mappings(); |
|
|
build_mappings(); |
|
|
|
|
|
|
|
|
} else if (mode == ShortCode::NORMAL && !normal_mode_available) { // build normal mode data
|
|
|
} else if (mode == ShortCode::NORMAL && !normal_mode_available) { // build normal mode data
|
|
|
BasicRanges::build_basic_ranges(); // blocking function
|
|
|
BasicRanges::build_basic_ranges(); // blocking function
|
|
|
normal_mode_available = true; |
|
|
normal_mode_available = true; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
void ShortCode::build_mappings() { // build fast search mappings
|
|
|
ShortCode::Mode ShortCode::check_mode() { // ensure speed up enabled and return current mode
|
|
|
|
|
|
|
|
|
if (fast_mode_available) { |
|
|
if (fast_mode_available) { |
|
|
return; // all cases map already built
|
|
|
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
|
|
|
if (map_building.try_lock()) { // lock success -> start building
|
|
|
|
|
|
|
|
|
AllCases::build_all_cases(); // blocking function
|
|
|
AllCases::build_all_cases(); // blocking function
|
|
|
|
|
|
|
|
|
for (int head = 0; head < 16; ++head) { |
|
|
for (int head = 0; head < 16; ++head) { |
|
|
uint64_t prefix = (uint64_t)head << 32; |
|
|
uint64_t prefix = (uint64_t)head << 32; |
|
|
for (const auto &range : (*AllCases::get_all_cases())[head]) { |
|
|
for (const auto &range : (*AllCases::get_all_cases())[head]) { |
|
@ -45,11 +44,9 @@ void ShortCode::build_mappings() { // build fast search mappings |
|
|
for (int index = 0; index < all_cases_list.size(); ++index) { |
|
|
for (int index = 0; index < all_cases_list.size(); ++index) { |
|
|
all_cases_dict[all_cases_list[index]] = index; // common_code -> short_code
|
|
|
all_cases_dict[all_cases_list[index]] = index; // common_code -> short_code
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
fast_mode_available = true; // set available flag
|
|
|
fast_mode_available = true; // set available flag
|
|
|
} else { // another thread building
|
|
|
} else { // another thread building
|
|
|
map_building.lock(); // blocking waiting
|
|
|
map_building.lock(); // blocking waiting
|
|
|
} |
|
|
} |
|
|
map_building.unlock(); |
|
|
map_building.unlock(); |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|