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
00033 #ifdef __MATRICE_CREUSE_H
00034 #define __export_template template
00035 #else
00036 #include "MatriceCreuse.h"
00037
00038 #include <map>
00039 #include <iostream>
00040 #include <string>
00041 #include <list>
00042 #include <fstream>
00043 #include <sstream>
00044
00045 #define __export_template export template
00046 #endif
00047
00048 using namespace std;
00049
00056 __export_template<typename T>
00057 MatriceCreuse<T>::MatriceCreuse(const string& nom, T _defaultT)
00058 : nbCols(0), nbLignes(0), defaultT( _defaultT )
00059 {
00060
00061 ifstream ifile(nom.c_str());
00062
00063 if(!ifile.is_open())
00064 throw runtime_error("MatriceCreuse<T>::MatriceCreuse(const string& nom, T _defaultT) : IO Error");
00065
00066
00067 unsigned _i=0,_j=0,_im=0,_jm=0;
00068 T _val = defaultT;
00069
00070 while (ifile >> _i >> _j >> _val)
00071 {
00072 Id S(_i,_j);
00073 if (!(_val == defaultT))
00074 {
00075 matrix[S] = _val;
00076 }
00077
00078 if (_i > _im) _im = _i;
00079 if (_j > _jm) _jm = _j;
00080
00081 }
00082 ifile.close();
00083
00084 nbCols = _im;
00085 nbLignes = _jm;
00086 }
00087
00094 __export_template<typename T>
00095 bool MatriceCreuse<T>::Open(const string& nom)
00096 {
00097
00098 ifstream ifile(nom.c_str());
00099
00100 if(!ifile.is_open())
00101 throw runtime_error("bool MatriceCreuse<T>::Open(const string& nom) : I/O Error");
00102
00103
00104
00105 unsigned _i=0,_j=0,_im=0,_jm=0;
00106 T _val = defaultT;
00107
00108 while (ifile >> _i >> _j >> _val)
00109 {
00110 Id S(_i,_j);
00111 if (!(_val == defaultT))
00112 {
00113 matrix[S] = _val;
00114 }
00115
00116 if (_i > _im) _im = _i;
00117 if (_j > _jm) _jm = _j;
00118
00119 }
00120 ifile.close();
00121
00122 nbCols = _im;
00123 nbLignes = _jm;
00124 return true;
00125 }
00126
00127 __export_template<typename T>
00134 T MatriceCreuse<T>::operator() (unsigned i, unsigned j)
00135 {
00136 const Id S(i,j);
00137 typename map<Id,T,Compare<T> >::const_iterator iter = matrix.find(S);
00138 if (iter != matrix.end())
00139 return iter->second;
00140 return defaultT;
00141 }
00142
00149 __export_template<typename T>
00150 T MatriceCreuse<T>::operator() (unsigned i, unsigned j) const
00151 {
00152 const Id S(i,j);
00153 typename map<Id,T,Compare<T> >::const_iterator iter = matrix.find(S);
00154 if (iter != matrix.end())
00155 return iter->second;
00156 return defaultT;
00157 }
00158
00165 __export_template<typename T>
00166 T MatriceCreuse<T>::Get(unsigned i, unsigned j) const
00167 {
00168 const Id S(i,j);
00169 typename map<Id,T,Compare<T> >::const_iterator iter = matrix.find(S);
00170 if (iter != matrix.end())
00171 return iter->second;
00172 return defaultT;
00173 }
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193