From e9cdc77d13ebe98f343398322438e60f776ca48e Mon Sep 17 00:00:00 2001 From: Yohan Boujon Date: Tue, 12 Dec 2023 19:46:02 +0100 Subject: [PATCH] TP2: Partie 2, Question1. --- tp/tp2/include/partie2.h | 48 ++++++++++++++++++ tp/tp2/src/main.cpp | 24 +++++++++ tp/tp2/src/partie2.cpp | 106 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 178 insertions(+) create mode 100644 tp/tp2/include/partie2.h create mode 100644 tp/tp2/src/partie2.cpp diff --git a/tp/tp2/include/partie2.h b/tp/tp2/include/partie2.h new file mode 100644 index 0000000..e69636e --- /dev/null +++ b/tp/tp2/include/partie2.h @@ -0,0 +1,48 @@ +#ifndef HEADER_PARTIE2 +#define HEADER_PARTIE2 + +#include +#include +#include +#include + +struct position { + uint16_t x; + uint16_t y; + + /** + * @brief Used by std::map. + * + * @param other the other position to check. + * @return true Returns & values); + MatriceCreuse(uint16_t lines, uint16_t columns, const std::vector& positions, int64_t value); + ~MatriceCreuse(); + friend std::ostream& operator<<(std::ostream& stream, MatriceCreuse matrice); + int64_t& operator()(uint16_t x, uint16_t y); + int64_t& operator[](position pos); + MatriceCreuse operator*(int64_t val); + MatriceCreuse operator+(int64_t val); + MatriceCreuse operator-(int64_t val); + MatriceCreuse operator+(MatriceCreuse other); + MatriceCreuse operator-(MatriceCreuse other); + MatriceCreuse& operator+=(int64_t val); + private: + void addValue(position pos, int64_t value); + bool isSameDimension(const MatriceCreuse& other); + + uint16_t _line; + uint16_t _column; + std::map _values; +}; + +#endif // HEADER_PARTIE2 \ No newline at end of file diff --git a/tp/tp2/src/main.cpp b/tp/tp2/src/main.cpp index c81db3f..0145a1a 100644 --- a/tp/tp2/src/main.cpp +++ b/tp/tp2/src/main.cpp @@ -7,6 +7,7 @@ #include #include #include +#include "../include/partie2.h" void question1(const uint maxVec); void question2(const uint max); @@ -23,6 +24,29 @@ int main(void) question4(); question5(); question6(); + + // Partie2 + MatriceCreuse m(5,5,{{0,2},{0,1}},2); + std::cout << m; + std::cout << m(0,2) << std::endl; + std::cout << m[{0,5}] << std::endl; + m(0,0) = 5; + std::cout << m << std::endl; + auto m2 = m+5; + std::cout << m2 << std::endl; + std::cout << m+m2 << std::endl; + std::cout << m2-m << std::endl; + MatriceCreuse m3(3,4,{{2,2}},5); + try { + std::cout << m+m3; + } + catch(const char* err) + { + std::cerr << err << std::endl; + } + std::cout << m << std::endl; + m+=1; + std::cout << m; return 0; } diff --git a/tp/tp2/src/partie2.cpp b/tp/tp2/src/partie2.cpp new file mode 100644 index 0000000..0df79bd --- /dev/null +++ b/tp/tp2/src/partie2.cpp @@ -0,0 +1,106 @@ +#include "../include/partie2.h" + +MatriceCreuse::MatriceCreuse(uint16_t lines, uint16_t columns) + : _line(lines) + , _column(columns) +{} + +MatriceCreuse::MatriceCreuse(uint16_t lines, uint16_t columns, const std::map& values) + : MatriceCreuse(lines,columns) +{ + for (const auto& val : values) + addValue(val.first,val.second); +} + +MatriceCreuse::MatriceCreuse(uint16_t lines, uint16_t columns, const std::vector& positions, int64_t value) + : MatriceCreuse(lines,columns) +{ + for(auto pos : positions) + addValue(pos,value); +} + +MatriceCreuse::~MatriceCreuse() {} + +std::ostream& operator<<(std::ostream& stream, MatriceCreuse matrice) +{ + for(uint16_t x(0); x < matrice._column; x++) + { + for(uint16_t y(0); y < matrice._line; y++) + { + int64_t value = matrice._values.find({x,y}) != matrice._values.end() ? matrice._values[{x,y}] : 0 ; + stream << "[" << value << "] "; + } + stream << "\n"; + } + return stream; +} + +int64_t& MatriceCreuse::operator()(uint16_t x, uint16_t y) +{ + if (_values.find({x,y}) == _values.end()) + _values[{x,y}] = 0; + return _values[{x,y}]; +} + +int64_t& MatriceCreuse::operator[](position pos) { return (*this)(pos.x,pos.y); } + +MatriceCreuse MatriceCreuse::operator+(int64_t val) +{ + auto newMatrix = *this; + for(uint16_t x(0); x < _column; x++) + { + for(uint16_t y(0); y < _line; y++) + newMatrix(x,y) = newMatrix(x,y) + val; + } + return newMatrix; +} + +MatriceCreuse MatriceCreuse::operator-(int64_t val) +{ + return (*this)+(-val); +} + +MatriceCreuse MatriceCreuse::operator+(MatriceCreuse other) +{ + if(!this->isSameDimension(other)) + throw "Could not '+' two matrixes with different dimensions!"; + auto newMatrix = *this; + for(uint16_t x(0); x < _column; x++) + { + for(uint16_t y(0); y < _line; y++) + newMatrix(x,y) = (*this)(x,y) + other(x,y); + } + return newMatrix; +} + +MatriceCreuse MatriceCreuse::operator-(MatriceCreuse other) +{ + if(!this->isSameDimension(other)) + throw "Could not '-' two matrixes with different dimensions!"; + auto newMatrix = *this; + for(uint16_t x(0); x < _column; x++) + { + for(uint16_t y(0); y < _line; y++) + newMatrix(x,y) = (*this)(x,y) - other(x,y); + } + return newMatrix; +} + +MatriceCreuse& MatriceCreuse::operator+=(int64_t val) +{ + for(uint16_t x(0); x < _column; x++) + { + for(uint16_t y(0); y < _line; y++) + (*this)(x,y) = (*this)(x,y) + val; + } + return *this; +} + +void MatriceCreuse::addValue(position pos, int64_t value) +{ + if((pos.x > _column-1) || (pos.y > _column-1)) + throw "Could not add value to the matrix because of matrix's length"; + _values[pos] = value; +} + +bool MatriceCreuse::isSameDimension(const MatriceCreuse& other) { return (this->_column == other._column) && (this->_line == other._line); } \ No newline at end of file