00001 #ifndef __NumVarHyperMatrix_H
00002 #define __NumVarHyperMatrix_H
00003
00004 #include "Constant.h"
00005 #include "MuteVar.h"
00006
00007 #include <vector>
00008 #include <string>
00009 #include <stdarg.h>
00010 #include <iostream>
00011
00012
00013 class Model;
00014
00016
00021 template <unsigned N>
00022 class NumVarHyperMatrix
00023 {
00024 protected:
00025 Model * model;
00026 std::vector< NumVarHyperMatrix<N-1> > NumVarFamily;
00027 MuteVar::VarType type;
00028 friend class NumVarHyperMatrix<N-1>;
00029 std::vector<unsigned> size;
00030
00031
00032 void ConstructNumVarHyperMatrix( Model & _model,
00033 MuteVar::VarType _type,
00034 const std::string & name,
00035 std::vector<unsigned> & pile,
00036 std::vector<std::string> & format,
00037 const std::string & Id = "")
00038 {
00039 model = &_model;
00040 type = _type;
00041 char buf[12];
00042 for(unsigned i = 0; i < pile[N-1]; ++i )
00043 {
00044 sprintf(buf,"%d",i);
00045 NumVarFamily.push_back( NumVarHyperMatrix<N-1>(_model,_type,name,pile,format, format[N-1] + buf+Id) );
00046 }
00047 }
00048
00049 public:
00051 NumVarHyperMatrix():model(0),type(MuteVar::FLOAT) {}
00052
00054 NumVarHyperMatrix( Model & _modele,
00055 MuteVar::VarType _type,
00056 const std::string & name,
00057 ... )
00058 {
00059 va_list pile;
00060 va_start(pile,name);
00061 for(unsigned i = 0; i < N; ++i)
00062 {
00063 unsigned p = va_arg(pile,unsigned);
00064 size.push_back( p );
00065 }
00066
00067 std::string nom;
00068 std::vector< std::string > format;
00069 unsigned i = 0;
00070 while( i < name.length() && name[i] != '%')
00071 {
00072 nom += name[i];
00073 ++i;
00074 }
00075 if( i < name.length() )
00076 {
00077 std::string tmp;
00078 while( i < name.length() )
00079 {
00080 if( name[i] == '%' && (i + 1 < name.length() && name[i+1] == 'd'))
00081 {
00082 format.push_back(tmp);
00083 ++i;
00084 }
00085 else
00086 tmp += name[i];
00087 ++i;
00088 }
00089 }
00090 while(format.size() < N)
00091 {
00092 format.push_back("");
00093 }
00094
00095 ConstructNumVarHyperMatrix(_modele,_type,nom,size,format);
00096 }
00097
00098 NumVarHyperMatrix( Model & _model,
00099 MuteVar::VarType _type,
00100 const std::string & name,
00101 std::vector<unsigned> & pile,
00102 std::vector<std::string> & format,
00103 const std::string & Id = "")
00104 {
00105 ConstructNumVarHyperMatrix( _model,_type,name,pile,format,Id);
00106 }
00107
00108 NumVar operator() (unsigned n, ...) const
00109 {
00110 va_list pile;
00111 va_start(pile,n);
00112 std::vector<unsigned> v;
00113 v.push_back( n );
00114 for(unsigned i = 0; i < N-1; ++i)
00115 {
00116 unsigned p = va_arg(pile,unsigned);
00117 v.push_back( p );
00118 }
00119 return NumVarFamily[v.back()](v);
00120 }
00121
00122 NumVar operator() ( const std::vector<unsigned> & pile) const
00123 {
00124 return (NumVarFamily[pile[N-1]])(pile);
00125 }
00126
00127 std::vector<unsigned> Size() const
00128 {
00129 return size;
00130 }
00131
00132 unsigned Size(unsigned dimension) const
00133 {
00134 return size[dimension];
00135 }
00136
00137 MuteVar::VarType GetType() const { return type;}
00138
00139
00140 ~NumVarHyperMatrix() {}
00141 };
00142
00144
00149 template<>
00150 class NumVarHyperMatrix<0>
00151 {
00152 protected:
00153 NumVar var;
00154
00155
00156 public:
00157 NumVarHyperMatrix( Model & _model,
00158 MuteVar::VarType _type,
00159 const std::string & name,
00160 std::vector<unsigned> & pile = std::vector<unsigned>(),
00161 std::vector<std::string> & format,
00162 const std::string & Id = "" )
00163 {
00164 var = NumVar(_model,0,Infinity,_type, name+Id );
00165 }
00166
00167 NumVar operator() ( const std::vector<unsigned> & pile) const
00168 {
00169 return var;
00170 }
00171
00172 ~NumVarHyperMatrix() {}
00173 };
00174
00175 #endif