From 45bdec740a54ac0ca462dac18ffc2ef60d7f1b0b Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Sun, 8 Jan 2023 20:41:29 +0800 Subject: [PATCH] update: interface of AllCases --- src/all_cases/all_cases.cc | 10 ++--- src/all_cases/all_cases.h | 6 +-- src/all_cases/basic_ranges.cc | 8 ++-- src/all_cases/basic_ranges.h | 6 +-- src/main.cc | 84 ++++++++++++++++++++--------------- src/short_code/short_code.cc | 5 +-- 6 files changed, 65 insertions(+), 54 deletions(-) diff --git a/src/all_cases/all_cases.cc b/src/all_cases/all_cases.cc index e54fd8d..4639c4f 100644 --- a/src/all_cases/all_cases.cc +++ b/src/all_cases/all_cases.cc @@ -5,7 +5,7 @@ std::mutex AllCases::all_cases_building; bool AllCases::all_cases_available = false; std::vector AllCases::all_cases[]; -AllCases::Status AllCases::get_status() { // get all cases status +AllCases::Status AllCases::status() { // get all cases status if (all_cases_available) { return AVAILABLE; // all cases already built } @@ -16,14 +16,14 @@ AllCases::Status AllCases::get_status() { // get all cases status return NO_INIT; } -const std::vector (*AllCases::get_all_cases())[16] { // get const ptr of all cases +const std::vector (*AllCases::fetch())[16] { // get const ptr of all cases if (all_cases->empty()) { - build_all_cases(); // all cases initialize + AllCases::build(); // all cases initialize } return &all_cases; // return ptr } -void AllCases::build_all_cases() { // build all cases +void AllCases::build() { // build all cases if (AllCases::all_cases_available) { return; // all cases already built } @@ -33,7 +33,7 @@ void AllCases::build_all_cases() { // build all cases if ((head & 0b11) == 0b11) { continue; // invalid 2x2 address } - for (uint32_t const &range : *get_basic_ranges()) { // check base on 2x2 address and range + for (uint32_t const &range : *BasicRanges::fetch()) { // check base on 2x2 address and range if (Common::check_case(head, range)) { all_cases[head].emplace_back(Common::range_reverse(range)); // found valid case } diff --git a/src/all_cases/all_cases.h b/src/all_cases/all_cases.h index fb9ddf9..db42833 100644 --- a/src/all_cases/all_cases.h +++ b/src/all_cases/all_cases.h @@ -7,9 +7,9 @@ class AllCases : public BasicRanges { public: - static void build_all_cases(); - static enum Status get_status(); - static const std::vector (*get_all_cases())[16]; + static void build(); + static enum Status status(); + static const std::vector (*fetch())[16]; private: static bool all_cases_available; diff --git a/src/all_cases/basic_ranges.cc b/src/all_cases/basic_ranges.cc index eb023c7..d0da21c 100644 --- a/src/all_cases/basic_ranges.cc +++ b/src/all_cases/basic_ranges.cc @@ -15,7 +15,7 @@ inline uint32_t binary_count(uint32_t bin) { // get number of non-zero bits return bin & 0b111111; } -BasicRanges::Status BasicRanges::get_status() { // get basic ranges status +BasicRanges::Status BasicRanges::status() { // get basic ranges status if (basic_ranges_available) { return AVAILABLE; // basic ranges already built } @@ -26,14 +26,14 @@ BasicRanges::Status BasicRanges::get_status() { // get basic ranges status return NO_INIT; } -const std::vector* BasicRanges::get_basic_ranges() { // get const ptr of basic ranges +const std::vector* BasicRanges::fetch() { // get const ptr of basic ranges if (basic_ranges.empty()) { - build_basic_ranges(); // basic ranges initialize + BasicRanges::build(); // basic ranges initialize } return &basic_ranges; // return ptr } -void BasicRanges::build_basic_ranges() { // build basic ranges +void BasicRanges::build() { // build basic ranges if (basic_ranges_available) { return; // basic ranges already built } diff --git a/src/all_cases/basic_ranges.h b/src/all_cases/basic_ranges.h index c14bb65..c81d755 100644 --- a/src/all_cases/basic_ranges.h +++ b/src/all_cases/basic_ranges.h @@ -11,9 +11,9 @@ public: BUILDING, AVAILABLE, }; - static enum Status get_status(); - static void build_basic_ranges(); - static const std::vector* get_basic_ranges(); + static void build(); + static enum Status status(); + static const std::vector* fetch(); private: static bool basic_ranges_available; diff --git a/src/main.cc b/src/main.cc index bec7a0c..2b498ef 100644 --- a/src/main.cc +++ b/src/main.cc @@ -7,7 +7,7 @@ #include void get_status() { - switch (BasicRanges::get_status()) { + switch (BasicRanges::status()) { case BasicRanges::NO_INIT: std::cout << "basic ranges no init" << std::endl; break; @@ -19,7 +19,7 @@ void get_status() { break; } - switch (AllCases::get_status()) { + switch (AllCases::status()) { case AllCases::NO_INIT: std::cout << "all cases no init" << std::endl; break; @@ -34,21 +34,33 @@ 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); + printf("%p\n", BasicRanges::build); + printf("%p\n", AllCases::build); + + printf("%p\n", BasicRanges::status); + printf("%p\n", AllCases::status); + + printf("%p\n", BasicRanges::fetch); + printf("%p\n", AllCases::fetch); + + get_status(); + BasicRanges::build(); + get_status(); + AllCases::build(); + get_status(); + + std::cout << BasicRanges::fetch()->size() << std::endl; + + uint32_t sum = 0; + for (auto const &all_case : *AllCases::fetch()) { + sum += all_case.size(); + std::cout << " " << all_case.size() << std::endl; + } + std::cout << sum << std::endl; + + std::cout << AllCases::fetch() << std::endl; + std::cout << AllCases::BasicRanges::fetch() << std::endl; + std::cout << BasicRanges::fetch() << std::endl; // std::cout << CommonCode::check(0x123456789) << std::endl; @@ -69,25 +81,25 @@ int main() { // 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; +// 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 5e708a3..c006e07 100644 --- a/src/short_code/short_code.cc +++ b/src/short_code/short_code.cc @@ -15,7 +15,7 @@ void ShortCode::speed_up(ShortCode::Mode mode) { if (mode == ShortCode::FAST) { // build fast mode data build_mappings(); } else if (mode == ShortCode::NORMAL && !normal_mode_available) { // build normal mode data - BasicRanges::build_basic_ranges(); // blocking function + BasicRanges::build(); // blocking function normal_mode_available = true; } } @@ -34,10 +34,9 @@ ShortCode::Mode ShortCode::check_mode() { // ensure speed up enabled and return /// 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]) { + for (const auto &range : (*AllCases::fetch())[head]) { // blocking function all_cases_list.emplace_back(prefix | range); // short_code -> common_code } }