|
@ -1,3 +1,4 @@ |
|
|
|
|
|
#include <format> |
|
|
#include <pybind11/pybind11.h> |
|
|
#include <pybind11/pybind11.h> |
|
|
|
|
|
|
|
|
#include "include/py_cases.h" |
|
|
#include "include/py_cases.h" |
|
@ -6,35 +7,25 @@ namespace py = pybind11; |
|
|
|
|
|
|
|
|
using namespace klotski::ffi; |
|
|
using namespace klotski::ffi; |
|
|
|
|
|
|
|
|
using ShortCodeIter = PyCases::ShortCodeIter; |
|
|
|
|
|
using CommonCodeIter = PyCases::CommonCodeIter; |
|
|
|
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------------------- //
|
|
|
// ----------------------------------------------------------------------------------------- //
|
|
|
|
|
|
|
|
|
CommonCodeIter::CommonCodeIter(const RangesUnion &data) : data_(data) {} |
|
|
PyCasesIter::PyCasesIter(const RangesUnion &data) : data_(data) {} |
|
|
|
|
|
|
|
|
ShortCodeIter::ShortCodeIter(PyCases::CommonCodeIter iter) : iter_(iter) {} |
|
|
|
|
|
|
|
|
|
|
|
PyShortCode ShortCodeIter::next() { |
|
|
PyCommonCode PyCasesIter::next() { |
|
|
return iter_.next().short_code(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
PyCommonCode CommonCodeIter::next() { |
|
|
|
|
|
while (head_ < 16) { |
|
|
while (head_ < 16) { |
|
|
const auto &ranges = data_[head_]; |
|
|
const auto &ranges = data_[head_]; |
|
|
if (index_ < ranges.size()) { |
|
|
if (index_ < ranges.size()) { |
|
|
auto code = (static_cast<uint64_t>(head_) << 32) | ranges[index_++]; |
|
|
auto code = (static_cast<uint64_t>(head_) << 32) | ranges[index_++]; |
|
|
return std::bit_cast<PyCommonCode>(code); |
|
|
return std::bit_cast<PyCommonCode>(code); |
|
|
} |
|
|
} |
|
|
++head_; |
|
|
index_ = 0, ++head_; |
|
|
index_ = 0; |
|
|
|
|
|
} |
|
|
} |
|
|
throw py::stop_iteration(); |
|
|
throw py::stop_iteration(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------------------- //
|
|
|
// ----------------------------------------------------------------------------------------- //
|
|
|
|
|
|
|
|
|
size_t PyCases::size() const { |
|
|
size_t PyCases::size() const noexcept { |
|
|
size_t num = 0; |
|
|
size_t num = 0; |
|
|
for (const auto &x : data_ref()) { // TODO: fetch from RangesUnion.size()
|
|
|
for (const auto &x : data_ref()) { // TODO: fetch from RangesUnion.size()
|
|
|
num += x.size(); |
|
|
num += x.size(); |
|
@ -42,16 +33,11 @@ size_t PyCases::size() const { |
|
|
return num; |
|
|
return num; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
auto PyCases::short_codes() const -> ShortCodeIter { |
|
|
PyCasesIter PyCases::codes() const noexcept { |
|
|
return ShortCodeIter(common_codes()); |
|
|
return PyCasesIter(data_ref()); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
auto PyCases::common_codes() const -> CommonCodeIter { |
|
|
PyCommonCode PyCases::at(size_t index) const { |
|
|
return CommonCodeIter(data_ref()); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
PyCommonCode PyCases::operator[](size_t index) const { |
|
|
|
|
|
|
|
|
|
|
|
if (index >= size()) { |
|
|
if (index >= size()) { |
|
|
throw py::index_error("cases index out of range"); |
|
|
throw py::index_error("cases index out of range"); |
|
|
} |
|
|
} |
|
@ -72,6 +58,10 @@ PyCommonCode PyCases::operator[](size_t index) const { |
|
|
return std::bit_cast<PyCommonCode>(code); |
|
|
return std::bit_cast<PyCommonCode>(code); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
std::string PyCases::repr(const PyCases &cases) noexcept { |
|
|
|
|
|
return std::format("<klotski.Cases size={}>", cases.size()); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------------------- //
|
|
|
// ----------------------------------------------------------------------------------------- //
|
|
|
|
|
|
|
|
|
PyCases PyCases::from(RangesUnion &&data) noexcept { |
|
|
PyCases PyCases::from(RangesUnion &&data) noexcept { |
|
|