Browse Source

update: interface of CommonCode

master
Dnomd343 1 year ago
parent
commit
2d46f9e209
  1. 32
      src/common_code/common_code.cc
  2. 15
      src/common_code/common_code.h
  3. 4
      src/common_code/serialize.cc
  4. 6
      src/main.cc

32
src/common_code/common_code.cc

@ -2,22 +2,34 @@
#include "common.h"
#include "common_code.h"
uint64_t CommonCode::unwrap() const { // get raw uint64_t code
return code;
uint64_t CommonCode::unwrap() const {
return code; // raw uint64_t code
}
RawCode CommonCode::to_raw_code() const { // convert to raw code
return RawCode(*this);
RawCode CommonCode::to_raw_code() const {
return RawCode(*this); // convert to raw code
}
ShortCode CommonCode::to_short_code() const { // convert to short code
return ShortCode(*this);
ShortCode CommonCode::to_short_code() const {
return ShortCode(*this); // convert to short code
}
CommonCode CommonCode::unsafe_create(uint64_t code) { // create CommonCode without check
auto common_code = CommonCode(); // init directly
common_code.code = code;
return common_code;
CommonCode CommonCode::create(uint64_t common_code) {
return CommonCode(common_code); // create from uint64_t
}
CommonCode CommonCode::from_raw_code(const RawCode &raw_code) {
return raw_code.to_common_code(); // create from raw code
}
CommonCode CommonCode::from_short_code(const ShortCode &short_code) {
return short_code.to_common_code(); // create from short code
}
CommonCode CommonCode::unsafe_create(uint64_t common_code) { // create without check
auto common = CommonCode(); // init directly
common.code = common_code;
return common;
}
CommonCode::CommonCode(uint64_t common_code) {

15
src/common_code/common_code.h

@ -11,22 +11,23 @@ class ShortCode;
class CommonCode {
public:
uint64_t unwrap() const;
static bool check(uint64_t common_code);
static CommonCode unsafe_create(uint64_t code);
RawCode to_raw_code() const;
ShortCode to_short_code() const;
std::string to_string(bool shorten = false) const;
static bool check(uint64_t common_code);
// TODO: std::cout << CommonCode(...)
explicit CommonCode(uint64_t common_code);
explicit CommonCode(const RawCode &raw_code);
explicit CommonCode(const ShortCode &short_code);
explicit CommonCode(const std::string &common_code);
// TODO: std::cout << CommonCode(...)
// TODO: CommonCode::create(...) / CommonCode::from_string(...)
// TODO: CommonCode::from_short_code(...) / CommonCode::from_raw_code(...)
static CommonCode create(uint64_t common_code);
static CommonCode unsafe_create(uint64_t common_code);
static CommonCode from_raw_code(const RawCode &raw_code);
static CommonCode from_string(const std::string &common_code);
static CommonCode from_short_code(const ShortCode &short_code);
private:
uint64_t code;

4
src/common_code/serialize.cc

@ -15,6 +15,10 @@ inline uint32_t last_zero_num(uint32_t bin) { // get last zero number
return binary_count(bin >> 1);
}
CommonCode CommonCode::from_string(const std::string &common_code) {
return CommonCode(common_code); // load from string
}
std::string CommonCode::to_string(bool shorten) const { // convert uint64_t code to string
char result[10]; // max length 9-bits
sprintf(result, "%09lX", code);

6
src/main.cc

@ -356,8 +356,10 @@ int main() {
// std::cout << CommonCode::check_demo(0x0000011CE) << std::endl;
// std::cout << CommonCode::check_demo(0x02ED67000) << std::endl;
std::cout << CommonCode("1a9bf0C0").to_string() << std::endl;
std::cout << CommonCode(0x4FEA13400).to_string() << std::endl;
// std::cout << CommonCode("1a9bf0C0").to_string() << std::endl;
// std::cout << CommonCode(0x4FEA13400).to_string() << std::endl;
printf("%09lX\n", CommonCode::from_string("1a9bf0c").unwrap());
// std::cerr << (clock() - start_time) * 1000 / CLOCKS_PER_SEC << "ms" << std::endl;
std::cerr << (clock() - start_time) * 1000000 / CLOCKS_PER_SEC << "us" << std::endl;

Loading…
Cancel
Save