18 print_created(
this, std::to_string(m_rows) +
"x" + std::to_string(m_cols) +
" matrix");
19 m_data =
new float[(
size_t) (rows*cols)];
20 memset(m_data, 0,
sizeof(
float) * (
size_t) (rows * cols));
23 Matrix(
const Matrix &
s) : m_rows(s.m_rows), m_cols(s.m_cols) {
24 print_copy_created(
this, std::to_string(m_rows) +
"x" + std::to_string(m_cols) +
" matrix");
25 m_data =
new float[(
size_t) (m_rows * m_cols)];
26 memcpy(m_data, s.m_data,
sizeof(
float) * (
size_t) (m_rows * m_cols));
29 Matrix(
Matrix &&s) : m_rows(s.m_rows), m_cols(s.m_cols), m_data(s.m_data) {
37 print_destroyed(
this, std::to_string(m_rows) +
"x" + std::to_string(m_cols) +
" matrix");
42 print_copy_assigned(
this, std::to_string(m_rows) +
"x" + std::to_string(m_cols) +
" matrix");
46 m_data =
new float[(
size_t) (m_rows * m_cols)];
47 memcpy(m_data, s.m_data,
sizeof(
float) * (
size_t) (m_rows * m_cols));
52 print_move_assigned(
this, std::to_string(m_rows) +
"x" + std::to_string(m_cols) +
" matrix");
55 m_rows = s.m_rows; m_cols = s.m_cols; m_data = s.m_data;
56 s.m_rows = 0; s.m_cols = 0; s.m_data =
nullptr;
62 return m_data[(
size_t) (i*m_cols + j)];
66 return m_data[(
size_t) (i*m_cols + j)];
69 float *
data() {
return m_data; }
78 py::class_<Matrix>(
m,
"Matrix", py::buffer_protocol())
79 .def(py::init<ssize_t, ssize_t>())
82 py::buffer_info
info = b.request();
83 if (info.format != py::format_descriptor<float>::format() || info.ndim != 2)
84 throw std::runtime_error(
"Incompatible buffer format!");
86 auto v =
new Matrix(info.shape[0], info.shape[1]);
87 memcpy(
v->data(), info.ptr,
sizeof(
float) * (
size_t) (
v->rows() *
v->cols()));
95 .def(
"__getitem__", [](
const Matrix &m, std::pair<ssize_t, ssize_t>
i) {
96 if (i.first >= m.rows() || i.second >= m.cols())
97 throw py::index_error();
98 return m(i.first, i.second);
100 .def(
"__setitem__", [](
Matrix &m, std::pair<ssize_t, ssize_t> i,
float v) {
101 if (i.first >= m.rows() || i.second >= m.cols())
102 throw py::index_error();
103 m(i.first, i.second) =
v;
106 .def_buffer([](
Matrix &m) -> py::buffer_info {
107 return py::buffer_info(
109 { m.rows(), m.cols() },
110 {
sizeof(
float) *
size_t(m.cols()),
118 class SquareMatrix :
public Matrix {
123 py::class_<SquareMatrix, Matrix>(
m,
"SquareMatrix")
124 .def(py::init<ssize_t>());
133 py::buffer_info get_buffer_info() {
134 return py::buffer_info(&value,
sizeof(value),
135 py::format_descriptor<int32_t>::format(), 1);
138 py::class_<Buffer>(
m,
"Buffer", py::buffer_protocol())
141 .def_buffer(&Buffer::get_buffer_info);
145 std::unique_ptr<int32_t>
value;
149 void set_value(
int32_t v) { *value =
v; }
151 py::buffer_info get_buffer_info()
const {
152 return py::buffer_info(value.get(),
sizeof(*value),
153 py::format_descriptor<int32_t>::format(), 1);
158 py::class_<ConstBuffer>(
m,
"ConstBuffer", py::buffer_protocol())
160 .def_property(
"value", &ConstBuffer::get_value, &ConstBuffer::set_value)
161 .def_buffer(&ConstBuffer::get_buffer_info);
163 struct DerivedBuffer :
public Buffer { };
164 py::class_<DerivedBuffer>(
m,
"DerivedBuffer", py::buffer_protocol())
167 .def_buffer(&DerivedBuffer::get_buffer_info);
169 struct BufferReadOnly {
173 py::buffer_info get_buffer_info() {
174 return py::buffer_info(&value, 1);
177 py::class_<BufferReadOnly>(
m,
"BufferReadOnly", py::buffer_protocol())
178 .def(py::init<uint8_t>())
179 .def_buffer(&BufferReadOnly::get_buffer_info);
181 struct BufferReadOnlySelect {
183 bool readonly =
false;
185 py::buffer_info get_buffer_info() {
186 return py::buffer_info(&value, 1, readonly);
189 py::class_<BufferReadOnlySelect>(
m,
"BufferReadOnlySelect", py::buffer_protocol())
192 .def_readwrite(
"readonly", &BufferReadOnlySelect::readonly)
193 .def_buffer(&BufferReadOnlySelect::get_buffer_info);
void print_destroyed(T *inst, Values &&...values)
void print_copy_assigned(T *inst, Values &&...values)
void print_copy_created(T *inst, Values &&...values)
TEST_SUBMODULE(buffers, m)
void print_move_assigned(T *inst, Values &&...values)
void print_created(T *inst, Values &&...values)
detail::initimpl::constructor< Args... > init()
Binds an existing constructor taking arguments Args...
Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor > Matrix
void print_move_created(T *inst, Values &&...values)