Browse Source

feat: common code to string

master
Dnomd343 2 years ago
parent
commit
e58fb35fbb
  1. 21
      klotski/common_code.cc
  2. 3
      klotski/common_code.h
  3. 20
      klotski/main.cc

21
klotski/common_code.cc

@ -1,6 +1,27 @@
#include <stdexcept>
#include "common.h"
#include "common_code.h"
inline uint8_t last_zero_num(uint32_t bin) { // get last zero number
bin ^= (bin - 1);
return __builtin_popcount(bin >> 1);
}
std::string CommonCode::code_to_string(uint64_t common_code, bool shorten) {
if (!CommonCode::check(common_code)) {
throw std::invalid_argument("invalid common code");
}
char result[10];
sprintf(result, "%09lX", common_code);
if (shorten) { // remove `0` after common code
if (common_code == 0x000000000) {
return "0"; // special case
}
result[9 - last_zero_num(common_code) / 4] = '\0'; // truncate string
}
return result;
}
bool CommonCode::check(uint64_t common_code) {
uint32_t head = common_code >> 32;
if (head >= 16 || (head & 0b11) == 0b11) { // check 2x2 block address

3
klotski/common_code.h

@ -1,8 +1,11 @@
#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);
};

20
klotski/main.cc

@ -1,35 +1,39 @@
#include <iostream>
#include "all_cases.h"
#include "short_code.h"
//#include "common_code.h"
#include "common_code.h"
int main() {
// auto a = AllCases();
// auto a = AllCases(AllCases::Build::BASIC_RANGES);
// auto a = AllCases(AllCases::Build::ALL_CASES);
//
// std::cout << "start getting basic ranges" << std::endl;
// std::cout << "basic range: " << a.get_basic_ranges()->size() << std::endl;
//
// std::cout << "start getting all cases" << std::endl;
// for (const auto &temp : *a.get_all_cases()) {
// std::cout << " " << temp.size() << std::endl;
// }
auto s = ShortCode();
// auto s = ShortCode();
// auto s = ShortCode(ShortCode::Mode::NORMAL);
// auto s = ShortCode(ShortCode::Mode::FAST);
// s.speed_up(ShortCode::Mode::NORMAL);
// s.speed_up(ShortCode::Mode::FAST);
printf("%d\n", s.zip_short_code(0x6EC0F8800));
printf("%09lX\n", s.unzip_short_code(14323231));
// printf("%d\n", s.zip_short_code(0x6EC0F8800));
// printf("%09lX\n", s.unzip_short_code(14323231));
// std::cout << ShortCode::code_to_string(14323231) << std::endl;
// std::cout << ShortCode::code_from_string("EP4HZ") << std::endl;
std::cout << ShortCode::code_to_string(14323231) << std::endl;
std::cout << ShortCode::code_from_string("EP4HZ") << std::endl;
std::cout << CommonCode::code_to_string(0x4FEA13400, true) << std::endl;
std::cout << CommonCode::code_to_string(0x4FEA13400) << std::endl;
return 0;
}

Loading…
Cancel
Save