Go to the documentation of this file.00001
00002
00003 inline double dssmatrix::operator()(const long& i, const long& j) const
00004 {VERBOSE_REPORT;
00005 #ifdef CPPL_DEBUG
00006 if( i<0 || j<0 || n<=i || n<=j ){
00007 ERROR_REPORT;
00008 std::cerr << "The required component is out of the matrix size." << std::endl
00009 << "Your input was (" << i << "," << j << ")." << std::endl;
00010 exit(1);
00011 }
00012 #endif//CPPL_DEBUG
00013
00014
00015 const uint32_t ii(std::max(i,j)), jj(std::min(i,j));
00016 for(std::vector<uint32_t>::const_iterator p=line[ii].begin(); p!=line[ii].end(); p++){
00017 if(data[*p].i==ii && data[*p].j==jj){ return data[*p].v; }
00018 }
00019
00020
00021 return 0.0;
00022 }
00023
00024
00025
00026 inline double& dssmatrix::operator()(const long& i, const long& j)
00027 {VERBOSE_REPORT;
00028 #ifdef CPPL_DEBUG
00029 if( i<0 || j<0 || n<=i || n<=j ){
00030 ERROR_REPORT;
00031 std::cerr << "The required component is out of the matrix size." << std::endl
00032 << "Your input was (" << i << "," << j << ")." << std::endl;
00033 exit(1);
00034 }
00035 #endif//CPPL_DEBUG
00036
00037
00038 const uint32_t ii(std::max(i,j)), jj(std::min(i,j));
00039 for(std::vector<uint32_t>::iterator p=line[ii].begin(); p!=line[ii].end(); p++){
00040 if(data[*p].i==ii && data[*p].j==jj){ return data[*p].v; }
00041 }
00042
00043
00044 line[ii].push_back(data.size());
00045 if(i!=j){
00046 line[jj].push_back(data.size());
00047 }
00048 data.push_back(dcomponent(ii,jj,0.));
00049 return data.back().v;
00050 }
00051
00052
00053
00054
00055
00056
00057
00058 inline dssmatrix& dssmatrix::put(const long& i, const long& j, const double& v)
00059 {VERBOSE_REPORT;
00060 #ifdef CPPL_DEBUG
00061 if( i<0 || j<0 || n<=i || n<=j ){
00062 ERROR_REPORT;
00063 std::cerr << "The required component is out of the matrix size." << std::endl
00064 << "Your input was (" << i << "," << j << ")." << std::endl;
00065 exit(1);
00066 }
00067
00068 if( isListed(i,j) ){
00069 ERROR_REPORT;
00070 std::cerr << "The required component is already listed." << std::endl
00071 << "Your input was (" << i << "," << j << ")." << std::endl;
00072 exit(1);
00073 }
00074 #endif//CPPL_DEBUG
00075
00076
00077 const long ii(std::max(i,j)), jj(std::min(i,j));
00078 line[ii].push_back(data.size());
00079 if(i!=j){
00080 line[jj].push_back(data.size());
00081 }
00082 data.push_back(dcomponent(ii,jj,v));
00083 return *this;
00084 }
00085
00086
00087
00088
00089
00090
00091
00092 inline dssmatrix& dssmatrix::del(const long i, const long j)
00093 {VERBOSE_REPORT;
00094 #ifdef CPPL_DEBUG
00095 if( i<0 || j<0 || n<=i || n<=j ){
00096 ERROR_REPORT;
00097 std::cerr << "The required component is out of the matrix size." << std::endl
00098 << "Your input was (" << i << "," << j << ")." << std::endl;
00099 exit(1);
00100 }
00101 #endif//CPPL_DEBUG
00102
00103 const long ii(std::max(i,j)), jj(std::min(i,j));
00104
00105
00106 for(std::vector<uint32_t>::iterator p=line[ii].begin(); p!=line[ii].end(); p++){
00107 if(long(data[*p].i)==ii && long(data[*p].j)==jj){
00108
00109 uint32_t c(*p);
00110 uint32_t C(data.size()-1);
00111
00112
00113 data[c]=data.back();
00114 data.pop_back();
00115
00116
00117 line[ii].erase(p);
00118 if(i!=j){
00119 for(std::vector<uint32_t>::iterator pj=line[jj].begin(); pj!=line[jj].end(); pj++){
00120 if(*pj==c){ line[jj].erase(pj); break; }
00121 }
00122 }
00123
00124
00125 uint32_t I(data[c].i), J(data[c].j);
00126 for(std::vector<uint32_t>::iterator q=line[I].begin(); q!=line[I].end(); q++){
00127 if(*q==C){ *q=c; break; }
00128 }
00129 if(I!=J){
00130 for(std::vector<uint32_t>::iterator q=line[J].begin(); q!=line[J].end(); q++){
00131 if(*q==C){ *q=c; break; }
00132 }
00133 }
00134 return *this;
00135 }
00136 }
00137
00138 #ifdef CPPL_DEBUG
00139 std::cerr << "# [NOTE]@dssmatrix::del(long&, long&): The required component was not listed. Your input was (" << i << "," << j << ")." << std::endl;
00140 #endif//CPPL_DEBUG
00141
00142 return *this;
00143 }
00144
00145
00146
00147 inline dssmatrix& dssmatrix::del(const long c)
00148 {VERBOSE_REPORT;
00149 #ifdef CPPL_DEBUG
00150 if( c<0 || c>=long(data.size()) ){
00151 ERROR_REPORT;
00152 std::cerr << "The required element is out of the matrix volume." << std::endl
00153 << "Your input was (" << c << ")." << std::endl;
00154 exit(1);
00155 }
00156 #endif//CPPL_DEBUG
00157
00158 if( c==long(data.size()-1) ){
00159 long i(data[c].i), j(data[c].j);
00160 for(std::vector<uint32_t>::iterator q=line[i].begin(); q!=line[i].end(); q++){
00161 if(long(*q)==c){ line[i].erase(q); break; }
00162 }
00163 if(i!=j){
00164 for(std::vector<uint32_t>::iterator q=line[j].begin(); q!=line[j].end(); q++){
00165 if(long(*q)==c){ line[j].erase(q); break; }
00166 }
00167 }
00168 data.pop_back();
00169 }
00170
00171 else{
00172
00173 uint32_t C(data.size()-1);
00174 long i(data[c].i), j(data[c].j), I(data.back().i), J(data.back().j);
00175 data[c]=data.back();
00176
00177
00178
00179 for(std::vector<uint32_t>::iterator q=line[i].begin(); q!=line[i].end(); q++){
00180 if(long(*q)==c){ line[i].erase(q); break; }
00181 }
00182 if(i!=j){
00183 for(std::vector<uint32_t>::iterator q=line[j].begin(); q!=line[j].end(); q++){
00184 if(long(*q)==c){ line[j].erase(q); break; }
00185 }
00186 }
00187
00188
00189 for(std::vector<uint32_t>::iterator q=line[I].begin(); q!=line[I].end(); q++){
00190 if(*q==C){ *q=c; break; }
00191 }
00192 if(I!=J){
00193 for(std::vector<uint32_t>::iterator q=line[J].begin(); q!=line[J].end(); q++){
00194 if(*q==C){ *q=c; break; }
00195 }
00196 }
00197
00198
00199 data.pop_back();
00200 }
00201
00202 return *this;
00203 }
00204
00205
00206
00207
00208
00209
00210 inline std::ostream& operator<<(std::ostream& s, const dssmatrix& mat)
00211 {VERBOSE_REPORT;
00212 for(long i=0; i<mat.n; i++){
00213 for(long j=0; j<mat.n; j++){
00214 if( i >= j ){
00215 long c =mat.number(i,j);
00216 if(c<0){
00217 s << " x ";
00218 }
00219 else{
00220 s << " " << mat.data[c].v << " ";
00221 }
00222 }
00223 else{
00224 long c =mat.number(i,j);
00225 if(c<0){
00226 s << "{x}";
00227 }
00228 else{
00229 s << "{" << mat.data[c].v << "}";
00230 }
00231 }
00232 }
00233 s << std::endl;
00234 }
00235
00236 return s;
00237 }
00238
00239
00240
00241
00242
00243
00244 inline void dssmatrix::write(const char* filename) const
00245 {VERBOSE_REPORT;
00246 std::ofstream ofs(filename, std::ios::trunc);
00247 ofs.setf(std::cout.flags());
00248 ofs.precision(std::cout.precision());
00249 ofs.width(std::cout.width());
00250 ofs.fill(std::cout.fill());
00251
00252 ofs << "#dssmatrix" << " " << n << std::endl;
00253 for(std::vector<dcomponent>::const_iterator it=data.begin(); it!=data.end(); it++){
00254 ofs << it->i << " " << it->j << " " << it->v << std::endl;
00255 }
00256
00257 ofs.close();
00258 }
00259
00260
00261 inline void dssmatrix::read(const char* filename)
00262 {VERBOSE_REPORT;
00263 std::ifstream s( filename );
00264 if(!s){
00265 ERROR_REPORT;
00266 std::cerr << "The file \"" << filename << "\" can not be opened." << std::endl;
00267 exit(1);
00268 }
00269
00270 std::string id;
00271 s >> id;
00272 if( id != "dssmatrix" && id != "#dssmatrix" ){
00273 ERROR_REPORT;
00274 std::cerr << "The type name of the file \"" << filename << "\" is not dssmatrix." << std::endl
00275 << "Its type name was " << id << " ." << std::endl;
00276 exit(1);
00277 }
00278
00279 s >> n;
00280 resize(n);
00281
00282 double val;
00283 long i, j, pos, tmp;
00284 while(!s.eof()){
00285 s >> i >> j >> val;
00286 put(i,j, val);
00287 pos =s.tellg();
00288 s >> tmp;
00289 s.seekg(pos);
00290 }
00291
00292 if(!s.eof()){
00293 ERROR_REPORT;
00294 std::cerr << "There is something is wrong with the file \"" << filename << " ." << std::endl
00295 << "Most likely, there are too many data components over the context." << std::endl;
00296 exit(1);
00297 }
00298 s.close();
00299 }