00001 #ifndef __MATRICE_CREUSE_H
00002 #define __MATRICE_CREUSE_H
00003
00004 #include "Config.h"
00005 #include <map>
00006 #include <iostream>
00007 #include <string>
00008 #include <fstream>
00009 #include <typeinfo>
00010 #include <algorithm>
00011 #include <utility>
00012 #include <stdexcept>
00013
00014
00016 struct Id {
00017 unsigned i;
00018 unsigned j;
00020 Id(const unsigned _i, const unsigned _j) : i(_i), j(_j) {};
00021 };
00022
00023
00024
00026 template<typename T>
00027 struct Compare
00028 {
00029 Compare() {};
00035 bool operator()(const Id& x, const Id& y) const
00036 {
00037 return (x.j < y.j)||(x.j==y.j && x.i <y.i);
00038 }
00039 };
00040
00041
00043
00048 template<typename T>
00049 class MatriceCreuse
00050 {
00051 protected:
00052
00053 unsigned nbCols;
00054 unsigned nbLignes;
00055 std::map<Id,T,Compare<T> > matrix;
00056 T defaultT;
00057
00058 friend class Model;
00059
00060 public:
00062 MatriceCreuse( T _defaultT = 0 )
00063 : nbCols(0), nbLignes(0), defaultT( _defaultT )
00064 {}
00065
00066 MatriceCreuse(const std::string& nom,T _defaultT = 0);
00067 bool Open(const std::string& nom);
00068
00069 T operator() (unsigned i, unsigned j) const;
00070 T operator() (unsigned i, unsigned j) ;
00071 T Get (unsigned i, unsigned j) const;
00072
00079 inline
00080 void Set (unsigned i, unsigned j, const T& value)
00081 {
00082 const Id S(i,j);
00083 matrix[S] = value;
00084
00085 if (i>nbCols) nbCols = i;
00086 if (j>nbLignes) nbLignes= j;
00087 }
00088
00089
00090
00091
00092
00093
00094
00095
00099 inline unsigned GetNbLignes() const { return nbLignes; }
00100 inline unsigned GetNbLignes() { return nbLignes; }
00104 inline unsigned GetNbCols() const { return nbCols; }
00105 inline unsigned GetNbCols() { return nbCols; }
00109 inline unsigned GetSize() const { return matrix.size(); }
00110 inline unsigned GetSize() { return matrix.size(); }
00111
00116 inline double Occupation()
00117 {
00118 return static_cast<double>(double(matrix.size()) / double(nbCols * nbLignes));
00119 }
00120
00123 void Out()
00124 {
00125 unsigned i;
00126 for (unsigned j=1;j<=nbLignes;j++)
00127 {
00128 for (i=1;i<=nbCols;i++)
00129 {
00130 std::cout << Get(i,j);
00131 std::cout << ',';
00132 }
00133 std::cout << std::endl;
00134 }
00135 }
00136
00137 ~MatriceCreuse()
00138 {
00139 }
00140 };
00141
00142 #ifdef __no_export
00143 #include "MatriceCreuse.cxx"
00144 #endif
00145
00146 #endif
00147