Browse Source

update: using const ref instead of const ptr

master
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;
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) {
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
}

4
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<uint32_t> (*fetch())[16];
static const std::vector<uint32_t> (&fetch())[16];
private:
static bool available;
@ -24,7 +24,5 @@ private:
static std::vector<uint32_t> data[16];
static void build_data();
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;
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) {
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

2
src/all_cases/basic_ranges.h

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

17
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;

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
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

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

Loading…
Cancel
Save