1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
| #ifndef TABLEAU_H_INCLUDED
#define TABLEAU_H_INCLUDED
#include <assert.h>
#include <vector>
template <typename T>
class Tab2D{
public:
Tab2D(int sizx, int sizy=1): _size_x(sizx), _size_y(sizy){
_tab = std::vector< std::vector< T > > (_size_x);
for (int i=0; i<_size_x; i++)
(_tab[i]).resize(_size_y);
};
// Constructeur de copie
Tab2D(const Tab2D& tab2){
_size_x=tab2.size(1); _size_y=tab2.size(2);
_tab = std::vector< std::vector <T> > (_size_x);
for (int i=0; i<_size_x; i++){
(_tab[i]).resize(_size_y);
for(int j=0; j<_size_y; j++)
_tab[i][j]=tab2(i,j);
}
};
~Tab2D(){ };
/// Surcharge de la fonction size de std::vector
int size(int dir) const {
int res=0;
assert(dir==1 || dir==2);
res=_tab.size(); //size_x
if ( dir==2 && res>0)
res=_tab[0].size(); //size_y
return res;
}
void print(std::ostream &flux) const{
for (int j=0; j<_size_y; j++){
for (int i=0; i<_size_x; i++){
std::cout << _tab[i][j] << " ";
}
std::cout << std::endl;
}
};
bool SameSize(Tab2D const& new_tab) const{
return (_size_x==new_tab._size_x && _size_y==new_tab._size_y);
}
bool estEgal(Tab2D const& b, int debug=0) const{
bool res=false;
if (SameSize(b)){
res=true;
for (int i=0; i<_size_x && res; i++){ // && res, car le break ne sort que de la premiere boucle !
for (int j=0; j<_size_y; j++){
if ( b(i,j)!= _tab[i][j]) {
res=false;
if (debug==1){
std::cout << "Première difference identifiée :" << std::endl;
std::cout << "(" <<i << "," << j << ") --> " << _tab[i][j] << " / " << b(i,j) << std::endl;
}
break;
}
}
}
} else if (debug==1){
std::cout << "Tableau qui ne sont pas de même taille :" << std::endl;
std::cout << _size_x << " "<< _size_y << " / " << b.size(1) << " " << b.size(2) << std::endl;
}
return res;
};
// ------- Opérateur d'indexation -----
T & operator()(int index, int index2){return _tab[index][index2];}
const T & operator()(int index, int index2) const{return _tab[index][index2];}
/// Permet de pouvoir ecrire : tab = 3
void operator=( T nombre ){
for(int i=0 ; i<_size_x; i++){
for(int j=0 ; j<_size_y; j++)
_tab[i][j]=nombre;
}
};
/// Permet de pouvoir ecrire : tab1 += tab2; /// A declarer dans la classe
Tab2D& operator+=( Tab2D const& tab2){
assert(SameSize(tab2));
for(int i=0 ; i<_size_x; i++){
for(int j=0 ; j<_size_y; j++)
_tab[i][j]+=tab2(i,j);
}
return *this;
};
/// Permet de pouvoir ecrire : tab1 -= tab2; /// A declarer dans la classe
Tab2D& operator-=( Tab2D const& tab2){
assert(SameSize(tab2));
for(int i=0 ; i<_size_x; i++){
for(int j=0 ; j<_size_y; j++)
_tab[i][j]-=tab2(i,j);
}
return *this;
};
//// Permet de pouvoir ecrire : tab1 += 30 ;
Tab2D& operator+=(T nb){
for(int i=0 ; i<_size_x; i++){
for(int j=0 ; j<_size_y; j++)
_tab[i][j]+=nb;
}
return *this;
};
//// Permet de pouvoir ecrire : tab1 -= 30 ;
Tab2D& operator-=(T nb){
for(int i=0 ; i<_size_x; i++){
for(int j=0 ; j<_size_y; j++)
_tab[i][j]-=nb;
}
return *this;
};
private:
int _size_x,_size_y;
std::vector< std::vector <T> > _tab;
};
///// ------------- Opérateur déclarer à l'extérieur de la classe -------------------
template<typename T>
inline std::ostream& operator<<(std::ostream &flux, const Tab2D<T> &tab){
tab.print(flux);
return flux;
}
/// Permet de pouvoir ecrire : tab1 + tab2;
template<typename T>
Tab2D<T> operator+(Tab2D<T> const& a, Tab2D<T> const& b){
Tab2D<T> c(a); //on fait appel au constructeur de copie
c+=b;
return c;
};
/// Permet de pouvoir ecrire : tab1 - tab2;
template<typename T>
Tab2D<T> operator-(Tab2D<T> const& a, Tab2D<T> const& b){
Tab2D<T> c(a); //on fait appel au constructeur de copie
c-=b;
return c;
};
/// Permet de pouvoir ecrire : tab1 + nb;
template<typename T>
Tab2D<T> operator+(Tab2D<T> const& a, T const& b){
Tab2D<T> c(a); //on fait appel au constructeur de copie
c+=b;
return c;
};
/// Permet de pouvoir ecrire : tab1 - nb;
template<typename T>
Tab2D<T> operator-(Tab2D<T> const& a, T const& b){
Tab2D<T> c(a); //on fait appel au constructeur de copie
c-=b;
return c;
};
/// Pour comparer deux tableaux
template<typename T>
bool operator==(Tab2D<T> const& a, Tab2D<T> const& b){
return a.estEgal(b,1);
};
#endif // TABLEAU_H_INCLUDED |
Partager