From 6dc2d7eef8eb52ba1bf1f5c898c802b7b11a55b4 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Sun, 8 Jan 2023 19:41:17 +0800 Subject: [PATCH] update: check mode for ShortCode --- src/main.cc | 68 +++++++++++++++++++----------------- src/short_code/short_code.cc | 23 ++++++------ src/short_code/short_code.h | 13 +++---- 3 files changed, 50 insertions(+), 54 deletions(-) diff --git a/src/main.cc b/src/main.cc index 1163c6c..bec7a0c 100644 --- a/src/main.cc +++ b/src/main.cc @@ -34,21 +34,21 @@ void get_status() { int main() { - get_status(); - BasicRanges::build_basic_ranges(); - get_status(); - AllCases::build_all_cases(); - get_status(); - - for (auto const &all_case : *AllCases::get_all_cases()) { - std::cout << " " << all_case.size() << std::endl; - } - - std::cout << BasicRanges::get_basic_ranges() << std::endl; - std::cout << AllCases::get_basic_ranges() << std::endl; - - printf("%p\n", BasicRanges::get_status); - printf("%p\n", AllCases::get_status); +// get_status(); +// BasicRanges::build_basic_ranges(); +// get_status(); +// AllCases::build_all_cases(); +// get_status(); +// +// for (auto const &all_case : *AllCases::get_all_cases()) { +// std::cout << " " << all_case.size() << std::endl; +// } +// +// std::cout << BasicRanges::get_basic_ranges() << std::endl; +// std::cout << AllCases::get_basic_ranges() << std::endl; +// +// printf("%p\n", BasicRanges::get_status); +// printf("%p\n", AllCases::get_status); // std::cout << CommonCode::check(0x123456789) << std::endl; @@ -67,23 +67,27 @@ int main() { // std::cout << CommonCode(0x1A9BF0C00).to_string() << std::endl; -// std::cout << "start NORMAL speed up" << std::endl; -//// ShortCode::speed_up(ShortCode::NORMAL); -// std::thread t1(ShortCode::speed_up, ShortCode::NORMAL); -// std::thread t2(ShortCode::speed_up, ShortCode::NORMAL); -// t1.join(); -// t2.join(); -// std::cout << "NORMAL speed up complete" << std::endl; -// -// std::cout << "start FAST speed up" << std::endl; -//// ShortCode::speed_up(ShortCode::FAST); -// std::thread t3(ShortCode::speed_up, ShortCode::FAST); -// std::thread t4(ShortCode::speed_up, ShortCode::FAST); -// t3.join(); -// t4.join(); -// std::cout << "FAST speed up complete" << std::endl; -// -// std::cout << ShortCode::all_cases_list.size() << std::endl; +// std::cout << ShortCode::check_mode() << std::endl; + + std::cout << "start NORMAL speed up" << std::endl; +// ShortCode::speed_up(ShortCode::NORMAL); + std::thread t1(ShortCode::speed_up, ShortCode::NORMAL); + std::thread t2(ShortCode::speed_up, ShortCode::NORMAL); + t1.join(); + t2.join(); + std::cout << "NORMAL speed up complete" << std::endl; + + std::cout << ShortCode::check_mode() << std::endl; + + std::cout << "start FAST speed up" << std::endl; +// ShortCode::speed_up(ShortCode::FAST); + std::thread t3(ShortCode::speed_up, ShortCode::FAST); + std::thread t4(ShortCode::speed_up, ShortCode::FAST); + t3.join(); + t4.join(); + std::cout << "FAST speed up complete" << std::endl; + + std::cout << ShortCode::check_mode() << std::endl; return 0; } diff --git a/src/short_code/short_code.cc b/src/short_code/short_code.cc index 6d70279..5e708a3 100644 --- a/src/short_code/short_code.cc +++ b/src/short_code/short_code.cc @@ -3,10 +3,8 @@ #include "basic_ranges.h" std::mutex ShortCode::map_building; - bool ShortCode::fast_mode_available = false; bool ShortCode::normal_mode_available = false; - std::vector ShortCode::all_cases_list; std::unordered_map ShortCode::all_cases_dict; @@ -15,27 +13,28 @@ void ShortCode::speed_up(ShortCode::Mode mode) { return; // fast mode already available } if (mode == ShortCode::FAST) { // build fast mode data - - // build_mappings build_mappings(); - } else if (mode == ShortCode::NORMAL && !normal_mode_available) { // build normal mode data BasicRanges::build_basic_ranges(); // blocking function 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) { - 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 - AllCases::build_all_cases(); // blocking function - for (int head = 0; head < 16; ++head) { uint64_t prefix = (uint64_t)head << 32; 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) { 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(); - } diff --git a/src/short_code/short_code.h b/src/short_code/short_code.h index 2c6f701..9ab0075 100644 --- a/src/short_code/short_code.h +++ b/src/short_code/short_code.h @@ -7,24 +7,19 @@ class ShortCode { public: - enum Mode { NORMAL, FAST }; - - static void build_mappings(); - + static enum Mode check_mode(); static void speed_up(enum Mode mode); - static std::mutex map_building; +private: + static std::mutex map_building; static bool fast_mode_available; static bool normal_mode_available; - - -// static std::vector *basic_ranges; - static std::vector all_cases_list; // short_code -> common_code static std::unordered_map all_cases_dict; // common_code -> short_code + static void build_mappings(); };