realroot_doc 0.1.1
/Users/mourrain/Devel/mmx/realroot/src/variables.cpp
Go to the documentation of this file.
00001 #include <cstdio>
00002 #include <stdlib.h>
00003 #include <realroot/variables.hpp>
00004 
00005 //======================================================================
00006 namespace mmx {
00007 //======================================================================
00008 
00009 variables variables::default_;
00010 
00011 std::string variables::operator[](int i)// const
00012 {
00013   if(_var_of_index[i+1]!="")
00014     return _var_of_index[i+1];
00015   else
00016     {
00017       int sz=1,m=1;
00018       while(m<i) {m*=10; sz++;}
00019       char str[sz];
00020       sprintf(str,"%d",i);
00021       return std::string("x")+str;
00022     }
00023 }
00024 
00025 std::string variables::operator[](int i) const
00026 {
00027   std::map<int,std::string>::const_iterator pt=_var_of_index.find(i+1);
00028   if(pt!= _var_of_index.end())
00029     return pt->second;
00030   else
00031     {
00032       int sz=1,m=1;
00033       while(m<i) {m*=10; sz++;}
00034       char str[sz];
00035       sprintf(str,"%d",i);
00036       return std::string("x")+str;
00037     }
00038 }
00039 
00040 std::string& variables::let(int i, const std::string& s)
00041 {
00042   _index_of_var[s]=i+1;
00043   _var_of_index[i+1]=s;
00044   if(i+1>_nbvar) _nbvar=i+1;
00045   return  _var_of_index[i+1];
00046 }
00047 
00048 variables& variables::operator=(const char* s)
00049 {
00050   std::string  v;
00051   _nbvar=0;
00052   std::istringstream ins(s);
00053   while(ins.good())
00054     {
00055       ins >>v;
00056       this->let(_nbvar,v);
00057     }
00058   return *this;
00059 }
00060 
00061 int variables::operator[](const std::string& s)
00062 {
00063   //  std::cout  <<">> "<<s<<" "<<_nbvar<<" ";
00064   if(s[0]=='x' && s.size()>1 )
00065     {
00066       const char* var=s.data()+1;
00067       //   _nbvar= std::max((int)_nbvar,atoi(var)+1);
00068       //   std::cout<<":"<<atoi(var)<<std::endl;
00069       return atoi(var);
00070     }
00071   else
00072     {
00073       if(_nbvar==0)
00074         {
00075           _nbvar++;
00076           _index_of_var[s]=_nbvar;
00077           _var_of_index[_nbvar]=s;
00078           return 0;
00079         }
00080       else 
00081         {
00082           int i=_index_of_var[s]-1;
00083           if(i<0)
00084             {
00085               while(_var_of_index[_nbvar]!="")
00086                 {
00087                   _nbvar++;
00088                 }
00089               i=_nbvar-1;
00090               //   std::cout<<"I  "<<i<<std::endl;
00091               _index_of_var[s]=_nbvar;
00092               _var_of_index[_nbvar]=s;
00093             }
00094           return i;
00095         }
00096     }
00097 }
00098 
00099 //======================================================================
00100 } //namespace mmx 
00101 //======================================================================