MCPcopy Create free account
hub / github.com/pybind/pybind11 / Matrix

Class Matrix

tests/test_buffers.cpp:52–128  ·  view source on GitHub ↗

test_from_python / test_to_python:

Source from the content-addressed store, hash-verified

50
51 // test_from_python / test_to_python:
52 class Matrix {
53 public:
54 Matrix(py::ssize_t rows, py::ssize_t cols) : m_rows(rows), m_cols(cols) {
55 print_created(this, std::to_string(m_rows) + "x" + std::to_string(m_cols) + " matrix");
56 // NOLINTNEXTLINE(cppcoreguidelines-prefer-member-initializer)
57 m_data = new float[(size_t) (rows * cols)];
58 memset(m_data, 0, sizeof(float) * (size_t) (rows * cols));
59 }
60
61 Matrix(const Matrix &s) : m_rows(s.m_rows), m_cols(s.m_cols) {
62 print_copy_created(this,
63 std::to_string(m_rows) + "x" + std::to_string(m_cols) + " matrix");
64 // NOLINTNEXTLINE(cppcoreguidelines-prefer-member-initializer)
65 m_data = new float[(size_t) (m_rows * m_cols)];
66 memcpy(m_data, s.m_data, sizeof(float) * (size_t) (m_rows * m_cols));
67 }
68
69 Matrix(Matrix &&s) noexcept : m_rows(s.m_rows), m_cols(s.m_cols), m_data(s.m_data) {
70 print_move_created(this);
71 s.m_rows = 0;
72 s.m_cols = 0;
73 s.m_data = nullptr;
74 }
75
76 ~Matrix() {
77 print_destroyed(this,
78 std::to_string(m_rows) + "x" + std::to_string(m_cols) + " matrix");
79 delete[] m_data;
80 }
81
82 Matrix &operator=(const Matrix &s) {
83 if (this == &s) {
84 return *this;
85 }
86 print_copy_assigned(this,
87 std::to_string(m_rows) + "x" + std::to_string(m_cols) + " matrix");
88 delete[] m_data;
89 m_rows = s.m_rows;
90 m_cols = s.m_cols;
91 m_data = new float[(size_t) (m_rows * m_cols)];
92 memcpy(m_data, s.m_data, sizeof(float) * (size_t) (m_rows * m_cols));
93 return *this;
94 }
95
96 Matrix &operator=(Matrix &&s) noexcept {
97 print_move_assigned(this,
98 std::to_string(m_rows) + "x" + std::to_string(m_cols) + " matrix");
99 if (&s != this) {
100 delete[] m_data;
101 m_rows = s.m_rows;
102 m_cols = s.m_cols;
103 m_data = s.m_data;
104 s.m_rows = 0;
105 s.m_cols = 0;
106 s.m_data = nullptr;
107 }
108 return *this;
109 }

Callers

nothing calls this directly

Calls 2

print_copy_assignedFunction · 0.85
print_move_assignedFunction · 0.85

Tested by

no test coverage detected