From 034a5fddfb292f22659f293d72ceb576f5c61d82 Mon Sep 17 00:00:00 2001 From: Bent Bisballe Nyeng Date: Thu, 3 Aug 2023 16:18:00 +0200 Subject: A5: code complete --- a5/matrix.cc | 222 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 211 insertions(+), 11 deletions(-) (limited to 'a5/matrix.cc') diff --git a/a5/matrix.cc b/a5/matrix.cc index 01744fa..4a34fa0 100644 --- a/a5/matrix.cc +++ b/a5/matrix.cc @@ -2,26 +2,226 @@ #include "imatrix_nm.h" #include "matrix.h" +#include + +template +std::ostream& operator<<(std::ostream& s, const Matrix& m) +{ + for(auto j = 0u; j < M; ++j) + { + for(auto i = 0u; i < N; ++i) + { + s << m.m(i,j) << ' '; + } + s << '\n'; + } + return s; +} + +template +std::ostream& operator<<(std::ostream& s, const ImatrixNM& m) +{ + for(auto j = 0u; j < M; ++j) + { + for(auto i = 0u; i < N; ++i) + { + s << m.m(i,j) << ' '; + } + s << '\n'; + } + return s; +} + +std::ostream& operator<<(std::ostream& s, const Imatrix& m) +{ + for(auto j = 0u; j < m.h; ++j) + { + for(auto i = 0u; i < m.w; ++i) + { + s << m.m(i,j) << ' '; + } + s << '\n'; + } + return s; +} + struct Chess_piece { + Chess_piece operator*(const Chess_piece&) const {return {};} + Chess_piece operator/(const Chess_piece&) const {return {};} + Chess_piece operator+(const Chess_piece&) const {return {};} + Chess_piece operator-(const Chess_piece&) const {return {};} + + Chess_piece& operator*=(const Chess_piece&){return *this;} + Chess_piece& operator/=(const Chess_piece&){return *this;} + Chess_piece& operator+=(const Chess_piece&){return *this;} + Chess_piece& operator-=(const Chess_piece&){return *this;} + + Chess_piece operator*(int) const {return {};} + Chess_piece operator/(int) const {return {};} + Chess_piece operator+(int) const {return {};} + Chess_piece operator-(int) const {return {};} + + Chess_piece& operator*=(int){return *this;} + Chess_piece& operator/=(int){return *this;} + Chess_piece& operator+=(int){return *this;} + Chess_piece& operator-=(int){return *this;} }; +std::ostream& operator<<(std::ostream& s, const Chess_piece&) +{ + s << "Chess_piece"; + return s; +} + +template +void test(M1& m1, M2& m2) +{ + std::cout << "m1=\n" << m1 << '\n'; + std::cout << "m2=\n" << m2 << '\n'; + + { + auto m3 = m1 * m2; + std::cout << "m1*m2\n" << m3 << '\n'; + } + + { + auto m3 = m1 / m2; + std::cout << "m1/m2\n" << m3 << '\n'; + } + + { + auto m3 = m1 + m1; + std::cout << "m1+m1\n" << m3 << '\n'; + } + + { + auto m3 = m1 - m1; + std::cout << "m1-m1\n" << m3 << '\n'; + } + + if constexpr (supports_modulo) + { + auto m3 = m1 % m1; + std::cout << "m1%m1\n" << m3 << '\n'; + } + + { + auto m3 = m1 * 2; + std::cout << "m1*2\n" << m3 << '\n'; + } + + { + auto m3 = m1 / 2; + std::cout << "m1/2\n" << m3 << '\n'; + } + + { + auto m3 = m1 + 2; + std::cout << "m1+2\n" << m3 << '\n'; + } + + { + auto m3 = m1 - 2; + std::cout << "m1-2\n" << m3 << '\n'; + } + + if constexpr (supports_modulo) + { + auto m3 = m1 % 2; + std::cout << "m1%2\n" << m3 << '\n'; + } + + { + auto m3 = m1; + m3.Move({0,0}, {1,1}); + std::cout << "m3.Move (copy assign)\n" << m3 << '\n'; + } + + { + auto m3(m1); + auto r = m3.Row(0); + std::cout << "m3.Row (copy ctor)\n"; + for(auto i : r) std::cout << i << ' '; + std::cout << "\n\n"; + } + + { + auto m3(std::move(m1)); + auto c = m3.Column(0); + std::cout << "m3.Column (move ctor)\n"; + for(auto i : c) std::cout << i << ' '; + std::cout << "\n\n"; + } + + { + auto m3 = std::move(m2); + std::cout << "m3 (move assign)\n" << m3 << '\n'; + } +} + int main() { - Matrix m1; - m1 = m1 + 2; - m1.m(2,2) = 42; - try + std::cout << "Imatrix:\n"; { - m1.m(6,6) = 0; + Imatrix m1(3,2); + m1.m(0,0) = 1; m1.m(1,0) = 2; m1.m(2,0) = 3; + m1.m(0,1) = 4; m1.m(1,1) = 5; m1.m(2,1) = 6; + + Imatrix m2(2,3); + m2.m(0,0) = 1; m2.m(1,0) = 2; + m2.m(0,1) = 3; m2.m(1,1) = 4; + m2.m(0,2) = 5; m2.m(1,2) = 6; + + test(m1, m2); } - catch(...) + + std::cout << "ImatrixNM<..,..>:\n"; { - // expected + ImatrixNM<3, 2> m1; + m1.m(0,0) = 1; m1.m(1,0) = 2; m1.m(2,0) = 3; + m1.m(0,1) = 4; m1.m(1,1) = 5; m1.m(2,1) = 6; + + ImatrixNM<2, 3> m2; + m2.m(0,0) = 1; m2.m(1,0) = 2; + m2.m(0,1) = 3; m2.m(1,1) = 4; + m2.m(0,2) = 5; m2.m(1,2) = 6; + + test(m1, m2); } - Matrix m2; - auto m3 = m1 * m2; - Matrix c1; - auto c2 = c1 + 42; + std::cout << "Matrix:\n"; + { + Matrix m1; + m1.m(0,0) = 1; m1.m(1,0) = 2; m1.m(2,0) = 3; + m1.m(0,1) = 4; m1.m(1,1) = 5; m1.m(2,1) = 6; + + Matrix m2; + m2.m(0,0) = 1; m2.m(1,0) = 2; + m2.m(0,1) = 3; m2.m(1,1) = 4; + m2.m(0,2) = 5; m2.m(1,2) = 6; + + test(m1, m2); + } + + std::cout << "Matrix:\n"; + { + Matrix m1; + m1.m(0,0) = 1; m1.m(1,0) = 2; m1.m(2,0) = 3; + m1.m(0,1) = 4; m1.m(1,1) = 5; m1.m(2,1) = 6; + + Matrix m2; + m2.m(0,0) = 1; m2.m(1,0) = 2; + m2.m(0,1) = 3; m2.m(1,1) = 4; + m2.m(0,2) = 5; m2.m(1,2) = 6; + + test(m1, m2); + } + + std::cout << "Matrix:\n"; + { + Matrix m1; + Matrix m2; + test(m1, m2); + } } -- cgit v1.2.3