Modelib: Fichier source de MatriceCreuse.cxx

Page principale | Liste des namespaces | Hiérarchie des classes | Liste alphabétique | Liste des classes | Liste des fichiers | Membres de namespace | Membres de classe | Membres de fichier

# MatriceCreuse.cxx

Aller à la documentation de ce fichier.

    00001 /*
    00002     Copyright (c) 2005, Quentin Lequy and Romain Gaucher
    00003     All rights reserved.
    00004
    00005     Redistribution and use in source and binary forms, with or
    00006     without modification, are permitted provided that the following
    00007     conditions are met:
    00008
    00009         * Redistributions of source code must retain the above copyright
    00010           notice, this list of conditions and the following disclaimer.
    00011
    00012         * Redistributions in binary form must reproduce the above copyright
    00013           notice, this list of conditions and the following disclaimer in the
    00014           documentation and/or other materials provided with the distribution.
    00015
    00016         * Neither the name of Quentin Lequy or Romain Gaucher nor the names
    00017           of its contributors may be used to endorse or promote products
    00018           derived from this software without specific prior written permission.
    00019
    00020     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    00021     AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    00022     IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    00023     ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
    00024     LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
    00025     DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
    00026     SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
    00027     AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    00028     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
    00029     SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    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     // ouvrir le fichier nom
    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     // boucler sur les lignes
    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   // ouvrir le fichier nom
    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   // boucler sur les lignes
    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 // --------------- Hors classe -------------
    00177 /*
    00178 __export_template<typename T>
    00179 ostream& operator<<(ostream& o, const MatriceCreuse<T>& mat)
    00180 {
    00181   unsigned i;
    00182   for (unsigned j=0;j<mat.GetnbLignes();j++)
    00183   {
    00184     for (i=0;i<mat.GetnbCols();i++)
    00185     {
    00186         o << mat(i,j);
    00187         o << ',';
    00188     }
    00189     o << endl;
    00190   }
    00191   return o;
    00192 }
    00193 */