00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #ifndef __MATRICE_CREUSE_H
00033 #define __MATRICE_CREUSE_H
00034
00035 #include "Config.h"
00036 #include <map>
00037 #include <iostream>
00038 #include <string>
00039 #include <fstream>
00040 #include <typeinfo>
00041 #include <algorithm>
00042 #include <utility>
00043 #include <stdexcept>
00044
00045
00046 namespace Modelib {
00047
00049 struct Id {
00050 unsigned i;
00051 unsigned j;
00053 Id(const unsigned _i, const unsigned _j) : i(_i), j(_j) {};
00054 };
00055
00056
00057
00059 template<typename T>
00060 struct Compare
00061 {
00062 Compare() {};
00068 bool operator()(const Id& x, const Id& y) const
00069 {
00070 return (x.j < y.j)||(x.j==y.j && x.i <y.i);
00071 }
00072 };
00073
00074
00076
00081 template<typename T>
00082 class MatriceCreuse
00083 {
00084 protected:
00085
00086 unsigned nbCols;
00087 unsigned nbLignes;
00088 std::map<Id,T,Compare<T> > matrix;
00089 T defaultT;
00090
00091 friend class Model;
00092
00093 public:
00095 MatriceCreuse( T _defaultT = 0 )
00096 : nbCols(0), nbLignes(0), defaultT( _defaultT )
00097 {}
00098
00099 MatriceCreuse(const std::string& nom,T _defaultT = 0);
00100 bool Open(const std::string& nom);
00101
00102 T operator() (unsigned i, unsigned j) const;
00103 T operator() (unsigned i, unsigned j) ;
00104 T Get (unsigned i, unsigned j) const;
00105
00112 inline
00113 void Set (unsigned i, unsigned j, const T& value)
00114 {
00115 const Id S(i,j);
00116 matrix[S] = value;
00117
00118 if (i>nbCols) nbCols = i;
00119 if (j>nbLignes) nbLignes= j;
00120 }
00121
00122
00123
00124
00125
00126
00127
00128
00132 inline unsigned GetNbLignes() const { return nbLignes; }
00133 inline unsigned GetNbLignes() { return nbLignes; }
00137 inline unsigned GetNbCols() const { return nbCols; }
00138 inline unsigned GetNbCols() { return nbCols; }
00142 inline unsigned GetSize() const { return matrix.size(); }
00143 inline unsigned GetSize() { return matrix.size(); }
00144
00149 inline double Occupation()
00150 {
00151 return static_cast<double>(double(matrix.size()) / double(nbCols * nbLignes));
00152 }
00153
00156 void Out()
00157 {
00158 unsigned i;
00159 for (unsigned j=1;j<=nbLignes;j++)
00160 {
00161 for (i=1;i<=nbCols;i++)
00162 {
00163 std::cout << Get(i,j);
00164 std::cout << ',';
00165 }
00166 std::cout << std::endl;
00167 }
00168 }
00169
00170 ~MatriceCreuse()
00171 {
00172 }
00173 };
00174
00175 #ifdef __no_export
00176 #include "MatriceCreuse.cxx"
00177 #endif
00178
00179 }
00180
00181 #endif
00182