From 39fe5f932d83a1a0f3f513bac65cbe8244a40abb Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Sun, 15 Jan 2023 10:21:10 +0800 Subject: [PATCH] update: using const ref instead of const ptr --- src/all_cases/all_cases.cc | 18 +++++++++--------- src/all_cases/all_cases.h | 4 +--- src/all_cases/basic_ranges.cc | 4 ++-- src/all_cases/basic_ranges.h | 2 +- src/main.cc | 17 ++++++++++------- src/short_code/convert.cc | 4 ++-- src/short_code/data_loader.cc | 2 +- 7 files changed, 26 insertions(+), 25 deletions(-) diff --git a/src/all_cases/all_cases.cc b/src/all_cases/all_cases.cc index 86af90d..5a0e2c1 100644 --- a/src/all_cases/all_cases.cc +++ b/src/all_cases/all_cases.cc @@ -5,11 +5,11 @@ std::mutex AllCases::building; bool AllCases::available = false; std::vector AllCases::data[]; -const std::vector (*AllCases::fetch())[16] { // get all cases content +const std::vector (&AllCases::fetch())[16] { // get all cases content if (status() != AVAILABLE) { AllCases::build(); // all cases initialize } - return &AllCases::data; // return const ptr + return AllCases::data; // return const ref } AllCases::Status AllCases::status() { // get all cases status @@ -36,20 +36,20 @@ void AllCases::build() { // ensure that all cases available } void AllCases::build_data() { // find all cases - auto basic_ranges = BasicRanges::fetch(); + const auto &basic_ranges = BasicRanges::fetch(); for (uint32_t head = 0; head < 15; ++head) { // address of 2x2 block if ((head & 0b11) == 0b11) { ++head; // skip invalid address } /// head -> 0/1/2 / 4/5/6 / 8/9/10 / 12/13/14 data[head].reserve(ALL_CASES_SIZE[head]); // memory pre-allocated - for (auto index = 0; index < basic_ranges->size(); ++index) { - uint32_t broken = check_case(head, (*basic_ranges)[index]); // check and get broken address - auto range_rev = Common::range_reverse((*basic_ranges)[index]); // reversed range + for (auto index = 0; index < basic_ranges.size(); ++index) { + uint32_t broken = check_case(head, basic_ranges[index]); // check and get broken address + auto range_rev = Common::range_reverse(basic_ranges[index]); // reversed range if (broken) { // invalid case auto delta = (uint32_t)1 << (32 - broken * 2); // this <--delta--> next possible range - auto min_next = (range_rev & ~(delta - 1)) + delta; - while (Common::range_reverse((*basic_ranges)[++index]) < min_next); // located next range + auto next_min = (range_rev & ~(delta - 1)) + delta; + while (Common::range_reverse(basic_ranges[++index]) < next_min); // located next range --index; } else { AllCases::data[head].emplace_back(range_rev); // release valid cases @@ -89,5 +89,5 @@ int AllCases::check_case(uint32_t head, uint32_t range) { // check the head and break; } } - return 0; // pass + return 0; // pass check } diff --git a/src/all_cases/all_cases.h b/src/all_cases/all_cases.h index 1069a11..7f2915e 100644 --- a/src/all_cases/all_cases.h +++ b/src/all_cases/all_cases.h @@ -16,7 +16,7 @@ class AllCases : public BasicRanges { public: static void build(); static enum Status status(); - static const std::vector (*fetch())[16]; + static const std::vector (&fetch())[16]; private: static bool available; @@ -24,7 +24,5 @@ private: static std::vector data[16]; static void build_data(); - static int check_case(uint32_t head, uint32_t range); - }; diff --git a/src/all_cases/basic_ranges.cc b/src/all_cases/basic_ranges.cc index edf7f01..ebf7b6a 100644 --- a/src/all_cases/basic_ranges.cc +++ b/src/all_cases/basic_ranges.cc @@ -7,11 +7,11 @@ std::mutex BasicRanges::building; bool BasicRanges::available = false; std::vector BasicRanges::data; -const std::vector* BasicRanges::fetch() { // get basic ranges content +const std::vector& BasicRanges::fetch() { // get basic ranges content if (status() != AVAILABLE) { BasicRanges::build(); // basic ranges initialize } - return &BasicRanges::data; // return const ptr + return BasicRanges::data; // return const ref } BasicRanges::Status BasicRanges::status() { // get basic ranges status diff --git a/src/all_cases/basic_ranges.h b/src/all_cases/basic_ranges.h index b874e98..4fb3378 100644 --- a/src/all_cases/basic_ranges.h +++ b/src/all_cases/basic_ranges.h @@ -15,7 +15,7 @@ public: }; static void build(); static enum Status status(); - static const std::vector* fetch(); + static const std::vector& fetch(); private: struct generate_t { diff --git a/src/main.cc b/src/main.cc index 420913f..23d1750 100644 --- a/src/main.cc +++ b/src/main.cc @@ -242,19 +242,22 @@ int main() { // } // BasicRanges::build(); -// std::cout << "size: " << BasicRanges::fetch()->size() << std::endl; +// const auto &br = BasicRanges::fetch(); +// br.push_back(123); +// std::cout << "size: " << BasicRanges::fetch().size() << std::endl; +// std::cout << "size: " << br.size() << std::endl; // for (const auto &range : *BasicRanges::fetch()) { // printf("%08X\n", range); // } AllCases::build(); - for (auto head = 0; head < 16; ++head) { - uint64_t prefix = (uint64_t)head << 32; - for (const auto &range : (*AllCases::fetch())[head]) { - printf("%09lX\n", prefix | range); - } - } +// for (auto head = 0; head < 16; ++head) { +// uint64_t prefix = (uint64_t)head << 32; +// for (const auto &range : AllCases::fetch()[head]) { +// printf("%09lX\n", prefix | range); +// } +// } std::cerr << (clock() - start_time) * 1000 / CLOCKS_PER_SEC << "ms" << std::endl; // std::cerr << (clock() - start_time) * 1000000 / CLOCKS_PER_SEC << "us" << std::endl; diff --git a/src/short_code/convert.cc b/src/short_code/convert.cc index 8a3420b..2f9aa1b 100644 --- a/src/short_code/convert.cc +++ b/src/short_code/convert.cc @@ -26,7 +26,7 @@ uint32_t ShortCode::tiny_encode(uint64_t common_code) { // common_code --low-mem auto target = Common::range_reverse((uint32_t)common_code); // target range for (int index = 0; index < BASIC_RANGES_INDEX[prefix]; ++index) { // traverse basic ranges - uint32_t range = (*BasicRanges::fetch())[index + BASIC_RANGES_OFFSET[prefix]]; + uint32_t range = BasicRanges::fetch()[index + BASIC_RANGES_OFFSET[prefix]]; if (range == target) { break; // found target range } @@ -55,7 +55,7 @@ uint64_t ShortCode::tiny_decode(uint32_t short_code) { // short_code --low-memor uint32_t range; for (int index = 0; index < BASIC_RANGES_INDEX[prefix]; ++index) { // traverse basic ranges - range = (*BasicRanges::fetch())[index + BASIC_RANGES_OFFSET[prefix]]; + range = BasicRanges::fetch()[index + BASIC_RANGES_OFFSET[prefix]]; if (Common::check_case(head, range)) { // search for valid cases if (short_code == 0) { break; // found target range diff --git a/src/short_code/data_loader.cc b/src/short_code/data_loader.cc index 2a2a666..4dddaa5 100644 --- a/src/short_code/data_loader.cc +++ b/src/short_code/data_loader.cc @@ -37,7 +37,7 @@ void ShortCode::build_mappings() { // build fast search mappings if (map_building.try_lock()) { // lock success -> start building for (int head = 0; head < 16; ++head) { uint64_t prefix = (uint64_t)head << 32; - for (const auto &range : (*AllCases::fetch())[head]) { // blocking function + for (const auto &range : AllCases::fetch()[head]) { // blocking function all_cases_list.emplace_back(prefix | range); // short_code -> common_code } }