Browse Source

feat: next ShortCode module

legacy
Dnomd343 2 years ago
parent
commit
36495525a0
  1. 4
      src/CMakeLists.txt
  2. 2
      src/all_cases/basic_ranges.h
  3. 22
      src/main.cc
  4. 4
      src/short_code/CMakeLists.txt
  5. 55
      src/short_code/short_code.cc
  6. 30
      src/short_code/short_code.h

4
src/CMakeLists.txt

@ -2,13 +2,15 @@ cmake_minimum_required(VERSION 3.0)
include_directories(common) include_directories(common)
include_directories(all_cases) include_directories(all_cases)
include_directories(short_code)
include_directories(common_code) include_directories(common_code)
add_subdirectory(common) add_subdirectory(common)
add_subdirectory(all_cases) add_subdirectory(all_cases)
add_subdirectory(short_code)
add_subdirectory(common_code) add_subdirectory(common_code)
add_executable(klotski main.cc) add_executable(klotski main.cc)
target_link_libraries(klotski all_cases) target_link_libraries(klotski all_cases)
target_link_libraries(klotski short_code)
target_link_libraries(klotski common_code) target_link_libraries(klotski common_code)

2
src/all_cases/basic_ranges.h

@ -12,7 +12,7 @@ public:
AVAILABLE, AVAILABLE,
}; };
static void build_basic_ranges(); static void build_basic_ranges();
static enum Status basic_ranges_status(); static enum Status basic_ranges_status(); // TODO: convert to get_status function
static const std::vector<uint32_t>* get_basic_ranges(); static const std::vector<uint32_t>* get_basic_ranges();
private: private:

22
src/main.cc

@ -2,6 +2,7 @@
#include "all_cases.h" #include "all_cases.h"
#include "basic_ranges.h" #include "basic_ranges.h"
#include "common_code.h" #include "common_code.h"
#include "short_code.h"
//void get_status() { //void get_status() {
// switch (BasicRanges::basic_ranges_status()) { // switch (BasicRanges::basic_ranges_status()) {
@ -53,12 +54,23 @@ int main() {
// std::cout << CommonCode::to_string(0x1A9BF0C00) << std::endl; // std::cout << CommonCode::to_string(0x1A9BF0C00) << std::endl;
// std::cout << CommonCode::to_string(0x1A9BF0C00, true) << std::endl; // std::cout << CommonCode::to_string(0x1A9BF0C00, true) << std::endl;
auto c = CommonCode("1A9bF0c0"); // auto c = CommonCode("1A9bF0c0");
std::cout << c.to_string(true) << std::endl; // std::cout << c.to_string(true) << std::endl;
std::cout << c.to_string() << std::endl; // std::cout << c.to_string() << std::endl;
printf("%09lX\n", c.unwrap()); // printf("%09lX\n", c.unwrap());
//
// std::cout << CommonCode(0x1A9BF0C00).to_string() << std::endl;
std::cout << "start NORMAL speed up" << std::endl;
ShortCode::speed_up(ShortCode::NORMAL);
std::cout << "NORMAL speed up complete" << std::endl;
std::cout << "start FAST speed up" << std::endl;
ShortCode::speed_up(ShortCode::FAST);
std::cout << "FAST speed up complete" << std::endl;
std::cout << CommonCode(0x1A9BF0C00).to_string() << std::endl; std::cout << ShortCode::all_cases_list.size() << std::endl;
return 0; return 0;
} }

4
src/short_code/CMakeLists.txt

@ -0,0 +1,4 @@
cmake_minimum_required(VERSION 3.0)
add_library(short_code short_code.cc)
target_link_libraries(short_code all_cases)

55
src/short_code/short_code.cc

@ -0,0 +1,55 @@
#include "all_cases.h"
#include "short_code.h"
#include "basic_ranges.h"
std::mutex ShortCode::map_building;
bool ShortCode::fast_mode_available = false;
bool ShortCode::normal_mode_available = false;
std::vector<uint64_t> ShortCode::all_cases_list;
std::unordered_map<uint64_t, uint32_t> ShortCode::all_cases_dict;
void ShortCode::speed_up(ShortCode::Mode mode) {
if (fast_mode_available) {
return; // fast mode already available
}
if (mode == ShortCode::FAST) { // build fast mode data
// build_mappings
build_mappings();
} else if (mode == ShortCode::NORMAL && !normal_mode_available) { // build normal mode data
BasicRanges::build_basic_ranges(); // blocking function
normal_mode_available = true;
}
}
void ShortCode::build_mappings() { // build fast search mappings
if (fast_mode_available) {
return; // all cases map already built
}
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]) {
all_cases_list.emplace_back(prefix | range); // short_code -> common_code
}
}
for (int index = 0; index < all_cases_list.size(); ++index) {
all_cases_dict[all_cases_list[index]] = index; // common_code -> short_code
}
fast_mode_available = true; // set available flag
} else { // another thread building
map_building.lock(); // blocking waiting
}
map_building.unlock();
}

30
src/short_code/short_code.h

@ -0,0 +1,30 @@
#pragma once
#include <mutex>
#include <vector>
#include <cstdint>
#include <unordered_map>
class ShortCode {
public:
enum Mode {
NORMAL, FAST
};
static void build_mappings();
static void speed_up(enum Mode mode);
static std::mutex map_building;
static bool fast_mode_available;
static bool normal_mode_available;
// static std::vector<uint32_t> *basic_ranges;
static std::vector<uint64_t> all_cases_list; // short_code -> common_code
static std::unordered_map<uint64_t, uint32_t> all_cases_dict; // common_code -> short_code
};
Loading…
Cancel
Save