diff --git a/all_cases/basic_ranges.cc b/all_cases/basic_ranges.cc index 79cd2d7..40bb413 100644 --- a/all_cases/basic_ranges.cc +++ b/all_cases/basic_ranges.cc @@ -15,11 +15,44 @@ inline uint32_t binary_count(uint32_t bin) { // get number of non-zero bits return bin & 0b111111; } -void BasicRanges::generate_ranges(int n1, int n2, int n3, int n4) { +const std::vector* BasicRanges::get_basic_ranges() { // get const ptr of basic ranges + if (basic_ranges.empty()) { + build_basic_ranges(); // basic ranges initialize + } + return &basic_ranges; // return ptr +} + +void BasicRanges::build_basic_ranges() { // build basic ranges + if (basic_ranges_available) { + return; // basic ranges already built + } + if (basic_ranges_building.try_lock()) { // lock success -> start building + /// 0x0 -> 00 | 1x2 -> 01 | 2x1 -> 10 | 1x1 -> 11 + for (int n = 0; n <= 7; ++n) { // number of 1x2 and 2x1 block -> 0 ~ 7 + for (int n_2x1 = 0; n_2x1 <= n; ++n_2x1) { // number of 2x1 block -> 0 ~ n + for (int n_1x1 = 0; n_1x1 <= (14 - n * 2); ++n_1x1) { // number of 1x1 block -> 0 ~ (14 - 2n) + int n_1x2 = n - n_2x1; + int n_space = 16 - n * 2 - n_1x1; + generate_ranges(n_space, n_1x2, n_2x1, n_1x1); // generate target ranges + } + } + } + std::sort(basic_ranges.begin(), basic_ranges.end()); // sort basic ranges + for (uint32_t &range : basic_ranges) { + range = Common::range_reverse(range); // basic ranges reverse + } + basic_ranges_available = true; // set available flag + } else { // another thread building + basic_ranges_building.lock(); // blocking waiting + } + basic_ranges_building.unlock(); +} + +void BasicRanges::generate_ranges(int n1, int n2, int n3, int n4) { // generate specific basic ranges int len, limit; - constexpr uint32_t M_01 = 0b01 << 30; - constexpr uint32_t M_10 = 0b10 << 30; - constexpr uint32_t M_11 = 0b11 << 30; + constexpr uint32_t MASK_01 = 0b01 << 30; + constexpr uint32_t MASK_10 = 0b10 << 30; + constexpr uint32_t MASK_11 = 0b11 << 30; std::vector cache_1, cache_2; len = n1 + n2; @@ -32,7 +65,7 @@ void BasicRanges::generate_ranges(int n1, int n2, int n3, int n4) { for (int i = 0; i < len; ++i) { // generate range base on binary value range >>= 2; if ((bin >> i) & 0b1) { // non-zero bit - range |= M_01; // 01000... + range |= MASK_01; } } cache_1.emplace_back(range); // insert into first layer @@ -48,10 +81,10 @@ void BasicRanges::generate_ranges(int n1, int n2, int n3, int n4) { uint32_t range = 0; for (int i = 0; i < len; ++i) { // generate range base on binary value if ((bin >> i) & 0b1) { // non-zero bit - (range >>= 2) |= M_10; // 10000... + (range >>= 2) |= MASK_10; continue; } - (range >>= 2) |= base & M_11; + (range >>= 2) |= base & MASK_11; base <<= 2; } cache_2.emplace_back(range); // insert into second layer @@ -68,74 +101,13 @@ void BasicRanges::generate_ranges(int n1, int n2, int n3, int n4) { uint32_t range = 0; for (int i = 0; i < len; ++i) { // generate range base on binary value if ((bin >> i) & 0b1) { // non-zero bit - (range >>= 2) |= M_11; // 11000... + (range >>= 2) |= MASK_11; continue; } - (range >>= 2) |= base & M_11; + (range >>= 2) |= base & MASK_11; base <<= 2; } - basic_ranges.emplace_back(range); // insert into release ranges - } - } -} - -#include -#include - -void BasicRanges::build_basic_ranges() { - - std::cout << std::this_thread::get_id() << " enter build function" << std::endl; - - if (basic_ranges_available) { - std::cout << std::this_thread::get_id() << " data already built -> skip" << std::endl; - return; // basic ranges already built - } - - if (basic_ranges_building.try_lock()) { // lock success -> not building - - std::cout << std::this_thread::get_id() << " try lock success -> start build process" << std::endl; - - -// AllCases::basic_ranges.emplace_back(0); -// AllCases::basic_ranges.emplace_back(1); -// AllCases::basic_ranges.emplace_back(2); -// sleep(2); // assume using a lot of time - - for (int n = 0; n <= 7; ++n) { // number of 1x2 and 2x1 block -> 0 ~ 7 - for (int n_2x1 = 0; n_2x1 <= n; ++n_2x1) { // number of 2x1 block -> 0 ~ n - for (int n_1x1 = 0; n_1x1 <= (14 - n * 2); ++n_1x1) { // number of 1x1 block -> 0 ~ (14 - 2n) - int n_1x2 = n - n_2x1; - int n_space = 16 - n * 2 - n_1x1; - /// 0x0 -> 00 | 1x2 -> 01 | 2x1 -> 10 | 1x1 -> 11 - generate_ranges(n_space, n_1x2, n_2x1, n_1x1); // generate target ranges - } - } + basic_ranges.emplace_back(range); // insert into release } - - std::sort(basic_ranges.begin(), basic_ranges.end()); // sort basic ranges - for (uint32_t &range : basic_ranges) { - range = Common::range_reverse(range); // range reverse - } - - - basic_ranges_available = true; // enable available flag - - std::cout << std::this_thread::get_id() << " build complete -> continue" << std::endl; - - } else { // another thread building - - std::cout << std::this_thread::get_id() << " another thread building -> wait" << std::endl; - basic_ranges_building.lock(); // blocking wait - - } - std::cout << std::this_thread::get_id() << " work complete -> unlock" << std::endl; - basic_ranges_building.unlock(); - -} - -const std::vector* BasicRanges::get_basic_ranges() { - if (basic_ranges.empty()) { - build_basic_ranges(); // basic ranges initialize } - return &basic_ranges; } diff --git a/all_cases/main.cc b/all_cases/main.cc index ceda4d2..9b2b963 100644 --- a/all_cases/main.cc +++ b/all_cases/main.cc @@ -3,25 +3,16 @@ #include "basic_ranges.h" #include -#include int main() { - - std::cout << BasicRanges::get_basic_ranges()->size() << std::endl; - std::thread t1(BasicRanges::build_basic_ranges); -// usleep(10); std::thread t2(BasicRanges::build_basic_ranges); -// usleep(10); std::thread t3(BasicRanges::build_basic_ranges); -// usleep(10); - t1.join(); t2.join(); t3.join(); - BasicRanges::build_basic_ranges(); - +// BasicRanges::build_basic_ranges(); std::cout << BasicRanges::get_basic_ranges()->size() << std::endl; return 0;