00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
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
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