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