Browse Source

update: enhance ranges module

master
Dnomd343 1 month ago
parent
commit
4121ca229e
  1. 10
      src/core/benchmark/ranges.cc
  2. 25
      src/core/ranges/internal/ranges.cc
  3. 25
      src/core/ranges/internal/ranges.inl
  4. 35
      src/core/ranges/internal/ranges_union.inl
  5. 1
      src/core/ranges/ranges.h

10
src/core/benchmark/ranges.cc

@ -5,6 +5,7 @@
#include "all_cases/all_cases.h" #include "all_cases/all_cases.h"
using klotski::cases::AllCases; using klotski::cases::AllCases;
using klotski::cases::BasicRanges;
static void SpawnRanges(benchmark::State &state) { static void SpawnRanges(benchmark::State &state) {
// constexpr auto nums = target_nums(); // constexpr auto nums = target_nums();
@ -23,6 +24,13 @@ static void SpawnRanges(benchmark::State &state) {
} }
static void RangesReverse(benchmark::State &state) {
auto ranges = BasicRanges::instance().fetch();
for (auto _ : state) {
ranges.reverse();
}
}
static void RangesUnionExport(benchmark::State &state) { static void RangesUnionExport(benchmark::State &state) {
auto &all_cases = AllCases::instance().fetch(); auto &all_cases = AllCases::instance().fetch();
for (auto _ : state) { for (auto _ : state) {
@ -71,6 +79,8 @@ static void RangesAt(benchmark::State &state) {
// BENCHMARK(SpawnRanges)->Unit(benchmark::kMillisecond); // BENCHMARK(SpawnRanges)->Unit(benchmark::kMillisecond);
// BENCHMARK(RangesReverse)->Unit(benchmark::kMillisecond);
// BENCHMARK(RangesUnionExport)->Unit(benchmark::kMillisecond); // BENCHMARK(RangesUnionExport)->Unit(benchmark::kMillisecond);
// BENCHMARK(RangesSize); // BENCHMARK(RangesSize);

25
src/core/ranges/internal/ranges.cc

@ -1,3 +1,5 @@
#include <span>
#include "ranges/ranges.h" #include "ranges/ranges.h"
#include "common_code/common_code.h" #include "common_code/common_code.h"
@ -5,18 +7,10 @@ using klotski::cases::Ranges;
using klotski::codec::CommonCode; using klotski::codec::CommonCode;
using klotski::cases::RangesUnion; using klotski::cases::RangesUnion;
RangesUnion& RangesUnion::operator+=(const RangesUnion &ranges_union) {
for (const auto head : Heads) {
ranges(head) += ranges_union.ranges(head);
}
return *this;
}
std::vector<CommonCode> RangesUnion::codes() const { std::vector<CommonCode> RangesUnion::codes() const {
std::vector<CommonCode> codes; std::vector<CommonCode> codes;
codes.reserve(size()); codes.reserve(size());
for (const auto head : Heads) { for (const auto head : Heads) {
// for (const auto range : (*this)[head]) {
for (const auto range : ranges(head)) { for (const auto range : ranges(head)) {
codes.emplace_back(CommonCode::unsafe_create(head << 32 | range)); codes.emplace_back(CommonCode::unsafe_create(head << 32 | range));
} }
@ -26,26 +20,19 @@ std::vector<CommonCode> RangesUnion::codes() const {
} }
KLSK_INLINE CommonCode RangesUnion::operator[](size_type n) const { KLSK_INLINE CommonCode RangesUnion::operator[](size_type n) const {
// uint64_t head = 0;
// while (n >= ranges(head).size()) {
// n -= ranges(head).size();
// ++head;
// }
// KLSK_ASSUME(head < 16);
// return CommonCode::unsafe_create(head << 32 | ranges(head)[n]);
if (n < ranges(0).size()) { if (n < ranges(0).size()) {
return CommonCode::unsafe_create(ranges(0)[n]); return CommonCode::unsafe_create(ranges(0)[n]);
} }
n -= ranges(0).size(); n -= ranges(0).size();
KLSK_UNROLL(sizeof(Heads) - 2) KLSK_UNROLL(Heads.size() - 2)
for (const uint64_t head : std::to_array({0x1, 0x2, 0x4, 0x5, 0x6, 0x8, 0x9, 0xA, 0xC, 0xD})) { for (const uint64_t head : std::span {Heads.data() + 1, Heads.size() - 2}) {
if (n < ranges(head).size()) { if (n < ranges(head).size()) {
return CommonCode::unsafe_create(head << 32 | ranges(head)[n]); return CommonCode::unsafe_create(head << 32 | ranges(head)[n]);
} }
n -= ranges(head).size(); n -= ranges(head).size();
} }
return CommonCode::unsafe_create((uint64_t)0xE << 32 | ranges(0xE)[n]); constexpr auto head = static_cast<uint64_t>(0xE);
return CommonCode::unsafe_create(head << 32 | ranges(0xE)[n]);
} }

25
src/core/ranges/internal/ranges.inl

@ -3,14 +3,39 @@
namespace klotski::cases { namespace klotski::cases {
KLSK_INLINE_H void Ranges::reverse() { KLSK_INLINE_H void Ranges::reverse() {
#pragma clang loop vectorize(enable)
for (auto &x : *this) { for (auto &x : *this) {
x = range_reverse(x); x = range_reverse(x);
} }
} }
inline Ranges& RangesUnion::ranges(const size_t head) {
return std::array<Ranges, 16>::operator[](head);
}
inline const Ranges& RangesUnion::ranges(const size_t head) const {
return std::array<Ranges, 16>::operator[](head);
}
inline Ranges& Ranges::operator+=(const Ranges &ranges) { inline Ranges& Ranges::operator+=(const Ranges &ranges) {
this->insert(this->end(), ranges.begin(), ranges.end()); this->insert(this->end(), ranges.begin(), ranges.end());
return *this; return *this;
} }
inline RangesUnion& RangesUnion::operator+=(const RangesUnion &ranges_union) {
for (const auto head : Heads) {
ranges(head) += ranges_union.ranges(head);
}
return *this;
}
KLSK_INLINE_H size_t RangesUnion::size() const {
size_type size = 0;
KLSK_UNROLL(sizeof(Heads))
for (const auto head : Heads) {
size += ranges(head).size();
}
return size;
}
} // namespace klotski::cases } // namespace klotski::cases

35
src/core/ranges/internal/ranges_union.inl

@ -1,35 +0,0 @@
#pragma once
namespace klotski::cases {
inline Ranges& RangesUnion::ranges(const size_t head) {
return std::array<Ranges, 16>::operator[](head);
}
inline const Ranges& RangesUnion::ranges(const size_t head) const {
return std::array<Ranges, 16>::operator[](head);
}
KLSK_INLINE_H size_t RangesUnion::size() const {
size_type size = 0;
KLSK_UNROLL(sizeof(Heads))
for (const auto head : Heads) {
size += ranges(head).size();
}
return size;
}
// KLSK_INLINE_H uint32_t RangesUnion::operator[](size_type n) const {
// size_t head = 0;
// for (;;) {
// if (n >= ranges(head).size()) {
// n -= ranges(head).size();
// ++head;
// } else {
// break;
// }
// }
// return ranges(head)[n];
// }
} // namespace klotski::cases

1
src/core/ranges/ranges.h

@ -73,4 +73,3 @@ private:
} // namespace klotski::cases } // namespace klotski::cases
#include "internal/ranges.inl" #include "internal/ranges.inl"
#include "internal/ranges_union.inl"

Loading…
Cancel
Save