8#include <nanobind/nanobind.h>
9#include <nanobind/ndarray.h>
20template <
class T,
class I,
class S, guanaqo::StorageOrder O>
27 { S::value } -> std::convertible_to<I>;
30 using NDArray = ndarray<numpy, T, ndim<2>, device::cpu>;
32 using Contig = std::conditional_t<is_row_major, c_contig, f_contig>;
38 type_caster() : value{{.data =
nullptr, .rows = 0, .cols = 0}} {}
46 cleanup_list *cleanup) noexcept {
47 if (!
caster.from_python(src, flags, cleanup))
50 const I rows =
static_cast<I
>(arr.shape(0)),
51 cols =
static_cast<I
>(arr.shape(1));
52 const I row_stride =
static_cast<I
>(arr.stride(0)),
53 col_stride =
static_cast<I
>(arr.stride(1));
59 if (rows > 0 && cols > 0 && inner_stride != S::value)
63 this->value.reassign({{
67 .outer_stride = outer_stride,
70 this->value.reassign({{
74 .inner_stride = inner_stride,
75 .outer_stride = outer_stride,
83 cleanup_list *cleanup)
noexcept {
84 flags &= ~(uint8_t)cast_flags::accepts_none;
85 const auto flags_no_convert = flags & ~(uint8_t)cast_flags::convert;
86 if (!std::is_const_v<T> || S{} > 1)
88 flags = flags_no_convert;
91 flags_no_convert, cleanup))
93 if (flags == flags_no_convert)
100 static handle
from_cpp(
const Value &view, rv_policy policy,
101 cleanup_list *cleanup)
noexcept {
102 const size_t shape[2]{
static_cast<size_t>(view.rows),
103 static_cast<size_t>(view.cols)};
104 const int64_t strides[2]{
static_cast<int64_t
>(view.row_stride()),
105 static_cast<int64_t
>(view.col_stride())};
106 if (policy == rv_policy::automatic ||
107 policy == rv_policy::automatic_reference)
108 policy = rv_policy::reference;
109 return NDArrayCaster::from_cpp(
110 NDArray(view.data(), 2, shape, handle(), strides), policy, cleanup);
A lightweight view of a 2D matrix.
static constexpr bool is_column_major
static constexpr bool is_row_major
std::conditional_t< is_row_major, c_contig, f_contig > Contig
ndarray< numpy, T, ndim< 2 >, Contig, device::cpu > NDArrayContig
static constexpr bool is_column_major
bool from_python_(auto &caster, handle src, uint8_t flags, cleanup_list *cleanup) noexcept
static constexpr bool is_row_major
NB_TYPE_CASTER(MatrixView, NDArrayCaster::Name)
type_caster< NDArray > NDArrayCaster
guanaqo::MatrixView< T, I, S, O > MatrixView
type_caster(type_caster &&) noexcept=default
ndarray< numpy, T, ndim< 2 >, device::cpu > NDArray
std::variant< NDArrayCaster, NDArrayContigCaster > caster
static constexpr bool has_static_inner_stride
type_caster(const type_caster &)=default
type_caster< NDArrayContig > NDArrayContigCaster
bool from_python(handle src, uint8_t flags, cleanup_list *cleanup) noexcept
static handle from_cpp(const Value &view, rv_policy policy, cleanup_list *cleanup) noexcept