Browse Source

update: interface of AllCases

legacy
Dnomd343 2 years ago
parent
commit
45bdec740a
  1. 10
      src/all_cases/all_cases.cc
  2. 6
      src/all_cases/all_cases.h
  3. 8
      src/all_cases/basic_ranges.cc
  4. 6
      src/all_cases/basic_ranges.h
  5. 84
      src/main.cc
  6. 5
      src/short_code/short_code.cc

10
src/all_cases/all_cases.cc

@ -5,7 +5,7 @@ std::mutex AllCases::all_cases_building;
bool AllCases::all_cases_available = false; bool AllCases::all_cases_available = false;
std::vector<uint32_t> AllCases::all_cases[]; std::vector<uint32_t> AllCases::all_cases[];
AllCases::Status AllCases::get_status() { // get all cases status AllCases::Status AllCases::status() { // get all cases status
if (all_cases_available) { if (all_cases_available) {
return AVAILABLE; // all cases already built return AVAILABLE; // all cases already built
} }
@ -16,14 +16,14 @@ AllCases::Status AllCases::get_status() { // get all cases status
return NO_INIT; return NO_INIT;
} }
const std::vector<uint32_t> (*AllCases::get_all_cases())[16] { // get const ptr of all cases const std::vector<uint32_t> (*AllCases::fetch())[16] { // get const ptr of all cases
if (all_cases->empty()) { if (all_cases->empty()) {
build_all_cases(); // all cases initialize AllCases::build(); // all cases initialize
} }
return &all_cases; // return ptr return &all_cases; // return ptr
} }
void AllCases::build_all_cases() { // build all cases void AllCases::build() { // build all cases
if (AllCases::all_cases_available) { if (AllCases::all_cases_available) {
return; // all cases already built return; // all cases already built
} }
@ -33,7 +33,7 @@ void AllCases::build_all_cases() { // build all cases
if ((head & 0b11) == 0b11) { if ((head & 0b11) == 0b11) {
continue; // invalid 2x2 address 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)) { if (Common::check_case(head, range)) {
all_cases[head].emplace_back(Common::range_reverse(range)); // found valid case all_cases[head].emplace_back(Common::range_reverse(range)); // found valid case
} }

6
src/all_cases/all_cases.h

@ -7,9 +7,9 @@
class AllCases : public BasicRanges { class AllCases : public BasicRanges {
public: public:
static void build_all_cases(); static void build();
static enum Status get_status(); static enum Status status();
static const std::vector<uint32_t> (*get_all_cases())[16]; static const std::vector<uint32_t> (*fetch())[16];
private: private:
static bool all_cases_available; static bool all_cases_available;

8
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; return bin & 0b111111;
} }
BasicRanges::Status BasicRanges::get_status() { // get basic ranges status BasicRanges::Status BasicRanges::status() { // get basic ranges status
if (basic_ranges_available) { if (basic_ranges_available) {
return AVAILABLE; // basic ranges already built return AVAILABLE; // basic ranges already built
} }
@ -26,14 +26,14 @@ BasicRanges::Status BasicRanges::get_status() { // get basic ranges status
return NO_INIT; return NO_INIT;
} }
const std::vector<uint32_t>* BasicRanges::get_basic_ranges() { // get const ptr of basic ranges const std::vector<uint32_t>* BasicRanges::fetch() { // get const ptr of basic ranges
if (basic_ranges.empty()) { if (basic_ranges.empty()) {
build_basic_ranges(); // basic ranges initialize BasicRanges::build(); // basic ranges initialize
} }
return &basic_ranges; // return ptr return &basic_ranges; // return ptr
} }
void BasicRanges::build_basic_ranges() { // build basic ranges void BasicRanges::build() { // build basic ranges
if (basic_ranges_available) { if (basic_ranges_available) {
return; // basic ranges already built return; // basic ranges already built
} }

6
src/all_cases/basic_ranges.h

@ -11,9 +11,9 @@ public:
BUILDING, BUILDING,
AVAILABLE, AVAILABLE,
}; };
static enum Status get_status(); static void build();
static void build_basic_ranges(); static enum Status status();
static const std::vector<uint32_t>* get_basic_ranges(); static const std::vector<uint32_t>* fetch();
private: private:
static bool basic_ranges_available; static bool basic_ranges_available;

84
src/main.cc

@ -7,7 +7,7 @@
#include <thread> #include <thread>
void get_status() { void get_status() {
switch (BasicRanges::get_status()) { switch (BasicRanges::status()) {
case BasicRanges::NO_INIT: case BasicRanges::NO_INIT:
std::cout << "basic ranges no init" << std::endl; std::cout << "basic ranges no init" << std::endl;
break; break;
@ -19,7 +19,7 @@ void get_status() {
break; break;
} }
switch (AllCases::get_status()) { switch (AllCases::status()) {
case AllCases::NO_INIT: case AllCases::NO_INIT:
std::cout << "all cases no init" << std::endl; std::cout << "all cases no init" << std::endl;
break; break;
@ -34,21 +34,33 @@ void get_status() {
int main() { int main() {
// get_status(); printf("%p\n", BasicRanges::build);
// BasicRanges::build_basic_ranges(); printf("%p\n", AllCases::build);
// get_status();
// AllCases::build_all_cases(); printf("%p\n", BasicRanges::status);
// get_status(); printf("%p\n", AllCases::status);
//
// for (auto const &all_case : *AllCases::get_all_cases()) { printf("%p\n", BasicRanges::fetch);
// std::cout << " " << all_case.size() << std::endl; printf("%p\n", AllCases::fetch);
// }
// get_status();
// std::cout << BasicRanges::get_basic_ranges() << std::endl; BasicRanges::build();
// std::cout << AllCases::get_basic_ranges() << std::endl; get_status();
// AllCases::build();
// printf("%p\n", BasicRanges::get_status); get_status();
// printf("%p\n", AllCases::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; // std::cout << CommonCode::check(0x123456789) << std::endl;
@ -69,25 +81,25 @@ int main() {
// std::cout << ShortCode::check_mode() << std::endl; // std::cout << ShortCode::check_mode() << std::endl;
std::cout << "start NORMAL speed up" << std::endl; // std::cout << "start NORMAL speed up" << std::endl;
// ShortCode::speed_up(ShortCode::NORMAL); //// ShortCode::speed_up(ShortCode::NORMAL);
std::thread t1(ShortCode::speed_up, ShortCode::NORMAL); // std::thread t1(ShortCode::speed_up, ShortCode::NORMAL);
std::thread t2(ShortCode::speed_up, ShortCode::NORMAL); // std::thread t2(ShortCode::speed_up, ShortCode::NORMAL);
t1.join(); // t1.join();
t2.join(); // t2.join();
std::cout << "NORMAL speed up complete" << std::endl; // std::cout << "NORMAL speed up complete" << std::endl;
//
std::cout << ShortCode::check_mode() << std::endl; // std::cout << ShortCode::check_mode() << std::endl;
//
std::cout << "start FAST speed up" << std::endl; // std::cout << "start FAST speed up" << std::endl;
// ShortCode::speed_up(ShortCode::FAST); //// ShortCode::speed_up(ShortCode::FAST);
std::thread t3(ShortCode::speed_up, ShortCode::FAST); // std::thread t3(ShortCode::speed_up, ShortCode::FAST);
std::thread t4(ShortCode::speed_up, ShortCode::FAST); // std::thread t4(ShortCode::speed_up, ShortCode::FAST);
t3.join(); // t3.join();
t4.join(); // t4.join();
std::cout << "FAST speed up complete" << std::endl; // std::cout << "FAST speed up complete" << std::endl;
//
std::cout << ShortCode::check_mode() << std::endl; // std::cout << ShortCode::check_mode() << std::endl;
return 0; return 0;
} }

5
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 if (mode == ShortCode::FAST) { // build fast mode data
build_mappings(); build_mappings();
} else if (mode == ShortCode::NORMAL && !normal_mode_available) { // build normal mode data } 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; 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 /// ensure that fast_mode_available = false
void ShortCode::build_mappings() { // build fast search mappings 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
AllCases::build_all_cases(); // blocking function
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::get_all_cases())[head]) { 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