Browse Source

update: common code support

master
Dnomd343 2 years ago
parent
commit
0c90647f69
  1. 11
      klotski/common_code.h
  2. 10
      src/CMakeLists.txt
  3. 4
      src/common_code/CMakeLists.txt
  4. 6
      src/common_code/common_code.cc
  5. 11
      src/common_code/common_code.h
  6. 80
      src/main.cc

11
klotski/common_code.h

@ -1,11 +0,0 @@
#pragma once
#include <string>
#include <cstdint>
class CommonCode {
public:
static bool check(uint64_t common_code);
static uint64_t code_from_string(const std::string &common_code);
static std::string code_to_string(uint64_t common_code, bool shorten = false);
};

10
src/CMakeLists.txt

@ -1,10 +1,14 @@
cmake_minimum_required(VERSION 3.0)
include_directories(${PROJECT_SOURCE_DIR}/src/common)
include_directories(${PROJECT_SOURCE_DIR}/src/all_cases)
include_directories(common)
include_directories(all_cases)
include_directories(common_code)
add_subdirectory(common)
add_subdirectory(all_cases)
add_subdirectory(common_code)
add_executable(klotski main.cc)
target_link_libraries(klotski common all_cases)
target_link_libraries(klotski all_cases)
target_link_libraries(klotski common_code)

4
src/common_code/CMakeLists.txt

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

6
klotski/common_code.cc → src/common_code/common_code.cc

@ -8,7 +8,7 @@ inline uint8_t last_zero_num(uint32_t bin) { // get last zero number
return __builtin_popcount(bin >> 1);
}
std::string CommonCode::code_to_string(uint64_t common_code, bool shorten) {
std::string CommonCode::to_string(uint64_t common_code, bool shorten) {
if (!CommonCode::check(common_code)) {
throw std::invalid_argument("invalid common code");
}
@ -23,7 +23,7 @@ std::string CommonCode::code_to_string(uint64_t common_code, bool shorten) {
return result;
}
uint64_t CommonCode::code_from_string(const std::string &common_code) {
uint64_t CommonCode::from_string(const std::string &common_code) {
if (common_code.length() > 9 || common_code.length() == 0) {
throw std::invalid_argument("common code format error");
}
@ -44,7 +44,7 @@ uint64_t CommonCode::code_from_string(const std::string &common_code) {
return result << (9 - common_code.length()) * 4; // low-bits fill with zero
}
bool CommonCode::check(uint64_t common_code) {
bool CommonCode::check(uint64_t common_code) { // check whether common code is valid
uint32_t head = common_code >> 32;
if (head >= 16 || (head & 0b11) == 0b11) { // check 2x2 block address
return false; // invalid common code

11
src/common_code/common_code.h

@ -0,0 +1,11 @@
#pragma once
#include <string>
#include <cstdint>
class CommonCode {
public:
static bool check(uint64_t common_code);
static uint64_t from_string(const std::string &common_code);
static std::string to_string(uint64_t common_code, bool shorten = false);
};

80
src/main.cc

@ -1,47 +1,57 @@
#include <iostream>
#include "all_cases.h"
#include "basic_ranges.h"
#include "common_code.h"
void get_status() {
switch (BasicRanges::basic_ranges_status()) {
case BasicRanges::NO_INIT:
std::cout << "basic ranges no init" << std::endl;
break;
case BasicRanges::BUILDING:
std::cout << "basic ranges building" << std::endl;
break;
case BasicRanges::AVAILABLE:
std::cout << "basic ranges available" << std::endl;
break;
}
switch (AllCases::all_cases_status()) {
case AllCases::NO_INIT:
std::cout << "all cases no init" << std::endl;
break;
case AllCases::BUILDING:
std::cout << "all cases building" << std::endl;
break;
case AllCases::AVAILABLE:
std::cout << "all cases available" << std::endl;
break;
}
}
//void get_status() {
// switch (BasicRanges::basic_ranges_status()) {
// case BasicRanges::NO_INIT:
// std::cout << "basic ranges no init" << std::endl;
// break;
// case BasicRanges::BUILDING:
// std::cout << "basic ranges building" << std::endl;
// break;
// case BasicRanges::AVAILABLE:
// std::cout << "basic ranges available" << std::endl;
// break;
// }
//
// switch (AllCases::all_cases_status()) {
// case AllCases::NO_INIT:
// std::cout << "all cases no init" << std::endl;
// break;
// case AllCases::BUILDING:
// std::cout << "all cases building" << std::endl;
// break;
// case AllCases::AVAILABLE:
// std::cout << "all cases available" << std::endl;
// break;
// }
//}
int main() {
get_status();
BasicRanges::build_basic_ranges();
get_status();
AllCases::build_all_cases();
get_status();
// 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;
for (auto const &all_case : *AllCases::get_all_cases()) {
std::cout << " " << all_case.size() << std::endl;
}
std::cout << CommonCode::check(0x123456789) << std::endl;
std::cout << CommonCode::check(0x4FEA13400) << std::endl;
std::cout << BasicRanges::get_basic_ranges() << std::endl;
std::cout << AllCases::get_basic_ranges() << std::endl;
// TODO: should we return a CommonCode object like String::new(...) in rust?
printf("%09lX\n", CommonCode::from_string("1A9bF0c0"));
std::cout << CommonCode::to_string(0x1A9BF0C00) << std::endl;
std::cout << CommonCode::to_string(0x1A9BF0C00, true) << std::endl;
return 0;
}

Loading…
Cancel
Save