Modelib: Fichier source de MatriceCreuse.h

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.h

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 #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     // En C# : // tout ca pour dire que le C# c beau !
    00123     // public unsigned nbLignes {
    00124     //     get { return nbLignes }
    00125     //     set { nbLignes = value }
    00126     // }
    00127     // Là, en C++ on est obligé de faire ces saloperies
    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