From bce32d45a506663592cc48bf440b05ff19155d84 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Sat, 15 Jun 2024 14:20:49 +0800 Subject: [PATCH] feat: operator overloading for append --- src/core/ranges/internal/ranges.cc | 24 +++++++++++++++++------- src/core/ranges/ranges.h | 8 +++++++- src/core_test/cases/ranges_union.cc | 0 3 files changed, 24 insertions(+), 8 deletions(-) create mode 100644 src/core_test/cases/ranges_union.cc diff --git a/src/core/ranges/internal/ranges.cc b/src/core/ranges/internal/ranges.cc index 29ae229..ae1c025 100644 --- a/src/core/ranges/internal/ranges.cc +++ b/src/core/ranges/internal/ranges.cc @@ -5,20 +5,30 @@ using klotski::cases::Ranges; using klotski::codec::CommonCode; using klotski::cases::RangesUnion; +static constexpr auto heads = std::to_array({ + 0x0, 0x1, 0x2, 0x4, 0x5, 0x6, + 0x8, 0x9, 0xA, 0xC, 0xD, 0xE, +}); + void Ranges::reverse() { for (auto &x : *this) { x = range_reverse(x); } } -std::vector RangesUnion::codes() const { - constexpr auto heads = std::to_array({ - 0x0, 0x1, 0x2, - 0x4, 0x5, 0x6, - 0x8, 0x9, 0xA, - 0xC, 0xD, 0xE, - }); +Ranges& Ranges::operator+=(const Ranges &ranges) { + this->insert(this->end(), ranges.begin(), ranges.end()); + return *this; +} + +RangesUnion& RangesUnion::operator+=(const RangesUnion &ranges_union) { + for (const auto head : heads) { + (*this)[head] += ranges_union[head]; + } + return *this; +} +std::vector RangesUnion::codes() const { size_type size = 0; for (const auto head : heads) { size += (*this)[head].size(); diff --git a/src/core/ranges/ranges.h b/src/core/ranges/ranges.h index 7c4a212..83f2f0a 100644 --- a/src/core/ranges/ranges.h +++ b/src/core/ranges/ranges.h @@ -14,6 +14,9 @@ namespace klotski::cases { class Ranges final : public std::vector { public: + /// Append the ranges from another instance. + Ranges& operator+=(const Ranges &ranges); + /// Spawn klotski-ranges that match the specified block numbers. void spawn(int n, int n_2x1, int n_1x1); @@ -29,7 +32,10 @@ public: class RangesUnion final : public std::array { public: - /// Export RangesUnion as CommonCode list. + /// Append the ranges from another instance. + RangesUnion& operator+=(const RangesUnion &ranges_union); + + /// Export the RangesUnion as CommonCode list. [[nodiscard]] std::vector codes() const; }; diff --git a/src/core_test/cases/ranges_union.cc b/src/core_test/cases/ranges_union.cc new file mode 100644 index 0000000..e69de29