TP2: Partie 2, Question1.
This commit is contained in:
parent
80ba7d5b0d
commit
e9cdc77d13
3 changed files with 178 additions and 0 deletions
48
tp/tp2/include/partie2.h
Normal file
48
tp/tp2/include/partie2.h
Normal file
|
@ -0,0 +1,48 @@
|
|||
#ifndef HEADER_PARTIE2
|
||||
#define HEADER_PARTIE2
|
||||
|
||||
#include <map>
|
||||
#include <cstdint>
|
||||
#include <ostream>
|
||||
#include <vector>
|
||||
|
||||
struct position {
|
||||
uint16_t x;
|
||||
uint16_t y;
|
||||
|
||||
/**
|
||||
* @brief Used by std::map.
|
||||
*
|
||||
* @param other the other position to check.
|
||||
* @return true Returns <x, if x is equal <y
|
||||
*/
|
||||
bool operator<(const position& other) const {
|
||||
return (x!=other.x ? x<other.x : y<other.y);
|
||||
}
|
||||
};
|
||||
|
||||
class MatriceCreuse {
|
||||
public:
|
||||
MatriceCreuse(uint16_t lines, uint16_t columns);
|
||||
MatriceCreuse(uint16_t lines, uint16_t columns, const std::map<position,int64_t>& values);
|
||||
MatriceCreuse(uint16_t lines, uint16_t columns, const std::vector<position>& 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<position,int64_t> _values;
|
||||
};
|
||||
|
||||
#endif // HEADER_PARTIE2
|
|
@ -7,6 +7,7 @@
|
|||
#include <vector>
|
||||
#include <list>
|
||||
#include <map>
|
||||
#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;
|
||||
}
|
||||
|
||||
|
|
106
tp/tp2/src/partie2.cpp
Normal file
106
tp/tp2/src/partie2.cpp
Normal file
|
@ -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<position,int64_t>& 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<position>& 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); }
|
Loading…
Add table
Reference in a new issue