Modelib: Fichier source de NumVarHyperMatrix.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

# NumVarHyperMatrix.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 __NumVarHyperMatrix_H
    00033 #define __NumVarHyperMatrix_H
    00034
    00035 #include "Constant.h"
    00036 #include "MuteVar.h"
    00037
    00038 #include <vector>
    00039 #include <string>
    00040 #include <stdarg.h>
    00041 #include <iostream>
    00042
    00043 namespace Modelib {
    00044
    00045 //Pour éviter les définitions circulaire
    00046 class Model;
    00047
    00049
    00054 template <unsigned N>
    00055 class NumVarHyperMatrix
    00056 {
    00057   protected:
    00058   Model * model;
    00059   std::vector< NumVarHyperMatrix<N-1> > NumVarFamily;
    00060   MuteVar::VarType type;
    00061   friend class NumVarHyperMatrix<N-1>;
    00062   std::vector<unsigned> size;
    00063
    00064
    00065   void ConstructNumVarHyperMatrix( Model & _model,
    00066                                 MuteVar::VarType _type,
    00067                                 const std::string & name,
    00068                                 std::vector<unsigned> & pile,
    00069                                 std::vector<std::string> & format,
    00070                                 const std::string & Id = "")
    00071   {
    00072       model = &_model;
    00073         type = _type;
    00074       char buf[12];
    00075       for(unsigned i = 0; i < pile[N-1]; ++i )
    00076       {
    00077         sprintf(buf,"%d",i);
    00078         NumVarFamily.push_back( NumVarHyperMatrix<N-1>(_model,_type,name,pile,format, format[N-1] + buf+Id) );
    00079       }
    00080    }
    00081
    00082   public:
    00084     NumVarHyperMatrix():model(0),type(MuteVar::FLOAT) {}
    00085
    00087     NumVarHyperMatrix( Model & _modele,
    00088                   MuteVar::VarType _type,
    00089                   const std::string & name,
    00090                   ... )
    00091     {
    00092       va_list pile;
    00093         va_start(pile,name);
    00094       for(unsigned i = 0; i < N; ++i)
    00095       {
    00096         unsigned p = va_arg(pile,unsigned);
    00097         size.push_back( p );
    00098       }
    00099
    00100       std::string nom;
    00101       std::vector< std::string > format;
    00102       unsigned i = 0;
    00103       while( i < name.length() && name[i] != '%')
    00104       {
    00105         nom += name[i];
    00106         ++i;
    00107       }
    00108       if( i < name.length() )
    00109       {
    00110          std::string tmp;
    00111          while( i < name.length() )
    00112          {
    00113               if( name[i] == '%' && (i + 1 < name.length() && name[i+1] == 'd'))
    00114             {
    00115                 format.push_back(tmp);
    00116               ++i;
    00117             }
    00118             else
    00119               tmp += name[i];
    00120             ++i;
    00121          }
    00122       }
    00123       while(format.size() < N)
    00124       {
    00125         format.push_back("");
    00126       }
    00127
    00128       ConstructNumVarHyperMatrix(_modele,_type,nom,size,format);
    00129     }
    00130
    00131     NumVarHyperMatrix( Model & _model,
    00132                   MuteVar::VarType _type,
    00133                   const std::string & name,
    00134                   std::vector<unsigned> & pile,
    00135                   std::vector<std::string> & format,
    00136                   const std::string & Id = "")
    00137     {
    00138        ConstructNumVarHyperMatrix( _model,_type,name,pile,format,Id);
    00139     }
    00140
    00141     NumVar operator()  (unsigned n, ...) const
    00142     {
    00143       va_list pile;
    00144         va_start(pile,n);
    00145         std::vector<unsigned> v;
    00146       v.push_back( n );
    00147       for(unsigned i = 0; i < N-1; ++i)
    00148       {
    00149         unsigned p = va_arg(pile,unsigned);
    00150         v.push_back( p );
    00151       }
    00152       return NumVarFamily[v.back()](v);
    00153     }
    00154
    00155     NumVar operator()  ( const std::vector<unsigned> & pile) const
    00156     {
    00157         return (NumVarFamily[pile[N-1]])(pile);
    00158     }
    00159
    00160     std::vector<unsigned> Size() const
    00161     {
    00162          return size;
    00163     }
    00164
    00165     unsigned Size(unsigned dimension) const
    00166     {
    00167          return size[dimension];
    00168     }
    00169
    00170     MuteVar::VarType GetType() const { return type;}
    00171
    00172
    00173     ~NumVarHyperMatrix() {}
    00174 };
    00175
    00177
    00182 template<>
    00183 class NumVarHyperMatrix<0>
    00184 {
    00185    protected:
    00186       NumVar var;
    00187
    00188
    00189    public:
    00190       NumVarHyperMatrix( Model & _model,
    00191                   MuteVar::VarType _type,
    00192                   const std::string & name,
    00193                   std::vector<unsigned> & pile = std::vector<unsigned>(),
    00194                   std::vector<std::string> & format,
    00195                   const std::string & Id = "" )
    00196      {
    00197         var = NumVar(_model,0,Infinity,_type, name+Id );
    00198      }
    00199
    00200      NumVar operator()  ( const std::vector<unsigned> & pile) const
    00201      {
    00202           return var;
    00203      }
    00204
    00205     ~NumVarHyperMatrix() {}
    00206 };
    00207
    00208 }
    00209
    00210 #endif