From c438e2ba9b4fe4164f6bd4b5fba4dc1403d676c7 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Sat, 4 May 2024 10:26:14 +0800 Subject: [PATCH] update: cpp style optimization of all cases --- src/core/all_cases/all_cases.cc | 13 ++++++------- src/core/all_cases/all_cases.h | 17 +++++++++-------- src/core/all_cases/basic_ranges.cc | 11 +++++------ .../{inline_impl.h => inline_impl.inl} | 6 ++---- src/core/utils/utility.h | 16 ++++++++-------- 5 files changed, 30 insertions(+), 33 deletions(-) rename src/core/all_cases/{inline_impl.h => inline_impl.inl} (93%) diff --git a/src/core/all_cases/all_cases.cc b/src/core/all_cases/all_cases.cc index 3a1af1b..9af44f4 100644 --- a/src/core/all_cases/all_cases.cc +++ b/src/core/all_cases/all_cases.cc @@ -1,8 +1,8 @@ #include + #include "all_cases.h" -namespace klotski { -namespace cases { +namespace klotski::cases { /// Calculate all possible klotski heads. consteval static std::array case_heads() { @@ -16,7 +16,7 @@ consteval static std::array case_heads() { } /// Check whether the combination of head and range is valid. -static int check_range(int head, uint32_t range) noexcept { +static int check_range(const int head, uint32_t range) noexcept { constexpr uint32_t M_1x1 = 0b00000001; constexpr uint32_t M_1x2 = 0b00000011; constexpr uint32_t M_2x1 = 0b00010001; @@ -24,7 +24,7 @@ static int check_range(int head, uint32_t range) noexcept { uint32_t flags = M_2x2 << head; // fill 2x2 block for (int addr = 0, offset = 1; range; range >>= 2, ++offset) { // traverse every 2-bit - auto num = low_zero_num(~flags); + const auto num = low_zero_num(~flags); addr += num; // next unfilled block flags >>= num; switch (range & 0b11) { @@ -50,7 +50,7 @@ static int check_range(int head, uint32_t range) noexcept { } /// Build all valid ranges of the specified head. -void AllCases::build_cases(int head, Ranges &release) noexcept { +void AllCases::build_cases(const int head, Ranges &release) noexcept { release.clear(); release.reserve(ALL_CASES_NUM[head]); auto &basic_ranges = BasicRanges::instance().fetch(); @@ -127,5 +127,4 @@ void AllCases::build_parallel_async(Executor &&executor, Notifier &&callback) no } } -} // namespace cases -} // namespace klotski +} // namespace klotski::cases diff --git a/src/core/all_cases/all_cases.h b/src/core/all_cases/all_cases.h index 21f0412..1503912 100644 --- a/src/core/all_cases/all_cases.h +++ b/src/core/all_cases/all_cases.h @@ -41,10 +41,12 @@ #include #include #include + #include "utility.h" -namespace klotski { -namespace cases { +namespace klotski::cases { + +// ----------------------------------------------------------------------------------------- // typedef uint32_t Range; typedef std::vector Ranges; @@ -57,7 +59,7 @@ typedef std::function&&)> Executor; constexpr auto BASIC_RANGES_NUM = 7311921; -constexpr std::array ALL_CASES_NUM { +constexpr std::array ALL_CASES_NUM { 2942906, 2260392, 2942906, 0, 2322050, 1876945, 2322050, 0, 2322050, 1876945, 2322050, 0, @@ -76,7 +78,7 @@ public: const Ranges& fetch() noexcept; [[nodiscard]] bool is_available() const noexcept; - DISALLOW_COPY_AND_ASSIGN(BasicRanges); + MARK_INSTANCE(BasicRanges); static BasicRanges& instance() noexcept; private: @@ -100,7 +102,7 @@ public: const RangesUnion& fetch() noexcept; [[nodiscard]] bool is_available() const noexcept; - DISALLOW_COPY_AND_ASSIGN(AllCases); + MARK_INSTANCE(AllCases); static AllCases& instance() noexcept; private: @@ -114,7 +116,6 @@ private: // ----------------------------------------------------------------------------------------- // -} // namespace cases -} // namespace klotski +} // namespace klotski::cases -#include "inline_impl.h" +#include "inline_impl.inl" diff --git a/src/core/all_cases/basic_ranges.cc b/src/core/all_cases/basic_ranges.cc index 114ed0d..209e26c 100644 --- a/src/core/all_cases/basic_ranges.cc +++ b/src/core/all_cases/basic_ranges.cc @@ -1,9 +1,9 @@ #include #include + #include "all_cases.h" -namespace klotski { -namespace cases { +namespace klotski::cases { typedef std::vector::iterator RangeIter; typedef std::tuple RangeType; @@ -52,7 +52,7 @@ void BasicRanges::spawn_ranges(Ranges &ranges, int n1, int n2, int n3, int n4) n do { // full permutation traversal uint32_t range = 0; - for (auto x : series) // store every 2-bit + for (const auto x : series) // store every 2-bit (range <<= 2) |= x; ranges.emplace_back(range << offset); } while (next_permutation(series.begin(), series.end())); @@ -87,7 +87,7 @@ void BasicRanges::build() noexcept { if (available_) { return; // reduce consumption of mutex } - std::lock_guard guard(building_); + std::lock_guard guard(building_); if (available_) { return; // data is already available } @@ -95,5 +95,4 @@ void BasicRanges::build() noexcept { available_ = true; } -} // namespace cases -} // namespace klotski +} // namespace klotski::cases diff --git a/src/core/all_cases/inline_impl.h b/src/core/all_cases/inline_impl.inl similarity index 93% rename from src/core/all_cases/inline_impl.h rename to src/core/all_cases/inline_impl.inl index fe44af0..151fbc6 100644 --- a/src/core/all_cases/inline_impl.h +++ b/src/core/all_cases/inline_impl.inl @@ -1,7 +1,6 @@ #pragma once -namespace klotski { -namespace cases { +namespace klotski::cases { // ----------------------------------------------------------------------------------------- // @@ -47,5 +46,4 @@ inline bool AllCases::is_available() const noexcept { // ----------------------------------------------------------------------------------------- // -} // namespace codec -} // namespace klotski +} // namespace klotski::codec diff --git a/src/core/utils/utility.h b/src/core/utils/utility.h index 03c57f5..40264e6 100644 --- a/src/core/utils/utility.h +++ b/src/core/utils/utility.h @@ -2,15 +2,15 @@ #include -#define DISALLOW_COPY_AND_ASSIGN(T) \ - T(T&&) = delete; \ - T(const T&) = delete; \ - T& operator=(T&&) = delete; \ - T& operator=(const T&) = delete; +#define MARK_INSTANCE(T) \ + T(T &&) = delete; \ + T(const T &) = delete; \ + T& operator=(T &&) = delete; \ + T& operator=(const T &) = delete; namespace klotski { -inline int low_zero_num(uint32_t bin) { +inline int low_zero_num(const uint32_t bin) { return __builtin_ctzl(bin); // TODO: using (bin ^ (bin - 1)) when non-builtin @@ -19,14 +19,14 @@ inline int low_zero_num(uint32_t bin) { // return __builtin_popcount(~(bin ^ -bin)) - 1; } -inline int low_zero_num(uint64_t bin) { +inline int low_zero_num(const uint64_t bin) { return __builtin_ctzll(bin); // WARN: be aware of serious performance issues // return __builtin_popcount(~(bin ^ -bin)) - 1; } -inline uint32_t range_reverse(uint32_t bin) noexcept { +inline uint32_t range_reverse(uint32_t bin) { #if defined(__GNUC__) || defined(__clang__) bin = __builtin_bswap32(bin); // TODO: using `std::byteswap` (c++23)