Browse Source

update: using const ref instead of const ptr

legacy
Dnomd343 2 years ago
parent
commit
39fe5f932d
  1. 18
      src/all_cases/all_cases.cc
  2. 4
      src/all_cases/all_cases.h
  3. 4
      src/all_cases/basic_ranges.cc
  4. 2
      src/all_cases/basic_ranges.h
  5. 17
      src/main.cc
  6. 4
      src/short_code/convert.cc
  7. 2
      src/short_code/data_loader.cc

18
src/all_cases/all_cases.cc

@ -5,11 +5,11 @@ std::mutex AllCases::building;
bool AllCases::available = false; bool AllCases::available = false;
std::vector<uint32_t> AllCases::data[]; std::vector<uint32_t> AllCases::data[];
const std::vector<uint32_t> (*AllCases::fetch())[16] { // get all cases content const std::vector<uint32_t> (&AllCases::fetch())[16] { // get all cases content
if (status() != AVAILABLE) { if (status() != AVAILABLE) {
AllCases::build(); // all cases initialize 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 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 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 for (uint32_t head = 0; head < 15; ++head) { // address of 2x2 block
if ((head & 0b11) == 0b11) { if ((head & 0b11) == 0b11) {
++head; // skip invalid address ++head; // skip invalid address
} }
/// head -> 0/1/2 / 4/5/6 / 8/9/10 / 12/13/14 /// head -> 0/1/2 / 4/5/6 / 8/9/10 / 12/13/14
data[head].reserve(ALL_CASES_SIZE[head]); // memory pre-allocated data[head].reserve(ALL_CASES_SIZE[head]); // memory pre-allocated
for (auto index = 0; index < basic_ranges->size(); ++index) { for (auto index = 0; index < basic_ranges.size(); ++index) {
uint32_t broken = check_case(head, (*basic_ranges)[index]); // check and get broken address 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 auto range_rev = Common::range_reverse(basic_ranges[index]); // reversed range
if (broken) { // invalid case if (broken) { // invalid case
auto delta = (uint32_t)1 << (32 - broken * 2); // this <--delta--> next possible range auto delta = (uint32_t)1 << (32 - broken * 2); // this <--delta--> next possible range
auto min_next = (range_rev & ~(delta - 1)) + delta; auto next_min = (range_rev & ~(delta - 1)) + delta;
while (Common::range_reverse((*basic_ranges)[++index]) < min_next); // located next range while (Common::range_reverse(basic_ranges[++index]) < next_min); // located next range
--index; --index;
} else { } else {
AllCases::data[head].emplace_back(range_rev); // release valid cases 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; break;
} }
} }
return 0; // pass return 0; // pass check
} }

4
src/all_cases/all_cases.h

@ -16,7 +16,7 @@ class AllCases : public BasicRanges {
public: public:
static void build(); static void build();
static enum Status status(); static enum Status status();
static const std::vector<uint32_t> (*fetch())[16]; static const std::vector<uint32_t> (&fetch())[16];
private: private:
static bool available; static bool available;
@ -24,7 +24,5 @@ private:
static std::vector<uint32_t> data[16]; static std::vector<uint32_t> data[16];
static void build_data(); static void build_data();
static int check_case(uint32_t head, uint32_t range); static int check_case(uint32_t head, uint32_t range);
}; };

4
src/all_cases/basic_ranges.cc

@ -7,11 +7,11 @@ std::mutex BasicRanges::building;
bool BasicRanges::available = false; bool BasicRanges::available = false;
std::vector<uint32_t> BasicRanges::data; std::vector<uint32_t> BasicRanges::data;
const std::vector<uint32_t>* BasicRanges::fetch() { // get basic ranges content const std::vector<uint32_t>& BasicRanges::fetch() { // get basic ranges content
if (status() != AVAILABLE) { if (status() != AVAILABLE) {
BasicRanges::build(); // basic ranges initialize 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 BasicRanges::Status BasicRanges::status() { // get basic ranges status

2
src/all_cases/basic_ranges.h

@ -15,7 +15,7 @@ public:
}; };
static void build(); static void build();
static enum Status status(); static enum Status status();
static const std::vector<uint32_t>* fetch(); static const std::vector<uint32_t>& fetch();
private: private:
struct generate_t { struct generate_t {

17
src/main.cc

@ -242,19 +242,22 @@ int main() {
// } // }
// BasicRanges::build(); // 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()) { // for (const auto &range : *BasicRanges::fetch()) {
// printf("%08X\n", range); // printf("%08X\n", range);
// } // }
AllCases::build(); AllCases::build();
for (auto head = 0; head < 16; ++head) { // for (auto head = 0; head < 16; ++head) {
uint64_t prefix = (uint64_t)head << 32; // uint64_t prefix = (uint64_t)head << 32;
for (const auto &range : (*AllCases::fetch())[head]) { // for (const auto &range : AllCases::fetch()[head]) {
printf("%09lX\n", prefix | range); // printf("%09lX\n", prefix | range);
} // }
} // }
std::cerr << (clock() - start_time) * 1000 / CLOCKS_PER_SEC << "ms" << std::endl; std::cerr << (clock() - start_time) * 1000 / CLOCKS_PER_SEC << "ms" << std::endl;
// std::cerr << (clock() - start_time) * 1000000 / CLOCKS_PER_SEC << "us" << std::endl; // std::cerr << (clock() - start_time) * 1000000 / CLOCKS_PER_SEC << "us" << std::endl;

4
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 auto target = Common::range_reverse((uint32_t)common_code); // target range
for (int index = 0; index < BASIC_RANGES_INDEX[prefix]; ++index) { // traverse basic ranges 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) { if (range == target) {
break; // found target range break; // found target range
} }
@ -55,7 +55,7 @@ uint64_t ShortCode::tiny_decode(uint32_t short_code) { // short_code --low-memor
uint32_t range; uint32_t range;
for (int index = 0; index < BASIC_RANGES_INDEX[prefix]; ++index) { // traverse basic ranges 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 (Common::check_case(head, range)) { // search for valid cases
if (short_code == 0) { if (short_code == 0) {
break; // found target range break; // found target range

2
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 if (map_building.try_lock()) { // lock success -> start building
for (int head = 0; head < 16; ++head) { for (int head = 0; head < 16; ++head) {
uint64_t prefix = (uint64_t)head << 32; 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 all_cases_list.emplace_back(prefix | range); // short_code -> common_code
} }
} }

Loading…
Cancel
Save