From 1e219d0eef6fb81b6f035063385eee741d3d95ff Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Sun, 26 Feb 2023 15:35:01 +0800 Subject: [PATCH] feat: add more operator overloads --- src/klotski_core/common_code/common_code.cc | 8 ++++---- src/klotski_core/common_code/common_code.h | 7 ++++++- src/klotski_core/raw_code/raw_code.cc | 4 ---- src/klotski_core/raw_code/raw_code.h | 6 +++++- src/klotski_core/short_code/short_code.cc | 10 +++++----- src/klotski_core/short_code/short_code.h | 7 ++++++- test/codec/common_code.cc | 10 ++++++++-- test/codec/raw_code.cc | 10 ++++++++-- test/codec/short_code.cc | 10 ++++++++-- 9 files changed, 50 insertions(+), 22 deletions(-) diff --git a/src/klotski_core/common_code/common_code.cc b/src/klotski_core/common_code/common_code.cc index 198a8d6..7530e35 100644 --- a/src/klotski_core/common_code/common_code.cc +++ b/src/klotski_core/common_code/common_code.cc @@ -20,14 +20,14 @@ namespace std { } namespace klotski { - bool CommonCode::operator==(uint64_t common_code) const { - return this->code == common_code; - } - bool CommonCode::operator==(const CommonCode &common_code) const { return this->code == common_code.code; } + bool CommonCode::operator!=(const CommonCode &common_code) const { + return this->code != common_code.code; + } + std::ostream& operator<<(std::ostream &out, const CommonCode &self) { char str[10]; sprintf(str, "%09lX", self.code); diff --git a/src/klotski_core/common_code/common_code.h b/src/klotski_core/common_code/common_code.h index e82dca3..13f4597 100644 --- a/src/klotski_core/common_code/common_code.h +++ b/src/klotski_core/common_code/common_code.h @@ -82,8 +82,8 @@ namespace klotski { static bool check(uint64_t common_code); /// Operators of CommonCode - bool operator==(uint64_t common_code) const; bool operator==(const CommonCode &common_code) const; + bool operator!=(const CommonCode &common_code) const; constexpr explicit operator uint64_t() const { return code; } friend std::ostream& operator<<(std::ostream &out, const CommonCode &self); @@ -119,4 +119,9 @@ namespace klotski { static CommonCode from_short_code(const ShortCode &short_code); static CommonCode from_short_code(const std::string &short_code); }; + + inline bool operator==(uint64_t c1, const CommonCode &c2) { return c1 == c2.unwrap(); } + inline bool operator!=(uint64_t c1, const CommonCode &c2) { return c1 != c2.unwrap(); } + inline bool operator==(const CommonCode &c1, uint64_t c2) { return c1.unwrap() == c2; } + inline bool operator!=(const CommonCode &c1, uint64_t c2) { return c1.unwrap() != c2; } } diff --git a/src/klotski_core/raw_code/raw_code.cc b/src/klotski_core/raw_code/raw_code.cc index a486aed..a9e24e7 100644 --- a/src/klotski_core/raw_code/raw_code.cc +++ b/src/klotski_core/raw_code/raw_code.cc @@ -20,10 +20,6 @@ namespace std { } namespace klotski { - bool RawCode::operator==(uint64_t raw_code) const { - return this->code == raw_code; - } - bool RawCode::operator==(const RawCode &raw_code) const { return this->code == raw_code.code; } diff --git a/src/klotski_core/raw_code/raw_code.h b/src/klotski_core/raw_code/raw_code.h index 40852c4..0de9378 100644 --- a/src/klotski_core/raw_code/raw_code.h +++ b/src/klotski_core/raw_code/raw_code.h @@ -72,7 +72,6 @@ namespace klotski { static bool check(uint64_t raw_code); /// Operators of RawCode - bool operator==(uint64_t raw_code) const; bool operator==(const RawCode &raw_code) const; bool operator!=(const RawCode &raw_code) const; constexpr explicit operator uint64_t() const { return code; } @@ -109,4 +108,9 @@ namespace klotski { bool is_horizontal_mirror(RawCode &&raw_code) const; // whether horizontally symmetric to another bool is_horizontal_mirror(const RawCode &raw_code) const; }; + + inline bool operator==(uint64_t r1, const RawCode &r2) { return r1 == r2.unwrap(); } + inline bool operator!=(uint64_t r1, const RawCode &r2) { return r1 != r2.unwrap(); } + inline bool operator==(const RawCode &r1, uint64_t r2) { return r1.unwrap() == r2; } + inline bool operator!=(const RawCode &r1, uint64_t r2) { return r1.unwrap() != r2; } } diff --git a/src/klotski_core/short_code/short_code.cc b/src/klotski_core/short_code/short_code.cc index 711253d..cde2eb1 100644 --- a/src/klotski_core/short_code/short_code.cc +++ b/src/klotski_core/short_code/short_code.cc @@ -23,15 +23,15 @@ namespace std { } namespace klotski { - bool ShortCode::operator==(uint32_t short_code) const { - return this->code == short_code; - } - bool ShortCode::operator==(const ShortCode &short_code) const { return this->code == short_code.code; } - std::ostream &operator<<(std::ostream &out, const ShortCode &self) { + bool ShortCode::operator!=(const ShortCode &short_code) const { + return this->code != short_code.code; + } + + std::ostream& operator<<(std::ostream &out, const ShortCode &self) { out << self.to_string() << "(" << self.code << ")"; // short code info return out; } diff --git a/src/klotski_core/short_code/short_code.h b/src/klotski_core/short_code/short_code.h index 6769047..28f0dc9 100644 --- a/src/klotski_core/short_code/short_code.h +++ b/src/klotski_core/short_code/short_code.h @@ -97,8 +97,8 @@ namespace klotski { static void speed_up(Mode mode); /// Operators of ShortCode - bool operator==(uint32_t short_code) const; bool operator==(const ShortCode &short_code) const; + bool operator!=(const ShortCode &short_code) const; constexpr explicit operator uint32_t() const { return code; } friend std::ostream& operator<<(std::ostream &out, const ShortCode &self); @@ -127,4 +127,9 @@ namespace klotski { static ShortCode from_common_code(const CommonCode &common_code); static ShortCode from_common_code(const std::string &common_code); }; + + inline bool operator==(uint32_t s1, const ShortCode &s2) { return s1 == s2.unwrap(); } + inline bool operator!=(uint32_t s1, const ShortCode &s2) { return s1 != s2.unwrap(); } + inline bool operator==(const ShortCode &s1, uint32_t s2) { return s1.unwrap() == s2; } + inline bool operator!=(const ShortCode &s1, uint32_t s2) { return s1.unwrap() != s2; } } diff --git a/test/codec/common_code.cc b/test/codec/common_code.cc index 28b31c8..d659e5a 100644 --- a/test/codec/common_code.cc +++ b/test/codec/common_code.cc @@ -88,9 +88,15 @@ TEST(CommonCode, operators) { std::cout.setstate(std::ios::failbit); // hide std::cout content std::cout << "TEST OUTPUT -> " << CommonCode(TEST_CODE) << std::endl; // ostream test std::cout.clear(); - EXPECT_EQ(CommonCode(TEST_CODE), TEST_CODE); // operator `==` - EXPECT_EQ(CommonCode(TEST_CODE), CommonCode(TEST_CODE)); // operator `==` EXPECT_EQ((uint64_t)CommonCode(TEST_CODE), TEST_CODE); // convert as uint64_t + + EXPECT_EQ(TEST_CODE, CommonCode(TEST_CODE)); // uint64_t == CommonCode + EXPECT_EQ(CommonCode(TEST_CODE), TEST_CODE); // CommonCode == uint64_t + EXPECT_EQ(CommonCode(TEST_CODE), CommonCode(TEST_CODE)); // CommonCode == CommonCode + + EXPECT_NE(TEST_CODE + 1, CommonCode(TEST_CODE)); // uint64_t != CommonCode + EXPECT_NE(CommonCode(TEST_CODE), TEST_CODE + 1); // CommonCode != uint64_t + EXPECT_NE(CommonCode(TEST_CODE), CommonCode::unsafe_create(TEST_CODE + 1)); // CommonCode != CommonCode } TEST(CommonCode, code_convert) { diff --git a/test/codec/raw_code.cc b/test/codec/raw_code.cc index dd0245c..058be4b 100644 --- a/test/codec/raw_code.cc +++ b/test/codec/raw_code.cc @@ -36,9 +36,15 @@ TEST(RawCode, operators) { std::cout.setstate(std::ios::failbit); // hide std::cout content std::cout << "TEST OUTPUT" << std::endl << RawCode(TEST_CODE); // ostream test std::cout.clear(); - EXPECT_EQ(RawCode(TEST_CODE), TEST_CODE); // operator `==` - EXPECT_EQ(RawCode(TEST_CODE), RawCode(TEST_CODE)); // operator `==` EXPECT_EQ((uint64_t)RawCode(TEST_CODE), TEST_CODE); // convert as uint64_t + + EXPECT_EQ(TEST_CODE, RawCode(TEST_CODE)); // uint64_t == RawCode + EXPECT_EQ(RawCode(TEST_CODE), TEST_CODE); // RawCode == uint64_t + EXPECT_EQ(RawCode(TEST_CODE), RawCode(TEST_CODE)); // RawCode == RawCode + + EXPECT_NE(TEST_CODE + 1, RawCode(TEST_CODE)); // uint64_t != RawCode + EXPECT_NE(RawCode(TEST_CODE), TEST_CODE + 1); // RawCode != uint64_t + EXPECT_NE(RawCode(TEST_CODE), RawCode::unsafe_create(TEST_CODE + 1)); // RawCode != RawCode } TEST(RawCode, code_convert) { diff --git a/test/codec/short_code.cc b/test/codec/short_code.cc index 8d70047..4c118b7 100644 --- a/test/codec/short_code.cc +++ b/test/codec/short_code.cc @@ -109,9 +109,15 @@ TEST(ShortCode, operators) { std::cout.setstate(std::ios::failbit); // hide std::cout content std::cout << "TEST OUTPUT -> " << ShortCode(TEST_CODE) << std::endl; // ostream test std::cout.clear(); - EXPECT_EQ(ShortCode(TEST_CODE), TEST_CODE); // operator `==` - EXPECT_EQ(ShortCode(TEST_CODE), ShortCode(TEST_CODE)); // operator `==` EXPECT_EQ((uint32_t)ShortCode(TEST_CODE), TEST_CODE); // convert as uint64_t + + EXPECT_EQ(TEST_CODE, ShortCode(TEST_CODE)); // uint32_t == ShortCode + EXPECT_EQ(ShortCode(TEST_CODE), TEST_CODE); // ShortCode == uint32_t + EXPECT_EQ(ShortCode(TEST_CODE), ShortCode(TEST_CODE)); // ShortCode == ShortCode + + EXPECT_NE(TEST_CODE + 1, ShortCode(TEST_CODE)); // uint32_t != ShortCode + EXPECT_NE(ShortCode(TEST_CODE), TEST_CODE + 1); // ShortCode != uint32_t + EXPECT_NE(ShortCode(TEST_CODE), ShortCode::unsafe_create(TEST_CODE + 1)); // ShortCode != ShortCode } TEST(ShortCode, code_convert) {