Browse Source

update: check mode for ShortCode

master
Dnomd343 2 years ago
parent
commit
6dc2d7eef8
  1. 68
      src/main.cc
  2. 23
      src/short_code/short_code.cc
  3. 13
      src/short_code/short_code.h

68
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;
}

23
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<uint64_t> ShortCode::all_cases_list;
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
}
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();
}

13
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<uint32_t> *basic_ranges;
static std::vector<uint64_t> all_cases_list; // short_code -> common_code
static std::unordered_map<uint64_t, uint32_t> all_cases_dict; // common_code -> short_code
static void build_mappings();
};

Loading…
Cancel
Save