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;
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) {
return AVAILABLE; // all cases already built
}
@ -16,14 +16,14 @@ AllCases::Status AllCases::get_status() { // get all cases status
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()) {
build_all_cases(); // all cases initialize
AllCases::build(); // all cases initialize
}
return &all_cases; // return ptr
}
void AllCases::build_all_cases() { // build all cases
void AllCases::build() { // build all cases
if (AllCases::all_cases_available) {
return; // all cases already built
}
@ -33,7 +33,7 @@ void AllCases::build_all_cases() { // build all cases
if ((head & 0b11) == 0b11) {
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)) {
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 {
public:
static void build_all_cases();
static enum Status get_status();
static const std::vector<uint32_t> (*get_all_cases())[16];
static void build();
static enum Status status();
static const std::vector<uint32_t> (*fetch())[16];
private:
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;
}
BasicRanges::Status BasicRanges::get_status() { // get basic ranges status
BasicRanges::Status BasicRanges::status() { // get basic ranges status
if (basic_ranges_available) {
return AVAILABLE; // basic ranges already built
}
@ -26,14 +26,14 @@ BasicRanges::Status BasicRanges::get_status() { // get basic ranges status
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()) {
build_basic_ranges(); // basic ranges initialize
BasicRanges::build(); // basic ranges initialize
}
return &basic_ranges; // return ptr
}
void BasicRanges::build_basic_ranges() { // build basic ranges
void BasicRanges::build() { // build basic ranges
if (basic_ranges_available) {
return; // basic ranges already built
}

6
src/all_cases/basic_ranges.h

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

84
src/main.cc

@ -7,7 +7,7 @@
#include <thread>
void get_status() {
switch (BasicRanges::get_status()) {
switch (BasicRanges::status()) {
case BasicRanges::NO_INIT:
std::cout << "basic ranges no init" << std::endl;
break;
@ -19,7 +19,7 @@ void get_status() {
break;
}
switch (AllCases::get_status()) {
switch (AllCases::status()) {
case AllCases::NO_INIT:
std::cout << "all cases no init" << std::endl;
break;
@ -34,21 +34,33 @@ void get_status() {
int main() {
// get_status();
// BasicRanges::build_basic_ranges();
// get_status();
// AllCases::build_all_cases();
// get_status();
//
// for (auto const &all_case : *AllCases::get_all_cases()) {
// std::cout << " " << all_case.size() << std::endl;
// }
//
// std::cout << BasicRanges::get_basic_ranges() << std::endl;
// std::cout << AllCases::get_basic_ranges() << std::endl;
//
// printf("%p\n", BasicRanges::get_status);
// printf("%p\n", AllCases::get_status);
printf("%p\n", BasicRanges::build);
printf("%p\n", AllCases::build);
printf("%p\n", BasicRanges::status);
printf("%p\n", AllCases::status);
printf("%p\n", BasicRanges::fetch);
printf("%p\n", AllCases::fetch);
get_status();
BasicRanges::build();
get_status();
AllCases::build();
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;
@ -69,25 +81,25 @@ int main() {
// std::cout << ShortCode::check_mode() << std::endl;
std::cout << "start NORMAL speed up" << std::endl;
// ShortCode::speed_up(ShortCode::NORMAL);
std::thread t1(ShortCode::speed_up, ShortCode::NORMAL);
std::thread t2(ShortCode::speed_up, ShortCode::NORMAL);
t1.join();
t2.join();
std::cout << "NORMAL speed up complete" << std::endl;
std::cout << ShortCode::check_mode() << std::endl;
std::cout << "start FAST speed up" << std::endl;
// ShortCode::speed_up(ShortCode::FAST);
std::thread t3(ShortCode::speed_up, ShortCode::FAST);
std::thread t4(ShortCode::speed_up, ShortCode::FAST);
t3.join();
t4.join();
std::cout << "FAST speed up complete" << std::endl;
std::cout << ShortCode::check_mode() << std::endl;
// std::cout << "start NORMAL speed up" << std::endl;
//// ShortCode::speed_up(ShortCode::NORMAL);
// std::thread t1(ShortCode::speed_up, ShortCode::NORMAL);
// std::thread t2(ShortCode::speed_up, ShortCode::NORMAL);
// t1.join();
// t2.join();
// std::cout << "NORMAL speed up complete" << std::endl;
//
// std::cout << ShortCode::check_mode() << std::endl;
//
// std::cout << "start FAST speed up" << std::endl;
//// ShortCode::speed_up(ShortCode::FAST);
// std::thread t3(ShortCode::speed_up, ShortCode::FAST);
// std::thread t4(ShortCode::speed_up, ShortCode::FAST);
// t3.join();
// t4.join();
// std::cout << "FAST speed up complete" << std::endl;
//
// std::cout << ShortCode::check_mode() << std::endl;
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
build_mappings();
} 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;
}
}
@ -34,10 +34,9 @@ ShortCode::Mode ShortCode::check_mode() { // ensure speed up enabled and return
/// ensure that fast_mode_available = false
void ShortCode::build_mappings() { // build fast search mappings
if (map_building.try_lock()) { // lock success -> start building
AllCases::build_all_cases(); // blocking function
for (int head = 0; head < 16; ++head) {
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
}
}

Loading…
Cancel
Save