Browse Source

feat: operator overloading for append

legacy
Dnomd343 7 months ago
parent
commit
bce32d45a5
  1. 24
      src/core/ranges/internal/ranges.cc
  2. 8
      src/core/ranges/ranges.h
  3. 0
      src/core_test/cases/ranges_union.cc

24
src/core/ranges/internal/ranges.cc

@ -5,20 +5,30 @@ using klotski::cases::Ranges;
using klotski::codec::CommonCode; using klotski::codec::CommonCode;
using klotski::cases::RangesUnion; using klotski::cases::RangesUnion;
static constexpr auto heads = std::to_array<uint64_t>({
0x0, 0x1, 0x2, 0x4, 0x5, 0x6,
0x8, 0x9, 0xA, 0xC, 0xD, 0xE,
});
void Ranges::reverse() { void Ranges::reverse() {
for (auto &x : *this) { for (auto &x : *this) {
x = range_reverse(x); x = range_reverse(x);
} }
} }
std::vector<CommonCode> RangesUnion::codes() const { Ranges& Ranges::operator+=(const Ranges &ranges) {
constexpr auto heads = std::to_array<uint64_t>({ this->insert(this->end(), ranges.begin(), ranges.end());
0x0, 0x1, 0x2, return *this;
0x4, 0x5, 0x6, }
0x8, 0x9, 0xA,
0xC, 0xD, 0xE, RangesUnion& RangesUnion::operator+=(const RangesUnion &ranges_union) {
}); for (const auto head : heads) {
(*this)[head] += ranges_union[head];
}
return *this;
}
std::vector<CommonCode> RangesUnion::codes() const {
size_type size = 0; size_type size = 0;
for (const auto head : heads) { for (const auto head : heads) {
size += (*this)[head].size(); size += (*this)[head].size();

8
src/core/ranges/ranges.h

@ -14,6 +14,9 @@ namespace klotski::cases {
class Ranges final : public std::vector<uint32_t> { class Ranges final : public std::vector<uint32_t> {
public: public:
/// Append the ranges from another instance.
Ranges& operator+=(const Ranges &ranges);
/// Spawn klotski-ranges that match the specified block numbers. /// Spawn klotski-ranges that match the specified block numbers.
void spawn(int n, int n_2x1, int n_1x1); void spawn(int n, int n_2x1, int n_1x1);
@ -29,7 +32,10 @@ public:
class RangesUnion final : public std::array<Ranges, 16> { class RangesUnion final : public std::array<Ranges, 16> {
public: 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<codec::CommonCode> codes() const; [[nodiscard]] std::vector<codec::CommonCode> codes() const;
}; };

0
src/core_test/cases/ranges_union.cc

Loading…
Cancel
Save